Example usage for org.xml.sax InputSource setByteStream

List of usage examples for org.xml.sax InputSource setByteStream

Introduction

In this page you can find the example usage for org.xml.sax InputSource setByteStream.

Prototype

public void setByteStream(InputStream byteStream) 

Source Link

Document

Set the byte stream for this input source.

Usage

From source file:org.exist.collections.Collection.java

private InputSource closeShieldInputSource(final InputSource source) {

    final InputSource protectedInputSource = new InputSource();
    protectedInputSource.setEncoding(source.getEncoding());
    protectedInputSource.setSystemId(source.getSystemId());
    protectedInputSource.setPublicId(source.getPublicId());

    if (source.getByteStream() != null) {
        //TODO consider AutoCloseInputStream
        final InputStream closeShieldByteStream = new CloseShieldInputStream(source.getByteStream());
        protectedInputSource.setByteStream(closeShieldByteStream);
    }/* w  w w.  java 2 s . co m*/

    if (source.getCharacterStream() != null) {
        //TODO consider AutoCloseReader
        final Reader closeShieldReader = new CloseShieldReader(source.getCharacterStream());
        protectedInputSource.setCharacterStream(closeShieldReader);
    }

    return protectedInputSource;
}

From source file:org.exist.collections.MutableCollection.java

private InputSource closeShieldInputSource(final InputSource source) {
    final InputSource protectedInputSource = new InputSource();
    protectedInputSource.setEncoding(source.getEncoding());
    protectedInputSource.setSystemId(source.getSystemId());
    protectedInputSource.setPublicId(source.getPublicId());

    if (source.getByteStream() != null) {
        //TODO consider AutoCloseInputStream
        final InputStream closeShieldByteStream = new CloseShieldInputStream(source.getByteStream());
        protectedInputSource.setByteStream(closeShieldByteStream);
    }/*from  www.j  a v  a 2 s .  c  o m*/

    if (source.getCharacterStream() != null) {
        //TODO consider AutoCloseReader
        final Reader closeShieldReader = new CloseShieldReader(source.getCharacterStream());
        protectedInputSource.setCharacterStream(closeShieldReader);
    }

    return protectedInputSource;
}

From source file:org.exolab.castor.mapping.Mapping.java

/**
 * Loads the mapping from the specified URL.
 *
 * @param url The URL of the mapping file.
 * @param type The source type./* w w  w .jav  a  2  s .  c om*/
 * @throws IOException An error occured when reading the mapping file.
 * @throws MappingException The mapping file is invalid.
 */
public void loadMapping(final URL url, final String type) throws IOException, MappingException {
    try {
        if (_resolver.getBaseURL() == null) {
            _resolver.setBaseURL(url);
        }
        InputSource source = _resolver.resolveEntity(null, url.toExternalForm());
        if (source == null) {
            source = new InputSource(url.toExternalForm());
            source.setByteStream(url.openStream());
        } else
            source.setSystemId(url.toExternalForm());
        LOG.info(Messages.format("mapping.loadingFrom", url.toExternalForm()));
        loadMapping(source, type);
    } catch (SAXException ex) {
        throw new MappingException(ex);
    }
}

From source file:org.infoscoop.web.InitializeServlet.java

/**
 * get the resource on the same context as InputSource.
 * @param config/*w ww .java  2s. com*/
 * @param path
 * @return
 * @throws ServletException
 */
public static InputSource getResource(ServletConfig config, String path) throws ServletException {
    try {
        URL url = config.getServletContext().getResource(path);
        InputStream input = config.getServletContext().getResourceAsStream(path);
        InputSource is = new InputSource(url.toExternalForm());
        is.setByteStream(input);
        is.setSystemId(config.getServletContext().getRealPath(path));
        return is;
    } catch (MalformedURLException e) {
        throw new ServletException(e);
    }
}

From source file:org.jboss.web.tomcat.tc4.SingleSignOnContextConfig.java

/**
 * Process the application configuration file, if it exists.
 *//*from  www .  j  ava2 s  . co m*/
