KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > AXIComponentFactory


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 package org.netbeans.modules.xml.axi;
20
21 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
22 import org.netbeans.modules.xml.axi.Compositor.CompositorType;
23 import org.netbeans.modules.xml.axi.ContentModel.ContentModelType;
24 import org.netbeans.modules.xml.axi.impl.AnyAttributeProxy;
25 import org.netbeans.modules.xml.axi.impl.AnyElementProxy;
26 import org.netbeans.modules.xml.axi.impl.AttributeImpl;
27 import org.netbeans.modules.xml.axi.impl.AttributeProxy;
28 import org.netbeans.modules.xml.axi.impl.AttributeRef;
29 import org.netbeans.modules.xml.axi.impl.CompositorProxy;
30 import org.netbeans.modules.xml.axi.impl.ElementImpl;
31 import org.netbeans.modules.xml.axi.impl.ElementProxy;
32 import org.netbeans.modules.xml.axi.impl.ElementRef;
33 import org.netbeans.modules.xml.axi.visitor.DefaultVisitor;
34 import org.netbeans.modules.xml.schema.model.SchemaComponent;
35
36 /**
37  * Factory class to help create various AXI components.
38  *
39  * @author Samaresh (Samaresh.Panda@Sun.Com)
40  */

41 public class AXIComponentFactory {
42         
43     /**
44      * Creates a new instance of AXIComponentFactory.
45      */

46     AXIComponentFactory(AXIModel model) {
47         this.model = model;
48     }
49     
50     public long getComponentCount() {
51         return elementCount + attributeCount +
52                compositorCount + contentModelCount +
53                proxyComponentCount + 1;
54     }
55     
56     /**
57      * Creates a copy of the original.
58      */

59     public AXIComponent copy(AXIComponent original) {
60         AXICopier copier = new AXICopier(model);
61         return copier.copy(original);
62     }
63     
64     /**
65      * Creates a new Element.
66      */

67     public Element createElement() {
68         elementCount++;
69         return new ElementImpl(model);
70     }
71     
72     /**
73      * Creates a new Element.
74      */

75     public Element createElement(SchemaComponent component) {
76         elementCount++;
77         return new ElementImpl(model, component);
78     }
79     
80     /**
81      * Creates a new Element reference.
82      */

83     public Element createElementReference(Element referent) {
84         elementCount++;
85         return new ElementRef(model, referent);
86     }
87     
88     /**
89      * Creates a new Element reference.
90      */

91     public Element createElementReference(SchemaComponent component, Element referent) {
92         elementCount++;
93         return new ElementRef(model, component, referent);
94     }
95     
96     /**
97      * Creates a new Attribute.
98      */

99     public Attribute createAttribute() {
100         attributeCount++;
101         return new AttributeImpl(model);
102     }
103         
104     /**
105      * Creates a new Attribute.
106      */

107     public Attribute createAttribute(SchemaComponent component) {
108         attributeCount++;
109         return new AttributeImpl(model, component);
110     }
111     
112     /**
113      * Creates a new Attribute reference.
114      */

115     public Attribute createAttributeReference(Attribute referent) {
116         attributeCount++;
117         return new AttributeRef(model, referent);
118     }
119     
120     /**
121      * Creates a new Attribute reference.
122      */

123     public Attribute createAttributeReference(SchemaComponent component, Attribute referent) {
124         attributeCount++;
125         return new AttributeRef(model, component, referent);
126     }
127     
128     /**
129      * Creates a new instance of compositor.
130      */

131     public Compositor createCompositor(CompositorType type) {
132         compositorCount++;
133         Compositor compositor = new Compositor(model, type);
134         return compositor;
135     }
136     
137     /**
138      * Creates a new instance Sequence compositor.
139      */

140     public Compositor createSequence() {
141         compositorCount++;
142         return new Compositor(model, CompositorType.SEQUENCE);
143     }
144     
145     /**
146      * Creates a new instance Sequence compositor.
147      */

148     public Compositor createSequence(SchemaComponent component) {
149         compositorCount++;
150         Compositor compositor = new Compositor(model, component);
151         return compositor;
152     }
153     
154     /**
155      * Creates a new instance Choice compositor.
156      */

157     public Compositor createChoice() {
158         compositorCount++;
159         return new Compositor(model, CompositorType.CHOICE);
160     }
161     
162     /**
163      * Creates a new instance Choice compositor.
164      */

165     public Compositor createChoice(SchemaComponent component) {
166         compositorCount++;
167         Compositor compositor = new Compositor(model, component);
168         return compositor;
169     }
170     
171     /**
172      * Creates a new instance All compositor.
173      */

174     public Compositor createAll() {
175         compositorCount++;
176         return new Compositor(model, CompositorType.ALL);
177     }
178     
179     /**
180      * Creates a new instance All compositor.
181      */

182     public Compositor createAll(SchemaComponent component) {
183         compositorCount++;
184         Compositor compositor = new Compositor(model, component);
185         return compositor;
186     }
187     
188     /**
189      * Creates a new instance AnyElement.
190      */

191     public AnyElement createAnyElement() {
192         compositorCount++;
193         return new AnyElement(model);
194     }
195     
196     /**
197      * Creates a new instance AnyElement.
198      */

199     public AnyElement createAnyElement(SchemaComponent component) {
200         compositorCount++;
201         return new AnyElement(model, component);
202     }
203     
204     /**
205      * Creates a new instance AnyAttribute.
206      */

207     public AnyAttribute createAnyAttribute() {
208         compositorCount++;
209         return new AnyAttribute(model);
210     }
211     /**
212      * Creates a new instance AnyAttribute.
213      */

214     public AnyAttribute createAnyAttribute(SchemaComponent component) {
215         compositorCount++;
216         return new AnyAttribute(model, component);
217     }
218     
219     /**
220      * Creates a ComplexType.
221      */

222     public ContentModel createComplexType() {
223         contentModelCount++;
224         return new ContentModel(model, ContentModelType.COMPLEX_TYPE);
225     }
226     
227     /**
228      * Creates a Group.
229      */

230     public ContentModel createGroup() {
231         contentModelCount++;
232         return new ContentModel(model, ContentModelType.GROUP);
233     }
234     
235     /**
236      * Creates an AttributeGroup.
237      */

238     public ContentModel createAttributeGroup() {
239         contentModelCount++;
240         return new ContentModel(model, ContentModelType.ATTRIBUTE_GROUP);
241     }
242     
243     /**
244      * Creates a ContentModel.
245      */

246     public ContentModel createContentModel(ContentModelType type) {
247         contentModelCount++;
248         return new ContentModel(model, type);
249     }
250     
251     /**
252      * Creates a ContentModel.
253      */

254     public ContentModel createContentModel(SchemaComponent component) {
255         contentModelCount++;
256         return new ContentModel(model, component);
257     }
258     
259     public AXIComponent createProxy(AXIComponent original) {
260         proxyComponentCount++;
261         return new ProxyComponentFactory().createProxy(original);
262     }
263     
264     public String JavaDoc toString() {
265         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
266         buffer.append("elementCount: " + elementCount + "\n");
267         buffer.append("attributeCount: " + attributeCount + "\n");
268         buffer.append("compositorCount: " + compositorCount + "\n");
269         buffer.append("contentModelCount: " + contentModelCount + "\n");
270         buffer.append("proxyComponentCount: " + proxyComponentCount + "\n");
271         return buffer.toString();
272     }
273     
274     /**
275      * Creates a proxy for an AXIComponent.
276      */

277     private class ProxyComponentFactory extends DefaultVisitor {
278         
279         private AXIComponent proxyComponent;
280         
281         ProxyComponentFactory() {
282         }
283         
284         AXIComponent createProxy(AXIComponent original) {
285             original.accept(this);
286             return proxyComponent;
287         }
288         
289         public void visit(Element element) {
290             proxyComponent = new ElementProxy(model, element);
291         }
292         
293         public void visit(AnyElement element) {
294             proxyComponent = new AnyElementProxy(model, element);
295         }
296         
297         public void visit(Attribute attribute) {
298             proxyComponent = new AttributeProxy(model, attribute);
299         }
300         
301         public void visit(AnyAttribute attribute) {
302             proxyComponent = new AnyAttributeProxy(model, attribute);
303         }
304         
305         public void visit(Compositor compositor) {
306             proxyComponent = new CompositorProxy(model, compositor);
307         }
308     }
309     
310     /**
311      * Creates a copy of the specified AXIComponent.
312      */

313     private class AXICopier extends DefaultVisitor {
314         
315         private AXIComponent copiedComponent;
316         private AXIModel model;
317         
318         /**
319          * Creates a new instance of AXICopier
320          */

321         public AXICopier(AXIModel model) {
322             this.model = model;
323         }
324         
325         public AXIComponent copy(AXIComponent original) {
326             //if proxy, create a new proxy, initialize and return
327
if(original.getComponentType() == ComponentType.PROXY) {
328                 AXIComponentFactory f = model.getComponentFactory();
329                 copiedComponent = f.createProxy(original.getSharedComponent());
330                 assert(copiedComponent != null);
331                 return copiedComponent;
332             }
333             
334             //visit so that it'll get created
335
original.accept(this);
336             assert(copiedComponent != null);
337             return copiedComponent;
338         }
339         
340         public void visit(Element element) {
341             if(element instanceof ElementRef) {
342                 ElementRef ref = (ElementRef)element;
343                 copiedComponent = model.getComponentFactory().
344                         createElementReference(ref.getReferent());
345                 ((Element)copiedComponent).setMaxOccurs(element.getMaxOccurs());
346                 ((Element)copiedComponent).setMinOccurs(element.getMinOccurs());
347                 return;
348             }
349             copiedComponent = model.getComponentFactory().
350                     createElement();
351             ((Element)copiedComponent).setAbstract(element.getAbstract());
352             ((Element)copiedComponent).setBlock(element.getBlock());
353             ((Element)copiedComponent).setDefault(element.getDefault());
354             ((Element)copiedComponent).setFinal(element.getFinal());
355             ((Element)copiedComponent).setForm(element.getForm());
356             ((Element)copiedComponent).setFixed(element.getFixed());
357             ((Element)copiedComponent).setMaxOccurs(element.getMaxOccurs());
358             ((Element)copiedComponent).setMinOccurs(element.getMinOccurs());
359             ((Element)copiedComponent).setName(element.getName());
360             ((Element)copiedComponent).setNillable(element.getNillable());
361         }
362         
363         public void visit(AnyElement element) {
364             copiedComponent = model.getComponentFactory().
365                     createAnyElement();
366             ((AnyElement)copiedComponent).setProcessContents(element.getProcessContents());
367             ((AnyElement)copiedComponent).setTargetNamespace(element.getTargetNamespace());
368         }
369         
370         public void visit(Attribute attribute) {
371             if(attribute instanceof AttributeRef) {
372                 AttributeRef ref = (AttributeRef)attribute;
373                 copiedComponent = model.getComponentFactory().
374                         createAttributeReference(ref.getReferent());
375                 ((Attribute)copiedComponent).setFixed(attribute.getFixed());
376                 ((Attribute)copiedComponent).setDefault(attribute.getDefault());
377                 ((Attribute)copiedComponent).setUse(attribute.getUse());
378                 return;
379             }
380             copiedComponent = model.getComponentFactory().
381                     createAttribute();
382             ((Attribute)copiedComponent).setDefault(attribute.getDefault());
383             ((Attribute)copiedComponent).setFixed(attribute.getFixed());
384             ((Attribute)copiedComponent).setForm(attribute.getForm());
385             ((Attribute)copiedComponent).setUse(attribute.getUse());
386             ((Attribute)copiedComponent).setName(attribute.getName());
387         }
388         
389         public void visit(AnyAttribute attribute) {
390             copiedComponent = model.getComponentFactory().
391                     createAnyAttribute();
392             ((AnyAttribute)copiedComponent).setProcessContents(attribute.getProcessContents());
393             ((AnyAttribute)copiedComponent).setTargetNamespace(attribute.getTargetNamespace());
394         }
395         
396         public void visit(Compositor compositor) {
397             copiedComponent = model.getComponentFactory().
398                     createCompositor(compositor.getType());
399             ((Compositor)copiedComponent).setMaxOccurs(compositor.getMaxOccurs());
400             ((Compositor)copiedComponent).setMinOccurs(compositor.getMinOccurs());
401         }
402         
403         public void visit(ContentModel contentModel) {
404             copiedComponent = model.getComponentFactory().
405                     createContentModel(contentModel.getType());
406         }
407     }
408         
409     /////////////////////////////////////////////////////////////////////
410
////////////////////////// member variables ////////////////////////
411
/////////////////////////////////////////////////////////////////////
412
private AXIModel model;
413     private long elementCount;
414     private long attributeCount;
415     private long compositorCount;
416     private long contentModelCount;
417     private long proxyComponentCount;
418 }
419
Popular Tags