Skip to content

Commit

Permalink
Removed dependency on Apache Commons-lang. Jsoup now has no external …
Browse files Browse the repository at this point in the history
…dependencies.
  • Loading branch information
jhy committed Jul 2, 2011
1 parent 1342c61 commit 18e9a8f
Show file tree
Hide file tree
Showing 23 changed files with 272 additions and 53 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
jsoup changelog

*** Release 1.2.4 [PENDING]
* Removed dependency on Apache Commons-lang. Jsoup now has no external dependencies.

* Further speed optimisations for parsing and output generation.

*** Release 1.2.3 [2010-Aug-04]
* Added support for automatic input character set detection and decoding. Jsoup now automatically detects the encoding
character set when parsing HTML from a File or URL. The parser checks the content-type header, then the
Expand Down
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@
<version>4.5</version>
<scope>test</scope>
</dependency>

<dependency>
<!-- Commons Lang, for method argument validation. -->
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/DataUtil.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jsoup;

import org.apache.commons.lang.Validate;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jsoup/examples/ListLinks.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.jsoup.examples;

import org.apache.commons.lang.Validate;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import java.net.URL;
import java.io.IOException;
import java.net.URL;

/**
* Example program to list links from a URL.
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/org/jsoup/helper/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.jsoup.helper;

import java.util.Collection;
import java.util.Iterator;

/**
* A minimal String utility class. Designed for interal jsoup use only.
*/
public final class StringUtil {
// memoised padding up to 10
private static final String[] padding = {"", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};

/**
* Join a collection of strings by a seperator
* @param strings collection of string objects
* @param sep string to place between strings
* @return joined string
*/
public static String join(Collection<String> strings, String sep) {
return join(strings.iterator(), sep);
}

/**
* Join a collection of strings by a seperator
* @param strings iterator of string objects
* @param sep string to place between strings
* @return joined string
*/
public static String join(Iterator<String> strings, String sep) {
if (!strings.hasNext())
return "";

String start = strings.next();
if (!strings.hasNext()) // only one, avoid builder
return start;

StringBuilder sb = new StringBuilder(64).append(start);
while (strings.hasNext()) {
sb.append(sep);
sb.append(strings.next());
}
return sb.toString();
}

/**
* Returns space padding
* @param width amount of padding desired
* @return string of spaces * width
*/
public static String padding(int width) {
if (width < 0)
throw new IllegalArgumentException("width must be > 0");

if (width < padding.length)
return padding[width];

char[] out = new char[width];
for (int i = 0; i < width; i++)
out[i] = ' ';
return String.valueOf(out);
}

/**
* Tests if a string is blank: null, emtpy, or only whitespace (" ", \r\n, \t, etc)
* @param string string to test
* @return if string is blank
*/
public static boolean isBlank(String string) {
if (string == null || string.length() == 0)
return true;

int l = string.length();
for (int i = 0; i < l; i++) {
if (!Character.isWhitespace(string.codePointAt(i)))
return false;
}
return true;
}

/**
* Tests if a string is numeric, i.e. contains only digit characters
* @param string string to test
* @return true if only digit chars, false if empty or null or contains non-digit chrs
*/
public static boolean isNumeric(String string) {
if (string == null || string.length() == 0)
return false;

int l = string.length();
for (int i = 0; i < l; i++) {
if (!Character.isDigit(string.codePointAt(i)))
return false;
}
return true;
}
}
85 changes: 85 additions & 0 deletions src/main/java/org/jsoup/helper/Validate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.jsoup.helper;

import java.util.Collection;

