You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Josh2007 <jo...@gmail.com> on 2007/10/06 21:44:04 UTC

How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Hi all,

I would like to load a pipeline in my flowscript.
There's a function for that in java: document =
loadDocument(cocoon:/your.pipeline);
I can't figure out which way will provide me the same result in javascript.

Actually, I need to get the result of a stream generation and xsl
transformation in my flowscript for further processing.
For now, I didn't find any solution in the previous posts.

Is there a way to do that?

Thanks,

Josh


-- 
View this message in context: http://www.nabble.com/How-to-get-same-function-as-java-loadDocument%28cocoon%3A-your.pipeline%29-in-javascript-flow--tf4581000.html#a13076932
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by Josh2007 <jo...@gmail.com>.
Thanks a lot, you make my day easier.
I've been searching for 2 days now, digging in all the posts without any
result.
I will give it a try and will let you know if I need any input about
transformation in javascript.

Josh


solprovider-2 wrote:
> 
> Hi Josh,
> 
> The function is posted:
> http://solprovider.com/lenya/flowxml
> 
> The function works for any source in Cocoon so you can use the
> "cocoon:", "file:", and "http:" protocols and any others you have
> configured.  Let me know if you need how to do a transform in
> JavaScript.
> 
> solprovider
> 
> On 10/6/07, Josh2007 <jo...@gmail.com> wrote:
>> I would like to load a pipeline in my flowscript.
>> There's a function for that in java: document =
>> loadDocument(cocoon:/your.pipeline);
>> I can't figure out which way will provide me the same result in
>> javascript.
>>
>> Actually, I need to get the result of a stream generation and xsl
>> transformation in my flowscript for further processing.
>> For now, I didn't find any solution in the previous posts.
>> Thanks,
>> Josh
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-same-function-as-java-loadDocument%28cocoon%3A-your.pipeline%29-in-javascript-flow--tf4581000.html#a13080438
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by Josh2007 <jo...@gmail.com>.
Thanks,
Everything works now. 
Actually I'm already using Captcha block with some minor changes.

josh


solprovider-2 wrote:
> 
> The code you supplied will not work.
> 
> With XML like:
> <rootElement>
> <captcha/>
> <captcha/>
> ...
> <captcha>returnValue</captcha>
> </rootElement>
> 
> You would expect returnValue to be the text contents of the last
> captcha element under root.  returnValue will be placed in a session
> attribute and used in pipeline variables.
> <map:transform src="variables.xsl">
> <map:parameter name="nodeFromSession"
> value="{session-attr:captcha-result}"/>
> <map:parameter name="nodeFromFlow"
> value="{flow-attribute:captcha-result}"/>
> 
> The nodeValue of an element is always blank.  Much of the W3C's role
> is protecting the jobs of IT professionals.  W3C XML has a layer of
> extra nodes that interfere with comprehension by normal people.  For
> <myNode>myText</myNode>, the XML is like:
> Node("myNode").getChild("#text").getNodeValue()
> Assuming the node has no children elements, use this function:
> 
> function getText(node){
>    var text = "";
>    if(node.getFirstChild() != null) text =
> node.getFirstChild().getNodeValue();
>    return text;
> }
> 
> I use print statements to test my JS.  The results appear on the console.
> var captchaUserEntry = captchaUserEntries.item(i);
> var captchaUserContent = getText(captchaUserEntry);
> print("" + i + "=" + captchaUserContent);
> 
> I am uncertain about scope in JavaScript.  I would never declare a
> variable inside a block and expect the variable to be available
> outside the block, but that rule is a LCD from using many programming
> languages.  If JS had an issue, this code should error about accessing
> a missing property "nodeValue" from a null object.
> 
> For other reading, Cocoon has a standard unstable "captcha" block that
> has been discussed on the Cocoon and Lenya MLs.
> 
> solprovider
> 
> 
> On 10/7/07, Josh2007 <jo...@gmail.com> wrote:
>> Everything works fine with the function.
>> I've been able to retrieve my xml and to fetch the required node.
>> Problem, I can't fetch any nodeValue.
>>
>> To check, I save the node.nodeValue content in a session attribute and
>> try
>> to get this attribute content back to the browser. Each time I get an
>> empty
>> result.
>> If I ask for the nodeName or node attribute, I get the required result.
>> It
>> only fails with nodeValue or data.
>>
>> Here's my flowscript, based on the link you sent to me. Hopefully you
>> know
>> what I'm doing wrong :
>> function getCaptchaUserEntry() {
>>   // retrieve the XML
>>   var xml = loadDocument("cocoon:/get-stream");
>>   // get the root element:
>>   var root = xml.getDocumentElement();
>>   // get captcha elements (actually only 1 captcha element is allowed in
>> the
>> xforms)
>>   var captchaUserEntries = root.getElementsByTagName("captcha");
>>   // get captcha node
>>   for (var i = 0; i < captchaUserEntries.getLength(); i++) {
>>       var captchaUserEntry = captchaUserEntries.item(i);
>>   }
>>   // get required content
>>   var captchaUserContent = captchaUserEntry.nodeValue;
>>   // save in session - to debugg
>>   var session = cocoon.session;
>>   session.setAttribute("captcha-result", captchaUserContent);
>>   // ---------------------------
>>   var parameters = null;
>>   parameters = {
>>       "captcha-result": captchaUserContent,
>>     };
>>   cocoon.sendPage("check-session", parameters);
>> }
>> Thanks,
>> Josh
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-same-function-as-java-loadDocument%28cocoon%3A-your.pipeline%29-in-javascript-flow--tf4581000.html#a13085727
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by Josh2007 <jo...@gmail.com>.
Thanks for the tip to avoid looping.
Everything works.

