Return-Path: Delivered-To: apmail-synapse-dev-archive@www.apache.org Received: (qmail 13426 invoked from network); 21 Jun 2009 12:02:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 21 Jun 2009 12:01:59 -0000 Received: (qmail 7209 invoked by uid 500); 21 Jun 2009 12:02:10 -0000 Delivered-To: apmail-synapse-dev-archive@synapse.apache.org Received: (qmail 7126 invoked by uid 500); 21 Jun 2009 12:02:10 -0000 Mailing-List: contact dev-help@synapse.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@synapse.apache.org Delivered-To: mailing list dev@synapse.apache.org Received: (qmail 7118 invoked by uid 99); 21 Jun 2009 12:02:10 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Jun 2009 12:02:10 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of esaliya@gmail.com designates 209.85.200.169 as permitted sender) Received: from [209.85.200.169] (HELO wf-out-1314.google.com) (209.85.200.169) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Jun 2009 12:02:00 +0000 Received: by wf-out-1314.google.com with SMTP id 26so1100241wfd.0 for ; Sun, 21 Jun 2009 05:01:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=wESCFNwjEXqplNJdBCXhGUwtfAmqo6Vuvs0cVTSzbig=; b=T62pwVU1eGOMf4SkI+AV7DtSCFnDXNLFEidpCrUQfXZUeIJsFIYyO5sJh87EErnJkR O2OQAeUpQNT4oL55szdcP2GWJ9vKdgQXmpedt8eXYjff54z4do9r3w2G9gQvi92zfDQv QeEOIw5PkLcMB0bJzpzIR6Goksyw6I0FGE3Oo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=PUYV/+WCXT/+cdv+zeQUek/Blpyqb6DM2OnZT7L78f1at+fBd1nx3lHZkeQX8jZyB8 RXLyLaRFS8xNvQR5+1Erx2EtTFnIt9unUFnApbUL5ZsqjIoAZ7Cg/IqXj/WAmGh0ItN6 IXl7LJ5v2B1fqr4BCPd1lb2KrhZPcG63Wta8w= MIME-Version: 1.0 Received: by 10.142.223.4 with SMTP id v4mr2196046wfg.11.1245585698332; Sun, 21 Jun 2009 05:01:38 -0700 (PDT) In-Reply-To: References: <17d2d1290906200910l4d9415cfmc9db167fd20214c3@mail.gmail.com> Date: Sun, 21 Jun 2009 17:31:38 +0530 Message-ID: <17d2d1290906210501n255f1550i4790e36e17f42ea0@mail.gmail.com> Subject: Re: XPath evaluation doesn't work on resources picked from registry From: Saliya Ekanayake To: dev@synapse.apache.org Content-Type: multipart/alternative; boundary=000e0cd255341621a8046cda85ec X-Virus-Checked: Checked by ClamAV on apache.org --000e0cd255341621a8046cda85ec Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi Indika, I tested your point and found the cause. Simply, in the second case there is no parent for the root element. Therefore, evaluating an XPath containing element will cause an exception. But if you evaluate an XPath like, //anotherthing/insideanother, you will get the desired result. So the solution I would like to suggest in this case is to first check the parent of the element. If the parent is an instance of OMDocument then we need not detach (if you need to build then just use OMNode#build()). If the parent is an instance of OMElement then you need to detach since you want the element to be isolated from the connecting tree. Thanks, Saliya On Sun, Jun 21, 2009 at 11:06 AM, indika kumara wrote: > Hi Saliya > > Following shown my scenarios. > > > Scenario 1 - without detach on root element > > String str = "wow > nicegreat"; > XMLStreamReader parser = XMLInputFactory.newInstance(). > createXMLStreamReader(new > ByteArrayInputStream(str.getBytes())); > StAXOMBuilder builder = new StAXOMBuilder(parser); > OMElement root = builder.getDocumentElement(); > *// root.detach();* > > AXIOMXPath xpath = new AXIOMXPath("//hello/something"); > OMElement node = (OMElement) xpath.selectSingleNode(root); > System.out.println(node == null); > if (node != null) { > System.out.println(node.getText()); > } > > > Out put : > > false > * wow nice* > > > Scenario 2 - with detach on root element > > String str = "wow > nicegreat"; > XMLStreamReader parser = XMLInputFactory.newInstance(). > createXMLStreamReader(new > ByteArrayInputStream(str.getBytes())); > StAXOMBuilder builder = new StAXOMBuilder(parser); > OMElement root = builder.getDocumentElement(); > * root.detach();* > > AXIOMXPath xpath = new AXIOMXPath("//hello/something"); > OMElement node = (OMElement) xpath.selectSingleNode(root); > System.out.println(node == null); > if (node != null) { > System.out.println(node.getText()); > } > > Output > > true > > > In second case , XPath have not been evaluated correctly. > > Thanks > Indika > > On Sat, Jun 20, 2009 at 9:40 PM, Saliya Ekanayake wrote: > >> Hi, >> >> Um, I am not really clarified with the problem here. I agree that the full >> tree should be built before we can evaluate XPath. Adding the element to a >> new OMDocument, however, just to get this done seems not right. >> Additionally, the original OMDocument created by the builder will be there >> even if you detach the element. Detach will only remove the element from the >> tree to which it belongs. To check this, you can get the builder from the >> detached element and ask for the OMDocument. IMHO, we can call >> result.build() if necessary. >> >> Btw. I am not clear why XPath doesn't work on the detached element. I did >> a simple test as follows and XPath worked, may be I have missed something >> (please let me know if so). >> >> public static void main(String[] args) throws XMLStreamException, >> JaxenException { >> String str = "wow >> nicegreat"; >> StAXOMBuilder builder = new StAXOMBuilder(new >> ByteArrayInputStream(str.getBytes())); >> OMElement root = builder.getDocumentElement(); >> >> OMDocument doc = builder.getDocument(); >> System.out.println(doc == null); >> >> OMElement anotherthing = root.getFirstChildWithName(new >> QName("anotherthing")); >> anotherthing.detach(); >> >> doc = ((StAXOMBuilder)anotherthing.getBuilder()).getDocument(); >> System.out.println(doc == null); >> >> AXIOMXPath xpath = new AXIOMXPath("//insideanother"); >> OMElement node = (OMElement) xpath.selectSingleNode(anotherthing); >> System.out.println(node.getText()); >> } >> >> Thanks, >> Saliya >> >> On Sat, Jun 20, 2009 at 4:45 AM, Andreas Veithen < >> andreas.veithen@gmail.com> wrote: >> >>> StAXOMBuilder actually already creates an OMDocument (which can be >>> retrieved by the getDocument method). The important thing is that we >>> need to make sure that the Axiom tree is fully built before closing >>> the input stream. I guess that the detach method is used because it >>> has the side effect of fully building the element and because >>> OMDocument has no method to build the entire tree (see WSCOMMONS-479). >>> >>> This gives us two solutions: >>> >>> - Use StAXOMBuilder#getDocument and iterate over its children to make >>> sure the document is fully built. >>> - Continue to use "detach" and add the element to a new document, as >>> you suggested. Note that you should not use OMDocumentImpl directly, >>> but create it using the OMFactory. >>> >>> Andreas >>> >>> On Fri, Jun 19, 2009 at 09:30, indika kumara >>> wrote: >>> > Devs >>> > >>> > $subject is due to we do 'detach()' on picked resource OMElement . >>> > If I add detached element to a OMDocument as a child, it works >>> > >>> > Existing code SImpleURLRegistry >>> > >>> > result.detach(); >>> > inputStream.close(); >>> > >>> > >>> > Modified code >>> > >>> > result.detach(); >>> > OMDocumentImpl omDocument = new OMDocumentImpl(); >>> > omDocument.addChild(result); >>> > inputStream.close(); >>> > >>> > >>> > Are there any best solution other than what I did ? I haven't deep >>> > AXIOM knowledge? Could anyone help me? >>> > >>> > Thanks >>> > Indika >>> > >>> > --------------------------------------------------------------------- >>> > To unsubscribe, e-mail: dev-unsubscribe@synapse.apache.org >>> > For additional commands, e-mail: dev-help@synapse.apache.org >>> > >>> > >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: dev-unsubscribe@synapse.apache.org >>> For additional commands, e-mail: dev-help@synapse.apache.org >>> >>> >> >> >> -- >> Saliya Ekanayake >> http://www.esaliya.blogspot.com >> http://www.esaliya.wordpress.com >> > > -- Saliya Ekanayake http://www.esaliya.blogspot.com http://www.esaliya.wordpress.com --000e0cd255341621a8046cda85ec Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi Indika,

