List of usage examples for javax.xml.parsers SAXParserFactory setValidating
public void setValidating(boolean validating)
From source file:com.dgwave.osrs.OsrsClient.java
private void initJaxb() throws OsrsException { if (jc != null && oj != null) return;/*from w w w .ja v a2 s . co m*/ try { this.jc = JAXBContext.newInstance("com.dgwave.osrs.jaxb"); this.oj = new ObjectFactory(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); spf.setNamespaceAware(true); spf.setValidating(false); xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { logger.debug("Ignoring DTD"); return new InputSource(new StringReader("")); } }); } catch (Exception e) { throw new OsrsException("JAXB Error", e); } }
From source file:net.sf.jasperreports.engine.xml.BaseSaxParserFactory.java
protected SAXParserFactory createSAXParserFactory() throws ParserConfigurationException, SAXException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); if (log.isDebugEnabled()) { log.debug("Instantiated SAX parser factory of type " + parserFactory.getClass().getName()); }/*from www . j a v a2 s. c o m*/ parserFactory.setNamespaceAware(true); boolean validating = isValidating(); parserFactory.setValidating(validating); parserFactory.setFeature("http://xml.org/sax/features/validation", validating); return parserFactory; }
From source file:com.typhoon.newsreader.engine.ChannelRefresh.java
public List<FeedsListItem> parser(String link) { if (link.contains("www.24h.com.vn")) { try {//w w w. j av 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; } } }
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 ) *//*w ww . j av a 2 s . com*/ 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:eionet.cr.util.xml.XmlAnalysis.java
/** * * @param inputStream//w w w. j av a 2 s.co m * @return * @throws SAXException * @throws ParserConfigurationException * @throws GDEMException * @throws SAXException * @throws IOException */ public void parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { // set up the parser and reader SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser parser = parserFactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); // turn off validation against schema or dtd (we only need the document to be well-formed XML) parserFactory.setValidating(false); reader.setFeature("http://xml.org/sax/features/validation", false); reader.setFeature("http://apache.org/xml/features/validation/schema", false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); reader.setFeature("http://xml.org/sax/features/namespaces", true); // turn on dtd handling doctypeReader = new SAXDoctypeReader(); try { parser.setProperty("http://xml.org/sax/properties/lexical-handler", doctypeReader); } catch (SAXNotRecognizedException e) { logger.warn("Installed XML parser does not provide lexical events", e); } catch (SAXNotSupportedException e) { logger.warn("Cannot turn on comment processing here", e); } // set the handler and do the parsing handler = new Handler(); reader.setContentHandler(handler); try { reader.parse(new InputSource(inputStream)); } catch (SAXException e) { Exception ee = e.getException(); if (ee == null || !(ee instanceof CRException)) throw e; } }
From source file:eionet.gdem.conversion.spreadsheet.DDXMLConverter.java
/** * Converts XML file/* w ww .j a v a 2 s .c om*/ * @param xmlSchema XML schema * @param outStream OutputStream * @throws Exception If an error occurs. */ protected void doConversion(String xmlSchema, OutputStream outStream) throws Exception { String instanceUrl = DataDictUtil.getInstanceUrl(xmlSchema); DD_XMLInstance instance = new DD_XMLInstance(instanceUrl); DD_XMLInstanceHandler handler = new DD_XMLInstanceHandler(instance); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); factory.setValidating(false); factory.setNamespaceAware(true); reader.setFeature("http://xml.org/sax/features/validation", false); reader.setFeature("http://apache.org/xml/features/validation/schema", false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setContentHandler(handler); reader.parse(instanceUrl); if (Utils.isNullStr(instance.getEncoding())) { String enc_url = Utils.getEncodingFromStream(instanceUrl); if (!Utils.isNullStr(enc_url)) { instance.setEncoding(enc_url); } } importSheetSchemas(sourcefile, instance, xmlSchema); instance.startWritingXml(outStream); sourcefile.writeContentToInstance(instance); instance.flushXml(); }
From source file:net.lightbody.bmp.proxy.jetty.xml.XmlParser.java
/** * Constructor.//w ww. j a va 2s. c o m */ public XmlParser(boolean validating) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validating); _parser = factory.newSAXParser(); try { if (validating) _parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", validating); } catch (Exception e) { if (validating) log.warn("Schema validation may not be supported: ", e); else LogSupport.ignore(log, e); } _parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", validating); _parser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", validating); _parser.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes", validating); } catch (Exception e) { log.warn(LogSupport.EXCEPTION, e); throw new Error(e.toString()); } }
From source file:com.cloudera.recordbreaker.analyzer.XMLSchemaDescriptor.java
void computeSchema() throws IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = null;/* www . jav a 2 s. com*/ // Unfortunately, validation is often not possible factory.setValidating(false); try { // The XMLProcessor builds up a tree of tags XMLProcessor xp = new XMLProcessor(); parser = factory.newSAXParser(); parser.parse(dd.getRawBytes(), xp); // Grab the root tag this.rootTag = xp.getRoot(); // Once the tree is built, we: // a) Find the correct repetition node (and throws out 'bad' repeats) // b) Flatten hierarchies of subfields into a single layer, so it's suitable // for relational-style handling // c) Build an overall schema object that can summarize every expected // object, even if the objects' individual schemas differ somewhat this.rootTag.completeTree(); } catch (SAXException saxe) { throw new IOException(saxe.toString()); } catch (ParserConfigurationException pcee) { throw new IOException(pcee.toString()); } }
From source file:net.lightbody.bmp.proxy.jetty.xml.XmlParser.java
/** * Construct//from w w w . java 2 s . c o m */ public XmlParser() { try { SAXParserFactory factory = SAXParserFactory.newInstance(); boolean notValidating = Boolean.getBoolean("net.lightbody.bmp.proxy.jetty.xml.XmlParser.NotValidating"); factory.setValidating(!notValidating); _parser = factory.newSAXParser(); try { if (!notValidating) _parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true); } catch (Exception e) { log.warn("Schema validation may not be supported"); log.debug("", e); notValidating = true; } _parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", !notValidating); _parser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", !notValidating); _parser.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes", !notValidating); } catch (Exception e) { log.warn(LogSupport.EXCEPTION, e); throw new Error(e.toString()); } }
From source file:net.sf.firemox.xml.XmlParser.java
/** * Constructor.// w ww . j a v a 2 s . c o m * * @param validation * validation flag. * @throws SAXException */ public XmlParser(boolean validation) throws SAXException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(validation); parser = factory.newSAXParser(); if (validation) { parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); parser.setProperty(JAXP_SCHEMA_SOURCE, MToolKit.getFile(MP_XML_SCHEMA)); } } catch (Exception e) { throw new SAXException(e.toString()); } }