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:net.sf.joost.trax.TrAXFilter.java

/**
 * Parses the <code>InputSource</code>
 * @param input A <code>InputSource</code> object.
 * @throws SAXException/*from w w w . j  a v a  2s  .  co  m*/
 * @throws IOException
 */
public void parse(InputSource input) throws SAXException, IOException {

    Transformer transformer = null;
    if (DEBUG) {
        if (log.isDebugEnabled())
            log.debug("parsing InputSource " + input.getSystemId());
    }

    try {
        // get a new Transformer
        transformer = this.templates.newTransformer();
        if (transformer instanceof TransformerImpl) {
            this.processor = ((TransformerImpl) transformer).getStxProcessor();
        } else {
            String msg = "An error is occured, because the given transformer is "
                    + "not an instance of TransformerImpl";
            if (log != null)
                log.fatal(msg);
            else
                System.err.println("Fatal error - " + msg);

        }
        XMLReader parent = this.getParent();

        if (parent == null) {
            parent = XMLReaderFactory.createXMLReader();
            setParent(parent);
        }
        ContentHandler handler = this.getContentHandler();

        if (handler == null) {
            handler = parent.getContentHandler();
        }
        if (handler == null) {
            throw new SAXException("no ContentHandler registered");
        }
        //init StxEmitter
        StxEmitter out = null;

        //SAX specific Implementation
        out = new SAXEmitter(handler);

        if (this.processor != null) {
            this.processor.setContentHandler(out);
            this.processor.setLexicalHandler(out);
        } else {
            throw new SAXException("Joost-Processor is not correct configured.");
        }
        if (parent == null) {
            throw new SAXException("No parent for filter");
        }
        parent.setContentHandler(this.processor);
        parent.setProperty("http://xml.org/sax/properties/lexical-handler", this.processor);
        parent.setEntityResolver(this);
        parent.setDTDHandler(this);
        parent.setErrorHandler(this);
        parent.parse(input);

    } catch (TransformerConfigurationException tE) {
        try {
            configErrListener.fatalError(tE);
        } catch (TransformerConfigurationException innerE) {
            throw new SAXException(innerE.getMessage(), innerE);
        }
    } catch (SAXException sE) {
        try {
            configErrListener.fatalError(new TransformerConfigurationException(sE.getMessage(), sE));
        } catch (TransformerConfigurationException innerE) {
            throw new SAXException(innerE.getMessage(), innerE);
        }
    } catch (IOException iE) {
        try {
            configErrListener.fatalError(new TransformerConfigurationException(iE.getMessage(), iE));
        } catch (TransformerConfigurationException innerE) {
            throw new IOException(innerE.getMessage());
        }
    }
}

From source file:com.flicklib.service.movie.netflix.NetflixInfoFetcher.java

@Override
public List<? extends MovieSearchResult> search(String title) throws IOException {
    List<? extends MovieSearchResult> result = null;
    OAuthClient oAuthClient = new OAuthHttpClient(new NotPoolingHttpClientPool());
    Map<String, String> params = new HashMap<String, String>();
    params.put("term", title);
    params.put("max_results", String.valueOf(20));
    try {//w  w  w  . j  av  a2  s . c o m
        OAuthMessage message = oAuthClient.invoke(accessor, "GET", "http://api.netflix.com/catalog/titles",
                params.entrySet());
        // LOGGER.info(message.getBodyAsString());

        SaxResultUnmarshaller saxUms = new SaxResultUnmarshaller();

        XMLReader rdr = XMLReaderFactory.createXMLReader();
        rdr.setContentHandler(saxUms);
        rdr.parse(new InputSource(message.getBodyAsStream()));
        result = saxUms.getResult();
    } catch (OAuthException e) {
        throw new IOException("Problem while requesting data from netflix", e);
    } catch (URISyntaxException e) {
        throw new IOException("Problem while requesting data from netflix", e);
    } catch (SAXException e) {
        throw new IOException("Problem while requesting data from netflix", e);
    }
    return result;
}

