Skip to content

Commit

Permalink
Correcting exception message thrown if attribute not found #7
Browse files Browse the repository at this point in the history
 * XMLStreamReader does not have its own implementation of toString.
  • Loading branch information
tomasbjerre committed Apr 7, 2016
1 parent 98360c4 commit 5371933
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,30 @@ Changelog of Git Changelog.
**Adding pitest parser**


[d92f4e82fc3f4d6](https://github.com/tomasbjerre/git-changelog-lib/commit/d92f4e82fc3f4d6) Tomas Bjerre *2016-03-26 19:20:29*
[eeb2a624a99a7ff](https://github.com/tomasbjerre/git-changelog-lib/commit/eeb2a624a99a7ff) Tomas Bjerre *2016-03-26 19:22:06*


### GitHub [#7](https://github.com/tomasbjerre/violations-lib/issues/7)

**Correcting exception message thrown if attribute not found**

* XMLStreamReader does not have its own implementation of toString.

[d3e3260e49072d9](https://github.com/tomasbjerre/git-changelog-lib/commit/d3e3260e49072d9) Tomas Bjerre *2016-04-07 16:23:24*


### No issue

**FindBugsParser is using string matching to try to parse XML, which is**

* failing because SourceLine elements may, or may not self-terminate.
* Convert to use a StAX parser, which is likely more performant since it
* does not need to construct regexes and lists to store the results in, but
* instead will do a forwards-only stream read.
* Signed-off-by: Nigel Magnay <nigel.magnay@gmail.com>

[c6679a504380bcd](https://github.com/tomasbjerre/git-changelog-lib/commit/c6679a504380bcd) Nigel Magnay *2016-04-07 15:41:56*

**Adding Jenkins plugin link to README.md**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.of;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Integer.parseInt;
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.quote;

import java.io.File;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

import se.bjurr.violations.lib.model.Violation;

import com.google.common.base.Optional;

import javax.xml.stream.XMLStreamReader;

public abstract class ViolationsParser {

public static Optional<String> findAttribute(String in, String attribute) {
Expand Down Expand Up @@ -53,8 +59,13 @@ public static String getAttribute(String in, String attribute) {

public static String getAttribute(XMLStreamReader in, String attribute) {
String foundOpt = in.getAttributeValue("", attribute);
if( foundOpt == null)
throw new RuntimeException("\"" + attribute + "\" not found in \"" + in + "\"");
if (foundOpt == null) {
try {
throw new RuntimeException("\"" + attribute + "\" not found in:\n" + asString(in));
} catch (Exception e) {
propagate(e);
}
}
return foundOpt;
}

Expand All @@ -66,11 +77,12 @@ public static Optional<Integer> findIntegerAttribute(String in, String attribute
}

public static Optional<Integer> findIntegerAttribute(XMLStreamReader in, String attribute) {
String attr = in.getAttributeValue("",attribute);
if( attr == null )
String attr = in.getAttributeValue("", attribute);
if (attr == null) {
return Optional.absent();
else
return Optional.of( Integer.parseInt(attr) );
} else {
return Optional.of(Integer.parseInt(attr));
}
}

public static List<String> getChunks(String in, String includingStart, String includingEnd) {
Expand Down Expand Up @@ -146,6 +158,13 @@ public static List<List<String>> getLines(String string, String regexpPerLine) {
return results;
}

private static String asString(XMLStreamReader xmlr) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new StAXSource(xmlr), new StreamResult(stringWriter));
return stringWriter.toString();
}

public abstract List<Violation> parseFile(File file) throws Exception;

}

0 comments on commit 5371933

Please sign in to comment.