PageRenderTime 123ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidatorHandler.java

https://bitbucket.org/pizzafactory/pf-gcc
Java | 393 lines | 307 code | 34 blank | 52 comment | 44 complexity | c38205fd8a426e0fe596e4935d97cec0 MD5 | raw file
  1. /* XMLSchemaValidatorHandler.java --
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.validation.xmlschema;
  32. import java.util.ArrayList;
  33. import java.util.LinkedList;
  34. import javax.xml.XMLConstants;
  35. import javax.xml.namespace.QName;
  36. import javax.xml.validation.TypeInfoProvider;
  37. import javax.xml.validation.ValidatorHandler;
  38. import org.relaxng.datatype.DatatypeException;
  39. import org.relaxng.datatype.DatatypeLibrary;
  40. import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
  41. import org.w3c.dom.TypeInfo;
  42. import org.w3c.dom.ls.LSResourceResolver;
  43. import org.xml.sax.Attributes;
  44. import org.xml.sax.ContentHandler;
  45. import org.xml.sax.ErrorHandler;
  46. import org.xml.sax.Locator;
  47. import org.xml.sax.SAXException;
  48. import org.xml.sax.ext.Attributes2Impl;
  49. import org.xml.sax.helpers.NamespaceSupport;
  50. import gnu.xml.validation.datatype.SimpleType;
  51. import gnu.xml.validation.datatype.Type;
  52. /**
  53. * Streaming validator.
  54. *
  55. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  56. */
  57. final class XMLSchemaValidatorHandler
  58. extends ValidatorHandler
  59. {
  60. final XMLSchema schema;
  61. final TypeInfoProvider typeInfoProvider;
  62. final NamespaceSupport namespaceSupport;
  63. final DatatypeLibrary typeLibrary;
  64. Locator loc;
  65. ContentHandler contentHandler;
  66. ErrorHandler errorHandler;
  67. LSResourceResolver resourceResolver;
  68. final LinkedList context; // element context
  69. final ArrayList attributes; // attribute context;
  70. XMLSchemaValidatorHandler(XMLSchema schema)
  71. {
  72. this.schema = schema;
  73. typeInfoProvider = new XMLSchemaTypeInfoProvider(this);
  74. namespaceSupport = new NamespaceSupport();
  75. context = new LinkedList();
  76. attributes = new ArrayList();
  77. final String ns = XMLConstants.W3C_XML_SCHEMA_NS_URI;
  78. typeLibrary = new DatatypeLibraryLoader().createDatatypeLibrary(ns);
  79. }
  80. public ContentHandler getContentHandler()
  81. {
  82. return contentHandler;
  83. }
  84. public void setContentHandler(ContentHandler contentHandler)
  85. {
  86. this.contentHandler = contentHandler;
  87. }
  88. public ErrorHandler getErrorHandler()
  89. {
  90. return errorHandler;
  91. }
  92. public void setErrorHandler(ErrorHandler errorHandler)
  93. {
  94. this.errorHandler = errorHandler;
  95. }
  96. public LSResourceResolver getResourceResolver()
  97. {
  98. return resourceResolver;
  99. }
  100. public void setResourceResolver(LSResourceResolver resourceResolver)
  101. {
  102. this.resourceResolver = resourceResolver;
  103. }
  104. public TypeInfoProvider getTypeInfoProvider()
  105. {
  106. return typeInfoProvider;
  107. }
  108. TypeInfo getElementTypeInfo()
  109. {
  110. return (XMLSchemaElementTypeInfo) context.getFirst();
  111. }
  112. TypeInfo getAttributeTypeInfo(int index)
  113. {
  114. return (XMLSchemaAttributeTypeInfo) attributes.get(index);
  115. }
  116. boolean isIdAttribute(int index)
  117. {
  118. XMLSchemaAttributeTypeInfo typeInfo =
  119. (XMLSchemaAttributeTypeInfo) attributes.get(index);
  120. return typeInfo.id;
  121. }
  122. boolean isSpecified(int index)
  123. {
  124. XMLSchemaAttributeTypeInfo typeInfo =
  125. (XMLSchemaAttributeTypeInfo) attributes.get(index);
  126. return typeInfo.specified;
  127. }
  128. public void setDocumentLocator(Locator locator)
  129. {
  130. loc = locator;
  131. if (contentHandler != null)
  132. {
  133. contentHandler.setDocumentLocator(locator);
  134. }
  135. }
  136. public void startDocument()
  137. throws SAXException
  138. {
  139. namespaceSupport.reset();
  140. context.clear();
  141. attributes.clear();
  142. if (contentHandler != null)
  143. {
  144. contentHandler.startDocument();
  145. }
  146. }
  147. public void endDocument()
  148. throws SAXException
  149. {
  150. if (contentHandler != null)
  151. {
  152. contentHandler.endDocument();
  153. }
  154. }
  155. public void startPrefixMapping(String prefix, String uri)
  156. throws SAXException
  157. {
  158. namespaceSupport.declarePrefix(prefix, uri);
  159. if (contentHandler != null)
  160. {
  161. contentHandler.startPrefixMapping(prefix, uri);
  162. }
  163. }
  164. public void endPrefixMapping(String prefix)
  165. throws SAXException
  166. {
  167. if (contentHandler != null)
  168. {
  169. contentHandler.endPrefixMapping(prefix);
  170. }
  171. }
  172. public void startElement(String uri, String localName, String qName,
  173. Attributes atts)
  174. throws SAXException
  175. {
  176. namespaceSupport.pushContext();
  177. QName name = new QName(uri, localName);
  178. ElementDeclaration decl =
  179. (ElementDeclaration) schema.elementDeclarations.get(name);
  180. // Validation Rule: Element Locally Valid (Element)
  181. String xsiType =
  182. atts.getValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type");
  183. xsiType = xsiType.trim(); // normalise
  184. String xsiNil =
  185. atts.getValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil");
  186. Type type = decl.datatype;
  187. if (xsiType.length() > 0)
  188. {
  189. try
  190. {
  191. Type specifiedType = resolveType(xsiType);
  192. // TODO 4.3
  193. type = specifiedType;
  194. }
  195. catch (DatatypeException e) // 4.1, 4.2
  196. {
  197. ValidationException e2 =
  198. new ValidationException("Can't resolve type " + xsiType,
  199. loc);
  200. e2.initCause(e);
  201. throw e2;
  202. }
  203. }
  204. XMLSchemaElementTypeInfo typeInfo =
  205. new XMLSchemaElementTypeInfo(schema, decl, type);
  206. if (decl == null) // 1
  207. {
  208. throw new ValidationException("No declaration for " + name, loc);
  209. }
  210. if (decl.isAbstract) // 2
  211. {
  212. throw new ValidationException("Declaration for " + name +
  213. " is abstract", loc);
  214. }
  215. if (xsiNil.length() > 0)
  216. {
  217. if (!decl.nillable) // 3.1
  218. {
  219. throw new ValidationException("Declaration for " + name +
  220. " is nillable but xsi:nil present",
  221. loc);
  222. }
  223. else if ("true".equals(xsiNil)) // 3.2
  224. {
  225. typeInfo.nil = true;
  226. if (decl.type == XMLSchema.CONSTRAINT_FIXED) // 3.2.2
  227. {
  228. throw new ValidationException("Declaration for " + name +
  229. " is fixed but xsi:nil is true",
  230. loc);
  231. }
  232. }
  233. }
  234. // TODO 5, 6, 7
  235. // parent
  236. if (!context.isEmpty())
  237. {
  238. XMLSchemaElementTypeInfo parent =
  239. (XMLSchemaElementTypeInfo) context.getFirst();
  240. if (parent.nil) // Element Locally Valid (Element) 3.2.1
  241. {
  242. throw new ValidationException("Parent of " + qName +
  243. " is declared xsi:nil", loc);
  244. }
  245. // TODO
  246. }
  247. context.addFirst(typeInfo);
  248. // attributes
  249. int len = atts.getLength();
  250. Attributes2Impl atts2 = new Attributes2Impl();
  251. int count = 0;
  252. for (int i = 0; i < len; i++)
  253. {
  254. String attUri = atts.getURI(i);
  255. String attLocalName = atts.getLocalName(i);
  256. String attQName = atts.getQName(i);
  257. String attValue = atts.getValue(i);
  258. if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(attUri))
  259. {
  260. continue; // ?
  261. }
  262. QName attName = new QName(attUri, attLocalName);
  263. AttributeDeclaration attDecl =
  264. (AttributeDeclaration) schema.attributeDeclarations.get(attName);
  265. boolean declared = (attDecl != null);
  266. String attType = (attDecl != null) ?
  267. attDecl.datatype.toString() : "CDATA";
  268. XMLSchemaAttributeTypeInfo attTypeInfo =
  269. new XMLSchemaAttributeTypeInfo(schema, attDecl, true);
  270. attributes.add(attTypeInfo);
  271. atts2.addAttribute(attUri, attLocalName, attQName, attType, attValue);
  272. atts2.setDeclared(count, declared);
  273. atts2.setSpecified(count, true);
  274. count++;
  275. }
  276. // add defaulted attributes to atts2
  277. // TODO
  278. // atts2.setSpecified(count, false);
  279. if (contentHandler != null)
  280. {
  281. contentHandler.startElement(uri, localName, qName, atts2);
  282. }
  283. }
  284. public void endElement(String uri, String localName, String qName)
  285. throws SAXException
  286. {
  287. // TODO check all required have been seen
  288. context.removeFirst();
  289. attributes.clear();
  290. namespaceSupport.popContext();
  291. if (contentHandler != null)
  292. {
  293. contentHandler.endElement(uri, localName, qName);
  294. }
  295. }
  296. public void characters(char[] ch, int start, int length)
  297. throws SAXException
  298. {
  299. XMLSchemaElementTypeInfo parent =
  300. (XMLSchemaElementTypeInfo) context.getFirst();
  301. if (parent.nil) // Element Locally Valid (Element) 3.2.1
  302. {
  303. throw new ValidationException(parent.decl.name.toString() +
  304. " is declared xsi:nil",
  305. loc);
  306. }
  307. // TODO
  308. if (contentHandler != null)
  309. {
  310. contentHandler.characters(ch, start, length);
  311. }
  312. }
  313. public void ignorableWhitespace(char[] ch, int start, int length)
  314. throws SAXException
  315. {
  316. if (contentHandler != null)
  317. {
  318. contentHandler.ignorableWhitespace(ch, start, length);
  319. }
  320. }
  321. public void processingInstruction(String target, String data)
  322. throws SAXException
  323. {
  324. if (contentHandler != null)
  325. {
  326. contentHandler.processingInstruction(target, data);
  327. }
  328. }
  329. public void skippedEntity(String name)
  330. throws SAXException
  331. {
  332. if (contentHandler != null)
  333. {
  334. contentHandler.skippedEntity(name);
  335. }
  336. }
  337. Type resolveType(String value)
  338. throws DatatypeException
  339. {
  340. QName name = QName.valueOf(value);
  341. String prefix = name.getPrefix();
  342. String localName = name.getLocalPart();
  343. if (prefix != null && prefix.length() > 0)
  344. {
  345. String uri = namespaceSupport.getURI(prefix);
  346. if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri))
  347. return null;
  348. }
  349. if ("anyType".equals(localName))
  350. return Type.ANY_TYPE;
  351. return (SimpleType) typeLibrary.createDatatype(localName);
  352. }
  353. }