1 22 23 package org.xquark.schema; 24 25 import java.util.Map ; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.xml.sax.SAXException ; 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 RCSRevision = "$Revision: 1.1 $"; 36 private static final String RCSName = "$Name: $"; 37 38 private org.xml.sax.ContentHandler handler = null; 39 private java.util.HashMap prefixMap; 40 private SchemaManager manager; 41 42 public ElementGenerator(SchemaManager manager, java.util.HashMap prefixMap) { 43 this.manager = manager; 44 if (prefixMap != null) { 45 this.prefixMap = (java.util.HashMap )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 handler) { 52 this.handler = handler; 53 } 54 55 public org.xml.sax.ContentHandler getContentHandler() { 56 return handler; 57 } 58 59 public void generateDocument(String namespace, String localName, Particle particle) 60 throws SAXException { 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 it = prefixMap.entrySet().iterator(); 73 while (it.hasNext()) { 74 Map.Entry entry = (Map.Entry )it.next(); 75 handler.startPrefixMapping((String )entry.getKey(),(String )entry.getValue()); 76 } 77 } 78 generate(decl, particle); 79 if (prefixMap != null) { 80 java.util.Iterator it = prefixMap.entrySet().iterator(); 81 while (it.hasNext()) { 82 Map.Entry entry = (Map.Entry )it.next(); 83 handler.endPrefixMapping((String )entry.getKey()); 84 } 85 } 86 handler.endDocument(); 87 } catch (SAXException ex) { ex.printStackTrace(); } 88 } 89 90 public Document generateDocument(Document doc, String namespace, String localName) 91 throws SAXException 92 { 93 handler = new DOMBuilder(doc); 94 generateDocument(namespace, localName, null); 95 return doc; 96 } 97 98 public Document generateDocument(Document doc, ElementDeclaration decl) { 99 handler = new DOMBuilder(doc); 100 generateDocument(decl, null); 101 return doc; 102 } 103 104 public Element generateElement(Document doc, ElementDeclaration decl, boolean stripNamespaces) 105 { 106 handler = new DOMBuilder(doc); 107 Element previous = doc.getDocumentElement(); 108 doc.removeChild(previous); 109 generateDocument(decl, null); 110 Element node = doc.getDocumentElement(); 111 doc.removeChild(node); 112 doc.appendChild(previous); 113 if (stripNamespaces) { 114 org.w3c.dom.NamedNodeMap atts = node.getAttributes(); 115 for (int i = 0; i < atts.getLength(); i++) { 116 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )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 generateElement(Document doc, ElementDeclaration decl, Particle parentParticle) 126 { 127 try { 128 handler = new DOMBuilder(doc); 129 Element previous = doc.getDocumentElement(); 130 doc.removeChild(previous); 131 generateDocument(decl, parentParticle); 132 Element node = doc.getDocumentElement(); 133 doc.removeChild(node); 134 doc.appendChild(previous); 135 return node.getChildNodes(); 136 } 137 catch ( Exception 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 namespace, String localName, Type type) { 148 generate(namespace, localName, type, false); 149 } 150 151 private void generate(String namespace, String localName, Type type, boolean nullable) 152 { 153 try { 154 if (namespace == null) namespace = ""; 155 org.xml.sax.helpers.AttributesImpl atts = new org.xml.sax.helpers.AttributesImpl (); 156 java.util.Collection attrDecls = type.getAttributeDeclarations(); 157 if (attrDecls != null) { 158 java.util.Iterator it = attrDecls.iterator(); 159 while (it.hasNext()) { 160 AttributeDeclaration decl = (AttributeDeclaration)it.next(); 161 if (decl.getUse() == SchemaConstants.REQUIRED) { 162 String attNamespace = decl.getNamespace(); 163 if (attNamespace == null) attNamespace = ""; 164 String 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 nextElements = iterator.nextValidElements(); 190 if (nextElements.size() > 0) 191 next = (ElementDeclaration)nextElements.get(nextElements.size()-1); 192 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 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 handler.startElement("", "rootElement", null, new org.xml.sax.helpers.AttributesImpl ()); 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 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 ex ) { 254 ex.printStackTrace(); 255 } 256 } 257 } 258 259 } 260 261 262 | Popular Tags |