You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Dirk Gronert <u1...@hs-harz.de> on 2004/06/18 10:54:07 UTC

Re: using config file in flow

Upayavira wrote:
> Charles F. Munat wrote:
> 
>> Simple question, but can't find the answer anywhere.
>>
>> I want to load an XML config file into memory, and then access its 
>> element and attribute values from flowscript using XPath. It will be a 
>> different file for each page request (these parameters will be used to 
>> help create the response).
>>
>> Essentially, I'd like to create something like this in the flowscript:
>>
>> function setParams(uri) {
>>     cocoon.context.setAttributes("params", -config file data here-);
>> }
>>
>> Here's the config file (example):
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <params>
>>     <color>blue</color>
>>     <size>XL</size>
>>     <quantity>5</quantity>
>> </params>
> 
> 
> No time to give you a thorough answer, but what you can do is:
> 
> (a) read your config file into a DOM document
>       Can't tell you this off the top of my head

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);
         cocoon.releaseComponent(parser);
         cocoon.releaseComponent(resolver);
     }
}

> (b) Create a JXPathContext, using your DOM document:
>            var jx  = 
> Packages.org.apache.commons.jxpath.JXPathContext.newContext(myDomObject);
>            jx.setLenient(true);
> (c) Use the JXPathContext to access your objects.
>            var color = jx.getValue("/params/color");
> 
> It isn't that hard, but if you do a lot of it, you might like to wrap 
> this in a small Java class so that you can:
> 
> var jx = MyJXPathObject.getConfigFile("myconfigfile.xml");
> var color = jx.getValue("/params/color");
> 
> 
> Regards, Upayavira


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


Re: using config file in flow

Posted by "Charles F. Munat" <ch...@munat.com>.
Thanks both of you. I'll try this over the weekend and will post 
whatever I get to work next week.

Chas.

Dirk Gronert wrote:

> Upayavira wrote:
> 
>> Charles F. Munat wrote:
>>
>>> Simple question, but can't find the answer anywhere.
>>>
>>> I want to load an XML config file into memory, and then access its 
>>> element and attribute values from flowscript using XPath. It will be 
>>> a different file for each page request (these parameters will be used 
>>> to help create the response).
>>>
>>> Essentially, I'd like to create something like this in the flowscript:
>>>
>>> function setParams(uri) {
>>>     cocoon.context.setAttributes("params", -config file data here-);
>>> }
>>>
>>> Here's the config file (example):
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <params>
>>>     <color>blue</color>
>>>     <size>XL</size>
>>>     <quantity>5</quantity>
>>> </params>
>>
>>
>>
>> No time to give you a thorough answer, but what you can do is:
>>
>> (a) read your config file into a DOM document
>>       Can't tell you this off the top of my head
> 
> 
> 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);
>         cocoon.releaseComponent(parser);
>         cocoon.releaseComponent(resolver);
>     }
> }
> 
>> (b) Create a JXPathContext, using your DOM document:
>>            var jx  = 
>> Packages.org.apache.commons.jxpath.JXPathContext.newContext(myDomObject);
>>            jx.setLenient(true);
>> (c) Use the JXPathContext to access your objects.
>>            var color = jx.getValue("/params/color");
>>
>> It isn't that hard, but if you do a lot of it, you might like to wrap 
>> this in a small Java class so that you can:
>>
>> var jx = MyJXPathObject.getConfigFile("myconfigfile.xml");
>> var color = jx.getValue("/params/color");
>>
>>
>> Regards, Upayavira
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 

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