use of javax.xml.parsers.SAXParserFactory in project cloudstack by apache.
the class DatabaseConfig method doConfig.
@DB
protected void doConfig() {
try {
final File configFile = new File(_configFileName);
SAXParserFactory spfactory = SAXParserFactory.newInstance();
final SAXParser saxParser = spfactory.newSAXParser();
final DbConfigXMLHandler handler = new DbConfigXMLHandler();
handler.setParent(this);
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<Exception>() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
// Save user configured values for all fields
saxParser.parse(configFile, handler);
// Save default values for configuration fields
saveVMTemplate();
saveRootDomain();
saveDefaultConfiguations();
}
});
// Check pod CIDRs against each other, and against the guest ip network/netmask
pzc.checkAllPodCidrSubnets();
} catch (Exception ex) {
System.out.print("ERROR IS" + ex);
s_logger.error("error", ex);
}
}
use of javax.xml.parsers.SAXParserFactory in project geode by apache.
the class CacheXmlParser method parse.
////////////////////// Static Methods //////////////////////
/**
* Parses XML data and from it creates an instance of <code>CacheXmlParser</code> that can be used
* to {@link #create}the {@link Cache}, etc.
*
* @param is the <code>InputStream</code> of XML to be parsed
*
* @return a <code>CacheXmlParser</code>, typically used to create a cache from the parsed XML
*
* @throws CacheXmlException Something went wrong while parsing the XML
*
* @since GemFire 4.0
*
*/
public static CacheXmlParser parse(InputStream is) {
/**
* The API doc http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html for the SAX
* InputSource says: "... standard processing of both byte and character streams is to close
* them on as part of end-of-parse cleanup, so applications should not attempt to re-use such
* streams after they have been handed to a parser."
*
* In order to block the parser from closing the stream, we wrap the InputStream in a filter,
* i.e., UnclosableInputStream, whose close() function does nothing.
*
*/
class UnclosableInputStream extends BufferedInputStream {
public UnclosableInputStream(InputStream stream) {
super(stream);
}
@Override
public void close() {
}
}
CacheXmlParser handler = new CacheXmlParser();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true);
factory.setValidating(true);
factory.setNamespaceAware(true);
UnclosableInputStream bis = new UnclosableInputStream(is);
try {
SAXParser parser = factory.newSAXParser();
// Parser always reads one buffer plus a little extra worth before
// determining that the DTD is there. Setting mark twice the parser
// buffer size.
bis.mark((Integer) parser.getProperty(BUFFER_SIZE) * 2);
parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
parser.parse(bis, new DefaultHandlerDelegate(handler));
} catch (CacheXmlException e) {
if (null != e.getCause() && e.getCause().getMessage().contains(DISALLOW_DOCTYPE_DECL_FEATURE)) {
// Not schema based document, try dtd.
bis.reset();
factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, false);
SAXParser parser = factory.newSAXParser();
parser.parse(bis, new DefaultHandlerDelegate(handler));
} else {
throw e;
}
}
return handler;
} catch (Exception ex) {
if (ex instanceof CacheXmlException) {
while (true) /* ex instanceof CacheXmlException */
{
Throwable cause = ex.getCause();
if (!(cause instanceof CacheXmlException)) {
break;
} else {
ex = (CacheXmlException) cause;
}
}
throw (CacheXmlException) ex;
} else if (ex instanceof SAXException) {
// Silly JDK 1.4.2 XML parser wraps RunTime exceptions in a
// SAXException. Pshaw!
SAXException sax = (SAXException) ex;
Exception cause = sax.getException();
if (cause instanceof CacheXmlException) {
while (true) /* cause instanceof CacheXmlException */
{
Throwable cause2 = cause.getCause();
if (!(cause2 instanceof CacheXmlException)) {
break;
} else {
cause = (CacheXmlException) cause2;
}
}
throw (CacheXmlException) cause;
}
}
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_WHILE_PARSING_XML.toLocalizedString(), ex);
}
}
use of javax.xml.parsers.SAXParserFactory in project jackrabbit by apache.
the class ParsingContentHandler method parse.
/**
* Utility method that parses the given input stream using this handler.
* The parser is namespace-aware and will not resolve external entity
* references.
*
* @param in XML input stream
* @throws IOException if an I/O error occurs
* @throws SAXException if an XML parsing error occurs
*/
public void parse(InputStream in) throws IOException, SAXException {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.newSAXParser().parse(new InputSource(in), this);
} catch (ParserConfigurationException e) {
throw new SAXException("SAX parser configuration error", e);
}
}
use of javax.xml.parsers.SAXParserFactory in project lucene-solr by apache.
the class PatternParser method createParser.
/**
* Creates a SAX parser using JAXP
*
* @return the created SAX parser
*/
static XMLReader createParser() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
return factory.newSAXParser().getXMLReader();
} catch (Exception e) {
throw new RuntimeException("Couldn't create XMLReader: " + e.getMessage());
}
}
use of javax.xml.parsers.SAXParserFactory in project karaf by apache.
the class XmlUtils method xmlReader.
public static XMLReader xmlReader() throws ParserConfigurationException, SAXException {
SAXParserFactory spf = SAX_PARSER_FACTORY.get();
if (spf == null) {
spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAX_PARSER_FACTORY.set(spf);
}
return spf.newSAXParser().getXMLReader();
}
Aggregations