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
{
19 private String propName
;
20 private boolean ascending
;
22 public PropertySort (String propName
, boolean ascending
) {
23 this.propName
= propName
;
24 this.ascending
= ascending
;
27 public String
getPropertyName() {
31 public boolean isAscending() {
40 private boolean ancestors
;
41 private List
<PropertySort
> properties
;
43 public Index (String kind
, boolean ancestors
) {
45 this.ancestors
= ancestors
;
46 this.properties
= new ArrayList
<PropertySort
>();
49 public void addNewProperty(String name
, boolean ascending
) {
50 properties
.add(new PropertySort(name
, ascending
));
53 public String
getKind() {
57 public boolean doIndexAncestors() {
61 public List
<PropertySort
> getProperties() {
66 * Builds a Yaml String representing this index, using the style of Yaml
67 * generation appropriate for a local indexes.yaml files.
68 * @return A Yaml String
70 private String
toLocalStyleYaml(){
71 StringBuilder builder
= new StringBuilder(50 * (1 + properties
.size()));
72 builder
.append("- kind: \"" + kind
+ "\"\n");
74 builder
.append(" ancestor: yes\n");
76 if (!properties
.isEmpty()) {
77 builder
.append(" properties:\n");
78 for (PropertySort prop
: properties
) {
79 builder
.append(" - name: \"" + prop
.getPropertyName() + "\"\n");
80 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 if (!properties
.isEmpty()) {
103 builder
.append(" properties:\n");
104 for (PropertySort prop
: properties
) {
105 builder
.append(" - ");
106 builder
.append(IndexYamlReader
.PROPERTY_TAG
);
107 builder
.append(" {direction: ");
108 builder
.append((prop
.isAscending() ?
"asc" : "desc"));
109 builder
.append(",\n");
111 builder
.append("name: " + prop
.getPropertyName());
112 builder
.append("}\n");
115 return builder
.toString();
118 public String
toXmlString() {
119 StringBuilder builder
= new StringBuilder(100 * (1 + properties
.size()));
120 builder
.append("<datastore-index kind=\"" + kind
+ "\" ancestor=\"" + ancestors
+ "\">\n");
121 for (PropertySort prop
: properties
) {
122 String direction
= (prop
.isAscending() ?
"asc" : "desc");
124 " <property name=\"" + prop
.getPropertyName() + "\" direction=\"" + direction
127 builder
.append("</datastore-index>\n");
128 return builder
.toString();
132 private List
<Index
> indexes
;
134 public IndexesXml() {
135 indexes
= new ArrayList
<Index
>();
139 public Iterator
<Index
> iterator() {
140 return indexes
.iterator();
144 return indexes
.size();
147 public Index
addNewIndex(String kind
, boolean ancestors
) {
148 Index index
= new Index(kind
, ancestors
);
154 * Adds the given {@link Index} to the collection
155 * contained in this object. Note that given {@link Index}
156 * is not cloned. The provided object instance will become
157 * incorporated into this object's collection.
160 public void addNewIndex(Index index
){
164 public String
toYaml() {
165 return toYaml(false);
169 * Builds yaml string representing the indexes
171 * @param serverStyle Use the admin server style of yaml generation. Since the
172 * admin server is written in python, it generates a slightly different
173 * style of yaml. Setting this parameter to {@code true} is useful only
174 * for testing that the client-side code is able to parse this style of
176 * @return A Yaml string.
178 public String
toYaml(boolean serverStyle
) {
179 StringBuilder builder
= new StringBuilder(1024);
181 builder
.append(IndexYamlReader
.INDEX_DEFINITIONS_TAG
).append("\n");
183 builder
.append("indexes:");
184 int numIndexes
= (null == indexes ?
0 : indexes
.size());
185 if (0 == numIndexes
&& serverStyle
) {
186 builder
.append(" []");
188 builder
.append("\n");
189 for (Index index
: indexes
) {
190 String indexYaml
= (serverStyle ? index
.toServerStyleYaml() : index
.toLocalStyleYaml());
191 builder
.append(indexYaml
);
193 return builder
.toString();