From source file:be.fedict.eidviewer.lib.file.imports.EidQuickKeyXMLFile.java

public void load(File file) throws CertificateException, FileNotFoundException, SAXException, IOException {
    logger.fine("Loading eID Quick Keys XML File");

    XMLReader reader = null;//from  ww  w. j  av  a2s.  c  om

    certificateFactory = CertificateFactory.getInstance("X.509");
    FileInputStream fis = new FileInputStream(file);

    reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(this);
    reader.setErrorHandler(this);
    BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    reader.parse(new InputSource(in));

    X509Utilities.setCertificateChainsFromCertificates(eidData, rootCert, citizenCert, authenticationCert,
            signingCert, rrnCert);
}

From source file:com.myjeeva.poi.ExcelXSSFRowCallbackHandler.java

/**
 * Parses the file, passing each row to the given callback.
 * At the end closes the opc package and underling input stream.
 *//*www. ja v a2 s  .c o  m*/
public void parse() throws Exception {

    XSSFReader reader = new XSSFReader(this.opcPackage);

    StylesTable styles = reader.getStylesTable();
    ReadOnlySharedStringsTable sharedStrings = new ReadOnlySharedStringsTable(this.opcPackage);

    ContentHandler handler = new XSSFSheetXMLHandler(styles, sharedStrings,
            new ExcelXSSFRowCallbackSheetContentsHandler(this.rowCallback), true);

    XMLReader parser = XMLReaderFactory.createXMLReader();
    parser.setContentHandler(handler);

    InputStream sheetInputStream = reader.getSheetsData().next();

    try {

        parser.parse(new InputSource(sheetInputStream));

    } finally {

        IOUtils.closeQuietly(sheetInputStream);
        this.opcPackage.close();

    }

}

From source file:com.bdaum.juploadr.uploadapi.smugrest.SmugmugMethod.java

/**
 * @param responseBodyAsString/*from  w ww  .  jav  a  2  s  .  c  o m*/
 * @return
 * @throws AuthException
 */
public boolean parseResponse(String response) throws ProtocolException {
    try {
        // System.setProperty("org.xml.sax.driver",
        // "com.bluecast.xml.Piccolo");
        // log.info(response);
        XMLReader reader = XMLReaderFactory.createXMLReader();
        handler = getResponseHandler();
        reader.setContentHandler(handler);
        reader.parse(new InputSource(new StringReader(response)));
        if (!handler.isSuccessful()) {
            throw new ProtocolException(handler.getFailureReason());
        }
        return handler.isSuccessful();
    } catch (SAXException e) {
        throw new AuthException(Messages.getString("juploadr.ui.error.response.unreadable.noreason"), e); //$NON-NLS-1$
    } catch (IOException e) {
        // this can't happen
    }
    return false;
}

From source file:mx.itesm.mexadl.metrics.util.Util.java

/**
 * Transform a logging report from XML to HTML format.
 * //from w ww.  j  a  v a 2s.c  o m
 * @param xmlFile
 * @param xsltFile
 * @param outputFile
 * @param componentTypes
 * @throws TransformerException
 * @throws SAXException
 * @throws IOException
 */
public static void transformXMLReport2Html(final File xmlFile, final InputStream xsltFile,
        final File outputFile, final Map<String, String> componentTypes)
        throws TransformerException, SAXException, IOException {
    Source xmlSource;
    Source xsltSource;
    Result outputTarget;
    XMLReader xmlReader;
    String reportContents;
    Transformer transformer;

    // Blank reader
    xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            if (systemId.endsWith(".dtd")) {
                StringReader stringInput = new StringReader(" ");
                return new InputSource(stringInput);
            } else {
                return null; // use default behavior
            }
        }
    });

    xmlSource = new SAXSource(xmlReader, new InputSource(new FileInputStream(xmlFile)));
    xsltSource = new StreamSource(xsltFile);
    outputTarget = new StreamResult(outputFile);

    // Generate output file
    transformer = transformerFactory.newTransformer(xsltSource);
    transformer.transform(xmlSource, outputTarget);

    // Add components type information
    reportContents = FileUtils.readFileToString(outputFile, "UTF-8");
    for (String componentType : componentTypes.keySet()) {
        reportContents = reportContents.replace("[ " + componentType + " ]", componentTypes.get(componentType));
    }
    FileUtils.writeStringToFile(outputFile, reportContents);
}