private void applicationConfig() {

    // Open the application web.xml file, if it exists
    InputStream stream = null;
    ServletContext servletContext = context.getServletContext();
    if (servletContext != null)
        stream = servletContext.getResourceAsStream(Constants.ApplicationWebXml);
    if (stream == null) {
        log(sm.getString("contextConfig.applicationMissing"));
        return;
    }

    // Process the application web.xml file
    synchronized (webDigester) {
        try {
            URL url = servletContext.getResource(Constants.ApplicationWebXml);
            InputSource is = new InputSource(url.toExternalForm());
            is.setByteStream(stream);
            webDigester.setDebug(getDebug());
            if (context instanceof StandardContext) {
                ((StandardContext) context).setReplaceWelcomeFiles(true);
            }
            webDigester.clear();
            webDigester.push(context);
            webDigester.parse(is);
        } catch (SAXParseException e) {
            log(sm.getString("contextConfig.applicationParse"), e);
            log(sm.getString("contextConfig.applicationPosition", "" + e.getLineNumber(),
                    "" + e.getColumnNumber()));
            ok = false;
        } catch (Exception e) {
            log(sm.getString("contextConfig.applicationParse"), e);
            ok = false;
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException e) {
                log(sm.getString("contextConfig.applicationClose"), e);
            }
        }
    }

}

From source file:org.jbpm.bpel.xml.ProcessWsdlLocator.java

