From a938b67bf3d3bd86f8dd7b1f958e81007bf6f2fe Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Fri, 4 Sep 2015 22:02:05 +0900 Subject: [PATCH] Add knowledge about the specific FB tags --- prod/net/jaekl/cfb/xml/BugClass.java | 41 ++++++++++----- prod/net/jaekl/cfb/xml/BugCollection.java | 6 +-- prod/net/jaekl/cfb/xml/BugInstance.java | 63 ++++++++++++++++++----- prod/net/jaekl/cfb/xml/BugMethod.java | 50 ++++++++++++++---- prod/net/jaekl/cfb/xml/LocalVariable.java | 40 ++++++++++++++ prod/net/jaekl/cfb/xml/SourceLine.java | 20 ++----- prod/net/jaekl/qd/xml/ParseResult.java | 23 ++++++++- 7 files changed, 187 insertions(+), 56 deletions(-) create mode 100644 prod/net/jaekl/cfb/xml/LocalVariable.java diff --git a/prod/net/jaekl/cfb/xml/BugClass.java b/prod/net/jaekl/cfb/xml/BugClass.java index 34e02a2..eb2c592 100644 --- a/prod/net/jaekl/cfb/xml/BugClass.java +++ b/prod/net/jaekl/cfb/xml/BugClass.java @@ -1,5 +1,7 @@ package net.jaekl.cfb.xml; +import java.util.ArrayList; + import org.xml.sax.Attributes; import net.jaekl.qd.xml.ParseResult; @@ -7,26 +9,37 @@ import net.jaekl.qd.xml.XmlParseException; public class BugClass extends ParseResult { - static final String ROOT_TAG = "Class"; + static final String TAG = "Class"; static final String[] INTERNAL = { }; - static final Object[][] EXTERNAL = { { SourceLine.ROOT_TAG, SourceLine.class} }; + static final Object[][] EXTERNAL = { { SourceLine.TAG, SourceLine.class} }; + static final String CLASS_NAME = "classname"; + + String m_className; + ArrayList m_sourceLines; + public BugClass() { - super(ROOT_TAG, INTERNAL, EXTERNAL); + super(TAG, INTERNAL, EXTERNAL); + m_className = ""; } @Override - public void endContents(String uri, String localName, String qName, - String chars, Attributes attr) throws XmlParseException { - // TODO Auto-generated method stub - - } - - @Override - public void endExternal(String uri, String localName, String qName) - throws XmlParseException { - // TODO Auto-generated method stub - + public void endContents(String uri, String localName, String qName, String chars, Attributes attr) + throws XmlParseException + { + m_className = getRequiredAttr(TAG, attr, CLASS_NAME); } + @Override + public void endExternal(String uri, String localName, String qName) + throws XmlParseException + { + if (SourceLine.TAG.equals(localName)) { + ParseResult[] collected = collectParsedChildren(SourceLine.class); + for (ParseResult pr : collected) { + assert(pr instanceof SourceLine); + m_sourceLines.add((SourceLine)pr); + } + } + } } diff --git a/prod/net/jaekl/cfb/xml/BugCollection.java b/prod/net/jaekl/cfb/xml/BugCollection.java index 9078e2f..b59bdc8 100644 --- a/prod/net/jaekl/cfb/xml/BugCollection.java +++ b/prod/net/jaekl/cfb/xml/BugCollection.java @@ -7,12 +7,12 @@ import net.jaekl.qd.xml.XmlParseException; public class BugCollection extends ParseResult { - static final String ROOT_TAG = "BugCollection"; + static final String TAG = "BugCollection"; static final String[] INTERNAL = { }; - static final Object[][] EXTERNAL = { { BugInstance.ROOT_TAG, BugInstance.class} }; + static final Object[][] EXTERNAL = { { BugInstance.TAG, BugInstance.class} }; public BugCollection() { - super(ROOT_TAG, INTERNAL, EXTERNAL); + super(TAG, INTERNAL, EXTERNAL); } @Override diff --git a/prod/net/jaekl/cfb/xml/BugInstance.java b/prod/net/jaekl/cfb/xml/BugInstance.java index 81ebcfc..f1f8537 100644 --- a/prod/net/jaekl/cfb/xml/BugInstance.java +++ b/prod/net/jaekl/cfb/xml/BugInstance.java @@ -1,5 +1,7 @@ package net.jaekl.cfb.xml; +import java.util.ArrayList; + import org.xml.sax.Attributes; import net.jaekl.qd.xml.ParseResult; @@ -7,28 +9,65 @@ import net.jaekl.qd.xml.XmlParseException; public class BugInstance extends ParseResult { - static final String ROOT_TAG = "BugInstance"; + static final String TAG = "BugInstance"; static final String[] INTERNAL = { }; - static final Object[][] EXTERNAL = { { BugClass.ROOT_TAG, BugClass.class}, - { BugMethod.ROOT_TAG, BugMethod.class}, - { SourceLine.ROOT_TAG, SourceLine.class} }; + static final Object[][] EXTERNAL = { { BugClass.TAG, BugClass.class}, + { BugMethod.TAG, BugMethod.class}, + { LocalVariable.TAG, LocalVariable.class}, + { SourceLine.TAG, SourceLine.class} }; + ArrayList m_classes; + ArrayList m_methods; + ArrayList m_locals; + ArrayList m_lines; + public BugInstance() { - super(ROOT_TAG, INTERNAL, EXTERNAL); + super(TAG, INTERNAL, EXTERNAL); + + m_classes = new ArrayList(); + m_methods = new ArrayList(); + m_locals = new ArrayList(); + m_lines = new ArrayList(); } @Override - public void endContents(String uri, String localName, String qName, - String chars, Attributes attr) throws XmlParseException { - // TODO Auto-generated method stub - + public void endContents(String uri, String localName, String qName, String chars, Attributes attr) + throws XmlParseException + { } @Override public void endExternal(String uri, String localName, String qName) - throws XmlParseException { - // TODO Auto-generated method stub - + throws XmlParseException + { + if (BugClass.TAG.equals(localName)) { + ParseResult[] collected = collectParsedChildren(BugClass.class); + for (ParseResult pr : collected) { + assert(pr instanceof BugClass); + m_classes.add((BugClass) pr); + } + } + else if (BugMethod.TAG.equals(localName)) { + ParseResult[] collected = collectParsedChildren(BugMethod.class); + for (ParseResult pr : collected) { + assert(pr instanceof BugMethod); + m_methods.add((BugMethod)pr); + } + } + else if (LocalVariable.TAG.equals(localName)) { + ParseResult[] collected = collectParsedChildren(LocalVariable.class); + for (ParseResult pr : collected) { + assert(pr instanceof LocalVariable); + m_locals.add((LocalVariable)pr); + } + } + else if (SourceLine.TAG.equals(localName)) { + ParseResult[] collected = collectParsedChildren(SourceLine.class); + for (ParseResult pr : collected) { + assert(pr instanceof SourceLine); + m_lines.add((SourceLine)pr); + } + } } } diff --git a/prod/net/jaekl/cfb/xml/BugMethod.java b/prod/net/jaekl/cfb/xml/BugMethod.java index 6a63b43..2e001b2 100644 --- a/prod/net/jaekl/cfb/xml/BugMethod.java +++ b/prod/net/jaekl/cfb/xml/BugMethod.java @@ -1,5 +1,7 @@ package net.jaekl.cfb.xml; +import java.util.ArrayList; + import org.xml.sax.Attributes; import net.jaekl.qd.xml.ParseResult; @@ -7,26 +9,54 @@ import net.jaekl.qd.xml.XmlParseException; public class BugMethod extends ParseResult { - static final String ROOT_TAG = "Method"; + static final String TAG = "Method"; static final String[] INTERNAL = { }; - static final Object[][] EXTERNAL = { { SourceLine.ROOT_TAG, SourceLine.class} }; + static final Object[][] EXTERNAL = { { SourceLine.TAG, SourceLine.class} }; + static final String CLASS_NAME = "classname"; + static final String METHOD_NAME = "name"; + static final String SIGNATURE = "signature"; + static final String IS_STATIC = "isStatic"; + static final String ROLE = "role"; + + static final String TRUE = "true"; // yes value for isStatic + + String m_className; + String m_methodName; + String m_signature; + boolean m_isStatic; + String m_role; // optional + ArrayList m_sourceLines; + public BugMethod() { - super(ROOT_TAG, INTERNAL, EXTERNAL); + super(TAG, INTERNAL, EXTERNAL); + m_className = m_methodName = m_signature = m_role = null; + m_isStatic = false; + m_sourceLines = new ArrayList(); } @Override - public void endContents(String uri, String localName, String qName, - String chars, Attributes attr) throws XmlParseException { - // TODO Auto-generated method stub - + public void endContents(String uri, String localName, String qName, String chars, Attributes attr) + throws XmlParseException + { + m_className = getRequiredAttr(TAG, attr, CLASS_NAME); + m_methodName = getRequiredAttr(TAG, attr, METHOD_NAME); + m_signature = getRequiredAttr(TAG, attr, SIGNATURE); + m_isStatic = getRequiredAttr(TAG, attr, IS_STATIC).equals(TRUE); + m_role = getOptionalAttr(attr, ROLE, null); } @Override public void endExternal(String uri, String localName, String qName) - throws XmlParseException { - // TODO Auto-generated method stub - + throws XmlParseException + { + if (SourceLine.TAG.equals(localName)) { + ParseResult[] collected = collectParsedChildren(SourceLine.class); + for (ParseResult pr : collected) { + assert(pr instanceof SourceLine); + m_sourceLines.add((SourceLine)pr); + } + } } } diff --git a/prod/net/jaekl/cfb/xml/LocalVariable.java b/prod/net/jaekl/cfb/xml/LocalVariable.java new file mode 100644 index 0000000..b52823f --- /dev/null +++ b/prod/net/jaekl/cfb/xml/LocalVariable.java @@ -0,0 +1,40 @@ +package net.jaekl.cfb.xml; + +import org.xml.sax.Attributes; + +import net.jaekl.qd.xml.ParseResult; +import net.jaekl.qd.xml.XmlParseException; + +public class LocalVariable extends ParseResult { + + static final String TAG = "LocalVariable"; + static final String[] INTERNAL = { }; + static final Object[][] EXTERNAL = { }; + + static final String NAME = "name"; + static final String ROLE = "role"; + + String m_name; + String m_role; + + public LocalVariable() { + super(TAG, INTERNAL, EXTERNAL); + + m_name = m_role = null; + } + + @Override + public void endContents(String uri, String localName, String qName, String chars, Attributes attr) + throws XmlParseException + { + m_name = getRequiredAttr(TAG, attr, NAME); + m_role = getRequiredAttr(TAG, attr, ROLE); + } + + @Override + public void endExternal(String uri, String localName, String qName) + throws XmlParseException + { + // no-op + } +} diff --git a/prod/net/jaekl/cfb/xml/SourceLine.java b/prod/net/jaekl/cfb/xml/SourceLine.java index c78ae3a..627667a 100644 --- a/prod/net/jaekl/cfb/xml/SourceLine.java +++ b/prod/net/jaekl/cfb/xml/SourceLine.java @@ -1,14 +1,13 @@ package net.jaekl.cfb.xml; -import org.xml.sax.Attributes; - -import net.jaekl.qd.xml.MissingAttributeException; import net.jaekl.qd.xml.ParseResult; import net.jaekl.qd.xml.XmlParseException; +import org.xml.sax.Attributes; + public class SourceLine extends ParseResult { - static final String ROOT_TAG = "SourceLine"; + static final String TAG = "SourceLine"; static final String[] INTERNAL = { }; static final Object[][] EXTERNAL = { }; @@ -21,7 +20,7 @@ public class SourceLine extends ParseResult { int m_end; public SourceLine() { - super(ROOT_TAG, INTERNAL, EXTERNAL); + super(TAG, INTERNAL, EXTERNAL); m_className = null; m_start = m_end = (-1); } @@ -41,21 +40,10 @@ public class SourceLine extends ParseResult { m_end = Integer.parseInt(scratch); } - String getRequiredAttr(String tagName, Attributes attr, String attrName) - throws MissingAttributeException - { - String result = attr.getValue(attrName); - if (null == result) { - throw new MissingAttributeException(tagName, attrName); - } - return result; - } - @Override public void endExternal(String uri, String localName, String qName) throws XmlParseException { // no-op } - } diff --git a/prod/net/jaekl/qd/xml/ParseResult.java b/prod/net/jaekl/qd/xml/ParseResult.java index 6c2c25f..e821015 100644 --- a/prod/net/jaekl/qd/xml/ParseResult.java +++ b/prod/net/jaekl/qd/xml/ParseResult.java @@ -19,6 +19,8 @@ import org.xml.sax.helpers.AttributesImpl; public abstract class ParseResult { + public static final String TAG_FIELD_NAME = "TAG"; + Stack m_current; // Name of the element that we're currently inside StringBuilder m_chars; // character content of m_current.peek() ArrayList m_childParsers; // Set of all child parsers @@ -90,7 +92,7 @@ public abstract class ParseResult ParseResult[] result = new ParseResult[collection.size()]; return collection.toArray(result); } - + // returns true if this ParseResult's context has ended with this endElement() call public boolean endElement(String uri, String localName, String qName) throws XmlParseException { @@ -179,5 +181,24 @@ public abstract class ParseResult // Default implementation is a no-op. // Override if you want to validate on endElement() } + + protected String getRequiredAttr(String tagName, Attributes attr, String attrName) + throws MissingAttributeException + { + String result = attr.getValue(attrName); + if (null == result) { + throw new MissingAttributeException(tagName, attrName); + } + return result; + } + + protected String getOptionalAttr(Attributes attr, String attrName, String defaultValue) + { + String value = attr.getValue(attrName); + if (null == value) { + return defaultValue; + } + return value; + } } -- 2.39.2