Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #16 Moved commons-io to test scope and migrated to TextResource #18

Merged
3 commits merged into from
Mar 14, 2014
Merged
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
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,9 @@
<scope>test</scope>
</dependency>
<dependency>
<!--
@todo #2 Get rid of commons-io dependency, or move it
to "test" scope. It's not efficient for such a small and
lightweight library to have a big dependency of this kind.
-->
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rexsl</groupId>
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/jcabi/xml/DomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.jcabi.aspects.Loggable;
import com.jcabi.log.Logger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;
Expand All @@ -44,7 +45,6 @@
import net.sourceforge.reb4j.Literal;
import net.sourceforge.reb4j.Sequence;
import net.sourceforge.reb4j.charclass.CharClass;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

Expand All @@ -67,11 +67,6 @@ final class DomParser {
*/
private static final Pattern PATTERN = DomParser.buildPattern();

/**
* Encoding.
*/
private static final String ENCODING = "UTF-8";

/**
* The XML as a text.
*/
Expand Down Expand Up @@ -115,7 +110,9 @@ public Document document() {
final Document doc;
try {
doc = this.factory.newDocumentBuilder().parse(
IOUtils.toInputStream(this.xml, ENCODING)
new ByteArrayInputStream(
this.xml.getBytes(TextResource.ENCODING)
)
);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
Expand Down
150 changes: 150 additions & 0 deletions src/main/java/com/jcabi/xml/TextResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright (c) 2012-2013, JCabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.xml;

import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Scanner;
import lombok.EqualsAndHashCode;

/**
* Represent a given resource (InputStream, URL/URI location content, File)
* as a string. UTF-8 encoding is used.
*
* <p>Objects of this class are immutable and thread-safe.
*
* @author Carlos Miranda (miranda.cma@gmail.com)
* @version $Id$
*/
@Immutable
@EqualsAndHashCode(of = "content")
@Loggable(Loggable.DEBUG)
final class TextResource {
/**
* Encoding.
*/
public static final String ENCODING = "UTF-8";

/**
* The text representation.
*/
private final transient String content;

/**
* Private constructor, used for initializing the field text content.
* @param text The text content
*/
private TextResource(final String text) {
this.content = text;
}

/**
* Public constructor, represent an InputStream as a text resource.
*
* <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));
}

/**
* Public constructor, represent a File as a text resource.
* @param file File to represent as text.
* @throws IOException If an IO problem occurs.
*/
public TextResource(final File file) throws IOException {
this(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));
}

/**
* 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()));
}

@Override
public String toString() {
return this.content;
}

/**
* Reads an entire stream's contents into a string.
* @param stream The stream to read
* @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");
final String result;
try {
if (scanner.hasNext()) {
result = scanner.next();
} else {
result = "";
}
} finally {
scanner.close();
}
return result;
}

/**
* Reads URI contents into a string.
* @param url The URL to read
* @return The stream content, in String form
* @throws IOException if an IO exception occurs
*/
private static String readAsString(final URL url) throws IOException {
return readAsString(new BufferedInputStream(url.openStream()));
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import lombok.EqualsAndHashCode;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -202,7 +200,7 @@ public XMLDocument(@NotNull(message = "source can't be NULL")
*/
public XMLDocument(@NotNull(message = "file can't be NULL")
final File file) throws IOException {
this(FileUtils.readFileToString(file, ENCODING));
this(new TextResource(file).toString());
}

/**
Expand All @@ -220,7 +218,7 @@ public XMLDocument(@NotNull(message = "file can't be NULL")
*/
public XMLDocument(@NotNull(message = "URL can't be NULL")
final URL url) throws IOException {
this(IOUtils.toString(url, ENCODING));
this(new TextResource(url).toString());
}

/**
Expand All @@ -238,7 +236,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(IOUtils.toString(uri, ENCODING));
this(new TextResource(uri.toURL()).toString());
}

/**
Expand All @@ -259,7 +257,7 @@ public XMLDocument(@NotNull(message = "URI can't be NULL")
*/
public XMLDocument(@NotNull(message = "input stream can't be NULL")
final InputStream stream) throws IOException {
this(IOUtils.toString(stream, ENCODING));
this(new TextResource(stream).toString());
stream.close();
}

Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/jcabi/xml/XSDDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import lombok.EqualsAndHashCode;
import org.apache.commons.io.IOUtils;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
Expand All @@ -65,11 +64,6 @@
@Loggable(Loggable.DEBUG)
public final class XSDDocument implements XSD {

/**
* Encoding.
*/
private static final String ENCODING = "UTF-8";

/**
* XSD document.
*/
Expand Down Expand Up @@ -101,7 +95,7 @@ public XSDDocument(@NotNull(message = "XSD text can't be NULL")
*/
public XSDDocument(@NotNull(message = "URL can't be NULL")
final URL url) throws IOException {
this(IOUtils.toString(url, ENCODING));
this(new TextResource(url).toString());
}

/**
Expand All @@ -111,7 +105,7 @@ public XSDDocument(@NotNull(message = "URL can't be NULL")
*/
public XSDDocument(@NotNull(message = "XSD input stream can't be NULL")
final InputStream stream) throws IOException {
this(IOUtils.toString(stream, ENCODING));
this(new TextResource(stream).toString());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/jcabi/xml/XSLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import lombok.EqualsAndHashCode;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;

/**
Expand Down Expand Up @@ -111,7 +110,7 @@ public XSLDocument(@NotNull(message = "XSL can't be NULL")
*/
public XSLDocument(@NotNull(message = "URL can't be NULL")
final URL url) throws IOException {
this(IOUtils.toString(url, ENCODING));
this(new TextResource(url).toString());
}

/**
Expand All @@ -121,7 +120,7 @@ public XSLDocument(@NotNull(message = "URL can't be NULL")
*/
public XSLDocument(@NotNull(message = "XSL input stream can't be NULL")
final InputStream stream) throws IOException {
this(IOUtils.toString(stream, ENCODING));
this(new TextResource(stream).toString());
}

/**
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/com/jcabi/xml/TextResourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2012-2013, JCabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.xml;

import com.google.common.io.Files;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharEncoding;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link TextResource}.
*
* @author Carlos Miranda (miranda.cma@gmail.com)
* @version $Id$
*/
public final class TextResourceTest {

/**
* TextResource should be able to read streams as text.
* @throws Exception If something goes wrong.
*/
@Test
public void readsStreamAsText() throws Exception {
final String text = "Blah!\u20ac\u2122";
final InputStream stream = new ByteArrayInputStream(
text.getBytes(CharEncoding.UTF_8)
);
MatcherAssert.assertThat(
new TextResource(stream).toString(),
Matchers.is(text)
);
}

/**
* TextResource should be able to read files as text.
* @throws Exception If something goes wrong.
*/
@Test
public void readsFileAsText() throws Exception {
final String text = "<a xmlns='urn:foo'><b>\u0433!</b></a>";
final File file = new File(Files.createTempDir(), "dummy.xml");
FileUtils.writeStringToFile(file, text, CharEncoding.UTF_8);
MatcherAssert.assertThat(
new TextResource(file).toString(),
Matchers.is(text)
);
}

}