Example usage for org.xml.sax.helpers XMLReaderFactory createXMLReader

List of usage examples for org.xml.sax.helpers XMLReaderFactory createXMLReader

Introduction

In this page you can find the example usage for org.xml.sax.helpers XMLReaderFactory createXMLReader.

Prototype

public static XMLReader createXMLReader() throws SAXException 

Source Link

Document

Obtains a new instance of a org.xml.sax.XMLReader .

Usage

From source file:cn.ctyun.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.java

/**
 * Constructs the XML SAX parser.//from   w w w.  ja va 2  s.  com
 *
 * @throws AmazonClientException
 */
public XmlResponsesSaxParser() throws AmazonClientException {
    // Ensure we can load the XML Reader.
    try {
        xr = XMLReaderFactory.createXMLReader();
    } catch (SAXException e) {
        throw new AmazonClientException("Couldn't initialize a SAX driver to create an XMLReader", e);
    }
}

From source file:ddf.security.pdp.realm.xacml.processor.BalanaClient.java

/**
 * Adds namespaces and namespace prefixes to the XACML response returned by the Balana PDP. The
 * Balana PDP returns a response with no namespaces, so we need to add them to unmarshal the
 * response./*from   www . j a v  a  2s.  c o m*/
 *
 * @param xacmlResponse The XACML response as a string.
 * @return DOM representation of the XACML response with namespaces and namespace prefixes.
 * @throws PdpException
 */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;

    try {
        xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    DOMResult domResult;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(BalanaClient.class.getClassLoader());
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        domResult = new DOMResult();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))),
                domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    return domResult;
}

From source file:ddf.security.pdp.realm.xacml.processor.XacmlClient.java

/**
 * Adds namespaces and namespace prefixes to the XACML response returned by the XACML PDP. The
 * XACML PDP returns a response with no namespaces, so we need to add them to unmarshal the
 * response.//from   w w  w .  j  ava  2 s . c  om
 *
 * @param xacmlResponse The XACML response as a string.
 * @return DOM representation of the XACML response with namespaces and namespace prefixes.
 * @throws PdpException
 */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;

    try {
        xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    DOMResult domResult;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(XacmlClient.class.getClassLoader());
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        domResult = new DOMResult();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))),
                domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    return domResult;
}

From source file:com.cloud.api.ApiServletTest.java

@SuppressWarnings("unchecked")
@Test/*  www.j a v  a 2s .  c  o m*/
public void getLoginSuccessResponseXml() throws JsonParseException, IOException, SAXException {
    Mockito.when(session.getAttributeNames())
            .thenReturn(new IteratorEnumeration(Arrays.asList("foo", "bar", "userid", "domainid").iterator()));
    Mockito.when(session.getAttribute(Mockito.anyString())).thenReturn("TEST");
    String loginResponse = servlet.getLoginSuccessResponse(session, "xml");
    XMLReaderFactory.createXMLReader().parse(new InputSource(new StringReader(loginResponse)));
    ;
}

From source file:com.thruzero.common.core.infonode.builder.SaxInfoNodeBuilder.java

/** construct complete {@code InfoNodeElement} from dom. */
protected InfoNodeElement doBuildInfoNode(final String xml, final InfoNodeElement targetNode,
        final InfoNodeFilterChain infoNodeFilterChain) throws Exception {
    handlePrimaryKey(targetNode);/*from   w  w  w.ja  v  a  2  s  . c o m*/
    if (StringUtils.isNotEmpty(xml)) {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        InfoNodeSaxHandler dnHandler = new InfoNodeSaxHandler(targetNode, infoNodeFilterChain); // state for this build is kept in this InfoNode Handler instance

        parser.setContentHandler(dnHandler);
        parser.setErrorHandler(dnHandler);
        parser.setFeature("http://xml.org/sax/features/validation", false);

        InputSource input = new InputSource(new StringReader(xml));
        parser.parse(input);
    }
    handleRootNode(targetNode);

    return targetNode;
}

From source file:net.sourceforge.vulcan.metrics.XmlMetricsPlugin.java

private void loadTransformers() throws IOException {
    LOG.info("Reloading XSL transformers because they have changed.");

    transformers.clear();/*from   w w w  . j  a va2  s  .c  om*/
    for (Resource r : resourceResolver.getResources(transformSourcePath)) {
        try {
            final long lastModified = r.getFile().lastModified();
            if (lastModified > newestTransformDate) {
                newestTransformDate = lastModified;
            }

            final SAXSource source = new SAXSource(XMLReaderFactory.createXMLReader(),
                    new InputSource(r.getInputStream()));

            transformers.put(r.getFilename(), transformerFactory.newTransformer(source));
        } catch (Exception e) {
            eventHandler.reportEvent(
                    new ErrorEvent(this, "metrics.errors.load.transform", new Object[] { e.getMessage() }, e));
        }
    }
}

