+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2004 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// DOMUtil.java\r
-// Since: 2004/12/24\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/xml/dom/DOMUtil.java $ \r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.util.xml.dom;\r
-\r
-import static org.w3c.dom.Node.CDATA_SECTION_NODE;\r
-import static org.w3c.dom.Node.ELEMENT_NODE;\r
-import static org.w3c.dom.Node.TEXT_NODE;\r
-\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.util.HashMap;\r
-import java.util.LinkedList;\r
-\r
-import javax.xml.parsers.DocumentBuilder;\r
-import javax.xml.parsers.DocumentBuilderFactory;\r
-import javax.xml.parsers.ParserConfigurationException;\r
-\r
-import org.w3c.dom.Attr;\r
-import org.w3c.dom.CDATASection;\r
-import org.w3c.dom.Document;\r
-import org.w3c.dom.Element;\r
-import org.w3c.dom.NamedNodeMap;\r
-import org.w3c.dom.Node;\r
-import org.w3c.dom.NodeList;\r
-import org.w3c.dom.Text;\r
-import org.xerial.util.StringUtil;\r
-import org.xerial.util.xml.XMLErrorCode;\r
-import org.xerial.util.xml.XMLException;\r
-import org.xml.sax.SAXException;\r
-\r
-/**\r
- * Utilities for manipulating DOM\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class DOMUtil\r
-{\r
- private DOMUtil()\r
- {}\r
-\r
- /**\r
- * Retrieves text contents under the specified element\r
- * \r
- * @param parentElement\r
- * @param tagName\r
- * @return tagName\82Ì\8eq\83m\81[\83h\82Ìtext content\r
- */\r
- static public String getTextContent(Element parentElement, String tagName)\r
- {\r
- //NodeList tagList = parentElement.getElementsByTagName(tagName);\r
-\r
- // TODO impl\r
- return null;\r
- }\r
-\r
- /**\r
- * Gets the text data enclosed by the specified element. If xml data has\r
- * several separated text data, the returned text will be their\r
- * concatination. For example, in the following XML data,\r
- * {@link #getText(Element)} for the wiki element gives " Hello World! Nice\r
- * to meet you.".\r
- * \r
- * <pre>\r
- * <wiki> \r
- * Hello World!\r
- * <author>leo</author>\r
- * Nice to meet you.\r
- * <wiki>\r
- * </pre>\r
- * \r
- * The result removes ignorable white spaces between tags.\r
- * \r
- * @param element\r
- * the target element\r
- * @return the text content of the element, or null if no text content is\r
- * found under the element\r
- */\r
- static public String getText(Element element)\r
- {\r
- NodeList nodeList = element.getChildNodes();\r
- StringBuilder buf = new StringBuilder();\r
- int numTextNodes = 0;\r
- for (int i = 0; i < nodeList.getLength(); i++)\r
- {\r
- Node node = nodeList.item(i);\r
- if (node.getNodeType() == Node.TEXT_NODE)\r
- {\r
- if (!((Text) node).isElementContentWhitespace())\r
- {\r
- numTextNodes++;\r
- buf.append(node.getNodeValue());\r
- }\r
- }\r
- }\r
- return numTextNodes > 0 ? buf.toString() : null;\r
- }\r
-\r
- static public HashMap<String, String> getTextContentMap(InputStream xmlStream) throws XMLException, IOException\r
- {\r
- try\r
- {\r
- DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();\r
- Document doc = docBuilder.parse(xmlStream);\r
- Element rootElem = doc.getDocumentElement();\r
- return getTextContentMap(rootElem);\r
- }\r
- catch (ParserConfigurationException e)\r
- {\r
- throw new XMLException(XMLErrorCode.INVALID_PARSER_CONFIGURATION, e);\r
- }\r
- catch (SAXException e)\r
- {\r
- throw new XMLException(XMLErrorCode.SAX_ERROR, e);\r
- }\r
- }\r
-\r
- /**\r
- * @param element\r
- * the element from which the search starts\r
- * \r
- * @return Relative Path Expression -> Text Content HashMap\r
- */\r
- static public HashMap<String, String> getTextContentMap(Element element)\r
- {\r
- //String currentPath = ""; \r
- DOMReadProcess readProcess = new DOMReadProcess();\r
- readProcess.traceSubTree(element);\r
- return readProcess.getContentMap();\r
- }\r
-\r
- static class DOMReadProcess\r
- {\r
- LinkedList<String> _relativePath = new LinkedList<String>();\r
- HashMap<String, String> _path2contentMap = new HashMap<String, String>();\r
-\r
- public DOMReadProcess()\r
- {}\r
-\r
- public void traceSubTree(Element subtreeRoot)\r
- {\r
- String tagName = subtreeRoot.getTagName();\r
- _relativePath.add(tagName);\r
-\r
- if (subtreeRoot.hasAttributes())\r
- {\r
- String currentPath = getCurrentPath();\r
- NamedNodeMap attribMap = subtreeRoot.getAttributes();\r
-\r
- for (int i = 0; i < attribMap.getLength(); i++)\r
- {\r
- Attr attrib = (Attr) attribMap.item(i);\r
- String attribPath = currentPath + "/@" + attrib.getName();\r
- _path2contentMap.put(attribPath, attrib.getValue());\r
- }\r
- }\r
-\r
- StringBuilder contentBuffer = new StringBuilder();\r
- IterableNodeList childNodes = new IterableNodeList(subtreeRoot.getChildNodes());\r
- for (Node node : childNodes)\r
- {\r
- switch (node.getNodeType())\r
- {\r
- case ELEMENT_NODE:\r
- traceSubTree((Element) node);\r
- break;\r
- case TEXT_NODE:\r
- Text text = (Text) node;\r
- contentBuffer.append(text.getData());\r
- break;\r
- case CDATA_SECTION_NODE:\r
- CDATASection cdata = (CDATASection) node;\r
- contentBuffer.append(cdata.getData());\r
- break;\r
- }\r
- }\r
- String textContent = contentBuffer.toString();\r
- if (!StringUtil.isWhiteSpace(textContent))\r
- _path2contentMap.put(getCurrentPath(), contentBuffer.toString());\r
- _relativePath.removeLast();\r
- }\r
-\r
- public HashMap<String, String> getContentMap()\r
- {\r
- return _path2contentMap;\r
- }\r
-\r
- String getCurrentPath()\r
- {\r
- StringBuilder pathExprBuilder = new StringBuilder();\r
- for (String tag : _relativePath)\r
- {\r
- pathExprBuilder.append("/");\r
- pathExprBuilder.append(tag);\r
- }\r
- return pathExprBuilder.substring(1); // relative path\r
- }\r
-\r
- }\r
-}\r