private void upgradeWsdlDocumentIfNeeded(InputSource source) {
    // get the thread-local document parser
    DocumentBuilder documentParser = XmlUtil.getDocumentBuilder();

    // install our problem handler as document parser's error handler
    documentParser.setErrorHandler(problemHandler.asSaxErrorHandler());

    // parse content
    Document document;//from w w  w .jav  a 2  s  .c om
    try {
        document = documentParser.parse(source);
        // halt on parse errors
        if (problemHandler.getProblemCount() > 0)
            return;
    } catch (IOException e) {
        Problem problem = new Problem(Problem.LEVEL_ERROR, "document is not readable", e);
        problem.setResource(latestImportURI);
        problemHandler.add(problem);
        return;
    } catch (SAXException e) {
        Problem problem = new Problem(Problem.LEVEL_ERROR, "document contains invalid xml", e);
        problem.setResource(latestImportURI);
        problemHandler.add(problem);
        return;
    } finally {
        // reset error handling behavior
        documentParser.setErrorHandler(null);
    }

    // check whether the wsdl document requires upgrading
    if (hasUpgradableElements(document)) {
        try {
            // create wsdl upgrader
            Transformer wsdlUpgrader = getWsdlUpgradeTemplates().newTransformer();

            // install our problem handler as transformer's error listener
            wsdlUpgrader.setErrorListener(problemHandler.asTraxErrorListener());

            // upgrade into memory stream
            ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
            wsdlUpgrader.transform(new DOMSource(document), new StreamResult(resultStream));

            // replace existing source with upgraded document
            source.setByteStream(new ByteArrayInputStream(resultStream.toByteArray()));

            log.debug("upgraded wsdl document: " + latestImportURI);
        } catch (TransformerException e) {
            Problem problem = new Problem(Problem.LEVEL_ERROR, "wsdl upgrade failed", e);
            problem.setResource(latestImportURI);
            problemHandler.add(problem);
        }
    } else {
        // if the source is a stream, reset it
        InputStream sourceStream = source.getByteStream();
        if (sourceStream != null) {
            try {
                sourceStream.reset();
            } catch (IOException e) {
                log.error("could not reset source stream: " + latestImportURI, e);
            }
        }
    }
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil.java

private static Object buildRepository(String repositoryFileName, Class targetRepository)
        throws IOException, ParserConfigurationException, SAXException {
    URL url = buildURL(repositoryFileName);

    String pathName = url.toExternalForm();

    LOG.debug("Building repository from :" + pathName);
    InputSource source = new InputSource(pathName);
    URLConnection conn = url.openConnection();
    conn.setUseCaches(false);//from  w w  w  .  ja  va 2  s.com
    conn.connect();
    InputStream i = conn.getInputStream();
    source.setByteStream(i);
    try {
        return readMetadataFromXML(source, targetRepository);
    } finally {
        try {
            i.close();
        } catch (IOException x) {
            LOG.warn("unable to close repository input stream [" + x.getMessage() + "]", x);
        }
    }
}

From source file:org.nuxeo.ecm.diff.service.impl.DocumentDiffServiceImpl.java

/**
 * Exports leftDoc and rightDoc to XML./*from   w w w . j a v  a 2 s  .  c  om*/
 *
 * @param session the session
 * @param leftDoc the left doc
 * @param rightDoc the right doc
 * @param leftDocXMLInputSource the left doc XML input source
 * @param rightDocXMLInputSource the right doc XML input source
 * @throws ClientException the client exception
 */
protected final void exportXML(CoreSession session, DocumentModel leftDoc, DocumentModel rightDoc,
        InputSource leftDocXMLInputSource, InputSource rightDocXMLInputSource) throws ClientException {

    DocumentXMLExporter docXMLExporter = getDocumentXMLExporter();

    leftDocXMLInputSource.setByteStream(docXMLExporter.exportXML(leftDoc, session));
    rightDocXMLInputSource.setByteStream(docXMLExporter.exportXML(rightDoc, session));
}

From source file:org.runnerup.export.JoggSE.java

@Override
public Status connect() {
    if (isConnected) {
        return Status.OK;
    }//  w  ww. j a  v  a  2  s.com

    Status s = Status.NEED_AUTH;
    s.authMethod = Uploader.AuthMethod.USER_PASS;
    if (username == null || password == null) {
        return s;
    }

    Exception ex = null;
    HttpURLConnection conn = null;
    try {
        /**
         * Login by making an empty save-gpx call and see what error message
         * you get Invalid/"Invalid Userdetails" => wrong user/pass
         * NOK/"Root element is missing" => OK
         */
        final String LOGIN_OK = "NOK";

        conn = (HttpURLConnection) new URL(BASE_URL).openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Host", "jogg.se");
        conn.addRequestProperty("Content-Type", "text/xml");

        final BufferedWriter wr = new BufferedWriter(new PrintWriter(conn.getOutputStream()));
        saveGPX(wr, "");
        wr.flush();
        wr.close();

        final InputStream in = new BufferedInputStream(conn.getInputStream());
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        final InputSource is = new InputSource();
        is.setByteStream(in);
        final Document doc = db.parse(is);
        conn.disconnect();
        conn = null;

        final String path[] = { "soap:Envelope", "soap:Body", "SaveGpxResponse", "SaveGpxResult",
                "ResponseStatus", "ResponseCode" };
        final Node e = navigate(doc, path);
        System.err.println("reply: " + e.getTextContent());
        if (e != null && e.getTextContent() != null && LOGIN_OK.contentEquals(e.getTextContent())) {
            isConnected = true;
            return Uploader.Status.OK;
        }

        return s;
    } catch (final MalformedURLException e) {
        ex = e;
    } catch (final IOException e) {
        ex = e;
    } catch (final ParserConfigurationException e) {
        ex = e;
    } catch (final SAXException e) {
        ex = e;
    }

    if (conn != null)
        conn.disconnect();

    s = Uploader.Status.ERROR;
    s.ex = ex;
    if (ex != null) {
        ex.printStackTrace();
    }
    return s;
}

From source file:org.runnerup.export.JoggSE.java

@Override
public Status upload(final SQLiteDatabase db, final long mID) {
    Status s;/*  w ww  .j a  v a 2s .c  o  m*/
    if ((s = connect()) != Status.OK) {
        return s;
    }

    Exception ex = null;
    HttpURLConnection conn = null;
    final GPX gpx = new GPX(db);
    try {
        final StringWriter gpxString = new StringWriter();
        gpx.export(mID, gpxString);

        conn = (HttpURLConnection) new URL(BASE_URL).openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Host", "jogg.se");
        conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8");

        final BufferedWriter wr = new BufferedWriter(new PrintWriter(conn.getOutputStream()));
        saveGPX(wr, gpxString.toString());
        wr.flush();
        wr.close();

        final InputStream in = new BufferedInputStream(conn.getInputStream());
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder dob = dbf.newDocumentBuilder();
        final InputSource is = new InputSource();
        is.setByteStream(in);
        final Document doc = dob.parse(is);
        conn.disconnect();
        conn = null;

        final String path[] = { "soap:Envelope", "soap:Body", "SaveGpxResponse", "SaveGpxResult",
                "ResponseStatus", "ResponseCode" };
        final Node e = navigate(doc, path);
        System.err.println("reply: " + e.getTextContent());
        if (e != null && e.getTextContent() != null && "OK".contentEquals(e.getTextContent())) {
            return Uploader.Status.OK;
        }
        throw new Exception(e.getTextContent());
    } catch (final MalformedURLException e) {
        ex = e;
    } catch (final IOException e) {
        ex = e;
    } catch (final ParserConfigurationException e) {
        ex = e;
    } catch (final SAXException e) {
        ex = e;
    } catch (final DOMException e) {
        ex = e;
        e.printStackTrace();
    } catch (final Exception e) {
        ex = e;
    }

    if (conn != null)
        conn.disconnect();

    s = Uploader.Status.ERROR;
    s.ex = ex;
    if (ex != null) {
        ex.printStackTrace();
    }
    return s;

}