I tested your point and found the cause. Simply, in the s= econd case there is no parent for the root element. Therefore, evaluating a= n XPath containing <hello> element will cause an exception. But if yo= u evaluate an XPath like, //anotherthing/insideanother, you will get the de= sired result.

So the solution I would like to suggest in this case is to first check = the parent of the element. If the parent is an instance of OMDocument then = we need not detach (if you need to build then just use OMNode#build()). If = the parent is an instance of OMElement then you need to detach since you wa= nt the element to be isolated from the connecting tree.

Thanks,
Saliya

On Sun, Jun 21, 200= 9 at 11:06 AM, indika kumara <indika.kuma@gmail.com> wrote:
=A0=A0=A0=A0 =A0=A0 Hi Saliya
=A0 =A0 =A0=A0
=A0 =A0 =A0 =A0 Follow= ing shown my scenarios.

=A0
=A0 =A0 =A0=A0 Scenario 1=A0 - with= out detach on root element

=A0=A0=A0=A0=A0=A0=A0 = String str =3D "<hello><something>wow nice</something&g= t;<anotherthing><insideanother>great</insideanother></= anotherthing></hello>";
=A0=A0=A0=A0=A0=A0=A0 XMLStreamReader parser =3D XMLInputFactory.newInstanc= e().
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 createXMLStreamReader= (new ByteArrayInputStream(str.getBytes()));
=A0=A0=A0=A0=A0=A0=A0 StAXOM= Builder builder =3D new StAXOMBuilder(parser);
=A0=A0=A0=A0=A0=A0=A0 OME= lement root =3D builder.getDocumentElement();
//=A0=A0=A0=A0=A0=A0=A0 root.detach();

=A0=A0=A0=A0=A0=A0=A0 = AXIOMXPath xpath =3D new AXIOMXPath("//hello/something");
=A0= =A0=A0=A0=A0=A0=A0 OMElement node =3D (OMElement) xpath.selectSingleNode(ro= ot);
=A0=A0=A0=A0=A0=A0=A0 System.out.println(node =3D=3D null);
=A0=A0=A0=A0=A0=A0=A0 if (node !=3D null) {

=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 System.out.println(node.getText());
=A0=A0=A0= =A0=A0=A0=A0 }


=A0=A0=A0 =A0 Out put :

=A0 =A0 =A0= =A0 false
=A0 =A0=A0=A0=A0 wow nice


=A0 =A0 =A0=A0 Sce= nario 2 - with=A0 detach on root element

=A0=A0 =A0 =A0=A0 String str =3D "<hello><something>wo= w nice</something><anotherthing><insideanother>great</= insideanother></anotherthing></hello>";
=A0=A0= =A0=A0=A0=A0=A0 XMLStreamReader parser =3D XMLInputFactory.newInstance(). =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 createXMLStreamReader(new Byt= eArrayInputStream(str.getBytes()));
=A0=A0=A0=A0=A0=A0=A0 StAXOMBuilder = builder =3D new StAXOMBuilder(parser);
=A0=A0=A0=A0=A0=A0=A0 OMElement r= oot =3D builder.getDocumentElement();
=A0=A0=A0=A0=A0=A0=A0 root.deta= ch();

=A0=A0=A0=A0=A0=A0=A0 AXIOMXPath xpath =3D new AXIOMXPath("//hello= /something");
=A0=A0=A0=A0=A0=A0=A0 OMElement node =3D (OMElement) = xpath.selectSingleNode(root);
=A0=A0=A0=A0=A0=A0=A0 System.out.println(n= ode =3D=3D null);
=A0=A0=A0=A0=A0=A0=A0 if (node !=3D null) {

=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 System.out.println(node.getText());
= =A0=A0=A0=A0=A0=A0=A0 }
=A0=A0=A0=A0=A0=A0
=A0=A0=A0=A0=A0=A0 = Output

=A0=A0=A0=A0=A0=A0 true


=A0=A0 In second case , = XPath=A0 have not been evaluated correctly.

Thanks
Indika

On Sat, Jun 20, 2009 at 9:40 PM, Saliya Ekanayake <esaliya@gmail.com&g= t; wrote:
Hi,

Um, I am not really clarified with the problem here. I agree tha= t the full tree should be built before we can evaluate XPath. Adding the el= ement to a new OMDocument, however, just to get this done seems not right. = Additionally, the original OMDocument created by the builder will be there = even if you detach the element. Detach will only remove the element from th= e tree to which it belongs. To check this, you can get the builder from the= detached element and ask for the OMDocument.=A0 IMHO, we can call result.b= uild() if necessary.

Btw. I am not clear why XPath doesn't work on the detached element.= I did a simple test as follows and XPath worked, may be I have missed some= thing (please let me know if so).

=A0=A0=A0 public static void main(= String[] args) throws XMLStreamException, JaxenException {
=A0=A0=A0=A0=A0=A0=A0 String str =3D "<hello><something>wo= w nice</something><anotherthing><insideanother>great</= insideanother></anotherthing></hello>";
=A0=A0=A0=A0= =A0=A0=A0 StAXOMBuilder builder =3D new StAXOMBuilder(new ByteArrayInputStr= eam(str.getBytes()));
=A0=A0=A0=A0=A0=A0=A0 OMElement root =3D builder.getDocumentElement();
= =A0=A0=A0=A0=A0=A0=A0
=A0=A0=A0=A0=A0=A0=A0 OMDocument doc =3D builder.= getDocument();
=A0=A0=A0=A0=A0=A0=A0 System.out.println(doc =3D=3D null)= ;

=A0=A0=A0=A0=A0=A0=A0 OMElement anotherthing =3D root.getFirstChil= dWithName(new QName("anotherthing"));
=A0=A0=A0=A0=A0=A0=A0 anotherthing.detach();

=A0=A0=A0=A0=A0=A0=A0 d= oc =3D ((StAXOMBuilder)anotherthing.getBuilder()).getDocument();
=A0=A0= =A0=A0=A0=A0=A0 System.out.println(doc =3D=3D null);

=A0=A0=A0=A0=A0= =A0=A0 AXIOMXPath xpath =3D new AXIOMXPath("//insideanother"); =A0=A0=A0=A0=A0=A0=A0 OMElement node =3D (OMElement) xpath.selectSingleNode= (anotherthing);
=A0=A0=A0=A0=A0=A0=A0 System.out.println(node.getText())= ;
=A0=A0=A0 }

Thanks,
Saliya

On Sat, Jun 20, 2009 at 4:45 AM, Andreas Veithen &l= t;andreas.ve= ithen@gmail.com> wrote:
StAXOMBuilder actually already creates an OMDocument (which can b= e
retrieved by the getDocument method). The important thing is that we
need to make sure that the Axiom tree is fully built before closing
the input stream. I guess that the detach method is used because it
has the side effect of fully building the element and because
OMDocument has no method to build the entire tree (see WSCOMMONS-479).

This gives us two solutions:

- Use StAXOMBuilder#getDocument and iterate over its children to make
sure the document is fully built.
- Continue to use "detach" and add the element to a new document,= as
you suggested. Note that you should not use OMDocumentImpl directly,
but create it using the OMFactory.

Andreas

On Fri, Jun 19, 2009 at 09:30, indika kumara<indika.kuma@gmail.com> wrote:
> Devs
>
> $subject is due to we do =A0'detach()' =A0on picked resource O= MElement .
> If I add detached element to a OMDocument as a child, it works
>
> Existing code =A0SImpleURLRegistry
>
> result.detach();
> inputStream.close();
>
>
> Modified code
>
> result.detach();
> OMDocumentImpl omDocument =3D new OMDocumentImpl();
> omDocument.addChild(result);
> inputStream.close();
>
>
> Are there any best solution other than what I did ? =A0I haven't d= eep
> AXIOM knowledge? =A0Could anyone help me?
>
> Thanks
> Indika
>
> ---------------------------------------------------------------------<= br> > To unsubscribe, e-mail: dev-unsubscribe@synapse.apache.org
> For additional commands, e-mail: dev-help@synapse.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@synapse.apache.org
For additional commands, e-mail: dev-help@synapse.apache.org




--
Saliya Ekanayake
http://www.esaliya.blogspot.com
http://www.esali= ya.wordpress.com




--
Saliya Ekan= ayake
http://www.esaliya.blo= gspot.com
http://www.es= aliya.wordpress.com
--000e0cd255341621a8046cda85ec--