javax.xml.parsers.SAXParser.getXMLReader()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(15.1k)|赞(0)|评价(0)|浏览(155)

本文整理了Java中javax.xml.parsers.SAXParser.getXMLReader()方法的一些代码示例,展示了SAXParser.getXMLReader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SAXParser.getXMLReader()方法的具体详情如下:
包路径:javax.xml.parsers.SAXParser
类名称:SAXParser
方法名:getXMLReader

SAXParser.getXMLReader介绍

[英]Returns the org.xml.sax.XMLReader that is encapsulated by the implementation of this class.
[中]返回组织。xml。萨克斯。由该类的实现封装的XMLReader。

代码示例

代码示例来源:origin: stackoverflow.com

class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> {

  private Exception exception;

  protected RSSFeed doInBackground(String... urls) {
    try {
      URL url = new URL(urls[0]);
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser parser = factory.newSAXParser();
      XMLReader xmlreader = parser.getXMLReader();
      RssHandler theRSSHandler = new RssHandler();
      xmlreader.setContentHandler(theRSSHandler);
      InputSource is = new InputSource(url.openStream());
      xmlreader.parse(is);

      return theRSSHandler.getFeed();
    } catch (Exception e) {
      this.exception = e;

      return null;
    }
  }

  protected void onPostExecute(RSSFeed feed) {
    // TODO: check this.exception
    // TODO: do something with the feed
  }
}

代码示例来源:origin: apache/nifi

public static XMLReader createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler) throws SAXException, ParserConfigurationException {
    if (saxParserFactory == null) {
      throw new IllegalArgumentException("The provided SAX parser factory cannot be null");
    }

    if (contentHandler == null) {
      throw new IllegalArgumentException("The provided SAX content handler cannot be null");
    }

    saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

    SAXParser saxParser = saxParserFactory.newSAXParser();
    XMLReader xmlReader = saxParser.getXMLReader();
    xmlReader.setContentHandler(contentHandler);

    return xmlReader;
  }
}

代码示例来源:origin: jersey/jersey

/**
 * Create new {@link javax.xml.transform.sax.SAXSource} for a given entity input stream.
 *
 * @param spf          SAX parser factory to be used to create the SAX source.
 * @param entityStream entity input stream.
 * @return new {@link javax.xml.transform.sax.SAXSource} representing the entity input stream.
 * @throws JAXBException in case SAX source creation fails.
 */
protected static SAXSource getSAXSource(SAXParserFactory spf, InputStream entityStream) throws JAXBException {
  try {
    return new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(entityStream));
  } catch (Exception ex) {
    throw new JAXBException("Error creating SAXSource", ex);
  }
}

代码示例来源:origin: jeremylong/DependencyCheck

final SuppressionHandler handler = new SuppressionHandler();
final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream12, schemaStream11, schemaStream10);
final XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setErrorHandler(new SuppressionErrorHandler());
xmlReader.setContentHandler(handler);
try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
  final InputSource in = new InputSource(reader);
  xmlReader.parse(in);
  return handler.getSuppressionRules();

代码示例来源:origin: languagetool-org/languagetool

public final List<BitextPatternRule> getRules(InputStream is, String filename) throws IOException {
 List<BitextPatternRule> rules;
 try {
  BitextPatternRuleHandler handler = new BitextPatternRuleHandler();
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser saxParser = factory.newSAXParser();
  saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
  saxParser.parse(is, handler);
  rules = handler.getBitextRules();
  return rules;
 } catch (Exception e) {
  throw new IOException("Cannot load or parse '" + filename + "'", e);
 }
}

代码示例来源:origin: hawtio/hawtio

protected final SAXParser createParser(SAXParserFactory parserFactory)
    throws ParserConfigurationException, SAXException {
  parserFactory.setNamespaceAware(true);
  final SAXParser parser = parserFactory.newSAXParser();
  final XMLReader reader = parser.getXMLReader();
  //reader.setProperty("http://xml.org/sax/properties/lexical-handler", this); //$NON-NLS-1$
  // disable DTD validation (bug 63625)
  try {
    //	be sure validation is "off" or the feature to ignore DTD's will not apply
    reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$
    reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
  } catch (SAXNotRecognizedException e) {
    // not a big deal if the parser does not recognize the features
  } catch (SAXNotSupportedException e) {
    // not a big deal if the parser does not support the features
  }
  return parser;
}

代码示例来源:origin: robovm/robovm

/**
 * Parse the content given {@link org.xml.sax.InputSource}
 * as XML using the specified
 * {@link org.xml.sax.helpers.DefaultHandler}.
 *
 * @param is The InputSource containing the content to be parsed.
 * @param dh The SAX DefaultHandler to use.
 *
 * @throws IllegalArgumentException If the <code>InputSource</code> object
 *   is <code>null</code>.
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler
 */
