summaryrefslogtreecommitdiffstats
path: root/tags/java-stable-20060304/sca/model/src/main/java/org/apache/tuscany/model/scdl/loader/impl/SCDLXMLReader.java
blob: 6fa426b2f8e1bbabed8a092752adb4ca3a69aab1 (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
/**
 *
 *  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.model.scdl.loader.impl;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.tuscany.model.scdl.ComponentType;
import org.apache.tuscany.model.scdl.Module;
import org.apache.tuscany.model.scdl.ModuleFragment;
import org.apache.tuscany.model.scdl.ScdlFactory;
import org.apache.tuscany.model.scdl.Subsystem;
import org.apache.tuscany.model.scdl.impl.ScdlPackageImpl;
import org.apache.tuscany.sdo.util.DataObjectUtil;
import org.apache.tuscany.sdo.util.SDOUtil;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import commonj.sdo.helper.XMLDocument;
import commonj.sdo.helper.XMLHelper;

/**
 */
public class SCDLXMLReader extends ResourceSetImpl {
    
    private Map<String, Object> cache=new HashMap<String, Object>();

    // Initialize the SDO runtime and register the SCDL model
    static {
        DataObjectUtil.initRuntime();
        SDOUtil.registerStaticTypes(ScdlFactory.class);
    }

    /**
     * Constructor
     */
    public SCDLXMLReader() {
    }

    /**
     * Returns an SCDL module.
     * @param uri
     * @return
     */
    public Module getModule(String uri) {
        return (Module)getRootObject(uri);
    }

    /**
     * Returns an SCDL module fragment.
     * @param uri
     * @return
     */
    public ModuleFragment getModuleFragment(String uri) {
        return (ModuleFragment)getRootObject(uri);
    }

    /**
     * Returns an SCDL component type.
     * @param uri
     * @return
     */
    public ComponentType getComponentType(String uri) {
        return (ComponentType)getRootObject(uri);
    }
    
    /**
     * Returns an SCDL subsystem.
     * @param uri
     * @return
     */
    public Subsystem getSubsystem(String uri) {
        return (Subsystem)getRootObject(uri);
    }

    /**
     * Returns the root object at the given URI.
     * @param uri
     * @return
     */
    private Object getRootObject(String uri) {
        Object object = cache.get(uri);
        if (object==null) {
            try {
                XMLDocument document=XMLHelper.INSTANCE.load(new URL(uri).openStream());
                return document.getRootObject();
           } catch (IOException e) {
               throw new RuntimeException(uri, e);
           }
        }
        return object;
    }
    
}