001: package org.geotools.maven.xmlcodegen.templates;
002:
003: import java.util.*;
004: import org.eclipse.xsd.*;
005: import org.geotools.xml.*;
006:
007: public class BindingConfigurationTemplate {
008: protected static String nl;
009:
010: public static synchronized BindingConfigurationTemplate create(
011: String lineSeparator) {
012: nl = lineSeparator;
013: BindingConfigurationTemplate result = new BindingConfigurationTemplate();
014: nl = null;
015: return result;
016: }
017:
018: protected final String NL = nl == null ? (System.getProperties()
019: .getProperty("line.separator")) : nl;
020: protected final String TEXT_1 = NL
021: + "import org.geotools.xml.BindingConfiguration;" + NL
022: + "import org.picocontainer.MutablePicoContainer;" + NL
023: + "" + NL + "/**" + NL
024: + " * Binding configuration for the ";
025: protected final String TEXT_2 = " schema." + NL + " *" + NL
026: + " * @generated" + NL + " */" + NL + "public final class ";
027: protected final String TEXT_3 = "BindingConfiguration"
028: + NL
029: + "\timplements BindingConfiguration {"
030: + NL
031: + ""
032: + NL
033: + ""
034: + NL
035: + "\t/**"
036: + NL
037: + "\t * @generated modifiable"
038: + NL
039: + "\t */"
040: + NL
041: + "\tpublic void configure(MutablePicoContainer container) {"
042: + NL + "\t";
043: protected final String TEXT_4 = NL + "\t\t//Types";
044: protected final String TEXT_5 = NL
045: + "\t\tcontainer.registerComponentImplementation(";
046: protected final String TEXT_6 = ",";
047: protected final String TEXT_7 = ");";
048: protected final String TEXT_8 = NL;
049: protected final String TEXT_9 = NL + "\t\t//Elements";
050: protected final String TEXT_10 = NL
051: + "\t\tcontainer.registerComponentImplementation(";
052: protected final String TEXT_11 = ",";
053: protected final String TEXT_12 = ");";
054: protected final String TEXT_13 = NL;
055: protected final String TEXT_14 = NL + "\t\t//Attributes";
056: protected final String TEXT_15 = NL
057: + "\t\tcontainer.registerComponentImplementation(";
058: protected final String TEXT_16 = ",";
059: protected final String TEXT_17 = ");";
060: protected final String TEXT_18 = NL + "\t}" + NL + "" + NL + "}";
061:
062: public String generate(Object argument) {
063: final StringBuffer stringBuffer = new StringBuffer();
064:
065: Object[] arguments = (Object[]) argument;
066: XSDSchema schema = (XSDSchema) arguments[0];
067: List components = (List) arguments[1];
068: String ns = schema.getTargetNamespace();
069: String prefix = Schemas.getTargetPrefix(schema);
070:
071: List types = new ArrayList();
072: List elements = new ArrayList();
073: List attributes = new ArrayList();
074:
075: for (Iterator itr = components.iterator(); itr.hasNext();) {
076: XSDNamedComponent component = (XSDNamedComponent) itr
077: .next();
078: if (component instanceof XSDTypeDefinition) {
079: types.add(component);
080: } else if (component instanceof XSDElementDeclaration) {
081: elements.add(component);
082: } else if (component instanceof XSDAttributeDeclaration) {
083: attributes.add(component);
084: }
085: }
086:
087: stringBuffer.append(TEXT_1);
088: stringBuffer.append(schema.getTargetNamespace());
089: stringBuffer.append(TEXT_2);
090: stringBuffer.append(prefix.toUpperCase());
091: stringBuffer.append(TEXT_3);
092:
093: if (!types.isEmpty()) {
094:
095: stringBuffer.append(TEXT_4);
096:
097: for (Iterator itr = types.iterator(); itr.hasNext();) {
098: XSDTypeDefinition type = (XSDTypeDefinition) itr.next();
099: if (type.getName() == null)
100: continue;
101:
102: String typeQName = prefix.toUpperCase() + "."
103: + type.getName();
104: String binding = type.getName().substring(0, 1)
105: .toUpperCase()
106: + type.getName().substring(1) + "Binding.class";
107:
108: stringBuffer.append(TEXT_5);
109: stringBuffer.append(typeQName);
110: stringBuffer.append(TEXT_6);
111: stringBuffer.append(binding);
112: stringBuffer.append(TEXT_7);
113:
114: }
115: }
116:
117: stringBuffer.append(TEXT_8);
118:
119: if (!elements.isEmpty()) {
120:
121: stringBuffer.append(TEXT_9);
122:
123: for (Iterator itr = elements.iterator(); itr.hasNext();) {
124: XSDNamedComponent named = (XSDNamedComponent) itr
125: .next();
126: if (named.getName() == null)
127: continue;
128:
129: String nQName = prefix.toUpperCase() + "."
130: + named.getName();
131: String binding = named.getName().substring(0, 1)
132: .toUpperCase()
133: + named.getName().substring(1)
134: + "Binding.class";
135:
136: stringBuffer.append(TEXT_10);
137: stringBuffer.append(nQName);
138: stringBuffer.append(TEXT_11);
139: stringBuffer.append(binding);
140: stringBuffer.append(TEXT_12);
141:
142: }
143: }
144:
145: stringBuffer.append(TEXT_13);
146:
147: if (!attributes.isEmpty()) {
148:
149: stringBuffer.append(TEXT_14);
150:
151: for (Iterator itr = attributes.iterator(); itr.hasNext();) {
152: XSDNamedComponent named = (XSDNamedComponent) itr
153: .next();
154: if (named.getName() == null)
155: continue;
156:
157: String nQName = prefix.toUpperCase() + "."
158: + named.getName();
159: String binding = named.getName().substring(0, 1)
160: .toUpperCase()
161: + named.getName().substring(1)
162: + "Binding.class";
163:
164: stringBuffer.append(TEXT_15);
165: stringBuffer.append(nQName);
166: stringBuffer.append(TEXT_16);
167: stringBuffer.append(binding);
168: stringBuffer.append(TEXT_17);
169:
170: }
171: }
172:
173: stringBuffer.append(TEXT_18);
174: return stringBuffer.toString();
175: }
176: }
|