public void parse(InputSource is, DefaultHandler dh)
  throws SAXException, IOException {
  if (is == null) {
    throw new IllegalArgumentException("InputSource cannot be null");
  }
  XMLReader reader = this.getXMLReader();
  if (dh != null) {
    reader.setContentHandler(dh);
    reader.setEntityResolver(dh);
    reader.setErrorHandler(dh);
    reader.setDTDHandler(dh);
  }
  reader.parse(is);
}

代码示例来源:origin: plutext/docx4j

/**
 * Creates a new SAX XMLReader, with sensible defaults
 */
public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
  XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
  xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER);
  trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING, true);
  trySetXercesSecurityManager(xmlReader);
  return xmlReader;
}

代码示例来源:origin: log4j/log4j

/**
 * Creates a new <code>LoadXMLAction</code> instance.
 *
 * @param aParent the parent frame
 * @param aModel the model to add events to
 * @exception SAXException if an error occurs
 * @throws ParserConfigurationException if an error occurs
 */
LoadXMLAction(JFrame aParent, MyTableModel aModel)
  throws SAXException, ParserConfigurationException
{
  mParent = aParent;
  mHandler = new XMLFileHandler(aModel);
  mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
  mParser.setContentHandler(mHandler);
}

代码示例来源:origin: jersey/jersey

/**
 * Unmarshal a jaxb bean into a type of {@code resultClass} from the given {@code inputStream}.
 *
 * @param inputStream Input stream that contains input xml that should be processed.
 * @param saxParserFactory Sax parser factory for unmarshalling xml.
 * @param resultClass Class of the result bean into which the content of {@code inputStream} should be unmarshalled.
 * @param <T> Type of the result jaxb bean.
 * @return Unmarshalled jaxb bean.
 *
 * @throws JAXBException In case of jaxb problem.
 * @throws ParserConfigurationException In case of problem with parsing xml.
 * @throws SAXException In case of problem with parsing xml.
 */
public static <T> T unmarshall(InputStream inputStream, SAXParserFactory saxParserFactory,
                Class<T> resultClass) throws JAXBException, ParserConfigurationException, SAXException {
  JAXBContext jaxbContext = null;
  try {
    jaxbContext = JAXBContext.newInstance(resultClass);
  } catch (JAXBException ex) {
    throw new ProcessingException(LocalizationMessages.ERROR_WADL_JAXB_CONTEXT(), ex);
  }
  final SAXParser saxParser = saxParserFactory.newSAXParser();
  SAXSource source = new SAXSource(saxParser.getXMLReader(), new InputSource(inputStream));
  final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  final Object result = unmarshaller.unmarshal(source);
  return resultClass.cast(result);
}

代码示例来源:origin: jeremylong/DependencyCheck

final HintHandler handler = new HintHandler();
final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream13, schemaStream12, schemaStream11);
final XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setErrorHandler(new HintErrorHandler());
xmlReader.setContentHandler(handler);
try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
  final InputSource in = new InputSource(reader);
  xmlReader.parse(in);
  this.hintRules = handler.getHintRules();
  this.vendorDuplicatingHintRules = handler.getVendorDuplicatingHintRules();

代码示例来源:origin: languagetool-org/languagetool

/**
 * @param is stream with the XML rules
 * @param filename used only for verbose exception message - should refer to where the stream comes from
 */
public final List<AbstractPatternRule> getRules(InputStream is, String filename) throws IOException {
 try {
  PatternRuleHandler handler = new PatternRuleHandler(filename);
  handler.setRelaxedMode(relaxedMode);
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser saxParser = factory.newSAXParser();
  Tools.setPasswordAuthenticator();
  saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
  saxParser.parse(is, handler);
  return handler.getRules();
 } catch (Exception e) {
  throw new IOException("Cannot load or parse input stream of '" + filename + "'", e);
 }
}

代码示例来源:origin: stackoverflow.com

SAXParserFactory spf = SAXParserFactoryImpl.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
spf.setFeature("http://xml.org/sax/features/validation", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SAXParser sp = spf.newSAXParser() ;
Source src = new SAXSource ( sp.getXMLReader(), new InputSource( input.getAbsolutePath() ) ) ;
String resultFileName = input.getAbsolutePath().replaceAll(".xml$", ".cooked.xml" ) ;
Result result = new StreamResult( new File (resultFileName) ) ;
TransformerFactory tf = TransformerFactory.newInstance();
Source xsltSource = new StreamSource( new File ( COOKER_XSL ) );
xsl = tf.newTransformer( xsltSource ) ;
xsl.setParameter( "srcDocumentName", input.getName() ) ;
xsl.setParameter( "srcDocumentPath", input.getAbsolutePath() ) ;

xsl.transform(src, result );

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Creates a new SAX XMLReader, with sensible defaults
 */
public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
  XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
  xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER);
  trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING);
  trySetXercesSecurityManager(xmlReader);
  return xmlReader;
}

代码示例来源:origin: stackoverflow.com

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);

SAXParser parser = factory.newSAXParser();

XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new SimpleErrorHandler());
reader.parse(new InputSource(new FileReader ("document.xml")));

代码示例来源:origin: jersey/jersey

