List of usage examples for org.dom4j.io SAXReader setProperty
public void setProperty(String name, Object value) throws SAXException
From source file:org.gmock.utils.JavaTestHelper.java
License:Apache License
public static void setSAXReaderProperty(SAXReader reader) throws SAXException { reader.setProperty(SCHEMA_LANGUAGE, SCHEMA_LANGUAGE_VALUE); }
From source file:org.hibernate.cfg.AnnotationConfiguration.java
License:Open Source License
private void setValidationFor(SAXReader saxReader, String xsd) { try {//from w ww .j av a 2 s . c om saxReader.setFeature("http://apache.org/xml/features/validation/schema", true); //saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic", true ); //set the default schema locators saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://java.sun.com/xml/ns/persistence/orm " + xsd); } catch (SAXException e) { saxReader.setValidation(false); } }
From source file:org.hibernate.ejb.Ejb3Configuration.java
License:Open Source License
private void addXMLEntities(List<String> xmlFiles, PersistenceUnitInfo info, List<String> entities) { //TODO handle inputstream related hbm files ClassLoader newTempClassLoader = info.getNewTempClassLoader(); if (newTempClassLoader == null) { log.warn(/*from w ww .j ava2s .c om*/ "Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null."); return; } XMLHelper xmlHelper = new XMLHelper(); List errors = new ArrayList(); SAXReader saxReader = xmlHelper.createSAXReader("XML InputStream", errors, cfg.getEntityResolver()); try { saxReader.setFeature("http://apache.org/xml/features/validation/schema", true); //saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic", true ); //set the default schema locators saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"); } catch (SAXException e) { saxReader.setValidation(false); } for (String xmlFile : xmlFiles) { InputStream resourceAsStream = newTempClassLoader.getResourceAsStream(xmlFile); if (resourceAsStream == null) continue; BufferedInputStream is = new BufferedInputStream(resourceAsStream); try { errors.clear(); org.dom4j.Document doc = saxReader.read(is); if (errors.size() != 0) { throw new MappingException("invalid mapping: " + xmlFile, (Throwable) errors.get(0)); } Element rootElement = doc.getRootElement(); if (rootElement != null && "entity-mappings".equals(rootElement.getName())) { Element element = rootElement.element("package"); String defaultPackage = element != null ? element.getTextTrim() : null; List<Element> elements = rootElement.elements("entity"); for (Element subelement : elements) { String classname = XMLContext.buildSafeClassName(subelement.attributeValue("class"), defaultPackage); if (!entities.contains(classname)) { entities.add(classname); } } elements = rootElement.elements("mapped-superclass"); for (Element subelement : elements) { String classname = XMLContext.buildSafeClassName(subelement.attributeValue("class"), defaultPackage); if (!entities.contains(classname)) { entities.add(classname); } } elements = rootElement.elements("embeddable"); for (Element subelement : elements) { String classname = XMLContext.buildSafeClassName(subelement.attributeValue("class"), defaultPackage); if (!entities.contains(classname)) { entities.add(classname); } } } else if (rootElement != null && "hibernate-mappings".equals(rootElement.getName())) { //FIXME include hbm xml entities to enhance them but entities is also used to collect annotated entities } } catch (DocumentException e) { throw new MappingException("Could not parse mapping document in input stream", e); } finally { try { is.close(); } catch (IOException ioe) { log.warn("Could not close input stream", ioe); } } } }
From source file:org.hibernate.internal.util.xml.MappingReader.java
License:LGPL
private void setValidationFor(SAXReader saxReader, String xsd) { try {//from www.java 2 s.co m saxReader.setFeature("http://apache.org/xml/features/validation/schema", true); // saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic", true ); if ("orm_2_1.xsd".equals(xsd)) { saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://xmlns.jcp.org/xml/ns/persistence/orm " + xsd); } else { saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://java.sun.com/xml/ns/persistence/orm " + xsd); } } catch (SAXException e) { saxReader.setValidation(false); } }
From source file:org.mule.module.xml.util.XMLUtils.java
License:Open Source License
/** * Converts an object of unknown type to an org.dom4j.Document if possible. * @return null if object cannot be converted * @throws DocumentException if an error occurs while parsing *///from w ww .j av a2 s . c o m public static org.dom4j.Document toDocument(Object obj, String externalSchemaLocation, MuleContext muleContext) throws Exception { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); if (externalSchemaLocation != null) { reader.setValidation(true); reader.setFeature(APACHE_XML_FEATURES_VALIDATION_SCHEMA, true); reader.setFeature(APACHE_XML_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, true); InputStream xsdAsStream = IOUtils.getResourceAsStream(externalSchemaLocation, XMLUtils.class); if (xsdAsStream == null) { throw new IllegalArgumentException("Couldn't find schema at " + externalSchemaLocation); } // Set schema language property (must be done before the schemaSource // is set) reader.setProperty(JAXP_PROPERTIES_SCHEMA_LANGUAGE, JAXP_PROPERTIES_SCHEMA_LANGUAGE_VALUE); // Need this one to map schemaLocation to a physical location reader.setProperty(JAXP_PROPERTIES_SCHEMA_SOURCE, xsdAsStream); } if (obj instanceof org.dom4j.Document) { return (org.dom4j.Document) obj; } else if (obj instanceof org.w3c.dom.Document) { org.dom4j.io.DOMReader domReader = new org.dom4j.io.DOMReader(); return domReader.read((org.w3c.dom.Document) obj); } else if (obj instanceof org.xml.sax.InputSource) { return reader.read((org.xml.sax.InputSource) obj); } else if (obj instanceof javax.xml.transform.Source || obj instanceof javax.xml.stream.XMLStreamReader) { // TODO Find a more direct way to do this XmlToDomDocument tr = new XmlToDomDocument(); tr.setMuleContext(muleContext); tr.setReturnDataType(DataTypeFactory.create(org.dom4j.Document.class)); return (org.dom4j.Document) tr.transform(obj); } else if (obj instanceof java.io.InputStream) { return reader.read((java.io.InputStream) obj); } else if (obj instanceof String) { return reader.read(new StringReader((String) obj)); } else if (obj instanceof byte[]) { // TODO Handle encoding/charset somehow return reader.read(new StringReader(new String((byte[]) obj))); } else if (obj instanceof File) { return reader.read((File) obj); } else { return null; } }
From source file:org.sysmodb.SpreadsheetTestHelper.java
License:BSD License
public static void validateAgainstSchema(String xml) throws Exception { URL resource = WorkbookParserXMLTest.class.getResource("/schema-v1.xsd"); SAXReader reader = new SAXReader(true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", new File(resource.getFile())); InputSource source = new InputSource(new StringReader(xml)); source.setEncoding("UTF-8"); try {/*from ww w . j a v a 2 s.co m*/ reader.read(source); } catch (DocumentException e) { // System.out.println(xml); throw e; } }