PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/xml.axi/src/org/netbeans/modules/xml/axi/impl/DefaultSchemaGenerator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1130 lines | 908 code | 132 blank | 90 comment | 235 complexity | 0c8d59f591bdc3a34c165c0156ec77b9 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.xml.axi.impl;
  45. import java.io.IOException;
  46. import java.util.ArrayList;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.HashMap;
  50. import java.util.Iterator;
  51. import java.util.Map;
  52. import java.util.Set;
  53. import java.util.SortedMap;
  54. import java.util.TreeMap;
  55. import java.util.Map.Entry;
  56. import org.netbeans.modules.xml.axi.datatype.CustomDatatype;
  57. import org.netbeans.modules.xml.schema.model.*;
  58. import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
  59. import org.netbeans.modules.xml.axi.*;
  60. import org.netbeans.modules.xml.axi.AXIDocument;
  61. import org.netbeans.modules.xml.axi.AXIModel;
  62. import org.netbeans.modules.xml.axi.AXIModelFactory;
  63. import org.netbeans.modules.xml.axi.AXIType;
  64. import org.netbeans.modules.xml.axi.AbstractAttribute;
  65. import org.netbeans.modules.xml.axi.AbstractElement;
  66. import org.netbeans.modules.xml.axi.Attribute;
  67. import org.netbeans.modules.xml.axi.Compositor;
  68. import org.netbeans.modules.xml.axi.ContentModel;
  69. import org.netbeans.modules.xml.axi.Element;
  70. import org.netbeans.modules.xml.axi.SchemaGenerator;
  71. import org.netbeans.modules.xml.axi.SchemaGenerator.PrimitiveCart;
  72. import org.netbeans.modules.xml.axi.SchemaGenerator.UniqueId;
  73. import org.netbeans.modules.xml.axi.datatype.Datatype;
  74. import org.netbeans.modules.xml.axi.visitor.AXINonCyclicVisitor;
  75. /**
  76. *
  77. * @author Ayub Khan
  78. */
  79. public abstract class DefaultSchemaGenerator extends SchemaGenerator {
  80. protected AXIModel am;
  81. protected SchemaModel sm;
  82. protected SchemaGenerator.UniqueId id;
  83. protected SchemaGenerator.PrimitiveCart pc;
  84. java.util.List<AXIComponent> path = new ArrayList<AXIComponent>();
  85. protected SchemaComponent sc;
  86. protected SchemaComponent scParent;
  87. protected SchemaComponent datatypeParent;
  88. protected SortedMap<Integer, java.util.List<Object>> fixNamesMap = null;
  89. protected java.util.List<SchemaComponent> createGlobals = null;
  90. protected HashMap<SchemaComponent, SchemaComponent> refMap = null;
  91. protected HashMap<Class, HashMap<String, SchemaComponent>> namesMap = null;
  92. protected java.util.List<Element> elementReuseList = null;
  93. protected SchemaGeneratorHelper sgh;
  94. private int fgeCount;
  95. public final static int commitRange = Integer.getInteger("schematools.axi.adp", 0);
  96. /**
  97. * Creates a new instance of DefaultSchemaGenerator
  98. */
  99. public DefaultSchemaGenerator(SchemaGenerator.Mode mode) {
  100. super(mode);
  101. id = createUniqueId();
  102. pc = createPrimitiveCart();
  103. fixNamesMap = new TreeMap<Integer, java.util.List<Object>>();
  104. createGlobals = new ArrayList<SchemaComponent>();
  105. refMap = new HashMap<SchemaComponent, SchemaComponent>();
  106. namesMap = new HashMap<Class, HashMap<String, SchemaComponent>>();
  107. elementReuseList = new ArrayList<Element>();
  108. fgeCount = 0;
  109. }
  110. public void updateSchema(SchemaModel sm) throws IOException {
  111. assert getMode() == SchemaGenerator.Mode.UPDATE;
  112. sgh = new UpdateHelper();
  113. sgh.execute(sm);
  114. }
  115. public void transformSchema(SchemaModel sm) throws IOException {
  116. assert getMode() == SchemaGenerator.Mode.TRANSFORM;
  117. sgh = new TransformHelper();
  118. sgh.execute(sm);
  119. }
  120. public void visit(Element element) {
  121. if(getMode() != SchemaGenerator.Mode.TRANSFORM && element instanceof ElementRef)
  122. prepareElementRef((ElementRef)element);
  123. else
  124. prepareLocalElement(element);
  125. }
  126. public void visit(Attribute attribute) {
  127. if(getMode() != SchemaGenerator.Mode.TRANSFORM && attribute instanceof AttributeRef)
  128. prepareAttributeRef((AttributeRef)attribute);
  129. else
  130. prepareLocalAttribute(attribute);
  131. }
  132. public void visit(AXIType type) {
  133. if(type instanceof Datatype)
  134. ((Datatype)type).accept(this);
  135. }
  136. public void visit(Datatype d) {
  137. SchemaGeneratorUtil.createInlineSimpleType(d, sm, this.datatypeParent, pc);
  138. }
  139. public void visit(ContentModel cm) {
  140. if(scParent instanceof Schema) {
  141. GlobalComplexType gct = SchemaGeneratorUtil.createGlobalComplexType(sm);
  142. assert gct != null;
  143. gct.setName(cm.getName());
  144. SchemaGeneratorUtil.populateContentModel(gct, cm);
  145. if(getMode() != SchemaGenerator.Mode.TRANSFORM) {
  146. cm.setPeer(gct);
  147. int index = cm.getIndex(false);
  148. SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), gct, index);
  149. } else
  150. scParent = gct;
  151. scParent = gct;
  152. for(AXIComponent child: cm.getChildren()) {
  153. child.accept(this);
  154. }
  155. }
  156. }
  157. public void visit(Compositor compositor) {
  158. int index = -1;
  159. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  160. index = compositor.getIndex(false);
  161. switch(compositor.getType()) {
  162. case SEQUENCE: {
  163. Sequence seq = null;
  164. if(scParent instanceof ComplexType) {
  165. if(scParent instanceof LocalComplexType) {
  166. LocalComplexType lct = (LocalComplexType) scParent;
  167. if(lct.getDefinition() != null) {
  168. ComplexTypeDefinition ctd = lct.getDefinition();
  169. if(ctd instanceof SimpleContent) {
  170. transformToComplexContent(compositor, ctd);
  171. seq = SchemaGeneratorUtil.createSequence(sm, lct);
  172. } else
  173. seq = SchemaGeneratorUtil.createSequence(sm, ctd, index);
  174. } else
  175. seq = SchemaGeneratorUtil.createSequence(sm,
  176. (LocalComplexType) scParent);
  177. } else if(scParent instanceof GlobalComplexType) {
  178. GlobalComplexType gct = (GlobalComplexType) scParent;
  179. if(gct.getDefinition() != null) {
  180. ComplexTypeDefinition ctd = gct.getDefinition();
  181. if(ctd instanceof SimpleContent) {
  182. transformToComplexContent(compositor, ctd);
  183. seq = SchemaGeneratorUtil.createSequence(sm, gct);
  184. } else
  185. seq = SchemaGeneratorUtil.createSequence(sm, ctd, index);
  186. } else
  187. seq = SchemaGeneratorUtil.createSequence(sm,
  188. (GlobalComplexType) scParent);
  189. }
  190. } else if(scParent instanceof ComplexContentDefinition) {
  191. ComplexContentDefinition ccd = (ComplexContentDefinition) scParent;
  192. if(ccd instanceof ComplexContentRestriction &&
  193. ((ComplexContentRestriction)ccd).getDefinition() != null)
  194. seq = SchemaGeneratorUtil.createSequence(sm,
  195. ((ComplexContentRestriction)ccd).getDefinition(), index);
  196. //TODO
  197. // else if(ccd instanceof ComplexExtension &&
  198. // ((ComplexExtension)ccd).getLocalDefinition() != null)
  199. // seq = ((ComplexExtension)ccd).getLocalDefinition();
  200. else
  201. seq = SchemaGeneratorUtil.createSequence(sm,
  202. (ComplexContentDefinition) scParent);
  203. } else if(scParent instanceof Sequence) {
  204. seq = SchemaGeneratorUtil.createSequence(sm, (Sequence)scParent, index);
  205. } else if(scParent instanceof Choice) {
  206. seq = SchemaGeneratorUtil.createSequence(sm, (Choice)scParent, index);
  207. }
  208. SchemaGeneratorUtil.populateCompositor(seq, compositor);
  209. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  210. compositor.setPeer(seq);
  211. else
  212. scParent = seq;
  213. }
  214. break;
  215. case CHOICE: {
  216. Choice c = null;
  217. if(scParent instanceof ComplexType) {
  218. if(scParent instanceof LocalComplexType) {
  219. LocalComplexType lct = (LocalComplexType) scParent;
  220. if(lct.getDefinition() != null) {
  221. ComplexTypeDefinition ctd = lct.getDefinition();
  222. if(ctd instanceof SimpleContent) {
  223. transformToComplexContent(compositor, ctd);
  224. c = SchemaGeneratorUtil.createChoice(sm, lct);
  225. } else
  226. c = SchemaGeneratorUtil.createChoice(sm, ctd, index);
  227. } else
  228. c = SchemaGeneratorUtil.createChoice(sm,
  229. (LocalComplexType) scParent);
  230. }
  231. if(scParent instanceof GlobalComplexType) {
  232. GlobalComplexType gct = (GlobalComplexType) scParent;
  233. if(gct.getDefinition() != null) {
  234. ComplexTypeDefinition ctd = gct.getDefinition();
  235. if(ctd instanceof SimpleContent) {
  236. transformToComplexContent(compositor, ctd);
  237. c = SchemaGeneratorUtil.createChoice(sm, gct);
  238. } else
  239. c = SchemaGeneratorUtil.createChoice(sm, ctd, index);
  240. }
  241. else
  242. c = SchemaGeneratorUtil.createChoice(sm,
  243. (GlobalComplexType) scParent);
  244. }
  245. } else if(scParent instanceof ComplexContentDefinition) {
  246. ComplexContentDefinition ccd = (ComplexContentDefinition) scParent;
  247. if(ccd instanceof ComplexContentRestriction &&
  248. ((ComplexContentRestriction)ccd).getDefinition() != null)
  249. c = SchemaGeneratorUtil.createChoice(sm,
  250. ((ComplexContentRestriction)ccd).getDefinition(), index);
  251. //TODO
  252. // else if(ccd instanceof ComplexExtension &&
  253. // ((ComplexExtension)ccd).getLocalDefinition() != null)
  254. // seq = ((ComplexExtension)ccd).getLocalDefinition();
  255. else
  256. c = SchemaGeneratorUtil.createChoice(sm,
  257. (ComplexContentDefinition) scParent);
  258. } else if(scParent instanceof Choice) {
  259. c = SchemaGeneratorUtil.createChoice(sm, (Choice)scParent, index);
  260. } else if(scParent instanceof Sequence) {
  261. c = SchemaGeneratorUtil.createChoice(sm, (Sequence)scParent, index);
  262. }
  263. SchemaGeneratorUtil.populateCompositor(c, compositor);
  264. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  265. compositor.setPeer(c);
  266. else
  267. scParent = c;
  268. }
  269. break;
  270. case ALL: {
  271. All a = null;
  272. if(scParent instanceof ComplexType) {
  273. a = SchemaGeneratorUtil.createAll(sm, (ComplexType) scParent);
  274. } else if(scParent instanceof ComplexContentDefinition) {
  275. ComplexContentDefinition ccd = (ComplexContentDefinition) scParent;
  276. if(ccd instanceof ComplexContentRestriction &&
  277. ((ComplexContentRestriction)ccd).getDefinition() != null)
  278. a = (All)
  279. ((ComplexContentRestriction)ccd).getDefinition();
  280. //TODO
  281. // else if(ccd instanceof ComplexExtension &&
  282. // ((ComplexExtension)ccd).getLocalDefinition() != null)
  283. // seq = ((ComplexExtension)ccd).getLocalDefinition();
  284. else
  285. a = SchemaGeneratorUtil.createAll(sm,
  286. (ComplexContentDefinition) scParent);
  287. }
  288. SchemaGeneratorUtil.populateCompositor(a, compositor);
  289. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  290. compositor.setPeer(a);
  291. else
  292. scParent = a;
  293. }
  294. break;
  295. default: assert false;
  296. }
  297. }
  298. private ComplexType transformToComplexContent(Compositor compositor, final ComplexTypeDefinition ctd) {
  299. AXIComponent parent = compositor.getParent();
  300. ComplexType lct = null;
  301. if(ctd instanceof SimpleContent) {
  302. lct = (ComplexType) ctd.getParent();
  303. if(ctd.getChildren().get(0) instanceof SimpleExtension) {
  304. NamedComponentReference base = ((SimpleExtension)ctd.getChildren().get(0)).getBase();
  305. if(base != null && base.get() instanceof GlobalSimpleType) {
  306. SimpleContent sc1 = (SimpleContent) ctd;
  307. copyAttribute(parent, lct, sc1);
  308. copyAttribute(parent, lct, sc1.getLocalDefinition());
  309. }
  310. }
  311. }
  312. return lct;
  313. }
  314. private void copyAttribute(AXIComponent parent, final LocalAttributeContainer lac,
  315. final SchemaComponent scd) {
  316. for(SchemaComponent sc: scd.getChildren()) {
  317. if(sc instanceof org.netbeans.modules.xml.schema.model.Attribute) {
  318. LocalAttribute la = (LocalAttribute)((org.netbeans.modules.xml.schema.model.Attribute)sc).copy(lac);
  319. lac.addLocalAttribute(la);
  320. for(AXIComponent child : parent.getChildren()) {
  321. if(child.getPeer() == sc) {
  322. child.setPeer(la);
  323. }
  324. }
  325. }
  326. }
  327. }
  328. public void visit(AXIComponent c) {
  329. throw new IllegalArgumentException("No action taken on this component: "+
  330. c.toString());
  331. }
  332. protected abstract SchemaGenerator.Pattern getSchemaDesignPattern();
  333. ////////////////////////////////////////////////////////////////////
  334. ////////////////////////// helper methods ////////////////////////
  335. ////////////////////////////////////////////////////////////////////
  336. protected void prepareGlobalElement(Element element) {
  337. GlobalElement e = null;
  338. ElementReference eref = null;
  339. int index = -1;
  340. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  341. index = element.getIndex(false);
  342. if(scParent instanceof Schema) {
  343. e = createGlobalElement(element);
  344. sgh.addElement(e, index);
  345. prepareFixGlobalElementNames(element, (GlobalElement) e, null);
  346. } else if(scParent instanceof ComplexTypeDefinition) {
  347. String seed = element.getName();
  348. boolean found = false;
  349. if(SchemaGeneratorUtil.isSimpleElement(element) ||
  350. SchemaGeneratorUtil.hasProxyChild(element)) {
  351. HashMap<String, SchemaComponent> map =
  352. namesMap.get(GlobalElement.class);
  353. if(map != null && map.get(seed) != null) {
  354. GlobalElement ge1 = (GlobalElement) map.get(seed);
  355. GlobalElement ge2 = SchemaGeneratorUtil.createGlobalElement(sm);
  356. ge2.setName(element.getName());
  357. SchemaGeneratorUtil.populateElement(ge2, element);
  358. this.datatypeParent = ge2;
  359. if(element.getType() instanceof Datatype)
  360. element.getType().accept(this);
  361. else if(element.getType() instanceof ContentModel)
  362. SchemaGeneratorUtil.setType(ge2,
  363. (GlobalComplexType) ((ContentModel)element.
  364. getType()).getPeer());
  365. if(SchemaGeneratorUtil.isIdentical(ge1, ge2)) {
  366. found = true;
  367. e = ge1;
  368. if(!elementReuseList.contains(element))
  369. elementReuseList.add(element);
  370. }
  371. }
  372. }
  373. if(!found) {
  374. e = createGlobalElement(element);
  375. sgh.addElement(e, -1);
  376. }
  377. eref = SchemaGeneratorUtil.createElementReference(sm, scParent, e, index);
  378. addRef(eref, e);
  379. prepareFixGlobalElementNames(element, e, eref);
  380. }
  381. assert e != null;
  382. sc = e;
  383. //set block, final, fixed, default, form etc
  384. SchemaGeneratorUtil.populateElement(e, element);
  385. if(eref != null)
  386. SchemaGeneratorUtil.populateElement(eref, element);
  387. this.datatypeParent = e;
  388. if(element.getType() instanceof Datatype)
  389. element.getType().accept(this);
  390. setPeer(element, e, eref);
  391. }
  392. protected void prepareElementRef(ElementRef element) {
  393. int index = element.getIndex();
  394. org.netbeans.modules.xml.schema.model.ElementReference eRef =
  395. SchemaGeneratorUtil.createElementReference(sm, (ComplexTypeDefinition) scParent,
  396. (GlobalElement)element.getReferent().getPeer(), index);
  397. setPeer(element, null, eRef);
  398. }
  399. protected void prepareLocalElement(Element element) {
  400. org.netbeans.modules.xml.schema.model.Element e = null;
  401. int index = -1;
  402. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  403. index = element.getIndex(false);
  404. if(scParent instanceof Schema) {
  405. e = createGlobalElement(element);
  406. sgh.addElement((GlobalElement) e, index);
  407. prepareFixGlobalElementNames(element, (GlobalElement) e, null);
  408. } else if(scParent instanceof ComplexTypeDefinition) {
  409. e = SchemaGeneratorUtil.createLocalElement(sm,
  410. (ComplexTypeDefinition) scParent, element.getName(), index);
  411. }
  412. assert e != null;
  413. sc = e;
  414. //set block, final, fixed, default, form etc
  415. SchemaGeneratorUtil.populateElement(e, element);
  416. this.datatypeParent = e;
  417. if(element.getType() instanceof Datatype)
  418. element.getType().accept(this);
  419. setPeer(element, e, null);
  420. }
  421. protected void prepareAttributeRef(AttributeRef attribute) {
  422. int index = attribute.getIndex();
  423. org.netbeans.modules.xml.schema.model.AttributeReference aRef =
  424. SchemaGeneratorUtil.createAttributeReference(sm, scParent,
  425. (GlobalAttribute)attribute.getReferent().getPeer(), index);
  426. setPeer(attribute, null, aRef);
  427. }
  428. protected void prepareLocalAttribute(Attribute attribute) {
  429. assert scParent != null;
  430. int index = -1;
  431. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  432. index = attribute.getIndex();
  433. LocalAttribute attr = null;
  434. if(scParent instanceof ComplexType &&
  435. ((ComplexType)scParent).getDefinition() instanceof ComplexTypeDefinition) {
  436. if(((ComplexType)scParent).getDefinition() instanceof SimpleContent) {
  437. SimpleContentDefinition def = ((SimpleContent)((ComplexType)scParent).
  438. getDefinition()).getLocalDefinition();
  439. if(def instanceof SimpleContentRestriction)
  440. attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(),
  441. (SimpleContentRestriction)def, index);
  442. else if(def instanceof Extension)
  443. attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(),
  444. (Extension)def, index);
  445. }
  446. else if(((ComplexType)scParent).getDefinition() instanceof ComplexContent) {
  447. ComplexContentDefinition def = ((ComplexContent)((ComplexType)scParent).
  448. getDefinition()).getLocalDefinition();
  449. if(def instanceof ComplexContentRestriction)
  450. attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(),
  451. (ComplexContentRestriction)def, index);
  452. else if(def instanceof Extension)
  453. attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(),
  454. (Extension)def, index);
  455. }
  456. else {
  457. attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(),
  458. scParent, index);
  459. }
  460. } else
  461. attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(),
  462. scParent, index);
  463. assert attr != null;
  464. //set fixed, default, form, use etc
  465. SchemaGeneratorUtil.populateAttribute(attr, attribute);
  466. this.datatypeParent = attr;
  467. if(attribute.getType() instanceof Datatype)
  468. attribute.getType().accept(this);
  469. if(getMode() != SchemaGenerator.Mode.TRANSFORM)
  470. attribute.setPeer(attr);
  471. }
  472. protected GlobalElement createGlobalElement(final Element element) {
  473. GlobalElement ge = SchemaGeneratorUtil.createGlobalElement(sm);
  474. String eName = findUniqueGlobalName(
  475. GlobalElement.class, ge, element.getName());
  476. ge.setName(eName);
  477. return ge;
  478. }
  479. protected GlobalComplexType createGlobalComplexType(final String seed) {
  480. GlobalComplexType gct;
  481. gct = (GlobalComplexType)
  482. SchemaGeneratorUtil.createGlobalComplexType(sm);
  483. String typeName = findUniqueGlobalName(
  484. GlobalComplexType.class, gct, seed);
  485. gct.setName(typeName);
  486. return gct;
  487. }
  488. public void createGlobalSimpleType(
  489. final Datatype d, final SchemaModel sm, final SchemaComponent sc,
  490. final SchemaGenerator.UniqueId id, SchemaGenerator.PrimitiveCart pc) {
  491. if(d != null) {
  492. NamedComponentReference<GlobalSimpleType> ref =null;
  493. if(SchemaGeneratorUtil.isPrimitiveType(d)) {
  494. ref = SchemaGeneratorUtil.createPrimitiveType(d, sc, pc);
  495. } else {
  496. GlobalSimpleType gst;
  497. gst = SchemaGeneratorUtil.createGlobalSimpleType(sm);
  498. String typeName = d.getName();
  499. typeName = findUniqueGlobalName(
  500. GlobalSimpleType.class, gst,
  501. "New"+typeName.substring(0, 1).toUpperCase()+
  502. typeName.substring(1)+"Type"+String.valueOf(id.nextId()));
  503. gst.setName(typeName);
  504. sgh.addSimpleType(gst, -1);
  505. if(d instanceof CustomDatatype)
  506. SchemaGeneratorUtil.populateSimpleType(
  507. ((CustomDatatype)d).getBase(), sm, gst, pc);
  508. else
  509. SchemaGeneratorUtil.populateSimpleType(d, sm, gst, pc);
  510. ref = sc.createReferenceTo(gst, GlobalSimpleType.class);
  511. }
  512. SchemaGeneratorUtil.setSimpleType(sc, ref);
  513. }
  514. }
  515. protected GlobalType createPeerGlobalComplexType(Element element) {
  516. org.netbeans.modules.xml.schema.model.Element e =
  517. (org.netbeans.modules.xml.schema.model.Element) element.getPeer();
  518. if(e instanceof ElementReference)
  519. e = (GlobalElement) getRef(e);
  520. GlobalComplexType gct = createGlobalComplexType(element.getName()+"Type");
  521. assert gct != null;
  522. sgh.addComplexType(gct, -1);
  523. SchemaGeneratorUtil.setType(e, gct);
  524. return gct;
  525. }
  526. protected void prepareFixGlobalElementNames(final Element element, final GlobalElement e,
  527. final ElementReference eref) {
  528. java.util.List<Object> scs = new ArrayList<Object>();
  529. scs.add(element);
  530. scs.add(e);
  531. scs.add(eref);
  532. fixNamesMap.put(new Integer(fgeCount++), scs);
  533. }
  534. protected void fixGlobalElementNames() {
  535. //clear unique names map
  536. namesMap.clear();
  537. //create buckets
  538. HashMap<GlobalElement, java.util.List<ElementReference>> erefMap = new
  539. HashMap<GlobalElement, java.util.List<ElementReference>>();
  540. for (Entry<Integer,java.util.List<Object>> e : fixNamesMap.entrySet()) {
  541. java.util.List<Object> scs = e.getValue();
  542. if(scs != null && scs.size() > 1) {
  543. GlobalElement ge = (GlobalElement) scs.get(1);
  544. ElementReference eref = (ElementReference) scs.get(2);
  545. java.util.List<ElementReference> erefs = erefMap.get(ge);
  546. if(erefs == null) {
  547. erefs = new ArrayList<ElementReference>();
  548. erefMap.put(ge, erefs);
  549. }
  550. if(eref != null && !erefs.contains(eref))
  551. erefs.add(eref);
  552. }
  553. }
  554. int count = 0;
  555. Iterator it = erefMap.keySet().iterator();
  556. while(it.hasNext()) {
  557. if(commitRange > 0 && (count++)%commitRange==0) {
  558. sm.endTransaction();
  559. sm.startTransaction();
  560. }
  561. GlobalElement ge = (GlobalElement) it.next();
  562. java.util.List<ElementReference> erefs = erefMap.get(ge);
  563. String name = findUniqueGlobalName(
  564. GlobalElement.class, ge, ge.getName());
  565. ge.setName(name);
  566. for(ElementReference eref:erefs)
  567. eref.setRef(eref.createReferenceTo(ge, GlobalElement.class));
  568. }
  569. erefMap.clear();
  570. erefMap = null;
  571. fixNamesMap.clear();
  572. }
  573. <T extends NameableSchemaComponent>String
  574. findUniqueGlobalName(Class<T> type, NameableSchemaComponent c,
  575. final String seed) {
  576. return sgh.findUniqueGlobalName(type, c, seed);
  577. }
  578. protected SchemaComponent getParent(final AXIComponent axiparent)
  579. throws IllegalArgumentException {
  580. return sgh.getParent(axiparent);
  581. }
  582. protected void setPeer(final Element element,
  583. final org.netbeans.modules.xml.schema.model.Element e,
  584. final ElementReference eref) {
  585. sgh.setPeer(element, e, eref);
  586. }
  587. protected void setPeer(final Attribute attribute,
  588. final org.netbeans.modules.xml.schema.model.Attribute a,
  589. final AttributeReference aRef) {
  590. sgh.setPeer(attribute, a, aRef);
  591. }
  592. private UniqueId createUniqueId() {
  593. return new UniqueId() {
  594. private int lastId = -1;
  595. public int nextId() {
  596. return ++lastId;
  597. }
  598. };
  599. }
  600. private PrimitiveCart createPrimitiveCart() {
  601. return new PrimitiveCart() {
  602. private Map<SchemaComponent, Datatype> pc = new HashMap<SchemaComponent, Datatype>();
  603. private Map<String, GlobalSimpleType> ptypes = new HashMap<String, GlobalSimpleType>();
  604. private GlobalSimpleType def = null;
  605. public void add(Datatype d, SchemaComponent referer) {
  606. pc.put(referer, d);
  607. }
  608. public Set<Map.Entry<SchemaComponent, Datatype>> getEntries() {
  609. return pc.entrySet();
  610. }
  611. public GlobalSimpleType getDefaultPrimitive() {
  612. if(def==null) {
  613. def = getPrimitiveType("string");//NoI18n
  614. }
  615. return def;
  616. }
  617. public GlobalSimpleType getPrimitiveType(String typeName) {
  618. if(ptypes.isEmpty()) {
  619. SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
  620. Collection<GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes();
  621. for(GlobalSimpleType ptype: primitives){
  622. ptypes.put(ptype.getName(), ptype);
  623. }
  624. }
  625. return ptypes.get(typeName);
  626. }
  627. };
  628. }
  629. protected void addRef(SchemaComponent referer, SchemaComponent ref) {
  630. sgh.addRef(referer, ref);
  631. }
  632. protected SchemaComponent getRef(SchemaComponent referer) {
  633. return sgh.getRef(referer);
  634. }
  635. protected void addToGlobal(SchemaComponent sc) {
  636. createGlobals.add(sc);
  637. }
  638. private void fixPrimitiveTypes() {
  639. for(Map.Entry<SchemaComponent, Datatype> e: pc.getEntries()) {
  640. Datatype d = (Datatype) e.getValue();
  641. SchemaComponent referer = (SchemaComponent) e.getKey();
  642. String typeName = d.getName();
  643. if(d instanceof CustomDatatype)
  644. typeName = ((CustomDatatype)d).getBase().getName();
  645. GlobalSimpleType gst = pc.getPrimitiveType(typeName);
  646. NamedComponentReference<GlobalSimpleType> ref =
  647. referer.createReferenceTo(gst, GlobalSimpleType.class);
  648. SchemaGeneratorUtil.setSimpleType(referer, ref);
  649. }
  650. }
  651. protected void clear() {
  652. path.clear();
  653. path = null;
  654. createGlobals.clear();
  655. createGlobals = null;
  656. fixNamesMap.clear();
  657. fixNamesMap = null;
  658. refMap.clear();
  659. refMap = null;
  660. namesMap.clear();
  661. namesMap = null;
  662. }
  663. interface SchemaGeneratorHelper {
  664. public void execute(SchemaModel sm) throws IOException;
  665. public SchemaComponent getParent(final AXIComponent axiparent)
  666. throws IllegalArgumentException;
  667. /*
  668. * finds unique name from a bucket of global components
  669. */
  670. public <T extends NameableSchemaComponent>String
  671. findUniqueGlobalName(Class<T> type, NameableSchemaComponent c,
  672. final String seed);
  673. public void setPeer(final Element element,
  674. final org.netbeans.modules.xml.schema.model.Element e,
  675. final ElementReference eref);
  676. public void setPeer(final Attribute attribute,
  677. final org.netbeans.modules.xml.schema.model.Attribute a,
  678. final AttributeReference aRef);
  679. public void addRef(SchemaComponent referer, SchemaComponent ref);
  680. public SchemaComponent getRef(SchemaComponent referer);
  681. public void addElement(GlobalElement ge, int index);
  682. public void addComplexType(GlobalComplexType gct, int index);
  683. public void addSimpleType(GlobalSimpleType gst, int index);
  684. }
  685. class UpdateHelper implements SchemaGeneratorHelper {
  686. UpdateHelper() {
  687. }
  688. public void execute(SchemaModel sm) throws IOException {
  689. DefaultSchemaGenerator.this.sm = sm;
  690. DefaultSchemaGenerator.this.am =
  691. AXIModelFactory.getDefault().getModel(sm);
  692. SchemaUpdate su = SchemaGeneratorUtil.getSchemaUpdate(am);
  693. Collection<SchemaUpdate.UpdateUnit> us = su.getUpdateUnits();
  694. try {
  695. ((AXIModelImpl)am).disableAutoSync();
  696. sm.startTransaction();
  697. for(SchemaUpdate.UpdateUnit u:us) {
  698. AXIComponent source = u.getSource();
  699. if(source.getModel() != am) //skip mutating other model
  700. continue;
  701. SchemaUpdate.UpdateUnit.Type type = u.getType();
  702. if(type == SchemaUpdate.UpdateUnit.Type.CHILD_ADDED)
  703. addSchemaComponent(source, u);
  704. else if(type == SchemaUpdate.UpdateUnit.Type.CHILD_DELETED)
  705. SchemaGeneratorUtil.removeSchemaComponent(source, u, sm);
  706. else if(type == SchemaUpdate.UpdateUnit.Type.CHILD_MODIFIED)
  707. SchemaGeneratorUtil.modifySchemaComponent(source, u, sm, pc);
  708. }
  709. // addAllGlobals();
  710. } finally {
  711. clear();
  712. sm.endTransaction();
  713. ((AXIModelImpl)am).enableAutoSync();
  714. }
  715. }
  716. protected void addSchemaComponent(AXIComponent source,
  717. SchemaUpdate.UpdateUnit u) {
  718. assert u.getNewValue() != null;
  719. scParent = DefaultSchemaGenerator.this.getParent(
  720. ((AXIComponent)u.getNewValue()).getParent());
  721. assert scParent != null;
  722. ((AXIComponent)u.getNewValue()).accept(DefaultSchemaGenerator.this);
  723. }
  724. public <T extends NameableSchemaComponent>String
  725. findUniqueGlobalName(Class<T> type, NameableSchemaComponent c,
  726. final String seed) {
  727. return SchemaGeneratorUtil.findUniqueGlobalName(type, seed, sm);
  728. }
  729. public SchemaComponent getParent(final AXIComponent axiparent)
  730. throws IllegalArgumentException {
  731. SchemaComponent scParent = null;
  732. if(axiparent instanceof AXIDocument)
  733. scParent = sm.getSchema();
  734. else if(axiparent instanceof Element){
  735. SchemaComponent e = axiparent.getPeer();
  736. if(e instanceof ElementReference)
  737. e = getRef(axiparent.getPeer());
  738. assert e != null;
  739. SchemaComponent lct = SchemaGeneratorUtil.getLocalComplexType(e);
  740. if(lct == null) {
  741. lct = SchemaGeneratorUtil.getGlobalComplexType(e);
  742. if(lct == null) {
  743. if((e instanceof GlobalElement && ((GlobalElement)e).getType() != null &&
  744. ((GlobalElement)e).getType().get() instanceof GlobalSimpleType) ||
  745. (e instanceof LocalElement && ((LocalElement)e).getType() != null &&
  746. ((LocalElement)e).getType().get() instanceof GlobalSimpleType)) {
  747. SimpleContent newsc = sm.getFactory().createSimpleContent();
  748. SimpleExtension se = sm.getFactory().createSimpleExtension();
  749. newsc.setLocalDefinition(se);
  750. GlobalType gst = null;
  751. if(e instanceof GlobalElement)
  752. gst = ((GlobalElement)e).getType().get();
  753. else if(e instanceof LocalElement)
  754. gst = ((LocalElement)e).getType().get();
  755. if(gst != null)
  756. se.setBase(se.createReferenceTo(gst, GlobalType.class));
  757. lct = SchemaGeneratorUtil.createLocalComplexType(sm, e);
  758. ((LocalComplexType)lct).setDefinition(newsc);
  759. } else
  760. lct = SchemaGeneratorUtil.createLocalComplexType(sm, e);
  761. }
  762. }
  763. assert lct != null;
  764. scParent = lct;
  765. } else if(axiparent instanceof ContentModel){
  766. scParent = axiparent.getPeer();
  767. } else if(axiparent instanceof Compositor){
  768. scParent = axiparent.getPeer();
  769. }
  770. return scParent;
  771. }
  772. public void setPeer(final Element element,
  773. final org.netbeans.modules.xml.schema.model.Element e,
  774. final ElementReference eref) {
  775. if(eref != null)
  776. element.setPeer(eref);
  777. else
  778. element.setPeer(e);
  779. }
  780. public void setPeer(final Attribute attribute,
  781. final org.netbeans.modules.xml.schema.model.Attribute a,
  782. final AttributeReference aRef) {
  783. if(aRef != null)
  784. attribute.setPeer(aRef);
  785. else
  786. attribute.setPeer(a);
  787. }
  788. public void addElement(GlobalElement ge, int index) {
  789. if(index != -1)
  790. SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), ge, index);
  791. else
  792. sm.getSchema().addElement((GlobalElement) ge);
  793. }
  794. public void addComplexType(GlobalComplexType gct, int index) {
  795. if(index != -1)
  796. SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), gct, index);
  797. else
  798. sm.getSchema().addComplexType(gct);
  799. }
  800. public void addSimpleType(GlobalSimpleType gst, int index) {
  801. if(index != -1)
  802. SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), gst, index);
  803. else
  804. sm.getSchema().addSimpleType(gst);
  805. }
  806. public void addRef(SchemaComponent referer, SchemaComponent ref) {
  807. }
  808. public SchemaComponent getRef(SchemaComponent referer) {
  809. if(referer instanceof ElementReference)
  810. return ((ElementReference)referer).getRef().get();
  811. else if(referer instanceof org.netbeans.modules.xml.schema.model.Element)
  812. if(referer instanceof GlobalElement &&
  813. ((GlobalElement)referer).getType() != null &&
  814. ((GlobalElement)referer).getType().get() instanceof GlobalComplexType)
  815. return ((GlobalElement)referer).getType().get();
  816. else if(referer instanceof LocalElement &&
  817. ((LocalElement)referer).getType() != null &&
  818. ((LocalElement)referer).getType().get() instanceof GlobalComplexType)
  819. return ((LocalElement)referer).getType().get();
  820. return null;
  821. }
  822. protected void clear() {
  823. DefaultSchemaGenerator.this.clear();
  824. }
  825. }
  826. class TransformHelper implements SchemaGeneratorHelper {
  827. TransformHelper() {
  828. }
  829. public void execute(SchemaModel sm) throws IOException {
  830. DefaultSchemaGenerator.this.sm = sm;
  831. DefaultSchemaGenerator.this.am = AXIModelFactory.getDefault().getModel(sm);
  832. Schema schema = sm.getSchema();
  833. assert schema != null;
  834. try {
  835. HashMap<Class, Map<String, SchemaComponent>> allGlobals =
  836. new HashMap<Class, Map<String, SchemaComponent>>();
  837. java.util.List<Element> lrges = preTransform(schema, allGlobals);
  838. ((AXIModelImpl)am).disableAutoSync();
  839. sm.startTransaction();
  840. for(Element element : lrges) {
  841. java.util.List<AXIComponent> pathFromRoot = new ArrayList<AXIComponent>();
  842. pathFromRoot.add(element);
  843. transformChildren(element, schema, pathFromRoot);
  844. }
  845. postTransform(schema, allGlobals);
  846. } finally {
  847. clear();
  848. try {
  849. sm.endTransaction();
  850. }
  851. finally {
  852. ((AXIModelImpl)am).enableAutoSync();
  853. am.sync();
  854. }
  855. }
  856. }
  857. public SchemaComponent getParent(final AXIComponent axiparent)
  858. throws IllegalArgumentException {
  859. throw new IllegalArgumentException("should not call this api during transform");
  860. }
  861. protected void transformChildren(AXIComponent component,
  862. SchemaComponent parent, java.util.List<AXIComponent> pathFromRoot) {
  863. //skip transforming components from other model
  864. if(!SchemaGeneratorUtil.fromSameSchemaModel(component.getPeer(), sm))
  865. return;
  866. assert parent != null;
  867. DefaultSchemaGenerator.this.scParent = parent;
  868. component.accept(DefaultSchemaGenerator.this);
  869. if(elementReuseList.contains(component))
  870. return;
  871. SchemaComponent cc = DefaultSchemaGenerator.this.sc;
  872. //check for cycle
  873. if(component instanceof Element) {
  874. Element orig = (Element)component;
  875. if(orig.isReference()) {
  876. orig = SchemaGeneratorUtil.findOriginalElement(orig);
  877. }
  878. if(path.size() > 0 && path.contains(orig))
  879. return;
  880. path.add(orig);
  881. }
  882. try {
  883. if(component.getChildren().size() > 0) {
  884. parent = scParent;
  885. }
  886. assert parent != null;
  887. if(component instanceof AbstractElement) {
  888. for(AbstractAttribute attr :
  889. ((AbstractElement)component).getAttributes()) {
  890. //skip transforming components from other model
  891. if(!SchemaGeneratorUtil.fromSameSchemaModel(attr.getPeer(), sm))
  892. continue;
  893. DefaultSchemaGenerator.this.scParent = parent;
  894. attr.accept(DefaultSchemaGenerator.this);
  895. }
  896. }
  897. for(AXIComponent child: component.getChildren()) {
  898. if(!(child instanceof AbstractAttribute)) {
  899. if(pathFromRoot.contains(child.getOriginal()))
  900. continue;
  901. pathFromRoot.add(child.getOriginal());
  902. transformChildren(child, parent, pathFromRoot);
  903. }
  904. }
  905. } finally {
  906. if(component instanceof Element)
  907. path.remove(path.size()-1);
  908. }
  909. }
  910. protected java.util.List<Element> preTransform(Schema schema,
  911. Map<Class, Map<String, SchemaComponent>> allGlobals) {
  912. java.util.List<Element> lrges =
  913. SchemaGeneratorUtil.findMasterGlobalElements(
  914. DefaultSchemaGenerator.this.am);
  915. //Now expand the AXI tree deep for some global elements from the list
  916. AXINonCyclicVisitor visitor = new AXINonCyclicVisitor(am);
  917. visitor.expand(lrges);
  918. //All saved globals
  919. SortedMap<String, SchemaComponent> ggmap =
  920. new TreeMap<String, SchemaComponent>();
  921. allGlobals.put(GlobalGroup.class, ggmap);
  922. for(GlobalGroup ag:schema.getGroups())
  923. ggmap.put(ag.getName(), ag);
  924. SortedMap<String, SchemaComponent> gctmap =
  925. new TreeMap<String, SchemaComponent>();
  926. allGlobals.put(GlobalComplexType.class, gctmap);
  927. for(GlobalComplexType gct:schema.getComplexTypes())
  928. gctmap.put(gct.getName(), gct);
  929. SortedMap<String, SchemaComponent> gagmap =
  930. new TreeMap<String, SchemaComponent>();
  931. allGlobals.put(GlobalAttributeGroup.class, gagmap);
  932. for(GlobalAttributeGroup ag:schema.getAttributeGroups())
  933. gagmap.put(ag.getName(), ag);
  934. SortedMap<String, SchemaComponent> gstmap =
  935. new TreeMap<String, SchemaComponent>();
  936. allGlobals.put(GlobalSimpleType.class, gstmap);
  937. for(GlobalType gst:schema.getSimpleTypes())
  938. gstmap.put(gst.getName(), gst);
  939. SortedMap<String, SchemaComponent> gemap =
  940. new TreeMap<String, SchemaComponent>();
  941. allGlobals.put(GlobalElement.class, gemap);
  942. for(GlobalElement ge:schema.getElements())
  943. gemap.put(ge.getName(), ge);
  944. SortedMap<String, SchemaComponent> gamap =
  945. new TreeMap<String, SchemaComponent>();
  946. allGlobals.put(GlobalAttribute.class, gamap);
  947. for(GlobalAttribute ga:schema.getAttributes())
  948. gamap.put(ga.getName(), ga);
  949. return lrges;
  950. }
  951. protected void postTransform(Schema schema,
  952. HashMap<Class, Map<String, SchemaComponent>> allGlobals) {
  953. //remove previous global elements, complextypes etc.,
  954. removeAllGlobals(schema, allGlobals);
  955. //add new elements, complextypes etc.,
  956. addAllGlobals(schema, createGlobals);
  957. //fix global element names, make them unique
  958. fixGlobalElementNames();
  959. //fix primitive types
  960. fixPrimitiveTypes();
  961. }
  962. //remove all previous global components
  963. private void removeAllGlobals(final Schema schema,
  964. final HashMap<Class, Map<String, SchemaComponent>> allGlobals) {
  965. //remove all global simpleType
  966. removeGlobalSchemaComponent(GlobalSimpleType.class, allGlobals, schema);
  967. //remove all global attribute
  968. removeGlobalSchemaComponent(GlobalAttribute.class, allGlobals, schema);
  969. //remove all global attribute group
  970. removeGlobalSchemaComponent(GlobalAttributeGroup.class, allGlobals, schema);
  971. //remove all global complexType
  972. removeGlobalSchemaComponent(GlobalComplexType.class, allGlobals, schema);
  973. //remove all global group
  974. removeGlobalSchemaComponent(GlobalGroup.class, allGlobals, schema);
  975. //remove all global element
  976. removeGlobalSchemaComponent(GlobalElement.class, allGlobals, schema);
  977. }
  978. private void removeGlobalSchemaComponent(final Class type,
  979. final HashMap<Class, Map<String, SchemaComponent>> allGlobals,
  980. final Schema schema) {
  981. Map<String, SchemaComponent> gmap = allGlobals.get(type);
  982. if(gmap == null) return;
  983. int count = 0;
  984. for (Map.Entry entry : gmap.entrySet()) {
  985. SchemaComponent sc = (SchemaComponent) entry.getValue();
  986. commitTransaction(count);
  987. if(sc instanceof GlobalSimpleType)
  988. schema.removeSimpleType((GlobalSimpleType) sc);
  989. else if(sc instanceof GlobalAttribute)
  990. schema.removeAttribute((GlobalAttribute) sc);
  991. else if(sc instanceof GlobalAttributeGroup)
  992. schema.removeAttributeGroup((GlobalAttributeGroup) sc);
  993. else if(sc instanceof GlobalComplexType)
  994. schema.removeComplexType((GlobalComplexType) sc);
  995. else if(sc instanceof GlobalGroup)
  996. schema.removeGroup((GlobalGroup) sc);
  997. else if(sc instanceof GlobalElement)
  998. schema.removeElement((Globa