Search in sources :

Example 6 with SAXParser

use of javax.xml.parsers.SAXParser in project Openfire by igniterealtime.

the class SimplePresence method parse.

//	public Presence convertSIPPresenceToXMPP(String sipPresence) {
//		Presence  xmppPresence = new Presence();
//		
//		SAXParser saxParser;
//		try {
//			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//			saxParser = saxParserFactory.newSAXParser();
//			
//		} catch (Exception e) {
//			Log.debug("Unable to load parser to parse SIP presence to XMPP Presence.", e);
//			return xmppPresence;
//		}
//		
//		return xmppPresence;
//	}
//	public String convertXMPPPresenceToSIP(Presence xmppPresence) {
//		String sipPresence = "";
//		String basic       = "open";
//		String rpid        = "unknown";
//		String dmNote      = "";
//		
//		if (!xmppPresence.isAvailable()) {
//			// Prepare "closed" basic presence.
//			basic = "closed";
//		} else {
//			Presence.Show xmppPresenceShow = xmppPresence.getShow();
//			if (xmppPresenceShow.equals(Presence.Show.away)) {
//				rpid = "away";
//			} else if (xmppPresenceShow.equals(Presence.Show.chat)) {
//				rpid = "away";
//			} else if (xmppPresenceShow.equals(Presence.Show.dnd)) {
//				rpid = "busy";
//			} else if (xmppPresenceShow.equals(Presence.Show.xa)) {
//				rpid = "away";
//			} else {
//				rpid = "";
//			}
//		}
//		
//		sipPresence = "<?xml version='1.0' encoding='UTF-8'?>"
//				+ "<presence xmlns='urn:ietf:params:xml:ns:pidf' xmlns:dm='urn:ietf:params:xml:ns:pidf:data-model' "
//				+ "xmlns:rpid='urn:ietf:params:xml:ns:pidf:rpid' xmlns:c='urn:ietf:params:xml:ns:pidf:cipid' "
//				+ "entity='pres:sip:sipdemo1@192.168.1.199'>"
//				+ "<tuple><status><basic>" + basic + "</basic></status></tuple>"
//				+ "<dm:person id='p3e32d940'><rpid:activities><rpid:" + rpid + "/></rpid:activities></dm:person></presence>";
//		
//		return sipPresence;
//	}
public void parse(String simplePresenceXml) throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxParserFactory.newSAXParser();
    ByteArrayInputStream bais = new ByteArrayInputStream(simplePresenceXml.getBytes());
    saxParser.parse(bais, new SimplePresenceParser());
    bais.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 7 with SAXParser

use of javax.xml.parsers.SAXParser in project musicbrainz-android by jdamcd.

the class ResponseParser method parse.

protected void parse(InputStream stream, DefaultHandler handler) throws IOException {
    try {
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        InputSource source = new InputSource(stream);
        reader.setContentHandler(handler);
        reader.parse(source);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 8 with SAXParser

use of javax.xml.parsers.SAXParser in project languagetool by languagetool-org.

the class XMLValidator method validateInternal.

private void validateInternal(String xml, String dtdPath, String docType) throws SAXException, IOException, ParserConfigurationException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser saxParser = factory.newSAXParser();
    //used for removing existing DOCTYPE from grammar.xml files
    String cleanXml = xml.replaceAll("<!DOCTYPE.+>", "");
    String decl = "<?xml version=\"1.0\"";
    String endDecl = "?>";
    URL dtdUrl = this.getClass().getResource(dtdPath);
    if (dtdUrl == null) {
        throw new RuntimeException("DTD not found in classpath: " + dtdPath);
    }
    String dtd = "<!DOCTYPE " + docType + " PUBLIC \"-//W3C//DTD Rules 0.1//EN\" \"" + dtdUrl + "\">";
    int pos = cleanXml.indexOf(decl);
    int endPos = cleanXml.indexOf(endDecl);
    if (pos == -1) {
        throw new IOException("No XML declaration found in '" + cleanXml.substring(0, Math.min(100, cleanXml.length())) + "...'");
    }
    String newXML = cleanXml.substring(0, endPos + endDecl.length()) + "\r\n" + dtd + cleanXml.substring(endPos + endDecl.length());
    InputSource is = new InputSource(new StringReader(newXML));
    saxParser.parse(is, new ErrorHandler());
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) URL(java.net.URL) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 9 with SAXParser

use of javax.xml.parsers.SAXParser in project AndEngine by nicolasgramlich.

the class LevelLoader method loadLevelFromStream.

public void loadLevelFromStream(final InputStream pInputStream) throws IOException {
    try {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        final SAXParser sp = spf.newSAXParser();
        final XMLReader xr = sp.getXMLReader();
        this.onBeforeLoadLevel();
        final LevelParser levelParser = new LevelParser(this.mDefaultEntityLoader, this.mEntityLoaders);
        xr.setContentHandler(levelParser);
        xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
        this.onAfterLoadLevel();
    } catch (final SAXException se) {
        Debug.e(se);
    /* Doesn't happen. */
    } catch (final ParserConfigurationException pe) {
        Debug.e(pe);
    /* Doesn't happen. */
    } finally {
        StreamUtils.close(pInputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 10 with SAXParser

use of javax.xml.parsers.SAXParser in project AndEngine by nicolasgramlich.

the class TexturePackLoader method load.

public TexturePack load(final InputStream pInputStream, final String pAssetBasePath) throws TexturePackParseException {
    try {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        final SAXParser sp = spf.newSAXParser();
        final XMLReader xr = sp.getXMLReader();
        final TexturePackParser texturePackParser = new TexturePackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
        xr.setContentHandler(texturePackParser);
        xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
        return texturePackParser.getTexturePack();
    } catch (final SAXException e) {
        throw new TexturePackParseException(e);
    } catch (final ParserConfigurationException pe) {
        /* Doesn't happen. */
        return null;
    } catch (final IOException e) {
        throw new TexturePackParseException(e);
    } finally {
        StreamUtils.close(pInputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) SAXParser(javax.xml.parsers.SAXParser) TexturePackParseException(org.andengine.util.texturepack.exception.TexturePackParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9