Search in sources :

Example 61 with NodeList

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));
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 62 with NodeList

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();
}
Also used : NodeList(org.w3c.dom.NodeList)

Example 63 with NodeList

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;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Properties(java.util.Properties) URL(java.net.URL)

Example 64 with NodeList

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();
}
Also used : NodeList(org.w3c.dom.NodeList)

Example 65 with NodeList

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);
                    }
                }
            }
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) PersistenceUnitTransactionType(javax.persistence.spi.PersistenceUnitTransactionType)

Aggregations

NodeList (org.w3c.dom.NodeList)2090 Node (org.w3c.dom.Node)1217 Element (org.w3c.dom.Element)1061 Document (org.w3c.dom.Document)705 ArrayList (java.util.ArrayList)356 DocumentBuilder (javax.xml.parsers.DocumentBuilder)304 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)237 IOException (java.io.IOException)217 NamedNodeMap (org.w3c.dom.NamedNodeMap)171 InputSource (org.xml.sax.InputSource)145 SAXException (org.xml.sax.SAXException)145 Test (org.junit.Test)138 HashMap (java.util.HashMap)135 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)126 XPath (javax.xml.xpath.XPath)118 StringReader (java.io.StringReader)111 Attr (org.w3c.dom.Attr)88 XPathExpressionException (javax.xml.xpath.XPathExpressionException)85 File (java.io.File)76 InputStream (java.io.InputStream)70