3

I am using XmlSlurper to parse an XML file for processing. I need to find the line number, of some matching criteria, within the file.

Here is what I have so far:

  def void findServlets() {
    this.servlets = webFile.depthFirst().findAll{
      it.name() == 'servlet'
    }
    println this.servlets
  }

Now I need to find the correlating line number for every matching 'servlet'. Using a SaxParser, I would do the following:

node.getUserData(SaxParser.KEY_LINE_NO)

How would I accomplish the same task on a GpathResult in groovy ?

Many thanks, Michael

2 Answers2

2

You cant do it directly, but you could decorate each element of the the parsed DOM with a line number attribute.

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.Attributes2Impl;
import javax.xml.parsers.ParserConfigurationException;

class MySlurper extends XmlSlurper {    
    public static final String LINE_NUM_ATTR = "_srmLineNum"
    Locator locator

    public MySlurper() throws ParserConfigurationException, SAXException {
        super();
    }

    @Override
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        Attributes2Impl newAttrs = new Attributes2Impl(attrs);        
        newAttrs.addAttribute(uri, LINE_NUM_ATTR, LINE_NUM_ATTR, "ENTITY", "" + locator.getLineNumber());        
        super.startElement(uri, localName, qName, newAttrs);
    }
}

def text = '''
<root>
  <a>one!</a>
  <a>two!</a>
</root>'''

def root = new MySlurper().parseText(text)

root.a.each { println it.@_srmLineNum }
preetham
  • 161
  • 6
1

Here is a slight modification of @preetham's suggestion. This allows you to set both the start and end line numbers in the XML

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.Attributes2Impl;
import javax.xml.parsers.ParserConfigurationException;

class NumberedXmlParser extends XmlSlurper {
  public static final String START_LINE = "_start"
  public static final String END_LINE = "_end"
  Locator locator
  // stack and parent are normally private variables in XmlParser
  // We create local references to these so that we can hook into them
  List<Node> localStack = new ArrayList<Node>();
  Node localParent;

  public NumberedXmlParser() throws ParserConfigurationException, SAXException {
    super();
  }

  @Override
  public void setDocumentLocator(Locator locator) {
    this.locator = locator;
  }

  @Override
  public void startDocument() throws SAXException {
    localParent = null;
    super.startDocument();
  }

  @Override
  public void endDocument() throws SAXException {
    localStack.clear();
    super.endDocument();
  }

  @Override
  public Node createNode(Node parent, Object name, Map attributes) {
    // Hack into this method to get access to the parent Node and stack
    this.localParent = new Node(parent, name, attributes);
    this.localParent["@$START_LINE"] = locator.getLineNumber();
    this.localStack.add(this.localParent);
    return this.localParent;
  }

  @Override
  public void endElement(final String uri, final String localName, final String qName) {
    if (!localStack.isEmpty()) {
      Node element = localStack.remove(localStack.size() - 1);
      element["@$END_LINE"] = locator.getLineNumber();
      if (!localStack.isEmpty()) {
        localParent = localStack.get(localStack.size() - 1);
      }
    }
    super.endElement(uri, localName, qName);
  }
}
abd3721
  • 1,374
  • 1
  • 11
  • 10