1 22 23 24 package org.xquark.xpath; 25 26 import java.util.Iterator ; 27 28 import org.xml.sax.*; 29 import org.xml.sax.helpers.AttributesImpl ; 30 import org.xquark.util.DefaultXMLReader; 31 import org.xquark.util.SAXConstants; 32 33 43 public class XTreeReader extends DefaultXMLReader implements SAXConstants 44 { 45 private static final String RCSRevision = "$Revision: 1.1 $"; 46 private static final String RCSName = "$Name: $"; 47 48 public static final String SOURCE_METADATA_NAMESPACE = 49 "http://www.xquark.org/XMLDBC/Metadata"; 50 public static final String SOURCE_METADATA_PREFIX = "xm"; 51 public static final String NS_ATTR = "ns"; 52 public static final String NAME_ATTR = "name"; 53 public static final String TYPE_ATTR = "type"; 54 public static final String ATTRIBUTE_TYPE_VALUE = "attribute"; 55 public static final String ELEMENT_TYPE_VALUE = "element"; 56 public static final String STEP_ELEMENT_NAME = "step"; 57 public static final String COLLECTION_ELEMENT_NAME = "collection"; 58 59 private XTree tree; 60 private boolean fragment = false; 61 private boolean trace = false; 62 private AttributesImpl atts = new AttributesImpl (); 63 64 68 public XTreeReader(XTree tree) 69 { 70 this.tree = tree; 71 } 72 73 76 public boolean getFeature(String name) 77 throws SAXNotRecognizedException, SAXNotSupportedException 78 { 79 if (name.equals(FRAGMENT_FEATURE)) 80 return fragment; 81 else if (name.equals(TRACE_FEATURE)) 82 return trace; 83 else 84 return super.getFeature(name); 85 } 86 87 90 public void setFeature(String name, boolean value) 91 throws SAXNotRecognizedException, SAXNotSupportedException 92 { 93 if (name.equals(FRAGMENT_FEATURE)) 94 fragment = value; 95 else if (name.equals(TRACE_FEATURE)) 96 trace = value; 97 else 98 super.setFeature(name, value); 99 } 100 101 104 public void setProperty(String name, Object value) 105 throws SAXNotRecognizedException, SAXNotSupportedException 106 { 107 if (name.equals(SAXConstants.SAX_LEXICAL_PROPERTY)) return; 109 else 110 super.setProperty(name, value); 111 } 112 113 117 public void parse(InputSource input) 118 throws SAXException, java.io.IOException 119 { 120 parse(""); 121 } 122 123 127 public void parse(String collectionName) throws SAXException, java.io.IOException 128 { 129 if (!fragment) 130 contentHandler.startDocument(); 131 132 contentHandler.startPrefixMapping( 133 SOURCE_METADATA_PREFIX, 134 SOURCE_METADATA_NAMESPACE); 135 136 if (collectionName != null) 137 atts.addAttribute("", NAME_ATTR, "", "CDATA", collectionName); 138 contentHandler.startElement( 139 SOURCE_METADATA_NAMESPACE, 140 COLLECTION_ELEMENT_NAME, 141 "", 142 atts); 143 atts.clear(); 144 145 XTreeNode root = tree.getRoot(); 146 if (root.getChildren() != null) 147 { 148 Iterator it = root.getChildren().iterator(); 149 while (it.hasNext()) 150 { 151 browseBranch((XTreeNode) it.next()); 152 } 153 } 154 contentHandler.endElement( 155 SOURCE_METADATA_NAMESPACE, 156 COLLECTION_ELEMENT_NAME, 157 ""); 158 160 contentHandler.endPrefixMapping(SOURCE_METADATA_PREFIX); 161 162 if (!fragment) 163 contentHandler.endDocument(); 164 } 165 166 private void browseBranch(XTreeNode node) 167 throws SAXException, java.io.IOException 168 { 169 if (node.getNamespace() != null) 170 atts.addAttribute("", NS_ATTR, "", "CDATA", node.getNamespace()); 171 172 atts.addAttribute("", NAME_ATTR, "", "CDATA", node.getLocalName()); 173 174 if (node.getType() == NodeKind.ATTRIBUTE) 175 atts.addAttribute("", TYPE_ATTR, "", "CDATA", ATTRIBUTE_TYPE_VALUE); 176 178 contentHandler.startElement( 179 SOURCE_METADATA_NAMESPACE, 180 STEP_ELEMENT_NAME, 181 "", 182 atts); 183 atts.clear(); 184 185 if (trace) 186 node.toXML(contentHandler); 187 188 if ((node.getType() == NodeKind.ELEMENT) && (node.getChildren() != null)) 190 { 191 Iterator it = node.getChildren().iterator(); 192 while (it.hasNext()) 193 { 194 browseBranch((XTreeNode) it.next()); 195 } 196 } 197 contentHandler.endElement( 198 SOURCE_METADATA_NAMESPACE, 199 STEP_ELEMENT_NAME, 200 ""); 201 } 202 203 } 204 | Popular Tags |