下面列出了怎么用javax.xml.parsers.FactoryConfigurationError的API类实例代码及写法,或者点击链接到github查看源代码。
public void testFontConfigParser( ) throws IOException,
FactoryConfigurationError, ParserConfigurationException,
SAXException
{
fontMappingManager = getFontMappingManager( "fontsConfigParser.xml" );
Map fontAliases = fontMappingManager.getFontAliases( );
assertEquals( 1, fontAliases.size( ) );
assertEquals( "Times-Roman", fontAliases.get( "test alias" ) );
Map compositeFonts = fontMappingManager.getCompositeFonts( );
assertEquals( 1, compositeFonts.size( ) );
CompositeFont testFont = (CompositeFont) compositeFonts
.get( "test font" );
assertEquals( "Symbol", testFont.getUsedFont( (char) 0 ) );
assertEquals( "Symbol", testFont.getUsedFont( (char) 0x7F ) );
assertEquals( "Helvetica", testFont.getDefaultFont( ) );
}
/**
* Reads itself from XML format
* New added - for Sandwich project (XML format instead of serialization) .
* @param is input stream (which is parsed)
* @return Table
*/
public void readFromXML(InputStream is, boolean validate)
throws SAXException {
StringBuffer fileName = new StringBuffer();
ElementHandler[] elmKeyService = { parseFirstLevel(), parseSecondLevel(fileName), parseThirdLevel(fileName) }; //
String dtd = getClass().getClassLoader().getResource(DTD_PATH).toExternalForm();
InnerParser parser = new InnerParser(PUBLIC_ID, dtd, elmKeyService);
try {
parser.parseXML(is, validate);
} catch (Exception ioe) {
throw (SAXException) ExternalUtil.copyAnnotation(
new SAXException(NbBundle.getMessage(DefaultAttributes.class, "EXC_DefAttrReadErr")), ioe
);
} catch (FactoryConfigurationError fce) {
// ??? see http://openide.netbeans.org/servlets/ReadMsg?msgId=340881&listName=dev
throw (SAXException) ExternalUtil.copyAnnotation(
new SAXException(NbBundle.getMessage(DefaultAttributes.class, "EXC_DefAttrReadErr")), fce
);
}
}
@Disabled
@Test
public void duplicateMessageBodiesTest2()
throws FactoryConfigurationError, XmlPullParserException, IOException, Exception {
String defaultLanguage = Stanza.getDefaultLanguage();
String otherLanguage = determineNonDefaultLanguage();
// message has no language, first body no language, second body no language
String control = XMLBuilder.create("message")
.namespace(StreamOpen.CLIENT_NAMESPACE)
.a("xml:lang", defaultLanguage)
.a("from", "[email protected]/orchard")
.a("to", "[email protected]/balcony")
.a("id", "zid615d9")
.a("type", "chat")
.e("body")
.t(defaultLanguage)
.up()
.e("body")
.t(otherLanguage)
.asString(outputProperties);
PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
}
/** Creates a SAX parser.
*
* <p>See {@link #parse} for hints on setting an entity resolver.
*
* @param validate if true, a validating parser is returned
* @param namespaceAware if true, a namespace aware parser is returned
*
* @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
* @throws SAXException if a parser fulfilling given parameters can not be created
*
* @return XMLReader configured according to passed parameters
*/
public static synchronized XMLReader createXMLReader(boolean validate, boolean namespaceAware)
throws SAXException {
SAXParserFactory factory = saxes[validate ? 0 : 1][namespaceAware ? 0 : 1];
if (factory == null) {
try {
factory = SAXParserFactory.newInstance();
} catch (FactoryConfigurationError err) {
Exceptions.attachMessage(
err,
"Info about thread context classloader: " + // NOI18N
Thread.currentThread().getContextClassLoader()
);
throw err;
}
factory.setValidating(validate);
factory.setNamespaceAware(namespaceAware);
saxes[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory;
}
try {
return factory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException ex) {
throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N
}
}
/**
* Parses the given XML contents and returns a DOM {@link Document} for it.
*/
protected Document parseXmlDocument(String contents)
throws FactoryConfigurationError, ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
builderFactory.setCoalescing(true);
// TODO: Somehow do XML validation on Android
// builderFactory.setValidating(true);
builderFactory.setNamespaceAware(true);
builderFactory.setIgnoringComments(true);
builderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(
new InputSource(new StringReader(contents)));
return doc;
}
/**
* Register DOM Parser Factory Services with the framework.
*
* @param parserFactoryClassNames - a {@code List} of {@code String} objects
* containing the names of the parser Factory Classes
* @throws FactoryConfigurationError if thrown from {@code getFactory}
*/
private void registerDOMParsers(List parserFactoryClassNames) throws FactoryConfigurationError {
Iterator e = parserFactoryClassNames.iterator();
int index = 0;
while (e.hasNext()) {
String parserFactoryClassName = (String) e.next();
// create a dom parser factory just to get it's default
// properties. It will never be used since
// this class will operate as a service factory and give each
// service requestor it's own DocumentBuilderFactory
DocumentBuilderFactory factory = (DocumentBuilderFactory) getFactory(parserFactoryClassName);
Hashtable properties = new Hashtable(7);
// figure out the default properties of the parser
setDefaultDOMProperties(factory, properties, index);
// store the parser factory class name in the properties so that
// it can be retrieved when getService is called
// to return a parser factory
properties.put(FACTORYNAMEKEY, parserFactoryClassName);
// register the factory as a service
context.registerService(DOMFACTORYNAME, this, properties);
index++;
}
}
@Test
public void parseElementMultipleNamespace()
throws ParserConfigurationException,
FactoryConfigurationError, XmlPullParserException,
IOException, TransformerException, SAXException {
// @formatter:off
final String stanza = XMLBuilder.create("outer", "outerNamespace").a("outerAttribute", "outerValue")
.element("inner", "innerNamespace").a("innerAttribute", "innerValue")
.element("innermost")
.t("some text")
.asString();
// @formatter:on
XmlPullParser parser = TestUtils.getParser(stanza, "outer");
CharSequence result = PacketParserUtils.parseElement(parser, true);
assertXmlSimilar(stanza, result.toString());
}
public static OMElement parse_xml(InputStream is) throws FactoryConfigurationError, XdsInternalException {
// create the parser
XMLStreamReader parser=null;
try {
parser = XMLInputFactory.newInstance().createXMLStreamReader(is);
} catch (XMLStreamException e) {
throw new XdsInternalException("Could not create XMLStreamReader from InputStream");
}
// create the builder
StAXOMBuilder builder = new StAXOMBuilder(parser);
// get the root element (in this case the envelope)
OMElement documentElement = builder.getDocumentElement();
if (documentElement == null)
throw new XdsInternalException("No document element");
return documentElement;
}
/**
Configure log4j by reading in a log4j.dtd compliant XML
configuration file.
*/
public
void doConfigure(final InputStream inputStream, LoggerRepository repository)
throws FactoryConfigurationError {
ParseAction action = new ParseAction() {
public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
InputSource inputSource = new InputSource(inputStream);
inputSource.setSystemId("dummy://log4j.dtd");
return parser.parse(inputSource);
}
public String toString() {
return "input stream [" + inputStream.toString() + "]";
}
};
doConfigure(action, repository);
}
/**
Configure log4j by reading in a log4j.dtd compliant XML
configuration file.
*/
public
void doConfigure(final Reader reader, LoggerRepository repository)
throws FactoryConfigurationError {
ParseAction action = new ParseAction() {
public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
InputSource inputSource = new InputSource(reader);
inputSource.setSystemId("dummy://log4j.dtd");
return parser.parse(inputSource);
}
public String toString() {
return "reader [" + reader.toString() + "]";
}
};
doConfigure(action, repository);
}
private void reloadConfig() throws MalformedURLException, FactoryConfigurationError, BException {
String blurHome = System.getenv("BLUR_HOME");
if (blurHome != null) {
File blurHomeFile = new File(blurHome);
if (blurHomeFile.exists()) {
File log4jFile = new File(new File(blurHomeFile, "conf"), "log4j.xml");
if (log4jFile.exists()) {
LOG.info("Reseting log4j config from [{0}]", log4jFile);
LogManager.resetConfiguration();
DOMConfigurator.configure(log4jFile.toURI().toURL());
return;
}
}
}
URL url = TableAdmin.class.getResource("/log4j.xml");
if (url != null) {
LOG.info("Reseting log4j config from classpath resource [{0}]", url);
LogManager.resetConfiguration();
DOMConfigurator.configure(url);
return;
}
throw new BException("Could not locate log4j file to reload, doing nothing.");
}
/**
* Configure log4j by reading in a log4j.dtd compliant XML configuration file.
*/
public void doConfigure(final InputStream inputStream, final LoggerRepository repository)
throws FactoryConfigurationError {
final ParseAction action = new ParseAction() {
public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
final InputSource inputSource = new InputSource(inputStream);
inputSource.setSystemId("dummy://log4j.dtd");
return parser.parse(inputSource);
}
public String toString() {
//noinspection ObjectToString
return "input stream [" + inputStream + ']';
}
};
doConfigure(action, repository);
}
/**
* Configure log4j by reading in a log4j.dtd compliant XML configuration file.
*/
public void doConfigure(final Reader reader, final LoggerRepository repository)
throws FactoryConfigurationError {
final ParseAction action = new ParseAction() {
public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
final InputSource inputSource = new InputSource(reader);
inputSource.setSystemId("dummy://log4j.dtd");
return parser.parse(inputSource);
}
public String toString() {
//noinspection ObjectToString
return "reader [" + reader + ']';
}
};
doConfigure(action, repository);
}
/**
* Configure log4j by reading in a log4j.dtd compliant XML configuration file.
*/
private void doConfigure(final InputSource inputSource, final LoggerRepository repository)
throws FactoryConfigurationError {
if (inputSource.getSystemId() == null) {
inputSource.setSystemId("dummy://log4j.dtd");
}
final ParseAction action = new ParseAction() {
public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
return parser.parse(inputSource);
}
public String toString() {
//noinspection ObjectToString
return "input source [" + inputSource + ']';
}
};
doConfigure(action, repository);
}
/**
* Register SAX Parser Factory Services with the framework.
*
* @param parserFactoryClassNames - a <code>List</code> of
* <code>String</code> objects containing the names of the parser
* Factory Classes
* @throws FactoryConfigurationError if thrown from <code>getFactory</code>
*/
private void registerSAXParsers(List parserFactoryClassNames)
throws FactoryConfigurationError {
Iterator e = parserFactoryClassNames.iterator();
int index = 0;
while (e.hasNext()) {
String parserFactoryClassName = (String) e.next();
// create a sax parser factory just to get it's default
// properties. It will never be used since
// this class will operate as a service factory and give each
// service requestor it's own SaxParserFactory
SAXParserFactory factory = (SAXParserFactory) getFactory(parserFactoryClassName);
Hashtable properties = new Hashtable(7);
// figure out the default properties of the parser
setDefaultSAXProperties(factory, properties, index);
// store the parser factory class name in the properties so that
// it can be retrieved when getService is called
// to return a parser factory
properties.put(FACTORYNAMEKEY, parserFactoryClassName);
// register the factory as a service
context.registerService(SAXFACTORYNAME, this, properties);
index++;
}
}
/**
* Register DOM Parser Factory Services with the framework.
*
* @param parserFactoryClassNames - a <code>List</code> of
* <code>String</code> objects containing the names of the parser
* Factory Classes
* @throws FactoryConfigurationError if thrown from <code>getFactory</code>
*/
private void registerDOMParsers(List parserFactoryClassNames)
throws FactoryConfigurationError {
Iterator e = parserFactoryClassNames.iterator();
int index = 0;
while (e.hasNext()) {
String parserFactoryClassName = (String) e.next();
// create a dom parser factory just to get it's default
// properties. It will never be used since
// this class will operate as a service factory and give each
// service requestor it's own DocumentBuilderFactory
DocumentBuilderFactory factory = (DocumentBuilderFactory) getFactory(parserFactoryClassName);
Hashtable properties = new Hashtable(7);
// figure out the default properties of the parser
setDefaultDOMProperties(factory, properties, index);
// store the parser factory class name in the properties so that
// it can be retrieved when getService is called
// to return a parser factory
properties.put(FACTORYNAMEKEY, parserFactoryClassName);
// register the factory as a service
context.registerService(DOMFACTORYNAME, this, properties);
index++;
}
}
/**
* Register SAX Parser Factory Services with the framework.
*
* @param parserFactoryClassNames - a {@code List} of {@code String} objects
* containing the names of the parser Factory Classes
* @throws FactoryConfigurationError if thrown from {@code getFactory}
*/
private void registerSAXParsers(List parserFactoryClassNames) throws FactoryConfigurationError {
Iterator e = parserFactoryClassNames.iterator();
int index = 0;
while (e.hasNext()) {
String parserFactoryClassName = (String) e.next();
// create a sax parser factory just to get it's default
// properties. It will never be used since
// this class will operate as a service factory and give each
// service requestor it's own SaxParserFactory
SAXParserFactory factory = (SAXParserFactory) getFactory(parserFactoryClassName);
Hashtable properties = new Hashtable(7);
// figure out the default properties of the parser
setDefaultSAXProperties(factory, properties, index);
// store the parser factory class name in the properties so that
// it can be retrieved when getService is called
// to return a parser factory
properties.put(FACTORYNAMEKEY, parserFactoryClassName);
// register the factory as a service
context.registerService(SAXFACTORYNAME, this, properties);
index++;
}
}
/**
* Register DOM Parser Factory Services with the framework.
*
* @param parserFactoryClassNames - a {@code List} of {@code String} objects
* containing the names of the parser Factory Classes
* @throws FactoryConfigurationError if thrown from {@code getFactory}
*/
private void registerDOMParsers(List parserFactoryClassNames) throws FactoryConfigurationError {
Iterator e = parserFactoryClassNames.iterator();
int index = 0;
while (e.hasNext()) {
String parserFactoryClassName = (String) e.next();
// create a dom parser factory just to get it's default
// properties. It will never be used since
// this class will operate as a service factory and give each
// service requestor it's own DocumentBuilderFactory
DocumentBuilderFactory factory = (DocumentBuilderFactory) getFactory(parserFactoryClassName);
Hashtable properties = new Hashtable(7);
// figure out the default properties of the parser
setDefaultDOMProperties(factory, properties, index);
// store the parser factory class name in the properties so that
// it can be retrieved when getService is called
// to return a parser factory
properties.put(FACTORYNAMEKEY, parserFactoryClassName);
// register the factory as a service
context.registerService(DOMFACTORYNAME, this, properties);
index++;
}
}
/**
* Creates a new DOM document.
*/
public static Document createDom() {
synchronized (DOMUtil.class) {
if (db == null) {
try {
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory();
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/**
* Creates a new DOM document.
*/
static Document createDom(boolean disableSecurityProcessing) {
synchronized(JAXBContextImpl.class) {
if(db==null) {
try {
DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(disableSecurityProcessing);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// impossible
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
public List<Process> addProcessFromXml(final Resource resource) throws IOException {
Reader reader = resource.getReader();
KnowledgeBuilderConfigurationImpl configuration = knowledgeBuilder.getBuilderConfiguration();
XmlProcessReader xmlReader = new XmlProcessReader(configuration.getSemanticModules(), knowledgeBuilder.getRootClassLoader());
List<Process> processes = null;
try {
String portRuleFlow = System.getProperty("drools.ruleflow.port", "false");
Reader portedReader = null;
if (portRuleFlow.equalsIgnoreCase("true")) {
portedReader = portToCurrentVersion(reader);
} else {
portedReader = reader;
}
processes = xmlReader.read(portedReader);
if (processes != null) {
// it is possible an xml file could not be parsed, so we need to
// stop null pointers
for (Process process : processes) {
buildProcess(process, resource);
xmlReader.getProcessBuildData().onBuildComplete(process);
}
} else {
// @TODO could we maybe add something a bit more informative about what is wrong with the XML ?
this.errors.add(new ProcessLoadError(resource, "unable to parse xml", null));
}
} catch (FactoryConfigurationError e1) {
this.errors.add(new ProcessLoadError(resource, "FactoryConfigurationError ", e1.getException()));
} catch (Exception e2) {
e2.printStackTrace();
this.errors.add(new ProcessLoadError(resource, "unable to parse xml", e2));
} finally {
reader.close();
}
return processes;
}
/**
* Creates a new DOM document.
*/
public static Document createDom() {
synchronized (DOMUtil.class) {
if (db == null) {
try {
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory();
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/**
* Creates a new DOM document.
*/
static Document createDom(boolean disableSecurityProcessing) {
synchronized(JAXBContextImpl.class) {
if(db==null) {
try {
DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(disableSecurityProcessing);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// impossible
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/**
* Creates a new DOM document.
*/
public static Document createDom() {
synchronized (DOMUtil.class) {
if (db == null) {
try {
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory();
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/**
* Creates a new DOM document.
*/
static Document createDom(boolean disableSecurityProcessing) {
synchronized(JAXBContextImpl.class) {
if(db==null) {
try {
DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(disableSecurityProcessing);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// impossible
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/** Starts parsing document - if you have document`s InputStream
* @param validate
* @param is document`s InputStream
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException */
public void parseXML(InputStream is, boolean validate)
throws IOException, SAXException, ParserConfigurationException, FactoryConfigurationError {
InputSource iSource = new InputSource(is);
XMLReader parser = getParser(validate);
parser.parse(iSource);
}
public void test_getMessage() {
assertNull(new FactoryConfigurationError().getMessage());
assertEquals("msg1",
new FactoryConfigurationError("msg1").getMessage());
assertEquals(new Exception("msg2").toString(),
new FactoryConfigurationError(
new Exception("msg2")).getMessage());
assertEquals(new NullPointerException().toString(),
new FactoryConfigurationError(
new NullPointerException()).getMessage());
}
/**
* Creates a new DOM document.
*/
public static Document createDom() {
synchronized (DOMUtil.class) {
if (db == null) {
try {
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory();
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/**
* Creates a new DOM document.
*/
static Document createDom(boolean disableSecurityProcessing) {
synchronized(JAXBContextImpl.class) {
if(db==null) {
try {
DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(disableSecurityProcessing);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// impossible
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
/**
* Creates a new DOM document.
*/
public static Document createDom() {
synchronized (DOMUtil.class) {
if (db == null) {
try {
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(true);
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}