private QName getQName(String name) { if (targetNamespace == null) { return documentFactory.createQName(name); } else { return documentFactory.createQName(name, targetNamespace); } }
public Attribute createAttribute(Element owner, QName qname, String value) { if (autoLoadSchema && qname.equals(XSI_NO_SCHEMA_LOCATION)) { Document document = (owner != null) ? owner.getDocument() : null; loadSchema(document, value); } else if (autoLoadSchema && qname.equals(XSI_SCHEMA_LOCATION)) { Document document = (owner != null) ? owner.getDocument() : null; String uri = value.substring(0, value.indexOf(' ')); Namespace namespace = owner.getNamespaceForURI(uri); loadSchema(document, value.substring(value.indexOf(' ') + 1), namespace); } return super.createAttribute(owner, qname, value); }
/** * DOCUMENT ME! * * @param name * The name of the element * * @return the <code>DatatypeElementFactory</code> for the given element * QName, creating one if it does not already exist */ private DatatypeElementFactory getDatatypeElementFactory(QName name) { DatatypeElementFactory factory = documentFactory .getElementFactory(name); if (factory == null) { factory = new DatatypeElementFactory(name); name.setDocumentFactory(factory); } return factory; }
protected DocumentFactory loadDocumentFactory() throws Exception { DatatypeDocumentFactory factory = new DatatypeDocumentFactory(); Document schemaDocument = getDocument("/xml/test/schema/personal.xsd"); factory.loadSchema(schemaDocument); return factory; } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testAttributeWithNamespace() throws Exception { QName personName = factory.createQName("person", "t", "urn://testing"); QName ageName = factory.createQName("age", "t", "urn://testing"); Element person = factory.createElement(personName); person.addAttribute(ageName, "10"); Attribute age = person.attribute(ageName); assertTrue("Created DatatypeAttribute not correct", age instanceof DatatypeAttribute); log("Found attribute: " + age); Object data = age.getData(); Object expected = new BigInteger("10"); assertEquals("Data is correct type", BigInteger.class, data.getClass()); assertEquals("Set age correctly", expected, data); age.setValue("32"); data = age.getData(); expected = new BigInteger("32"); assertEquals("Set age correctly", expected, data); age.setValue("abc"); fail("Appeared to set an invalid value"); }
protected DocumentFactory loadDocumentFactory() throws Exception { DatatypeDocumentFactory factory = new DatatypeDocumentFactory(); Document schemaDocument = getDocument("/xml/test/schema/personal-prefix.xsd"); factory.loadSchema(schemaDocument); return factory; } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testAttribute() throws Exception { QName personName = factory.createQName("person"); QName ageName = factory.createQName("age"); Element person = factory.createElement(personName); person.addAttribute(ageName, "10"); Attribute age = person.attribute(ageName); assertTrue("Created DatatypeAttribute not correct", age instanceof DatatypeAttribute); log("Found attribute: " + age); Object data = age.getData(); Object expected = new BigInteger("10"); assertEquals("Data is correct type", BigInteger.class, data.getClass()); assertEquals("Set age correctly", expected, data); age.setValue("32"); data = age.getData(); expected = new BigInteger("32"); assertEquals("Set age correctly", expected, data); /** * not sure if numeric types should be round tripped back to BigDecimal * (say) age.setData( new Long( 21 ) ); data = age.getData(); expected = * new BigInteger( "21" ); assertEquals( "Set age correctly", expected, * data ); */ // now lets set an invalid value age.setValue("abc"); fail("Appeared to set an invalid value"); }
protected void loadSchema(Document document, String schemaInstanceURI) { try { EntityResolver resolver = document.getEntityResolver(); if (resolver == null) { String msg = "No EntityResolver available for resolving URI: "; throw new InvalidSchemaException(msg + schemaInstanceURI); } InputSource inputSource = resolver.resolveEntity(null, schemaInstanceURI); if (resolver == null) { throw new InvalidSchemaException("Could not resolve the URI: " + schemaInstanceURI); } Document schemaDocument = xmlSchemaReader.read(inputSource); loadSchema(schemaDocument); } catch (Exception e) { System.out.println("Failed to load schema: " + schemaInstanceURI); System.out.println("Caught: " + e); e.printStackTrace(); throw new InvalidSchemaException("Failed to load schema: " + schemaInstanceURI); } }
private QName getQName(String name) { if (targetNamespace == null) { return documentFactory.createQName(name); } else { return documentFactory.createQName(name, targetNamespace); } }
/** * DOCUMENT ME! * * @param name * The name of the element * * @return the <code>DatatypeElementFactory</code> for the given element * QName, creating one if it does not already exist */ private DatatypeElementFactory getDatatypeElementFactory(QName name) { DatatypeElementFactory factory = documentFactory .getElementFactory(name); if (factory == null) { factory = new DatatypeElementFactory(name); name.setDocumentFactory(factory); } return factory; }
private Document getSource(Document schema) throws Exception { StringBuffer b = new StringBuffer(); b.append("<?xml version='1.0' ?>"); b.append("<test xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"); b.append(" xsi:noNamespaceSchemaLocation='long.xsd'"); b.append(" longAttribute='123' >"); b.append(" <floatElement>1.23</floatElement>"); b.append(" <dateElement>" + getDateString() + "</dateElement>"); b.append("</test>"); StringReader in = new StringReader(b.toString()); DatatypeDocumentFactory docFactory = new DatatypeDocumentFactory(); docFactory.loadSchema(schema); SAXReader parser = new SAXReader(docFactory); return parser.read(in); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testElement() throws Exception { QName personName = factory.createQName("person"); QName numberOfCarsName = factory.createQName("numberOfCars"); Element person = factory.createElement(personName); Element cars = person.addElement(numberOfCarsName); log("Found element: " + cars); short expected = 10; cars.setData(expected); Object data = cars.getData(); assertEquals("Data is correct type", Short.class, data.getClass()); assertEquals("Set cars correctly", expected, data); cars.setData((short) 32); data = cars.getData(); expected = 32; assertEquals("Set cars correctly", expected, data); cars.setText("34"); data = cars.getData(); expected = 34; assertEquals("Set cars correctly", expected, data); // now lets set an invalid value cars.setText("abc"); fail("Appeared to set an invalid value"); }
protected void loadSchema(Document document, String schemaInstanceURI, Namespace namespace) { try { EntityResolver resolver = document.getEntityResolver(); if (resolver == null) { String msg = "No EntityResolver available for resolving URI: "; throw new InvalidSchemaException(msg + schemaInstanceURI); } InputSource inputSource = resolver.resolveEntity(null, schemaInstanceURI); if (resolver == null) { throw new InvalidSchemaException("Could not resolve the URI: " + schemaInstanceURI); } Document schemaDocument = xmlSchemaReader.read(inputSource); loadSchema(schemaDocument, namespace); } catch (Exception e) { System.out.println("Failed to load schema: " + schemaInstanceURI); System.out.println("Caught: " + e); e.printStackTrace(); throw new InvalidSchemaException("Failed to load schema: " + schemaInstanceURI); } } }
private QName getQName(String name) { if (targetNamespace == null) { return documentFactory.createQName(name); } else { return documentFactory.createQName(name, targetNamespace); } }
/** * DOCUMENT ME! * * @param name * The name of the element * * @return the <code>DatatypeElementFactory</code> for the given element * QName, creating one if it does not already exist */ private DatatypeElementFactory getDatatypeElementFactory(QName name) { DatatypeElementFactory factory = documentFactory .getElementFactory(name); if (factory == null) { factory = new DatatypeElementFactory(name); name.setDocumentFactory(factory); } return factory; }
public void testParseSchema() throws Exception { DatatypeDocumentFactory factory = new DatatypeDocumentFactory(); SAXReader reader = new SAXReader(); reader.setDocumentFactory(factory); Document schema = getDocument("/xml/test/LuisSchema.xsd", reader); factory.loadSchema(schema); log("Loaded the schema"); // now load an instance document } }
public Attribute createAttribute(Element owner, QName qname, String value) { if (autoLoadSchema && qname.equals(XSI_NO_SCHEMA_LOCATION)) { Document document = (owner != null) ? owner.getDocument() : null; loadSchema(document, value); } else if (autoLoadSchema && qname.equals(XSI_SCHEMA_LOCATION)) { Document document = (owner != null) ? owner.getDocument() : null; String uri = value.substring(0, value.indexOf(' ')); Namespace namespace = owner.getNamespaceForURI(uri); loadSchema(document, value.substring(value.indexOf(' ') + 1), namespace); } return super.createAttribute(owner, qname, value); }
private QName getQName(String name) { if (targetNamespace == null) { return documentFactory.createQName(name); } else { return documentFactory.createQName(name, targetNamespace); } }
/** * DOCUMENT ME! * * @param name * The name of the element * * @return the <code>DatatypeElementFactory</code> for the given element * QName, creating one if it does not already exist */ private DatatypeElementFactory getDatatypeElementFactory(QName name) { DatatypeElementFactory factory = documentFactory .getElementFactory(name); if (factory == null) { factory = new DatatypeElementFactory(name); name.setDocumentFactory(factory); } return factory; }
public Attribute createAttribute(Element owner, QName qname, String value) { if (autoLoadSchema && qname.equals(XSI_NO_SCHEMA_LOCATION)) { Document document = (owner != null) ? owner.getDocument() : null; loadSchema(document, value); } else if (autoLoadSchema && qname.equals(XSI_SCHEMA_LOCATION)) { Document document = (owner != null) ? owner.getDocument() : null; String uri = value.substring(0, value.indexOf(' ')); Namespace namespace = owner.getNamespaceForURI(uri); loadSchema(document, value.substring(value.indexOf(' ') + 1), namespace); } return super.createAttribute(owner, qname, value); }