Skip to content

Commit

Permalink
add new static helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jan 5, 2020
1 parent 386f011 commit ef385b0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/main/java/ch/digitalfondue/jfiveparse/JFiveParse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright © 2015 digitalfondue (info@digitalfondue.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.digitalfondue.jfiveparse;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.EnumSet;
import java.util.Set;

/**
* Expose convenient static methods for parsing String or Reader as Html document and serializing nodes back to String.
*/
public class JFiveParse {

public static Document parse(String input) {
return parse(input, EnumSet.noneOf(Option.class));
}

public static Document parse(String input, Set<Option> options) {
return new Parser(options).parse(input);
}

public static Document parse(Reader input) {
return parse(input, EnumSet.noneOf(Option.class));
}

public static Document parse(Reader input, Set<Option> options) {
return new Parser(options).parse(input);
}

public static String serialize(Node node) {
return serialize(node, EnumSet.noneOf(Option.class));
}

public static String serialize(Node node, Set<Option> options) {
return HtmlSerializer.serialize(node, options);
}

public static void serialize(Node node, Writer writer) throws IOException {
serialize(node, EnumSet.noneOf(Option.class), writer);
}

public static void serialize(Node node, Set<Option> options, Writer writer) throws IOException {
HtmlSerializer.serialize(node, options, writer);
}
}
5 changes: 5 additions & 0 deletions src/test/java/ch/digitalfondue/jfiveparse/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.StringReader;


public class DocumentTest {

Expand All @@ -27,6 +29,9 @@ public void checkDocumentRelatedMethods() {
Document d1 = new Parser().parse("<!DOCTYPE html><div>Hello World</div>");
Assert.assertEquals("<!DOCTYPE html><html><head></head><body><div>Hello World</div></body></html>", HtmlSerializer.serialize(d1));

Assert.assertEquals("<!DOCTYPE html><html><head></head><body><div>Hello World</div></body></html>", JFiveParse.serialize(JFiveParse.parse("<!DOCTYPE html><div>Hello World</div>")));
Assert.assertEquals("<!DOCTYPE html><html><head></head><body><div>Hello World</div></body></html>", JFiveParse.serialize(JFiveParse.parse(new StringReader("<!DOCTYPE html><div>Hello World</div>"))));

Assert.assertEquals("html", d1.getDocumentElement().getNodeName());
Assert.assertEquals("<head></head>", d1.getHead().getOuterHTML());
Assert.assertEquals("<body><div>Hello World</div></body>", d1.getBody().getOuterHTML());
Expand Down

0 comments on commit ef385b0

Please sign in to comment.