Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » inst2xsd » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   package org.apache.xmlbeans.impl.inst2xsd;
   16   
   17   import org.apache.xmlbeans.impl.inst2xsd.util.Element;
   18   import org.apache.xmlbeans.impl.inst2xsd.util.TypeSystemHolder;
   19   import org.apache.xmlbeans.impl.inst2xsd.util.Type;
   20   
   21   import javax.xml.namespace.QName;
   22   
   23   /**
   24    * @author Cezar Andrei ( cezar.andrei at bea.com )
   25    * Date: Jul 26, 2004
   26    */
   27   public class VenetianBlindStrategy
   28       extends RussianDollStrategy
   29       implements XsdGenStrategy
   30   {
   31       protected void checkIfReferenceToGlobalTypeIsNeeded(Element elem,
   32           TypeSystemHolder typeSystemHolder, Inst2XsdOptions options)
   33       {
   34           // VenetianBlindDesign defines global complex types
   35           Type elemType = elem.getType();
   36           QName elemName = elem.getName();
   37   
   38           if (elemType.isGlobal())
   39               // is already global, do nothing
   40               return;
   41   
   42           if (elemType.isComplexType())
   43           {
   44               for (int i = 0; ; i++)
   45               {
   46                   elemType.setName(new QName(elemName.getNamespaceURI(), elemName.getLocalPart() + "Type" + (i==0 ? "" : "" + i)));
   47   
   48                   Type candidate = typeSystemHolder.getGlobalType(elemType.getName());
   49                   if (candidate==null)
   50                   {
   51                       elemType.setGlobal(true);
   52                       typeSystemHolder.addGlobalType(elemType);
   53                       break;
   54                   }
   55                   else
   56                   {
   57                       if (compatibleTypes(candidate, elemType))
   58                       {
   59                           combineTypes(candidate, elemType, options);
   60                           elem.setType(candidate);
   61                           break;
   62                       }
   63                   }
   64               }
   65           }
   66       }
   67   
   68       private boolean compatibleTypes(Type elemType, Type candidate)
   69       {
   70           // when two types look like they are the same ?
   71   
   72           if (elemType==candidate)
   73               return true;
   74   
   75   //        if (typeIsReferencedInside(elemType, candidate) || typeIsReferencedInside(candidate, elemType))
   76   //            return false;
   77   //
   78   //        if (!elemType.isComplexType() && !candidate.isComplexType())
   79   //            return true;
   80   //
   81   //        if (elemType.isComplexType() && !candidate.isComplexType())
   82   //            return false;
   83   //        if (!elemType.isComplexType() && candidate.isComplexType())
   84   //            return false;
   85   //
   86   //        // both complex after this point
   87   //
   88   //        //todo: be smarter: look at att and elem names and types - compute a difference index
   89   
   90           return true;
   91       }
   92   
   93   //    private boolean typeIsReferencedInside(Type entity, Type container)
   94   //    {
   95   //        for (int i = 0; i < container.getElements().size(); i++)
   96   //        {
   97   //            Element element = (Element) container.getElements().get(i);
   98   //            if (entity==element.getType())
   99   //                return true;
  100   //
  101   //            if (typeIsReferencedInside(entity, element.getType()))
  102   //                return true;
  103   //        }
  104   //
  105   //        for (int i = 0; i < container.getAttributes().size(); i++)
  106   //        {
  107   //            Attribute attribute = (Attribute) container.getAttributes().get(i);
  108   //            if (entity==attribute.getType())
  109   //                return true;
  110   //
  111   //            if (typeIsReferencedInside(entity, attribute.getType()))
  112   //                return true;
  113   //        }
  114   //        return false;
  115   //    }
  116   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » inst2xsd » [javadoc | source]