From jira-events at lists.jboss.org Thu Mar 24 08:33:49 2011 Content-Type: multipart/mixed; boundary="===============1983153191648772167==" MIME-Version: 1.0 From: Lincoln Baxter III (JIRA) To: shrinkwrap-issues at lists.jboss.org Subject: [shrinkwrap-issues] [JBoss JIRA] Commented: (SHRINKDESC-45) Import XML to generic Node structure should be public API Date: Thu, 24 Mar 2011 08:33:49 -0400 Message-ID: <1815820772.49431.1300970029083.JavaMail.tomcat@jira02.app.mwc.hst.phx2.redhat.com> In-Reply-To: 1608362618.48097.1300918068191.JavaMail.tomcat@jira02.app.mwc.hst.phx2.redhat.com --===============1983153191648772167== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable [ https://issues.jboss.org/browse/SHRINKDESC-45?page=3Dcom.atlassian.ji= ra.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D1259153= 1#comment-12591531 ] = Lincoln Baxter III commented on SHRINKDESC-45: ---------------------------------------------- Actually. This is as simple as pulling up the Node import from XMLImporter: {code}/* * JBoss, Home of Professional Open Source * Copyright 2011, Red Hat, Inc., and individual contributors * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.seam.forge.parser.xml; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.jboss.shrinkwrap.descriptor.api.DescriptorImportException; import org.jboss.shrinkwrap.descriptor.api.Node; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NodeList; /** * @author Lincoln Baxter, III= * @author Aslak Knutsen * = */ public class XMLParser { public static Node parse(byte[] xml) { return parse(new ByteArrayInputStream(xml)); } public static Node parse(String xml) { return parse(xml.getBytes()); } public static Node parse(InputStream stream) throws DescriptorImportExce= ption { try { if (stream.available() =3D=3D 0) { return null; } DocumentBuilderFactory factory =3D DocumentBuilderFactory.newInsta= nce(); factory.setNamespaceAware(true); DocumentBuilder builder =3D factory.newDocumentBuilder(); Document doc =3D builder.parse(stream); Node root =3D new Node(doc.getDocumentElement().getNodeName()); readRecursive(root, doc.getDocumentElement()); return root; } catch (Exception e) { throw new DescriptorImportException("Could not import XML from str= eam", e); } } private static void readRecursive(Node target, org.w3c.dom.Node source) { readAttributes(target, source); NodeList sourceChildren =3D source.getChildNodes(); if (sourceChildren !=3D null) { for (int i =3D 0; i < sourceChildren.getLength(); i++) { org.w3c.dom.Node child =3D sourceChildren.item(i); if (child.getNodeType() !=3D org.w3c.dom.Node.TEXT_NODE) { Node newTarget =3D target.create(child.getNodeName()); if (onlyTextChildren(child)) { newTarget.text(child.getTextContent()); readAttributes(newTarget, child); } else { readRecursive(newTarget, child); } } } } } private static void readAttributes(Node target, org.w3c.dom.Node source) { NamedNodeMap attributes =3D source.getAttributes(); if (attributes !=3D null) { for (int i =3D 0; i < attributes.getLength(); i++) { org.w3c.dom.Node attribute =3D attributes.item(i); target.attribute(attribute.getNodeName(), attribute.getNodeValu= e()); } } } private static boolean onlyTextChildren(org.w3c.dom.Node source) { NodeList children =3D source.getChildNodes(); for (int i =3D 0; i < children.getLength(); i++) { org.w3c.dom.Node child =3D children.item(i); if (child.getNodeType() !=3D org.w3c.dom.Node.TEXT_NODE) { return false; } } return true; } }{code} > Import XML to generic Node structure should be public API > --------------------------------------------------------- > > Key: SHRINKDESC-45 > URL: https://issues.jboss.org/browse/SHRINKDESC-45 > Project: ShrinkWrap Descriptors > Issue Type: Feature Request > Components: api > Affects Versions: 0.1.5 > Reporter: Lincoln Baxter III > > XMLImporter should probably be public, and provide a few convenience meth= ods to build an ad-hoc Node graph. > XMLImporter.asNodes(InputStream stream); > XMLImporter.asNodes(String xml); > XMLImporter.asNodes(byte[] xml); > This would make Descriptors useful as a pure XML manipulation library, an= d provide a nice path to entry for new users. A nice appealing feature to g= et people to use it in their projects. -- This message is automatically generated by JIRA. For more information on JIRA, see: http://www.atlassian.com/software/jira --===============1983153191648772167==--