1 22 23 package org.xquark.mediator.DOMUtils ; 24 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.helpers.DefaultHandler ; 28 import org.xquark.schema.Type; 29 import org.xquark.schema.validation.ElementPSVInfoset; 30 import org.xquark.schema.validation.PSVInfoSetProvider; 31 import org.xquark.schema.validation.PSVInfoset; 32 import org.xquark.xpath.datamodel.*; 33 34 43 44 public class SAX2DOM extends DefaultHandler { 45 private static final String RCSRevision = "$Revision: 1.5 $"; 49 private static final String RCSName = "$Name: $"; 50 private TypedDocumentImpl doc = null ; 54 private TypedNode currentNode = null ; 56 59 private Tuple tuple ; 61 private int index = 0; 63 private int depth = 0; 64 private PSVInfoSetProvider psvip = null; 65 private StringBuffer ongoingchars = new StringBuffer (); 66 72 public SAX2DOM(PSVInfoSetProvider psvip) { this.psvip = psvip; doc = new TypedDocumentImpl() ; } 74 75 public void init(Tuple tuple) { this.tuple = tuple ; index = 0; } 76 77 83 public void characters(char[] ch, int start, int length) throws SAXException { 84 90 ongoingchars.append(new String (ch, start, length)); 91 } 92 93 95 public void endElement(String uri, String local, String raw) throws SAXException { 96 boolean onlytext = false; 97 if (depth >= 2) { 98 ElementPSVInfoset epsvis = psvip.getCurrentInfoset(); 99 Type type = epsvis.getType(); 100 if (ongoingchars.length() > 0) { 101 TypedValueImpl tv = (TypedValueImpl)doc.createTextNode(ongoingchars.toString()); 102 if (type != null && type.getValueType() != null) { 103 tv.setType(type.getValueType()); 104 tv.setTypedValue(epsvis.getActualValue()); 105 tv.setNodeValue(epsvis.getNormalizedValue()); 106 } 107 else { 108 tv.setType(this.psvip.getSchemaManager().getAnySimpleType()); 109 tv.setTypedValue(ongoingchars.toString()); 110 } 111 if (currentNode == null) { currentNode = tv; depth++; onlytext = true;} 112 else currentNode.appendChild(tv); 113 ongoingchars.setLength(0); 114 } 115 if (currentNode != null && !onlytext) { 116 currentNode.setType(epsvis.getType()); 117 currentNode.setDeclaration(epsvis.getDeclaration()); 118 } 119 } 120 depth--; 121 switch (depth) { 122 case 0 : index = 0; break; 123 case 1 : index++; break; 124 case 2 : 125 tuple.addNodeAtIndex(index, currentNode) ; 129 if (onlytext) { depth--; index++; } 130 currentNode = null; 131 break; 132 default : currentNode = (TypedNode)currentNode.getParentNode(); 133 } 134 } 135 136 138 152 154 public void startDocument() throws SAXException { 155 156 depth = 0; 157 index = 0; 158 } 159 160 public void startElement(String uri, String local, String raw, Attributes atts) throws SAXException { 161 if (depth >= 2) { 162 TypedElement el = (TypedElement)doc.createElementNS(uri,local); 163 ElementPSVInfoset epsvis = psvip.getCurrentInfoset(); 164 for (int i=0; i<epsvis.getAttributeCount();i++) { 165 PSVInfoset infoset = epsvis.getAttributePSVInfoset(i); 166 TypedAttribute attr = (TypedAttribute)doc.createAttributeNS(infoset.getNamespaceURI(),infoset.getLocalName()); 167 attr.setNodeValue(infoset.getNormalizedValue()); 168 attr.setTypedValue(infoset.getActualValue()); 169 attr.setType(infoset.getType()); 170 attr.setDeclaration(infoset.getDeclaration()); 171 el.setAttributeNode(attr); 172 } 173 if (ongoingchars.length() > 0) { 174 TypedValueImpl tv = (TypedValueImpl)doc.createTextNode(ongoingchars.toString()); 175 tv.setType(this.psvip.getSchemaManager().getAnySimpleType()); 176 tv.setTypedValue(ongoingchars.toString()); 177 if (currentNode == null) throw new SAXException ("Incorrect result!"); 178 currentNode.appendChild(tv); 179 ongoingchars.setLength(0); 180 } 181 if (depth > 2) currentNode.appendChild(el); 182 currentNode = el; 183 } 184 depth++; 185 186 } 187 } 188 189 | Popular Tags |