/**
 * Unmarshal a jaxb bean into a type of {@code resultClass} from the given {@code inputStream}.
 *
 * @param inputStream Input stream that contains input xml that should be processed.
 * @param saxParserFactory Sax parser factory for unmarshalling xml.
 * @param resultClass Class of the result bean into which the content of {@code inputStream} should be unmarshalled.
 * @param <T> Type of the result jaxb bean.
 * @return Unmarshalled jaxb bean.
 *
 * @throws JAXBException In case of jaxb problem.
 * @throws ParserConfigurationException In case of problem with parsing xml.
 * @throws SAXException In case of problem with parsing xml.
 */
public static <T> T unmarshall(InputStream inputStream, SAXParserFactory saxParserFactory,
                Class<T> resultClass) throws JAXBException, ParserConfigurationException, SAXException {
  JAXBContext jaxbContext = null;
  try {
    jaxbContext = JAXBContext.newInstance(resultClass);
  } catch (JAXBException ex) {
    throw new ProcessingException(LocalizationMessages.ERROR_WADL_JAXB_CONTEXT(), ex);
  }
  final SAXParser saxParser = saxParserFactory.newSAXParser();
  SAXSource source = new SAXSource(saxParser.getXMLReader(), new InputSource(inputStream));
  final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  final Object result = unmarshaller.unmarshal(source);
  return resultClass.cast(result);
}

代码示例来源:origin: jeremylong/DependencyCheck

final PomHandler handler = new PomHandler();
  final SAXParser saxParser = XmlUtils.buildSecureSaxParser();
  final XMLReader xmlReader = saxParser.getXMLReader();
  xmlReader.setContentHandler(handler);
  final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName();
  final Reader reader = new InputStreamReader(bomStream, charsetName);
  final InputSource in = new InputSource(reader);
  xmlReader.parse(in);
  return handler.getModel();
} catch (ParserConfigurationException | SAXException | FileNotFoundException ex) {

代码示例来源:origin: languagetool-org/languagetool

public final List<AbstractPatternRule> getRules(InputStream stream,
  Language textLanguage, Language motherTongue)
  throws ParserConfigurationException, SAXException, IOException {
 FalseFriendRuleHandler handler = new FalseFriendRuleHandler(
   textLanguage, motherTongue);
 SAXParserFactory factory = SAXParserFactory.newInstance();
 SAXParser saxParser = factory.newSAXParser();
 saxParser.getXMLReader().setFeature(
     "http://apache.org/xml/features/nonvalidating/load-external-dtd",
     false);
 saxParser.parse(stream, handler);
 List<AbstractPatternRule> rules = handler.getRules();
 // Add suggestions to each rule:
 ResourceBundle messages = ResourceBundle.getBundle(
     JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale());
 MessageFormat msgFormat = new MessageFormat(messages.getString("false_friend_suggestion"));
 for (AbstractPatternRule rule : rules) {
  List<String> suggestions = handler.getSuggestionMap().get(rule.getId());
  if (suggestions != null) {
   String[] msg = { formatSuggestions(suggestions) };
   rule.setMessage(rule.getMessage() + " " + msgFormat.format(msg));
  }
 }
 return rules;
}

代码示例来源:origin: groovy/groovy-core

/**
 * Creates a <code>XmlParser</code>.
 *
 * @param validating <code>true</code> if the parser should validate documents as they are parsed; false otherwise.
 * @param namespaceAware <code>true</code> if the parser should provide support for XML namespaces; <code>false</code> otherwise.
 * @param allowDocTypeDeclaration <code>true</code> if the parser should provide support for DOCTYPE declarations; <code>false</code> otherwise.
 * @throws ParserConfigurationException if no parser which satisfies the requested configuration can be created.
 * @throws SAXException for SAX errors.
 */
public XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) throws ParserConfigurationException, SAXException {
  SAXParserFactory factory = FactorySupport.createSaxParserFactory();
  factory.setNamespaceAware(namespaceAware);
  this.namespaceAware = namespaceAware;
  factory.setValidating(validating);
  setQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
  setQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration);
  reader = factory.newSAXParser().getXMLReader();
}

代码示例来源:origin: checkstyle/checkstyle

/**
 * Creates a new instance.
 * @param publicIdToResourceNameMap maps public IDs to DTD resource names
 * @throws SAXException if an error occurs
 * @throws ParserConfigurationException if an error occurs
 */
protected XmlLoader(Map<String, String> publicIdToResourceNameMap)
    throws SAXException, ParserConfigurationException {
  this.publicIdToResourceNameMap = new HashMap<>(publicIdToResourceNameMap);
  final SAXParserFactory factory = SAXParserFactory.newInstance();
  FeaturesForVerySecureJavaInstallations.addFeaturesForVerySecureJavaInstallations(factory);
  factory.setValidating(true);
  parser = factory.newSAXParser().getXMLReader();
  parser.setContentHandler(this);
  parser.setEntityResolver(this);
  parser.setErrorHandler(this);
}

相关文章