Example usage for javax.xml.parsers SAXParserFactory newInstance

List of usage examples for javax.xml.parsers SAXParserFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory newInstance.

Prototype


public static SAXParserFactory newInstance() 

Source Link

Document

Obtain a new instance of a SAXParserFactory .

Usage

From source file:com.aurel.track.exchange.docx.importer.HTMLParser.java

private void parse(ByteArrayOutputStream byteArrayOutputStream, Locale locale) {
    localizedHeading = "berschrift";//LocalizeUtil.getLocalizedTextFromApplicationResources(HEADING_KEY, locale);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    Reader fileReader = null;//ww w . j a  va 2s  .c  o  m
    try {
        fileReader = new FileReader(new File("d:/docx/PN164_SC_Track.docx.html"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    try {
        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();
        //InputSource is=new InputSource(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
        InputSource is = new InputSource(fileReader);
        sp.parse(is, this);
    } catch (SAXException se) {
        LOGGER.error("Parsing failed with " + se.getMessage());
        if (LOGGER.isDebugEnabled()) {
            LOGGER.error(ExceptionUtils.getStackTrace(se));
        }
    } catch (ParserConfigurationException pce) {
        LOGGER.error("Parsing failed with " + pce.getMessage());
        if (LOGGER.isDebugEnabled()) {
            LOGGER.error(ExceptionUtils.getStackTrace(pce));
        }
    } catch (IOException ie) {
        LOGGER.error("Reading failed with " + ie.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(ie));
    }
}

From source file:es.mityc.firmaJava.libreria.utilidades.AnalizadorFicheroFirma.java

public void analizar(File fichero) {
    SAXParserFactory factoria = SAXParserFactory.newInstance();
    factoria.setNamespaceAware(true);//from  ww  w.j ava2  s . c  o m
    factoria.setValidating(false);
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(fichero);
        SAXParser parser = factoria.newSAXParser();
        parser.parse(fis, this);
    } catch (ParserConfigurationException e) {
        log.error(e);
    } catch (SAXException e) {
        log.error(e);
    } catch (FileNotFoundException e) {
        log.error(e);
    } catch (IOException e) {
        log.error(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java

@Override
public void OutputJaxpImplementationInfo() {
    logger.debug(getJaxpImplementationInfo("DocumentBuilderFactory",
            DocumentBuilderFactory.newInstance().getClass()));
    logger.debug(getJaxpImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));
    logger.debug(getJaxpImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));
    logger.debug(getJaxpImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));

}

From source file:com.shin1ogawa.appengine.marketplace.gdata.LicensingAPI.java

static List<Map<String, String>> parseLicenseFeed(InputStream is)
        throws SAXException, IOException, ParserConfigurationException {
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final List<Map<String, String>> list = Lists.newArrayList();
    parser.parse(is, new DefaultHandler() {

        boolean entity = false;

        String currentElement;/* www.j av a 2s. co m*/

        Map<String, String> map;

        @Override
        public void characters(char[] ch, int start, int length) {
            if (entity) {
                map.put(currentElement, new String(ch, start, length));
            }
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            if (entity == false && StringUtils.equals(qName, "entity")) {
                entity = true;
                map = Maps.newHashMap();
                list.add(map);
                return;
            }
            currentElement = qName;
        }

        @Override
        public void endElement(String uri, String localName, String qName) {
            if (entity && StringUtils.equals(qName, "entity")) {
                entity = false;
            }
        }
    });
    return list;
}

From source file:net.itransformers.topologyviewer.gui.MyGraphMLReader.java

/**
 * Creates a <code>GraphMLReader</code> instance with the specified
 * vertex and edge factories.//w  w w  . ja  va2s .c  o  m
 *
 * @param vertex_factory the vertex factory to use to create vertex objects
 * @param edge_factory the edge factory to use to create edge objects
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public MyGraphMLReader(Factory<V> vertex_factory, Factory<E> edge_factory)
        throws ParserConfigurationException, SAXException {
    current_vertex = null;
    current_edge = null;

    SAXParserFactory factory = SAXParserFactory.newInstance();
    saxp = factory.newSAXParser();

    current_states = new LinkedList<TagState>();

    tag_state = new DualHashBidiMap<String, TagState>();
    tag_state.put("node", TagState.VERTEX);
    tag_state.put("edge", TagState.EDGE);
    tag_state.put("hyperedge", TagState.HYPEREDGE);
    tag_state.put("endpoint", TagState.ENDPOINT);
    tag_state.put("graph", TagState.GRAPH);
    tag_state.put("data", TagState.DATA);
    tag_state.put("key", TagState.KEY);
    tag_state.put("desc", TagState.DESC);
    tag_state.put("default", TagState.DEFAULT_KEY);
    tag_state.put("graphml", TagState.GRAPHML);

    this.key_type = KeyType.NONE;

    this.vertex_factory = vertex_factory;
    this.edge_factory = edge_factory;
}

From source file:com.google.enterprise.connector.sharepoint.wsclient.mock.XmlClientFactory.java

public XmlClientFactory(String xmlFilePath) {
    XmlHandler handler = new XmlHandler();
    try {//  w  w w  .j  av a 2 s.  c o  m
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(xmlFilePath, handler);
    } catch (Exception e) {
        handler = null;
        LOGGER.log(Level.WARNING, "Unable to load mock xml.", e);
    }

    if (null != handler) {
        root = handler.getRoot();
    } else {
        root = null;
    }
}

From source file:com.zegoggles.smssync.XOAuthConsumer.java

protected String getUsernameFromContacts() {

    final HttpClient httpClient = new DefaultHttpClient();
    final String url = "https://www.google.com/m8/feeds/contacts/default/thin?max-results=1";
    final StringBuilder email = new StringBuilder();

    try {/*  w  w  w.  j  a v  a 2 s.c o  m*/
        HttpGet get = new HttpGet(sign(url));
        HttpResponse resp = httpClient.execute(get);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(new DefaultHandler() {
            boolean inEmail;

            @Override
            public void startElement(String uri, String localName, String qName, Attributes atts) {
                inEmail = "email".equals(localName);
            }

            @Override
            public void characters(char[] c, int start, int length) {
                if (inEmail) {
                    email.append(c, start, length);
                }
            }
        });
        xr.parse(new InputSource(resp.getEntity().getContent()));
        return email.toString();

    } catch (oauth.signpost.exception.OAuthException e) {
        Log.e(TAG, "error", e);
        return null;
    } catch (org.xml.sax.SAXException e) {
        Log.e(TAG, "error", e);
        return null;
    } catch (java.io.IOException e) {
        Log.e(TAG, "error", e);
        return null;
    } catch (javax.xml.parsers.ParserConfigurationException e) {
        Log.e(TAG, "error", e);
        return null;
    }
}