Josh


Joerg Heinicke wrote:
> 
> On 07.10.2007 11:15 Uhr, solprovider@apache.org wrote:
> 
>> I am uncertain about scope in JavaScript.  I would never declare a
>> variable inside a block and expect the variable to be available
>> outside the block, but that rule is a LCD from using many programming
>> languages.  If JS had an issue, this code should error about accessing
>> a missing property "nodeValue" from a null object.
> 
> This is the actual reason. Even if it works at all (as you explain why 
> it should not) the variable is declared only inside the loop and not 
> available outside of it. Cocoon once had a feature to put non-declared 
> variables into a global scope, but I thought this was somehow protected 
> during method execution. Anyway, this means that you have a local 
> variable inside the loop - and a different global one, conincidentally 
> with the same name, which never gets set. But in that case you really 
> should get an exception since you access the nodeValue property of null.
> 
> The code has to change at least (besides possible DOM changes) to:
> 
>    var captchaUserEntries = root.getElementsByTagName("captcha");
> 
>    var captchaUserEntry;
>    // get captcha node
>    for (var i = 0; i < captchaUserEntries.getLength(); i++) {
>        captchaUserEntry = captchaUserEntries.item(i);
>    }
> 
>    // get required content
>    var captchaUserContent = captchaUserEntry.nodeValue;
> 
> Or to avoid the meaningless looping do:
> 
>    var captchaUserEntries = root.getElementsByTagName("captcha");
> 
>    var captchaUserEntry =
>      captchaUserEntries.item(captchaUserEntries.getLength() - 1);
> 
>    // get required content
>    var captchaUserContent = captchaUserEntry.nodeValue;
> 
> (Both examples should have a check for the case there is no captcha 
> element.)
> 
> Hope this helps,
> 
> Joerg
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-same-function-as-java-loadDocument%28cocoon%3A-your.pipeline%29-in-javascript-flow--tf4581000.html#a13085776
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by Joerg Heinicke <jo...@gmx.de>.
On 07.10.2007 11:15 Uhr, solprovider@apache.org wrote:

> I am uncertain about scope in JavaScript.  I would never declare a
> variable inside a block and expect the variable to be available
> outside the block, but that rule is a LCD from using many programming
> languages.  If JS had an issue, this code should error about accessing
> a missing property "nodeValue" from a null object.

This is the actual reason. Even if it works at all (as you explain why 
it should not) the variable is declared only inside the loop and not 
available outside of it. Cocoon once had a feature to put non-declared 
variables into a global scope, but I thought this was somehow protected 
during method execution. Anyway, this means that you have a local 
variable inside the loop - and a different global one, conincidentally 
with the same name, which never gets set. But in that case you really 
should get an exception since you access the nodeValue property of null.

The code has to change at least (besides possible DOM changes) to:

   var captchaUserEntries = root.getElementsByTagName("captcha");

   var captchaUserEntry;
   // get captcha node
   for (var i = 0; i < captchaUserEntries.getLength(); i++) {
       captchaUserEntry = captchaUserEntries.item(i);
   }

   // get required content
   var captchaUserContent = captchaUserEntry.nodeValue;

Or to avoid the meaningless looping do:

   var captchaUserEntries = root.getElementsByTagName("captcha");

   var captchaUserEntry =
     captchaUserEntries.item(captchaUserEntries.getLength() - 1);

   // get required content
   var captchaUserContent = captchaUserEntry.nodeValue;

(Both examples should have a check for the case there is no captcha 
element.)

Hope this helps,

Joerg

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by so...@apache.org.
The code you supplied will not work.

With XML like:
<rootElement>
<captcha/>
<captcha/>
...
<captcha>returnValue</captcha>
</rootElement>

You would expect returnValue to be the text contents of the last
captcha element under root.  returnValue will be placed in a session
attribute and used in pipeline variables.
<map:transform src="variables.xsl">
<map:parameter name="nodeFromSession" value="{session-attr:captcha-result}"/>
<map:parameter name="nodeFromFlow" value="{flow-attribute:captcha-result}"/>

The nodeValue of an element is always blank.  Much of the W3C's role
is protecting the jobs of IT professionals.  W3C XML has a layer of
extra nodes that interfere with comprehension by normal people.  For
<myNode>myText</myNode>, the XML is like:
Node("myNode").getChild("#text").getNodeValue()
Assuming the node has no children elements, use this function:

function getText(node){
   var text = "";
   if(node.getFirstChild() != null) text = node.getFirstChild().getNodeValue();
   return text;
}

