diff options
author | Tor Norbye <tnorbye@google.com> | 2012-01-26 15:04:57 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-01-26 15:14:37 -0800 |
commit | a2a7d644db6b742fda413600f62c64c1133a6c21 (patch) | |
tree | 5ffb4a48c46c6507670a151875927201ec93b01c /common | |
parent | 73926b1e6312781da56f29954b95e1791f4e1ee9 (diff) | |
download | sdk-a2a7d644db6b742fda413600f62c64c1133a6c21.zip sdk-a2a7d644db6b742fda413600f62c64c1133a6c21.tar.gz sdk-a2a7d644db6b742fda413600f62c64c1133a6c21.tar.bz2 |
Fix a few misc Lint issues
First, stop reporting Java-related parser errors as lint
errors. Lombok fails to parse a few files (see
http://code.google.com/p/projectlombok/issues/detail?id=311) and even
when it fails, the error messages aren't useful (see
http://code.google.com/p/projectlombok/issues/detail?id=313).
Second, the XML parser could throw an exception in some circumstances,
because it passes invalid column numbers at the end of the file. This
CL guards against that (and against any other potential errors by
wrapping all exceptions during parsing as a SAXEception).
Finally, fix the positions reported in text nodes for the ExtraText
check such that it points to the line containing the extra text, not
the beginning of the text node (which is frequently the previous
line).
Change-Id: I9630ea49d30d8afdc8bd9cf2c87ca0a0306b8ec5
Diffstat (limited to 'common')
-rw-r--r-- | common/src/com/android/util/PositionXmlParser.java | 112 |
1 files changed, 61 insertions, 51 deletions
diff --git a/common/src/com/android/util/PositionXmlParser.java b/common/src/com/android/util/PositionXmlParser.java index a72ad95..22ea4c1 100644 --- a/common/src/com/android/util/PositionXmlParser.java +++ b/common/src/com/android/util/PositionXmlParser.java @@ -458,63 +458,67 @@ public class PositionXmlParser { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - flushText(); - Element element = mDocument.createElement(qName); - for (int i = 0; i < attributes.getLength(); i++) { - if (attributes.getURI(i) != null && attributes.getURI(i).length() > 0) { - Attr attr = mDocument.createAttributeNS(attributes.getURI(i), - attributes.getQName(i)); - attr.setValue(attributes.getValue(i)); - element.setAttributeNodeNS(attr); - assert attr.getOwnerElement() == element; - } else { - Attr attr = mDocument.createAttribute(attributes.getQName(i)); - attr.setValue(attributes.getValue(i)); - element.setAttributeNode(attr); - assert attr.getOwnerElement() == element; + try { + flushText(); + Element element = mDocument.createElement(qName); + for (int i = 0; i < attributes.getLength(); i++) { + if (attributes.getURI(i) != null && attributes.getURI(i).length() > 0) { + Attr attr = mDocument.createAttributeNS(attributes.getURI(i), + attributes.getQName(i)); + attr.setValue(attributes.getValue(i)); + element.setAttributeNodeNS(attr); + assert attr.getOwnerElement() == element; + } else { + Attr attr = mDocument.createAttribute(attributes.getQName(i)); + attr.setValue(attributes.getValue(i)); + element.setAttributeNode(attr); + assert attr.getOwnerElement() == element; + } } - } - Position pos = getCurrentPosition(); - - // The starting position reported to us by SAX is really the END of the - // open tag in an element, when all the attributes have been processed. - // We have to scan backwards to find the real beginning. We'll do that - // by scanning backwards. - // -1: Make sure that when we have <foo></foo> we don't consider </foo> - // the beginning since pos.offset will typically point to the first character - // AFTER the element open tag, which could be a closing tag or a child open - // tag - - for (int offset = pos.getOffset() - 1; offset >= 0; offset--) { - char c = mXml.charAt(offset); - // < cannot appear in attribute values or anywhere else within - // an element open tag, so we know the first occurrence is the real - // element start - if (c == '<') { - // Adjust line position - int line = pos.getLine(); - for (int i = offset, n = pos.getOffset(); i < n; i++) { - if (mXml.charAt(i) == '\n') { - line--; + Position pos = getCurrentPosition(); + + // The starting position reported to us by SAX is really the END of the + // open tag in an element, when all the attributes have been processed. + // We have to scan backwards to find the real beginning. We'll do that + // by scanning backwards. + // -1: Make sure that when we have <foo></foo> we don't consider </foo> + // the beginning since pos.offset will typically point to the first character + // AFTER the element open tag, which could be a closing tag or a child open + // tag + + for (int offset = pos.getOffset() - 1; offset >= 0; offset--) { + char c = mXml.charAt(offset); + // < cannot appear in attribute values or anywhere else within + // an element open tag, so we know the first occurrence is the real + // element start + if (c == '<') { + // Adjust line position + int line = pos.getLine(); + for (int i = offset, n = pos.getOffset(); i < n; i++) { + if (mXml.charAt(i) == '\n') { + line--; + } } - } - // Compute new column position - int column = 0; - for (int i = offset; i >= 0; i--, column++) { - if (mXml.charAt(i) == '\n') { - break; + // Compute new column position + int column = 0; + for (int i = offset; i >= 0; i--, column++) { + if (mXml.charAt(i) == '\n') { + break; + } } - } - pos = createPosition(line, column, offset); - break; + pos = createPosition(line, column, offset); + break; + } } - } - element.setUserData(POS_KEY, pos, null); - mStack.add(element); + element.setUserData(POS_KEY, pos, null); + mStack.add(element); + } catch (Exception t) { + throw new SAXException(t); + } } @Override @@ -547,9 +551,10 @@ public class PositionXmlParser { // Compute offset incrementally now that we have the new line and column // numbers - while (mCurrentLine < line) { + int xmlLength = mXml.length(); + while (mCurrentLine < line && mCurrentOffset < xmlLength) { char c = mXml.charAt(mCurrentOffset); - if (c == '\r' && mCurrentOffset < mXml.length() - 1) { + if (c == '\r' && mCurrentOffset < xmlLength - 1) { if (mXml.charAt(mCurrentOffset + 1) != '\n') { mCurrentLine++; mCurrentColumn = 0; @@ -564,6 +569,11 @@ public class PositionXmlParser { } mCurrentOffset += column - mCurrentColumn; + if (mCurrentOffset >= xmlLength) { + // The parser sometimes passes wrong column numbers at the + // end of the file: Ensure that the offset remains valid. + mCurrentOffset = xmlLength; + } mCurrentColumn = column; return createPosition(mCurrentLine, mCurrentColumn, mCurrentOffset); |