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: /*
043: * AdvancedSchemaComponentCreator.java
044: *
045: * Created on April 20, 2006, 5:05 PM
046: *
047: */
048:
049: package org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype;
050:
051: import java.util.Collection;
052: import java.util.HashSet;
053: import org.netbeans.modules.xml.schema.model.AnyAttribute;
054: import org.netbeans.modules.xml.schema.model.Attribute;
055: import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
056: import org.netbeans.modules.xml.schema.model.AttributeReference;
057: import org.netbeans.modules.xml.schema.model.ComplexContent;
058: import org.netbeans.modules.xml.schema.model.ComplexContentRestriction;
059: import org.netbeans.modules.xml.schema.model.ComplexExtension;
060: import org.netbeans.modules.xml.schema.model.ComplexExtensionDefinition;
061: import org.netbeans.modules.xml.schema.model.ComplexTypeDefinition;
062: import org.netbeans.modules.xml.schema.model.ElementReference;
063: import org.netbeans.modules.xml.schema.model.Enumeration;
064: import org.netbeans.modules.xml.schema.model.Field;
065: import org.netbeans.modules.xml.schema.model.GlobalAttribute;
066: import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
067: import org.netbeans.modules.xml.schema.model.GlobalComplexType;
068: import org.netbeans.modules.xml.schema.model.GlobalElement;
069: import org.netbeans.modules.xml.schema.model.GlobalGroup;
070: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
071: import org.netbeans.modules.xml.schema.model.GroupReference;
072: import org.netbeans.modules.xml.schema.model.Import;
073: import org.netbeans.modules.xml.schema.model.Include;
074: import org.netbeans.modules.xml.schema.model.Redefine;
075: import org.netbeans.modules.xml.schema.model.Key;
076: import org.netbeans.modules.xml.schema.model.KeyRef;
077: import org.netbeans.modules.xml.schema.model.LocalAttribute;
078: import org.netbeans.modules.xml.schema.model.LocalComplexType;
079: import org.netbeans.modules.xml.schema.model.LocalElement;
080: import org.netbeans.modules.xml.schema.model.LocalGroupDefinition;
081: import org.netbeans.modules.xml.schema.model.LocalSimpleType;
082: import org.netbeans.modules.xml.schema.model.LocalType;
083: import org.netbeans.modules.xml.schema.model.SchemaComponent;
084: import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
085: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
086: import org.netbeans.modules.xml.schema.model.Selector;
087: import org.netbeans.modules.xml.schema.model.Sequence;
088: import org.netbeans.modules.xml.schema.model.SimpleContent;
089: import org.netbeans.modules.xml.schema.model.SimpleContentRestriction;
090: import org.netbeans.modules.xml.schema.model.SimpleExtension;
091: import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
092: import org.netbeans.modules.xml.schema.model.Unique;
093: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedGlobalAttributeCustomizer;
094: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedGlobalElementCustomizer;
095: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalAttributeCustomizer;
096: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalElementCustomizer;
097: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ComplexTypeCustomizer;
098: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ImportCustomizer;
099: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.IncludeCustomizer;
100: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.RedefineCustomizer;
101: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.SimpleTypeCustomizer;
102: import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ReferenceCustomizer;
103: import org.netbeans.modules.xml.xam.Nameable;
104: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
105:
106: /**
107: * This is advanced creator which creates templates for some components.
108: *
109: * @author Ajit Bhate
110: */
111: public class AdvancedSchemaComponentCreator extends
112: SchemaComponentCreator {
113: /** Creates a new instance of AdvancedSchemaComponentCreator */
114: public AdvancedSchemaComponentCreator() {
115: }
116:
117: public void visit(ElementReference le) {
118: super .visit(le);
119: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
120: setCustomizer(new ReferenceCustomizer<ElementReference>(
121: SchemaComponentReference.create(le), getParent()));
122: }
123: }
124:
125: public void visit(AttributeReference la) {
126: super .visit(la);
127: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
128: setCustomizer(new ReferenceCustomizer<AttributeReference>(
129: SchemaComponentReference.create(la), getParent()));
130: }
131: }
132:
133: public void visit(GroupReference gr) {
134: super .visit(gr);
135: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
136: setCustomizer(new ReferenceCustomizer<GroupReference>(
137: SchemaComponentReference.create(gr), getParent()));
138: }
139: }
140:
141: public void visit(AttributeGroupReference agr) {
142: super .visit(agr);
143: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
144: setCustomizer(new ReferenceCustomizer<AttributeGroupReference>(
145: SchemaComponentReference.create(agr), getParent()));
146: }
147: }
148:
149: public void visit(Import imp) {
150: super .visit(imp);
151: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
152: setCustomizer(new ImportCustomizer(imp));
153: }
154: }
155:
156: public void visit(Include include) {
157: super .visit(include);
158: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
159: setCustomizer(new IncludeCustomizer(include));
160: }
161: }
162:
163: public void visit(Redefine redefine) {
164: super .visit(redefine);
165: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
166: setCustomizer(new RedefineCustomizer(redefine));
167: }
168: }
169:
170: public void visit(LocalSimpleType lst) {
171: super .visit(lst);
172: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
173: SchemaComponent parent = getParent();
174: SimpleTypeRestriction str = getParent().getModel()
175: .getFactory().createSimpleTypeRestriction();
176: lst.setDefinition(str);
177: setParent(lst);
178: visit(str);
179: setCustomizer(new SimpleTypeCustomizer<LocalSimpleType>(
180: SchemaComponentReference.create(lst), parent,
181: getStringType()));
182: }
183: }
184:
185: public void visit(GlobalSimpleType gst) {
186: super .visit(gst);
187: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
188: updateName(gst, "newSimpleType"); //TODO FIX hardcoding
189: SchemaComponent parent = getParent();
190: SimpleTypeRestriction str = getParent().getModel()
191: .getFactory().createSimpleTypeRestriction();
192: gst.setDefinition(str);
193: setParent(gst);
194: visit(str);
195: setCustomizer(new SimpleTypeCustomizer<GlobalSimpleType>(
196: SchemaComponentReference.create(gst), parent,
197: getStringType()));
198: }
199: }
200:
201: public void visit(SimpleTypeRestriction str) {
202: super .visit(str);
203: if (getOperation() == Operation.ADD
204: || getOperation() == Operation.SHOW_CUSTOMIZER) {
205: // set type to primitive string
206: NamedComponentReference<GlobalSimpleType> stringType = createStringTypeReference(str);
207: if (stringType != null) {
208: str.setBase(stringType);
209: }
210: }
211: }
212:
213: public void visit(LocalComplexType lct) {
214: if (getOperation() == Operation.VERIFY_ADD) {
215: // exclusivity of ComplexTypeDefinition
216: if (getChild() instanceof ComplexTypeDefinition) {
217: if (lct.getDefinition() != null)
218: setAddAllowed(false);
219: }
220: // mutual exclusive attributes and Complex/SimpleContents
221: if (getChild() instanceof AnyAttribute
222: || getChild() instanceof Attribute
223: || getChild() instanceof AttributeGroupReference) {
224: if (lct.getDefinition() instanceof ComplexContent
225: || lct.getDefinition() instanceof SimpleContent) {
226: setAddAllowed(false);
227: }
228: }
229: // exclusivity of AnyAttribute
230: if (getChild() instanceof AnyAttribute) {
231: if (lct.getAnyAttribute() != null)
232: setAddAllowed(false);
233: }
234: setVisitLevels(4);
235: }
236: super .visit(lct);
237: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
238: Sequence sequence = getParent().getModel().getFactory()
239: .createSequence();
240: lct.setDefinition(sequence);
241: setCustomizer(new ComplexTypeCustomizer<LocalComplexType>(
242: SchemaComponentReference.create(lct), getParent()));
243: }
244: }
245:
246: public void visit(GlobalComplexType gct) {
247: if (getOperation() == Operation.VERIFY_ADD) {
248: // exclusivity of ComplexTypeDefinition
249: if (getChild() instanceof ComplexTypeDefinition) {
250: if (gct.getDefinition() != null)
251: setAddAllowed(false);
252: }
253: // mutual exclusive attributes and Complex/SimpleContents
254: if (getChild() instanceof AnyAttribute
255: || getChild() instanceof Attribute
256: || getChild() instanceof AttributeGroupReference) {
257: if (gct.getDefinition() instanceof ComplexContent
258: || gct.getDefinition() instanceof SimpleContent) {
259: setAddAllowed(false);
260: }
261: }
262: // anyAttribute can be 0 or 1
263: if (getChild() instanceof AnyAttribute) {
264: if (gct.getAnyAttribute() != null)
265: setAddAllowed(false);
266: }
267: // we will look for
268: //1 complextype
269: //2 complexcontent
270: //3 extension
271: //4 sequence
272: setVisitLevels(4);
273: }
274: super .visit(gct);
275: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
276: updateName(gct, "newComplexType"); //TODO FIX hardcoding
277: Sequence sequence = getParent().getModel().getFactory()
278: .createSequence();
279: gct.setDefinition(sequence);
280: setCustomizer(new ComplexTypeCustomizer<GlobalComplexType>(
281: SchemaComponentReference.create(gct), getParent()));
282: }
283: }
284:
285: public void visit(ComplexContentRestriction ccr) {
286: if (getOperation() == Operation.VERIFY_ADD) {
287: // exclusivity of ComplexTypeDefinition
288: if (getChild() instanceof ComplexTypeDefinition) {
289: if (ccr.getDefinition() != null)
290: setAddAllowed(false);
291: }
292: // anyAttribute can be 0 or 1
293: if (getChild() instanceof AnyAttribute) {
294: if (ccr.getAnyAttribute() != null)
295: setAddAllowed(false);
296: }
297: }
298: super .visit(ccr);
299: }
300:
301: public void visit(ComplexExtension ce) {
302: if (getOperation() == Operation.VERIFY_ADD) {
303: // exclusivity of ComplexExtensionDefinition
304: if (getChild() instanceof ComplexExtensionDefinition) {
305: if (ce.getLocalDefinition() != null)
306: setAddAllowed(false);
307: }
308: // anyAttribute can be 0 or 1
309: if (getChild() instanceof AnyAttribute) {
310: if (ce.getAnyAttribute() != null)
311: setAddAllowed(false);
312: }
313: }
314: super .visit(ce);
315: }
316:
317: public void visit(SimpleContentRestriction scr) {
318: if (getOperation() == Operation.VERIFY_ADD) {
319: // anyAttribute can be 0 or 1
320: if (getChild() instanceof AnyAttribute) {
321: if (scr.getAnyAttribute() != null)
322: setAddAllowed(false);
323: }
324: }
325: super .visit(scr);
326: }
327:
328: public void visit(SimpleExtension se) {
329: if (getOperation() == Operation.VERIFY_ADD) {
330: // anyAttribute can be 0 or 1
331: if (getChild() instanceof AnyAttribute) {
332: if (se.getAnyAttribute() != null)
333: setAddAllowed(false);
334: }
335: }
336: super .visit(se);
337: }
338:
339: public void visit(GlobalAttributeGroup gag) {
340: if (getOperation() == Operation.VERIFY_ADD) {
341: // anyAttribute can be 0 or 1
342: if (getChild() instanceof AnyAttribute) {
343: if (gag.getAnyAttribute() != null)
344: setAddAllowed(false);
345: }
346: }
347: super .visit(gag);
348: if (getOperation() == Operation.ADD) {
349: updateName(gag, "newAttributeGroup"); //TODO FIX hardcoding
350: }
351: }
352:
353: public void visit(LocalElement le) {
354: if (getOperation() == Operation.VERIFY_ADD) {
355: // exclusivity of type definition
356: if (getChild() instanceof LocalType) {
357: if (le.getType() != null || le.getInlineType() != null)
358: setAddAllowed(false);
359: }
360: // we will look for
361: //1 element
362: //2 complextype
363: //3 complexcontent
364: //4 extension
365: //5 sequence
366: setVisitLevels(5);
367: }
368: super .visit(le);
369: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
370: updateName(le, "newElement"); //TODO FIX hardcoding
371: LocalComplexType lct = getParent().getModel().getFactory()
372: .createLocalComplexType();
373: le.setInlineType(lct);
374: Sequence sequence = getParent().getModel().getFactory()
375: .createSequence();
376: lct.setDefinition(sequence);
377: setCustomizer(new AdvancedLocalElementCustomizer(
378: SchemaComponentReference.create(le), getParent()));
379: }
380: }
381:
382: public void visit(GlobalElement ge) {
383: if (getOperation() == Operation.VERIFY_ADD) {
384: // exclusivity of type definition
385: if (getChild() instanceof LocalType) {
386: if (ge.getType() != null || ge.getInlineType() != null)
387: setAddAllowed(false);
388: }
389: setVisitLevels(5);
390: }
391: super .visit(ge);
392: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
393: updateName(ge, "newElement"); //TODO FIX hardcoding
394: LocalComplexType lct = getParent().getModel().getFactory()
395: .createLocalComplexType();
396: ge.setInlineType(lct);
397: Sequence sequence = getParent().getModel().getFactory()
398: .createSequence();
399: lct.setDefinition(sequence);
400: setCustomizer(new AdvancedGlobalElementCustomizer(
401: SchemaComponentReference.create(ge), getParent()));
402: }
403: }
404:
405: public void visit(GlobalAttribute ga) {
406: if (getOperation() == Operation.VERIFY_ADD) {
407: // exclusivity of type definition
408: if (getChild() instanceof LocalType) {
409: if (ga.getType() != null || ga.getInlineType() != null)
410: setAddAllowed(false);
411: }
412: }
413: super .visit(ga);
414: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
415: updateName(ga, "newAttribute"); //TODO FIX hardcoding
416: // set type to primitive string
417: NamedComponentReference<GlobalSimpleType> stringType = createStringTypeReference(ga);
418: if (stringType != null) {
419: ga.setType(stringType);
420: }
421: setCustomizer(new AdvancedGlobalAttributeCustomizer(
422: SchemaComponentReference.create(ga), getParent(),
423: getStringType()));
424: }
425: }
426:
427: public void visit(LocalAttribute la) {
428: if (getOperation() == Operation.VERIFY_ADD) {
429: // exclusivity of type definition
430: if (getChild() instanceof LocalType) {
431: if (la.getType() != null || la.getInlineType() != null)
432: setAddAllowed(false);
433: }
434: }
435: super .visit(la);
436: if (getOperation() == Operation.SHOW_CUSTOMIZER) {
437: updateName(la, "newAttribute"); //TODO FIX hardcoding
438: // set type to primitive string
439: NamedComponentReference<GlobalSimpleType> stringType = createStringTypeReference(la);
440: if (stringType != null) {
441: la.setType(stringType);
442: }
443: setCustomizer(new AdvancedLocalAttributeCustomizer(
444: SchemaComponentReference.create(la), getParent(),
445: getStringType()));
446: }
447: }
448:
449: public void visit(GlobalGroup gd) {
450: if (getOperation() == Operation.VERIFY_ADD) {
451: // exclusivity of group definition
452: if (getChild() instanceof LocalGroupDefinition) {
453: if (gd.getDefinition() != null)
454: setAddAllowed(false);
455: }
456: }
457: super .visit(gd);
458: if (getOperation() == Operation.ADD) {
459: updateName(gd, "newGroup"); //TODO FIX hardcoding
460: Sequence s = getParent().getModel().getFactory()
461: .createSequence();
462: setParent(gd);
463: visit(s);
464: }
465: }
466:
467: public void visit(Unique u) {
468: if (getOperation() == Operation.VERIFY_ADD) {
469: // Selector can be 1
470: if (getChild() instanceof Selector) {
471: if (u.getSelector() != null)
472: setAddAllowed(false);
473: }
474: }
475: super .visit(u);
476: if (getOperation() == Operation.ADD) {
477: updateName(u, "newUnique");//TODO FIX hardcoding
478: setParent(u);
479: Selector s = getParent().getModel().getFactory()
480: .createSelector();
481: visit(s);
482: Field f = getParent().getModel().getFactory().createField();
483: visit(f);
484: }
485: }
486:
487: public void visit(Key key) {
488: if (getOperation() == Operation.VERIFY_ADD) {
489: // Selector can be 1
490: if (getChild() instanceof Selector) {
491: if (key.getSelector() != null)
492: setAddAllowed(false);
493: }
494: }
495: super .visit(key);
496: if (getOperation() == Operation.ADD) {
497: updateName(key, "newKey");//TODO FIX hardcoding
498: setParent(key);
499: Selector s = getParent().getModel().getFactory()
500: .createSelector();
501: visit(s);
502: Field f = getParent().getModel().getFactory().createField();
503: visit(f);
504: }
505: }
506:
507: public void visit(KeyRef kr) {
508: if (getOperation() == Operation.VERIFY_ADD) {
509: // Selector can be 1
510: if (getChild() instanceof Selector) {
511: if (kr.getSelector() != null)
512: setAddAllowed(false);
513: }
514: }
515: super .visit(kr);
516: if (getOperation() == Operation.ADD) {
517: updateName(kr, "newKeyRef");//TODO FIX hardcoding
518: setParent(kr);
519: Selector s = getParent().getModel().getFactory()
520: .createSelector();
521: visit(s);
522: Field f = getParent().getModel().getFactory().createField();
523: visit(f);
524: }
525: }
526:
527: public void visit(Enumeration e) {
528: super .visit(e);
529: if (getOperation() == Operation.ADD) {
530: e.setValue("");
531: }
532: }
533:
534: public void visit(Selector s) {
535: super .visit(s);
536: if (getOperation() == Operation.ADD) {
537: s.setXPath("");
538: }
539: }
540:
541: public void visit(Field f) {
542: super .visit(f);
543: if (getOperation() == Operation.ADD) {
544: f.setXPath("");
545: }
546: }
547:
548: private void updateName(final Nameable<SchemaComponent> component,
549: final String preferedName) {
550: String name = preferedName;
551: HashSet<String> nameSet = new HashSet<String>();
552: String takenName;
553: for (SchemaComponent child : getParent().getChildren(
554: ((SchemaComponent) component).getComponentType())) {
555: takenName = ((Nameable) child).getName();
556: if (takenName != null)
557: nameSet.add(takenName);
558: }
559: int cnt = 1;
560: if (nameSet.size() < Integer.MAX_VALUE) {
561: while (nameSet.contains(name)) {
562: name = preferedName.concat("" + cnt++);
563: }
564: }
565: component.setName(name);
566: }
567:
568: // creates primitive string global reference
569: private NamedComponentReference<GlobalSimpleType> createStringTypeReference(
570: SchemaComponent component) {
571: GlobalSimpleType stringType = getStringType();
572: if (stringType != null)
573: return getParent().getModel().getFactory()
574: .createGlobalReference(stringType,
575: GlobalSimpleType.class, component);
576: return null;
577: }
578:
579: private GlobalSimpleType getStringType() {
580: Collection<GlobalSimpleType> types = SchemaModelFactory
581: .getDefault().getPrimitiveTypesModel().getSchema()
582: .getSimpleTypes();
583: for (GlobalSimpleType type : types) {
584: if (type.getName().equals("string")) {
585: return type;
586: }
587: }
588: return null;
589: }
590: }
|