KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > MDRXMLModule


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Forte for Java, Community Edition. The Initial
16  * Developer of the Original Software is Sun Microsystems, Inc. Portions
17  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.mdrxml;
21
22 import java.util.*;
23
24 import javax.jmi.reflect.RefPackage;
25 import javax.jmi.model.*;
26
27 import org.openide.TopManager;
28 import org.openide.util.*;
29 import org.openide.modules.ModuleInstall;
30 import org.netbeans.api.mdr.*;
31 import xmlmodel.*;
32
33 public class MDRXMLModule extends ModuleInstall {
34
35     // standard extent name for all MOF metamodels
36
public static final String JavaDoc XML_METAMODEL = "org.netbeans.mof";
37     // extent name for instance of metamodel of XML
38
public static final String JavaDoc XML_MODEL = "org.netbeans.xmlmodel";
39     // name of XML metamodel package
40
public static final String JavaDoc XML_PACKAGE = "XMLModel";
41     
42     /** Called when module is installed for the first time.
43      */

44     public void installed () {
45         restored();
46     }
47
48     /** Module is being loaded into the IDE.
49      */

50     public void restored () {
51         RequestProcessor.postRequest(
52             new Runnable JavaDoc() {
53                 public void run() {
54                     TopManager.getDefault().setStatusText("Restoring metamodel of XML...");
55                     installXML();
56                     TopManager.getDefault().setStatusText("");
57                 }
58             }
59         );
60     }
61
62     /** Module was uninstalled.
63      */

64     public void uninstalled () {
65     }
66
67     /** Module is being closed.
68      * @return True if the close is O.K.
69      */

70     public boolean closing () {
71         return true; // agree to close
72
}
73
74     // PRIVATE METHODS ---------------------------------------------------------
75

76     public static void installXML() {
77         // find repository implementation using MDRManager
78
MDRepository repository = MDRManager.getDefault().getDefaultRepository();
79
80         // [PENDING] here I should check whether the repository was found
81

82         // get connection to instance of XML metamodel
83
if (repository.getExtent(XML_MODEL) != null)
84             return;
85
86         // if the instance was not found, try to get an instance of MOF metamodel (standard extent for storing metamodels)
87
ModelPackage xmlMMExtent = (ModelPackage) repository.getExtent(XML_METAMODEL);
88         
89         try {
90             if (xmlMMExtent == null) {
91                 // if the MOF instance was not found, create it
92
xmlMMExtent = (ModelPackage) repository.createExtent(XML_METAMODEL);
93             }
94             
95             // find definition of metamodel of XML in the instance of MOF
96
MofPackage xmlMMPackage = findXMLPackage(xmlMMExtent);
97             
98             // if it was not found, read it from its XML definition file
99
if (xmlMMPackage == null) {
100                 // Read the XMI document
101
XMIReader xmiReader = XMIReader.getDefault();
102                 xmiReader.read(XmlmodelPackage.class.getResource("resources/xmlmodel.xml").toString (), new RefPackage[] {xmlMMExtent});
103                 // after the metamodel was read, try to find its definition again
104
xmlMMPackage = findXMLPackage(xmlMMExtent);
105             }
106             
107             // create instance of the metamodel of filesystems from its definition
108
repository.createExtent(XML_MODEL, xmlMMPackage);
109         } catch (CreationFailedException mer) {
110             TopManager.getDefault().getErrorManager().notify (mer);
111             mer.printStackTrace();
112         } catch (java.io.IOException JavaDoc ioex) {
113             TopManager.getDefault().getErrorManager().notify (ioex);
114         } catch(Exception JavaDoc saex) {
115             TopManager.getDefault().getErrorManager().notify (saex);
116         }
117     }
118     
119     private static MofPackage findXMLPackage(ModelPackage extent) {
120         for (Iterator it = extent.getMofPackage().refAllOfClass().iterator(); it.hasNext();) {
121             ModelElement temp = (ModelElement) it.next();
122             if (temp.getName().equals(XML_PACKAGE)) {
123                 return (MofPackage) temp;
124             }
125         }
126
127         return null;
128     }
129 }
130
Popular Tags