Search in sources :

Example 11 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class DomParser method parse.

/**
 * Parses the XML. It returns a Map.
 * @param uri The location of the content to be parsed.
 * @param validate if the XML is to be validated while parsing.
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies the configuration requested.
 * @throws SAXException If any parse errors occur.
 * @throws IOException If any IO errors occur.
 * @return The Map.
 */
public static Map parse(String uri, boolean validate) throws ParserConfigurationException, SAXException, IOException {
    // Create a factory object for creating DOM parsers
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Specifies that the parser produced by this factory will validate documents as they are parsed.
    factory.setValidating(validate);
    // Now use the factory to create a DOM parser
    DocumentBuilder parser = factory.newDocumentBuilder();
    // Specifies the EntityResolver to resolve DTD used in XML documents
    parser.setEntityResolver(new DefaultEntityResolver());
    // Specifies the ErrorHandler to handle warning/error/fatalError conditions
    parser.setErrorHandler(new DefaultErrorHandler());
    Document document = parser.parse(uri);
    return parseXML(document);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler)

Example 12 with DefaultEntityResolver

use of org.jaffa.util.DefaultEntityResolver in project jaffa-framework by jaffa-projects.

the class PatternGenerator method generateSourceFiles.

/**
 * Create source files based on the contents of an XML file
 * @param tempFileName the temporary file to be parsed, whose contents will
 *                     determine which files get created.
 * @param context contains information needed for processing, including
 *                information from WebMacro.properties and PatternGenerator
 *                properties.
 * @throws PatternGeneratorException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws WebMacroException
 * @throws SourceDecomposerException
 */
private void generateSourceFiles(String tempFileName, Context context) throws PatternGeneratorException, ParserConfigurationException, SAXException, IOException, WebMacroException, SourceDecomposerException {
    // parse the tempFileName.. Force XML validation
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    DocumentBuilder parser = factory.newDocumentBuilder();
    parser.setEntityResolver(new DefaultEntityResolver());
    parser.setErrorHandler(new DefaultErrorHandler());
    Document document = parser.parse(tempFileName);
    // check for Prerequisites
    // @todo : this currently only supports java files..
    // Need to enhance this logic to support other files too !!
    String preReqErrLog = null;
    Node preReq = document.getElementsByTagName(XML_PREREQUESITES).item(0);
    NodeList classes = preReq.getChildNodes();
    if (classes != null) {
        for (int i = 0; i < classes.getLength(); i++) {
            Node clazz = classes.item(i);
            if (clazz.getNodeType() == Node.ELEMENT_NODE) {
                String requiredClassName = XmlHelper.getTextTrim(clazz);
                try {
                    Class.forName(requiredClassName);
                } catch (ClassNotFoundException e) {
                    if (preReqErrLog == null) {
                        preReqErrLog = "";
                    }
                    preReqErrLog += "Error: PreRequesite Class " + requiredClassName + " not found\n";
                }
            }
        }
    }
    if (preReqErrLog != null) {
        writeMessage(m_auditFile, preReqErrLog, true);
        throw new PatternGeneratorException(preReqErrLog);
    }
    processComponents(context, document);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler)

Aggregations

DefaultEntityResolver (org.jaffa.util.DefaultEntityResolver)12 DefaultErrorHandler (org.jaffa.util.DefaultErrorHandler)12 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 IOException (java.io.IOException)6 InputSource (org.xml.sax.InputSource)6 SAXException (org.xml.sax.SAXException)6 Document (org.w3c.dom.Document)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 StringReader (java.io.StringReader)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 XMLReader (org.xml.sax.XMLReader)3 FileNotFoundException (java.io.FileNotFoundException)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 StringTokenizer (java.util.StringTokenizer)1