Search in sources :

Example 86 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project head by mifos.

the class ChartOfAccountsConfig method load.

/**
     * Factory method which loads the Chart of Accounts configuration from the
     * given filename. Given XML filename will be validated against
     * {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
     *
     * @param chartOfAccountsXml
     *            a relative path to the Chart of Accounts configuration file.
     */
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
    ChartOfAccountsConfig instance = null;
    Document document = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = dbf.newDocumentBuilder();
        if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
            // default chart of accounts
            document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
        } else {
            // custom chart of accounts
            document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
        }
        // create a SchemaFactory capable of understanding XML schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load an XML schema
        ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
        Source schemaFile = null;
        if (schemaFileResource.exists()) {
            InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
            schemaFile = new StreamSource(in);
        } else {
            schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
        }
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance and validate document
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (IOException e) {
        throw new ConfigurationException(e);
    } catch (SAXException e) {
        throw new ConfigurationException(e);
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException(e);
    }
    instance = new ChartOfAccountsConfig();
    instance.coaDocument = document;
    return instance;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) Document(org.w3c.dom.Document) ClassPathResource(org.springframework.core.io.ClassPathResource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 87 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project head by mifos.

the class StateXMLParser method loadMapFromXml.

public Map<StateEntity, List<StateEntity>> loadMapFromXml(String filename, String configurationName) {
    Map<StateEntity, List<StateEntity>> transitionMap = new HashMap<StateEntity, List<StateEntity>>();
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        // Specify our own schema - this overrides the schemaLocation in the
        // xml file
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "StateMachine.xsd");
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(null);
        Document document = builder.parse(MifosResourceUtil.getClassPathResourceAsStream(filename));
        Node mapToprocess = null;
        /*
             * String configurationName = ""; if
             * (stateConfiguration=="configuration1") configurationName = "1";
             * else configurationName = "2";
             */
        NodeList configMaps = document.getElementsByTagName("stateConfiguration");
        for (int m = 0; m < configMaps.getLength(); m++) {
            Node stateConfiguration = configMaps.item(m);
            if (configurationName.equals(stateConfiguration.getAttributes().getNamedItem("configurationName").getNodeValue())) {
                mapToprocess = stateConfiguration;
            }
        }
        // NodeList stateList = firstChild.getChildNodes();
        NodeList stateList = mapToprocess.getChildNodes();
        for (int i = 0; i < stateList.getLength(); i++) {
            // each state has state id and possiblestates as childern
            Node state = stateList.item(i);
            // iterate for each child of state
            NodeList stateInfoList = state.getChildNodes();
            StateEntity currentState = null;
            List<StateEntity> currntPossibleStates = new ArrayList<StateEntity>();
            for (int j = 0; j < stateInfoList.getLength(); j++) {
                Node info = stateInfoList.item(j);
                if ("stateid".equals(info.getLocalName())) {
                    Element ele = (Element) info;
                    currentState = new StateEntity(Short.valueOf(((Text) ele.getFirstChild()).getData()));
                }
                if ("possiblestates".equals(info.getLocalName())) {
                    // get all the childern
                    NodeList allStates = info.getChildNodes();
                    currntPossibleStates = new ArrayList<StateEntity>();
                    for (int k = 0; k < allStates.getLength(); k++) {
                        Node infoState = allStates.item(k);
                        NodeList eachPossiblechild = infoState.getChildNodes();
                        for (int l = 0; l < eachPossiblechild.getLength(); l++) {
                            Node eachPossiblechildelement = eachPossiblechild.item(l);
                            if ("stateid".equals(eachPossiblechildelement.getLocalName())) {
                                Element element = (Element) eachPossiblechildelement;
                                Short possibleTrantionId = Short.valueOf(((Text) element.getFirstChild()).getData());
                                currntPossibleStates.add(new StateEntity(possibleTrantionId));
                            }
                        }
                    }
                }
            }
            if (currentState != null) {
                transitionMap.put(currentState, currntPossibleStates);
            }
        }
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (SAXParseException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    }
    return transitionMap;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) StateEntity(org.mifos.application.master.business.StateEntity) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 88 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project openblocks by mikaelhg.

the class WorkspaceController method setLangDefStream.

