KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import org.netbeans.modules.xml.axi.AXIComponent;
24 import org.netbeans.modules.xml.axi.AXIComponentFactory;
25 import org.netbeans.modules.xml.axi.AXIDocument;
26 import org.netbeans.modules.xml.axi.AXIModel;
27 import org.netbeans.modules.xml.axi.AbstractAttribute;
28 import org.netbeans.modules.xml.axi.AbstractElement;
29 import org.netbeans.modules.xml.axi.Attribute;
30 import org.netbeans.modules.xml.axi.Compositor;
31 import org.netbeans.modules.xml.axi.ContentModel;
32 import org.netbeans.modules.xml.axi.Element;
33 import org.netbeans.modules.xml.axi.AnyElement;
34 import org.netbeans.modules.xml.axi.AnyAttribute;
35 import org.netbeans.modules.xml.axi.datatype.Datatype;
36 import org.netbeans.modules.xml.schema.model.*;
37 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
38 import java.util.List JavaDoc;
39 import org.netbeans.modules.xml.axi.AXIType;
40 import org.netbeans.modules.xml.axi.impl.ElementImpl.AnonymousType;
41 /**
42  * Utility class.
43  *
44  * @author Samaresh (Samaresh.Panda@Sun.Com)
45  */

46 public class Util {
47     
48     /**
49      * Creates a new instance of Util
50      */

51     private Util() {
52     }
53             
54     public static void moveChildren(AXIComponent oldParent, AXIComponent newParent) {
55         List JavaDoc<AXIComponent> children = new ArrayList JavaDoc<AXIComponent>();
56         for(AXIComponent c: oldParent.getChildren()) {
57             children.add(c);
58         }
59         for(AXIComponent c: children) {
60             oldParent.removeChild(c);
61             newParent.appendChild(c);
62         }
63     }
64     
65     /**
66      * Adds proxy children to the specified parent. The proxy children are created against
67      * each shared child. If called during bootstrapping the children list is updated, else
68      * added to the parent.
69      */

70     public static void addProxyChildren(AXIComponent parent, AXIComponent shared, List JavaDoc<AXIComponent> children) {
71         for(AXIComponent child : shared.getChildren()) {
72             AXIComponentFactory factory = parent.getModel().getComponentFactory();
73             AXIComponent proxy = factory.createProxy(child);
74             if(children != null)
75                 children.add(proxy);
76             else
77                 parent.appendChild(proxy);
78         }
79         
80         if(shared instanceof ContentModel) {
81             shared.addListener(parent);
82         }
83     }
84     
85     /**
86      * Finds an AXI component against the specified global schema component.
87      */

88     public static AXIComponent lookup(AXIModel axiModel, SchemaComponent gsc) {
89         AXIModelImpl model = (AXIModelImpl)axiModel;
90         if(model.fromSameSchemaModel(gsc)) {
91             return model.lookup(gsc);
92         }
93         
94         return model.lookupFromOtherModel(gsc);
95     }
96     
97     public static boolean canSetType(AXIType oldValue, AXIType newValue) {
98         if(oldValue == newValue)
99             return false;
100         
101         if(oldValue instanceof Datatype && newValue instanceof Datatype) {
102             if(((Datatype)oldValue).getKind() == ((Datatype)newValue).getKind()) {
103                 return false;
104             }
105         }
106         
107         if(oldValue instanceof AnonymousType && newValue instanceof AnonymousType) {
108             if(((AnonymousType)oldValue).getPeer() == ((AnonymousType)newValue).getPeer()) {
109                 return false;
110             }
111         }
112         
113         if(oldValue instanceof ContentModel && newValue instanceof ContentModel) {
114             if(((ContentModel)oldValue).getPeer() == ((ContentModel)newValue).getPeer()) {
115                 return false;
116             }
117         }
118         
119         return true;
120     }
121     
122     /**
123      * Returns an element's type.
124      */

125     public static SchemaComponent getSchemaType(AXIModelImpl model,
126             SchemaComponent schemaComponent) {
127         if(schemaComponent instanceof GlobalElement) {
128             GlobalElement ge = (GlobalElement)schemaComponent;
129             NamedComponentReference ref = ge.getType();
130             if(ref != null) {
131                 SchemaComponent sc = model.getReferenceableSchemaComponent(ref);
132                 if(sc != null)
133                     return sc;
134                 return (SchemaComponent)ref.get();
135             }
136             return ge.getInlineType();
137         }
138         
139         if(schemaComponent instanceof LocalElement) {
140             LocalElement le = (LocalElement)schemaComponent;
141             NamedComponentReference ref = le.getType();
142             if(ref != null) {
143                 SchemaComponent sc = model.getReferenceableSchemaComponent(ref);
144                 if(sc != null)
145                     return sc;
146                 return (SchemaComponent)ref.get();
147             }
148             return le.getInlineType();
149         }
150         
151         return null;
152     }
153     
154     public static AXIType getAXIType(Element element, SchemaComponent type) {
155         if(type == null)
156             return null;
157         if(type instanceof SimpleType) {
158             DatatypeBuilder builder = new DatatypeBuilder(element.getModel());
159             return builder.getDatatype(element.getPeer());
160         }
161         if(type instanceof LocalComplexType) {
162             return new AnonymousType(type);
163         }
164         if(type instanceof GlobalComplexType) {
165             AXIModelImpl modelImpl = (AXIModelImpl)element.getModel();
166             return (ContentModel)lookup(modelImpl, type);
167         }
168         return null;
169     }
170         
171     public static String JavaDoc getProperty(AXIComponent child) {
172         if(child instanceof Compositor)
173             return Compositor.PROP_COMPOSITOR;
174         if(child instanceof AbstractElement)
175             return AbstractElement.PROP_ELEMENT;
176         if(child instanceof AbstractAttribute)
177             return AbstractAttribute.PROP_ATTRIBUTE;
178         if(child instanceof ContentModel)
179             return ContentModel.PROP_CONTENT_MODEL;
180         
181         return null;
182     }
183     
184     public static Datatype getDatatype(AXIModel model, SchemaComponent component) {
185         DatatypeBuilder builder = new DatatypeBuilder(model);
186         return builder.getDatatype(component);
187     }
188         
189     public static void updateAnyElement(AnyElement element) {
190         org.netbeans.modules.xml.schema.model.AnyElement any =
191                 (org.netbeans.modules.xml.schema.model.AnyElement)element.getPeer();
192         element.setMinOccurs(String.valueOf(any.getMinOccursEffective()));
193         element.setMaxOccurs(any.getMaxOccursEffective());
194         element.setTargetNamespace(any.getNameSpaceEffective());
195         element.setProcessContents(any.getProcessContentsEffective());
196     }
197     
198     public static void updateAnyAttribute(AnyAttribute attribute) {
199         org.netbeans.modules.xml.schema.model.AnyAttribute anyAttr =
200                 (org.netbeans.modules.xml.schema.model.AnyAttribute)attribute.getPeer();
201         attribute.setProcessContents(anyAttr.getProcessContentsEffective());
202         attribute.setTargetNamespace(anyAttr.getNameSpaceEffective());
203     }
204     
205     public static void updateAXIDocument(AXIDocument document) {
206         Schema schema = (Schema)document.getPeer();
207         document.setTargetNamespace(schema.getTargetNamespace());
208         document.setVersion(schema.getVersion());
209         document.setLanguage(schema.getLanguage());
210         document.setAttributeFormDefault(schema.getAttributeFormDefaultEffective());
211         document.setElementFormDefault(schema.getElementFormDefaultEffective());
212     }
213     
214     public static void updateGlobalAttribute(Attribute attribute) {
215         GlobalAttribute component = (GlobalAttribute)attribute.getPeer();
216         attribute.setName(component.getName());
217         attribute.setDefault(component.getDefault());
218         attribute.setFixed(component.getFixed());
219     }
220     
221     public static void updateLocalAttribute(Attribute attribute) {
222         LocalAttribute component = (LocalAttribute)attribute.getPeer();
223         attribute.setName(component.getName());
224         attribute.setDefault(component.getDefault());
225         attribute.setFixed(component.getFixed());
226         attribute.setForm(component.getFormEffective());
227         attribute.setUse(component.getUseEffective());
228     }
229     
230     public static void updateAttributeReference(Attribute attribute) {
231         AttributeReference component = (AttributeReference)attribute.getPeer();
232         //for AttributeRef, only use, default and fixed needs to be updated.
233
attribute.setDefault(component.getDefault());
234         attribute.setFixed(component.getFixed());
235         attribute.setUse(component.getUseEffective());
236     }
237         
238     public static void updateGlobalElement(Element element) {
239         GlobalElement component = (GlobalElement)element.getPeer();
240         element.setName(component.getName());
241         element.setFixed(component.getFixed());
242         element.setDefault(component.getDefault());
243         element.setAbstract(component.getAbstractEffective());
244         element.setNillable(component.getNillableEffective());
245         //element.setContentType(getDatatype(element.getModel(), component));
246
//element.setFinal();
247
//element.setBlock();
248
}
249     
250     public static void updateLocalElement(Element element) {
251         LocalElement component = (LocalElement)element.getPeer();
252         element.setName(component.getName());
253         element.setMaxOccurs(component.getMaxOccursEffective());
254         element.setMinOccurs(String.valueOf(component.getMinOccursEffective()));
255         element.setFixed(component.getFixed());
256         element.setDefault(component.getDefault());
257         element.setNillable(component.getNillableEffective());
258         element.setForm(component.getFormEffective());
259         //element.setBlock(component.getBlockEffective());
260
//element.setContentType(getDatatype(element.getModel(), component));
261
//element.setFinal();
262
}
263     
264     public static void updateElementReference(Element elementRef) {
265         ElementReference component = (ElementReference)elementRef.getPeer();
266         //for an element-ref, get the min and max from that ref
267
elementRef.setMaxOccurs(component.getMaxOccursEffective());
268         elementRef.setMinOccurs(String.valueOf(component.getMinOccursEffective()));
269     }
270         
271     public static void updateCompositor(Compositor compositor) {
272         switch(compositor.getType()) {
273             case SEQUENCE: {
274                 Sequence component = (Sequence)compositor.getPeer();
275                 Cardinality c = component.getCardinality();
276                 if (c != null) {
277                     compositor.setMaxOccurs(c.getMaxOccursEffective());
278                     compositor.setMinOccurs(String.valueOf(c.getMinOccursEffective()));
279                 } else {
280                     compositor.setMaxOccurs("1");
281                     compositor.setMinOccurs("1");
282                 }
283                 break;
284             }
285             case CHOICE: {
286                 Choice component = (Choice)compositor.getPeer();
287                 Cardinality c = component.getCardinality();
288                 if (c != null) {
289                     compositor.setMaxOccurs(c.getMaxOccursEffective());
290                     compositor.setMinOccurs(String.valueOf(c.getMinOccursEffective()));
291                 } else {
292                     compositor.setMaxOccurs("1");
293                     compositor.setMinOccurs("1");
294                 }
295                 break;
296             }
297             case ALL: {
298                 All component = (All)compositor.getPeer();
299                 //Compositor compositor = new All(model, component);
300
//compositor.setMaxOccurs(component.getMaxOccursEffective());
301
compositor.setMinOccurs(String.valueOf(component.getMinOccursEffective()));
302                 break;
303             }
304         }
305         
306     }
307
308     public static void updateContentModel(ContentModel contentModel) {
309         SchemaComponent peer = contentModel.getPeer();
310         if(peer instanceof GlobalComplexType) {
311             contentModel.setName(((GlobalComplexType)peer).getName());
312             return;
313         }
314         if(peer instanceof GlobalAttributeGroup) {
315             contentModel.setName(((GlobalAttributeGroup)peer).getName());
316             return;
317         }
318         if(peer instanceof GlobalGroup) {
319             contentModel.setName(((GlobalGroup)peer).getName());
320             return;
321         }
322     }
323 }
324
Popular Tags