/**
* Simple validation methods. Designed for jsoup internal use
*/
public final class Validate {

/**
* Validates that the obect is not null
* @param obj object to test
*/
public static void notNull(Object obj) {
if (obj == null)
throw new IllegalArgumentException("Object must not be null");
}

/**
* Validates that the object is not null
* @param obj object to test
* @param msg message to output if validation fails
*/
public static void notNull(Object obj, String msg) {
if (obj == null)
throw new IllegalArgumentException(msg);
}

/**
* Validates that the value is true
* @param val object to test
*/
public static void isTrue(boolean val) {
if (!val)
throw new IllegalArgumentException("Must be true");
}

/**
* Validates that the value is true
* @param val object to test
* @param msg message to output if validation fails
*/
public static void isTrue(boolean val, String msg) {
if (!val)
throw new IllegalArgumentException(msg);
}

/**
* Validates that the array contains no null elements
* @param objects the array to test
*/
public static void noNullElements(Object[] objects) {
noNullElements(objects, "Array must not contain any null objects");
}

/**
* Validates that the array contains no null elements
* @param objects the array to test
* @param msg message to output if validation fails
*/
public static void noNullElements(Object[] objects, String msg) {
for (Object obj : objects)
if (obj == null)
throw new IllegalArgumentException(msg);
}

/**
* Validates that the string is not empty
* @param string the string to test
*/
public static void notEmpty(String string) {
if (string == null || string.length() == 0)
throw new IllegalArgumentException("String must not be empty");
}

/**
* Validates that the string is not empty
* @param string the string to test
* @param msg message to output if validation fails
*/
public static void notEmpty(String string, String msg) {
if (string == null || string.length() == 0)
throw new IllegalArgumentException(msg);
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/jsoup/nodes/Attribute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jsoup.nodes;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.Validate;
import org.jsoup.helper.Validate;

import java.util.Map;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/nodes/Attributes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jsoup.nodes;

import org.apache.commons.lang.Validate;
import org.jsoup.helper.Validate;

import java.util.*;

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jsoup/nodes/DataNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jsoup.nodes;

import org.apache.commons.lang.StringEscapeUtils;

/**
A data node, for contents of style, script tags etc, where contents should not show in text().
Expand Down Expand Up @@ -48,7 +46,7 @@ public String toString() {
@return new DataNode
*/
public static DataNode createFromEncoded(String encodedData, String baseUri) {
String data = StringEscapeUtils.unescapeHtml(encodedData);
String data = Entities.unescape(encodedData);
return new DataNode(data, baseUri);
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/jsoup/nodes/Document.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jsoup.nodes;

import org.apache.commons.lang.Validate;
import org.jsoup.helper.Validate;
import org.jsoup.parser.Tag;

import java.util.List;
import java.util.ArrayList;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.List;

/**
A HTML Document.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jsoup.nodes;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.StringUtils;
import org.jsoup.helper.StringUtil;
import org.jsoup.helper.Validate;
import org.jsoup.parser.Parser;
import org.jsoup.parser.Tag;
import org.jsoup.select.Collector;
Expand Down Expand Up @@ -810,7 +810,7 @@ public Set<String> classNames() {
*/
public Element classNames(Set<String> classNames) {
Validate.notNull(classNames);
attributes.put("class", StringUtils.join(classNames, " "));
attributes.put("class", StringUtil.join(classNames, " "));
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/nodes/Entities.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jsoup.nodes;

import java.nio.charset.CharsetEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.nio.charset.CharsetEncoder;
import java.util.regex.Pattern;

/**
* HMTL entities, and escape routines.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jsoup/nodes/Evaluator.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jsoup.nodes;

import org.jsoup.helper.Validate;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;

import org.apache.commons.lang.Validate;


/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jsoup/nodes/Node.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.jsoup.nodes;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.StringUtils;
import org.jsoup.select.NodeVisitor;
import org.jsoup.helper.StringUtil;
import org.jsoup.helper.Validate;
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -365,7 +365,7 @@ public String toString() {
}

protected void indent(StringBuilder accum, int depth) {
accum.append("\n").append(StringUtils.leftPad("", depth));
accum.append("\n").append(StringUtil.padding(depth));
}

@Override
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/jsoup/nodes/TextNode.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.jsoup.nodes;

import java.util.regex.Pattern;
import org.jsoup.helper.StringUtil;
import org.jsoup.helper.Validate;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.StringUtils;
import java.util.regex.Pattern;

/**
A text node.
Expand Down Expand Up @@ -62,7 +61,7 @@ public String getWholeText() {
@return true if this document is empty or only whitespace, false if it contains any text content.
*/
public boolean isBlank() {
return StringUtils.isBlank(normaliseWhitespace(getWholeText()));
return StringUtil.isBlank(getWholeText());
}

void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) {
Expand Down Expand Up @@ -102,10 +101,6 @@ static String stripLeadingWhitespace(String text) {
}

static boolean lastCharIsWhitespace(StringBuilder sb) {
if (sb.length() == 0)
return false;
String lastChar = sb.substring(sb.length()-1, sb.length());
Validate.isTrue(lastChar.length() == 1); // todo: remove check
return lastChar.equals(" ");
return sb.length() != 0 && sb.charAt(sb.length() - 1) == ' ';
}
}
Loading

0 comments on commit 18e9a8f

Please sign in to comment.