Skip to content

Commit

Permalink
#39 polished a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Jan 6, 2015
1 parent 2d9e732 commit b66a67c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 55 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/jcabi/xml/DomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.jcabi.log.Logger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -108,6 +109,8 @@ public Document document() {
this.xml.getBytes(TextResource.ENCODING)
)
);
} catch (final UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
} catch (final ParserConfigurationException ex) {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/jcabi/xml/ListWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,14 @@ private static final class NodeNotFoundException
* @param query The query in XPath
*/
NodeNotFoundException(final String message, final Node node,
final String query) {
final CharSequence query) {
super(
Logger.format(
"XPath '%s' not found in '%[text]s': %s",
escapeUnicode(query),
escapeUnicode(new XMLDocument(node).toString()),
ListWrapper.NodeNotFoundException.escapeUnicode(query),
ListWrapper.NodeNotFoundException.escapeUnicode(
new XMLDocument(node).toString()
),
message
)
);
Expand All @@ -300,7 +302,7 @@ private static final class NodeNotFoundException
* @param input Input string
* @return Escaped output
*/
private static String escapeUnicode(final String input) {
private static String escapeUnicode(final CharSequence input) {
final int length = input.length();
final StringBuilder output = new StringBuilder(length);
for (int index = 0; index < length; index += 1) {
Expand All @@ -312,7 +314,7 @@ private static String escapeUnicode(final String input) {
output.append(character);
}
}
return String.valueOf(input);
return output.toString();
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/jcabi/xml/Sources.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package com.jcabi.xml;

import com.jcabi.aspects.Immutable;
import javax.xml.transform.Source;
import javax.xml.transform.URIResolver;

/**
Expand All @@ -38,8 +39,22 @@
* @author Yegor Bugayenko (yegor@tpc2.com)
* @version $Id$
* @since 0.9
* @checkstyle InterfaceIsType (500 lines)
*/
@Immutable
public interface Sources extends URIResolver {

/**
* Dummy sources.
*/
Sources DUMMY = new Sources() {
@Override
public Source resolve(final String href, final String base) {
throw new UnsupportedOperationException(
// @checkstyle LineLength (1 line)
"URI resolving is not configured in XSLDocument, use #with(URIResolver) method"
);
}
};

}
7 changes: 4 additions & 3 deletions src/main/java/com/jcabi/xml/StrictXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
package com.jcabi.xml;

import com.jcabi.log.Logger;
import com.jcabi.xml.XSDDocument.ValidationHandler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -161,7 +160,7 @@ private static Iterable<String> print(
}

/**
* Joins many objects's string representations with the given separator
* Joins many objects' string representations with the given separator
* string. The separator will not be appended to the beginning or the end.
* @param iterable Iterable of objects.
* @param sep Separator string.
Expand Down Expand Up @@ -197,7 +196,9 @@ private static Collection<SAXParseException> validate(final XML xml) {
final Schema schema = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema();
final Validator validator = schema.newValidator();
validator.setErrorHandler(new ValidationHandler(errors));
validator.setErrorHandler(
new XSDDocument.ValidationHandler(errors)
);
validator.validate(new DOMSource(xml.node()));
} catch (final SAXException ex) {
throw new IllegalStateException(ex);
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/com/jcabi/xml/TextResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -52,6 +53,7 @@
@Immutable
@EqualsAndHashCode(of = "content")
final class TextResource {

/**
* Encoding.
*/
Expand All @@ -76,37 +78,40 @@ private TextResource(final String text) {
* <p>The provided input stream will be closed automatically after
* getting data from it.
* @param stream Stream to represent as text.
* @throws IOException If an IO problem occurs.
*/
public TextResource(final InputStream stream) throws IOException {
this(readAsString(stream));
TextResource(final InputStream stream) {
this(TextResource.readAsString(stream));
}

/**
* Public constructor, represent a File as a text resource.
* @param file File to represent as text.
* @throws IOException If an IO problem occurs.
* @throws FileNotFoundException If file not found
*/
public TextResource(final File file) throws IOException {
this(readAsString(new BufferedInputStream(new FileInputStream(file))));
TextResource(final File file) throws FileNotFoundException {
this(
TextResource.readAsString(
new BufferedInputStream(new FileInputStream(file))
)
);
}

/**
* Public constructor, represent a URL location as a text resource.
* @param url URL to represent as text.
* @throws IOException If an IO problem occurs.
*/
public TextResource(final URL url) throws IOException {
this(readAsString(url));
TextResource(final URL url) throws IOException {
this(TextResource.readAsString(url));
}

/**
* Public constructor, represent a URI location as a text resource.
* @param uri URI to represent as text.
* @throws IOException If an IO problem occurs.
*/
public TextResource(final URI uri) throws IOException {
this(readAsString(uri.toURL()));
TextResource(final URI uri) throws IOException {
this(TextResource.readAsString(uri.toURL()));
}

@Override
Expand All @@ -120,9 +125,8 @@ public String toString() {
* @return The stream content, in String form
*/
private static String readAsString(final InputStream stream) {
@SuppressWarnings("resource")
final Scanner scanner =
new Scanner(stream, ENCODING).useDelimiter("\\A");
new Scanner(stream, TextResource.ENCODING).useDelimiter("\\A");
final String result;
try {
if (scanner.hasNext()) {
Expand All @@ -143,6 +147,8 @@ private static String readAsString(final InputStream stream) {
* @throws IOException if an IO exception occurs
*/
private static String readAsString(final URL url) throws IOException {
return readAsString(new BufferedInputStream(url.openStream()));
return TextResource.readAsString(
new BufferedInputStream(url.openStream())
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public XMLDocument(@NotNull(message = "URL can't be NULL")
*/
public XMLDocument(@NotNull(message = "URI can't be NULL")
final URI uri) throws IOException {
this(new TextResource(uri.toURL()).toString());
this(new TextResource(uri).toString());
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/jcabi/xml/XPathContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
import com.jcabi.immutable.ArrayMap;
import com.jcabi.log.Logger;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.validation.constraints.NotNull;
import javax.xml.XMLConstants;
Expand Down Expand Up @@ -94,7 +93,10 @@ public XPathContext() {
* @param namespaces List of namespaces
*/
public XPathContext(final Object... namespaces) {
this(namespacesAsMap(namespaces), new Array<NamespaceContext>());
this(
XPathContext.namespacesAsMap(namespaces),
new Array<NamespaceContext>()
);
}

/**
Expand Down Expand Up @@ -226,10 +228,10 @@ public XPathContext merge(@NotNull(message = "context can't be NULL")
* @param namespaces The namespaces
* @return Namespaces as map
*/
@SuppressWarnings("PMD.UseConcurrentHashMap")
private static ArrayMap<String, String> namespacesAsMap(
final Object...namespaces) {
final Map<String, String> map = new HashMap<String, String>();
final ConcurrentMap<String, String> map =
new ConcurrentHashMap<String, String>(namespaces.length);
for (int pos = 0; pos < namespaces.length; ++pos) {
map.put(
Logger.format("ns%d", pos + 1),
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/jcabi/xml/XSDDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -96,13 +97,23 @@ public XSDDocument(@NotNull(message = "URL can't be NULL")
this(new TextResource(url).toString());
}

/**
* Public ctor, from URI.
* @param uri Location of document
* @throws IOException If fails to read
* @since 0.15
*/
public XSDDocument(@NotNull(message = "URI can't be NULL")
final URI uri) throws IOException {
this(new TextResource(uri).toString());
}

/**
* Public ctor, from XSD as an input stream.
* @param stream XSD input stream
* @throws IOException If fails to read
*/
public XSDDocument(@NotNull(message = "XSD input stream can't be NULL")
final InputStream stream) throws IOException {
final InputStream stream) {
this(new TextResource(stream).toString());
}

Expand All @@ -124,11 +135,7 @@ public XSDDocument(@NotNull(message = "XSD input stream can't be NULL")
*/
public static XSD make(@NotNull(message = "XSD input stream can't be NULL")
final InputStream stream) {
try {
return new XSDDocument(stream);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
return new XSDDocument(stream);
}

/**
Expand Down
37 changes: 16 additions & 21 deletions src/main/java/com/jcabi/xml/XSLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import javax.validation.constraints.NotNull;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
Expand Down Expand Up @@ -126,17 +126,27 @@ public XSLDocument(@NotNull(message = "XML can't be NULL") final XML src) {
* @since 0.7.4
*/
public XSLDocument(@NotNull(message = "URL can't be NULL")
final URL url) throws IOException {
final URL url) throws IOException {
this(new TextResource(url).toString());
}

/**
* Public ctor, from URI.
* @param uri Location of document
* @throws IOException If fails to read
* @since 0.15
*/
public XSLDocument(@NotNull(message = "URI can't be NULL")
final URI uri) throws IOException {
this(new TextResource(uri).toString());
}

/**
* Public ctor, from XSL as an input stream.
* @param stream XSL input stream
* @throws IOException If fails to read
*/
public XSLDocument(@NotNull(message = "XSL input stream can't be NULL")
final InputStream stream) throws IOException {
final InputStream stream) {
this(new TextResource(stream).toString());
}

Expand All @@ -145,18 +155,7 @@ public XSLDocument(@NotNull(message = "XSL input stream can't be NULL")
* @param src XML document body
*/
public XSLDocument(final String src) {
this(
src,
new Sources() {
@Override
public Source resolve(final String href, final String base) {
throw new UnsupportedOperationException(
// @checkstyle LineLength (1 line)
"URI resolving is not configured in XSLDocument, use #with(URIResolver) method"
);
}
}
);
this(src, Sources.DUMMY);
}

/**
Expand Down Expand Up @@ -197,11 +196,7 @@ public XSL with(@NotNull(message = "sources can't be NULL")
*/
public static XSL make(@NotNull(message = "XSL input stream can't be NULL")
final InputStream stream) {
try {
return new XSLDocument(stream);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
return new XSLDocument(stream);
}

/**
Expand Down

0 comments on commit b66a67c

Please sign in to comment.