Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:1179355
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/**
* Singleton publicly-visible view of the {@link DescriptorImporter}
* using a {@link Document} API strategy.
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
public enum XmlDomNodeImporter implements NodeImporter {
INSTANCE;
 
/**
* Internal delegate, singleton
*/
private final NodeImporter delegate = new XmlDomNodeImporterImpl();
 
/**
* {@inheritDoc}
* @see org.jboss.shrinkwrap.descriptor.spi.node.NodeImporter#importAsNode(java.io.InputStream, boolean)
*/
public Node importAsNode(InputStream stream, boolean close) throws IllegalArgumentException
{
return delegate.importAsNode(stream, close);
}
 
}
 
/**
* {@link NodeImporter} implementation backed by the
* {@link Document} API.
*
* @see {@link XmlDomNodeImporter} For convenience singleton
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
*/
public final class XmlDomNodeImporterImpl implements NodeImporter
{
/**
* Logger
*/
private final Logger log = Logger.getLogger(XmlDomNodeImporterImpl.class.getName());
 
/**
* {@inheritDoc}
* @see org.jboss.shrinkwrap.descriptor.spi.node.NodeImporter#importAsNode(java.io.InputStream, boolean)
*/
@Override
public Node importAsNode(final InputStream stream, final boolean close) throws DescriptorImportException
{
try
{
// Empty contents? If so, no root Node
if (stream.available() == 0)
{
return null;
}
 
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(stream);
 
final Node root = new Node(doc.getDocumentElement().getNodeName());
readRecursive(root, doc.getDocumentElement());
return root;
 
}
catch (final Exception e)
{
throw new DescriptorImportException("Could not import XML from stream", e);
}
finally
{
if (close)
{
try
{
stream.close();
}
catch (final IOException ioe)
{
try
{
stream.close();
}
catch (final IOException i)
{
log.log(Level.WARNING, "Unclosable stream specified to be closed: {0}", stream);
}
}
}
}
}
 
private void readRecursive(final Node target, final org.w3c.dom.Node source)
{
readAttributes(target, source);
final NodeList sourceChildren = source.getChildNodes();
if (sourceChildren != null)
{
for (int i = 0; i < sourceChildren.getLength(); i++)
{
final org.w3c.dom.Node child = sourceChildren.item(i);
if (child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
{
// Create our representation of the Node
final Node newTarget = target.createChild(child.getNodeName());
 
if (onlyTextChildren(child))
{
// See if we're dealing with a comment and mark specifically
if (child.getNodeType() == org.w3c.dom.Node.COMMENT_NODE)
{
newTarget.setComment(true);
}
 
// Set text
newTarget.text(child.getTextContent());
readAttributes(newTarget, child);
}
else
{
readRecursive(newTarget, child);
}
}
}
}
}
 
private void readAttributes(final Node target, final org.w3c.dom.Node source)
{
final NamedNodeMap attributes = source.getAttributes();
if (attributes != null)
{
for (int i = 0; i < attributes.getLength(); i++)
{
final org.w3c.dom.Node attribute = attributes.item(i);
target.attribute(attribute.getNodeName(), attribute.getNodeValue());
}
}
}
 
/**
* @param source
* @return
*/
private boolean onlyTextChildren(final org.w3c.dom.Node source)
{
final NodeList children = source.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
final org.w3c.dom.Node child = children.item(i);
if (child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
{
return false;
}
}
return true;
}
}
 
/**
* Adaptor to use a {@link Document} API approach to {@link Descriptor} imports
* as defined by the {@link DescriptorImporter} API
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
public class XmLDomNodeDescriptorImporterImpl<T extends Descriptor> extends NodeDescriptorImporterBase<T>
implements
DescriptorImporter<T>
{
 
/**
* Creates a new instance
* @param endUserViewImplType
* @param descriptorName
* @throws IllegalArgumentException
*/
public XmLDomNodeDescriptorImporterImpl(final Class<T> endUserViewImplType, final String descriptorName)
throws IllegalArgumentException
{
super(endUserViewImplType, descriptorName);
}
 
/**
* {@inheritDoc}
* @see org.jboss.shrinkwrap.descriptor.spi.node.NodeDescriptorImporterBase#getNodeImporter()
*/
@Override
public NodeImporter getNodeImporter()
{
return XmlDomNodeImporter.INSTANCE;
}
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.