001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.axi.impl;
043:
044: import java.util.ArrayList;
045: import org.netbeans.modules.xml.axi.AXIComponent;
046: import org.netbeans.modules.xml.axi.AXIComponentFactory;
047: import org.netbeans.modules.xml.axi.AXIDocument;
048: import org.netbeans.modules.xml.axi.AXIModel;
049: import org.netbeans.modules.xml.axi.AbstractAttribute;
050: import org.netbeans.modules.xml.axi.AbstractElement;
051: import org.netbeans.modules.xml.axi.Attribute;
052: import org.netbeans.modules.xml.axi.Compositor;
053: import org.netbeans.modules.xml.axi.ContentModel;
054: import org.netbeans.modules.xml.axi.Element;
055: import org.netbeans.modules.xml.axi.AnyElement;
056: import org.netbeans.modules.xml.axi.AnyAttribute;
057: import org.netbeans.modules.xml.axi.datatype.Datatype;
058: import org.netbeans.modules.xml.schema.model.*;
059: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
060: import java.util.List;
061: import org.netbeans.modules.xml.axi.AXIType;
062: import org.netbeans.modules.xml.axi.impl.ElementImpl.AnonymousType;
063:
064: /**
065: * Utility class.
066: *
067: * @author Samaresh (Samaresh.Panda@Sun.Com)
068: */
069: public class Util {
070:
071: /**
072: * Creates a new instance of Util
073: */
074: private Util() {
075: }
076:
077: public static void moveChildren(AXIComponent oldParent,
078: AXIComponent newParent) {
079: List<AXIComponent> children = new ArrayList<AXIComponent>();
080: for (AXIComponent c : oldParent.getChildren()) {
081: children.add(c);
082: }
083: for (AXIComponent c : children) {
084: oldParent.removeChild(c);
085: newParent.appendChild(c);
086: }
087: }
088:
089: /**
090: * Adds proxy children to the specified parent. The proxy children are created against
091: * each shared child. If called during bootstrapping the children list is updated, else
092: * added to the parent.
093: */
094: public static void addProxyChildren(AXIComponent parent,
095: AXIComponent shared, List<AXIComponent> children) {
096: for (AXIComponent child : shared.getChildren()) {
097: AXIComponentFactory factory = parent.getModel()
098: .getComponentFactory();
099: AXIComponent proxy = factory.createProxy(child);
100: if (children != null)
101: children.add(proxy);
102: else
103: parent.appendChild(proxy);
104: }
105:
106: if (shared instanceof ContentModel) {
107: shared.addListener(parent);
108: }
109: }
110:
111: /**
112: * Finds an AXI component against the specified global schema component.
113: */
114: public static AXIComponent lookup(AXIModel axiModel,
115: SchemaComponent gsc) {
116: AXIModelImpl model = (AXIModelImpl) axiModel;
117: if (model.fromSameSchemaModel(gsc)) {
118: return model.lookup(gsc);
119: }
120:
121: return model.lookupFromOtherModel(gsc);
122: }
123:
124: public static boolean canSetType(AXIType oldValue, AXIType newValue) {
125: if (oldValue == newValue)
126: return false;
127:
128: if (oldValue instanceof Datatype
129: && newValue instanceof Datatype) {
130: if (((Datatype) oldValue).getKind() == ((Datatype) newValue)
131: .getKind()) {
132: return false;
133: }
134: }
135:
136: if (oldValue instanceof AnonymousType
137: && newValue instanceof AnonymousType) {
138: if (((AnonymousType) oldValue).getPeer() == ((AnonymousType) newValue)
139: .getPeer()) {
140: return false;
141: }
142: }
143:
144: if (oldValue instanceof ContentModel
145: && newValue instanceof ContentModel) {
146: if (((ContentModel) oldValue).getPeer() == ((ContentModel) newValue)
147: .getPeer()) {
148: return false;
149: }
150: }
151:
152: return true;
153: }
154:
155: /**
156: * Returns an element's type.
157: */
158: public static SchemaComponent getSchemaType(AXIModelImpl model,
159: SchemaComponent schemaComponent) {
160: if (schemaComponent instanceof GlobalElement) {
161: GlobalElement ge = (GlobalElement) schemaComponent;
162: NamedComponentReference ref = ge.getType();
163: if (ref != null) {
164: SchemaComponent sc = model
165: .getReferenceableSchemaComponent(ref);
166: if (sc != null)
167: return sc;
168: return (SchemaComponent) ref.get();
169: }
170: return ge.getInlineType();
171: }
172:
173: if (schemaComponent instanceof LocalElement) {
174: LocalElement le = (LocalElement) schemaComponent;
175: NamedComponentReference ref = le.getType();
176: if (ref != null) {
177: SchemaComponent sc = model
178: .getReferenceableSchemaComponent(ref);
179: if (sc != null)
180: return sc;
181: return (SchemaComponent) ref.get();
182: }
183: return le.getInlineType();
184: }
185:
186: return null;
187: }
188:
189: public static AXIType getAXIType(Element element,
190: SchemaComponent type) {
191: if (type == null)
192: return null;
193: if (type instanceof SimpleType) {
194: DatatypeBuilder builder = new DatatypeBuilder(element
195: .getModel());
196: return builder.getDatatype(element.getPeer());
197: }
198: if (type instanceof LocalComplexType) {
199: return new AnonymousType(type);
200: }
201: if (type instanceof GlobalComplexType) {
202: AXIModelImpl modelImpl = (AXIModelImpl) element.getModel();
203: return (ContentModel) lookup(modelImpl, type);
204: }
205: return null;
206: }
207:
208: public static String getProperty(AXIComponent child) {
209: if (child instanceof Compositor)
210: return Compositor.PROP_COMPOSITOR;
211: if (child instanceof AbstractElement)
212: return AbstractElement.PROP_ELEMENT;
213: if (child instanceof AbstractAttribute)
214: return AbstractAttribute.PROP_ATTRIBUTE;
215: if (child instanceof ContentModel)
216: return ContentModel.PROP_CONTENT_MODEL;
217:
218: return null;
219: }
220:
221: public static Datatype getDatatype(AXIModel model,
222: SchemaComponent component) {
223: DatatypeBuilder builder = new DatatypeBuilder(model);
224: return builder.getDatatype(component);
225: }
226:
227: public static void updateAnyElement(AnyElement element) {
228: org.netbeans.modules.xml.schema.model.AnyElement any = (org.netbeans.modules.xml.schema.model.AnyElement) element
229: .getPeer();
230: element.setMinOccurs(String
231: .valueOf(any.getMinOccursEffective()));
232: element.setMaxOccurs(any.getMaxOccursEffective());
233: element.setTargetNamespace(any.getNameSpaceEffective());
234: element.setProcessContents(any.getProcessContentsEffective());
235: }
236:
237: public static void updateAnyAttribute(AnyAttribute attribute) {
238: org.netbeans.modules.xml.schema.model.AnyAttribute anyAttr = (org.netbeans.modules.xml.schema.model.AnyAttribute) attribute
239: .getPeer();
240: attribute.setProcessContents(anyAttr
241: .getProcessContentsEffective());
242: attribute.setTargetNamespace(anyAttr.getNameSpaceEffective());
243: }
244:
245: public static void updateAXIDocument(AXIDocument document) {
246: Schema schema = (Schema) document.getPeer();
247: document.setTargetNamespace(schema.getTargetNamespace());
248: document.setVersion(schema.getVersion());
249: document.setLanguage(schema.getLanguage());
250: document.setAttributeFormDefault(schema
251: .getAttributeFormDefaultEffective());
252: document.setElementFormDefault(schema
253: .getElementFormDefaultEffective());
254: }
255:
256: public static void updateGlobalAttribute(Attribute attribute) {
257: GlobalAttribute component = (GlobalAttribute) attribute
258: .getPeer();
259: attribute.setName(component.getName());
260: attribute.setDefault(component.getDefault());
261: attribute.setFixed(component.getFixed());
262: }
263:
264: public static void updateLocalAttribute(Attribute attribute) {
265: LocalAttribute component = (LocalAttribute) attribute.getPeer();
266: attribute.setName(component.getName());
267: attribute.setDefault(component.getDefault());
268: attribute.setFixed(component.getFixed());
269: attribute.setForm(component.getFormEffective());
270: attribute.setUse(component.getUseEffective());
271: }
272:
273: public static void updateAttributeReference(Attribute attribute) {
274: AttributeReference component = (AttributeReference) attribute
275: .getPeer();
276: //for AttributeRef, only use, default and fixed needs to be updated.
277: attribute.setDefault(component.getDefault());
278: attribute.setFixed(component.getFixed());
279: attribute.setUse(component.getUseEffective());
280: }
281:
282: public static void updateGlobalElement(Element element) {
283: GlobalElement component = (GlobalElement) element.getPeer();
284: element.setName(component.getName());
285: element.setFixed(component.getFixed());
286: element.setDefault(component.getDefault());
287: element.setAbstract(component.getAbstractEffective());
288: element.setNillable(component.getNillableEffective());
289: //element.setContentType(getDatatype(element.getModel(), component));
290: //element.setFinal();
291: //element.setBlock();
292: }
293:
294: public static void updateLocalElement(Element element) {
295: LocalElement component = (LocalElement) element.getPeer();
296: element.setName(component.getName());
297: element.setMaxOccurs(component.getMaxOccursEffective());
298: element.setMinOccurs(String.valueOf(component
299: .getMinOccursEffective()));
300: element.setFixed(component.getFixed());
301: element.setDefault(component.getDefault());
302: element.setNillable(component.getNillableEffective());
303: element.setForm(component.getFormEffective());
304: //element.setBlock(component.getBlockEffective());
305: //element.setContentType(getDatatype(element.getModel(), component));
306: //element.setFinal();
307: }
308:
309: public static void updateElementReference(Element elementRef) {
310: ElementReference component = (ElementReference) elementRef
311: .getPeer();
312: //for an element-ref, get the min and max from that ref
313: elementRef.setMaxOccurs(component.getMaxOccursEffective());
314: elementRef.setMinOccurs(String.valueOf(component
315: .getMinOccursEffective()));
316: }
317:
318: public static void updateCompositor(Compositor compositor) {
319: switch (compositor.getType()) {
320: case SEQUENCE: {
321: Sequence component = (Sequence) compositor.getPeer();
322: Cardinality c = component.getCardinality();
323: if (c != null) {
324: compositor.setMaxOccurs(c.getMaxOccursEffective());
325: compositor.setMinOccurs(String.valueOf(c
326: .getMinOccursEffective()));
327: } else {
328: compositor.setMaxOccurs("1");
329: compositor.setMinOccurs("1");
330: }
331: break;
332: }
333: case CHOICE: {
334: Choice component = (Choice) compositor.getPeer();
335: Cardinality c = component.getCardinality();
336: if (c != null) {
337: compositor.setMaxOccurs(c.getMaxOccursEffective());
338: compositor.setMinOccurs(String.valueOf(c
339: .getMinOccursEffective()));
340: } else {
341: compositor.setMaxOccurs("1");
342: compositor.setMinOccurs("1");
343: }
344: break;
345: }
346: case ALL: {
347: All component = (All) compositor.getPeer();
348: //Compositor compositor = new All(model, component);
349: //compositor.setMaxOccurs(component.getMaxOccursEffective());
350: compositor.setMinOccurs(String.valueOf(component
351: .getMinOccursEffective()));
352: break;
353: }
354: }
355:
356: }
357:
358: public static void updateContentModel(ContentModel contentModel) {
359: SchemaComponent peer = contentModel.getPeer();
360: if (peer instanceof GlobalComplexType) {
361: contentModel.setName(((GlobalComplexType) peer).getName());
362: return;
363: }
364: if (peer instanceof GlobalAttributeGroup) {
365: contentModel.setName(((GlobalAttributeGroup) peer)
366: .getName());
367: return;
368: }
369: if (peer instanceof GlobalGroup) {
370: contentModel.setName(((GlobalGroup) peer).getName());
371: return;
372: }
373: }
374: }
|