List of usage examples for javax.xml.parsers SAXParser parse
public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException
From source file:org.languagetool.rules.patterns.FalseFriendRuleLoader.java
public final List<PatternRule> getRules(final InputStream file, final Language textLanguage, final Language motherTongue) throws ParserConfigurationException, SAXException, IOException { final FalseFriendRuleHandler handler = new FalseFriendRuleHandler(textLanguage, motherTongue); final SAXParserFactory factory = SAXParserFactory.newInstance(); final SAXParser saxParser = factory.newSAXParser(); saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);//from w ww. j a v a 2 s . c om saxParser.parse(file, handler); final List<PatternRule> rules = handler.getRules(); // Add suggestions to each rule: final ResourceBundle messages = ResourceBundle.getBundle(JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale()); for (final PatternRule rule : rules) { final List<String> suggestionMap = handler.getSuggestionMap().get(rule.getId()); if (suggestionMap != null) { final MessageFormat msgFormat = new MessageFormat(messages.getString("false_friend_suggestion")); final Object[] msg = { formatSuggestions(suggestionMap) }; rule.setMessage(rule.getMessage() + " " + msgFormat.format(msg)); } } return rules; }
From source file:org.moe.natjgen.XcodeFullDocumentation.java
public XcodeFullDocumentation(Indexer indexer, String lang, String type, String element, String name, TextEditGroup editGroup) throws IOException { this.editGroup = editGroup; if (indexer.getXcodePath() == null) { throw new IOException("Xcode path could not be retrieved"); }/* w w w . j a v a 2s . c om*/ File file = null; ArrayList<String> docsets = indexer.getConfiguration().getDocsets(); for (String docset : docsets) { File ds = new File(indexer.getXcodePath(), "Documentation/DocSets"); ds = new File(ds, docset); ds = new File(ds, DocTokensBase + "/" + lang + "/" + type + "/" + element + "/" + name + ".xml"); if (ds.exists()) { file = ds; break; } } if (file == null) { throw new IOException("no documentation found for /" + lang + "/" + type + "/" + element + "/" + name); } try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { private final int kROOT = 0; private final int kAbstract = 1; private final int kDiscussion = 2; private final int kParameters = 3; private final int kParameter = 31; private final int kParameter_Term = 311; private final int kParameter_Definition = 312; private final int kResultDescription = 4; private final int kAvailability = 5; private final int kAvailabilityItem = 51; private final int kDeclaration = 6; private final int kCodeLine = 61; private int phase = kROOT; private StringBuilder capture; private final int maxStackDepth = 1024; private boolean contentStack[] = new boolean[maxStackDepth]; private int contentStackDepth = 0; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { switch (phase) { case kAbstract: case kDiscussion: case kResultDescription: case kParameter_Definition: contentStack[contentStackDepth] = true; ++contentStackDepth; for (HTMLTagReplacement replacement : replacements) { if (qName.equalsIgnoreCase(replacement.xml)) { contentStack[contentStackDepth - 1] = replacement.param; capture.append(replacement.openPlaceholder); break; } } case kROOT: if (qName.equalsIgnoreCase("Abstract")) { phase = kAbstract; capture = new StringBuilder(); } else if (qName.equalsIgnoreCase("Discussion")) { phase = kDiscussion; capture = new StringBuilder(); } else if (qName.equalsIgnoreCase("Parameters")) { phase = kParameters; } else if (qName.equalsIgnoreCase("ResultDescription")) { phase = kResultDescription; capture = new StringBuilder(); } else if (qName.equalsIgnoreCase("Availability")) { phase = kAvailability; } else if (qName.equalsIgnoreCase("Declaration")) { phase = kDeclaration; } break; case kParameters: if (qName.equalsIgnoreCase("Parameter")) { phase = kParameter; } break; case kParameter: if (qName.equalsIgnoreCase("Term")) { phase = kParameter_Term; capture = new StringBuilder(); } else if (qName.equalsIgnoreCase("Definition")) { phase = kParameter_Definition; capture = new StringBuilder(); } break; case kAvailability: if (qName.equalsIgnoreCase("AvailabilityItem")) { phase = kAvailabilityItem; capture = new StringBuilder(); } break; case kDeclaration: if (qName.equalsIgnoreCase("CodeLine")) { phase = kCodeLine; capture = new StringBuilder(); } break; default: break; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (contentStackDepth > 0) { --contentStackDepth; for (HTMLTagReplacement replacement : replacements) { if (qName.equalsIgnoreCase(replacement.xml)) { capture.append(replacement.closePlaceholder); break; } } } else { switch (phase) { case kAbstract: if (qName.equalsIgnoreCase("Abstract")) { Abstract = capture.toString(); capture = null; phase = kROOT; } break; case kDiscussion: if (qName.equalsIgnoreCase("Discussion")) { Discussion = capture.toString(); capture = null; phase = kROOT; } break; case kParameters: if (qName.equalsIgnoreCase("Parameters")) { phase = kROOT; } break; case kResultDescription: if (qName.equalsIgnoreCase("ResultDescription")) { ResultDescription = capture.toString(); capture = null; phase = kROOT; } break; case kAvailability: if (qName.equalsIgnoreCase("Availability")) { phase = kROOT; } break; case kDeclaration: if (qName.equalsIgnoreCase("Declaration")) { phase = kROOT; } break; case kParameter: if (qName.equalsIgnoreCase("Parameter")) { phase = kParameters; } break; case kParameter_Term: if (qName.equalsIgnoreCase("Term")) { ParameterNames.add(capture.toString()); capture = null; phase = kParameter; } break; case kParameter_Definition: if (qName.equalsIgnoreCase("Definition")) { ParameterDescriptions.add(capture.toString()); capture = null; phase = kParameter; } break; case kAvailabilityItem: if (qName.equalsIgnoreCase("AvailabilityItem")) { Availabilities.add(capture.toString()); capture = null; phase = kAvailability; } break; case kCodeLine: if (qName.equalsIgnoreCase("CodeLine")) { Declaration = capture.toString(); capture = null; phase = kDeclaration; } break; default: break; } } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (capture != null) { if (contentStackDepth == 0 || contentStack[contentStackDepth - 1]) { capture.append(new String(ch, start, length)); } } } }; InputStream inputStream = new FileInputStream(file); Reader reader = new InputStreamReader(inputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); parser.parse(is, handler); } catch (Exception e) { throw new IOException("failed to parse doc", e); } }
From source file:org.mycore.common.MCRUtils.java
public static String parseDocumentType(InputStream in) { SAXParser parser = null; try {/*from w w w. j ava2 s .c om*/ parser = SAXParserFactory.newInstance().newSAXParser(); } catch (Exception ex) { String msg = "Could not build a SAX Parser for processing XML input"; throw new MCRConfigurationException(msg, ex); } final Properties detected = new Properties(); final String forcedInterrupt = "mcr.forced.interrupt"; DefaultHandler handler = new DefaultHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) { LOGGER.debug("MCRLayoutService detected root element = " + qName); detected.setProperty("docType", qName); throw new MCRException(forcedInterrupt); } }; try { parser.parse(new InputSource(in), handler); } catch (Exception ex) { if (!forcedInterrupt.equals(ex.getMessage())) { String msg = "Error while detecting XML document type from input source"; throw new MCRException(msg, ex); } } String docType = detected.getProperty("docType"); int pos = docType.indexOf(':') + 1; if (pos > 0) { //filter namespace prefix docType = docType.substring(pos); } return docType; }
From source file:org.n52.ifgicopter.spf.xml.SchematronValidator.java
/** * Here the real validation process is started. * //www. jav a 2 s . c om * @return true if no schematron rule was violated. */ public boolean validate() { Transform trans = new Transform(); /* * transform the schematron */ String[] arguments = new String[] { "-x", "org.apache.xerces.parsers.SAXParser", "-w1", "-o", DOCS_FOLDER + "/tmp/tmp.xsl", this.schematronFile, DOCS_FOLDER + "/iso_svrl_for_xslt2.xsl", "generate-paths=yes" }; trans.doTransform(arguments, "java net.sf.saxon.Transform"); /* * transform the instance */ String report = DOCS_FOLDER + "/tmp/" + this.xmlInstanceFile.substring(this.xmlInstanceFile.lastIndexOf("/") + 1) + ".report.xml"; arguments = new String[] { "-x", "org.apache.xerces.parsers.SAXParser", "-w1", "-o", report, this.xmlInstanceFile, DOCS_FOLDER + "/tmp/tmp.xsl" }; trans.doTransform(arguments, "java net.sf.saxon.Transform"); LocatorImpl locator = new LocatorImpl(); /* * an extension of DefaultHandler */ DefaultHandler handler = new DefaultHandler() { private String failTmp; private Locator locator2; private boolean insideFail = false; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.endsWith("failed-assert")) { this.failTmp = "Assertion error at \"" + attributes.getValue("test") + "\" (line " + this.locator2.getLineNumber() + "): "; this.insideFail = true; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.endsWith("failed-assert")) { SchematronValidator.this.assertFails.add(this.failTmp); this.failTmp = null; this.insideFail = false; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (this.insideFail) { this.failTmp += new String(ch, start, length).trim(); } } @Override public void setDocumentLocator(Locator l) { this.locator2 = l; } }; handler.setDocumentLocator(locator); try { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(new File(report), handler); } catch (SAXException e) { log.warn(e.getMessage(), e); } catch (IOException e) { log.warn(e.getMessage(), e); } catch (ParserConfigurationException e) { log.warn(e.getMessage(), e); } return (this.assertFails.size() == 0) ? true : false; }
From source file:org.nuxeo.apidoc.worker.ExtractXmlAttributesWorker.java
public String extractAttributes(Blob blob) throws ParserConfigurationException, SAXException, IOException { if (blob == null) { return null; }/* w w w .ja v a 2 s . c o m*/ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); Set<String> attributes = new HashSet<>(); saxParser.parse(blob.getStream(), new Handler(attributes)); return StringUtils.join(attributes, ' '); }
From source file:org.objectspace.library.marcxmlimporter.MarcReader.java
/** * @param args/*w ww . ja v a2 s. c o m*/ * @throws SAXException * @throws ParserConfigurationException * @throws IOException * @throws ConfigurationException * @throws SQLException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ConfigurationException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { String configfilename = "tagreader.xml"; if (args.length > 0) { configfilename = args[0]; } Parameters params = new Parameters(); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>( XMLConfiguration.class).configure(params.xml().setFileName(configfilename)); XMLConfiguration config = builder.getConfiguration(); String xmlfile = config.getString("marcreader.file", null); // Create a "parser factory" for creating SAX parsers SAXParserFactory spfac = SAXParserFactory.newInstance(); // Now use the parser factory to create a SAXParser object SAXParser sp = spfac.newSAXParser(); // Create an instance of this class; it defines all the handler methods MarcReader handler = new MarcReader(config); // HIL3/2$0030422 639.1 Hes MARC 954 // Finally, tell the parser to parse the input and notify the handler sp.parse(xmlfile, handler); }
From source file:org.onehippo.repository.bootstrap.instructions.ContentResourceInstruction.java
InputStream getPartialContentInputStream(InputStream in, final String contextRelPath) throws IOException, RepositoryException { File file = File.createTempFile("bootstrap-", ".xml"); OutputStream out = null;// ww w .ja va2 s . c om try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SAXParser parser = factory.newSAXParser(); out = new FileOutputStream(file); TransformerHandler handler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()) .newTransformerHandler(); Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); handler.setResult(new StreamResult(out)); parser.parse(new InputSource(in), new DefaultContentHandler(new PartialSystemViewFilter(handler, contextRelPath))); return new TempFileInputStream(file); } catch (FactoryConfigurationError e) { throw new RepositoryException("SAX parser implementation not available", e); } catch (ParserConfigurationException e) { throw new RepositoryException("SAX parser configuration error", e); } catch (SAXException e) { Exception exception = e.getException(); if (exception instanceof RepositoryException) { throw (RepositoryException) exception; } else if (exception instanceof IOException) { throw (IOException) exception; } else { throw new InvalidSerializedDataException("Error parsing XML import", e); } } catch (TransformerConfigurationException e) { throw new RepositoryException("SAX transformation error", e); } finally { IOUtils.closeQuietly(out); } }
From source file:org.onehippo.repository.bootstrap.util.PartialSystemViewFilterTest.java
public String getPartialContent(InputStream in, String startPath) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);// w w w.j ava 2s. c o m factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SAXParser parser = factory.newSAXParser(); StringWriter out = new StringWriter(); TransformerHandler handler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()) .newTransformerHandler(); Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); handler.setResult(new StreamResult(out)); parser.parse(new InputSource(in), new DefaultContentHandler(new PartialSystemViewFilter(handler, startPath))); return out.toString(); }
From source file:org.openbravo.erpCommon.ad_forms.TranslationManager.java
private static String importContributors(ConnectionProvider cp, String directory, String AD_Language) { final String fileName = directory + File.separator + CONTRIBUTORS_FILENAME + "_" + AD_Language + ".xml"; final File in = new File(fileName); if (!in.exists()) { final String msg = "File does not exist: " + fileName; log4j.debug(msg);//from w ww .j a va 2 s . c om return msg; } try { final TranslationHandler handler = new TranslationHandler(cp); final SAXParserFactory factory = SAXParserFactory.newInstance(); final SAXParser parser = factory.newSAXParser(); parser.parse(in, handler); return ""; } catch (final Exception e) { log4j.error("importContrib", e); return e.toString(); } }
From source file:org.openbravo.erpCommon.ad_forms.TranslationManager.java
private static String importTrlFile(ConnectionProvider conn, String directory, int AD_Client_ID, String AD_Language, String Trl_Table) { final String fileName = directory + File.separator + Trl_Table + "_" + AD_Language + ".xml"; log4j.debug("importTrl - " + fileName); final File in = new File(fileName); if (!in.exists()) { final String msg = "File does not exist: " + fileName; log4j.debug("importTrl - " + msg); return msg; }//w w w.jav a2 s.c om Connection con = null; try { con = conn.getTransactionConnection(); final TranslationHandler handler = new TranslationHandler(AD_Client_ID, conn, con); final SAXParserFactory factory = SAXParserFactory.newInstance(); // factory.setValidating(true); final SAXParser parser = factory.newSAXParser(); parser.parse(in, handler); conn.releaseCommitConnection(con); log4j.info("importTrl - Updated=" + handler.getUpdateCount() + " - from file " + in.getName()); return ""; } catch (final Exception e) { log4j.error("importTrlFile - error parsing file: " + fileName, e); try { conn.releaseRollbackConnection(con); } catch (SQLException e1) { log4j.error("Error on releaseRollbackConnection", e1); } throw new OBException(fileName); } }