001: //==============================================================================
002: //===
003: //=== ComplexTypeEntry
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:
033: import org.jdom.Attribute;
034: import org.jdom.Element;
035:
036: //==============================================================================
037:
038: class ComplexTypeEntry {
039: public String name;
040: public boolean isOrType = false;
041: public boolean isAbstract;
042:
043: public ComplexContentEntry complexContent;
044: public SimpleContentEntry simpleContent;
045:
046: public ArrayList alElements = new ArrayList();
047: public ArrayList alAttribs = new ArrayList();
048: public ArrayList alAttribGroups = new ArrayList();
049:
050: //---------------------------------------------------------------------------
051: //---
052: //--- Constructor
053: //---
054: //---------------------------------------------------------------------------
055:
056: private ComplexTypeEntry() {
057: }
058:
059: //---------------------------------------------------------------------------
060:
061: public ComplexTypeEntry(Element el, String file, String targetNS,
062: String targetNSPrefix) {
063: this (new ElementInfo(el, file, targetNS, targetNSPrefix));
064: }
065:
066: //---------------------------------------------------------------------------
067:
068: public ComplexTypeEntry(ElementInfo ei) {
069: handleAttribs(ei);
070: handleChildren(ei);
071: }
072:
073: //---------------------------------------------------------------------------
074: //---
075: //--- Private methods
076: //---
077: //---------------------------------------------------------------------------
078:
079: private void handleAttribs(ElementInfo ei) {
080: List attribs = ei.element.getAttributes();
081:
082: for (int i = 0; i < attribs.size(); i++) {
083: Attribute at = (Attribute) attribs.get(i);
084:
085: String attrName = at.getName();
086: if (attrName.equals("name")) {
087: name = at.getValue();
088: if ((name.indexOf(":") == -1)
089: && (ei.targetNSPrefix != null))
090: name = ei.targetNSPrefix + ":" + at.getValue();
091: } else if (attrName.equals("abstract")) {
092: String abValue = at.getValue();
093: isAbstract = abValue.equals("true");
094: } else if (attrName.equals("mixed"))
095: Logger
096: .log("Skipping 'mixed' attribute in <complexType> element '"
097: + name + "'");
098: else
099: Logger.log("Unknown attribute '" + attrName
100: + "' in <complexType> element", ei);
101:
102: }
103: }
104:
105: //---------------------------------------------------------------------------
106:
107: private void handleChildren(ElementInfo ei) {
108: List children = ei.element.getChildren();
109:
110: for (int i = 0; i < children.size(); i++) {
111: Element elChild = (Element) children.get(i);
112: String elName = elChild.getName();
113: boolean first = true;
114:
115: if (elName.equals("sequence") || elName.equals("choice")) {
116: isOrType = elName.equals("choice") && (first);
117:
118: List sequence = elChild.getChildren();
119:
120: for (int j = 0; j < sequence.size(); j++) {
121: Element elElem = (Element) sequence.get(j);
122:
123: if (elElem.getName().equals("element")
124: || elElem.getName().equals("group")
125: || elElem.getName().equals("choice")
126: || elElem.getName().equals("sequence")) {
127: alElements
128: .add(new ElementEntry(elElem, ei.file,
129: ei.targetNS, ei.targetNSPrefix));
130: } else
131: Logger.log("Unknown child '" + elElem.getName()
132: + "' in <sequence|choice> element", ei);
133: }
134: } else if (elName.equals("group")) {
135: first = false;
136: isOrType = false;
137: alElements.add(new ElementEntry(elChild, ei.file,
138: ei.targetNS, ei.targetNSPrefix));
139: }
140:
141: else if (elName.equals("attribute")) {
142: first = false;
143: alAttribs.add(new AttributeEntry(elChild, ei.file,
144: ei.targetNS, ei.targetNSPrefix));
145: }
146:
147: else if (elName.equals("attributeGroup")) {
148: first = false;
149: String attribGroup = elChild.getAttributeValue("ref");
150:
151: if (attribGroup == null)
152: throw new IllegalArgumentException(
153: "'ref' is null for <attributeGroup> in complexType : "
154: + name);
155:
156: alAttribGroups.add(attribGroup);
157: }
158:
159: else if (elName.equals("complexContent")) {
160: first = false;
161: complexContent = new ComplexContentEntry(elChild,
162: ei.file, ei.targetNS, ei.targetNSPrefix);
163: }
164:
165: else if (elName.equals("simpleContent")) {
166: first = false;
167: simpleContent = new SimpleContentEntry(elChild,
168: ei.file, ei.targetNS, ei.targetNSPrefix);
169: } else if (elName.equals("annotation"))
170: ;
171:
172: else
173: Logger.log("Unknown child '" + elName
174: + "' in <complexType> element", ei);
175: }
176: }
177:
178: }
179:
180: //==============================================================================
|