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;
}
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;
}
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);
}
}
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);
}
}
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));
}
Aggregations