/**
     * Sets language definition file from the given input stream
     * @param in input stream to read
     */
public void setLangDefStream(InputStream in) {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder;
    final Document doc;
    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(in);
        langDefRoot = doc.getDocumentElement();
        langDefDirty = true;
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 89 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project openblocks by mikaelhg.

the class WorkspaceController method loadProject.

/**
     * Loads the programming project specified in the projectContents String,
     * which is associated with the language definition file contained in the
     * specified langDefContents.  All the blocks contained in projectContents
     * must have an associted block genus defined in langDefContents.
     *
     * If the langDefContents have any workspace settings such as pages or
     * drawers and projectContents has workspace settings as well, the
     * workspace settings within the projectContents will override the
     * workspace settings in langDefContents.
     *
     * NOTE: The language definition contained in langDefContents does
     * not replace the default language definition file set by: setLangDefFilePath() or
     * setLangDefFile().
     *
     * @param projectContents
     * @param langDefContents String XML that defines the language of
     * projectContents
     */
public void loadProject(String projectContents, String langDefContents) {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder;
    final Document projectDoc;
    final Document langDoc;
    try {
        builder = factory.newDocumentBuilder();
        projectDoc = builder.parse(new InputSource(new StringReader(projectContents)));
        final Element projectRoot = projectDoc.getDocumentElement();
        langDoc = builder.parse(new InputSource(new StringReader(projectContents)));
        final Element langRoot = langDoc.getDocumentElement();
        if (workspaceLoaded) {
            resetWorkspace();
        }
        if (langDefContents == null) {
            loadBlockLanguage(langDefRoot);
        } else {
            loadBlockLanguage(langRoot);
        }
        workspace.loadWorkspaceFrom(projectRoot, langRoot);
        workspaceLoaded = true;
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 90 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project head by mifos.

the class ChartOfAccountsConfigIntegrationTest method testTraverse.

@Test
public void testTraverse() throws Exception {
    String invalid = "<GLAccount code=\"11100\" name=\"Petty Cash Accounts\">" + "<GLAccount code=\"AB CD\" name=\"Cash 1\" />" + "<GLAccount code=\"11102\" name=\"Cash 2\" />" + "</GLAccount>";
    ByteArrayInputStream bstr = new ByteArrayInputStream(invalid.getBytes("UTF-8"));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = dbf.newDocumentBuilder();
    Document document = parser.parse(bstr);
    Set<GLAccount> accounts = ChartOfAccountsConfig.traverse(document.getFirstChild(), null);
    Assert.assertEquals(3, accounts.size());
    GLAccount expected = new GLAccount();
    expected.glCode = "AB CD";
    expected.name = "Cash 1";
    expected.parentGlCode = "11100";
    Assert.assertTrue(accounts.contains(expected));
    expected = new GLAccount();
    expected.glCode = "11102";
    expected.name = "Cash 2";
    expected.parentGlCode = "11100";
    Assert.assertTrue(accounts.contains(expected));
    GLAccount unExpected = new GLAccount();
    unExpected.glCode = "11102";
    unExpected.name = "Cash 3";
    unExpected.parentGlCode = "11100";
    Assert.assertFalse(accounts.contains(unExpected));
    unExpected = new GLAccount();
    unExpected.glCode = "11199";
    unExpected.name = "Cash 2";
    Assert.assertFalse(accounts.contains(unExpected));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)759 DocumentBuilder (javax.xml.parsers.DocumentBuilder)622 Document (org.w3c.dom.Document)526 Element (org.w3c.dom.Element)244 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)232 NodeList (org.w3c.dom.NodeList)209 IOException (java.io.IOException)197 InputSource (org.xml.sax.InputSource)193 SAXException (org.xml.sax.SAXException)173 Node (org.w3c.dom.Node)145 StringReader (java.io.StringReader)142 Test (org.junit.Test)117 File (java.io.File)86 DOMSource (javax.xml.transform.dom.DOMSource)77 ByteArrayInputStream (java.io.ByteArrayInputStream)72 InputStream (java.io.InputStream)63 StreamResult (javax.xml.transform.stream.StreamResult)50 ArrayList (java.util.ArrayList)47 SAXParseException (org.xml.sax.SAXParseException)46 Transformer (javax.xml.transform.Transformer)44