List of usage examples for org.xml.sax XMLReader setFeature
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException;
From source file:org.dspace.content.authority.LCNameAuthority.java
/** * Guts of the implementation, returns a complete Choices result, or * null for a failure.// w w w . j av a 2 s. c o m */ private Choices queryPerson(String text, int start, int limit) { // punt if there is no query text if (text == null || text.trim().length() == 0) { return new Choices(true); } // 1. build CQL query DCPersonName pn = new DCPersonName(text); StringBuilder query = new StringBuilder(); query.append("local.FirstName = \"").append(pn.getFirstNames()).append("\" and local.FamilyName = \"") .append(pn.getLastName()).append("\""); // XXX arbitrary default limit - should be configurable? if (limit == 0) { limit = 50; } NameValuePair args[] = new NameValuePair[6]; args[0] = new NameValuePair("operation", "searchRetrieve"); args[1] = new NameValuePair("version", "1.1"); args[2] = new NameValuePair("recordSchema", "info:srw/schema/1/marcxml-v1.1"); args[3] = new NameValuePair("query", query.toString()); args[4] = new NameValuePair("maximumRecords", String.valueOf(limit)); args[5] = new NameValuePair("startRecord", String.valueOf(start + 1)); HttpClient hc = new HttpClient(); String srUrl = url + "?" + EncodingUtil.formUrlEncode(args, "UTF8"); GetMethod get = new GetMethod(srUrl); log.debug("Trying SRU query, URL=" + srUrl); // 2. web request try { int status = hc.executeMethod(get); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRUHandler handler = new SRUHandler(); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(get.getResponseBodyAsStream())); // this probably just means more results available.. if (handler.hits != handler.result.size()) { log.warn("Discrepency in results, result.length=" + handler.result.size() + ", yet expected results=" + handler.hits); } boolean more = handler.hits > (start + handler.result.size()); // XXX add non-auth option; perhaps the UI should do this? // XXX it's really a policy matter if they allow unauth result. // XXX good, stop it. // handler.result.add(new Choice("", text, "Non-Authority: \""+text+"\"")); int confidence; if (handler.hits == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.hits == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result.toArray(new Choice[handler.result.size()]), start, handler.hits, confidence, more); } } catch (HttpException e) { log.error("SRU query failed: ", e); return new Choices(true); } catch (IOException e) { log.error("SRU query failed: ", e); return new Choices(true); } catch (ParserConfigurationException e) { log.warn("Failed parsing SRU result: ", e); return new Choices(true); } catch (SAXException e) { log.warn("Failed parsing SRU result: ", e); return new Choices(true); } finally { get.releaseConnection(); } return new Choices(true); }
From source file:org.dspace.content.authority.SHERPARoMEOProtocol.java
protected Choices query(String result, String label, String authority, NameValuePair[] args, int start, int limit) { HttpClient hc = new HttpClient(); String srUrl = url + "?" + EncodingUtil.formUrlEncode(args, "UTF8"); GetMethod get = new GetMethod(srUrl); log.debug("Trying SHERPA/RoMEO Query, URL=" + srUrl); try {// w ww . j av a2s .com int status = hc.executeMethod(get); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRHandler handler = new SRHandler(result, label, authority); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(get.getResponseBodyAsStream())); int confidence; if (handler.total == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.total == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result, start, handler.total, confidence, false); } } catch (HttpException e) { log.error("SHERPA/RoMEO query failed: ", e); return null; } catch (IOException e) { log.error("SHERPA/RoMEO query failed: ", e); return null; } catch (ParserConfigurationException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } catch (SAXException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } finally { get.releaseConnection(); } return null; }
From source file:org.dspace.content.authority.SkylightAPIProtocol.java
protected Choices query(String result, String label, String authority, String args, int start, int limit) throws UnsupportedEncodingException { String encodedArgs = new String(args.getBytes("UTF-8"), "UTF-8"); HttpClient hc = new HttpClient(); String srUrl = url + "/search/" + encodedArgs + ".xml?field=" + label; GetMethod get = new GetMethod(srUrl); log.info("Trying Skylight Query, URL=" + srUrl); try {//from ww w . j ava 2s . c o m int status = hc.executeMethod(get); log.info("Status" + status); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRHandler handler = new SRHandler(result, label, authority); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); InputSource src = new InputSource(get.getResponseBodyAsStream()); log.info("Source from sklightui :: " + src.toString()); xr.parse(src); int confidence; if (handler.total == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.total == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result, start, handler.total, confidence, false); } } catch (HttpException e) { log.error("Skylight query failed: ", e); return null; } catch (IOException e) { log.error("Skylight query failed: ", e); return null; } catch (ParserConfigurationException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } catch (SAXException e) { log.warn("Failed parsing Skylight result: ", e); return null; } finally { get.releaseConnection(); } return null; }
From source file:org.eclipse.rdf4j.rio.rdfxml.RDFXMLParser.java
private void parse(InputSource inputSource) throws IOException, RDFParseException, RDFHandlerException { clear();//from ww w . j a va2 s . c o m try { documentURI = inputSource.getSystemId(); saxFilter.setParseStandAloneDocuments( getParserConfig().get(XMLParserSettings.PARSE_STANDALONE_DOCUMENTS)); // saxFilter.clear(); saxFilter.setDocumentURI(documentURI); XMLReader xmlReader; if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) { xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER); } else { xmlReader = XMLReaderFactory.createXMLReader(); } xmlReader.setContentHandler(saxFilter); xmlReader.setErrorHandler(this); // Set all compulsory feature settings, using the defaults if they are // not explicitly set for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) { try { xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting)); } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey())); } } // Set all compulsory property settings, using the defaults if they are // not explicitly set for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) { try { xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting)); } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey())); } } // Check for any optional feature settings that are explicitly set in // the parser config for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) { try { if (getParserConfig().isSet(aSetting)) { xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting)); } } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey())); } } // Check for any optional property settings that are explicitly set in // the parser config for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) { try { if (getParserConfig().isSet(aSetting)) { xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting)); } } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey())); } } xmlReader.parse(inputSource); } catch (SAXParseException e) { Exception wrappedExc = e.getException(); if (wrappedExc == null) { reportFatalError(e, e.getLineNumber(), e.getColumnNumber()); } else { reportFatalError(wrappedExc, e.getLineNumber(), e.getColumnNumber()); } } catch (SAXException e) { Exception wrappedExc = e.getException(); if (wrappedExc == null) { reportFatalError(e); } else if (wrappedExc instanceof RDFParseException) { throw (RDFParseException) wrappedExc; } else if (wrappedExc instanceof RDFHandlerException) { throw (RDFHandlerException) wrappedExc; } else { reportFatalError(wrappedExc); } } finally { // Clean up saxFilter.clear(); xmlLang = null; elementStack.clear(); usedIDs.clear(); clear(); } }
From source file:org.everit.authentication.cas.CasAuthentication.java
/** * Returns the value of an XML element. This method is used to process the XMLs sent by the CAS * server./*ww w. ja v a2 s . c om*/ * * @param xmlAsString * the XML string to process * @param elementName * the name of the queried element * @return the value assigned to the queried element name * @throws RuntimeException * if any error occurs during the parsing of the XML string */ private String getTextForElement(final String xmlAsString, final String elementName) { XMLReader xmlReader; try { xmlReader = saxParserFactory.newSAXParser().getXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (SAXException | ParserConfigurationException e) { throw new RuntimeException("Unable to create XMLReader", e); } StringBuilder builder = new StringBuilder(); DefaultHandler handler = new DefaultHandlerExt(builder, elementName); xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(handler); try { xmlReader.parse(new InputSource(new StringReader(xmlAsString))); } catch (Exception e) { throw new RuntimeException(e); } return builder.toString(); }
From source file:org.everit.osgi.authentication.cas.internal.CasAuthenticationComponent.java
/** * Returns the value of an XML element. This method is used to process the XMLs sent by the CAS server. * * @param xmlAsString//from w ww . j av a 2 s. c o m * the XML string to process * @param elementName * the name of the queried element * @return the value assigned to the queried element name * @throws RuntimeException * if any error occurs during the parsing of the XML string */ private String getTextForElement(final String xmlAsString, final String elementName) { XMLReader xmlReader; try { xmlReader = saxParserFactory.newSAXParser().getXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (final Exception e) { throw new RuntimeException("Unable to create XMLReader", e); } StringBuilder builder = new StringBuilder(); DefaultHandler handler = new DefaultHandler() { private boolean foundElement = false; @Override public void characters(final char[] ch, final int start, final int length) throws SAXException { if (foundElement) { builder.append(ch, start, length); } } @Override public void endElement(final String uri, final String localName, final String qName) throws SAXException { if (localName.equals(elementName)) { foundElement = false; } } @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { if (localName.equals(elementName)) { foundElement = true; } } }; xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(handler); try { xmlReader.parse(new InputSource(new StringReader(xmlAsString))); } catch (Exception e) { throw new RuntimeException(e); } return builder.toString(); }
From source file:org.exist.util.XMLReaderObjectFactory.java
private static void setReaderFeature(XMLReader xmlReader, String featureName, boolean value) { try {/*from ww w .ja v a 2 s . com*/ xmlReader.setFeature(featureName, value); } catch (final SAXNotRecognizedException ex) { LOG.error("SAXNotRecognizedException: " + ex.getMessage()); } catch (final SAXNotSupportedException ex) { LOG.error("SAXNotSupportedException:" + ex.getMessage()); } }
From source file:org.exoplatform.services.document.impl.MSXPPTDocumentReader.java
/** * Extracts the text content of the n first slides */// ww w .j a v a 2 s .c om public String getContentAsText(InputStream is, int maxSlides) throws IOException, DocumentReadException { if (is == null) { throw new IllegalArgumentException("InputStream is null."); } StringBuilder appendText = new StringBuilder(); try { int slideCount = 0; ZipInputStream zis = new ZipInputStream(is); try { ZipEntry ze = zis.getNextEntry(); if (ze == null) return ""; XMLReader xmlReader = SAXHelper.newXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/validation", false); xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); Map<Integer, String> slides = new TreeMap<Integer, String>(); // PPTX: ppt/slides/slide<slide_no>.xml while (ze != null && slideCount < maxSlides) { String zeName = ze.getName(); if (zeName.startsWith(PPTX_SLIDE_PREFIX) && zeName.length() > PPTX_SLIDE_PREFIX.length()) { String slideNumberStr = zeName.substring(PPTX_SLIDE_PREFIX.length(), zeName.indexOf(".xml")); int slideNumber = -1; try { slideNumber = Integer.parseInt(slideNumberStr); } catch (NumberFormatException e) { LOG.warn("Could not parse the slide number: " + e.getMessage()); } if (slideNumber > -1 && slideNumber <= maxSlides) { MSPPTXContentHandler contentHandler = new MSPPTXContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource((new ByteArrayInputStream(IOUtils.toByteArray(zis))))); slides.put(slideNumber, contentHandler.getContent()); slideCount++; } } ze = zis.getNextEntry(); } for (String slide : slides.values()) { appendText.append(slide); appendText.append(' '); } } finally { try { zis.close(); } catch (IOException e) { if (LOG.isTraceEnabled()) { LOG.trace("An exception occurred: " + e.getMessage()); } } } return appendText.toString(); } catch (ParserConfigurationException e) { throw new DocumentReadException(e.getMessage(), e); } catch (SAXException e) { throw new DocumentReadException(e.getMessage(), e); } finally { try { is.close(); } catch (IOException e) { if (LOG.isTraceEnabled()) { LOG.trace("An exception occurred: " + e.getMessage()); } } } }
From source file:org.exoplatform.services.document.impl.MSXPPTDocumentReader.java
public Properties getProperties(InputStream is) throws IOException, DocumentReadException { try {//from w w w . jav a 2 s. c o m ZipInputStream zis = new ZipInputStream(is); try { ZipEntry ze = zis.getNextEntry(); while (ze != null && !ze.getName().equals(PPTX_CORE_NAME)) { ze = zis.getNextEntry(); } if (ze == null) return new Properties(); MSPPTXMetaHandler metaHandler = new MSPPTXMetaHandler(); XMLReader xmlReader = SAXHelper.newXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/validation", false); xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); xmlReader.setContentHandler(metaHandler); xmlReader.parse(new InputSource(zis)); return metaHandler.getProperties(); } finally { zis.close(); } } catch (ParserConfigurationException e) { throw new DocumentReadException(e.getMessage(), e); } catch (SAXException e) { throw new DocumentReadException(e.getMessage(), e); } finally { if (is != null) try { is.close(); } catch (IOException e) { if (LOG.isTraceEnabled()) { LOG.trace("An exception occurred: " + e.getMessage()); } } } }
From source file:org.finra.jtaf.ewd.impl.DefaultExtWebDriver.java
@Override public String evaluateXpath(String xpath) throws Exception { XPathFactory xpathFac = XPathFactory.newInstance(); XPath theXpath = xpathFac.newXPath(); String html = getHtmlSource(); html = html.replaceAll(">\\s+<", "><"); InputStream input = new ByteArrayInputStream(html.getBytes()); XMLReader reader = new Parser(); reader.setFeature(Parser.namespacesFeature, false); Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMResult result = new DOMResult(); transformer.transform(new SAXSource(reader, new InputSource(input)), result); Node htmlNode = result.getNode(); // This code gets a Node from the // result./*from w w w.j a v a 2s .c o m*/ return (String) theXpath.evaluate(xpath, htmlNode, XPathConstants.STRING); }