1 17 package org.apache.ws.jaxme.xs; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import javax.xml.parsers.ParserConfigurationException ; 24 25 import org.apache.ws.jaxme.xs.impl.XSLogicalParser; 26 import org.apache.ws.jaxme.xs.parser.XSContext; 27 import org.apache.ws.jaxme.xs.parser.XsSAXParser; 28 import org.apache.ws.jaxme.xs.parser.impl.XSContextImpl; 29 import org.apache.ws.jaxme.xs.xml.XsESchema; 30 import org.apache.ws.jaxme.xs.xml.XsObjectFactory; 31 import org.w3c.dom.Node ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.XMLReader ; 35 36 37 42 public class XSParser { 43 45 public static final String XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema"; 46 47 private static final ThreadLocal parser = new ThreadLocal (); 48 49 private XSContext data; 50 private boolean notValidating; 51 private List addedImports = new ArrayList (); 52 53 55 public XSParser() { 56 } 57 58 60 public void setValidating(boolean pValidating) { 61 notValidating = !pValidating; 62 } 63 64 66 public boolean isValidating() { 67 return !notValidating; 68 } 69 70 81 public void addImport(String pNamespace, String pSchemaLocation) { 82 addedImports.add(new XSLogicalParser.AddedImport(pNamespace, pSchemaLocation)); 83 } 84 85 99 public void addImport(String pNamespace, String pSchemaLocation, Node pSchema) { 100 addedImports.add(new XSLogicalParser.AddedImport(pNamespace, pSchemaLocation, pSchema)); 101 } 102 103 106 public XSContext getContext() { 107 if (data == null) { 108 setData(new XSContextImpl()); 109 } 110 return data; 111 } 112 113 protected void setData(XSContext pData) { data = pData; } 114 115 119 public XsESchema parseSyntax(InputSource pSource) throws ParserConfigurationException , SAXException , IOException { 120 XSContext context = getContext(); 121 parser.set(this); 122 try { 123 XsObjectFactory factory = context.getXsObjectFactory(); 124 XsSAXParser xsSAXParser = factory.newXsSAXParser(); 125 context.setCurrentContentHandler(xsSAXParser); 126 XMLReader xr = factory.newXMLReader(isValidating()); 127 xr.setContentHandler(xsSAXParser); 128 xr.parse(pSource); 129 return (XsESchema) xsSAXParser.getBean(); 130 } finally { 131 context.setCurrentContentHandler(null); 132 parser.set(null); 133 } 134 } 135 136 protected XSLogicalParser newXSLogicalParser() { 137 XSLogicalParser logicalParser = getContext().getXSObjectFactory().newXSLogicalParser(); 138 logicalParser.setValidating(isValidating()); 139 for (int i = 0; i < addedImports.size(); i++) { 140 XSLogicalParser.AddedImport addedImport = (XSLogicalParser.AddedImport) addedImports.get(i); 141 if (addedImport.getNode() == null) { 142 logicalParser.addImport(addedImport.getNamespace(), addedImport.getSchemaLocation()); 143 } else { 144 logicalParser.addImport(addedImport.getNamespace(), addedImport.getSchemaLocation(), addedImport.getNode()); 145 } 146 } 147 return logicalParser; 148 } 149 150 152 public XSSchema parse(InputSource pSource) throws ParserConfigurationException , SAXException , IOException { 153 XSContext myData = getContext(); 154 parser.set(this); 155 try { 156 return newXSLogicalParser().parse(pSource); 157 } finally { 158 myData.setCurrentContentHandler(null); 159 parser.set(null); 160 } 161 } 162 163 171 public XSSchema parse(Node pNode) throws SAXException { 172 XSContext myData = getContext(); 173 parser.set(this); 174 try { 175 return newXSLogicalParser().parse(pNode); 176 } finally { 177 myData.setCurrentContentHandler(null); 178 parser.set(null); 179 } 180 } 181 182 185 public XSContentHandler getXSContentHandler(String pSchemaLocation) throws SAXException { 186 parser.set(this); 187 return newXSLogicalParser().getXSContentHandler(pSchemaLocation); 188 } 189 190 192 public static XSParser getRunningInstance() { 193 return (XSParser) parser.get(); 194 } 195 } 196 | Popular Tags |