use of org.xml.sax.SAXParseException in project sling by apache.
the class JspDocumentParser method startElement.
/*
* Receives notification of the start of an element.
*
* This method assigns the given tag attributes to one of 3 buckets:
*
* - "xmlns" attributes that represent (standard or custom) tag libraries.
* - "xmlns" attributes that do not represent tag libraries.
* - all remaining attributes.
*
* For each "xmlns" attribute that represents a custom tag library, the
* corresponding TagLibraryInfo object is added to the set of custom
* tag libraries.
*/
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
AttributesImpl taglibAttrs = null;
AttributesImpl nonTaglibAttrs = null;
AttributesImpl nonTaglibXmlnsAttrs = null;
processChars();
checkPrefixes(uri, qName, attrs);
if (directivesOnly && !(JSP_URI.equals(uri) && localName.startsWith(DIRECTIVE_ACTION))) {
return;
}
// jsp:text must not have any subelements
if (JSP_URI.equals(uri) && TEXT_ACTION.equals(current.getLocalName())) {
throw new SAXParseException(Localizer.getMessage("jsp.error.text.has_subelement"), locator);
}
startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
if (attrs != null) {
/*
* Notice that due to a bug in the underlying SAX parser, the
* attributes must be enumerated in descending order.
*/
boolean isTaglib = false;
for (int i = attrs.getLength() - 1; i >= 0; i--) {
isTaglib = false;
String attrQName = attrs.getQName(i);
if (!attrQName.startsWith("xmlns")) {
if (nonTaglibAttrs == null) {
nonTaglibAttrs = new AttributesImpl();
}
nonTaglibAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
} else {
if (attrQName.startsWith("xmlns:jsp")) {
isTaglib = true;
} else {
String attrUri = attrs.getValue(i);
// TaglibInfo for this uri already established in
// startPrefixMapping
isTaglib = pageInfo.hasTaglib(attrUri);
}
if (isTaglib) {
if (taglibAttrs == null) {
taglibAttrs = new AttributesImpl();
}
taglibAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
} else {
if (nonTaglibXmlnsAttrs == null) {
nonTaglibXmlnsAttrs = new AttributesImpl();
}
nonTaglibXmlnsAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
}
}
}
}
Node node = null;
if (tagDependentPending && JSP_URI.equals(uri) && localName.equals(BODY_ACTION)) {
tagDependentPending = false;
tagDependentNesting++;
current = parseStandardAction(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
return;
}
if (tagDependentPending && JSP_URI.equals(uri) && localName.equals(ATTRIBUTE_ACTION)) {
current = parseStandardAction(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
return;
}
if (tagDependentPending) {
tagDependentPending = false;
tagDependentNesting++;
}
if (tagDependentNesting > 0) {
node = new Node.UninterpretedTag(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
} else if (JSP_URI.equals(uri)) {
node = parseStandardAction(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
} else {
node = parseCustomAction(qName, localName, uri, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
if (node == null) {
node = new Node.UninterpretedTag(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
} else {
// custom action
String bodyType = getBodyType((Node.CustomTag) node);
if (scriptlessBodyNode == null && bodyType.equalsIgnoreCase(TagInfo.BODY_CONTENT_SCRIPTLESS)) {
scriptlessBodyNode = node;
} else if (TagInfo.BODY_CONTENT_TAG_DEPENDENT.equalsIgnoreCase(bodyType)) {
tagDependentPending = true;
}
}
}
current = node;
}
use of org.xml.sax.SAXParseException in project sling by apache.
the class JspDocumentParser method processChars.
private void processChars() throws SAXException {
if (charBuffer == null || directivesOnly) {
return;
}
/*
* JSP.6.1.1: All textual nodes that have only white space are to be
* dropped from the document, except for nodes in a jsp:text element,
* and any leading and trailing white-space-only textual nodes in a
* jsp:attribute whose 'trim' attribute is set to FALSE, which are to
* be kept verbatim.
* JSP.6.2.3 defines white space characters.
*/
boolean isAllSpace = true;
if (!(current instanceof Node.JspText) && !(current instanceof Node.NamedAttribute)) {
for (int i = 0; i < charBuffer.length(); i++) {
if (!(charBuffer.charAt(i) == ' ' || charBuffer.charAt(i) == '\n' || charBuffer.charAt(i) == '\r' || charBuffer.charAt(i) == '\t')) {
isAllSpace = false;
break;
}
}
}
if (!isAllSpace && tagDependentPending) {
tagDependentPending = false;
tagDependentNesting++;
}
if (tagDependentNesting > 0) {
if (charBuffer.length() > 0) {
new Node.TemplateText(charBuffer.toString(), startMark, current);
}
startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
charBuffer = null;
return;
}
if ((current instanceof Node.JspText) || (current instanceof Node.NamedAttribute) || !isAllSpace) {
int line = startMark.getLineNumber();
int column = startMark.getColumnNumber();
CharArrayWriter ttext = new CharArrayWriter();
int lastCh = 0, elType = 0;
for (int i = 0; i < charBuffer.length(); i++) {
int ch = charBuffer.charAt(i);
if (ch == '\n') {
column = 1;
line++;
} else {
column++;
}
if ((lastCh == '$' || lastCh == '#') && ch == '{') {
elType = lastCh;
if (ttext.size() > 0) {
new Node.TemplateText(ttext.toString(), startMark, current);
ttext = new CharArrayWriter();
//We subtract two from the column number to
//account for the '[$,#]{' that we've already parsed
startMark = new Mark(ctxt, path, line, column - 2);
}
// following "${" || "#{" to first unquoted "}"
i++;
boolean singleQ = false;
boolean doubleQ = false;
lastCh = 0;
for (; ; i++) {
if (i >= charBuffer.length()) {
throw new SAXParseException(Localizer.getMessage("jsp.error.unterminated", (char) elType + "{"), locator);
}
ch = charBuffer.charAt(i);
if (ch == '\n') {
column = 1;
line++;
} else {
column++;
}
if (lastCh == '\\' && (singleQ || doubleQ)) {
ttext.write(ch);
lastCh = 0;
continue;
}
if (ch == '}') {
new Node.ELExpression((char) elType, ttext.toString(), startMark, current);
ttext = new CharArrayWriter();
startMark = new Mark(ctxt, path, line, column);
break;
}
if (ch == '"')
doubleQ = !doubleQ;
else if (ch == '\'')
singleQ = !singleQ;
ttext.write(ch);
lastCh = ch;
}
} else if (lastCh == '\\' && (ch == '$' || ch == '#')) {
ttext.write(ch);
// Not start of EL anymore
ch = 0;
} else {
if (lastCh == '$' || lastCh == '#' || lastCh == '\\') {
ttext.write(lastCh);
}
if (ch != '$' && ch != '#' && ch != '\\') {
ttext.write(ch);
}
}
lastCh = ch;
}
if (lastCh == '$' || lastCh == '#' || lastCh == '\\') {
ttext.write(lastCh);
}
if (ttext.size() > 0) {
new Node.TemplateText(ttext.toString(), startMark, current);
}
}
startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
charBuffer = null;
}
use of org.xml.sax.SAXParseException in project sling by apache.
the class MyErrorHandler method parseXMLDocument.
// --------------------------------------------------------- Public Methods
/**
* Parse the specified XML document, and return a <code>TreeNode</code>
* that corresponds to the root node of the document tree.
*
* @param uri URI of the XML document being parsed
* @param is Input source containing the deployment descriptor
*
* @exception JasperException if an input/output error occurs
* @exception JasperException if a parsing error occurs
*/
public TreeNode parseXMLDocument(String uri, InputSource is) throws JasperException {
Document document = null;
// Perform an XML parse of this document, via JAXP
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(validating);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(entityResolver);
builder.setErrorHandler(errorHandler);
document = builder.parse(is);
} catch (ParserConfigurationException ex) {
throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), ex);
} catch (SAXParseException ex) {
throw new JasperException(Localizer.getMessage("jsp.error.parse.xml.line", uri, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber())), ex);
} catch (SAXException sx) {
throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), sx);
} catch (IOException io) {
throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), io);
}
// Convert the resulting document to a graph of TreeNodes
return (convert(null, document.getDocumentElement()));
}
use of org.xml.sax.SAXParseException in project robovm by robovm.
the class Properties method loadFromXML.
/**
* Loads the properties from an {@code InputStream} containing the
* properties in XML form. The XML document must begin with (and conform to)
* following DOCTYPE:
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* Also the content of the XML data must satisfy the DTD but the xml is not
* validated against it. The DTD is not loaded from the SYSTEM ID. After
* this method returns the InputStream is not closed.
*
* @param in the InputStream containing the XML document.
* @throws IOException in case an error occurs during a read operation.
* @throws InvalidPropertiesFormatException if the XML data is not a valid
* properties file.
*/
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
if (in == null) {
throw new NullPointerException("in == null");
}
if (builder == null) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new Error(e);
}
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
throw e;
}
public void error(SAXParseException e) throws SAXException {
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
});
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (systemId.equals(PROP_DTD_NAME)) {
InputSource result = new InputSource(new StringReader(PROP_DTD));
result.setSystemId(PROP_DTD_NAME);
return result;
}
throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
}
});
}
try {
Document doc = builder.parse(in);
NodeList entries = doc.getElementsByTagName("entry");
if (entries == null) {
return;
}
int entriesListLength = entries.getLength();
for (int i = 0; i < entriesListLength; i++) {
Element entry = (Element) entries.item(i);
String key = entry.getAttribute("key");
String value = entry.getTextContent();
/*
* key != null & value != null but key or(and) value can be
* empty String
*/
put(key, value);
}
} catch (IOException e) {
throw e;
} catch (SAXException e) {
throw new InvalidPropertiesFormatException(e);
}
}
use of org.xml.sax.SAXParseException in project robovm by robovm.
the class XMLFilterImplTest method testError.
public void testError() {
SAXParseException exception = new SAXParseException("Oops!", null);
try {
parent.error(exception);
} catch (SAXException e) {
throw new RuntimeException("Unexpected exception", e);
}
assertEquals(logger.size(), 1);
assertEquals("error", logger.getMethod());
assertEquals(new Object[] { exception }, logger.getArgs());
}
Aggregations