List of usage examples for org.w3c.dom Element getNodeName
public String getNodeName();
From source file:org.eclipse.skalli.core.persistence.XStreamPersistence.java
void setVersionAttribute(Document doc, int version) { Element documentElement = doc.getDocumentElement(); if (doc.getDocumentElement().hasAttribute(TAG_VERSION)) { throw new RuntimeException(MessageFormat.format("<{0}> element already has a ''{1}'' attribute", documentElement.getNodeName(), TAG_VERSION)); }//from w w w .j a va 2 s . c o m documentElement.setAttribute(TAG_VERSION, Integer.toString(version)); }
From source file:org.eclipse.skalli.core.persistence.XStreamPersistence.java
SortedMap<String, Element> getExtensions(Document doc, Map<String, Class<?>> aliases, boolean byClassName) throws MigrationException { TreeMap<String, Element> result = new TreeMap<String, Element>(); if (aliases != null && aliases.size() > 0) { List<Element> extensionElements = MigrationUtils.getExtensions(doc, aliases.keySet()); for (Element extensionElement : extensionElements) { String name = extensionElement.getNodeName(); if (byClassName) { Class<?> extensionClass = aliases.get(name); if (extensionClass != null) { result.put(extensionClass.getName(), extensionElement); }//from w w w.j a v a 2 s.com } else { result.put(name, extensionElement); } } } return result; }
From source file:org.eclipse.skalli.core.persistence.XStreamPersistenceTest.java
private void assertPostProcessedXML(Document newDoc, Document oldDoc, String userId, String expectedGlobalLastModified, String expectedGlobalLastModifiedBy, String[] expectedAliases, String[] expectedExtLastModified, String[] expectedExtLastModifiedBy) throws Exception { XStreamPersistence xp = new TestXStreamPersistence(); Map<String, Class<?>> aliases = getAliases(); xp.postProcessXML(newDoc, oldDoc, aliases, userId, CURRENT_MODEL_VERSION); Element documentElement = newDoc.getDocumentElement(); assertLastModifiedTime(expectedGlobalLastModified, xp.getLastModifiedAttribute(documentElement)); assertEquals(expectedGlobalLastModifiedBy, xp.getLastModifiedByAttribute(documentElement)); SortedMap<String, Element> extensions = xp.getExtensionsByAlias(newDoc, aliases); int i = 0;//from w w w . ja v a2 s . co m for (Element ext : extensions.values()) { assertEquals(expectedAliases[i], ext.getNodeName()); assertLastModifiedTime(expectedExtLastModified[i], xp.getLastModifiedAttribute(ext)); assertEquals(expectedExtLastModifiedBy[i], xp.getLastModifiedByAttribute(ext)); ++i; } assertEquals(CURRENT_MODEL_VERSION, xp.getVersionAttribute(newDoc)); }
From source file:org.eclipse.skalli.nexus.internal.NexusResponseParser.java
static Node findNode(Element rootElement, String nodeName) throws NexusClientException { List<Node> nodes = new ArrayList<Node>(); NodeList children = rootElement.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (nodeName.equals(node.getNodeName())) { nodes.add(node);/*from w ww . j a v a 2 s . co m*/ } } if (nodes.size() == 0) { return null; } else if (nodes.size() > 1) { throw new NexusClientException( MessageFormat.format("Root element ''{0}'' has {1} nodes with name ''{2}''", rootElement.getNodeName(), nodes.size(), nodeName)); } return nodes.get(0); }
From source file:org.eclipse.skalli.nexus.internal.NexusResponseParser.java
static int getNodeTextContentAsInt(Element rootElement, String nodeName, int defaultValue) throws NexusClientException { int result;/*from ww w. j a v a 2 s . co m*/ String fromStr = getNodeTextContent(rootElement, nodeName); if (StringUtils.isNotBlank(fromStr)) { try { result = Integer.parseInt(fromStr); } catch (NumberFormatException e) { throw new NexusClientException(MessageFormat.format( "NodeContet ''{0}'' of node ''{1}'' from Root element ''{2}'' is not an integer", fromStr, nodeName, rootElement.getNodeName())); } } else { result = defaultValue; } return result; }
From source file:org.eclipse.smila.connectivity.framework.crawler.web.http.SitemapParser.java
/** * Returns a {@link Outlink} array with links extracted from site map file. * /*from ww w . j av a 2 s . c o m*/ * @param sitemapContent * the site map content * * @return the outlink[] */ Outlink[] parseSitemapLinks(byte[] sitemapContent) { if (sitemapContent == null) { return EMPTY_LINKS; } final List<Outlink> sitemapLinks = new ArrayList<Outlink>(); try { final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); final InputStream content = new ByteArrayInputStream(sitemapContent); final Document document = documentBuilder.parse(content); final Element element = document.getDocumentElement(); final NodeList childNodeList = element.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { final Node sitemapNode = childNodeList.item(i); if (sitemapNode instanceof Element) { final Element sitemapElements = (Element) sitemapNode; final String sitemapElementsName = sitemapElements.getNodeName(); if ("url".equals(sitemapElementsName)) { final NodeList urlItemsNodeList = sitemapElements.getChildNodes(); for (int j = 0; j < urlItemsNodeList.getLength(); j++) { final Node urlItemNode = urlItemsNodeList.item(j); if (urlItemNode instanceof Element) { final Element urlItemElement = (Element) urlItemNode; final String urlItemElementName = urlItemElement.getNodeName(); if ("loc".equals(urlItemElementName)) { sitemapLinks.add(new Outlink(urlItemElement.getTextContent(), urlItemElement.getTextContent(), _conf)); } } } } } } } catch (MalformedURLException exception) { LOG.error("Error creationg outlink while parsing sitemap"); } catch (Exception exception) { LOG.error("Error parsing sitemap"); return EMPTY_LINKS; } final Outlink[] linksArray = new Outlink[sitemapLinks.size()]; sitemapLinks.toArray(linksArray); return linksArray; }
From source file:org.eclipse.swordfish.plugins.compression.CompressorImpl.java
public Source asUncompressedSource(Source src) { try {/* w w w. j a v a2 s. c o m*/ String encoded = null; if (src instanceof DOMSource) { Node root = ((DOMSource) src).getNode(); if (root instanceof Document) { root = ((Document) root).getDocumentElement(); } Element rootElement = (Element) root; String qName = rootElement.getNodeName(); String localName = qName.substring(qName.indexOf(":") + 1, qName.length()); if (localName.equalsIgnoreCase(CompressionConstants.COMPRESSED_ELEMENT)) { Node node = rootElement.getFirstChild(); if (node != null && node.getNodeType() == Node.TEXT_NODE) { encoded = node.getNodeValue(); } } if (null != encoded && encoded.length() > 0) { byte[] decoded = new Base64().decode(encoded.getBytes()); InputStream is = new GZIPInputStream(new ByteArrayInputStream(decoded)); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(is); return new DOMSource(doc); } else { return src; } } else if (src instanceof StreamSource) { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); CompressedContentHandler ch = new CompressedContentHandler(); xmlReader.setContentHandler(ch); xmlReader.parse(new InputSource(((StreamSource) src).getInputStream())); encoded = ch.getContent(); if (null != encoded && encoded.length() > 0) { byte[] decoded = new Base64().decode(encoded.getBytes()); InputStream is = new GZIPInputStream(new ByteArrayInputStream(decoded)); return new StreamSource(is); } else { throw new SwordfishException("Payload is empty, cannot uncompress."); } } else { return asUncompressedSource(new SourceTransformer().toDOMSource(src)); } } catch (Exception e) { LOG.error("Couldn't decompress source", e); throw new SwordfishException("Couldn't decompress source", e); } }
From source file:org.eclipse.uomo.xml.test.XMLTestCase.java
private String compareElements(Element e1, Element e2, String p) { if (!e1.getNamespaceURI().equals(e2.getNamespaceURI())) return "element namespaces differ at " + p; if (!e1.getLocalName().equals(e2.getLocalName())) return "element names differ at " + p; String msg = compareAttributes(e1, e2, p); if (msg != null) return msg; p = p + "/" + e1.getNodeName(); int i = 0;/*from w w w . j av a2s .co m*/ Node c1 = getNextRelevantNode(e1.getFirstChild()); Node c2 = getNextRelevantNode(e2.getFirstChild()); while (c1 != null && c2 != null) { if (c1.getNodeType() != c2.getNodeType()) return "Different node types (" + Integer.toString(c1.getNodeType()) + "/" + c2.getNodeType() + ") @ " + p; msg = null; if (c1.getNodeType() == Node.TEXT_NODE) { msg = compareTexts((Text) c1, (Text) c2, p + "[" + Integer.toString(i) + "]"); } else if (c1.getNodeType() == Node.ELEMENT_NODE) { msg = compareElements((Element) c1, (Element) c2, p + "[" + Integer.toString(i) + "]"); } else msg = "unknown node type " + Integer.toString(c1.getNodeType()); if (msg != null) return msg; c1 = getNextRelevantNode(c1.getNextSibling()); c2 = getNextRelevantNode(c2.getNextSibling()); i++; } if (c1 != null && c2 == null) return "node present in one and not in two @ " + p; if (c2 != null && c1 == null) return "node present in two and not in one @ " + p; return null; }
From source file:org.efaps.eclipse.wizards.CopyCIWizardPage.java
@Override protected InputStream getInitialContents() { InputStream ret = null;//from ww w . j a v a 2 s. com if (this.fileResource != null && this.fileResource.isAccessible()) { final URI uri = this.fileResource.getLocationURI(); final File file = new File(uri); try { ret = new FileInputStream(file); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = factory.newDocumentBuilder(); final Document doc = docBuilder.parse(file); // create the root element final Element root = doc.getDocumentElement(); final NodeList children = root.getChildNodes(); boolean def = false; boolean uuid = false; for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Element) { final Element child = (Element) children.item(i); if ("uuid".equals(child.getNodeName())) { child.setTextContent(UUID.randomUUID().toString()); uuid = true; } if ("definition".equals(child.getNodeName())) { final NodeList defChildren = child.getChildNodes(); for (int j = 0; j < defChildren.getLength(); j++) { if (defChildren.item(j) instanceof Element) { final Element defChild = (Element) defChildren.item(j); if ("name".equals(defChild.getNodeName())) { defChild.setTextContent( getFileName().replace("." + getFileExtension(), "")); break; } } } def = true; } if (def && uuid) { break; } } } final TransformerFactory transfac = TransformerFactory.newInstance(); final Transformer trans = transfac.newTransformer(); final Source source = new DOMSource(doc); final StreamResult result = new StreamResult(new StringWriter()); trans.transform(source, result); ret = IOUtils.toInputStream(result.getWriter().toString()); } catch (final FileNotFoundException e) { EfapsPlugin.getDefault().logError(getClass(), "FileNotFoundException", e); } catch (final ParserConfigurationException e) { EfapsPlugin.getDefault().logError(getClass(), "ParserConfigurationException", e); } catch (final SAXException e) { EfapsPlugin.getDefault().logError(getClass(), "SAXException", e); } catch (final IOException e) { EfapsPlugin.getDefault().logError(getClass(), "IOException", e); } catch (final TransformerConfigurationException e) { EfapsPlugin.getDefault().logError(getClass(), "TransformerConfigurationException", e); } catch (final TransformerException e) { EfapsPlugin.getDefault().logError(getClass(), "TransformerException", e); } } return ret; }
From source file:org.etudes.mneme.impl.ImportQti2ServiceImpl.java
/** * Find the Question type by identifying Interaction name. * /* www . j a v a2 s . c om*/ * @param contentsDOM * @return * @throws Exception */ private String findInteraction(Document contentsDOM) throws Exception { String interaction = null; Element interactionElement = null; XPath interactionTypePath = new DOMXPath( "/assessmentItem/itemBody//*[contains(local-name(),'Interaction')]"); interaction = ((interactionElement = (Element) interactionTypePath.selectSingleNode(contentsDOM)) != null) ? interactionElement.getNodeName() : ""; return interaction; }