groovy

Extend XmlParser and Node to allow subclassing, permitting additional capabilities to reuse basic functionality

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.5
  • Fix Version/s: 1.6-rc-1, 1.7-beta-1
  • Component/s: XML Processing
  • Labels:
    None
  • Patch Submitted:
    Yes
  • Number of attachments :
    4

Description

For uses of XML parsing that go beyond the basic capabilities provided by XmlParser and Node, it would be good to be able to reuse the functionality provided by these classes, by subclassing them.

Currently this is not possible. This "improvement" is to alter these classes in a compatible way to allow subclassing, and to describe how to do this in the Documentation beneath the http://groovy.codehaus.org/Processing+XML page.

For example, one extension that I've done allows the parse result to record the file path, line number, and column number of the elements - these can be quite useful if you have some processing that can generate messages about the XML back to the user.

See the thread in the user mailing list for some more discussion on this: http://archive.codehaus.org/groovy/user/4765F82F.9030601@schor.com

  1. Node.patch
    19/Dec/07 8:47 PM
    2 kB
    Marshall Schor
  2. NodeWithLoc.java
    01/Jan/08 4:24 PM
    2 kB
    Marshall Schor
  3. XmlParser.patch
    19/Dec/07 8:47 PM
    3 kB
    Marshall Schor
  4. XmlParserExt.java
    01/Jan/08 4:24 PM
    3 kB
    Marshall Schor

Issue Links

Activity

Hide
Marshall Schor added a comment -

A suggest approach to resolving this issue

Show
Marshall Schor added a comment - A suggest approach to resolving this issue
Hide
Guillaume Laforge added a comment -

Paul, what do you think of this patch?

Show
Guillaume Laforge added a comment - Paul, what do you think of this patch?
Hide
Paul King added a comment -

Something like this would be a useful improvement. I haven't had time to fully digest it yet. It might be OK as is or maybe could be improved. I'll have a closer look shortly.

Show
Paul King added a comment - Something like this would be a useful improvement. I haven't had time to fully digest it yet. It might be OK as is or maybe could be improved. I'll have a closer look shortly.
Hide
Paul King added a comment -

Marshall,

Would you consider sharing any of the extensions you are working on? Do you think they are generally applicable?

I guess in the first instance, it would be kind of useful to have something to use as an example to make sure I get the extensions right if I do make changes. And I guess if they are generally appicable, might make useful additions anyway.

Paul.

Show
Paul King added a comment - Marshall, Would you consider sharing any of the extensions you are working on? Do you think they are generally applicable? I guess in the first instance, it would be kind of useful to have something to use as an example to make sure I get the extensions right if I do make changes. And I guess if they are generally appicable, might make useful additions anyway. Paul.
Hide
Marshall Schor added a comment -

Sure. The extensions I'm doing are 2:

1) incorporating the "location" information for each XML element in the Node This allows me to issue error messsages such as "element <xyz> has an invalid value for attiribute xxx, in file [substitute the file name here from the location], line {substitute the line number} near column {substitute the column number}.

2) The XML uses a name space, but only one name space. So the XML parser is set up to be name-space aware, and it generates QNames for the element names. But because there is only one namespace being used, it's more convenient to just replace those with non-name-space plain strings.

Perhaps there is an better way to do this, though. (I'm not an XML expert .

I'll see about attaching some code, as an example.

Show
Marshall Schor added a comment - Sure. The extensions I'm doing are 2: 1) incorporating the "location" information for each XML element in the Node This allows me to issue error messsages such as "element <xyz> has an invalid value for attiribute xxx, in file [substitute the file name here from the location], line {substitute the line number} near column {substitute the column number}. 2) The XML uses a name space, but only one name space. So the XML parser is set up to be name-space aware, and it generates QNames for the element names. But because there is only one namespace being used, it's more convenient to just replace those with non-name-space plain strings. Perhaps there is an better way to do this, though. (I'm not an XML expert . I'll see about attaching some code, as an example.
Hide
Marshall Schor added a comment -

Here is a sample of an extension to XmlParser using the proposed patches. There is a subclass extension of the Node class to provide additional fields to hold the "location" information. And there is a subclass of the XmlParser class that makes instances of this new Node class and adds the location info.

I also included in the Node class a setAttributes to allow groovy statements to set the value of the attributes, e.g.
myNode.@myattribute = value

This is probably worth adding to the base Node.

Show
Marshall Schor added a comment - Here is a sample of an extension to XmlParser using the proposed patches. There is a subclass extension of the Node class to provide additional fields to hold the "location" information. And there is a subclass of the XmlParser class that makes instances of this new Node class and adds the location info. I also included in the Node class a setAttributes to allow groovy statements to set the value of the attributes, e.g. myNode.@myattribute = value This is probably worth adding to the base Node.
Hide
Paul King added a comment -

GROOVY-2429 is now resolved which covers the possibility of creating custom nodes and custom name classes. Other changes still pending.

Show
Paul King added a comment - GROOVY-2429 is now resolved which covers the possibility of creating custom nodes and custom name classes. Other changes still pending.
Hide
Paul King added a comment -

I realise it has been a while but I would be keen to close this issue before its first birthday! If you have any comments on what changes are still required to allow you to meet your original objectives, that would be great, otherwise I will try to determine what I think is required over the next week. You will need to build the latest version from trunk or the 1.6 branch (or grab a jar out of the snapshot repo) to get the changes to date.

Show
Paul King added a comment - I realise it has been a while but I would be keen to close this issue before its first birthday! If you have any comments on what changes are still required to allow you to meet your original objectives, that would be great, otherwise I will try to determine what I think is required over the next week. You will need to build the latest version from trunk or the 1.6 branch (or grab a jar out of the snapshot repo) to get the changes to date.
Hide
Paul King added a comment -

Just made a minor update to groovy.util.Node which along with the earlier changes to GROOVY-2429 I think covers the requests given in this issue. Closing but if you find any other specific tweaks you still need please open a new specfiic issue. Thanks.

Show
Paul King added a comment - Just made a minor update to groovy.util.Node which along with the earlier changes to GROOVY-2429 I think covers the requests given in this issue. Closing but if you find any other specific tweaks you still need please open a new specfiic issue. Thanks.

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: