summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-final/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/impl/FactoryBase.java
blob: e57fb54544cd6e125b7b98046ee060970e7fa5e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 *
 *  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.impl;

import org.apache.tuscany.sdo.util.DataObjectUtil;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.impl.EClassImpl;
import org.eclipse.emf.ecore.impl.EFactoryImpl;
import org.eclipse.emf.ecore.impl.EPackageImpl;

import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Type;

/**
 */
public class FactoryBase extends EPackageImpl
{
  protected FactoryBase(String namespaceURI, String namespacePrefix)
  {
    super(new SDOEFactoryImpl());
    
    int index = namespacePrefix.lastIndexOf(".");
    setName(index != -1 ? namespacePrefix.substring(index + 1) : namespacePrefix);
    setNsPrefix(namespacePrefix);

    createResource(namespaceURI);
    setNsURI(namespaceURI);
    //FIXME ... figure out proper (scoped) way to register static packages
    EPackage.Registry.INSTANCE.put(namespaceURI, this);
    
    ((SDOEFactoryImpl)getEFactoryInstance()).sdoFactory = this;
  }
  
  public DataObject create(int typeNumber)
  {
    return null;
  }
  
  protected Type createType(boolean isDataType, int typeNumber)
  {
    if (isDataType)
      return (Type)createEDataType(typeNumber);
    else
      return (Type)createEClass(typeNumber);
  }
  
  protected void createProperty(boolean isDataType, Type containingType, int propertyNumber)
  {
    if (isDataType)
      createEAttribute((EClass)containingType, propertyNumber);
    else
      createEReference((EClass)containingType, propertyNumber);
  }
  
  protected void initializeType(Type type, Class instanceClass, String name)
  {
    initEClass((EClass)type, instanceClass, name, !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);   
  }

  protected void initializeProperty(Property property, Type type, String name, String defaultValue, int lower, int upper, Class containerClass, boolean isReadonly, boolean isUnsettable, boolean isDerived)
  {
    initEAttribute((EAttribute)property, (EClassifier)type, name, defaultValue, lower, upper, containerClass, isDerived, isDerived, !isReadonly, isUnsettable, !IS_ID, !IS_UNIQUE, isDerived, IS_ORDERED);
  }
  
  protected void initializeProperty(Property property, Type type, String name, String defaultValue, int lower, int upper, Class containerClass, boolean isReadonly, boolean isUnsettable, boolean isDerived, boolean isComposite, Property oppositeProperty)
  {
    initEReference((EReference)property, (EClassifier)type, (EReference)oppositeProperty, name, defaultValue, lower, upper, containerClass, isDerived, isDerived, !isReadonly, isComposite, !isComposite /*resolve*/, isUnsettable, IS_UNIQUE, isDerived, IS_ORDERED);
  }


  
  protected void createXSDMetaData()
  {
    createDocumentRoot();
  }
  
  protected void addXSDMapping(Type type, String[] xsdMappings)
  {
    addAnnotation((ENamedElement)type, ANNOTATION_SOURCE, xsdMappings);
  }
  
  protected void addXSDMapping(Property property, String[] xsdMappings)
  {
    addAnnotation((ENamedElement)property, ANNOTATION_SOURCE, xsdMappings);
  }
  
  protected void createGlobalProperty(String name, Type type, String[] xsdMappings)
  {
    int propertyNumber = documentRootEClass.getEStructuralFeatures().size();
    createEReference(documentRootEClass, propertyNumber);
    EReference globalProperty = (EReference)documentRootEClass.getEStructuralFeatures().get(propertyNumber);
    initEReference(globalProperty, (EClass)type, null, name, null, 0, -2, null, IS_TRANSIENT, IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, IS_DERIVED, IS_ORDERED);
    addAnnotation((ENamedElement)globalProperty, ANNOTATION_SOURCE, xsdMappings);
  }
  
  protected Type getSequence() {
    return (Type)ecorePackage.getEFeatureMapEntry();
  }

  //public static FactoryBase getStaticFactory(String namespaceURI)
  // temporarily return Object - until everything is gen'd with new codegen pattern
  public static Object getStaticFactory(String namespaceURI)
  {
    EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(namespaceURI);
    //return (FactoryBase)ePackage;
    return ePackage instanceof FactoryBase ? (Object)ePackage : (Object)ePackage.getEFactoryInstance(); 
  }
  
  // private EMF-specific methods

  private static class SDOEFactoryImpl extends EFactoryImpl
  {
    protected FactoryBase sdoFactory;
    
    public SDOEFactoryImpl()
    {
      super();
    }
    
    public EObject create(EClass eClass)
    {
      DataObject result = sdoFactory.create(eClass.getClassifierID());
      if (result == null) {
        return super.create(eClass);
      }
      return (EObject)result;
    }
  }
  
  private static final int DOCUMENT_ROOT = 0;
  private static final int DOCUMENT_ROOT__MIXED = 0;
  private static final int DOCUMENT_ROOT__XMLNS_PREFIX_MAP = 1;
  private static final int DOCUMENT_ROOT__XSI_SCHEMA_LOCATION = 2;
  private static final String ANNOTATION_SOURCE = "http:///org/eclipse/emf/ecore/util/ExtendedMetaData";
  private EClass documentRootEClass = null;

  private void createDocumentRoot()
  {
    documentRootEClass = ecoreFactory.createEClass();
    ((EClassImpl)documentRootEClass).setClassifierID(DOCUMENT_ROOT);
    getEClassifiers().add(DOCUMENT_ROOT, documentRootEClass);

    createEAttribute(documentRootEClass, DOCUMENT_ROOT__MIXED);
    createEReference(documentRootEClass, DOCUMENT_ROOT__XMLNS_PREFIX_MAP);
    createEReference(documentRootEClass, DOCUMENT_ROOT__XSI_SCHEMA_LOCATION);
    
    initEClass(documentRootEClass, null, "DocumentRoot", !IS_ABSTRACT, !IS_INTERFACE, !IS_GENERATED_INSTANCE_CLASS);
    initEAttribute((EAttribute)documentRootEClass.getEStructuralFeatures().get(DOCUMENT_ROOT__MIXED), ecorePackage.getEFeatureMapEntry(), "mixed", null, 0, -1, null, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, !IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
    initEReference((EReference)documentRootEClass.getEStructuralFeatures().get(DOCUMENT_ROOT__XMLNS_PREFIX_MAP), ecorePackage.getEStringToStringMapEntry(), null, "xMLNSPrefixMap", null, 0, -1, null, IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
    initEReference((EReference)documentRootEClass.getEStructuralFeatures().get(DOCUMENT_ROOT__XSI_SCHEMA_LOCATION), ecorePackage.getEStringToStringMapEntry(), null, "xSISchemaLocation", null, 0, -1, null, IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);

    addAnnotation
      (documentRootEClass, 
       ANNOTATION_SOURCE, 
       new String[] 
       {
       "name", "",
       "kind", "mixed"
       });      
    addAnnotation
      ((EAttribute)documentRootEClass.getEStructuralFeatures().get(DOCUMENT_ROOT__MIXED), 
       ANNOTATION_SOURCE, 
       new String[] 
       {
       "kind", "elementWildcard",
       "name", ":mixed"
       });      
    addAnnotation
      ((EReference)documentRootEClass.getEStructuralFeatures().get(DOCUMENT_ROOT__XMLNS_PREFIX_MAP), 
       ANNOTATION_SOURCE, 
       new String[] 
       {
       "kind", "attribute",
       "name", "xmlns:prefix"
       });      
    addAnnotation
      ((EReference)documentRootEClass.getEStructuralFeatures().get(DOCUMENT_ROOT__XSI_SCHEMA_LOCATION), 
       ANNOTATION_SOURCE, 
       new String[] 
       {
       "kind", "attribute",
       "name", "xsi:schemaLocation"
       });      
  }
  
  /**
   * Initialize SDO runtime.
   */
  static
  {
    DataObjectUtil.initRuntime();
  }
}