1 22 23 package org.xquark.mediator.DOMUtils; 24 25 import java.util.Iterator ; 26 27 import org.w3c.dom.*; 28 import org.xml.sax.SAXException ; 29 import org.xquark.schema.*; 30 import org.xquark.schema.validation.PSVInfoSetProvider; 31 import org.xquark.util.DOMReader; 32 import org.xquark.xpath.datamodel.TypedAttribute; 33 import org.xquark.xpath.datamodel.TypedElement; 34 import org.xquark.xpath.datamodel.TypedNode; 35 import org.xquark.xquery.parser.XQueryException; 36 import org.xquark.xquery.typing.QAtomicSerializer; 37 38 39 49 public class TypedDOMReader extends DOMReader { 50 private static final String RCSRevision = "$Revision: 1.7 $"; 51 private static final String RCSName = "$Name: $"; 52 53 protected PSVInfoSetProvider psvisp = null; 54 protected QAtomicSerializer qAtomicSerializer = null; 55 56 public TypedDOMReader() { 57 super(); 58 } 59 60 public TypedDOMReader(Document doc) { 61 super(doc); 62 } 63 64 public TypedDOMReader(Element root) { 65 super(root); 66 } 67 68 public void setPSVInfoSetProvider(PSVInfoSetProvider psvisp) { 69 this.psvisp = psvisp; 70 } 71 72 public void setQAtomicSerializer(QAtomicSerializer qAtomicSerializer) { 73 this.qAtomicSerializer = qAtomicSerializer; 74 } 75 76 protected void generateText(Node node) throws SAXException { 77 if (psvisp != null && node.getNodeValue() != null && node instanceof TypedNode) { 78 psvisp.setActualValue(-1,((TypedNode)node).getTypedValue()); 79 psvisp.setNormalizedValue(-1,((TypedNode)node).getNormalizedStringValue()); 80 } 81 String value = null; 83 if (qAtomicSerializer != null) { 84 Object typedValue = ((TypedNode)node).getTypedValue(); 85 try { 86 value = qAtomicSerializer.serialize((SimpleType)((TypedNode)node).getType(), typedValue); 87 } catch (XQueryException e) { 88 throw new SAXException (e); 89 } 90 } 91 if (value == null) { 92 value = node.getNodeValue(); 93 } 94 if (value != null) { 95 contentHandler.characters(value.toCharArray(), 0, value.length()); 96 } 97 } 98 99 100 protected void generateStartElement(Node node) throws SAXException { 101 102 String uri = node.getNamespaceURI(); 104 if (uri == null) uri = ""; 105 String local = node.getLocalName(); 106 if (local == null) local = ""; 107 String prefix = node.getPrefix(); 108 String qname = prefix == null ? local : prefix + ":" +local; 109 110 wAtts.setDOMAttributes(node.getAttributes()); 111 Iterator it = wAtts.getPrefixIterator(); if (it != null) 113 while (it.hasNext()) { 114 Attr a = (Attr)it.next(); 115 contentHandler.startPrefixMapping( 116 a.getLocalName().equals(XMLNS_PREFIX) ? "": a.getLocalName(), a.getValue()); 118 } 119 120 if (psvisp != null) { 121 psvisp.pushElementInfoset(uri, local); 122 if (node instanceof TypedElement) { 123 TypedElement elt = (TypedElement)node; 124 psvisp.initElementValidationInfo((ElementDeclaration)elt.getDeclaration(),elt.getType(),false,SchemaConstants.LAX); 125 for (int i = 0; i < wAtts.getLength(); i++) { 126 String namespace = wAtts.getURI(i); 127 String localName = wAtts.getLocalName(i); 128 String attrValue = wAtts.getValue(i); 129 TypedAttribute attr = (TypedAttribute)elt.getAttributeNodeNS(namespace, localName); 130 int index = psvisp.addAttributePSVI(namespace, localName, (AttributeDeclaration)attr.getDeclaration(), true); 131 psvisp.setActualValue(index, attr.getTypedValue()); 132 psvisp.setNormalizedValue(index, attr.getNormalizedStringValue()); 133 } 134 } 135 } 136 137 contentHandler.startElement(uri, local, qname, wAtts); 138 139 } 141 142 protected void generateEndElement(Node node) throws SAXException { 143 super.generateEndElement(node); 144 if (psvisp != null) { 145 psvisp.popElementInfoset(); 146 } 147 } 148 149 } 150 151 152 | Popular Tags |