Search in sources :

Example 26 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 27 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 28 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)

Example 29 with SAXParseException

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

the class FeatureDeploymentListener method parse.

protected Document parse(File artifact) throws Exception {
    if (dbf == null) {
        dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
    }
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {
        }

        public void error(SAXParseException exception) throws SAXException {
        }

        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    return db.parse(artifact);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 30 with SAXParseException

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

the class JspDocumentParser method parseStandardAction.

//*********************************************************************
// Private utility methods
private Node parseStandardAction(String qName, String localName, Attributes nonTaglibAttrs, Attributes nonTaglibXmlnsAttrs, Attributes taglibAttrs, Mark start, Node parent) throws SAXException {
    Node node = null;
    if (localName.equals(ROOT_ACTION)) {
        if (!(current instanceof Node.Root)) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.nested_jsproot"), locator);
        }
        node = new Node.JspRoot(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        if (isTop) {
            pageInfo.setHasJspRoot(true);
        }
    } else if (localName.equals(PAGE_DIRECTIVE_ACTION)) {
        if (isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.istagfile", localName), locator);
        }
        node = new Node.PageDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        String imports = nonTaglibAttrs.getValue("import");
        // There can only be one 'import' attribute per page directive
        if (imports != null) {
            ((Node.PageDirective) node).addImport(imports);
        }
    } else if (localName.equals(INCLUDE_DIRECTIVE_ACTION)) {
        node = new Node.IncludeDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        processIncludeDirective(nonTaglibAttrs.getValue("file"), node);
    } else if (localName.equals(DECLARATION_ACTION)) {
        if (scriptlessBodyNode != null) {
            // declared to be scriptless
            throw new SAXParseException(Localizer.getMessage("jsp.error.no.scriptlets", localName), locator);
        }
        node = new Node.Declaration(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(SCRIPTLET_ACTION)) {
        if (scriptlessBodyNode != null) {
            // declared to be scriptless
            throw new SAXParseException(Localizer.getMessage("jsp.error.no.scriptlets", localName), locator);
        }
        node = new Node.Scriptlet(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(EXPRESSION_ACTION)) {
        if (scriptlessBodyNode != null) {
            // declared to be scriptless
            throw new SAXParseException(Localizer.getMessage("jsp.error.no.scriptlets", localName), locator);
        }
        node = new Node.Expression(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(USE_BEAN_ACTION)) {
        node = new Node.UseBean(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(SET_PROPERTY_ACTION)) {
        node = new Node.SetProperty(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(GET_PROPERTY_ACTION)) {
        node = new Node.GetProperty(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(INCLUDE_ACTION)) {
        node = new Node.IncludeAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(FORWARD_ACTION)) {
        node = new Node.ForwardAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(PARAM_ACTION)) {
        node = new Node.ParamAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(PARAMS_ACTION)) {
        node = new Node.ParamsAction(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(PLUGIN_ACTION)) {
        node = new Node.PlugIn(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(TEXT_ACTION)) {
        node = new Node.JspText(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(BODY_ACTION)) {
        node = new Node.JspBody(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(ATTRIBUTE_ACTION)) {
        node = new Node.NamedAttribute(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(OUTPUT_ACTION)) {
        node = new Node.JspOutput(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(TAG_DIRECTIVE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.TagDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        String imports = nonTaglibAttrs.getValue("import");
        // There can only be one 'import' attribute per tag directive
        if (imports != null) {
            ((Node.TagDirective) node).addImport(imports);
        }
    } else if (localName.equals(ATTRIBUTE_DIRECTIVE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.AttributeDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(VARIABLE_DIRECTIVE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.VariableDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(INVOKE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.InvokeAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(DOBODY_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.DoBodyAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(ELEMENT_ACTION)) {
        node = new Node.JspElement(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(FALLBACK_ACTION)) {
        node = new Node.FallBackAction(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else {
        throw new SAXParseException(Localizer.getMessage("jsp.error.xml.badStandardAction", localName), locator);
    }
    return node;
}
Also used : SAXParseException(org.xml.sax.SAXParseException)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)365 SAXException (org.xml.sax.SAXException)170 IOException (java.io.IOException)131 DocumentBuilder (javax.xml.parsers.DocumentBuilder)73 InputSource (org.xml.sax.InputSource)69 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)68 Document (org.w3c.dom.Document)64 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)56 ErrorHandler (org.xml.sax.ErrorHandler)52 InputStream (java.io.InputStream)36 File (java.io.File)34 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)31 FileNotFoundException (java.io.FileNotFoundException)31 Element (org.w3c.dom.Element)30 StringReader (java.io.StringReader)21 NodeList (org.w3c.dom.NodeList)21 URL (java.net.URL)20 Test (org.junit.jupiter.api.Test)19 Node (org.w3c.dom.Node)19