From source file:com.daphne.es.showcase.excel.service.ExcelDataService.java

@Async
public void importExcel2007(final User user, final InputStream is) {

    ExcelDataService proxy = ((ExcelDataService) AopContext.currentProxy());

    BufferedInputStream bis = null;
    try {//from   w  w w. ja  v  a 2 s . c om
        long beginTime = System.currentTimeMillis();

        List<ExcelData> dataList = Lists.newArrayList();

        bis = new BufferedInputStream(is);
        OPCPackage pkg = OPCPackage.open(bis);
        XSSFReader r = new XSSFReader(pkg);

        XMLReader parser = XMLReaderFactory.createXMLReader();
        ContentHandler handler = new Excel2007ImportSheetHandler(proxy, dataList, batchSize);
        parser.setContentHandler(handler);

        Iterator<InputStream> sheets = r.getSheetsData();
        while (sheets.hasNext()) {
            InputStream sheet = null;
            try {
                sheet = sheets.next();
                InputSource sheetSource = new InputSource(sheet);
                parser.parse(sheetSource);
            } catch (Exception e) {
                throw e;
            } finally {
                IOUtils.closeQuietly(sheet);
            }
        }

        //??batchSize?
        if (dataList.size() > 0) {
            proxy.doBatchSave(dataList);
        }

        long endTime = System.currentTimeMillis();
        Map<String, Object> context = Maps.newHashMap();
        context.put("seconds", (endTime - beginTime) / 1000);
        notificationApi.notify(user.getId(), "excelImportSuccess", context);
    } catch (Exception e) {
        log.error("excel import error", e);
        Map<String, Object> context = Maps.newHashMap();
        context.put("error", e.getMessage());
        notificationApi.notify(user.getId(), "excelImportError", context);
    } finally {
        IOUtils.closeQuietly(bis);
    }
}

From source file:ddf.security.pdp.xacml.processor.BalanaPdp.java

/**
 * Adds namespaces and namespace prefixes to the XACML response returned by the Balana PDP. The
 * Balana PDP returns a response with no namespaces, so we need to add them to unmarshal the
 * response./*from w  w w.j  a v  a 2  s  .  co  m*/
 * 
 * @param xacmlResponse
 *            The XACML response as a string.
 * @return DOM representation of the XACML response with namespaces and namespace prefixes.
 * @throws PdpException
 */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;

    try {
        xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    DOMResult domResult = new DOMResult();

    try {
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))),
                domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    return domResult;
}

From source file:de.mpg.mpdl.inge.citationmanager.utils.XsltHelper.java

/**
 * Reads all CONE-entries with a citation-style field filled in. Generates a Map with citation
 * styles and idValue-Type-Pairs.// ww w.  j  av  a  2  s  .c  o m
 * 
 * @throws Exception
 */
public static void getJournalsXML() throws Exception {
    HttpClient client = new HttpClient();

    String coneQuery =
            // JUS-Testserver CoNE
            // "http://193.174.132.114/cone/journals/query?format=rdf&escidoc:citation-style=*&m=full&n=0";
            PropertyReader.getProperty("escidoc.cone.service.url")
                    + "journals/query?format=rdf&escidoc:citation-style=*&m=full&n=0";
    logger.info("cone query:" + coneQuery);
    GetMethod getMethod = new GetMethod(coneQuery);

    ProxyHelper.executeMethod(client, getMethod);

    XMLReader xr;

    xr = XMLReaderFactory.createXMLReader();
    JusXmlHandler handler = new JusXmlHandler();
    xr.setContentHandler(handler);
    xr.setErrorHandler(handler);

    Reader r = new InputStreamReader(getMethod.getResponseBodyAsStream());
    xr.parse(new InputSource(r));

    citationMap = handler.getCitationStyleMap();

}

From source file:com.puppycrawl.tools.checkstyle.XmlTreeWalker.java

/**
 * Static helper method to parses a Java source file.
 *
 * @param source//  w ww  .ja  v  a2 s  .  com
 *                contains the contents of the file
 * @throws TokenStreamException
 *                 if lexing failed
 * @throws RecognitionException
 *                 if parsing failed
 * @return the root of the AST
 */
public static DetailAST parse(InputSource source, File file)
        throws IOException, XMLStreamException, SAXException {

    XMLReader reader = XMLReaderFactory.createXMLReader();
    XmlContentHandler contentHandler = new XmlContentHandler(file);
    reader.setContentHandler(contentHandler);

    reader.parse(source);

    return contentHandler.getAST();
}