You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuscany.apache.org by "Frank Budinsky (JIRA)" <tu...@ws.apache.org> on 2006/04/05 22:29:43 UTC

[jira] Resolved: (TUSCANY-119) XMLDocumentImpl should implement get/setSchemaLocation and get/setNoNamespaceSchemaLocation

     [ http://issues.apache.org/jira/browse/TUSCANY-119?page=all ]
     
Frank Budinsky resolved TUSCANY-119:
------------------------------------

    Resolution: Fixed

Committed revision 391789.

> XMLDocumentImpl should implement get/setSchemaLocation and get/setNoNamespaceSchemaLocation
> -------------------------------------------------------------------------------------------
>
>          Key: TUSCANY-119
>          URL: http://issues.apache.org/jira/browse/TUSCANY-119
>      Project: Tuscany
>         Type: New Feature

>   Components: Java SDO Implementation
>     Reporter: Paul Golick
>  Attachments: XMLDocumentImplPatch.txt
>
> A patch containing a suggested solution and its test case follows:
> Index: C:/Tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java
> ===================================================================
> --- C:/Tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java	(revision 0)
> +++ C:/Tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java	(revision 0)
> @@ -0,0 +1,88 @@
> +/**
> + *
> + *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
> + *
> + *  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.tuscany.sdo.test;
> +
> +import java.io.IOException;
> +
> +import junit.framework.TestCase;
> +
> +import commonj.sdo.helper.XMLDocument;
> +import commonj.sdo.helper.XMLHelper;
> +
> +public class XMLDocumentTestCase extends TestCase {
> +
> +  private final String SCHEMA_XML = "/XMLDocumentTestCase.xml";
> +  
> +  /**
> +   * This method will load an xml document consisting of a xsi:schemaLocation and 
> +   * xsi:noNamespaceSchemaLocation defined.  It will then use the XMLDocument API to get and
> +   * set the schemaLocation property.
> +   * 
> +   * @throws IOException
> +   */
> +  public void testSchemaLocation() throws IOException
> +  {
> +	  // load the xml document which has xsi:noNamespaceSchemaLocation and xsi:schemaLocation defined
> +      XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(SCHEMA_XML));
> +      
> +      // get the schemaLocation
> +      assertEquals("http://www.example.org/emp emp.xsd http://www.example.com/Report http://www.example.com/Report.xsd", doc.getSchemaLocation());
> +      
> +      // set the schemaLocation to another value and test to see if the value was set
> +      doc.setSchemaLocation("namespace schemaLocation");
> +      assertEquals("namespace schemaLocation", doc.getSchemaLocation());
> +      
> +      // remove the schemaLocation and ensure it returns null
> +      doc.setSchemaLocation(null);
> +      assertNull(doc.getSchemaLocation());
> +      
> +      // ensure changes to schemaLocation have not changed noNamespaceSchemaLocation
> +      assertEquals("http://www.example.com/Report.xsd emp.xsd", doc.getNoNamespaceSchemaLocation());
> +    }
> +
> +  /**
> +   * This method will load an xml document consisting of a xsi:schemaLocation and 
> +   * xsi:noNamespaceSchemaLocation defined.  It will then use the XMLDocument API to get and
> +   * set the noNamespaceSchemaLocation property.
> +   * 
> +   * @throws IOException
> +   */
> +  public void testNoNamespaceSchemaLocation() throws IOException
> +  {
> +	  // load the xml document which has xsi:noNamespaceSchemaLocation and xsi:schemaLocation defined
> +      XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(SCHEMA_XML));
> +      
> +      // get the noNamespaceSchemaLocation
> +      assertEquals("http://www.example.com/Report.xsd emp.xsd", doc.getNoNamespaceSchemaLocation());
> +            
> +      // set the noNameSpaceSchemaLocation to another value and test to see if the value was set
> +      doc.setNoNamespaceSchemaLocation("noNamespaceSchemaLocation");
> +      assertEquals("noNamespaceSchemaLocation", doc.getNoNamespaceSchemaLocation());
> +
> +      // remove the noNameSpaceSchemaLocation and ensure it returns null
> +      doc.setNoNamespaceSchemaLocation(null);
> +      assertNull(doc.getNoNamespaceSchemaLocation());
> +
> +      // ensure changes to noNameSpaceSchemaLocation have not changed schemaLocation
> +      assertEquals("http://www.example.org/emp emp.xsd http://www.example.com/Report http://www.example.com/Report.xsd", doc.getSchemaLocation());
> +    }
> +  
> +    protected void setUp() throws Exception {
> +        super.setUp();
> +    }
> +
> +}
> Index: C:/Tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml
> ===================================================================
> --- C:/Tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml	(revision 0)
> +++ C:/Tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml	(revision 0)
> @@ -0,0 +1,10 @@
> +<purchaseReport
> +  xmlns="http://www.example.com/Report"
> +  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> +  xsi:schemaLocation="http://www.example.com/Report
> +  http://www.example.com/Report.xsd
> +  http://www.example.org/emp 
> +  emp.xsd"
> +  xsi:noNamespaceSchemaLocation="http://www.example.com/Report.xsd emp.xsd"
> +  period="P3M" periodEnding="1999-12-31">
> +</purchaseReport>
> Index: C:/Tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
> ===================================================================
> --- C:/Tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java	(revision 385550)
> +++ C:/Tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java	(working copy)
> @@ -23,11 +23,13 @@
>  import java.io.OutputStream;
>  import java.io.Reader;
>  import java.io.Writer;
> +import java.util.Iterator;
>  import java.util.List;
>  import java.util.Map;
>  
>  import org.apache.tuscany.sdo.SDOPackage;
>  import org.apache.tuscany.sdo.util.DataObjectUtil;
> +import org.eclipse.emf.common.util.EMap;
>  import org.eclipse.emf.common.util.URI;
>  import org.eclipse.emf.ecore.EClass;
>  import org.eclipse.emf.ecore.EObject;
> @@ -91,6 +93,8 @@
>  
>    protected EObject documentRoot;
>    
> +  protected final static String WHITESPACE_REGEX = "\\s";
> +  
>    protected static XMLParserPool globalXMLParserPool = new XMLParserPoolImpl();
>    
>    //TODO clean up the options thing
> @@ -327,23 +331,192 @@
>      throw new UnsupportedOperationException(); //TODO
>    }
>  
> +  /**
> +   * @return an EMap containing the schema locations or null when no map
> +   */
> +  protected EMap getSchemaLocationMap()
> +  {
> +    EMap result = null;
> +    if ((documentRoot != null) && (extendedMetaData != null))
> +    {
> +      EReference xsiSchemaLocationMapFeature = extendedMetaData
> +          .getXSISchemaLocationMapFeature(documentRoot.eClass());
> +      if (xsiSchemaLocationMapFeature != null)
> +      {
> +        result = (EMap) documentRoot.eGet(xsiSchemaLocationMapFeature);
> +      }
> +    }
> +    return result;
> +  }
> +
> +  /**
> +   * @param value
> +   *          from schema location map.
> +   * @return string form of URI from provided value, deresolved if appropriate.
> +   */
> +  protected String deresolve(String value)
> +  {
> +    URI location = URI.createURI(value);
> +    URI resourceURI = resource.getURI();
> +    boolean shouldDeresolve = resourceURI != null && !resourceURI.isRelative()
> +        && resourceURI.isHierarchical();
> +    if (shouldDeresolve && !location.isRelative())
> +    {
> +      URI deresolvedURI = location.deresolve(resourceURI, true, true, false);
> +      if (deresolvedURI.hasRelativePath())
> +      {
> +        location = deresolvedURI;
> +      }
> +    }
> +    return location.toString();
> +  }
> +
> +  /**
> +   * @param value
> +   *          for schema location from input parameter.
> +   * @return string form of URI from provided value, resolved if appropriate.
> +   */
> +  protected String resolve(String value)
> +  {
> +    URI location = URI.createURI(value);
> +    URI resourceURI = resource.getURI();
> +    boolean shouldResolve = resourceURI != null && resourceURI.isHierarchical()
> +        && !resourceURI.isRelative();
> +    if (shouldResolve && location.isRelative() && location.hasRelativePath())
> +    {
> +      location = location.resolve(resourceURI, false);
> +    }
> +    return location.toString();
> +  }
> +
>    public String getSchemaLocation()
>    {
> -    throw new UnsupportedOperationException(); //TODO
> +    EMap xsiSchemaLocationMap = getSchemaLocationMap();
> +    if (xsiSchemaLocationMap != null)
> +    {
> +      if (!xsiSchemaLocationMap.isEmpty())
> +      {
> +        StringBuffer xsiSchemaLocation = new StringBuffer();
> +        for (Iterator i = xsiSchemaLocationMap.entrySet().iterator(); i
> +            .hasNext();)
> +        {
> +          Map.Entry entry = (Map.Entry) i.next();
> +          String namespace = (String) entry.getKey();
> +          if (namespace != null)
> +          {
> +            if (xsiSchemaLocation.length() > 0)
> +            {
> +              xsiSchemaLocation.append(' ');
> +            }
> +            xsiSchemaLocation.append(namespace);
> +            xsiSchemaLocation.append(' ');
> +            String value = entry.getValue().toString();
> +            xsiSchemaLocation.append(deresolve(value));
> +          }
> +        }
> +        return xsiSchemaLocation.toString().equals("") ? null
> +            : xsiSchemaLocation.toString();
> +      }
> +    }
> +    return null;
>    }
>  
>    public void setSchemaLocation(String schemaLocation)
>    {
> -    throw new UnsupportedOperationException(); //TODO
> +    EMap xsiSchemaLocationMap = getSchemaLocationMap();
> +    if (xsiSchemaLocationMap != null)
> +    {
> +      // only remove the entries from xsiSchemaLocationMap that contain a
> +      // non-null key
> +      for (Iterator i = xsiSchemaLocationMap.entrySet().iterator(); i.hasNext();)
> +      {
> +        Map.Entry entry = (Map.Entry) i.next();
> +        if (entry.getKey() != null)
> +        {
> +          i.remove();
> +        }
> +      }
> +      if (xsiSchemaLocationMap.size() == 0)
> +      {
> +        resource.getDefaultSaveOptions().put(
> +            XMLResource.OPTION_SCHEMA_LOCATION, Boolean.FALSE);
> +      }
> +      if (schemaLocation != null)
> +      {
> +        String[] values = schemaLocation.split(WHITESPACE_REGEX);
> +        for (int i = 0; i < values.length; i++) // note: also incremented in
> +        // loop
> +        {
> +          String key = values[i++];
> +          if (i < values.length)
> +          {
> +            xsiSchemaLocationMap.put(key, resolve(values[i]));
> +          }
> +        }
> +        if (xsiSchemaLocationMap.size() != 0)
> +        {
> +          resource.getDefaultSaveOptions().put(
> +              XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
> +        }
> +      }
> +    }
>    }
>  
>    public String getNoNamespaceSchemaLocation()
>    {
> -    throw new UnsupportedOperationException(); //TODO
> +    EMap xsiSchemaLocationMap = getSchemaLocationMap();
> +    if (xsiSchemaLocationMap != null)
> +    {
> +      StringBuffer xsiSchemaLocation = new StringBuffer();
> +      if (!xsiSchemaLocationMap.isEmpty())
> +      {
> +        Object valueObject = xsiSchemaLocationMap.get(null);
> +        if (valueObject != null)
> +        {
> +          String valueString = (String) valueObject;
> +          String[] values = valueString.split(WHITESPACE_REGEX);
> +          for (int i = 0; i < values.length; i++)
> +          {
> +            if (xsiSchemaLocation.length() > 0)
> +            {
> +              xsiSchemaLocation.append(' ');
> +            }
> +            xsiSchemaLocation.append(deresolve(values[i]));
> +          }
> +        }
> +        String result = xsiSchemaLocation.toString();
> +        return result.equals("") ? null : result;
> +      }
> +    }
> +    return null;
>    }
>  
>    public void setNoNamespaceSchemaLocation(String schemaLocation)
>    {
> -    throw new UnsupportedOperationException(); //TODO
> +    EMap xsiSchemaLocationMap = getSchemaLocationMap();
> +    if (xsiSchemaLocationMap != null)
> +    {
> +      // only remove the entries from xsiSchemaLocationMap that contain a null
> +      // key
> +      xsiSchemaLocationMap.removeKey(null);
> +      if (xsiSchemaLocationMap.size() == 0)
> +      {
> +        resource.getDefaultSaveOptions().put(
> +            XMLResource.OPTION_SCHEMA_LOCATION, Boolean.FALSE);
> +      }
> +      if (schemaLocation != null)
> +      {
> +        String[] values = schemaLocation.split(WHITESPACE_REGEX);
> +        for (int i = 0; i < values.length; i++)
> +        {
> +          xsiSchemaLocationMap.put(null, resolve(values[i]));
> +        }
> +        if (xsiSchemaLocationMap.size() != 0)
> +        {
> +          resource.getDefaultSaveOptions().put(
> +              XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
> +        }
> +      }
> +    }
>    }
>  }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira