Hi Steve,
Is there any reason why we can't do this with static methods:
QName ElementHelper.resolveQName (OMElement, String);
That avoids creating the extra object .. which is thrown away
immediately.
Sanjiva.
On Fri, 2005-11-04 at 14:06 +0000, [EMAIL PROTECTED] wrote:
> Author: stevel
> Date: Fri Nov 4 06:06:35 2005
> New Revision: 330808
>
> URL: http://svn.apache.org/viewcvs?rev=330808&view=rev
> Log:
> pull out qname support into an ElementHelper class; anything that works with
> OMElement interfaces can go there. Test for everything too.
>
> Added:
>
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/util/ElementHelper.java
>
> webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementQNameTest.java
> Modified:
>
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/OMElement.java
>
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java
>
> Modified:
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/OMElement.java
> URL:
> http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/OMElement.java?rev=330808&r1=330807&r2=330808&view=diff
> ==============================================================================
> ---
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/OMElement.java
> (original)
> +++
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/OMElement.java
> Fri Nov 4 06:06:35 2005
> @@ -287,5 +287,5 @@
> * @param qname prefixed qname string to resolve
> * @return null for any failure to extract a qname.
> */
> -// QName resolveQName(String qname);
> + QName resolveQName(String qname);
> }
>
> Modified:
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java
> URL:
> http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java?rev=330808&r1=330807&r2=330808&view=diff
> ==============================================================================
> ---
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java
> (original)
> +++
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java
> Fri Nov 4 06:06:35 2005
> @@ -17,6 +17,7 @@
> package org.apache.axis2.om.impl.llom;
>
> import org.apache.axis2.om.*;
> +import org.apache.axis2.om.util.ElementHelper;
> import org.apache.axis2.om.impl.OMContainerEx;
> import org.apache.axis2.om.impl.OMNodeEx;
> import org.apache.axis2.om.impl.OMOutputImpl;
> @@ -810,44 +811,12 @@
>
> /**
> * Turn a prefix:local qname string into a proper QName, evaluating it
> in the OMElement context
> - *
> - * @param qname qname to resolve
> - * @param defaultToParentNameSpace flag that controls behaviour when
> there is no namespace.
> - * @return null for any failure to extract a qname.
> - */
> - public QName resolveQName(String qname, boolean
> defaultToParentNameSpace) {
> - int colon = qname.indexOf(':');
> - if (colon < 0) {
> - if (defaultToParentNameSpace) {
> - //get the parent ns and use it for the child
> - OMNamespace namespace = this.getNamespace();
> - return new QName(namespace.getName(), qname,
> namespace.getPrefix());
> - } else {
> - //else things without no prefix are local.
> - return new QName(qname);
> - }
> - }
> - String prefix = qname.substring(0, colon);
> - String local = qname.substring(colon + 1);
> - if (local.length() == 0) {
> - //empy local, exit accordingly
> - return null;
> - }
> -
> - OMNamespace namespace = findNamespace(null, prefix);
> - if (namespace == null) {
> - return null;
> - }
> - return new QName(namespace.getName(), local, prefix);
> - }
> -
> - /**
> - * Turn a prefix:local qname string into a proper QName, evaluating it
> in the OMElement context
> * unprefixed qnames resolve to the local namespace
> * @param qname prefixed qname string to resolve
> * @return null for any failure to extract a qname.
> */
> public QName resolveQName(String qname) {
> - return resolveQName(qname,true);
> + ElementHelper helper=new ElementHelper(this);
> + return helper.resolveQName(qname);
> }
> }
>
> Added:
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/util/ElementHelper.java
> URL:
> http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/util/ElementHelper.java?rev=330808&view=auto
> ==============================================================================
> ---
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/util/ElementHelper.java
> (added)
> +++
> webservices/axis2/trunk/java/modules/xml/src/org/apache/axis2/om/util/ElementHelper.java
> Fri Nov 4 06:06:35 2005
> @@ -0,0 +1,84 @@
> +/*
> + * Copyright 2005 The Apache Software Foundation.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +package org.apache.axis2.om.util;
> +
> +import org.apache.axis2.om.OMElement;
> +import org.apache.axis2.om.OMNamespace;
> +
> +import javax.xml.namespace.QName;
> +
> +/**
> + * helper class to provide extra utility stuff against elements.
> + * The code is designed to work with any element implementation.
> + */
> +
> +public class ElementHelper {
> +
> + private OMElement element;
> +
> + /**
> + * construct and bind to an element
> + * @param element element to work with
> + */
> + public ElementHelper(OMElement element) {
> + this.element = element;
> + }
> +
> + /**
> + * Turn a prefix:local qname string into a proper QName, evaluating it
> in the OMElement context
> + *
> + * @param qname qname to resolve
> + * @param defaultToParentNameSpace flag that controls behaviour when
> there is no namespace.
> + * @return null for any failure to extract a qname.
> + */
> + public QName resolveQName(String qname, boolean
> defaultToParentNameSpace) {
> + int colon = qname.indexOf(':');
> + if (colon < 0) {
> + if (defaultToParentNameSpace) {
> + //get the parent ns and use it for the child
> + OMNamespace namespace = element.getNamespace();
> + return new QName(namespace.getName(), qname,
> namespace.getPrefix());
> + } else {
> + //else things without no prefix are local.
> + return new QName(qname);
> + }
> + }
> + String prefix = qname.substring(0, colon);
> + String local = qname.substring(colon + 1);
> + if (local.length() == 0) {
> + //empy local, exit accordingly
> + return null;
> + }
> +
> + OMNamespace namespace = element.findNamespace(null, prefix);
> + if (namespace == null) {
> + return null;
> + }
> + return new QName(namespace.getName(), local, prefix);
> + }
> +
> + /**
> + * Turn a prefix:local qname string into a proper QName, evaluating it
> in the OMElement context
> + * unprefixed qnames resolve to the local namespace
> + *
> + * @param qname prefixed qname string to resolve
> + * @return null for any failure to extract a qname.
> + */
> + public QName resolveQName(String qname) {
> + return resolveQName(qname, true);
> + }
> +
> +}
>
> Added:
> webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementQNameTest.java
> URL:
> http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementQNameTest.java?rev=330808&view=auto
> ==============================================================================
> ---
> webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementQNameTest.java
> (added)
> +++
> webservices/axis2/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementQNameTest.java
> Fri Nov 4 06:06:35 2005
> @@ -0,0 +1,91 @@
> +/** (C) Copyright 2005 Hewlett-Packard Development Company, LP
> +
> + This library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + This library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with this library; if not, write to the Free Software
> + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> +
> + For more information: www.smartfrog.org
> +
> + */
> +package org.apache.axis2.om;
> +
> +import org.apache.axis2.soap.SOAPEnvelope;
> +import org.apache.axis2.om.impl.llom.OMElementImpl;
> +import org.apache.axis2.om.util.ElementHelper;
> +
> +import javax.xml.namespace.QName;
> +
> +/**
> + * created 03-Nov-2005 11:46:32
> + */
> +
> +public class OMElementQNameTest extends OMTestCase {
> +
> + OMElement element;
> +
> + private static final String WSA=
> "http://schemas.xmlsoap.org/ws/2004/03/addressing";
> + private static final String SOAPENV =
> "http://schemas.xmlsoap.org/soap/envelope/";
> + private static final String XSD = "http://www.w3.org/2001/XMLSchema";
> +
> + public OMElementQNameTest(String testName) {
> + super(testName);
> + }
> +
> + protected void setUp() throws Exception {
> + super.setUp();
> + element = OMTestUtils.getOMBuilder(
> + getTestResourceFile(TestConstants.SOAP_SOAPMESSAGE1))
> + .getDocumentElement();
> + }
> +
> + public void testSimpleQName() throws Exception {
> + QName result = element.resolveQName("wsa:To");
> + assertEquals(WSA,result.getNamespaceURI());
> + assertEquals("wsa", result.getPrefix());
> + assertEquals("To", result.getLocalPart());
> + }
> +
> + public void testDefaultQName() throws Exception {
> + QName result = element.resolveQName("localonly");
> + assertEquals(SOAPENV, result.getNamespaceURI());
> + assertEquals("soapenv", result.getPrefix());
> + assertEquals("localonly", result.getLocalPart());
> + }
> +
> + public void testDefaultQNameCanBeLocal() throws Exception {
> + ElementHelper helper=new ElementHelper(element);
> + QName result = helper.resolveQName("localonly",false);
> + assertEquals("", result.getNamespaceURI());
> + assertEquals("localonly", result.getLocalPart());
> + }
> +
> + public void testNoLocal() throws Exception {
> + assertResolvesToNull("wsa:");
> + }
> +
> + public void testNoMatch() throws Exception {
> + assertResolvesToNull("wsa2005:To");
> + }
> +
> + public void testNothing() throws Exception {
> + assertResolvesToNull(":");
> + }
> +
> +
> +
> + private void assertResolvesToNull(String qname) {
> + QName result = element.resolveQName(qname);
> + assertNull("Expected "+qname+" to resolve to null",result);
> + }
> +
> +}
>
>