From source file:com.joshdrummond.webpasswordsafe.android.GetCurrentPassword.java

private String parseResponse(String responseSOAP) {
    String response = "";
    try {/*from  w w  w .  java2  s. com*/
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        GetCurrentPasswordHandler handler = new GetCurrentPasswordHandler();
        xr.setContentHandler(handler);
        xr.parse(new InputSource(new StringReader(responseSOAP)));
        response = handler.getParsedData();
    } catch (Exception e) {
        response = "ERROR parsing: " + Arrays.deepToString(e.getStackTrace());
    }
    return response;
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfRetriever.java

private SAXParser newSaxParser() {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

    try {/*  w  ww.  j a  v a 2s.c  om*/
        return saxParserFactory.newSAXParser();
    } catch (SAXException | ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.typhoon.newsreader.engine.ChannelRefresh.java

public List<FeedsListItem> parser(String link) {
    if (link.contains("www.24h.com.vn")) {
        try {/*from  w  ww . j a v a  2  s .c o  m*/
            URL url = new URL(link);
            URLConnection connection = url.openConnection();
            connection.addRequestProperty("http.agent", USER_AGENT);
            InputSource input = new InputSource(url.openStream());
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(false);
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();

            reader.setContentHandler(this);
            reader.parse(input);

            return getFeedsList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } else {
        try {
            // URL url= new URL(link);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(link);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(false);
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setContentHandler(this);
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(EntityUtils.toString(entity)));
            reader.parse(inStream);
            return getFeedsList();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        } catch (SAXException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}