Example usage for org.xml.sax XMLReader parse

List of usage examples for org.xml.sax XMLReader parse

Introduction

In this page you can find the example usage for org.xml.sax XMLReader parse.

Prototype

public void parse(String systemId) throws IOException, SAXException;

Source Link

Document

Parse an XML document from a system identifier (URI).

Usage

From source file:jproxy.DefaultSamplerCreator.java

/**
 * Tries parsing to see if content is xml
 * @param postData String//from   w  w  w  . j av a 2s .c  o  m
 * @return boolean
 */
private static boolean isPotentialXml(String postData) {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        ErrorDetectionHandler detectionHandler = new ErrorDetectionHandler();
        xmlReader.setContentHandler(detectionHandler);
        xmlReader.setErrorHandler(detectionHandler);
        xmlReader.parse(new InputSource(new StringReader(postData)));
        return !detectionHandler.isErrorDetected();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

public static boolean validateWithDTDUsingSAX(String xml) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);/*from w ww .j a v a2  s.c  o m*/
    factory.setNamespaceAware(true);

    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    reader.parse(new InputSource(xml));
    return true;
}

From source file:io.milton.http.LockInfoSaxHandler.java

public static LockInfo parseLockInfo(Request request) throws IOException, FileNotFoundException, SAXException {
    InputStream in = request.getInputStream();

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

    LockInfoSaxHandler handler = new LockInfoSaxHandler();
    reader.setContentHandler(handler);// ww  w .  ja v a  2 s .  co  m
    if (log.isDebugEnabled()) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(in, bout);
        byte[] bytes = bout.toByteArray();
        in = new ByteArrayInputStream(bytes);
        log.debug("LockInfo: " + bout.toString());
    }
    reader.parse(new InputSource(in));
    LockInfo info = handler.getInfo();
    info.depth = LockDepth.INFINITY; // todo
    if (info.lockedByUser == null) {
        if (request.getAuthorization() != null) {
            if (request.getAuthorization().getUser() != null) {
                info.lockedByUser = request.getAuthorization().getUser();
            } else {
                Object user = request.getAuthorization().getTag();
                if (user instanceof DiscretePrincipal) {
                    DiscretePrincipal dp = (DiscretePrincipal) user;
                    info.lockedByUser = dp.getPrincipalURL();
                }
            }

        }
    }
    if (info.lockedByUser == null) {
        log.warn("resource is being locked with a null user. This won't really be locked at all...");
    }
    return info;
}

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.//from w ww.ja v  a2 s .co 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:de.mpg.escidoc.services.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. 
 * @throws Exception//from  w  ww. j a  v a  2  s.  c o  m
 */
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.binomed.showtime.android.util.CineShowtimeRequestManage.java

private static String getAppEngineUrl(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String appEngineUrl = prefs.getString(CineShowtimeCst.PREF_KEY_APP_ENGINE, null);
    if (appEngineUrl == null) {
        try {/*ww w.j a  va  2s.c o m*/
            URLBuilder andShowtimeUriBuilder = new URLBuilder(
                    CineShowTimeEncodingUtil.convertLocaleToEncoding());
            andShowtimeUriBuilder.setProtocol(HttpParamsCst.BINOMED_APP_PROTOCOL);
            andShowtimeUriBuilder.setAdress(HttpParamsCst.BINOMED_APP_URL);
            andShowtimeUriBuilder.completePath(HttpParamsCst.BINOMED_APP_PATH);
            andShowtimeUriBuilder.completePath(HttpParamsCst.SEVER_GET_METHODE);
            String uri = andShowtimeUriBuilder.toUri();
            Log.i(TAG, "send request : " + uri); //$NON-NLS-1$
            HttpGet getMethod = CineShowtimeFactory.getHttpGet();
            getMethod.setURI(new URI(uri));
            HttpResponse res = CineShowtimeFactory.getHttpClient().execute(getMethod);
            XMLReader reader = CineShowtimeFactory.getXmlReader();
            ParserSimpleResultXml parser = CineShowtimeFactory.getParserSimpleResultXml();
            reader.setContentHandler(parser);
            InputSource inputSource = CineShowtimeFactory.getInputSource();
            inputSource.setByteStream(res.getEntity().getContent());

            reader.parse(inputSource);

            appEngineUrl = parser.getUrl();
            if ((appEngineUrl == null) || (appEngineUrl.length() == 0)) {
                appEngineUrl = HttpParamsCst.BINOMED_APP_URL;
            }

            try {
                Editor editor = prefs.edit();
                editor.putString(CineShowtimeCst.PREF_KEY_APP_ENGINE, appEngineUrl);
                editor.commit();
            } catch (Exception e) {
            }
        } catch (Exception e) {
            appEngineUrl = HttpParamsCst.BINOMED_APP_URL;
        }
        Log.i(TAG, "result : " + appEngineUrl); //$NON-NLS-1$
    }

    return appEngineUrl;

}

From source file:com.expertiseandroid.lib.sociallib.utils.Utils.java

