001: //==============================================================================
002: //===
003: //=== ElementEntry
004: //===
005: //==============================================================================
006: //=== Copyright (C) 2001-2005 Food and Agriculture Organization of the
007: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
008: //=== and United Nations Environment Programme (UNEP)
009: //===
010: //=== This program is free software; you can redistribute it and/or modify
011: //=== it under the terms of the GNU General Public License as published by
012: //=== the Free Software Foundation; either version 2 of the License, or (at
013: //=== your option) any later version.
014: //===
015: //=== This program is distributed in the hope that it will be useful, but
016: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
017: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: //=== General Public License for more details.
019: //===
020: //=== You should have received a copy of the GNU General Public License
021: //=== along with this program; if not, write to the Free Software
022: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
023: //===
024: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
025: //=== Rome - Italy. email: geonetwork@osgeo.org
026: //==============================================================================
027:
028: package org.fao.geonet.kernel.schema;
029:
030: import java.util.ArrayList;
031: import java.util.List;
032: import java.util.StringTokenizer;
033:
034: import org.jdom.Attribute;
035: import org.jdom.Element;
036:
037: //==============================================================================
038:
039: class ElementEntry {
040: public String name;
041: public String ns;
042: public String type;
043: public int min = 1;
044: public int max = 1;
045: public String substGroup;
046: public String substGroupNS;
047: public String ref;
048: public boolean abstrElem;
049: public boolean choiceElem;
050: public boolean groupElem;
051: public boolean sequenceElem;
052: public ArrayList alContainerElems = new ArrayList();
053:
054: public ComplexTypeEntry complexType;
055: public SimpleTypeEntry simpleType;
056:
057: //---------------------------------------------------------------------------
058: //---
059: //--- Constructor - this class handles both <element> and <choice> entries
060: //---
061: //---------------------------------------------------------------------------
062:
063: private ElementEntry() {
064: }
065:
066: //---------------------------------------------------------------------------
067:
068: public ElementEntry(Element el, String file, String targetNS,
069: String targetNSPrefix) {
070: this (new ElementInfo(el, file, targetNS, targetNSPrefix));
071: }
072:
073: //---------------------------------------------------------------------------
074:
075: public ElementEntry(ElementInfo ei) {
076: ns = ei.targetNS;
077: handleAttribs(ei);
078: if (choiceElem)
079: handleContainerChildren(ei, alContainerElems);
080: else if (groupElem)
081: ; // no need to do anything - read elements from group later
082: else if (sequenceElem)
083: handleContainerChildren(ei, alContainerElems);
084: else
085: handleChildren(ei);
086: }
087:
088: //---------------------------------------------------------------------------
089: //---
090: //--- API methods
091: //---
092: //---------------------------------------------------------------------------
093:
094: public ElementEntry copy() {
095: ElementEntry ee = new ElementEntry();
096:
097: ee.name = name;
098: ee.ns = ns;
099: ee.type = type;
100: ee.min = min;
101: ee.max = max;
102: ee.substGroup = substGroup;
103: ee.substGroupNS = substGroupNS;
104: ee.ref = ref;
105: ee.abstrElem = abstrElem;
106:
107: ee.complexType = complexType;
108: ee.simpleType = simpleType;
109: ee.choiceElem = choiceElem;
110: ee.groupElem = groupElem;
111: ee.sequenceElem = sequenceElem;
112: ee.alContainerElems = alContainerElems;
113:
114: return ee;
115: }
116:
117: public String toString() {
118: return new String("ElementEntry name: " + name + " ref: " + ref
119: + " type: " + type + " abstract: " + abstrElem
120: + " choice: " + choiceElem + " group: " + groupElem
121: + " sequence: " + sequenceElem);
122: }
123:
124: //---------------------------------------------------------------------------
125: //---
126: //--- Private methods
127: //---
128: //---------------------------------------------------------------------------
129:
130: private void handleAttribs(ElementInfo ei) {
131: if (ei.element.getName().equals("choice"))
132: choiceElem = true;
133: else if (ei.element.getName().equals("group"))
134: groupElem = true;
135: else if (ei.element.getName().equals("sequence"))
136: sequenceElem = true;
137:
138: List attrs = ei.element.getAttributes();
139:
140: for (int i = 0; i < attrs.size(); i++) {
141: Attribute at = (Attribute) attrs.get(i);
142:
143: String attrName = at.getName();
144: String value = at.getValue();
145:
146: if (attrName.equals("name")) {
147: name = at.getValue();
148: if ((name.indexOf(":") == -1)
149: && (ei.targetNSPrefix != null))
150: name = ei.targetNSPrefix + ":" + at.getValue();
151: // System.out.println("Doing Element "+name);
152: } else if (attrName.equals("type")) {
153: type = value;
154: }
155:
156: else if (attrName.equals("ref"))
157: ref = value;
158:
159: else if (attrName.equals("substitutionGroup"))
160: substGroup = value;
161:
162: else if (attrName.equals("abstract")) {
163: abstrElem = "true".equals(value);
164: } else if (attrName.equals("minOccurs"))
165: min = Integer.parseInt(value);
166:
167: else if (attrName.equals("maxOccurs")) {
168: if (value.equals("unbounded"))
169: max = 10000;
170: else
171: max = Integer.parseInt(value);
172: }
173:
174: else {
175: if (choiceElem)
176: Logger.log("Unknown attribute '" + attrName
177: + "'in <choice> element", ei);
178: else if (groupElem)
179: Logger.log("Unknown attribute '" + attrName
180: + "'in <group> element", ei);
181: else if (sequenceElem)
182: Logger.log("Unknown attribute '" + attrName
183: + "'in <sequence> element", ei);
184: else
185: Logger.log("Unknown attribute '" + attrName
186: + "'in <element> element", ei);
187:
188: }
189: }
190: }
191:
192: //---------------------------------------------------------------------------
193:
194: private void handleChildren(ElementInfo ei) {
195: List children = ei.element.getChildren();
196:
197: for (int i = 0; i < children.size(); i++) {
198: Element elChild = (Element) children.get(i);
199: String elName = elChild.getName();
200:
201: if (elName.equals("complexType"))
202: complexType = new ComplexTypeEntry(elChild, ei.file,
203: ei.targetNS, ei.targetNSPrefix);
204:
205: else if (elName.equals("simpleType")) {
206: simpleType = new SimpleTypeEntry(elChild, ei.file,
207: ei.targetNS, ei.targetNSPrefix);
208: if (simpleType.name == null)
209: simpleType.name = name + "HASHS";
210: }
211:
212: else if (elName.equals("key"))
213: Logger
214: .log("Skipping 'key' child in <element> element '"
215: + name + "'");
216:
217: else if (elName.equals("annotation"))
218: ;
219:
220: else
221: Logger.log("Unknown child '" + elName
222: + "' in <element> element", ei);
223: }
224: }
225:
226: //---------------------------------------------------------------------------
227:
228: private void handleContainerChildren(ElementInfo ei,
229: ArrayList elements) {
230: List children = ei.element.getChildren();
231:
232: for (int i = 0; i < children.size(); i++) {
233:
234: if (groupElem)
235: System.out
236: .println("WARNING found element children for group in element "
237: + name + " " + ref);
238: Element elChild = (Element) children.get(i);
239: String elName = elChild.getName();
240:
241: if (elName.equals("annotation")) {
242: List appinfo = elChild.getChildren();
243: for (int j = 0; j < appinfo.size(); j++) {
244: Element elElem = (Element) appinfo.get(j);
245: if (elElem.getName().equals("appinfo")) {
246: name = (String) elElem.getText();
247: }
248: }
249: }
250:
251: else if (elName.equals("element")
252: || elName.equals("choice")
253: || elName.equals("sequence")
254: || elName.equals("group")) {
255: elements.add(new ElementEntry(elChild, ei.file,
256: ei.targetNS, ei.targetNSPrefix));
257: }
258:
259: else
260: Logger.log("handleContainerChildren: Unknown child '"
261: + elName
262: + "' in <choice>|<group>|<sequence> element",
263: ei);
264: }
265: }
266: }
267:
268: //==============================================================================
|