Repository: incubator-juneau Updated Branches: refs/heads/master ffab95631 -> bacc911af
Code cleanup. Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/bacc911a Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/bacc911a Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/bacc911a Branch: refs/heads/master Commit: bacc911af8c0dd066625340b73bafd0f42386fff Parents: ffab956 Author: JamesBognar <[email protected]> Authored: Mon Jul 3 14:44:54 2017 -0400 Committer: JamesBognar <[email protected]> Committed: Mon Jul 3 14:44:54 2017 -0400 ---------------------------------------------------------------------- .../java/org/apache/juneau/html/HtmlParser.java | 40 ++++++++--------- .../apache/juneau/html/HtmlParserSession.java | 8 ++-- .../java/org/apache/juneau/html/HtmlTag.java | 8 ++-- .../java/org/apache/juneau/ini/ConfigFile.java | 3 +- .../java/org/apache/juneau/json/JsonParser.java | 8 ++-- .../apache/juneau/parser/ParseException.java | 2 +- .../apache/juneau/xml/XmlParseException.java | 47 ++++++++++++++++++++ .../java/org/apache/juneau/xml/XmlParser.java | 6 +-- .../org/apache/juneau/xml/XmlParserSession.java | 14 +++--- 9 files changed, 93 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/html/HtmlParser.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlParser.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlParser.java index cbb3120..12f3fb6 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlParser.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlParser.java @@ -24,7 +24,6 @@ import javax.xml.stream.*; import org.apache.juneau.*; import org.apache.juneau.annotation.*; import org.apache.juneau.http.*; -import org.apache.juneau.json.*; import org.apache.juneau.parser.*; import org.apache.juneau.transform.*; import org.apache.juneau.xml.*; @@ -91,7 +90,7 @@ public class HtmlParser extends XmlParser { int event = r.getEventType(); if (event != START_ELEMENT) - throw new XMLStreamException("parseAnything must be called on outer start element.", r.getLocation()); + throw new XmlParseException(r.getLocation(), "parseAnything must be called on outer start element."); if (! isRoot) event = r.next(); @@ -102,7 +101,7 @@ public class HtmlParser extends XmlParser { event = skipWs(r); if (event == END_DOCUMENT) - throw new XMLStreamException("Unexpected end of stream in parseAnything for type '"+eType+"'", r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected end of stream in parseAnything for type ''{0}''", eType); // Handle @Html(asXml=true) beans. HtmlClassMeta hcm = sType.getExtendedMeta(HtmlClassMeta.class); @@ -251,7 +250,7 @@ public class HtmlParser extends XmlParser { } if (! isValid) - throw new XMLStreamException("Unexpected tag '"+tag+"' for type '"+eType+"'", r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected tag ''{0}'' for type ''{1}''", tag, eType); if (transform != null && o != null) o = transform.unswap(session, o, eType); @@ -266,7 +265,7 @@ public class HtmlParser extends XmlParser { /** * For parsing output from HtmlDocSerializer, this skips over the head, title, and links. */ - private static HtmlTag skipToData(XMLStreamReader r) throws XMLStreamException { + private static HtmlTag skipToData(XMLStreamReader r) throws Exception { while (true) { int event = r.next(); if (event == START_ELEMENT && "div".equals(r.getLocalName()) && "data".equals(r.getAttributeValue(null, "id"))) { @@ -277,7 +276,7 @@ public class HtmlParser extends XmlParser { if (! isEmpty) event = skipWs(r); if (event == END_DOCUMENT) - throw new XMLStreamException("Unexpected end of stream looking for data.", r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected end of stream looking for data."); return (event == CHARACTERS ? null : HtmlTag.forString(r.getName().getLocalPart(), false)); } } @@ -294,7 +293,7 @@ public class HtmlParser extends XmlParser { * Reads an anchor tag and converts it into a bean. */ private static <T> T parseAnchor(HtmlParserSession session, XMLStreamReader r, ClassMeta<T> beanType) - throws XMLStreamException { + throws Exception { String href = r.getAttributeValue(null, "href"); String name = session.getElementText(r); Class<T> beanClass = beanType.getInnerClass(); @@ -493,14 +492,14 @@ public class HtmlParser extends XmlParser { * Precondition: Must be pointing before the event we want to parse. * Postcondition: Pointing at the tag just parsed. */ - private static HtmlTag nextTag(XMLStreamReader r, HtmlTag...expected) throws XMLStreamException { + private static HtmlTag nextTag(XMLStreamReader r, HtmlTag...expected) throws Exception { int et = r.next(); while (et != START_ELEMENT && et != END_ELEMENT && et != END_DOCUMENT) et = r.next(); if (et == END_DOCUMENT) - throw new XMLStreamException("Unexpected end of document: " + r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected end of document."); HtmlTag tag = HtmlTag.forEvent(r); if (expected.length == 0) @@ -509,9 +508,7 @@ public class HtmlParser extends XmlParser { if (t == tag) return tag; - throw new XMLStreamException( - "Unexpected tag: " + tag + ". Expected one of the following: " - + JsonSerializer.DEFAULT.toString(expected), r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected tag: ''{0}''. Expected one of the following: {1}", tag, expected); } /* @@ -523,13 +520,15 @@ public class HtmlParser extends XmlParser { * @param r The stream being read from. * @throws XMLStreamException */ - private static void skipTag(XMLStreamReader r) throws XMLStreamException { + private static void skipTag(XMLStreamReader r) throws Exception { int et = r.getEventType(); if (et != START_ELEMENT) - throw new XMLStreamException( - "skipToNextTag() call on invalid event ["+XmlUtils.toReadableEvent(r) - +"]. Must only be called on START_ELEMENT events."); + throw new XmlParseException( + r.getLocation(), + "skipToNextTag() call on invalid event ''{0}''. Must only be called on START_ELEMENT events.", + XmlUtils.toReadableEvent(r) + ); String n = r.getLocalName(); @@ -550,14 +549,15 @@ public class HtmlParser extends XmlParser { } } - private static void skipTag(XMLStreamReader r, HtmlTag...expected) throws XMLStreamException { + private static void skipTag(XMLStreamReader r, HtmlTag...expected) throws Exception { HtmlTag tag = HtmlTag.forEvent(r); if (tag.isOneOf(expected)) r.next(); else - throw new XMLStreamException( - "Unexpected tag: " + tag + ". Expected one of the following: " - + JsonSerializer.DEFAULT.toString(expected), r.getLocation()); + throw new XmlParseException( + r.getLocation(), + "Unexpected tag: ''{0}''. Expected one of the following: {1}", + tag, expected); } private static int skipWs(XMLStreamReader r) throws XMLStreamException { http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/html/HtmlParserSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlParserSession.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlParserSession.java index 21ccd9a..5b4d9d6 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlParserSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlParserSession.java @@ -85,7 +85,7 @@ public final class HtmlParserSession extends XmlParserSession { * @throws XMLStreamException */ @Override /* XmlParserSession */ - public String parseText(XMLStreamReader r) throws XMLStreamException { + public String parseText(XMLStreamReader r) throws Exception { StringBuilder sb = getStringBuilder(); @@ -176,7 +176,7 @@ public final class HtmlParserSession extends XmlParserSession { * @throws XMLStreamException */ @Override /* XmlParserSession */ - public String getElementText(XMLStreamReader r) throws XMLStreamException { + public String getElementText(XMLStreamReader r) throws Exception { r.next(); return parseText(r); } @@ -188,7 +188,7 @@ public final class HtmlParserSession extends XmlParserSession { } @Override /* XmlParserSession */ - public String parseWhitespaceElement(XMLStreamReader r) throws XMLStreamException { + public String parseWhitespaceElement(XMLStreamReader r) throws Exception { HtmlTag tag = HtmlTag.forEvent(r); int et = r.next(); @@ -208,7 +208,7 @@ public final class HtmlParserSession extends XmlParserSession { } return ""; } else { - throw new XMLStreamException("Invalid tag found in parseWhitespaceElement(): " + tag); + throw new XmlParseException(r.getLocation(), "Invalid tag found in parseWhitespaceElement(): ''{0}''", tag); } } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/html/HtmlTag.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlTag.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlTag.java index 74a98ce..c93aa5d 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlTag.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlTag.java @@ -71,16 +71,16 @@ enum HtmlTag { cache.put(id, this); } - static HtmlTag forEvent(XMLStreamReader r) throws XMLStreamException { + static HtmlTag forEvent(XMLStreamReader r) throws Exception { int et = r.getEventType(); if (et == START_ELEMENT) return forString(r.getLocalName(), false); else if (et == END_ELEMENT) return forString(r.getLocalName(), true); - throw new XMLStreamException("Invalid call to HtmlTag.forEvent on event of type ["+XmlUtils.toReadableEvent(r)+"]"); + throw new XmlParseException(r.getLocation(), "Invalid call to HtmlTag.forEvent on event of type ''{0}''", XmlUtils.toReadableEvent(r)); } - static HtmlTag forString(String tag, boolean end) throws XMLStreamException { + static HtmlTag forString(String tag, boolean end) throws Exception { char c = tag.charAt(0); HtmlTag t = null; if (c == 'u') @@ -130,7 +130,7 @@ enum HtmlTag { else if (c == 'h') t = (end ? xHTML : HTML); if (t == null) - throw new XMLStreamException("Unknown tag '"+tag+"' encountered"); + throw new XmlParseException(null, "Unknown tag ''{0}'' encountered", tag); return t; } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/ini/ConfigFile.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/ConfigFile.java b/juneau-core/src/main/java/org/apache/juneau/ini/ConfigFile.java index 1421502..5a956a5 100644 --- a/juneau-core/src/main/java/org/apache/juneau/ini/ConfigFile.java +++ b/juneau-core/src/main/java/org/apache/juneau/ini/ConfigFile.java @@ -26,7 +26,6 @@ import java.util.*; import org.apache.juneau.*; import org.apache.juneau.internal.*; -import org.apache.juneau.json.*; import org.apache.juneau.parser.*; import org.apache.juneau.serializer.*; import org.apache.juneau.svl.*; @@ -857,7 +856,7 @@ public abstract class ConfigFile implements Map<String,Section> { } } if (! (ignoreUnknownProperties || keys.isEmpty())) - throw new ParseException("Invalid properties found in config file section ["+sectionName+"]: " + JsonSerializer.DEFAULT_LAX.toString(keys)); + throw new ParseException("Invalid properties found in config file section ''{0}'': {1}", sectionName, keys); return om; } finally { readUnlock(); http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/json/JsonParser.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/json/JsonParser.java b/juneau-core/src/main/java/org/apache/juneau/json/JsonParser.java index e2aab27..3afd115 100644 --- a/juneau-core/src/main/java/org/apache/juneau/json/JsonParser.java +++ b/juneau-core/src/main/java/org/apache/juneau/json/JsonParser.java @@ -291,7 +291,7 @@ public class JsonParser extends ReaderParser { // Lax allows blank strings to represent 0. // Strict does not allow blank strings. if (s.length() == 0) - throw new ParseException(session, "Invalid JSON number: '"+s+"'"); + throw new ParseException(session, "Invalid JSON number: ''{0}''", s); // Need to weed out octal and hexadecimal formats: 0123,-0123,0x123,-0x123. // Don't weed out 0 or -0. @@ -304,19 +304,19 @@ public class JsonParser extends ReaderParser { // JSON doesn't allow '.123' and '-.123'. if (c == '.') - throw new ParseException(session, "Invalid JSON number: '"+s+"'"); + throw new ParseException(session, "Invalid JSON number: ''{0}''", s); // '01' is not a valid number, but '0.1', '0e1', '0e+1' are valid. if (c == '0' && s.length() > (isNegative ? 2 : 1)) { char c2 = s.charAt((isNegative ? 2 : 1)); if (c2 != '.' && c2 != 'e' && c2 != 'E') - throw new ParseException(session, "Invalid JSON number: '"+s+"'"); + throw new ParseException(session, "Invalid JSON number: ''{0}''", s); } // JSON doesn't allow '1.' or '0.e1'. int i = s.indexOf('.'); if (i != -1 && (s.length() == (i+1) || ! decChars.contains(s.charAt(i+1)))) - throw new ParseException(session, "Invalid JSON number: '"+s+"'"); + throw new ParseException(session, "Invalid JSON number: ''{0}''", s); } return StringUtils.parseNumber(s, type); http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/parser/ParseException.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/parser/ParseException.java b/juneau-core/src/main/java/org/apache/juneau/parser/ParseException.java index 756e7ce..f675377 100644 --- a/juneau-core/src/main/java/org/apache/juneau/parser/ParseException.java +++ b/juneau-core/src/main/java/org/apache/juneau/parser/ParseException.java @@ -23,7 +23,7 @@ import org.apache.juneau.json.*; /** * Exception that indicates invalid syntax encountered during parsing. */ -public final class ParseException extends FormattedException { +public class ParseException extends FormattedException { private static final long serialVersionUID = 1L; http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParseException.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParseException.java b/juneau-core/src/main/java/org/apache/juneau/xml/XmlParseException.java new file mode 100644 index 0000000..ca91a50 --- /dev/null +++ b/juneau-core/src/main/java/org/apache/juneau/xml/XmlParseException.java @@ -0,0 +1,47 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.xml; + +import static org.apache.juneau.internal.StringUtils.*; + +import java.text.*; + +import javax.xml.stream.*; + +import org.apache.juneau.parser.ParseException; + +/** + * Exception that indicates invalid syntax encountered during XML parsing. + */ +@SuppressWarnings("serial") +public class XmlParseException extends ParseException { + + /** + * Constructor. + * + * @param location The location of the exception. + * @param message The exception message containing {@link MessageFormat}-style arguments. + * @param args Optional {@link MessageFormat}-style arguments. + */ + public XmlParseException(Location location, String message, Object...args) { + super(getMessage(location, message, args)); + } + + private static String getMessage(Location location, String msg, Object... args) { + if (args.length != 0) + msg = format(msg, args); + if (location != null) + msg = "Parse exception occurred at " + location + ". " + msg; + return msg; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParser.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParser.java b/juneau-core/src/main/java/org/apache/juneau/xml/XmlParser.java index 3d57a16..aed0b63 100644 --- a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParser.java +++ b/juneau-core/src/main/java/org/apache/juneau/xml/XmlParser.java @@ -427,7 +427,7 @@ public class XmlParser extends ReaderParser { private Object getUnknown(XmlParserSession session, XMLStreamReader r) throws Exception { if (r.getEventType() != XMLStreamConstants.START_ELEMENT) { - throw new XMLStreamException("parser must be on START_ELEMENT to read next text", r.getLocation()); + throw new XmlParseException(r.getLocation(), "Parser must be on START_ELEMENT to read next text."); } ObjectMap m = null; @@ -449,7 +449,7 @@ public class XmlParser extends ReaderParser { } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) { // skipping } else if (eventType == XMLStreamConstants.END_DOCUMENT) { - throw new XMLStreamException("Unexpected end of document when reading element text content", r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected end of document when reading element text content"); } else if (eventType == XMLStreamConstants.START_ELEMENT) { // Oops...this has an element in it. // Parse it as a map. @@ -482,7 +482,7 @@ public class XmlParser extends ReaderParser { } while (depth > 0); break; } else { - throw new XMLStreamException("Unexpected event type " + eventType, r.getLocation()); + throw new XmlParseException(r.getLocation(), "Unexpected event type ''{0}''", eventType); } eventType = r.next(); } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bacc911a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParserSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParserSession.java b/juneau-core/src/main/java/org/apache/juneau/xml/XmlParserSession.java index 8b0f7c7..3dd0d13 100644 --- a/juneau-core/src/main/java/org/apache/juneau/xml/XmlParserSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/xml/XmlParserSession.java @@ -108,6 +108,9 @@ public class XmlParserSession extends ParserSession { * @throws Exception If problem occurred trying to create reader. */ public final XMLStreamReader getXmlStreamReader() throws Exception { + if (xmlStreamReader != null) + return xmlStreamReader; + try { Reader r = getBufferedReader(getReader()); XMLInputFactory factory = XMLInputFactory.newInstance(); @@ -207,9 +210,9 @@ public class XmlParserSession extends ParserSession { * * @param r The reader to read the element text from. * @return The decoded text. <jk>null</jk> if the text consists of the sequence <js>'_x0000_'</js>. - * @throws XMLStreamException + * @throws Exception */ - public String getElementText(XMLStreamReader r) throws XMLStreamException { + public String getElementText(XMLStreamReader r) throws Exception { String s = r.getElementText().trim(); return decodeString(s); } @@ -282,9 +285,9 @@ public class XmlParserSession extends ParserSession { * * @param r * @return The parsed text. - * @throws XMLStreamException + * @throws Exception */ - public String parseText(XMLStreamReader r) throws XMLStreamException { + public String parseText(XMLStreamReader r) throws Exception { StringBuilder sb2 = getStringBuilder(); int depth = 0; @@ -332,8 +335,9 @@ public class XmlParserSession extends ParserSession { * @param r The XML stream reader to read the current event from. * @return The whitespace character or characters. * @throws XMLStreamException + * @throws Exception */ - public String parseWhitespaceElement(XMLStreamReader r) throws XMLStreamException { + public String parseWhitespaceElement(XMLStreamReader r) throws Exception { return null; }
