App Engine 1.8.4.
[gae.git] / java / src / main / com / google / apphosting / utils / config / IndexesXml.java
blob9ccf27ff96c3e49c75a0df39fc9f309a36c167cc
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;
7 import java.util.List;
9 /**
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() {
29 return propName;
32 public boolean isAscending() {
33 return ascending;
37 /**
39 public class Index {
41 private String kind;
42 private boolean ancestors;
43 private List<PropertySort> properties;
45 public Index (String kind, boolean ancestors) {
46 this.kind = kind;
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() {
56 return kind;
59 public boolean doIndexAncestors() {
60 return ancestors;
63 public List<PropertySort> getProperties() {
64 return properties;
67 /**
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");
75 if (ancestors) {
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();
86 /**
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
91 * style of yaml.
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");
99 if (ancestors) {
100 builder.append(" ancestor: yes\n");
102 builder.append(" properties:\n");
103 for (PropertySort prop : properties) {
104 builder
105 .append(" - ")
106 .append(IndexYamlReader.PROPERTY_TAG)
107 .append(" {direction: ")
108 .append((prop.isAscending() ? "asc" : "desc"))
109 .append(",\n")
110 .append(" ")
111 .append("name: " + prop.getPropertyName())
112 .append("}\n");
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");
122 builder.append(
123 " <property name=\"" + prop.getPropertyName() + "\" direction=\"" + direction
124 + "\"/>\n");
126 builder.append("</datastore-index>\n");
127 return builder.toString();
131 private List<Index> indexes;
133 public IndexesXml() {
134 indexes = new ArrayList<Index>();
137 @Override
138 public Iterator<Index> iterator() {
139 return indexes.iterator();
142 public int size(){
143 return indexes.size();
146 public Index addNewIndex(String kind, boolean ancestors) {
147 Index index = new Index(kind, ancestors);
148 indexes.add(index);
149 return index;
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.
157 * @param index
159 public void addNewIndex(Index index){
160 indexes.add(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
174 * yaml.
175 * @return A Yaml string.
177 public String toYaml(boolean serverStyle) {
178 StringBuilder builder = new StringBuilder(1024);
179 if (serverStyle) {
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();