List of usage examples for org.w3c.dom Attr getValue
public String getValue();
From source file:org.structr.web.entity.dom.DOMElement.java
@Override public Node doImport(final Page newPage) throws DOMException { DOMElement newElement = (DOMElement) newPage.createElement(getTagName()); // copy attributes for (String _name : getHtmlAttributeNames()) { Attr attr = getAttributeNode(_name); if (attr.getSpecified()) { newElement.setAttribute(attr.getName(), attr.getValue()); }/*from ww w .jav a 2s . co m*/ } return newElement; }
From source file:org.tizzit.util.XercesHelper.java
public static Node renameNode(Node nde, String strName) { if (!strName.equals(nde.getNodeName())) { Document xdoc = nde.getOwnerDocument(); Element retnode = xdoc.createElement(strName); NodeList nl = nde.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { retnode.appendChild(nl.item(i).cloneNode(true)); }//w w w . j a v a 2 s. c om NamedNodeMap al = nde.getAttributes(); for (int i = 0; i < al.getLength(); i++) { Attr attr = (Attr) al.item(i); retnode.setAttribute(attr.getName(), attr.getValue()); } return retnode; } return nde; }
From source file:org.unitedinternet.cosmo.util.DomWriter.java
private static void writeAttribute(Attr a, XMLStreamWriter writer) throws XMLStreamException { //if (log.isDebugEnabled()) //log.debug("Writing attribute " + a.getNodeName()); String local = a.getLocalName(); if (local == null) { local = a.getNodeName();/*from w ww .ja v a2 s . c o m*/ } String ns = a.getNamespaceURI(); String value = a.getValue(); // was handled by writing the default namespace in writeElement if (local.equals("xmlns")) { return; } if (ns != null) { String prefix = a.getPrefix(); if (prefix != null) { writer.writeAttribute(prefix, ns, local, value); } else { writer.writeAttribute(ns, local, value); } } else { writer.writeAttribute(local, value); } }
From source file:org.wso2.carbon.dashboard.template.deployer.DashboardTemplateDeployer.java
@Override public void deployArtifact(DeployableTemplate template) throws TemplateDeploymentException { String artifactId = template.getArtifactId(); String content = null;//from w w w .j a v a2s .com Map<String, String> properties = new HashMap<>(); DocumentBuilderFactory factory = getSecuredDocumentBuilder(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(template.getArtifact()))); NodeList configNodes = document.getElementsByTagName(DashboardTemplateDeployerConstants.CONFIG_TAG); if (configNodes.getLength() > 0) { Node configNode = configNodes.item(0); // Only one node is expected if (configNode.hasChildNodes()) { // Extract the details NodeList nodeList = configNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (DashboardTemplateDeployerConstants.PROPERTIES_TAG.equalsIgnoreCase(node.getNodeName()) && node.hasChildNodes()) { // Properties NodeList propertiesNodeList = node.getChildNodes(); for (int j = 0; j < propertiesNodeList.getLength(); j++) { Node propertyNode = propertiesNodeList.item(j); if (DashboardTemplateDeployerConstants.PROPERTY_TAG .equalsIgnoreCase(propertyNode.getNodeName())) { Attr attr = (Attr) propertyNode.getAttributes() .getNamedItem(DashboardTemplateDeployerConstants.NAME_ATTRIBUTE); properties.put(attr.getValue(), propertyNode.getFirstChild().getNodeValue().trim()); } } } else if (DashboardTemplateDeployerConstants.CONTENT_TAG .equalsIgnoreCase(node.getNodeName())) { content = node.getFirstChild().getNodeValue(); } } } } } catch (ParserConfigurationException e) { throw new DashboardTemplateDeployerException("Error in creating XML document builder.", e); } catch (SAXException e) { throw new DashboardTemplateDeployerException("Error in parsing XML content of: " + artifactId, e); } catch (IOException e) { throw new DashboardTemplateDeployerException("Error in loading XML content of: " + artifactId, e); } if (content == null || content.trim().isEmpty()) { throw new DashboardTemplateDeployerException("Empty dashboard content for artifact: " + artifactId); } // Store the directory name for the artifact id Registry registry = DashboardTemplateDeployerUtility.getRegistry(); try { Resource resource; if (registry.resourceExists(DashboardTemplateDeployerConstants.ARTIFACT_DASHBOARD_ID_MAPPING_PATH)) { // If same gadgets for same artifact exist, remove them first resource = registry.get(DashboardTemplateDeployerConstants.ARTIFACT_DASHBOARD_ID_MAPPING_PATH); // Delete this artifact if exists if (resource.getProperty(artifactId) != null) { undeployArtifact(artifactId); } } else { resource = registry.newResource(); } resource.setProperty(artifactId, properties.get(DashboardTemplateDeployerConstants.DASHBOARD_ID)); // Save the resource registry.put(DashboardTemplateDeployerConstants.ARTIFACT_DASHBOARD_ID_MAPPING_PATH, resource); } catch (RegistryException e) { throw new DashboardTemplateDeployerException("Failed to access resource at: " + DashboardTemplateDeployerConstants.ARTIFACT_DASHBOARD_ID_MAPPING_PATH + " in registry", e); } try { Resource resource = registry.newResource(); resource.setContent(content); resource.setMediaType("application/json"); registry.put(DashboardTemplateDeployerConstants.DASHBOARDS_RESOURCE_PATH + properties.get(DashboardTemplateDeployerConstants.DASHBOARD_ID), resource); log.info("Dashboard definition of [" + artifactId + "] has been created."); } catch (RegistryException e) { throw new DashboardTemplateDeployerException("Failed to access resource at: " + DashboardTemplateDeployerConstants.ARTIFACT_DASHBOARD_ID_MAPPING_PATH + " in registry", e); } }
From source file:org.wso2.carbon.event.output.adapter.core.internal.util.EventAdapterConfigHelper.java
private static void secureLoadElement(Element element) throws CryptoException { Attr secureAttr = element.getAttributeNodeNS(EventAdapterConstants.SECURE_VAULT_NS, EventAdapterConstants.SECRET_ALIAS_ATTR_NAME); if (secureAttr != null) { element.setTextContent(loadFromSecureVault(secureAttr.getValue())); element.removeAttributeNode(secureAttr); }/*ww w.j ava 2 s . com*/ NodeList childNodes = element.getChildNodes(); int count = childNodes.getLength(); Node tmpNode; for (int i = 0; i < count; i++) { tmpNode = childNodes.item(i); if (tmpNode instanceof Element) { secureLoadElement((Element) tmpNode); } } }
From source file:org.wso2.carbon.gadget.template.deployer.GadgetTemplateDeployer.java
@Override public void deployArtifact(DeployableTemplate template) throws TemplateDeploymentException { String artifactId = template.getArtifactId(); String content = template.getArtifact(); Map<String, String> artifacts = new HashMap<>(); Map<String, String> properties = new HashMap<>(); DocumentBuilderFactory factory = getSecuredDocumentBuilder(); try {/*from w w w.j a v a 2 s. c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(content))); NodeList configNodes = document.getElementsByTagName(GadgetTemplateDeployerConstants.CONFIG_TAG); if (configNodes.getLength() > 0) { Node configNode = configNodes.item(0); // Only one node is expected if (configNode.hasChildNodes()) { // Extract the details NodeList nodeList = configNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (GadgetTemplateDeployerConstants.PROPERTIES_TAG.equalsIgnoreCase(node.getNodeName()) && node.hasChildNodes()) { // Properties NodeList propertiesNodeList = node.getChildNodes(); for (int j = 0; j < propertiesNodeList.getLength(); j++) { Node propertyNode = propertiesNodeList.item(j); if (GadgetTemplateDeployerConstants.PROPERTY_TAG .equalsIgnoreCase(propertyNode.getNodeName())) { Attr attr = (Attr) propertyNode.getAttributes() .getNamedItem(GadgetTemplateDeployerConstants.NAME_ATTRIBUTE); properties.put(attr.getValue(), propertyNode.getFirstChild().getNodeValue().trim()); } } } else if (GadgetTemplateDeployerConstants.ARTIFACTS_TAG .equalsIgnoreCase(node.getNodeName()) && node.hasChildNodes()) { NodeList artifactNodeList = node.getChildNodes(); for (int j = 0; j < artifactNodeList.getLength(); j++) { Node artifactNode = artifactNodeList.item(j); if (GadgetTemplateDeployerConstants.ARTIFACT_TAG .equalsIgnoreCase(artifactNode.getNodeName())) { Attr attr = (Attr) artifactNode.getAttributes() .getNamedItem(GadgetTemplateDeployerConstants.FILE_ATTRIBUTE); artifacts.put(attr.getValue(), artifactNode.getFirstChild().getNodeValue()); } } } } } } } catch (ParserConfigurationException e) { throw new GadgetTemplateDeployerException("Error in creating XML document builder.", e); } catch (SAXException e) { throw new GadgetTemplateDeployerException("Error in parsing XML content of: " + artifactId, e); } catch (IOException e) { throw new GadgetTemplateDeployerException("Error in loading XML content of: " + artifactId, e); } if (!properties.containsKey(GadgetTemplateDeployerConstants.DIRECTORY_NAME)) { throw new GadgetTemplateDeployerException( "Artifact does not contain " + GadgetTemplateDeployerConstants.DIRECTORY_NAME + " property."); } String gadgetArtifactPath = GadgetTemplateDeployerUtility.getGadgetArtifactPath(); File destination = new File( gadgetArtifactPath + properties.get(GadgetTemplateDeployerConstants.DIRECTORY_NAME)); GadgetTemplateDeployerUtility.validatePath(properties.get(GadgetTemplateDeployerConstants.DIRECTORY_NAME)); // Store the directory name for the artifact id Registry registry = GadgetTemplateDeployerUtility.getRegistry(); try { Resource resource; if (registry.resourceExists(GadgetTemplateDeployerConstants.ARTIFACT_DIRECTORY_MAPPING_PATH)) { // If same gadgets for same artifact exist, remove them first resource = registry.get(GadgetTemplateDeployerConstants.ARTIFACT_DIRECTORY_MAPPING_PATH); // Delete this artifact if exists if (resource.getProperty(artifactId) != null) { undeployArtifact(artifactId); } } else { resource = registry.newResource(); } resource.setProperty(artifactId, properties.get(GadgetTemplateDeployerConstants.DIRECTORY_NAME)); // Save the resource registry.put(GadgetTemplateDeployerConstants.ARTIFACT_DIRECTORY_MAPPING_PATH, resource); } catch (RegistryException e) { throw new GadgetTemplateDeployerException( "Failed to access resource at: " + GadgetTemplateDeployerConstants.ARTIFACT_DIRECTORY_MAPPING_PATH + " from registry", e); } // Copy the static files String templateParentDir = new StringBuilder(CarbonUtils.getCarbonConfigDirPath()).append(File.separator) .append(GadgetTemplateDeployerConstants.TEMPLATE_MANAGER).append(File.separator) .append(GadgetTemplateDeployerConstants.GADGET_TEMPLATES).toString(); File templateDirectory = new File(templateParentDir, properties.get(GadgetTemplateDeployerConstants.TEMPLATE_DIRECTORY)); GadgetTemplateDeployerUtility .validatePath(properties.get(GadgetTemplateDeployerConstants.TEMPLATE_DIRECTORY)); // Copy all the default templates try { FileUtils.copyDirectory(templateDirectory, destination); } catch (IOException e) { throw new GadgetTemplateDeployerException("Failed to copy " + templateDirectory.getAbsolutePath() + " to " + destination.getAbsolutePath(), e); } // Save the artifacts for (Map.Entry<String, String> entry : artifacts.entrySet()) { String fileName = entry.getKey(); GadgetTemplateDeployerUtility.validatePath(fileName); File targetFile = new File(destination, fileName); FileWriter writer = null; try { writer = new FileWriter(targetFile); writer.write(entry.getValue()); } catch (IOException e) { throw new GadgetTemplateDeployerException( "Failed to write artifact to: " + targetFile.getAbsolutePath(), e); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { log.warn("Failed to close FileWriter of " + targetFile.getAbsolutePath()); } } } } log.info("Deployed successfully gadget: " + artifactId); }
From source file:org.wso2.carbon.governance.taxonomy.util.TaxonomyCategoryParser.java
/*** * This method is use to go through xml DOM and push the elements names into a stack * * @param childNodes//from w w w. j a v a 2 s. c o m */ private static void loopNodes(NodeList childNodes) throws JSONException { for (int i = 0; i < childNodes.getLength(); ++i) { HashMap<String, String> tempHashMap = new HashMap<>(); Node node = childNodes.item(i); String nodeName = node.getNodeName(); if (!"#text".equals(nodeName)) { Attr attr = (Attr) node.getAttributes().getNamedItem("displayname"); String attribute = null; if (attr != null) { attribute = attr.getValue(); } if (attribute != null) { tempHashMap.put(node.getNodeName(), attribute); elementStack.push(tempHashMap); } else { tempHashMap.put(node.getNodeName(), null); elementStack.push(tempHashMap); } if (node.hasChildNodes()) { loopNodes(node.getChildNodes()); } else { addPathToCategories(); } if (elementStack.size() > 0) { elementStack.pop(); } } } }
From source file:org.wso2.carbon.humantask.core.engine.runtime.xpath.XPathExpressionRuntime.java
private Element replaceElement(Element lval, Element src) { Document doc = lval.getOwnerDocument(); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) { lval.appendChild(doc.importNode(nl.item(i), true)); }//from w w w .ja va 2s . com NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { lval.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) { lval.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } } return lval; }
From source file:org.wso2.carbon.jaggeryapp.template.deployer.JaggeryappTemplateDeployer.java
@Override public void deployArtifact(DeployableTemplate template) throws TemplateDeploymentException { String artifactId = template.getArtifactId(); String content = template.getArtifact(); Map<String, String> artifacts = new HashMap<>(); Map<String, String> properties = new HashMap<>(); DocumentBuilderFactory factory = JaggeryappTemplateDeployerHelper.getSecuredDocumentBuilder(); try {/*www . j ava 2 s . c om*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(content))); NodeList configNodes = document.getElementsByTagName(JaggeryappTemplateDeployerConstants.CONFIG_TAG); if (configNodes.getLength() > 0) { Node configNode = configNodes.item(0); // Only one node is expected if (configNode.hasChildNodes()) { // Extract the details NodeList nodeList = configNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (JaggeryappTemplateDeployerConstants.PROPERTIES_TAG.equalsIgnoreCase(node.getNodeName()) && node.hasChildNodes()) { // Properties NodeList propertiesNodeList = node.getChildNodes(); for (int j = 0; j < propertiesNodeList.getLength(); j++) { Node propertyNode = propertiesNodeList.item(j); if (JaggeryappTemplateDeployerConstants.PROPERTY_TAG .equalsIgnoreCase(propertyNode.getNodeName())) { Attr attr = (Attr) propertyNode.getAttributes() .getNamedItem(JaggeryappTemplateDeployerConstants.NAME_ATTRIBUTE); properties.put(attr.getValue(), propertyNode.getFirstChild().getNodeValue().trim()); } } } else if (JaggeryappTemplateDeployerConstants.ARTIFACTS_TAG .equalsIgnoreCase(node.getNodeName()) && node.hasChildNodes()) { NodeList artifactNodeList = node.getChildNodes(); for (int j = 0; j < artifactNodeList.getLength(); j++) { Node artifactNode = artifactNodeList.item(j); if (JaggeryappTemplateDeployerConstants.ARTIFACT_TAG .equalsIgnoreCase(artifactNode.getNodeName())) { Attr attr = (Attr) artifactNode.getAttributes() .getNamedItem(JaggeryappTemplateDeployerConstants.FILE_ATTRIBUTE); artifacts.put(attr.getValue(), artifactNode.getFirstChild().getNodeValue()); } } } } } } } catch (ParserConfigurationException e) { throw new JaggeryappTemplateDeployerException("Error in creating XML document builder. ", e); } catch (SAXException e) { throw new JaggeryappTemplateDeployerException("Error in parsing XML content of: " + artifactId, e); } catch (IOException e) { throw new JaggeryappTemplateDeployerException("Error in loading XML content of: " + artifactId, e); } if (!properties.containsKey(JaggeryappTemplateDeployerConstants.DIRECTORY_NAME)) { throw new JaggeryappTemplateDeployerException("Artifact does not contain " + JaggeryappTemplateDeployerConstants.DIRECTORY_NAME + " property."); } String jaggeryArtifactPath = JaggeryappTemplateDeployerUtility.getJaggeryappArtifactPath(); File destination = new File( jaggeryArtifactPath + properties.get(JaggeryappTemplateDeployerConstants.DIRECTORY_NAME)); JaggeryappTemplateDeployerHelper .validateFilePath(properties.get(JaggeryappTemplateDeployerConstants.DIRECTORY_NAME)); String templateParentDirectory = new StringBuilder(CarbonUtils.getCarbonConfigDirPath()) .append(File.separator).append(JaggeryappTemplateDeployerConstants.TEMPLATE_MANAGER) .append(File.separator).append(JaggeryappTemplateDeployerConstants.JAGGERYAPP_TEMPLATES) .append(File.separator).toString(); File templateDirectory = new File( templateParentDirectory + properties.get(JaggeryappTemplateDeployerConstants.TEMPLATE_DIRECTORY)); JaggeryappTemplateDeployerHelper .validateFilePath(properties.get(JaggeryappTemplateDeployerConstants.TEMPLATE_DIRECTORY)); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { Registry registry = JaggeryappTemplateDeployerValueHolder.getRegistryService() .getConfigSystemRegistry(tenantId); Resource resource; if (registry.resourceExists(JaggeryappTemplateDeployerConstants.META_INFO_COLLECTION_PATH)) { resource = registry.get(JaggeryappTemplateDeployerConstants.META_INFO_COLLECTION_PATH); // If same artifact id exists it is an edit if (resource.getProperty(artifactId) != null) { undeployArtifact(artifactId); } // Throw exception if same jaggeryapp exists if (destination.exists()) { throw new JaggeryappTemplateDeployerException("Jaggeryapp with same name " + properties.get(JaggeryappTemplateDeployerConstants.DIRECTORY_NAME) + " already exists"); } } else { resource = registry.newResource(); } resource.setProperty(artifactId, properties.get(JaggeryappTemplateDeployerConstants.DIRECTORY_NAME)); // Save the resource registry.put(JaggeryappTemplateDeployerConstants.META_INFO_COLLECTION_PATH, resource); } catch (RegistryException e) { throw new JaggeryappTemplateDeployerException("Failed to access resource at: " + JaggeryappTemplateDeployerConstants.META_INFO_COLLECTION_PATH + " from registry", e); } boolean failedToDeploy = false; try { // Copy all the default templates FileUtils.copyDirectory(templateDirectory, destination); // Save the artifacts for (Map.Entry<String, String> entry : artifacts.entrySet()) { String fileName = entry.getKey(); File targetFile = new File(destination, fileName); FileWriter writer = null; try { writer = new FileWriter(targetFile); writer.write(entry.getValue()); } catch (IOException e) { failedToDeploy = true; log.error("Failed to write artifact to: " + targetFile.getAbsolutePath()); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { log.warn("Failed to close FileWriter of " + targetFile.getAbsolutePath()); } } } } } catch (IOException e) { failedToDeploy = true; log.error("Failed to copy " + templateDirectory.getAbsolutePath() + " to " + destination.getAbsolutePath()); } if (failedToDeploy) { undeployArtifact(artifactId); throw new JaggeryappTemplateDeployerException("Failed to deploy jaggerapp " + artifactId); } else { log.info("Deployed successfully jaggeryapp: " + artifactId); } }
From source file:org.wso2.carbon.lcm.core.util.LifecycleUtils.java
private static void validateSCXMLDataModel(Document lcConfig) throws LifecycleException { NodeList stateList = lcConfig.getElementsByTagName(LifecycleConstants.STATE_TAG); for (int i = 0; i < stateList.getLength(); i++) { List<String> targetValues = new ArrayList<>(); if (stateList.item(i).getNodeType() == Node.ELEMENT_NODE) { Element state = (Element) stateList.item(i); String stateName = state.getAttribute("id"); NodeList targetList = state.getElementsByTagName(LifecycleConstants.TRANSITION_ATTRIBUTE); for (int targetCount = 0; targetCount < targetList.getLength(); targetCount++) { if (targetList.item(targetCount).getNodeType() == Node.ELEMENT_NODE) { Element target = (Element) targetList.item(targetCount); targetValues.add(target.getAttribute(LifecycleConstants.TARGET_ATTRIBUTE)); }/* w w w . j av a 2 s .co m*/ } XPath xPathInstance = XPathFactory.newInstance().newXPath(); String xpathQuery = "//state[@id='" + stateName + "']//@forTarget"; try { XPathExpression exp = xPathInstance.compile(xpathQuery); NodeList forTargetNodeList = (NodeList) exp.evaluate(state, XPathConstants.NODESET); for (int forTargetCount = 0; forTargetCount < forTargetNodeList.getLength(); forTargetCount++) { if (forTargetNodeList.item(forTargetCount).getNodeType() == Node.ATTRIBUTE_NODE) { Attr attr = (Attr) forTargetNodeList.item(forTargetCount); if (!"".equals(attr.getValue()) && !targetValues.contains(attr.getValue())) { throw new LifecycleException("forTarget attribute value " + attr.getValue() + " does not included as target state in the state object " + stateName); } } } } catch (XPathExpressionException e) { throw new LifecycleException("Error while reading for target attributes ", e); } } } }