Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » ServerTools (WTP) » Line number for a node in XML?
Line number for a node in XML? [message #190350] Thu, 22 March 2007 22:54 Go to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Is there an available way to simply get a line number for a given Node in
a document? I've searched and searched, but I can't seem to find anything.

This is an xml document in a file in a project. I have a Document and
Node. Is there anything else I might need?

- Paul
Re: Line number for a node in XML? [message #190389 is a reply to message #190350] Fri, 23 March 2007 21:29 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 4435
Registered: July 2009
Senior Member

Paul Fullbright wrote:
> Is there an available way to simply get a line number for a given Node
> in a document? I've searched and searched, but I can't seem to find
> anything.
>
> This is an xml document in a file in a project. I have a Document and
> Node. Is there anything else I might need?

If it's an SSE IDOMNode, you can ask it for its start offset and
then use the text IDocument to map that offset to a line number.
Otherwise, there's no simple way to do it--the standard interfaces
don't support that level of detail between the DOM and text
representations.

--
Nitin Dahyabhai
Structured Source Editor


_
Nitin Dahyabhai
Eclipse Web Tools Platform
Re: Line number for a node in XML? [message #190443 is a reply to message #190389] Mon, 26 March 2007 15:55 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
> If it's an SSE IDOMNode, you can ask it for its start offset and
> then use the text IDocument to map that offset to a line number.
> Otherwise, there's no simple way to do it--the standard interfaces
> don't support that level of detail between the DOM and text
> representations.

Ah yes! That's exactly what I need. Second question: what's the best way
to get an IDocument for an XML file?
Re: Line number for a node in XML? [message #190451 is a reply to message #190443] Mon, 26 March 2007 16:11 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Paul Fullbright wrote:


>> If it's an SSE IDOMNode, you can ask it for its start offset and
>> then use the text IDocument to map that offset to a line number.
>> Otherwise, there's no simple way to do it--the standard interfaces
>> don't support that level of detail between the DOM and text
>> representations.

> Ah yes! That's exactly what I need. Second question: what's the best way
> to get an IDocument for an XML file?

I should add that we are a) basing this off a possibly changing XML file,
and b) we don't want to add "UI" dependencies. (that is, this is used
during validation and should be usable in a headless environment, if
needed)
Re: Line number for a node in XML? [message #190468 is a reply to message #190451] Mon, 26 March 2007 17:24 Go to previous messageGo to next message
Thomas Hallgren is currently offline Thomas HallgrenFriend
Messages: 3240
Registered: July 2009
Senior Member
Paul Fullbright wrote:
> Paul Fullbright wrote:
>
>
> I should add that we are a) basing this off a possibly changing XML
> file, and b) we don't want to add "UI" dependencies. (that is, this is
> used during validation and should be usable in a headless environment,
> if needed)
>
You could try using a SAX based parser instead of a DOM tree. Most of
them (the one bundled with Java 1.5 for instance) support the document
locator API. Take a look at org.xml.sax.Locator and
org.xml.sax.ContentHandler.

Kind Regards,
Thomas Hallgren
Re: Line number for a node in XML? [message #190476 is a reply to message #190468] Mon, 26 March 2007 17:35 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Thomas Hallgren wrote:

> You could try using a SAX based parser instead of a DOM tree. Most of
> them (the one bundled with Java 1.5 for instance) support the document
> locator API. Take a look at org.xml.sax.Locator and
> org.xml.sax.ContentHandler.

Ah, I should further add that we are using the translator framework, so
we're not actually doing any of the XML parsing ourselves. We can get a
handle on the node, and we know which file we're working with, but other
than that, we're a little hamstrung.

Thanks, though!
Re: Line number for a node in XML? [message #190485 is a reply to message #190451] Mon, 26 March 2007 19:43 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 4435
Registered: July 2009
Senior Member

Paul Fullbright wrote:
>> Ah yes! That's exactly what I need. Second question: what's the best
>> way to get an IDocument for an XML file?

If it's a SSE IDOMNode, you can ask it for its "structured document"
as that's the corresponding IDocument.

> I should add that we are a) basing this off a possibly changing XML
> file, and b) we don't want to add "UI" dependencies. (that is, this is
> used during validation and should be usable in a headless environment,
> if needed)

Headless access to the models can be had using the model manager
instance returned from
org.eclipse.wst.sse.core.StructuredModelManager.getModelMana ger().
You'll want to use a getModelForEdit if you plan to save the
changes, and be sure to release the model when you're done with it.

--
Nitin Dahyabhai
Structured Source Editor


_
Nitin Dahyabhai
Eclipse Web Tools Platform
Re: Line number for a node in XML? [message #190491 is a reply to message #190485] Mon, 26 March 2007 19:54 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Nitin Dahyabhai wrote:

>>> Ah yes! That's exactly what I need. Second question: what's the best
>>> way to get an IDocument for an XML file?

> If it's a SSE IDOMNode, you can ask it for its "structured document"
> as that's the corresponding IDocument.

Thanks a ton! That was a lot simpler than I was making it, apparently.
Looks like I was lost in the maze of interfaces.

So given an IDOMNode, all you really need to do is:

int lineNumber =
node.getStructuredDocument().getLineOfOffset(node.getStartOf fset());

Will test this and post results.


>> I should add that we are a) basing this off a possibly changing XML
>> file, and b) we don't want to add "UI" dependencies. (that is, this is
>> used during validation and should be usable in a headless environment,
>> if needed)

> Headless access to the models can be had using the model manager
> instance returned from
> org.eclipse.wst.sse.core.StructuredModelManager.getModelMana ger().
> You'll want to use a getModelForEdit if you plan to save the
> changes, and be sure to release the model when you're done with it.

