List of usage examples for org.w3c.dom Element getNodeName
public String getNodeName();
From source file:Main.java
public static Element[] getElementsByName(Element parent, String name) { ArrayList resList = new ArrayList(); NodeList nl = getNodeList(parent); for (int i = 0; i < nl.getLength(); i++) { Node nd = nl.item(i);/* www . j a v a 2s . c o m*/ if (nd.getNodeName().equals(name)) { resList.add(nd); } } Element[] res = new Element[resList.size()]; for (int i = 0; i < resList.size(); i++) { res[i] = (Element) resList.get(i); } logger.debug(parent.getNodeName() + "'s children of " + name + "'s num:" + res.length); return res; }
From source file:Main.java
public static List<String> getAllLeaves(Element element) { // Get a list of strings representing the relative path // (including the current element) to all the leaf elements // under the current element // Eric: Why return a List? Returning a Set seems to make // more sense. if (element == null) { return null; }//from www . ja va 2s.co m List<String> ret = new LinkedList<String>(); if (isLeaf(element)) { ret.add(element.getNodeName()); } else { NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n instanceof Element) { Element childElement = (Element) n; for (String childNodeName : getAllLeaves(childElement)) { ret.add(element.getNodeName() + "/" + childNodeName); } } } } return ret; }
From source file:it.delli.mwebc.servlet.MWebCRenderer.java
public static void buildLayout(Element elem, Widget parent, Page page) throws Exception { Widget widget = null;//from www. j ava 2s. c o m if (elem.getNodeType() == Node.ELEMENT_NODE && elem.getNodeName().equals("Page")) { for (int j = 0; j < elem.getAttributes().getLength(); j++) { Node attribute = elem.getAttributes().item(j); if (!attribute.getNodeName().equals("id")) { if (attribute.getNodeName().equals("eventListener")) { log.debug("Setting PageEventListener"); try { page.setEventListener( (PageEventListener) Class.forName(attribute.getNodeValue()).newInstance()); } catch (Exception e) { log.error("Exception in setting PageEventListener"); } } } } if (page.getEventListener() == null) { log.debug("Setting default PageEventListener"); page.setEventListener(new PageEventListener() { @Override public void onLoad(Event event) { } @Override public void onInit(Event event) { } }); } for (int i = 0; i < elem.getChildNodes().getLength(); i++) { Node node = elem.getChildNodes().item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { buildLayout((Element) node, widget, page); } } } else if (elem.getNodeType() == Node.ELEMENT_NODE && elem.getNodeName().equals("PageFragment")) { for (int i = 0; i < elem.getChildNodes().getLength(); i++) { Node node = elem.getChildNodes().item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { buildLayout((Element) node, parent, page); } } } else { Class<?> widgetClass = page.getApplication().getWidgetClass(elem.getNodeName()); if (widgetClass == null) { log.fatal("Exception in getting widget class for widget " + elem.getNodeName()); throw new Exception(); } else if (widgetClass.getName().equals("com.dodifferent.applications.commander.widget.DataListItem")) { System.out.println(); } CastHelper castHelper = page.getApplication().getCastHelper(widgetClass); String id = null; for (int j = 0; j < elem.getAttributes().getLength(); j++) { Node attribute = elem.getAttributes().item(j); if (attribute.getNodeName().equals("id")) { id = attribute.getNodeValue(); break; } } HashMap<String, Object> initParams = new HashMap<String, Object>(); for (int j = 0; j < elem.getAttributes().getLength(); j++) { Node attributeNode = elem.getAttributes().item(j); if (!attributeNode.getNodeName().equals("id") && !attributeNode.getNodeName().startsWith("on")) { Field fieldAttribute = ReflectionUtils.getAnnotatedField(widgetClass, attributeNode.getNodeName(), WidgetAttribute.class); if (fieldAttribute != null) { fieldAttribute.setAccessible(true); Class<?> propertyType = fieldAttribute.getType(); // Type[] genericTypes = ((ParameterizedType)fieldAttribute.getGenericType()).getActualTypeArguments(); initParams.put(attributeNode.getNodeName(), castHelper.toType(attributeNode.getNodeValue(), propertyType)); } else { log.warn("Warning in updating server widgets attribute. The attribute '" + attributeNode.getNodeName() + "' doesn't exist for widget " + widgetClass.getName()); } } } try { if (id != null && initParams.keySet().size() == 0) { Class[] constructorParams = { Page.class, String.class }; Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName()) .getConstructor(constructorParams); widget = (Widget) widgetConstructor.newInstance(page, id); } else if (id != null && initParams.keySet().size() > 0) { Class[] constructorParams = { Page.class, String.class, Map.class }; Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName()) .getConstructor(constructorParams); widget = (Widget) widgetConstructor.newInstance(page, id, initParams); } else if (id == null && initParams.keySet().size() == 0) { Class[] constructorParams = { Page.class }; Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName()) .getConstructor(constructorParams); widget = (Widget) widgetConstructor.newInstance(page); } else if (id == null && initParams.keySet().size() > 0) { Class[] constructorParams = { Page.class, Map.class }; Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName()) .getConstructor(constructorParams); widget = (Widget) widgetConstructor.newInstance(page, initParams); } } catch (Exception e) { log.error("Exception in constructing widget " + widget.getClass().getName() + " (" + widget.getId() + ")", e); } if (widget != null) { for (int j = 0; j < elem.getAttributes().getLength(); j++) { Node attributeNode = elem.getAttributes().item(j); if (!attributeNode.getNodeName().equals("id") && attributeNode.getNodeName().startsWith("on")) { String event = attributeNode.getNodeName(); try { widget.addEventListener(event, page.getEventListener(), attributeNode.getNodeValue()); // Method eventMethod = widget.getClass().getMethod("addEventListener", String.class, EventListener.class, String.class); // eventMethod.invoke(widget, event, page.getEventListener(), attributeNode.getNodeValue()); } catch (Exception e) { log.warn("Warning in registering EventListener for widget " + widget.getClass().getName() + " (" + widget.getId() + ")", e); } } } for (int i = 0; i < elem.getChildNodes().getLength(); i++) { Node node = elem.getChildNodes().item(i); if (node.getNodeType() == Node.CDATA_SECTION_NODE || node.getNodeType() == Node.TEXT_NODE) { try { String content = node.getNodeValue().replace("\t", "").replace("\n", "").replace("\"", "\\\""); if (!content.equals("")) { // Method attributeMethod = widget.getClass().getMethod("addTextContent", String.class); // attributeMethod.invoke(widget, content); widget.addTextContent(content); } } catch (Exception e) { log.warn("Warning in setting content for widget " + widget.getClass().getName() + " (" + widget.getId() + ")", e); } } else if (node.getNodeType() == Node.ELEMENT_NODE) { buildLayout((Element) node, widget, page); } } widget.setParent(parent); } } }
From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java
/** * Change result in xml result/*from w w w. j a va2 s. c om*/ * * @param rawString * XML result * @param context * Calling context * @return Changed result * @throws ParserConfigurationException * Parser error * @throws SAXException * Parse error * @throws IOException * IO error */ public static Document changeXMLResult(String rawString, String context) throws ParserConfigurationException, SAXException, IOException { Document doc = XMLUtils.parseDocument(new StringReader(rawString)); List<Element> elms = XMLUtils.getElementsRecursive(doc.getDocumentElement(), new XMLUtils.ElementsFilter() { @Override public boolean acceptElement(Element element) { return (element.getNodeName().equals("doc")); } }); for (Element docE : elms) { changeMasterPidInDOM(docE); filterFieldsInDOM(docE); replacePidsInDOM(docE); } return doc; }
From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java
public static void changeMasterPidInDOM(Element docElem) { // <str name="PID">uuid:2ad31d65-50ca-11e1-916e-001b63bd97ba</str> Element elm = XMLUtils.findElement(docElem, new XMLUtils.ElementsFilter() { @Override/*from w w w .j a v a 2s . c o m*/ public boolean acceptElement(Element element) { if (element.getNodeName().equals("str")) { if (element.hasAttribute("name") && (element.getAttribute("name").equals("PID"))) { return true; } } return false; } }); if (elm != null) { String pid = elm.getTextContent(); if (pid.contains("/")) { pid = pid.replace("/", ""); elm.setTextContent(pid); } } }
From source file:de.codesourcery.utils.xml.XmlHelper.java
public static String getAttribute(Element root, String attrName, boolean isRequired) throws ParseException { String sValue = root.getAttribute(attrName); if (!StringUtils.isBlank(sValue)) { return sValue; } else {// w w w . j a va2 s .co m if (isRequired) { final String msg = "Tag <" + root.getNodeName() + "> is lacking required attribute " + attrName; log.error("getAttribute(): " + msg); throw new ParseException(msg, -1); } return null; } }
From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java
public static void replacePidsInDOM(Element docE) { String[] apiReplace = KConfiguration.getInstance().getAPIPIDReplace(); for (String k : apiReplace) { if (k.equals("PID")) continue; // already replaced Element foundElm = findSolrElement(docE, k); if (foundElm != null) { if (foundElm.getNodeName().equals("str")) { String value = SOLRUtils.value(foundElm.getTextContent(), String.class); if (value != null && (value.indexOf("/@") > 0)) { value = value.replace("/@", "@"); foundElm.setTextContent(value); }/* w ww . java2 s . c o m*/ } else if (foundElm.getNodeName().equals("arr")) { List<String> array = SOLRUtils.array(docE, k, String.class); List<String> newArray = new ArrayList<String>(); for (String value : array) { value = value.replace("/@", "@"); newArray.add(value); } docE.removeChild(foundElm); Element newArrElm = SOLRUtils.arr(foundElm.getOwnerDocument(), k, newArray); docE.appendChild(newArrElm); } else { LOGGER.warning("skipping object type '" + foundElm.getNodeName() + "'"); } } } }
From source file:de.codesourcery.utils.xml.XmlHelper.java
public static int getIntAttribute(Element root, String attrName, boolean isRequired) throws ParseException { String sValue = root.getAttribute(attrName); if (!StringUtils.isBlank(sValue)) { try {/*from w ww . j av a2 s . c o m*/ return Integer.parseInt(sValue); } catch (NumberFormatException ex) { final String msg = "Invalid attribute value (not a number) for tag <" + root.getNodeName() + ">" + " , attribute " + attrName + " , value '" + sValue + "'"; log.error("getIntAttribute(): " + msg); throw new ParseException(msg, -1); } } else { if (isRequired) { final String msg = "Tag <" + root.getNodeName() + "> is lacking required integer attribute " + attrName; log.error("getIntAttribute(): " + msg); throw new ParseException(msg, -1); } return 0; } }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java
private static void readTiposBaseDatosConfiguration(Document xmlConfiguration, XPath xpath) throws XPathExpressionException { tiposBaseDatos = new LinkedList<String>(); Element doc = xmlConfiguration.getDocumentElement(); NodeList databasetype_aux = doc.getElementsByTagName("databaseType"); // iterate over the repositories for (int i = 0; i < databasetype_aux.getLength(); i++) { Node databaseTypeNode = databasetype_aux.item(i); if (databaseTypeNode instanceof Element) { Element databaseTypeElem = (Element) databaseTypeNode; // get alias and url from the repository node NodeList databaseTypeChildren = databaseTypeElem.getChildNodes(); for (int a = 0; a < databaseTypeChildren.getLength(); a++) { Node childrenAux = databaseTypeChildren.item(a); if (childrenAux instanceof Element) { Element childrenAuxElement = (Element) childrenAux; if (childrenAuxElement.getNodeName().equals("name")) { tiposBaseDatos.add(childrenAuxElement.getAttributeNode("value").getValue()); }//from w w w . j av a 2s . co m } } } } }
From source file:de.codesourcery.utils.xml.XmlHelper.java
public static String getElementValue(Element parent, String tagName, boolean isRequired) throws ParseException { Element element = getElement(parent, tagName, isRequired); String result = null;/*from ww w .ja v a2 s. c om*/ if (element != null) { result = getNodeValue(element, null, isRequired); } if (StringUtils.isBlank(result) && isRequired) { final String msg = "Required child tag <" + tagName + " of parent <" + parent.getNodeName() + " to have non-blank value"; log.error("getElement(): " + msg); throw new ParseException(msg, -1); } return result; }