Search in sources :

Example 61 with Schema

use of javax.xml.validation.Schema in project nokogiri by sparklemotion.

the class XmlSchema method createSchemaInstance.

static XmlSchema createSchemaInstance(ThreadContext context, RubyClass klazz, Source source) {
    Ruby runtime = context.getRuntime();
    XmlSchema xmlSchema = (XmlSchema) NokogiriService.XML_SCHEMA_ALLOCATOR.allocate(runtime, klazz);
    xmlSchema.setInstanceVariable("@errors", runtime.newEmptyArray());
    try {
        Schema schema = xmlSchema.getSchema(source, context.getRuntime().getCurrentDirectory(), context.getRuntime().getInstanceConfig().getScriptFileName());
        xmlSchema.setValidator(schema.newValidator());
        return xmlSchema;
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    }
}
Also used : Schema(javax.xml.validation.Schema) Ruby(org.jruby.Ruby) SAXException(org.xml.sax.SAXException)

Example 62 with Schema

use of javax.xml.validation.Schema in project CoreNLP by stanfordnlp.

the class XMLUtils method getValidatingXmlParser.

/**
   * Returns a validating XML parser given an XSD (not DTD!).
   *
   * @param schemaFile File wit hXML schema
   * @return An XML parser in the form of a DocumentBuilder
   */
public static DocumentBuilder getValidatingXmlParser(File schemaFile) {
    DocumentBuilder db = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(schemaFile);
        dbf.setSchema(schema);
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(new SAXErrorHandler());
    } catch (ParserConfigurationException e) {
        System.err.printf("%s: Unable to create XML parser\n", XMLUtils.class.getName());
        e.printStackTrace();
    } catch (SAXException e) {
        System.err.printf("%s: XML parsing exception while loading schema %s\n", XMLUtils.class.getName(), schemaFile.getPath());
        e.printStackTrace();
    } catch (UnsupportedOperationException e) {
        System.err.printf("%s: API error while setting up XML parser. Check your JAXP version\n", XMLUtils.class.getName());
        e.printStackTrace();
    }
    return db;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Schema(javax.xml.validation.Schema) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 63 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class ValidateV2RC2SamplesTest method unmarshall.

private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaPath != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 64 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class ValidateV2RC2SamplesTest method marshall.

private void marshall(Object object, String path) throws JAXBException, SAXException, URISyntaxException {
    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File(getClass().getResource(path).toURI()));
    marshaller.setSchema(schema);
    marshaller.marshal(object, System.out);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Marshaller(javax.xml.bind.Marshaller) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) File(java.io.File)

Example 65 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class ValidateV2RC1SamplesTest method validateGroupIdValue.

@Test
public void validateGroupIdValue() throws SAXException, IOException, JAXBException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/group-id-2.0_rc1/group-id-2.0_rc1.xsd"));
    Validator validator = schema.newValidator();
    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char[] upperAlphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase().toCharArray();
    char[] numbers = "0123456789".toCharArray();
    char[] validCharacters = "^._~:/?#[]@!$&'()*+,;=-".toCharArray();
    //All valid characters
    char[] allValid = ArrayUtils.addAll(alphabet, upperAlphabet);
    allValid = ArrayUtils.addAll(allValid, numbers);
    allValid = ArrayUtils.addAll(allValid, validCharacters);
    String invalidCharactersString = "{}\"<>\\";
    char[] invalidCharacters = invalidCharactersString.toCharArray();
    //All valid and non valid characters
    char[] allWithInvalids = ArrayUtils.addAll(allValid, invalidCharacters);
    GroupIdRecord g1 = new GroupIdRecord();
    g1.setDescription("description");
    g1.setType("newsletter");
    System.out.println("Validating group_id agains a list of 3000 valid values");
    for (int i = 0; i < 3000; i++) {
        String randomValue = "orcid-generated:" + RandomStringUtils.random(200, allValid);
        g1.setName(randomValue);
        g1.setGroupId(randomValue);
        JAXBContext context;
        context = JAXBContext.newInstance(GroupIdRecord.class);
        Source source = new JAXBSource(context, g1);
        try {
            validator.validate(source);
        } catch (Exception e) {
            fail("fail validating: " + randomValue + " on iteration " + i);
        }
    }
    System.out.println("Validating group_id agains a list of 3000 invalid values");
    for (int i = 0; i < 3000; i++) {
        String randomValue = "orcid-generated:" + RandomStringUtils.random(200, allWithInvalids);
        boolean regenerateString = true;
        do {
            for (int j = 0; j < randomValue.length(); j++) {
                if (invalidCharactersString.contains(String.valueOf(randomValue.charAt(j)))) {
                    regenerateString = false;
                    break;
                }
            }
            if (regenerateString) {
                randomValue += RandomStringUtils.random(3, invalidCharacters);
                regenerateString = false;
            }
        } while (regenerateString);
        g1.setName(randomValue);
        g1.setGroupId(randomValue);
        JAXBContext context;
        context = JAXBContext.newInstance(GroupIdRecord.class);
        Source source = new JAXBSource(context, g1);
        try {
            validator.validate(source);
            fail(randomValue + " should not be vaild according to the XSD on iteration " + i);
        } catch (Exception e) {
        }
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) GroupIdRecord(org.orcid.jaxb.model.groupid_rc1.GroupIdRecord) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) Validator(javax.xml.validation.Validator) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) JAXBSource(javax.xml.bind.util.JAXBSource) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Aggregations

Schema (javax.xml.validation.Schema)102 SchemaFactory (javax.xml.validation.SchemaFactory)72 Validator (javax.xml.validation.Validator)51 StreamSource (javax.xml.transform.stream.StreamSource)45 SAXException (org.xml.sax.SAXException)35 Source (javax.xml.transform.Source)29 DOMSource (javax.xml.transform.dom.DOMSource)25 IOException (java.io.IOException)22 JAXBContext (javax.xml.bind.JAXBContext)18 Document (org.w3c.dom.Document)18 InputStream (java.io.InputStream)17 File (java.io.File)15 URL (java.net.URL)15 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 JAXBException (javax.xml.bind.JAXBException)13 Unmarshaller (javax.xml.bind.Unmarshaller)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 InputSource (org.xml.sax.InputSource)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 SAXParseException (org.xml.sax.SAXParseException)9