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: package org.netbeans.modules.xml.axi;
042:
043: import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
044: import org.netbeans.modules.xml.axi.Compositor.CompositorType;
045: import org.netbeans.modules.xml.axi.ContentModel.ContentModelType;
046: import org.netbeans.modules.xml.axi.impl.AnyAttributeProxy;
047: import org.netbeans.modules.xml.axi.impl.AnyElementProxy;
048: import org.netbeans.modules.xml.axi.impl.AttributeImpl;
049: import org.netbeans.modules.xml.axi.impl.AttributeProxy;
050: import org.netbeans.modules.xml.axi.impl.AttributeRef;
051: import org.netbeans.modules.xml.axi.impl.CompositorProxy;
052: import org.netbeans.modules.xml.axi.impl.ElementImpl;
053: import org.netbeans.modules.xml.axi.impl.ElementProxy;
054: import org.netbeans.modules.xml.axi.impl.ElementRef;
055: import org.netbeans.modules.xml.axi.visitor.DefaultVisitor;
056: import org.netbeans.modules.xml.schema.model.SchemaComponent;
057:
058: /**
059: * Factory class to help create various AXI components.
060: *
061: * @author Samaresh (Samaresh.Panda@Sun.Com)
062: */
063: public class AXIComponentFactory {
064:
065: /**
066: * Creates a new instance of AXIComponentFactory.
067: */
068: AXIComponentFactory(AXIModel model) {
069: this .model = model;
070: }
071:
072: public long getComponentCount() {
073: return elementCount + attributeCount + compositorCount
074: + contentModelCount + proxyComponentCount + 1;
075: }
076:
077: /**
078: * Creates a copy of the original.
079: */
080: public AXIComponent copy(AXIComponent original) {
081: AXICopier copier = new AXICopier(model);
082: return copier.copy(original);
083: }
084:
085: /**
086: * Creates a new Element.
087: */
088: public Element createElement() {
089: elementCount++;
090: return new ElementImpl(model);
091: }
092:
093: /**
094: * Creates a new Element.
095: */
096: public Element createElement(SchemaComponent component) {
097: elementCount++;
098: return new ElementImpl(model, component);
099: }
100:
101: /**
102: * Creates a new Element reference.
103: */
104: public Element createElementReference(Element referent) {
105: elementCount++;
106: return new ElementRef(model, referent);
107: }
108:
109: /**
110: * Creates a new Element reference.
111: */
112: public Element createElementReference(SchemaComponent component,
113: Element referent) {
114: elementCount++;
115: return new ElementRef(model, component, referent);
116: }
117:
118: /**
119: * Creates a new Attribute.
120: */
121: public Attribute createAttribute() {
122: attributeCount++;
123: return new AttributeImpl(model);
124: }
125:
126: /**
127: * Creates a new Attribute.
128: */
129: public Attribute createAttribute(SchemaComponent component) {
130: attributeCount++;
131: return new AttributeImpl(model, component);
132: }
133:
134: /**
135: * Creates a new Attribute reference.
136: */
137: public Attribute createAttributeReference(Attribute referent) {
138: attributeCount++;
139: return new AttributeRef(model, referent);
140: }
141:
142: /**
143: * Creates a new Attribute reference.
144: */
145: public Attribute createAttributeReference(
146: SchemaComponent component, Attribute referent) {
147: attributeCount++;
148: return new AttributeRef(model, component, referent);
149: }
150:
151: /**
152: * Creates a new instance of compositor.
153: */
154: public Compositor createCompositor(CompositorType type) {
155: compositorCount++;
156: Compositor compositor = new Compositor(model, type);
157: return compositor;
158: }
159:
160: /**
161: * Creates a new instance Sequence compositor.
162: */
163: public Compositor createSequence() {
164: compositorCount++;
165: return new Compositor(model, CompositorType.SEQUENCE);
166: }
167:
168: /**
169: * Creates a new instance Sequence compositor.
170: */
171: public Compositor createSequence(SchemaComponent component) {
172: compositorCount++;
173: Compositor compositor = new Compositor(model, component);
174: return compositor;
175: }
176:
177: /**
178: * Creates a new instance Choice compositor.
179: */
180: public Compositor createChoice() {
181: compositorCount++;
182: return new Compositor(model, CompositorType.CHOICE);
183: }
184:
185: /**
186: * Creates a new instance Choice compositor.
187: */
188: public Compositor createChoice(SchemaComponent component) {
189: compositorCount++;
190: Compositor compositor = new Compositor(model, component);
191: return compositor;
192: }
193:
194: /**
195: * Creates a new instance All compositor.
196: */
197: public Compositor createAll() {
198: compositorCount++;
199: return new Compositor(model, CompositorType.ALL);
200: }
201:
202: /**
203: * Creates a new instance All compositor.
204: */
205: public Compositor createAll(SchemaComponent component) {
206: compositorCount++;
207: Compositor compositor = new Compositor(model, component);
208: return compositor;
209: }
210:
211: /**
212: * Creates a new instance AnyElement.
213: */
214: public AnyElement createAnyElement() {
215: compositorCount++;
216: return new AnyElement(model);
217: }
218:
219: /**
220: * Creates a new instance AnyElement.
221: */
222: public AnyElement createAnyElement(SchemaComponent component) {
223: compositorCount++;
224: return new AnyElement(model, component);
225: }
226:
227: /**
228: * Creates a new instance AnyAttribute.
229: */
230: public AnyAttribute createAnyAttribute() {
231: compositorCount++;
232: return new AnyAttribute(model);
233: }
234:
235: /**
236: * Creates a new instance AnyAttribute.
237: */
238: public AnyAttribute createAnyAttribute(SchemaComponent component) {
239: compositorCount++;
240: return new AnyAttribute(model, component);
241: }
242:
243: /**
244: * Creates a ComplexType.
245: */
246: public ContentModel createComplexType() {
247: contentModelCount++;
248: return new ContentModel(model, ContentModelType.COMPLEX_TYPE);
249: }
250:
251: /**
252: * Creates a Group.
253: */
254: public ContentModel createGroup() {
255: contentModelCount++;
256: return new ContentModel(model, ContentModelType.GROUP);
257: }
258:
259: /**
260: * Creates an AttributeGroup.
261: */
262: public ContentModel createAttributeGroup() {
263: contentModelCount++;
264: return new ContentModel(model, ContentModelType.ATTRIBUTE_GROUP);
265: }
266:
267: /**
268: * Creates a ContentModel.
269: */
270: public ContentModel createContentModel(ContentModelType type) {
271: contentModelCount++;
272: return new ContentModel(model, type);
273: }
274:
275: /**
276: * Creates a ContentModel.
277: */
278: public ContentModel createContentModel(SchemaComponent component) {
279: contentModelCount++;
280: return new ContentModel(model, component);
281: }
282:
283: public AXIComponent createProxy(AXIComponent original) {
284: proxyComponentCount++;
285: return new ProxyComponentFactory().createProxy(original);
286: }
287:
288: public String toString() {
289: StringBuffer buffer = new StringBuffer();
290: buffer.append("elementCount: " + elementCount + "\n");
291: buffer.append("attributeCount: " + attributeCount + "\n");
292: buffer.append("compositorCount: " + compositorCount + "\n");
293: buffer.append("contentModelCount: " + contentModelCount + "\n");
294: buffer.append("proxyComponentCount: " + proxyComponentCount
295: + "\n");
296: return buffer.toString();
297: }
298:
299: /**
300: * Creates a proxy for an AXIComponent.
301: */
302: private class ProxyComponentFactory extends DefaultVisitor {
303:
304: private AXIComponent proxyComponent;
305:
306: ProxyComponentFactory() {
307: }
308:
309: AXIComponent createProxy(AXIComponent original) {
310: original.accept(this );
311: return proxyComponent;
312: }
313:
314: public void visit(Element element) {
315: proxyComponent = new ElementProxy(model, element);
316: }
317:
318: public void visit(AnyElement element) {
319: proxyComponent = new AnyElementProxy(model, element);
320: }
321:
322: public void visit(Attribute attribute) {
323: proxyComponent = new AttributeProxy(model, attribute);
324: }
325:
326: public void visit(AnyAttribute attribute) {
327: proxyComponent = new AnyAttributeProxy(model, attribute);
328: }
329:
330: public void visit(Compositor compositor) {
331: proxyComponent = new CompositorProxy(model, compositor);
332: }
333: }
334:
335: /**
336: * Creates a copy of the specified AXIComponent.
337: */
338: private class AXICopier extends DefaultVisitor {
339:
340: private AXIComponent copiedComponent;
341: private AXIModel model;
342:
343: /**
344: * Creates a new instance of AXICopier
345: */
346: public AXICopier(AXIModel model) {
347: this .model = model;
348: }
349:
350: public AXIComponent copy(AXIComponent original) {
351: //if proxy, create a new proxy, initialize and return
352: if (original.getComponentType() == ComponentType.PROXY) {
353: AXIComponentFactory f = model.getComponentFactory();
354: copiedComponent = f.createProxy(original
355: .getSharedComponent());
356: assert (copiedComponent != null);
357: return copiedComponent;
358: }
359:
360: //visit so that it'll get created
361: original.accept(this );
362: assert (copiedComponent != null);
363: return copiedComponent;
364: }
365:
366: public void visit(Element element) {
367: if (element instanceof ElementRef) {
368: ElementRef ref = (ElementRef) element;
369: copiedComponent = model.getComponentFactory()
370: .createElementReference(ref.getReferent());
371: ((Element) copiedComponent).setMaxOccurs(element
372: .getMaxOccurs());
373: ((Element) copiedComponent).setMinOccurs(element
374: .getMinOccurs());
375: return;
376: }
377: copiedComponent = model.getComponentFactory()
378: .createElement();
379: ((Element) copiedComponent).setAbstract(element
380: .getAbstract());
381: ((Element) copiedComponent).setBlock(element.getBlock());
382: ((Element) copiedComponent)
383: .setDefault(element.getDefault());
384: ((Element) copiedComponent).setFinal(element.getFinal());
385: ((Element) copiedComponent).setForm(element.getForm());
386: ((Element) copiedComponent).setFixed(element.getFixed());
387: ((Element) copiedComponent).setMaxOccurs(element
388: .getMaxOccurs());
389: ((Element) copiedComponent).setMinOccurs(element
390: .getMinOccurs());
391: ((Element) copiedComponent).setName(element.getName());
392: ((Element) copiedComponent).setNillable(element
393: .getNillable());
394: }
395:
396: public void visit(AnyElement element) {
397: copiedComponent = model.getComponentFactory()
398: .createAnyElement();
399: ((AnyElement) copiedComponent).setProcessContents(element
400: .getProcessContents());
401: ((AnyElement) copiedComponent).setTargetNamespace(element
402: .getTargetNamespace());
403: }
404:
405: public void visit(Attribute attribute) {
406: if (attribute instanceof AttributeRef) {
407: AttributeRef ref = (AttributeRef) attribute;
408: copiedComponent = model.getComponentFactory()
409: .createAttributeReference(ref.getReferent());
410: ((Attribute) copiedComponent).setFixed(attribute
411: .getFixed());
412: ((Attribute) copiedComponent).setDefault(attribute
413: .getDefault());
414: ((Attribute) copiedComponent)
415: .setUse(attribute.getUse());
416: return;
417: }
418: copiedComponent = model.getComponentFactory()
419: .createAttribute();
420: ((Attribute) copiedComponent).setDefault(attribute
421: .getDefault());
422: ((Attribute) copiedComponent)
423: .setFixed(attribute.getFixed());
424: ((Attribute) copiedComponent).setForm(attribute.getForm());
425: ((Attribute) copiedComponent).setUse(attribute.getUse());
426: ((Attribute) copiedComponent).setName(attribute.getName());
427: }
428:
429: public void visit(AnyAttribute attribute) {
430: copiedComponent = model.getComponentFactory()
431: .createAnyAttribute();
432: ((AnyAttribute) copiedComponent)
433: .setProcessContents(attribute.getProcessContents());
434: ((AnyAttribute) copiedComponent)
435: .setTargetNamespace(attribute.getTargetNamespace());
436: }
437:
438: public void visit(Compositor compositor) {
439: copiedComponent = model.getComponentFactory()
440: .createCompositor(compositor.getType());
441: ((Compositor) copiedComponent).setMaxOccurs(compositor
442: .getMaxOccurs());
443: ((Compositor) copiedComponent).setMinOccurs(compositor
444: .getMinOccurs());
445: }
446:
447: public void visit(ContentModel contentModel) {
448: copiedComponent = model.getComponentFactory()
449: .createContentModel(contentModel.getType());
450: }
451: }
452:
453: /////////////////////////////////////////////////////////////////////
454: ////////////////////////// member variables ////////////////////////
455: /////////////////////////////////////////////////////////////////////
456: private AXIModel model;
457: private long elementCount;
458: private long attributeCount;
459: private long compositorCount;
460: private long contentModelCount;
461: private long proxyComponentCount;
462: }
|