use of org.w3c.dom.NodeList in project XobotOS by xamarin.
the class XMLParser method loadNode.
private static void loadNode(Preferences prefs, Element node) {
// load preferences
NodeList children = selectNodeList(node, "node");
NodeList entries = selectNodeList(node, "map/entry");
int childNumber = children.getLength();
Preferences[] prefChildren = new Preferences[childNumber];
int entryNumber = entries.getLength();
synchronized (((AbstractPreferences) prefs).lock) {
if (((AbstractPreferences) prefs).isRemoved()) {
return;
}
for (int i = 0; i < entryNumber; i++) {
Element entry = (Element) entries.item(i);
String key = entry.getAttribute("key");
String value = entry.getAttribute("value");
prefs.put(key, value);
}
// get children preferences node
for (int i = 0; i < childNumber; i++) {
Element child = (Element) children.item(i);
String name = child.getAttribute("name");
prefChildren[i] = prefs.node(name);
}
}
// load children nodes after unlock
for (int i = 0; i < childNumber; i++) {
loadNode(prefChildren[i], (Element) children.item(i));
}
}
use of org.w3c.dom.NodeList in project hibernate-orm by hibernate.
the class XmlHelper method getElementContent.
/**
* Get the content of the given element.
*
* @param element The element to get the content for.
* @param defaultStr The default to return when there is no content.
* @return The content of the element or the default.
*/
public static String getElementContent(Element element, String defaultStr) throws Exception {
if (element == null) {
return defaultStr;
}
final NodeList children = element.getChildNodes();
final StringBuilder result = new StringBuilder("");
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.TEXT_NODE || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
result.append(children.item(i).getNodeValue());
}
// else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) {
// // Ignore comment nodes
// }
}
return result.toString().trim();
}
use of org.w3c.dom.NodeList in project hibernate-orm by hibernate.
the class PersistenceXmlParser method parsePersistenceXml.
private Map<String, ParsedPersistenceXmlDescriptor> parsePersistenceXml(URL xmlUrl, Map integration) {
LOG.tracef("Attempting to parse persistence.xml file : %s", xmlUrl.toExternalForm());
final Document doc = loadUrl(xmlUrl);
final Element top = doc.getDocumentElement();
final Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits = new ConcurrentHashMap<>();
final NodeList children = top.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
final Element element = (Element) children.item(i);
final String tag = element.getTagName();
if (tag.equals("persistence-unit")) {
final URL puRootUrl = ArchiveHelper.getJarURLFromURLEntry(xmlUrl, "/META-INF/persistence.xml");
ParsedPersistenceXmlDescriptor persistenceUnit = new ParsedPersistenceXmlDescriptor(puRootUrl);
bindPersistenceUnit(persistenceUnit, element);
if (persistenceUnits.containsKey(persistenceUnit.getName())) {
LOG.duplicatedPersistenceUnitName(persistenceUnit.getName());
continue;
}
// values found in persistence.xml
if (integration.containsKey(AvailableSettings.JPA_PERSISTENCE_PROVIDER)) {
persistenceUnit.setProviderClassName((String) integration.get(AvailableSettings.JPA_PERSISTENCE_PROVIDER));
}
if (integration.containsKey(AvailableSettings.JPA_TRANSACTION_TYPE)) {
String transactionType = (String) integration.get(AvailableSettings.JPA_TRANSACTION_TYPE);
persistenceUnit.setTransactionType(parseTransactionType(transactionType));
}
if (integration.containsKey(AvailableSettings.JPA_JTA_DATASOURCE)) {
persistenceUnit.setJtaDataSource(integration.get(AvailableSettings.JPA_JTA_DATASOURCE));
}
if (integration.containsKey(AvailableSettings.JPA_NON_JTA_DATASOURCE)) {
persistenceUnit.setNonJtaDataSource(integration.get(AvailableSettings.JPA_NON_JTA_DATASOURCE));
}
decodeTransactionType(persistenceUnit);
Properties properties = persistenceUnit.getProperties();
ConfigurationHelper.overrideProperties(properties, integration);
persistenceUnits.put(persistenceUnit.getName(), persistenceUnit);
}
}
}
return persistenceUnits;
}
use of org.w3c.dom.NodeList in project hibernate-orm by hibernate.
the class PersistenceXmlParser method extractContent.
private static String extractContent(Element element, String defaultStr) {
if (element == null) {
return defaultStr;
}
NodeList children = element.getChildNodes();
StringBuilder result = new StringBuilder("");
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.TEXT_NODE || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
result.append(children.item(i).getNodeValue());
}
}
return result.toString().trim();
}
use of org.w3c.dom.NodeList in project hibernate-orm by hibernate.
the class PersistenceXmlParser method bindPersistenceUnit.
private void bindPersistenceUnit(ParsedPersistenceXmlDescriptor persistenceUnit, Element persistenceUnitElement) {
final String name = persistenceUnitElement.getAttribute("name");
if (StringHelper.isNotEmpty(name)) {
LOG.tracef("Persistence unit name from persistence.xml : %s", name);
persistenceUnit.setName(name);
}
final PersistenceUnitTransactionType transactionType = parseTransactionType(persistenceUnitElement.getAttribute("transaction-type"));
if (transactionType != null) {
persistenceUnit.setTransactionType(transactionType);
}
NodeList children = persistenceUnitElement.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) children.item(i);
String tag = element.getTagName();
if (tag.equals("non-jta-data-source")) {
persistenceUnit.setNonJtaDataSource(extractContent(element));
} else if (tag.equals("jta-data-source")) {
persistenceUnit.setJtaDataSource(extractContent(element));
} else if (tag.equals("provider")) {
persistenceUnit.setProviderClassName(extractContent(element));
} else if (tag.equals("class")) {
persistenceUnit.addClasses(extractContent(element));
} else if (tag.equals("mapping-file")) {
persistenceUnit.addMappingFiles(extractContent(element));
} else if (tag.equals("jar-file")) {
persistenceUnit.addJarFileUrl(ArchiveHelper.getURLFromPath(extractContent(element)));
} else if (tag.equals("exclude-unlisted-classes")) {
persistenceUnit.setExcludeUnlistedClasses(extractBooleanContent(element, true));
} else if (tag.equals("delimited-identifiers")) {
persistenceUnit.setUseQuotedIdentifiers(true);
} else if (tag.equals("validation-mode")) {
persistenceUnit.setValidationMode(extractContent(element));
} else if (tag.equals("shared-cache-mode")) {
persistenceUnit.setSharedCacheMode(extractContent(element));
} else if (tag.equals("properties")) {
NodeList props = element.getChildNodes();
for (int j = 0; j < props.getLength(); j++) {
if (props.item(j).getNodeType() == Node.ELEMENT_NODE) {
Element propElement = (Element) props.item(j);
if (!"property".equals(propElement.getTagName())) {
continue;
}
String propName = propElement.getAttribute("name").trim();
String propValue = propElement.getAttribute("value").trim();
if (StringHelper.isEmpty(propValue)) {
//fall back to the natural (Hibernate) way of description
propValue = extractContent(propElement, "");
}
persistenceUnit.getProperties().put(propName, propValue);
}
}
}
}
}
}
Aggregations