001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xs.bindings;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.xml.namespace.QName;
025:
026: import org.geotools.xml.AbstractComplexBinding;
027: import org.geotools.xml.ElementInstance;
028: import org.geotools.xml.Node;
029: import org.picocontainer.MutablePicoContainer;
030:
031: /**
032: * Binding object for the type http://www.w3.org/2001/XMLSchema:anyType.
033: *
034: * <p>
035: * <pre>
036: * <code>
037: * <xs:complexType name="anyType" mixed="true">
038: * <xs:annotation>
039: * <xs:documentation> Not the real urType, but as close an
040: * approximation as we can get in the XML representation</xs:documentation>
041: * </xs:annotation>
042: * <xs:sequence>
043: * <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
044: * </xs:sequence>
045: * <xs:anyAttribute processContents="lax"/>
046: * </xs:complexType>
047: *
048: * </code>
049: * </pre>
050: * </p>
051: *
052: * @generated
053: */
054: public class XSAnyTypeBinding extends AbstractComplexBinding {
055: /**
056: * @generated
057: */
058: public QName getTarget() {
059: return XS.ANYTYPE;
060: }
061:
062: /**
063: * <!-- begin-user-doc -->
064: * <!-- end-user-doc -->
065: *
066: * @generated modifiable
067: */
068: public int getExecutionMode() {
069: return OVERRIDE;
070: }
071:
072: /**
073: * <!-- begin-user-doc -->
074: * This binding returns objects of type Map
075: * <!-- end-user-doc -->
076: *
077: * @generated modifiable
078: */
079: public Class getType() {
080: return Object.class;
081: }
082:
083: /**
084: * <!-- begin-user-doc -->
085: * <!-- end-user-doc -->
086: *
087: * @generated modifiable
088: */
089: public void initialize(ElementInstance instance, Node node,
090: MutablePicoContainer context) {
091: }
092:
093: /**
094: *
095: * <!-- begin-user-doc -->
096: * A quick approx of the available content:
097: * <p>
098: * This method returns a {@link Map} in which the names of children and
099: * attributes are keys, and the parsed children and attributes are the
100: * values. If the element being parsed contains child text, it is available
101: * under the <code>nulll</code> key.
102: *
103: * @return Map,
104: * </p>
105: * <!-- end-user-doc -->
106: *
107: * @generated modifiable
108: */
109: public Object parse(ElementInstance instance, Node node,
110: Object value) throws Exception {
111:
112: String text = null;
113: if ((value != null) && value instanceof String) {
114: text = ((String) value).trim();
115: if ("".equals(text)) {
116: text = null;
117: }
118: }
119:
120: //if there is just some text, return it
121: if (node.getChildren().isEmpty()
122: && node.getAttributes().isEmpty() && text != null) {
123: return text;
124: }
125:
126: //if there is only a single child, return it
127: if (node.getChildren().size() == 1
128: && node.getAttributes().isEmpty() && text == null) {
129: return node.getChildValue(0);
130: }
131:
132: //if there is a single attribute, return it
133: if (node.getAttributes().size() == 1
134: && node.getChildren().isEmpty() && text == null) {
135: return ((Node) node.getAttributes().get(0)).getValue();
136: }
137:
138: //create a map of the elements and attributes
139: Map map = new HashMap();
140: List attributes = node.getAttributes();
141: List children = node.getChildren();
142: mapBinding(map, attributes);
143: mapBinding(map, children);
144:
145: if (text != null && !"".equals(text.trim())) {
146: map.put(null, text.trim());
147: }
148:
149: return map;
150: }
151:
152: private void mapBinding(Map map, List attributes) {
153: for (Iterator i = attributes.iterator(); i.hasNext();) {
154: Node attribute = (Node) i.next();
155: String name = attribute.getComponent().getName();
156: Object value = attribute.getValue();
157:
158: if (map.containsKey(name)) {
159: List values;
160: Object obj = map.get(name);
161:
162: if (obj instanceof List) {
163: values = (List) obj;
164: } else {
165: values = new ArrayList();
166: values.add(obj);
167: map.put(name, values);
168: }
169:
170: values.add(value);
171: } else {
172: map.put(name, value);
173: }
174: }
175: }
176: }
|