List of usage examples for org.w3c.dom Document getFirstChild
public Node getFirstChild();
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
/** * based on our use case, it only check the children of the root node... * // w w w . j a v a 2 s . c om * @param originalDom * @param modifiedDom * @return Document */ private Document replace(final Document originalDom, final Document modifiedDom) { /** * the children nodes of a xml Document is the first level nodes in the * xml doc, for example, for a protocol xml, the children nodes of the * doc is <code><protocol></code> */ // it needs to find the latest identical node then start replacing // whatever under it... Element rootNode = (Element) modifiedDom.getFirstChild(); Document finalDom = originalDom; Element finalDomRoot = (Element) finalDom.getFirstChild(); NodeList modifiedNodes = rootNode.getChildNodes(); int l = modifiedNodes.getLength(); logger.trace("lenght: " + l); for (int i = 0; i < l; i++) { Node currentNode = modifiedNodes.item(i); logger.trace("currentNode: " + currentNode.getNodeName()); NodeList matchedNodes = finalDomRoot.getElementsByTagName(currentNode.getNodeName()); for (int j = 0; j < matchedNodes.getLength(); j++) { try { finalDomRoot.removeChild(matchedNodes.item(j)); } catch (Exception e) { logger.debug("Failed to remove node: " + matchedNodes.item(j).getNodeName()); } } finalDomRoot.appendChild(finalDom.importNode(currentNode, true)); } logger.trace("finalDom: " + DomUtils.elementToString(finalDom)); return finalDom; }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
/** * thread-safety tested//from w ww . ja va 2 s .co m */ @Override public String replaceRootTagWith(final String xmlData, final String rootTag) throws SAXException, IOException { Document document = parse(xmlData); String resultXml = ""; if (rootTag == null) { // remove which will lose the attirbutes on the // root element NodeList nodes = document.getDocumentElement().getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); resultXml += DomUtils.elementToString((Element) n); } } else { document.renameNode(document.getFirstChild(), null, rootTag); resultXml = DomUtils.elementToString(document.getFirstChild()); } return resultXml; }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
@Override public synchronized String listElementsByPath(String path, final String originalXml, boolean inList, boolean includeChildren) throws XPathExpressionException, SAXException, IOException { Assert.hasText(path);// w ww .jav a 2s . c o m Assert.hasText(originalXml); Document originalDom = parse(originalXml); XPath xPath = getXPathInstance(); logger.debug("xpath: " + path); NodeList nodeList = (NodeList) (xPath.evaluate(path, originalDom, XPathConstants.NODESET)); Assert.notNull(nodeList); Document finalDom = documentBuilder.newDocument(); Element root = null; if (inList) { root = finalDom.createElement("list"); } else { root = finalDom.createElement(originalDom.getFirstChild().getNodeName()); } int l = nodeList.getLength(); logger.trace("find: " + l); Node currentNode = null; for (int i = 0; i < l; i++) { currentNode = nodeList.item(i); logger.trace("find: " + currentNode.getNodeName()); root.appendChild(finalDom.importNode(currentNode, includeChildren)); } return DomUtils.elementToString(root); }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
@Override public String deleteAttributeByPathAndAttributeName(String path, String attributeName, final String xmlData) throws SAXException, IOException, XPathExpressionException { Assert.hasText(path);// ww w.j ava 2s .c o m Assert.hasText(xmlData); Document finalDom = parse(xmlData); XPath xPath = getXPathInstance(); NodeList nodeList = (NodeList) (xPath.evaluate(path, finalDom, XPathConstants.NODESET)); int l = nodeList.getLength(); Element currentElement = null; for (int i = 0; i < l; i++) { if (nodeList.item(i) == null) continue; currentElement = (Element) nodeList.item(i); currentElement.removeAttribute(attributeName); } return DomUtils.elementToString(finalDom.getFirstChild()); }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
@Override public String replaceAttributeValueByPathAndAttributeName(String path, String attributeName, final String xmlData, String value) throws SAXException, IOException, XPathExpressionException { Assert.hasText(path);//w ww .j av a 2 s . c o m Assert.hasText(xmlData); Document finalDom = parse(xmlData); XPath xPath = getXPathInstance(); NodeList nodeList = (NodeList) (xPath.evaluate(path, finalDom, XPathConstants.NODESET)); int l = nodeList.getLength(); Element currentElement = null; for (int i = 0; i < l; i++) { if (nodeList.item(i) == null) continue; currentElement = (Element) nodeList.item(i); currentElement.setAttribute(attributeName, value); } return DomUtils.elementToString(finalDom.getFirstChild()); }
From source file:it.imtech.metadata.MetaUtility.java
/** * Metodo adibito alla lettura ricorsiva del file dei metadati per la * costruzione di una struttura dati che li contenga. * * @return TreeMap<Object, Metadata> * @throws Exception/*from w ww.jav a2s . c om*/ */ public TreeMap<Object, Metadata> metadata_reader(String filepath) throws Exception { String lang = Globals.CURRENT_LOCALE.getLanguage(); TreeMap<Object, Metadata> metadatas = new TreeMap<Object, Metadata>(); //Hash dei MIDs dei nodi obbligatori TreeMap forceAddMID = new TreeMap(); forceAddMID.put("98", 1); forceAddMID.put("14", 1); forceAddMID.put("15", 1); forceAddMID.put("64", 1); forceAddMID.put("65", 1); forceAddMID.put("122", 1); forceAddMID.put("105", 1); forceAddMID.put("23", 1); //format forceAddMID.put("24", 1); //location forceAddMID.put("25", 1); //filesize forceAddMID.put("17", 1); //technical forceAddMID.put("22", 1); //Classification forceAddMID.put("45", 1); //Taxonpath forceAddMID.put("46", 1); //Source forceAddMID.put("47", 1); //Taxon forceAddMID.put("82", 1); //Kontextuelle Angaben forceAddMID.put("89", 1); //REFERENZ forceAddMID.put("94", 1); //Referenz forceAddMID.put("95", 1); //Referenznummer forceAddMID.put("137", 1); //Referenznummer forceAddMID.put("123", 1); //identifiers forceAddMID.put("124", 1); //identifiers forceAddMID.put("125", 1); //identifiers forceAddMID.put("5", 1); //keywords forceAddMID.put("96", 1); // GPS forceAddMID.put("6", 1); // Copertura forceAddMID.put("84", 1); // Dimensioni forceAddMID.put("83", 1); // Descrizione forceAddMID.put("93", 1); // Tipo Materiale forceAddMID.put("88", 1); // Unita di misura forceAddMID.put("85", 1); // Lunghezza forceAddMID.put("86", 1); // Larghezza forceAddMID.put("87", 1); // Altezza forceAddMID.put("92", 1); // Diametro forceAddMID.put("11", 1); // Contributo forceAddMID.put("12", 1); // Ruolo //forceAddMID.put("126", 1); // Altro Ruolo forceAddMID.put("13", 1); // Dati Personali forceAddMID.put("14", 1); // Nome forceAddMID.put("15", 1); // Cognome forceAddMID.put("63", 1); // Ente forceAddMID.put("64", 1); // Titolo forceAddMID.put("65", 1); // Titolo //forceAddMID.put("66", 1); // Tipo forceAddMID.put("148", 1); // Numero Matricola /*forceAddMID.put("115", 1); // Provenienza forceAddMID.put("114", 1); // Provenienza - info forceAddMID.put("121", 1); // Provenienza - tipo materiale forceAddMID.put("119", 1); // Provenienza - note forceAddMID.put("107", 1); // Provenienza - ruolo forceAddMID.put("106", 1); // Provenienza - dati personali forceAddMID.put("108", 1); // Provenienza - Nome forceAddMID.put("109", 1); // Provenienza - Cognome forceAddMID.put("113", 1); // Provenienza - Ente forceAddMID.put("110", 1); // Provenienza - Titolo uno forceAddMID.put("111", 1); // Provenienza - Titolo due forceAddMID.put("112", 1); // Provenienza - tipo forceAddMID.put("117", 1); // Provenienza - contributo forceAddMID.put("118", 1); // Provenienza - contributo forceAddMID.put("116", 1); // Provenienza - contributo forceAddMID.put("120", 1); // Provenienza - contributo*/ try { //selectedClassificationList = new TreeMap<String, String>(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc; File urlf = new File(filepath); doc = dBuilder.parse(urlf); Node n = doc.getFirstChild(); if (n.getNodeType() == Node.ELEMENT_NODE) { Element iENode = (Element) n; metadata_reader_metadatas(iENode, metadatas, false, forceAddMID, lang); } } catch (Exception ex) { throw ex; } return metadatas; }
From source file:ch.entwine.weblounge.test.harness.rest.PagesEndpointTest.java
/** * Moves the page to a different url and back. * // www . j av a2s.c o m * @param serverUrl * the server url * @param id * the page id * @throws Exception * if moving fails */ private void testMovePage(String serverUrl, String id) throws Exception { String requestUrl = UrlUtils.concat(serverUrl, "system/weblounge/pages"); HttpGet getPageRequest = new HttpGet(UrlUtils.concat(requestUrl, id)); HttpClient httpClient = new DefaultHttpClient(); Document pageXml = null; String oldPath = null; // Read what's currently stored logger.info("Requesting page at {}", requestUrl); try { HttpResponse response = TestUtils.request(httpClient, getPageRequest, null); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); pageXml = TestUtils.parseXMLResponse(response); assertNotNull(XPathHelper.valueOf(pageXml, "/page")); oldPath = XPathHelper.valueOf(pageXml, "/page/@path"); } finally { httpClient.getConnectionManager().shutdown(); } // Lock the page HttpPut lockPageRequest = new HttpPut(UrlUtils.concat(requestUrl, id, "lock")); httpClient = new DefaultHttpClient(); logger.info("Locking the page at {}", requestUrl); try { HttpResponse response = TestUtils.request(httpClient, lockPageRequest, null); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); } finally { httpClient.getConnectionManager().shutdown(); } // Move the page HttpPut movePageRequest = new HttpPut(UrlUtils.concat(requestUrl, id)); String newPath = PathUtils.concat(oldPath, "/new/"); pageXml.getFirstChild().getAttributes().getNamedItem("path").setNodeValue(newPath); String[][] params = new String[][] { { "content", serializeDoc(pageXml) } }; httpClient = new DefaultHttpClient(); logger.info("Moving page from {} to {}", oldPath, newPath); try { HttpResponse response = TestUtils.request(httpClient, movePageRequest, params); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); } finally { httpClient.getConnectionManager().shutdown(); } // Unlock the page HttpDelete unlockRequest = new HttpDelete(UrlUtils.concat(requestUrl, id, "lock")); httpClient = new DefaultHttpClient(); logger.info("Unlocking the page at {}", requestUrl); try { HttpResponse response = TestUtils.request(httpClient, unlockRequest, null); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); } finally { httpClient.getConnectionManager().shutdown(); } // Make sure the page is gone from the original path but can be found on // the new one HttpGet getPageByPathRequest = new HttpGet(requestUrl); params = new String[][] { { "path", newPath } }; logger.info("Requesting page by path at {}", newPath); // Wait as long as 10s for the asynchronous processing boolean success = false; for (int i = 0; i < 5; i++) { httpClient = new DefaultHttpClient(); try { HttpResponse response = TestUtils.request(httpClient, getPageByPathRequest, params); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); Document pagesXml = TestUtils.parseXMLResponse(response); assertNotNull(XPathHelper.select(pagesXml, "/pages/page[1]")); assertEquals(id, XPathHelper.valueOf(pagesXml, "/pages/page[1]/@id")); assertEquals(newPath, XPathHelper.valueOf(pagesXml, "/pages/page[1]/@path")); success = true; break; } catch (AssertionError a) { logger.debug("Waiting for asynchronous processing"); Thread.sleep(2000); } finally { httpClient.getConnectionManager().shutdown(); } } assertTrue(success); // Check that the page is gone from the old path getPageByPathRequest = new HttpGet(requestUrl); params = new String[][] { { "path", oldPath } }; httpClient = new DefaultHttpClient(); logger.info("Requesting page using the old (no longer valid path) {}", oldPath); success = false; for (int i = 0; i < 5; i++) { httpClient = new DefaultHttpClient(); try { HttpResponse response = TestUtils.request(httpClient, getPageByPathRequest, params); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); assertNull(XPathHelper.select(TestUtils.parseXMLResponse(response), "/pages/page")); success = true; } catch (AssertionError a) { logger.debug("Waiting for asynchronous processing"); Thread.sleep(2000); } finally { httpClient.getConnectionManager().shutdown(); } } assertTrue(success); // Move the page back movePageRequest = new HttpPut(UrlUtils.concat(requestUrl, id)); pageXml.getFirstChild().getAttributes().getNamedItem("path").setNodeValue(oldPath); params = new String[][] { { "content", serializeDoc(pageXml) } }; httpClient = new DefaultHttpClient(); logger.info("Moving page back from {} to {}", newPath, oldPath); try { HttpResponse response = TestUtils.request(httpClient, movePageRequest, params); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); } finally { httpClient.getConnectionManager().shutdown(); } // Make sure the page is back to the original path getPageByPathRequest = new HttpGet(requestUrl); params = new String[][] { { "path", oldPath } }; logger.info("Requesting page by path at {}", oldPath); // Wait as long as 10s for the asynchronous processing success = false; for (int i = 0; i < 5; i++) { httpClient = new DefaultHttpClient(); try { HttpResponse response = TestUtils.request(httpClient, getPageByPathRequest, params); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); Document pagesXml = TestUtils.parseXMLResponse(response); assertNotNull(XPathHelper.select(pagesXml, "/pages/page[1]")); assertEquals(id, XPathHelper.valueOf(pagesXml, "/pages/page[1]/@id")); assertEquals(oldPath, XPathHelper.valueOf(pagesXml, "/pages/page[1]/@path")); success = true; break; } catch (AssertionError a) { logger.debug("Waiting for asynchronous processing"); Thread.sleep(2000); } finally { httpClient.getConnectionManager().shutdown(); } } assertTrue(success); }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
@Override public String addAttributesByPath(String path, final String xmlData, final Map<String, String> attributes) throws SAXException, IOException, XPathExpressionException { Document finalDom = parse(xmlData); XPath xPath = getXPathInstance(); NodeList nodeList = (NodeList) (xPath.evaluate(path, finalDom, XPathConstants.NODESET)); int l = nodeList.getLength(); Element currentElement = null; for (int i = 0; i < l; i++) { if (nodeList.item(i) == null) continue; currentElement = (Element) nodeList.item(i); for (Entry<String, String> entry : attributes.entrySet()) { currentElement.setAttribute(entry.getKey(), entry.getValue()); }/*w ww. ja va2s . c o m*/ } return DomUtils.elementToString(finalDom.getFirstChild()); }