/**
 * Parses an XML response/*from   w  w  w. j  a v  a 2  s  . c  o  m*/
 * @param response the response to parse
 * @param rules the parsing rules to be used
 * @return the list of objects built
 * @throws SAXException
 * @throws ParserConfigurationException
 * @throws IOException
 */
public static Object parseXML(ReadableResponse response, ParsingRules rules)
        throws SAXException, ParserConfigurationException, IOException {
    XMLReader reader = createXMLReader(new DynamicXmlParser(rules));
    String xml = response.getContents();
    InputSource is = new InputSource(new StringReader(xml));
    reader.parse(is);
    return rules.getContents();
}

From source file:Examples.java

/**
 * Show the Transformer as a SAX2 XMLReader.  An XMLFilter obtained 
 * from newXMLFilter should act as a transforming XMLReader if setParent is not
 * called.  Internally, an XMLReader is created as the parent for the XMLFilter.
 *//*  www .  j  av a 2s.  c o m*/
public static void exampleXMLReader(String sourceID, String xslID)
        throws TransformerException, TransformerConfigurationException, SAXException, IOException // , ParserConfigurationException
{
    TransformerFactory tfactory = TransformerFactory.newInstance();
    if (tfactory.getFeature(SAXSource.FEATURE)) {
        XMLReader reader = ((SAXTransformerFactory) tfactory).newXMLFilter(new StreamSource(xslID));

        reader.setContentHandler(new ExampleContentHandler());

        reader.parse(new InputSource(sourceID));
    } else
        System.out.println("tfactory does not support SAX features!");
}

From source file:com.binomed.showtime.android.util.CineShowtimeRequestManage.java

public static void completeMovieDetail(Context context, MovieBean movie, String near) throws Exception {
    URLBuilder andShowtimeUriBuilder = new URLBuilder(CineShowTimeEncodingUtil.convertLocaleToEncoding());
    andShowtimeUriBuilder.setProtocol(HttpParamsCst.BINOMED_APP_PROTOCOL);
    andShowtimeUriBuilder.setAdress(getAppEngineUrl(context));
    andShowtimeUriBuilder.completePath(HttpParamsCst.IMDB_GET_METHODE);
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_LANG, Locale.getDefault().getLanguage());
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_OUTPUT, HttpParamsCst.VALUE_XML);
    // andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_ZIP, HttpParamsCst.VALUE_TRUE);
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_IE, CineShowTimeEncodingUtil.getEncoding());
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_OE, CineShowTimeEncodingUtil.getEncoding());
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_MOVIE_ID, movie.getId());
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_TRAILER, String.valueOf(true));
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_MOVIE_NAME, movie.getEnglishMovieName());
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_MOVIE_CUR_LANG_NAME, movie.getMovieName());
    andShowtimeUriBuilder.addQueryParameter(HttpParamsCst.PARAM_PLACE, URLEncoder
            .encode((near != null) ? near : SpecialChars.EMPTY, CineShowTimeEncodingUtil.getEncoding()));

    String uri = andShowtimeUriBuilder.toUri();
    Log.i(TAG, "send request : " + uri); //$NON-NLS-1$
    HttpGet getMethod = CineShowtimeFactory.getHttpGet();
    getMethod.setURI(new URI(uri));
    HttpResponse res = CineShowtimeFactory.getHttpClient().execute(getMethod);

    XMLReader reader = CineShowtimeFactory.getXmlReader();
    ParserImdbResultXml parser = CineShowtimeFactory.getParserImdbResultXml();

    reader.setContentHandler(parser);//from w ww .  j a va 2s.  c  o m
    InputSource inputSource = CineShowtimeFactory.getInputSource();
    // inputSource.setByteStream(new GZIPInputStream(res.getEntity().getContent()));
    inputSource.setByteStream(res.getEntity().getContent());

    reader.parse(inputSource);

    MovieBean movieResult = parser.getMovieBean();

    movie.setImdbDesrciption(movieResult.isImdbDesrciption());
    movie.setDescription(movieResult.getDescription());
    movie.setUrlImg(movieResult.getUrlImg());
    movie.setUrlWikipedia(movieResult.getUrlWikipedia());
    movie.setImdbId(movieResult.getImdbId());
    movie.setRate(movieResult.getRate());
    movie.setStyle(movieResult.getStyle());
    movie.setDirectorList(movieResult.getDirectorList());
    movie.setActorList(movieResult.getActorList());
    movie.setReviews(movieResult.getReviews());
    movie.setYoutubeVideos(movieResult.getYoutubeVideos());

}

From source file:de.zib.scalaris.examples.wikipedia.data.xml.Main.java

/**
 * @param handler// w  w w . j av a 2  s  . c o  m
 * @param file
 * @throws SAXException
 * @throws IOException
 */
private static void runXmlHandler(WikiDumpHandler handler, InputSource[] files)
        throws SAXException, IOException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    handler.setUp();
    ReportAtShutDown shutdownHook = handler.new ReportAtShutDown();
    Runtime.getRuntime().addShutdownHook(shutdownHook);
    reader.setContentHandler(handler);
    for (InputSource file : files) {
        reader.parse(file);
    }
    handler.tearDown();
    shutdownHook.reportAtEnd();
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    exitCheckHandler(handler);
}