I use print statements to test my JS.  The results appear on the console.
var captchaUserEntry = captchaUserEntries.item(i);
var captchaUserContent = getText(captchaUserEntry);
print("" + i + "=" + captchaUserContent);

I am uncertain about scope in JavaScript.  I would never declare a
variable inside a block and expect the variable to be available
outside the block, but that rule is a LCD from using many programming
languages.  If JS had an issue, this code should error about accessing
a missing property "nodeValue" from a null object.

For other reading, Cocoon has a standard unstable "captcha" block that
has been discussed on the Cocoon and Lenya MLs.

solprovider


On 10/7/07, Josh2007 <jo...@gmail.com> wrote:
> Everything works fine with the function.
> I've been able to retrieve my xml and to fetch the required node.
> Problem, I can't fetch any nodeValue.
>
> To check, I save the node.nodeValue content in a session attribute and try
> to get this attribute content back to the browser. Each time I get an empty
> result.
> If I ask for the nodeName or node attribute, I get the required result. It
> only fails with nodeValue or data.
>
> Here's my flowscript, based on the link you sent to me. Hopefully you know
> what I'm doing wrong :
> function getCaptchaUserEntry() {
>   // retrieve the XML
>   var xml = loadDocument("cocoon:/get-stream");
>   // get the root element:
>   var root = xml.getDocumentElement();
>   // get captcha elements (actually only 1 captcha element is allowed in the
> xforms)
>   var captchaUserEntries = root.getElementsByTagName("captcha");
>   // get captcha node
>   for (var i = 0; i < captchaUserEntries.getLength(); i++) {
>       var captchaUserEntry = captchaUserEntries.item(i);
>   }
>   // get required content
>   var captchaUserContent = captchaUserEntry.nodeValue;
>   // save in session - to debugg
>   var session = cocoon.session;
>   session.setAttribute("captcha-result", captchaUserContent);
>   // ---------------------------
>   var parameters = null;
>   parameters = {
>       "captcha-result": captchaUserContent,
>     };
>   cocoon.sendPage("check-session", parameters);
> }
> Thanks,
> Josh

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by Josh2007 <jo...@gmail.com>.
Everything works fine with the function.

I've been able to retrieve my xml and to fetch the required node.
Problem, I can't fetch any nodeValue.

To check, I save the node.nodeValue content in a session attribute and try
to get this attribute content back to the browser. Each time I get an empty
result.
If I ask for the nodeName or node attribute, I get the required result. It
only fails with nodeValue or data.

Here's my flowscript, based on the link you sent to me. Hopefully you know
what I'm doing wrong :


// loadDocument() reads in an XML file and returns a DOM Document. 
function loadDocument(uri) {
var parser = null;
var source = null;
var resolver = null;
try {
parser =
cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
resolver =
cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
source = resolver.resolveURI(uri);
var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
is.setSystemId(source.getURI());
return parser.parseDocument(is);
} finally {
if (source != null) resolver.release(source);
if (parser != null) cocoon.releaseComponent(parser);
if (resolver != null) cocoon.releaseComponent(resolver);
}
}

function getCaptchaUserEntry() {
  // retrieve the XML
  var xml = loadDocument("cocoon:/get-stream");
  
  // get the root element:
  var root = xml.getDocumentElement();
  
  // get captcha elements (actually only 1 captcha element is allowed in the
xforms)
  var captchaUserEntries = root.getElementsByTagName("captcha");
  
  // get captcha node
  for (var i = 0; i < captchaUserEntries.getLength(); i++) {
      var captchaUserEntry = captchaUserEntries.item(i);
  } 
  
  // get required content
  var captchaUserContent = captchaUserEntry.nodeValue;
  
  // save in session - to debugg
  var session = cocoon.session;
  session.setAttribute("captcha-result", captchaUserContent);
  
  // ---------------------------
  var parameters = null;
  parameters = {
      "captcha-result": captchaUserContent,
    };
  cocoon.sendPage("check-session", parameters);
}

Thanks,

Josh

-- 
View this message in context: http://www.nabble.com/How-to-get-same-function-as-java-loadDocument%28cocoon%3A-your.pipeline%29-in-javascript-flow--tf4581000.html#a13081947
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to get same function as java loadDocument(cocoon:/your.pipeline) in javascript flow?

Posted by so...@apache.org.
Hi Josh,

The function is posted:
http://solprovider.com/lenya/flowxml

The function works for any source in Cocoon so you can use the
"cocoon:", "file:", and "http:" protocols and any others you have
configured.  Let me know if you need how to do a transform in
JavaScript.

solprovider

On 10/6/07, Josh2007 <jo...@gmail.com> wrote:
> I would like to load a pipeline in my flowscript.
> There's a function for that in java: document =
> loadDocument(cocoon:/your.pipeline);
> I can't figure out which way will provide me the same result in javascript.
>
> Actually, I need to get the result of a stream generation and xsl
> transformation in my flowscript for further processing.
> For now, I didn't find any solution in the previous posts.
> Thanks,
> Josh

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org