Home » Language IDEs » ServerTools (WTP) » Line number for a node in XML?
|
Re: Line number for a node in XML? [message #190389 is a reply to message #190350] |
Fri, 23 March 2007 21:29 |
|
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 #190468 is a reply to message #190451] |
Mon, 26 March 2007 17:24 |
|
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 #190485 is a reply to message #190451] |
Mon, 26 March 2007 19:43 |
|
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 |
Paul Fullbright 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 #190579 is a reply to message #190499] |
Wed, 28 March 2007 17:48 |
|
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 #190601 is a reply to message #190587] |
Wed, 28 March 2007 18:21 |
Eclipse User |
|
|
|
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 |
Paul Fullbright 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 |
Eclipse User |
|
|
|
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.
>
|
|
| | |
Goto Forum:
Current Time: Thu Sep 19 05:36:22 GMT 2024
Powered by FUDForum. Page generated in 0.05295 seconds
|