axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aleksander Slominski <as...@cs.indiana.edu>
Subject Re: [Axis2][OM] Simpler way to get NameSpace
Date Mon, 13 Dec 2004 21:30:19 GMT
Glen Daniels wrote:

> Hi Alek!
>
> Comments below.
>
> Aleksander Slominski wrote:
>
>> Glen Daniels wrote:
>>
>>> Hi Eran, all:
>>>
>>> Eran Chinthaka wrote:
>>>
>>>>>> "Glen Daniels" <glen@thoughtcraft.com> writes:
>>>>>>
>>>>>>> 3) Map namespaces at a given OMElement:
>>>>>>>
>>>>>>>   void registerNamespace(String uri, String prefix)
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> declareNamespace(.,.)?
>>>>>
>>>>>
>>>>>
>>>
>>> Sure.
>>>
>>>>>>> 4) Ensure that we have a consistent view on what is a namespace
>>>>>>> declaration and what is an attribute.  For this XML:
>>>>>>>
>>>>>>>   <foo xmlns:ns1="http://ns1" attr1="bar"/>
>>>>>>>
>>>>>>> EITHER:
>>>>>>>
>>>>>>> getAttributes() returns JUST attr1, and getAllAttributes() 
>>>>>>> returns both
>>>>>>> the ns1 declaration and att1
>>>>>>>
>>>>>>> OR:
>>>>>>>
>>>>>>> getAttributes() returns both attributes.
>>>>>>>
>>>>>>> I prefer the former.  In either case I think we should 
>>>>>>> consistency check
>>>>>>> attributes by noticing if they are namespace declarations when

>>>>>>> they get
>>>>>>> added.  After this:
>>>>>>>
>>>>>>>   OMAttribute attr = new OMAttribute(new QName(XMLNS, "ns1"),
>>>>>>>                                      "http://ns1");
>>>>>>>   element.addAttribute(attr);
>>>>>>>
>>>>>>> The element should now have a new namespace mapping, and our

>>>>>>> code should
>>>>>>> complain if the "ns1" prefix is already mapped in this element.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> So this comes down to whether the AXIOM representation is for XML

>>>>>> 1.0
>>>>>> or XML 1.0 + Namespaces. My pref would be to say that its *only*
a
>>>>>> namespace aware Infoset model and *never* treat namespace 
>>>>>> declarations
>>>>>> as attributes.
>>>>>
>>>>>
>>>>>
>>>
>>> I don't think this works, unfortunately - there are some use cases I 
>>> can think of, like encryption/signing, which definitely need the 
>>> ability to get the entire attribute list, in order including 
>>> namespace declarations.  I'm sure there are other scenarios I can't 
>>> think of at the moment, too.
>>>
>>>>>> That means, getAttribues() only returns "attr1" and we don't have
>>>>>> something called getAllAttributes().
>>>>>
>>>>>
>>>>>
>>>
>>> -1, see above.
>>>
>>>> [Chinthaka] I also agree with you for not returning namespaces, 
>>>> when asked
>>>> for allAttributes()
>>>>
>>>> Current OMElement is like this.
>>>>
>>>> We have a getAllAttributes() to return a collection of *only* the
>>>> attributes.
>>>> We have getAllDeclaredNamespaces() in OMElement to retrieve all the
>>>> namespaces declared in this element (not in the scope).
>>>
>>>
>>>
>>>
>>> I'd like to see element.getNamespaces() (or getNamespaceDecls()) to 
>>> get all namespace declarations on this element, and 
>>> element.getInScopeNamespaces() to get all active namespace mappings.
>>>
>>>> In addition to that we have following methods as Sanjiva and Alek 
>>>> suggested
>>>> earlier.
>>>>
>>>> public OMNamespace declareNamespace(String uri, String prefix);
>>>>     public OMNamespace declareNamespace(OMNamespace namespace);
>>>>
>>>>     public OMNamespace findInScopeNamespace(String uri, String prefix)
>>>> throws OMException;
>>>>
>>>>     public OMNamespace findDeclaredNamespace(String uri, String 
>>>> prefix)
>>>> throws OMException;
>>>
>>>
>>>
>>>
>>> Why do we need these last two at all?  Does OMNamespace give me some 
>>> kind of pointer to the OMElement where it is attached?  If not, I 
>>> don't see a reason to have these methods.  If all I want to know is 
>>> whether a particular prefix/uri combination is in scope, I can just do:
>>>
>>> String mappedURI = element.getNamespaceFromPrefix(prefix);
>>
>>
>>
>>> if (myURI.equals(mappedURI)) {
>>>   // namespace is there
>>> }
>>
>>
>>
>> this code looks strange - when do you need to check prefix foo is 
>> bound to some namespace? instead i would resolve prefix to namespace 
>> when recreating QName.
>
>
> I was just noting that I didn't like the findNamespace(String uri, 
> String prefix) operations.  There is definitely a use for finding out 
> what a particular prefix maps to - QName resolution, for instance.  I 
> just think that either you're going to want to find the URI for a 
> prefix, or the prefix for a URI - that's it, no need for OMNamespace 
> objects or methods which take both URI and prefix.
>
>> good point but if you want to use namespace for declarations in other 
>> scope and you want to preserve prefix then OmNamespace is easier to use.
>>
>> URI uri;
>> OmNamespace ns = element.lookupNamespaceForPrefix(prefix);
>> if(ns == null) {
>>    throw new MyException("missing namespace declaration for prefix");
>> }
>>
>> // or one coul ddelcare namespace when building OM
>> OmNamespace ns = element.lookupNamespaceForUri(uri);
>> if(ns == null) {
>>  ns = element.declareNamespace(uri, prefix);
>> }
>
>
> I still don't see what OMNamespace (and it's "OM" not "Om", right? 
> Since OM is an acronym?) buys you.  

it will keep number of objects to represent namespace (it will be 
represented internally as pair <namespaceUri, prefix> anyway) to monimum 
and allows easily to delcare namespaces (as pairs) on child elements and 
attributes (and that includes prefixes!).

> Both of the above pieces of code could have just returned Strings, right?

they could

>
> If I want to know that a given URI is mapped, I do something like:
>
> String prefix = element.getPrefixForNamespace("http://namespace"

you could

> )
>
> If that returns anything other than null, I know it's mapped and I 
> know the prefix.  If it returns "", it's the default namespace.
>
> If I care about a prefix (for resolving a "foo:bar" string, say), I'll 
> do:
>
> String uri = element.getNamespaceForPrefix("foo")
>
> Why is using OMNamespace any better/cleaner?

because you encourage users to keep number of OmNamespace low, code 
simpler, and memory forptint small :)

code i like and use a lot is to delcare namespace as constant and reuse 
it for all messages, ex:

    public final static XmlNamespace POLICY_NS = builder.newNamespace(
        "wsp", "http://schemas.xmlsoap.org/ws/2002/12/policy");

alek

-- 
The best way to predict the future is to invent it - Alan Kay


Mime
View raw message