KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > ElementGenerator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema;
24
25 import java.util.Map JavaDoc;
26
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xquark.schema.validation.ContentIterator;
31 import org.xquark.schema.validation.ContentIteratorFactory;
32 import org.xquark.util.DOMBuilder;
33
34 public class ElementGenerator {
35 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36 private static final String JavaDoc RCSName = "$Name: $";
37   
38   private org.xml.sax.ContentHandler JavaDoc handler = null;
39   private java.util.HashMap JavaDoc prefixMap;
40   private SchemaManager manager;
41   
42   public ElementGenerator(SchemaManager manager, java.util.HashMap JavaDoc prefixMap) {
43     this.manager = manager;
44     if (prefixMap != null) {
45       this.prefixMap = (java.util.HashMap JavaDoc)prefixMap.clone();
46       if (prefixMap.get("xsi") == null)
47           this.prefixMap.put("xsi", SchemaConstants.XSI_URI);
48     }
49   }
50
51   public void setContentHandler(org.xml.sax.ContentHandler JavaDoc handler) {
52     this.handler = handler;
53   }
54   
55   public org.xml.sax.ContentHandler JavaDoc getContentHandler() {
56     return handler;
57   }
58   
59   public void generateDocument(String JavaDoc namespace, String JavaDoc localName, Particle particle)
60     throws SAXException JavaDoc {
61     ElementDeclaration decl =
62       manager.getElementDeclaration(namespace, localName);
63     if (decl == null) return;
64     generateDocument(decl, particle);
65   }
66   
67   public void generateDocument(ElementDeclaration decl, Particle particle) {
68     if (handler == null) return;
69     try {
70       handler.startDocument();
71       if (prefixMap != null) {
72         java.util.Iterator JavaDoc it = prefixMap.entrySet().iterator();
73         while (it.hasNext()) {
74           Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
75           handler.startPrefixMapping((String JavaDoc)entry.getKey(),(String JavaDoc)entry.getValue());
76         }
77       }
78       generate(decl, particle);
79       if (prefixMap != null) {
80         java.util.Iterator JavaDoc it = prefixMap.entrySet().iterator();
81         while (it.hasNext()) {
82           Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
83           handler.endPrefixMapping((String JavaDoc)entry.getKey());
84         }
85       }
86       handler.endDocument();
87     } catch (SAXException JavaDoc ex) { ex.printStackTrace(); }
88   }
89
90   public Document JavaDoc generateDocument(Document JavaDoc doc, String JavaDoc namespace, String JavaDoc localName)
91     throws SAXException JavaDoc
92   {
93     handler = new DOMBuilder(doc);
94     generateDocument(namespace, localName, null);
95     return doc;
96   }
97
98   public Document JavaDoc generateDocument(Document JavaDoc doc, ElementDeclaration decl) {
99     handler = new DOMBuilder(doc);
100     generateDocument(decl, null);
101     return doc;
102   }
103
104   public Element JavaDoc generateElement(Document JavaDoc doc, ElementDeclaration decl, boolean stripNamespaces)
105   {
106     handler = new DOMBuilder(doc);
107     Element JavaDoc previous = doc.getDocumentElement();
108     doc.removeChild(previous);
109     generateDocument(decl, null);
110     Element JavaDoc node = doc.getDocumentElement();
111     doc.removeChild(node);
112     doc.appendChild(previous);
113     if (stripNamespaces) {
114       org.w3c.dom.NamedNodeMap JavaDoc atts = node.getAttributes();
115       for (int i = 0; i < atts.getLength(); i++) {
116         org.w3c.dom.Attr JavaDoc attr = (org.w3c.dom.Attr JavaDoc)atts.item(i);
117         if ("xmlns".equals(attr.getPrefix()) || "xsi".equals(attr.getPrefix())
118             || "xmlns".equals(attr.getNodeName()))
119           node.removeAttributeNode(attr);
120       }
121     }
122     return node;
123   }
124   
125   public org.w3c.dom.NodeList JavaDoc generateElement(Document JavaDoc doc, ElementDeclaration decl, Particle parentParticle)
126   {
127     try {
128       handler = new DOMBuilder(doc);
129       Element JavaDoc previous = doc.getDocumentElement();
130       doc.removeChild(previous);
131       generateDocument(decl, parentParticle);
132       Element JavaDoc node = doc.getDocumentElement();
133       doc.removeChild(node);
134       doc.appendChild(previous);
135       return node.getChildNodes();
136     }
137     catch ( Exception JavaDoc e ) {
138       e.printStackTrace();
139     }
140     return null;
141   }
142   
143   private void generate(ElementDeclaration decl) {
144     generate(decl.getNamespace(), decl.getName(), decl.getType(), decl.isNillable());
145   }
146   
147   private void generate(String JavaDoc namespace, String JavaDoc localName, Type type) {
148     generate(namespace, localName, type, false);
149   }
150
151   private void generate(String JavaDoc namespace, String JavaDoc localName, Type type, boolean nullable)
152   {
153     try {
154       if (namespace == null) namespace = "";
155       org.xml.sax.helpers.AttributesImpl JavaDoc atts = new org.xml.sax.helpers.AttributesImpl JavaDoc();
156       java.util.Collection JavaDoc attrDecls = type.getAttributeDeclarations();
157       if (attrDecls != null) {
158     java.util.Iterator JavaDoc it = attrDecls.iterator();
159     while (it.hasNext()) {
160       AttributeDeclaration decl = (AttributeDeclaration)it.next();
161           if (decl.getUse() == SchemaConstants.REQUIRED) {
162             String JavaDoc attNamespace = decl.getNamespace();
163             if (attNamespace == null) attNamespace = "";
164             String JavaDoc value = decl.getDefaultValue();
165             if (value == null) value = "";
166             atts.addAttribute(attNamespace, decl.getName(), null, "CDATA", value);
167           }
168     }
169       }
170       
171       ContentIterator iterator = type.childIterator();
172       if (iterator == null) {
173     if (nullable) {
174       atts.addAttribute(SchemaConstants.XSI_URI, "nil", null,
175                 "CDATA", "true");
176       handler.startElement(namespace, localName, null, atts);
177       handler.endElement(namespace, localName, null);
178     } else {
179       handler.startElement(namespace, localName, null, atts);
180       handler.characters(new char[0], 0, 0);
181       handler.endElement(namespace, localName, null);
182     }
183       } else {
184     handler.startElement(namespace, localName, null, atts);
185     ElementDeclaration next = null;
186     ElementDeclaration prev = null;
187     int count = 0;
188     while (true) {
189       java.util.List JavaDoc nextElements = iterator.nextValidElements();
190           if (nextElements.size() > 0)
191             next = (ElementDeclaration)nextElements.get(nextElements.size()-1);
192           /*
193       Iterator it = nextElements.iterator();
194           while (it.hasNext()) {
195               next = (ElementDeclaration)it.next();
196               if (next != null) {
197                 int[] occurrenceCount =
198                   ((ComplexType)type).getDeclarationOccurrence(next.getNamespace(), next.getName());
199                 if (occurrenceCount != null && occurrenceCount[0] > 0) break;
200               }
201              count++;
202              next = null;
203           }
204       if (prev != next) {
205         count++;
206         prev = next;
207       } else if (nextElements.size() > count) {
208         next = (ElementDeclaration)nextElements.get(count);
209         count++;
210       }
211            */

212       if (next == null) break;
213       iterator.startElement(next.getNamespace(), next.getName());
214       generate(next);
215       iterator.endElement(next.getNamespace(), next.getName());
216     }
217     handler.endElement(namespace, localName, null);
218       }
219     } catch (SchemaException ex) {
220       ex.printStackTrace();
221     } catch (SAXException JavaDoc ex) {
222       ex.printStackTrace();
223     }
224   }
225   
226   private void generate(ElementDeclaration decl, Particle parentParticle)
227   {
228     if ( parentParticle == null )
229       generate(decl);
230     else {
231       try {
232         // create a root element
233
handler.startElement("", "rootElement", null, new org.xml.sax.helpers.AttributesImpl JavaDoc());
234         
235         ContentIterator iterator = ContentIteratorFactory.createIterator(parentParticle);
236     ElementDeclaration next = decl;
237     while (true) {
238       iterator.startElement(next.getNamespace(), next.getName());
239       generate(next);
240       iterator.endElement(next.getNamespace(), next.getName());
241           
242       java.util.List JavaDoc nextElements = iterator.nextValidElements();
243           if ( nextElements != null && nextElements.size() > 0)
244               next = (ElementDeclaration)nextElements.get(nextElements.size()-1);
245           else
246             next = null;
247           
248       if (next == null) break;
249     }
250         
251         handler.endElement("", "rootElement", null);
252       }
253       catch ( Exception JavaDoc ex ) {
254         ex.printStackTrace();
255       }
256     }
257   }
258   
259 }
260   
261   
262
Popular Tags