use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.
the class HtmlParser method readTag.
/**
* The reader offset should be
*
* @param reader
* @return
*/
private String readTag(final Node parent, final LineNumberReader reader) throws IOException {
final StringBuilder sb = new StringBuilder();
int chInt;
chInt = reader.read();
if (chInt != -1) {
boolean cont = true;
char ch;
LOOP: for (; ; ) {
ch = (char) chInt;
if (Character.isLetter(ch)) {
// Speed up normal case
break LOOP;
} else if (ch == '!') {
sb.append('!');
chInt = reader.read();
if (chInt != -1) {
ch = (char) chInt;
if (ch == '-') {
sb.append('-');
chInt = reader.read();
if (chInt != -1) {
ch = (char) chInt;
if (ch == '-') {
sb.append('-');
cont = false;
}
} else {
cont = false;
}
}
} else {
cont = false;
}
} else if (ch == '/') {
sb.append(ch);
chInt = reader.read();
if (chInt != -1) {
ch = (char) chInt;
} else {
cont = false;
}
} else if (ch == '<') {
final StringBuilder ltText = new StringBuilder(3);
ltText.append('<');
while ((chInt = reader.read()) == '<') {
ltText.append('<');
}
final Document doc = this.document;
final Node textNode = doc.createTextNode(ltText.toString());
try {
parent.appendChild(textNode);
} catch (final DOMException de) {
if ((parent.getNodeType() != NodeType.DOCUMENT_NODE) || (de.getCode() != DOMException.HIERARCHY_REQUEST_ERR)) {
logger.log(Level.WARNING, "parseToken(): Unable to append child to " + parent + ".", de);
}
}
if (chInt == -1) {
cont = false;
} else {
continue;
}
} else if (Character.isWhitespace(ch)) {
final StringBuilder ltText = new StringBuilder();
ltText.append('<');
ltText.append(ch);
while ((chInt = reader.read()) != -1) {
ch = (char) chInt;
if (ch == '<') {
chInt = reader.read();
break;
}
ltText.append(ch);
}
final Document doc = this.document;
final Node textNode = doc.createTextNode(ltText.toString());
try {
parent.appendChild(textNode);
} catch (final DOMException de) {
if ((parent.getNodeType() != NodeType.DOCUMENT_NODE) || (de.getCode() != DOMException.HIERARCHY_REQUEST_ERR)) {
logger.log(Level.WARNING, "parseToken(): Unable to append child to " + parent + ".", de);
}
}
if (chInt == -1) {
cont = false;
} else {
continue;
}
}
break LOOP;
}
if (cont) {
boolean lastCharSlash = false;
for (; ; ) {
if (Character.isWhitespace(ch)) {
break;
} else if (ch == '>') {
this.justReadTagEnd = true;
this.justReadTagBegin = false;
this.justReadEmptyElement = lastCharSlash;
final String tag = sb.toString();
return tag;
} else if (ch == '/') {
lastCharSlash = true;
} else {
if (lastCharSlash) {
sb.append('/');
}
lastCharSlash = false;
sb.append(ch);
}
chInt = reader.read();
if (chInt == -1) {
break;
}
ch = (char) chInt;
}
}
}
if (sb.length() > 0) {
this.justReadTagEnd = false;
this.justReadTagBegin = false;
this.justReadEmptyElement = false;
}
final String tag = sb.toString();
return tag;
}
use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.
the class AbstractCSSProperties method setCssText.
/**
* <p>setCssText.</p>
*
* @param cssText a {@link java.lang.String} object.
*/
public void setCssText(final String cssText) throws DOMException {
AbstractCSSProperties sds = new AbstractCSSProperties(null);
if (Strings.isNotBlank(cssText)) {
final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
try {
final CSSStyleDeclarationImpl sd = parser.parseStyleDeclaration(cssText);
sds.addStyleDeclaration(sd);
setLocalStyleProperties(sds);
if (Strings.isBlank(this.cssText)) {
this.cssText = cssText;
context.setAttribute("style", cssText);
}
} catch (final Exception err) {
logger.log(Level.WARNING, "Unable to parse style attribute value", err);
}
}
}
use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.
the class HTMLDocumentTest method testAttributes.
@Test
public void testAttributes() {
Element p = document.createElement("p");
Attr attr = document.createAttribute("id");
attr.setValue("theId");
p.setAttributeNode(attr);
Attr cloned = (Attr) attr.cloneNode(false);
assertNotNull(cloned);
assertEquals(attr.getName(), cloned.getName());
assertEquals(attr.getNamespaceURI(), cloned.getNamespaceURI());
assertEquals(attr.getValue(), cloned.getValue());
try {
document.createAttribute(null);
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttribute("");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttribute("\u0000");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttribute("<");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttribute(">");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttribute("\"");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, null);
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, "");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, ":");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, "x:");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, ":x");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, ">");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, "<");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createAttributeNS(Document.XML_NAMESPACE_URI, "\"");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
}
use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.
the class HTMLDocumentTest method testBaseElement.
@Test
public void testBaseElement() {
assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getDocumentURI());
assertEquals("http://www.example.com/", document.getBaseURI());
assertEquals("http://www.example.com/", document.getBaseURI());
Element base = (Element) document.getElementsByTagName("base").item(0);
assertEquals("http://www.example.com/", base.getBaseURI());
base.setAttribute("href", "http://www.example.com/newbase/");
assertEquals("http://www.example.com/newbase/", document.getBaseURI());
assertEquals("http://www.example.com/newbase/", base.getBaseURI());
Element anchor = (Element) document.getElementsByTagName("a").item(0);
anchor.setAttribute("href", "http://www.example.com/foo/");
assertEquals("http://www.example.com/foo/", anchor.getAttribute("href"));
assertEquals("http://www.example.com/newbase/", document.getBaseURI());
Attr attr = document.createAttribute("href");
attr.setValue("http://www.example.com/other/base/");
base.setAttributeNode(attr);
assertEquals("http://www.example.com/other/base/", document.getBaseURI());
Node parent = base.getParentNode();
parent.removeChild(base);
attr.setValue("http://www.example.com/yet/another/base/");
assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getBaseURI());
parent.appendChild(base);
assertEquals("http://www.example.com/yet/another/base/", document.getBaseURI());
base.removeAttributeNode(attr);
assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getBaseURI());
try {
base.removeAttributeNode(attr);
fail("Must throw exception.");
} catch (DOMException e) {
}
base.setAttributeNode(attr);
assertEquals("http://www.example.com/yet/another/base/", document.getBaseURI());
attr.setValue("foo:");
assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getBaseURI());
}
use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.
the class HTMLDocumentTest method testAppendChildToTextError.
@Test
public void testAppendChildToTextError() throws DOMException {
Text text = document.createTextNode("text");
Element p = document.createElement("p");
try {
text.appendChild(p);
fail("Must throw exception.");
} catch (DOMException e) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
}
Attr foo = document.createAttribute("foo");
try {
text.appendChild(foo);
fail("Must throw exception.");
} catch (DOMException e) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
}
ProcessingInstruction pi = document.createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"sheet.css\"");
try {
text.appendChild(pi);
fail("Must throw exception.");
} catch (DOMException e) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
}
}
Aggregations