Search in sources :

Example 51 with SAXParseException

use of org.xml.sax.SAXParseException in project OpenClinica by OpenClinica.

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(File xmlFile, File xsdFile) {
    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        Document document = parser.parse(xmlFile);
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(xsdFile);
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException("File was not found", ex.getCause());
    } catch (IOException ioe) {
        throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
    } catch (SAXParseException spe) {
        //spe.printStackTrace();
        throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
    } catch (SAXException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ParserConfigurationException pce) {
        throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 52 with SAXParseException

use of org.xml.sax.SAXParseException in project jdk8u_jdk by JetBrains.

the class XMLKit method readFrom.

public static Element readFrom(Reader in, boolean tokenizing, boolean makeFrozen) throws IOException {
    Element sink = new Element();
    ContentHandler b = makeBuilder(sink.asList(), tokenizing, makeFrozen);
    XMLReader parser;
    try {
        parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    } catch (SAXException ee) {
        throw new Error(ee);
    }
    //parser.setFastStandalone(true);
    parser.setContentHandler(b);
    try {
        parser.setProperty("http://xml.org/sax/properties/lexical-handler", (LexicalHandler) b);
    } catch (SAXException ee) {
    // Ignore.  We will miss the comments and whitespace.
    }
    try {
        parser.parse(new InputSource(in));
    } catch (SAXParseException ee) {
        throw new RuntimeException("line " + ee.getLineNumber() + " col " + ee.getColumnNumber() + ": ", ee);
    } catch (SAXException ee) {
        throw new RuntimeException(ee);
    }
    switch(sink.size()) {
        case 0:
            return null;
        case 1:
            if (sink.get(0) instanceof Element) {
                return (Element) sink.get(0);
            }
        // fall through
        default:
            if (makeFrozen) {
                sink.shallowFreeze();
            }
            return sink;
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXParseException(org.xml.sax.SAXParseException) ContentHandler(org.xml.sax.ContentHandler) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 53 with SAXParseException

use of org.xml.sax.SAXParseException in project midpoint by Evolveum.

the class SchemaRegistryImpl method initialize.

/**
	 * This can be used to read additional schemas even after the registry was initialized.
	 */
@Override
public void initialize() throws SAXException, IOException, SchemaException {
    if (prismContext == null) {
        throw new IllegalStateException("Prism context not set");
    }
    if (namespacePrefixMapper == null) {
        throw new IllegalStateException("Namespace prefix mapper not set");
    }
    try {
        // TODO remove (all of these)
        LOGGER.trace("initialize() starting");
        initResolver();
        LOGGER.trace("initResolver() done");
        parsePrismSchemas();
        LOGGER.trace("parsePrismSchemas() done");
        parseJavaxSchema();
        LOGGER.trace("parseJavaxSchema() done");
        compileCompileTimeClassList();
        LOGGER.trace("compileCompileTimeClassList() done");
        initialized = true;
    } catch (SAXException ex) {
        if (ex instanceof SAXParseException) {
            SAXParseException sex = (SAXParseException) ex;
            throw new SchemaException("Error parsing schema " + sex.getSystemId() + " line " + sex.getLineNumber() + ": " + sex.getMessage(), sex);
        }
        throw ex;
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 54 with SAXParseException

use of org.xml.sax.SAXParseException in project jackrabbit by apache.

the class WorkspaceConfigTest method parseXML.

private static Element parseXML(InputSource xml, boolean validate) throws ConfigurationException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validate);
        DocumentBuilder builder = factory.newDocumentBuilder();
        if (validate) {
            builder.setErrorHandler(new ConfigurationErrorHandler());
        }
        builder.setEntityResolver(ConfigurationEntityResolver.INSTANCE);
        Document document = builder.parse(xml);
        return document.getDocumentElement();
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException("Unable to create configuration XML parser", e);
    } catch (SAXParseException e) {
        throw new ConfigurationException("Configuration file syntax error. (Line: " + e.getLineNumber() + " Column: " + e.getColumnNumber() + ")", e);
    } catch (SAXException e) {
        throw new ConfigurationException("Configuration file syntax error. ", e);
    } catch (IOException e) {
        throw new ConfigurationException("Configuration file could not be read.", e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 55 with SAXParseException

use of org.xml.sax.SAXParseException in project lucene-solr by apache.

the class TestCoreContainer method testCoreInitFailuresOnReload.

@Test
public void testCoreInitFailuresOnReload() throws Exception {
    // reused state
    Map<String, CoreContainer.CoreLoadFailure> failures = null;
    Collection<String> cores = null;
    Exception fail = null;
    // -----
    // init the  CoreContainer with the mix of ok/bad cores
    MockCoresLocator cl = new MockCoresLocator();
    SolrResourceLoader resourceLoader = new SolrResourceLoader(createTempDir());
    System.setProperty("configsets", getFile("solr/configsets").getAbsolutePath());
    final CoreContainer cc = new CoreContainer(SolrXmlConfig.fromString(resourceLoader, CONFIGSETS_SOLR_XML), new Properties(), cl);
    cl.add(new CoreDescriptor("col_ok", resourceLoader.getInstancePath().resolve("col_ok"), cc.getContainerProperties(), cc.isZooKeeperAware(), "configSet", "minimal"));
    cl.add(new CoreDescriptor("col_bad", resourceLoader.getInstancePath().resolve("col_bad"), cc.getContainerProperties(), cc.isZooKeeperAware(), "configSet", "bad-mergepolicy"));
    cc.load();
    // check that we have the cores we expect
    cores = cc.getLoadedCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 1, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    fail = failures.get("col_bad").exception;
    assertNotNull("null failure for test core", fail);
    assertTrue("init failure doesn't mention problem: " + fail.getMessage(), 0 < fail.getMessage().indexOf("DummyMergePolicy"));
    // check that we get null accessing a non-existent core
    assertNull(cc.getCore("does_not_exist"));
    // check that we get a 500 accessing the core with an init failure
    try {
        SolrCore c = cc.getCore("col_bad");
        fail("Failed to get Exception on accessing core with init failure");
    } catch (SolrException ex) {
        assertEquals(500, ex.code());
        // double wrapped
        String cause = ex.getCause().getCause().getMessage();
        assertTrue("getCore() ex cause doesn't mention init fail: " + cause, 0 < cause.indexOf("DummyMergePolicy"));
    }
    // -----
    // "fix" the bad collection
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig-defaults.xml"), FileUtils.getFile(cc.getSolrHome(), "col_bad", "conf", "solrconfig.xml"));
    FileUtils.copyFile(getFile("solr/collection1/conf/schema-minimal.xml"), FileUtils.getFile(cc.getSolrHome(), "col_bad", "conf", "schema.xml"));
    cc.create("col_bad", ImmutableMap.of());
    // check that we have the cores we expect
    cores = cc.getLoadedCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 2, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 0, failures.size());
    // try to add a collection with a path that doesn't exist
    try {
        ignoreException(Pattern.quote("bogus_path"));
        cc.create("bogus", ImmutableMap.of("configSet", "bogus_path"));
        fail("bogus inst dir failed to trigger exception from create");
    } catch (SolrException e) {
        assertTrue("init exception doesn't mention bogus dir: " + e.getCause().getCause().getMessage(), 0 < e.getCause().getCause().getMessage().indexOf("bogus_path"));
    }
    // check that we have the cores we expect
    cores = cc.getLoadedCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 2, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    fail = failures.get("bogus").exception;
    assertNotNull("null failure for test core", fail);
    assertTrue("init failure doesn't mention problem: " + fail.getMessage(), 0 < fail.getMessage().indexOf("bogus_path"));
    // check that we get null accessing a non-existent core
    assertNull(cc.getCore("does_not_exist"));
    // check that we get a 500 accessing the core with an init failure
    try {
        SolrCore c = cc.getCore("bogus");
        fail("Failed to get Exception on accessing core with init failure");
    } catch (SolrException ex) {
        assertEquals(500, ex.code());
        // double wrapped
        String cause = ex.getCause().getMessage();
        assertTrue("getCore() ex cause doesn't mention init fail: " + cause, 0 < cause.indexOf("bogus_path"));
    }
    // -----
    // break col_bad's config and try to RELOAD to add failure
    final long col_bad_old_start = getCoreStartTime(cc, "col_bad");
    FileUtils.write(FileUtils.getFile(cc.getSolrHome(), "col_bad", "conf", "solrconfig.xml"), "This is giberish, not valid XML <", IOUtils.UTF_8);
    try {
        ignoreException(Pattern.quote("SAX"));
        cc.reload("col_bad");
        fail("corrupt solrconfig.xml failed to trigger exception from reload");
    } catch (SolrException e) {
        Throwable rootException = getWrappedException(e);
        assertTrue("We're supposed to have a wrapped SAXParserException here, but we don't", rootException instanceof SAXParseException);
        SAXParseException se = (SAXParseException) rootException;
        assertTrue("reload exception doesn't refer to slrconfig.xml " + se.getSystemId(), 0 < se.getSystemId().indexOf("solrconfig.xml"));
    }
    assertEquals("Failed core reload should not have changed start time", col_bad_old_start, getCoreStartTime(cc, "col_bad"));
    // check that we have the cores we expect
    cores = cc.getLoadedCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 2, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 2, failures.size());
    Throwable ex = getWrappedException(failures.get("col_bad").exception);
    assertNotNull("null failure for test core", ex);
    assertTrue("init failure isn't SAXParseException", ex instanceof SAXParseException);
    SAXParseException saxEx = (SAXParseException) ex;
    assertTrue("init failure doesn't mention problem: " + saxEx.toString(), saxEx.getSystemId().contains("solrconfig.xml"));
    // ----
    // fix col_bad's config (again) and RELOAD to fix failure
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig-defaults.xml"), FileUtils.getFile(cc.getSolrHome(), "col_bad", "conf", "solrconfig.xml"));
    cc.reload("col_bad");
    assertTrue("Core reload should have changed start time", col_bad_old_start < getCoreStartTime(cc, "col_bad"));
    // check that we have the cores we expect
    cores = cc.getLoadedCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 2, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    cc.shutdown();
}
Also used : JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Properties(java.util.Properties) SolrException(org.apache.solr.common.SolrException) SAXParseException(org.xml.sax.SAXParseException) SAXParseException(org.xml.sax.SAXParseException) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)206 SAXException (org.xml.sax.SAXException)109 IOException (java.io.IOException)75 DocumentBuilder (javax.xml.parsers.DocumentBuilder)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)47 Document (org.w3c.dom.Document)47 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 InputSource (org.xml.sax.InputSource)39 ErrorHandler (org.xml.sax.ErrorHandler)34 Test (org.junit.Test)28 Element (org.w3c.dom.Element)24 InputStream (java.io.InputStream)20 ArrayList (java.util.ArrayList)18 NodeList (org.w3c.dom.NodeList)18 FileInputStream (java.io.FileInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 File (java.io.File)16 Node (org.w3c.dom.Node)13 StringReader (java.io.StringReader)12 URL (java.net.URL)11