Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package org.jboss.shrinkwrap.descriptor.spi.node;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Constructor;

import org.jboss.shrinkwrap.descriptor.api.Descriptor;
Expand Down Expand Up @@ -90,6 +93,52 @@ public T fromStream(final InputStream in, final boolean close) throws IllegalArg
return descriptor;
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.descriptor.api.DescriptorImporter#fromString(String)
*/
@Override
public T fromString(final String in) throws IllegalArgumentException,
DescriptorImportException {
// Precondition check
if (in == null || in.length() == 0) {
throw new IllegalArgumentException("Input string must be specified");
}

Reader reader = new StringReader(in);
try {
final Node rootNode = this.getNodeImporter().importAsNode(reader);

// Create the end-user view
final Constructor<T> constructor;
try {
constructor = endUserViewImplType.getConstructor(String.class, Node.class);
} catch (final NoSuchMethodException e) {
throw new DescriptorImportException("Descriptor impl " + endUserViewImplType.getName()
+ " does not have a constructor accepting " + String.class.getName() + " and " + Node.class.getName(),
e);
}
final T descriptor;
try {
descriptor = constructor.newInstance(descriptorName, rootNode);
} catch (final Exception e) {
throw new DescriptorImportException("Could not create new instance using " + constructor + " with arg: "
+ rootNode);
}

// Return
return descriptor;
}
finally {
try {
reader.close();
} catch (IOException e) {
throw new DescriptorImportException("Exception while closing StringReader", e);
}
}
}

// -------------------------------------------------------------------------------------||
// Contracts --------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jboss.shrinkwrap.descriptor.spi.node;

import java.io.InputStream;
import java.io.Reader;

/**
* Imports a {@link InputStream} into a hierarchal {@link Node} structure
Expand All @@ -38,4 +39,14 @@ public interface NodeImporter {
*/
Node importAsNode(InputStream stream, boolean close) throws IllegalArgumentException;

/**
* Imports the specified {@link Reader} into a {@link Node} structure, returning the root {@link Node}.
*
* @param input
* The reader containing the xml file content
* @return
* @throws IllegalArgumentException
* If the reader is not specified
*/
Node importAsNode(Reader input) throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jboss.shrinkwrap.descriptor.spi.node.dom;

import java.io.InputStream;
import java.io.Reader;

import org.jboss.shrinkwrap.descriptor.api.DescriptorImporter;
import org.jboss.shrinkwrap.descriptor.spi.node.Node;
Expand Down Expand Up @@ -45,4 +46,13 @@ public Node importAsNode(InputStream stream, boolean close) throws IllegalArgume
return delegate.importAsNode(stream, close);
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.descriptor.spi.node.NodeImporter#importAsNode(Reader)
*/
public Node importAsNode(Reader input) throws IllegalArgumentException {
return delegate.importAsNode(input);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -31,6 +32,7 @@
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
* {@link NodeImporter} implementation backed by the {@link Document} API.
Expand Down Expand Up @@ -88,6 +90,37 @@ public Node importAsNode(final InputStream stream, final boolean close) throws D
}
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.descriptor.spi.node.NodeImporter#importAsNode(Reader)
*/
@Override
public Node importAsNode(Reader reader) throws IllegalArgumentException {
try {
// Empty contents? If so, no root Node
if (reader == null || reader.ready() == false) {
return null;
}

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
final DocumentBuilder builder = factory.newDocumentBuilder();
InputSource source = new InputSource(reader);
final Document doc = builder.parse(source);

final Element element = doc.getDocumentElement();

final Node root = new Node(element.getNodeName());

readRecursive(root, element);
return root;

} catch (final Exception e) {
throw new DescriptorImportException("Could not import XML from string", e);
}
}

private void readRecursive(final Node target, final org.w3c.dom.Node source) {
readAttributes(target, source);
final NodeList sourceChildren = source.getChildNodes();
Expand Down