List of usage examples for javax.xml.parsers SAXParser parse
public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException
From source file:com.vionto.vithesaurus.wikipedia.WiktionarySynonymDumper.java
private void run(InputStream is) throws IOException, SAXException, ParserConfigurationException { WiktionaryPageHandler handler = new WiktionaryPageHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);//from ww w . ja v a2s .c o m System.out.println("SET NAMES utf8;"); System.out.println("DROP TABLE IF EXISTS wiktionary;"); System.out.println("CREATE TABLE `wiktionary` ( " + "`headword` varchar(255) NOT NULL default '', " + "`meanings` text, " + "`synonyms` text, " + "KEY `headword` (`headword`)" + ") ENGINE = MYISAM;"); saxParser.parse(is, handler); System.err.println("Exported: " + handler.exported); System.err.println("Skipped: " + handler.skipped); }
From source file:com.mirth.connect.model.converters.DICOMSerializer.java
@Override public String fromXML(String source) throws SerializerException { if (source == null || source.length() == 0) { return StringUtils.EMPTY; }//from w w w. j a v a 2 s . c o m try { // re-parse the xml to Mirth format Document document = documentSerializer.fromXML(source); Element element = document.getDocumentElement(); Node node = element.getChildNodes().item(0); // change back to <attr> tag for all tags under <dicom> tag while (node != null) { renameTagToAttr(document, node); node = node.getNextSibling(); } NodeList items = document.getElementsByTagName("item"); // change back to <attr> tag for all tags under <item> tags if (items != null) { for (int i = 0; i < items.getLength(); i++) { Node itemNode = items.item(i); if (itemNode.getChildNodes() != null) { NodeList itemNodes = itemNode.getChildNodes(); for (int j = 0; j < itemNodes.getLength(); j++) { Node nodeItem = itemNodes.item(j); renameTagToAttr(document, nodeItem); } } } } // find the charset String charset = null; Element charsetElement = (Element) document.getElementsByTagName("tag00080005").item(0); if (charsetElement != null) { charset = charsetElement.getNodeValue(); } else { charset = "utf-8"; } // parse the Document into a DicomObject SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); DicomObject dicomObject = new BasicDicomObject(); ContentHandlerAdapter contentHandler = new ContentHandlerAdapter(dicomObject); byte[] documentBytes = documentSerializer.toXML(document).trim().getBytes(charset); parser.parse(new InputSource(new ByteArrayInputStream(documentBytes)), contentHandler); return new String(Base64.encodeBase64Chunked(DICOMUtil.dicomObjectToByteArray(dicomObject))); } catch (Exception e) { throw new SerializerException(e); } }
From source file:com.mirth.connect.plugins.datatypes.dicom.DICOMSerializer.java
@Override public String fromXML(String source) throws MessageSerializerException { if (source == null || source.length() == 0) { return org.apache.commons.lang3.StringUtils.EMPTY; }/*from ww w .java 2 s.c o m*/ try { // re-parse the xml to Mirth format Document document = documentSerializer.fromXML(source); Element element = document.getDocumentElement(); Node node = element.getChildNodes().item(0); // change back to <attr> tag for all tags under <dicom> tag while (node != null) { renameTagToAttr(document, node); node = node.getNextSibling(); } NodeList items = document.getElementsByTagName("item"); // change back to <attr> tag for all tags under <item> tags if (items != null) { for (int i = 0; i < items.getLength(); i++) { Node itemNode = items.item(i); if (itemNode.getChildNodes() != null) { NodeList itemNodes = itemNode.getChildNodes(); for (int j = 0; j < itemNodes.getLength(); j++) { Node nodeItem = itemNodes.item(j); renameTagToAttr(document, nodeItem); } } } } // find the charset String charset = null; Element charsetElement = (Element) document.getElementsByTagName("tag00080005").item(0); if (charsetElement != null) { charset = charsetElement.getNodeValue(); } else { charset = "utf-8"; } // parse the Document into a DicomObject SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); DicomObject dicomObject = new BasicDicomObject(); ContentHandlerAdapter contentHandler = new ContentHandlerAdapter(dicomObject); byte[] documentBytes = documentSerializer.toXML(document).trim().getBytes(charset); parser.parse(new InputSource(new ByteArrayInputStream(documentBytes)), contentHandler); return StringUtils .newStringUsAscii(Base64Util.encodeBase64(DICOMConverter.dicomObjectToByteArray(dicomObject))); } catch (Exception e) { throw new MessageSerializerException("Error converting XML to DICOM", e, ErrorMessageBuilder .buildErrorMessage(this.getClass().getSimpleName(), "Error converting XML to DICOM", e)); } }
From source file:net.sbbi.upnp.messages.ActionMessage.java
/** * Executes the message and retuns the UPNP device response, according to the UPNP specs, * this method could take up to 30 secs to process ( time allowed for a device to respond to a request ) * @return a response object containing the UPNP parsed response * @throws IOException if some IOException occurs during message send and reception process * @throws UPNPResponseException if an UPNP error message is returned from the server * or if some parsing exception occurs ( detailErrorCode = 899, detailErrorDescription = SAXException message ) *///from ww w . jav a 2 s. co m public ActionResponse service() throws IOException, UPNPResponseException { ActionResponse rtrVal = null; UPNPResponseException upnpEx = null; IOException ioEx = null; StringBuffer body = new StringBuffer(256); body.append("<?xml version=\"1.0\"?>\r\n"); body.append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""); body.append(" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"); body.append("<s:Body>"); body.append("<u:").append(serviceAction.getName()).append(" xmlns:u=\"").append(service.getServiceType()) .append("\">"); if (serviceAction.getInputActionArguments() != null) { // this action requires params so we just set them... for (Iterator itr = inputParameters.iterator(); itr.hasNext();) { InputParamContainer container = (InputParamContainer) itr.next(); body.append("<").append(container.name).append(">").append(container.value); body.append("</").append(container.name).append(">"); } } body.append("</u:").append(serviceAction.getName()).append(">"); body.append("</s:Body>"); body.append("</s:Envelope>"); if (log.isDebugEnabled()) log.debug("POST prepared for URL " + service.getControlURL()); URL url = new URL(service.getControlURL().toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); HttpURLConnection.setFollowRedirects(false); //conn.setConnectTimeout( 30000 ); conn.setRequestProperty("HOST", url.getHost() + ":" + url.getPort()); conn.setRequestProperty("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); conn.setRequestProperty("CONTENT-LENGTH", Integer.toString(body.length())); conn.setRequestProperty("SOAPACTION", "\"" + service.getServiceType() + "#" + serviceAction.getName() + "\""); OutputStream out = conn.getOutputStream(); out.write(body.toString().getBytes()); out.flush(); out.close(); conn.connect(); InputStream input = null; if (log.isDebugEnabled()) log.debug("executing query :\n" + body); try { input = conn.getInputStream(); } catch (IOException ex) { // java can throw an exception if he error code is 500 or 404 or something else than 200 // but the device sends 500 error message with content that is required // this content is accessible with the getErrorStream input = conn.getErrorStream(); } if (input != null) { int response = conn.getResponseCode(); String responseBody = getResponseBody(input); if (log.isDebugEnabled()) log.debug("received response :\n" + responseBody); SAXParserFactory saxParFact = SAXParserFactory.newInstance(); saxParFact.setValidating(false); saxParFact.setNamespaceAware(true); ActionMessageResponseParser msgParser = new ActionMessageResponseParser(serviceAction); StringReader stringReader = new StringReader(responseBody); InputSource src = new InputSource(stringReader); try { SAXParser parser = saxParFact.newSAXParser(); parser.parse(src, msgParser); } catch (ParserConfigurationException confEx) { // should never happen // we throw a runtimeException to notify the env problem throw new RuntimeException( "ParserConfigurationException during SAX parser creation, please check your env settings:" + confEx.getMessage()); } catch (SAXException saxEx) { // kind of tricky but better than nothing.. upnpEx = new UPNPResponseException(899, saxEx.getMessage()); } finally { try { input.close(); } catch (IOException ex) { // ignore } } if (upnpEx == null) { if (response == HttpURLConnection.HTTP_OK) { rtrVal = msgParser.getActionResponse(); } else if (response == HttpURLConnection.HTTP_INTERNAL_ERROR) { upnpEx = msgParser.getUPNPResponseException(); } else { ioEx = new IOException("Unexpected server HTTP response:" + response); } } } try { out.close(); } catch (IOException ex) { // ignore } conn.disconnect(); if (upnpEx != null) { throw upnpEx; } if (rtrVal == null && ioEx == null) { ioEx = new IOException("Unable to receive a response from the UPNP device"); } if (ioEx != null) { throw ioEx; } return rtrVal; }
From source file:com.piketec.jenkins.plugins.tpt.publisher.TPTReportPublisher.java
/** * Xml SAXparser//from w ww . j av a 2 s . c o m * * @param xmlFile * @param tptFile * @param failedTests * @param reportDirOnRemote * @param executionConfiguration * @throws InterruptedException */ private void parse(FilePath xmlFile, TPTFile tptFile, ArrayList<TPTTestCase> failedTests, String reportDirOnRemote, String executionConfiguration, TptLogger logger) throws InterruptedException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); try { SAXParser saxParser = saxParserFactory.newSAXParser(); TPTReportSAXHandler handler = new TPTReportSAXHandler(tptFile, failedTests, reportDirOnRemote, executionConfiguration); InputStream inputStream = xmlFile.read(); try { saxParser.parse(inputStream, handler); } finally { IOUtils.closeQuietly(inputStream); } } catch (ParserConfigurationException | SAXException | IOException e) { logger.error(e.getMessage()); } }
From source file:de.tudarmstadt.ukp.dkpro.core.io.ancora.AncoraReader.java
@Override public void getNext(JCas aJCas) throws IOException, CollectionException { Resource res = nextFile();//from ww w . j a v a 2s.com initCas(aJCas, res); // Set up language if (getLanguage() != null) { aJCas.setDocumentLanguage(getLanguage()); } // Configure mapping only now, because now the language is set in the CAS try { posMappingProvider.configure(aJCas.getCas()); } catch (AnalysisEngineProcessException e1) { throw new IOException(e1); } InputStream is = null; try { is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream()); // Create handler AncoraHandler handler = new AncoraHandler(); handler.setJCas(aJCas); handler.setLogger(getLogger()); // Parse XML SAXParserFactory pf = SAXParserFactory.newInstance(); SAXParser parser = pf.newSAXParser(); InputSource source = new InputSource(is); source.setPublicId(res.getLocation()); source.setSystemId(res.getLocation()); parser.parse(source, handler); } catch (ParserConfigurationException | SAXException e) { throw new IOException(e); } finally { closeQuietly(is); } if (dropSentencesMissingPosTags) { List<FeatureStructure> toRemove = new ArrayList<>(); // Remove sentences without pos TAGs for (Sentence s : select(aJCas, Sentence.class)) { boolean remove = false; for (Token t : selectCovered(Token.class, s)) { if (t.getPos() == null) { toRemove.add(s); remove = true; break; } } if (remove) { for (Token t : selectCovered(Token.class, s)) { toRemove.add(t); if (t.getLemma() != null) { toRemove.add(t.getLemma()); } if (t.getPos() != null) { toRemove.add(t.getPos()); } } } } for (FeatureStructure fs : toRemove) { aJCas.getCas().removeFsFromIndexes(fs); } // Remove tokens without pos tags that are located *BETWEEN* sentences! toRemove.clear(); for (Token t : select(aJCas, Token.class)) { if (t.getPos() == null) { toRemove.add(t); if (t.getLemma() != null) { toRemove.add(t.getLemma()); } if (t.getPos() != null) { toRemove.add(t.getPos()); } } } for (FeatureStructure fs : toRemove) { aJCas.getCas().removeFsFromIndexes(fs); } } }
From source file:ch.entwine.weblounge.common.impl.content.AbstractResourceReaderImpl.java
/** * This method is called when a <code>Page</code> object is instantiated. * /*from w ww . j ava 2s.c om*/ * @param is * the xml input stream * @param uri * the page uri * * @throws IOException * if reading the input stream fails */ public T read(InputStream is, Site site) throws SAXException, IOException, ParserConfigurationException { reset(); resource = createResource(site); readHeader = true; readBody = true; SAXParser parser = parserRef.get(); if (parser == null) { parser = parserFactory.newSAXParser(); parserRef = new WeakReference<SAXParser>(parser); } parser.parse(is, this); return resource; }
From source file:com.magicmod.mmweather.engine.YahooWeatherProvider.java
@Override public void refreshData(String id, String localizedCityName, boolean metricUnits) { mCityId = id;/*from w w w . j a v a 2s.c om*/ mLocalizedCityName = localizedCityName; mMetricUnits = metricUnits; String url = String.format(URL_WEATHER, id, metricUnits ? "c" : "f"); String response = HttpRetriever.retrieve(url); if (response == null) { mWeatherInfo = null; return; } SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser parser = factory.newSAXParser(); StringReader reader = new StringReader(response); WeatherHandler handler = new WeatherHandler(); parser.parse(new InputSource(reader), handler); if (handler.isComplete()) { // There are cases where the current condition is unknown, but the forecast // is not - using the (inaccurate) forecast is probably better than showing // the question mark if (handler.conditionCode.equals(3200)) { handler.condition = handler.forecasts.get(0).getCondition(); handler.conditionCode = handler.forecasts.get(0).getConditionCode(); } ArrayList<DayForecast> forecasts = new ArrayList<WeatherInfo.DayForecast>(); long time = System.currentTimeMillis(); for (DayForecast forecast : handler.forecasts) { if (forecast.getDate().equals(handler.date)) { forecast.setCondition(handler.condition); forecast.setConditionCode(handler.conditionCode); forecast.setHumidity(handler.humidity); forecast.setSunRaise(handler.sunrise); forecast.setSunSet(handler.sunset); forecast.setTemperature(handler.temperature); forecast.setTempUnit(handler.temperatureUnit); forecast.setWindSpeed(handler.windSpeed); forecast.setWindDirection(handler.windDirection); forecast.setWindSpeedUnit(handler.speedUnit); } if (localizedCityName != null) { forecast.setCity(localizedCityName); } forecast.setSynctimestamp(String.valueOf(time)); forecasts.add(forecast); } mWeatherInfo = new WeatherInfo(forecasts); Log.d(TAG, "Weather updated: " + mWeatherInfo); } else { Log.w(TAG, "Received incomplete weather XML (id=" + id + ")"); mWeatherInfo = null; } } catch (ParserConfigurationException e) { Log.e(TAG, "Could not create XML parser", e); mWeatherInfo = null; } catch (SAXException e) { Log.e(TAG, "Could not parse weather XML (id=" + id + ")", e); mWeatherInfo = null; } catch (IOException e) { Log.e(TAG, "Could not parse weather XML (id=" + id + ")", e); mWeatherInfo = null; } if (mWeatherDataChangedListener != null) mWeatherDataChangedListener.onDataChanged(); }
From source file:ch.entwine.weblounge.common.impl.content.AbstractResourceReaderImpl.java
/** * This method is called to read the head section of a resource. * /*from w w w . ja v a 2 s .c o m*/ * @param is * the xml input stream * @param site * the site * @throws ParserConfigurationException * if the SAX parser setup failed * @throws IOException * if reading the input stream fails * @throws SAXException * if an error occurs while parsing */ public T readHeader(InputStream is, Site site) throws SAXException, IOException, ParserConfigurationException { if (resource == null) { resource = createResource(site); } readHeader = true; readBody = false; SAXParser parser = parserRef.get(); if (parser == null) { parser = parserFactory.newSAXParser(); parserRef = new WeakReference<SAXParser>(parser); } parser.parse(is, this); return resource; }
From source file:ch.entwine.weblounge.common.impl.content.AbstractResourceReaderImpl.java
/** * This method is called to read the body of a resource. * /* ww w. ja va 2 s . c o m*/ * @param is * the xml input stream * @param site * the site * @throws ParserConfigurationException * if the SAX parser setup failed * @throws IOException * if reading the input stream fails * @throws SAXException * if an error occurs while parsing */ public T readBody(InputStream is, Site site) throws SAXException, IOException, ParserConfigurationException { if (resource == null) { resource = createResource(site); } readHeader = false; readBody = true; SAXParser parser = parserRef.get(); if (parser == null) { parser = parserFactory.newSAXParser(); parserRef = new WeakReference<SAXParser>(parser); } parser.parse(is, this); return resource; }