下面列出了javax.xml.parsers.SAXParserFactory#newInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private XMLReader createReader() throws SAXException {
try {
SAXParserFactory pfactory = SAXParserFactory.newInstance();
pfactory.setValidating(true);
pfactory.setNamespaceAware(true);
// Enable schema validation
SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd");
pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)}));
return pfactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException ex) {
throw new SAXException(ex);
}
}
@SuppressWarnings("rawtypes")
public void load(String file_input) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setXIncludeAware(true);
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
SAXReader reader = new SAXReader(parser.getXMLReader());
Document document = reader.read(file_input);
info = new Info();
info.load(document.selectSingleNode("//unittype/info"));
Enums enums = new Enums();
enums.load(document.selectSingleNode("//unittype/parameters/enums"));
parameters = new Parameters();
List parameters_nodes = document.selectNodes("//unittype/parameters");
for (Object parameters_node : parameters_nodes) {
Node parameter_node = (Node) parameters_node;
parameters.load(parameter_node, enums);
}
}
static public void main(String[] args) throws Exception {
if (args.length < 3) {
usage();
exit(-1);
}
String tmxFn = args[0];
String outputPrefixFn = args[1];
Set<String> langs = new HashSet<String>();
for (int i = 2; i < args.length; i++) {
langs.add(args[i]);
}
SAXParserFactory saxpf = SAXParserFactory.newInstance();
SAXParser saxParser = saxpf.newSAXParser();
saxParser.parse(new File(tmxFn), new TMXExtractor(outputPrefixFn, langs));
}
/**
* main routine for testing class implementation
*
* @param argv command line arguments
*/
public static void main(String[] argv)
{
try
{
// Create a new Parser
PvXMLHandler handler = new PvXMLHandler();
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new FileInputStream(argv[0]), handler);
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Setup readers.
*/
public void setupReaders() {
SAXParserFactory spf = catalogManager.useServicesMechanism() ?
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXCatalogReader saxReader = new SAXCatalogReader(spf);
saxReader.setCatalogParser(null, "XMLCatalog",
"com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");
saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
"catalog",
"com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");
addReader("application/xml", saxReader);
TR9401CatalogReader textReader = new TR9401CatalogReader();
addReader("text/plain", textReader);
}
/**
* Load and returns the list of files.
*
* @param in
* The config file stream.
* @return list of group entries
*
* @throws SAXException
* If a SAX error occurred.
* @throws IOException
* If an I/O error occurred.
*/
protected List<URI> load(final InputStream in) throws SAXException, IOException {
SAXParser saxParser;
// Use the default (non-validating) parser
final SAXParserFactory factory = SAXParserFactory.newInstance();
try {
saxParser = factory.newSAXParser();
} catch (final ParserConfigurationException ex) {
throw new SAXException(ex);
}
// Parse the XML
groups = new LinkedList<URI>();
saxParser.parse(in, this);
return groups;
}
public static void main(String[] args) throws Exception {
SAXParserFactory parserFactor = SAXParserFactory.newInstance();
SAXParser parser = parserFactor.newSAXParser();
SAXHandler handler = new SAXHandler();
parser.parse(ClassLoader.getSystemResourceAsStream("Student.xml"),
handler);
}
/**
* Load the user parameters
*/
@Override
public void readUserParameters(InputStream inputStream)
throws IOException, ParserConfigurationException, SAXException {
logger.info("Loading user parameters");
charBuffer = new StringBuffer();
// Parse the XML file
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(inputStream, this);
}
@Test(dataProvider = "TestSchemaFiles")
public void testWhitespacesCollapse(String schemaFile) throws Exception {
XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
XsomErrorHandler eh = new XsomErrorHandler();
parser.setErrorHandler(eh);
parser.parse(getSchemaFile(schemaFile));
if (eh.gotError) {
throw new RuntimeException("XSOM parser encountered error", eh.e);
}
}
public DefenseInboundAdapter(AdapterDefinition definition) throws ComponentException, ParserConfigurationException, SAXException, IOException
{
super(definition);
messageParser = new MessageParser(this);
saxFactory = SAXParserFactory.newInstance();
saxParser = saxFactory.newSAXParser();
}
public static void main(String[] args) throws Throwable {
System.setSecurityManager(new SecurityManager());
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
SAXParser sp = spf.newSAXParser();
// The following line shouldn't throw a
// java.security.AccessControlException.
sp.setProperty("foo", "bar");
} catch (SAXNotRecognizedException e) {
// Ignore this expected exception.
}
}
public void parse(DefaultHandler handler, InputStream is) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(is));
}
@Override
public void process(File source) throws Exception {
zeroTime = TIME_MILLIS_FORMAT.parse("00:00:00.000");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(source, this);
}
public Collection<DefaultSpell> load(URI uri) throws SAXException {
loadedSpells = new LinkedList<DefaultSpell>();
// Use the default (non-validating) parser
final SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Parse the input
final SAXParser saxParser = factory.newSAXParser();
final InputStream is = SpellXMLLoader.class.getResourceAsStream(uri.getPath());
if (is == null) {
throw new FileNotFoundException("cannot find resource '" + uri
+ "' in classpath");
}
try {
saxParser.parse(is, this);
} finally {
is.close();
}
} catch (final ParserConfigurationException t) {
logger.error(t);
} catch (final IOException e) {
logger.error(e);
throw new SAXException(e);
}
return loadedSpells;
}
static Map<String, Object> getCLDRBundle(String id) throws Exception {
Map<String, Object> bundle = cldrBundles.get(id);
if (bundle != null) {
return bundle;
}
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
SAXParser parser = factory.newSAXParser();
enableFileAccess(parser);
LDMLParseHandler handler = new LDMLParseHandler(id);
File file = new File(SOURCE_FILE_DIR + File.separator + id + ".xml");
if (!file.exists()) {
// Skip if the file doesn't exist.
return Collections.emptyMap();
}
info("..... main directory .....");
info("Reading file " + file);
parser.parse(file, handler);
bundle = handler.getData();
cldrBundles.put(id, bundle);
String country = getCountryCode(id);
if (country != null) {
bundle = handlerSuppl.getData(country);
if (bundle != null) {
//merge two maps into one map
Map<String, Object> temp = cldrBundles.remove(id);
bundle.putAll(temp);
cldrBundles.put(id, bundle);
}
}
return bundle;
}
/**
* Test the functionality of setNamespaceAware and
* isNamespaceAware methods.
*/
@Test
public void testNamespace02() {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
assertTrue(spf.isNamespaceAware());
}
/** Creates a new instance of XmlRecordInput */
public XmlRecordInput(InputStream in) {
try{
valList = new ArrayList<Value>();
DefaultHandler handler = new XMLParser(valList);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(in, handler);
vLen = valList.size();
vIdx = 0;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
protected SAXParserFactory initialValue() throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
return factory;
}
@Before
public void setUp() throws Exception {
factory = SAXParserFactory.newInstance();
saxParser = factory.newSAXParser();
}
public Element parse(InputStream stream) throws Exception {
Document doc = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// xxe protection
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);
factory.setNamespaceAware(true);
if (policy == ValidationPolicy.EVERYTHING) {
// use a slower parser that keeps location data
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer nullTransformer = transformerFactory.newTransformer();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
doc = docBuilder.newDocument();
DOMResult domResult = new DOMResult(doc);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
// xxe protection
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
XmlLocationAnnotator locationAnnotator = new XmlLocationAnnotator(xmlReader, doc);
InputSource inputSource = new InputSource(stream);
SAXSource saxSource = new SAXSource(locationAnnotator, inputSource);
nullTransformer.transform(saxSource, domResult);
} else {
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(stream);
}
} catch (Exception e) {
logError(0, 0, "(syntax)", IssueType.INVALID, e.getMessage(), IssueSeverity.FATAL);
doc = null;
}
if (doc == null)
return null;
else
return parse(doc);
}