From source file:org.callimachusproject.xml.XdmNodeFactory.java

private XdmNode parse(InputSource isource) throws SAXException {
    // Make sure the builder uses our entity resolver
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setEntityResolver(xmlResolver);
    SAXSource source = new SAXSource(reader, isource);
    if (isource.getSystemId() != null) {
        source.setSystemId(isource.getSystemId());
    }//  ww w.j a  va 2 s  .c o  m
    return parse(source);
}

From source file:in.gov.uidai.core.aua.client.OtpClient.java

private OtpRes parseOtpResponseXML(String xmlToParse) throws JAXBException {

    //Create an XMLReader to use with our filter
    try {//from www . j a  v  a  2s . co m
        //Prepare JAXB objects
        JAXBContext jc = JAXBContext.newInstance(OtpRes.class);
        Unmarshaller u = jc.createUnmarshaller();

        XMLReader reader;
        reader = XMLReaderFactory.createXMLReader();

        //Create the filter (to add namespace) and set the xmlReader as its parent.
        NamespaceFilter inFilter = new NamespaceFilter("http://www.uidai.gov.in/auth/otp/1.6", true);
        inFilter.setParent(reader);

        //Prepare the input, in this case a java.io.File (output)
        InputSource is = new InputSource(new StringReader(xmlToParse));

        //Create a SAXSource specifying the filter
        SAXSource source = new SAXSource(inFilter, is);

        //Do unmarshalling
        OtpRes res = u.unmarshal(source, OtpRes.class).getValue();
        return res;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Erorr while parsing response XML" + e.getMessage());
    }
}

From source file:com.notesrender.templatej.TemplateProcessor.java

public void process(String filePath) {
    try {//from ww  w .j a  v a 2  s. c  om
        XMLReader rd = XMLReaderFactory.createXMLReader();
        rd.setContentHandler(this);
        rd.parse(new InputSource(new FileInputStream(filePath)));

        String fullFilePath = new File(filePath).getAbsolutePath();
        String dir = FilenameUtils.getFullPath(fullFilePath);
        String tplFullFilePath = new File(dir, _ref).getAbsolutePath();
        String outFullFilePath = new File(dir, _out).getAbsolutePath();

        _writer = new PrintWriter(new BufferedWriter(new FileWriter(outFullFilePath)));
        rd.parse(new InputSource(new FileInputStream(tplFullFilePath)));
        _writer.close();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:in.gov.uidai.auth.aua.httpclient.OtpClient.java

private OtpRes parseOtpResponseXML(String xmlToParse) throws JAXBException {

    //Create an XMLReader to use with our filter 
    try {//from w ww .j  av a  2  s . c  o  m
        //Prepare JAXB objects 
        JAXBContext jc = JAXBContext.newInstance(OtpRes.class);
        Unmarshaller u = jc.createUnmarshaller();

        XMLReader reader;
        reader = XMLReaderFactory.createXMLReader();

        //Create the filter (to add namespace) and set the xmlReader as its parent. 
        NamespaceFilter inFilter = new NamespaceFilter("http://www.uidai.gov.in/authentication/otp/1.0", true);
        inFilter.setParent(reader);

        //Prepare the input, in this case a java.io.File (output) 
        InputSource is = new InputSource(new StringReader(xmlToParse));

        //Create a SAXSource specifying the filter 
        SAXSource source = new SAXSource(inFilter, is);

        //Do unmarshalling 
        OtpRes res = u.unmarshal(source, OtpRes.class).getValue();
        return res;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Erorr while parsing response XML" + e.getMessage());
    }
}