1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.apphosting
.utils
.config
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Iterator
;
10 * Parsed datastore-indexes.xml file.
12 * Any additions to this class should also be made to the YAML
13 * version in IndexYamlReader.java.
16 public class IndexesXml
implements Iterable
<IndexesXml
.Index
>{
18 public class PropertySort
{
20 private String propName
;
21 private boolean ascending
;
23 public PropertySort (String propName
, boolean ascending
) {
24 this.propName
= propName
;
25 this.ascending
= ascending
;
28 public String
getPropertyName() {
32 public boolean isAscending() {
42 private boolean ancestors
;
43 private List
<PropertySort
> properties
;
45 public Index (String kind
, boolean ancestors
) {
47 this.ancestors
= ancestors
;
48 this.properties
= new ArrayList
<PropertySort
>();
51 public void addNewProperty(String name
, boolean ascending
) {
52 properties
.add(new PropertySort(name
, ascending
));
55 public String
getKind() {
59 public boolean doIndexAncestors() {
63 public List
<PropertySort
> getProperties() {
68 * Builds a Yaml String representing this index, using the style of Yaml
69 * generation appropriate for a local indexes.yaml files.
70 * @return A Yaml String
72 private String
toLocalStyleYaml(){
73 StringBuilder builder
= new StringBuilder(50 * (1 + properties
.size()));
74 builder
.append("- kind: \"" + kind
+ "\"\n");
76 builder
.append(" ancestor: yes\n");
78 builder
.append(" properties:\n");
79 for (PropertySort prop
: properties
) {
80 builder
.append(" - name: \"" + prop
.getPropertyName() + "\"\n");
81 builder
.append(" direction: " + (prop
.isAscending() ?
"asc" : "desc")+ "\n");
83 return builder
.toString();
87 * Builds a Yaml string representing this index, mimicking the style of Yaml
88 * generation used on the admin server. Since the admin server is written in
89 * python, it generates a slightly different style of yaml. This method is
90 * useful only for testing that the client-side code is able to parse this
93 * @return An admin-server-style Yaml String.
95 private String
toServerStyleYaml() {
96 StringBuilder builder
= new StringBuilder(50 * (1 + properties
.size()));
97 builder
.append("- ").append(IndexYamlReader
.INDEX_TAG
).append("\n");
98 builder
.append(" kind: " + kind
+ "\n");
100 builder
.append(" ancestor: yes\n");
102 builder
.append(" properties:\n");
103 for (PropertySort prop
: properties
) {
106 .append(IndexYamlReader
.PROPERTY_TAG
)
107 .append(" {direction: ")
108 .append((prop
.isAscending() ?
"asc" : "desc"))
111 .append("name: " + prop
.getPropertyName())
114 return builder
.toString();
117 public String
toXmlString() {
118 StringBuilder builder
= new StringBuilder(100 * (1 + properties
.size()));
119 builder
.append("<datastore-index kind=\"" + kind
+ "\" ancestor=\"" + ancestors
+ "\">\n");
120 for (PropertySort prop
: properties
) {
121 String direction
= (prop
.isAscending() ?
"asc" : "desc");
123 " <property name=\"" + prop
.getPropertyName() + "\" direction=\"" + direction
126 builder
.append("</datastore-index>\n");
127 return builder
.toString();
131 private List
<Index
> indexes
;
133 public IndexesXml() {
134 indexes
= new ArrayList
<Index
>();
138 public Iterator
<Index
> iterator() {
139 return indexes
.iterator();
143 return indexes
.size();
146 public Index
addNewIndex(String kind
, boolean ancestors
) {
147 Index index
= new Index(kind
, ancestors
);
153 * Adds the given {@link Index} to the collection
154 * contained in this object. Note that given {@link Index}
155 * is not cloned. The provided object instance will become
156 * incorporated into this object's collection.
159 public void addNewIndex(Index index
){
163 public String
toYaml() {
164 return toYaml(false);
168 * Builds yaml string representing the indexes
170 * @param serverStyle Use the admin server style of yaml generation. Since the
171 * admin server is written in python, it generates a slightly different
172 * style of yaml. Setting this parameter to {@code true} is useful only
173 * for testing that the client-side code is able to parse this style of
175 * @return A Yaml string.
177 public String
toYaml(boolean serverStyle
) {
178 StringBuilder builder
= new StringBuilder(1024);
180 builder
.append(IndexYamlReader
.INDEX_DEFINITIONS_TAG
).append("\n");
182 builder
.append("indexes:");
183 int numIndexes
= (null == indexes ?
0 : indexes
.size());
184 if (0 == numIndexes
&& serverStyle
) {
185 builder
.append(" []");
187 builder
.append("\n");
188 for (Index index
: indexes
) {
189 String indexYaml
= (serverStyle ? index
.toServerStyleYaml() : index
.toLocalStyleYaml());
190 builder
.append(indexYaml
);
192 return builder
.toString();