If done headlessly, we won't be making changes to the documents. It would
be purely a way to report validation errors as part of a build process. I
just wanted to make sure I could find a way to access line numbers without
resorting to "visible" (e.g. widgets and editors) UI code. (I also
believe that the translator framework does the model reading and writing
part, so if they can work headlessly, so can we.)

Thanks again!
- Paul
Re: Line number for a node in XML? [message #190499 is a reply to message #190491] Mon, 26 March 2007 20:05 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Paul Fullbright wrote:

>> If it's a SSE IDOMNode, you can ask it for its "structured document"
>> as that's the corresponding IDocument.

> Thanks a ton! That was a lot simpler than I was making it, apparently.
> Looks like I was lost in the maze of interfaces.

> So given an IDOMNode, all you really need to do is:

> int lineNumber =
> node.getStructuredDocument().getLineOfOffset(node.getStartOf fset());

> Will test this and post results.


Well, it's very, very close. For some reason, that actually returns one
less than the actual line number. I can't find a consistent reason that
it would be like that, so for now I'll just use

int lineNumber =
node.getStructuredDocument().getLineOfOffset(node.getStartOf fset()) + 1;


Thanks again!
- Paul
Re: Line number for a node in XML? [message #190579 is a reply to message #190499] Wed, 28 March 2007 17:48 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 4435
Registered: July 2009
Senior Member

Paul Fullbright wrote:
> Well, it's very, very close. For some reason, that actually returns one
> less than the actual line number. I can't find a consistent reason that
> it would be like that, so for now I'll just use
>
> int lineNumber =
> node.getStructuredDocument().getLineOfOffset(node.getStartOf fset()) + 1;

It's simply that programmers and code like to start counting by
using "zero", but people count starting with "one".

--
Nitin Dahyabhai
Structured Source Editor


_
Nitin Dahyabhai
Eclipse Web Tools Platform
Re: Line number for a node in XML? [message #190587 is a reply to message #190579] Wed, 28 March 2007 17:52 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Nitin Dahyabhai wrote:

> It's simply that programmers and code like to start counting by
> using "zero", but people count starting with "one".

I did think of that, but when you say something like "line number", I'd
think it would match up with the *actual* line number. There's no line 0,
at least in eclipse.
Re: Line number for a node in XML? [message #190601 is a reply to message #190587] Wed, 28 March 2007 18:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ns_dkerber.ns_WarrenRogersAssociates.com

In article <7fda378034efca9b4aaf7ebac3646257$1@www.eclipse.org>,
paul.fullbright@oracle.com says...
> Nitin Dahyabhai wrote:
>
> > It's simply that programmers and code like to start counting by
> > using "zero", but people count starting with "one".
>
> I did think of that, but when you say something like "line number", I'd
> think it would match up with the *actual* line number. There's no line 0,
> at least in eclipse.

But in java, the first character position in a string is number 0, so
it's consistent with java standards.

--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
Re: Line number for a node in XML? [message #190609 is a reply to message #190601] Wed, 28 March 2007 19:17 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
David Kerber wrote:

> paul.fullbright@oracle.com says...
>> Nitin Dahyabhai wrote:
>>
>> > It's simply that programmers and code like to start counting by
>> > using "zero", but people count starting with "one".
>>
>> I did think of that, but when you say something like "line number", I'd
>> think it would match up with the *actual* line number. There's no line 0,
>> at least in eclipse.

> But in java, the first character position in a string is number 0, so
> it's consistent with java standards.

True, but when you have line numbers turned on in your editor, and you
have an error on line X, and the error's location is "line X-1", I'd think
those standards aren't the ones to be applying.
Re: Line number for a node in XML? [message #190617 is a reply to message #190609] Wed, 28 March 2007 19:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Paul,

I ran into this type of issue long ago as well. But clearly it can't be
changed anymore, so whatever the reason, good or bad, it's just that way
now...


Paul Fullbright wrote:
> David Kerber wrote:
>
>> paul.fullbright@oracle.com says...
>>> Nitin Dahyabhai wrote:
>>>
>>> > It's simply that programmers and code like to start counting by >
>>> using "zero", but people count starting with "one".
>>>
>>> I did think of that, but when you say something like "line number",
>>> I'd think it would match up with the *actual* line number. There's
>>> no line 0, at least in eclipse.
>
>> But in java, the first character position in a string is number 0, so
>> it's consistent with java standards.
>
> True, but when you have line numbers turned on in your editor, and you
> have an error on line X, and the error's location is "line X-1", I'd
> think those standards aren't the ones to be applying.
>
Re: Line number for a node in XML? [message #190853 is a reply to message #190617] Fri, 30 March 2007 22:13 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 201
Registered: July 2009
Senior Member
Ed Merks wrote:

> Paul,

> I ran into this type of issue long ago as well. But clearly it can't be
> changed anymore, so whatever the reason, good or bad, it's just that way
> now...

Ok, if it's that way for that reason, at least I know my solution will
always work. I was wondering if there were some other reason it was off,
and whether or not it would always be 1 different. :)

- Paul

(BTW, is January 1st in Java-land 0/0 ? Musings on a Friday evening ....)
Re: Line number for a node in XML? [message #190861 is a reply to message #190853] Sat, 31 March 2007 03:57 Go to previous message
Eclipse UserFriend
Originally posted by: first.last.oracle.com

> (BTW, is January 1st in Java-land 0/0 ? Musings on a Friday evening ....)
>

No, it's even funnier: 0/1.
Previous Topic:IDE for struts-enabled web application
Next Topic:NoSuchMethodError:
Goto Forum:
  


Current Time: Fri Apr 19 01:22:23 GMT 2024

Powered by FUDForum. Page generated in 0.03008 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top