KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > common > DDUtils


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.dd.impl.common;
20
21 import org.netbeans.modules.j2ee.dd.api.client.AppClient;
22 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
23 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
24 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
25 import org.netbeans.modules.j2ee.dd.impl.ejb.EjbJarProxy;
26 import org.netbeans.modules.schema2beans.Schema2BeansRuntimeException;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXParseException JavaDoc;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.Reader JavaDoc;
34
35 /**
36  * @author pfiala
37  */

38 public class DDUtils {
39     public static EjbJarProxy createEjbJarProxy(InputStream JavaDoc inputStream) throws IOException JavaDoc {
40         return createEjbJarProxy(new InputSource JavaDoc(inputStream));
41     }
42     
43     public static EjbJarProxy createEjbJarProxy(Reader JavaDoc reader) throws IOException JavaDoc {
44         return createEjbJarProxy(new InputSource JavaDoc(reader));
45     }
46     
47     public static EjbJarProxy createEjbJarProxy(InputSource JavaDoc inputSource) throws IOException JavaDoc {
48         try {
49             return (EjbJarProxy) DDProvider.getDefault().getDDRoot(inputSource);
50         } catch (SAXException JavaDoc ex) {
51             // XXX lets throw an exception here
52
EjbJar ejbJar = org.netbeans.modules.j2ee.dd.impl.ejb.model_2_0.EjbJar.createGraph();
53             EjbJarProxy ejbJarProxy = new EjbJarProxy(ejbJar, ejbJar.getVersion().toString());
54             ejbJarProxy.setStatus(EjbJar.STATE_INVALID_UNPARSABLE);
55             if (ex instanceof SAXParseException JavaDoc) {
56                 ejbJarProxy.setError((SAXParseException JavaDoc) ex);
57             } else if (ex.getException() instanceof SAXParseException JavaDoc) {
58                 ejbJarProxy.setError((SAXParseException JavaDoc) ex.getException());
59             }
60             return ejbJarProxy;
61         }
62     }
63     
64     
65     public static void merge(EjbJarProxy ejbJarProxy, Reader JavaDoc reader) {
66         try {
67             EjbJarProxy newEjbJarProxy = createEjbJarProxy(reader);
68             if (newEjbJarProxy.getStatus() == EjbJar.STATE_INVALID_UNPARSABLE) {
69                 ejbJarProxy.setStatus(EjbJar.STATE_INVALID_UNPARSABLE);
70                 ejbJarProxy.setError(newEjbJarProxy.getError());
71                 return;
72             }
73             ejbJarProxy.merge(newEjbJarProxy, EjbJar.MERGE_UPDATE);
74             ejbJarProxy.setStatus(newEjbJarProxy.getStatus());
75             ejbJarProxy.setError(newEjbJarProxy.getError());
76         } catch (IOException JavaDoc ex) {
77             ejbJarProxy.setStatus(EjbJar.STATE_INVALID_UNPARSABLE);
78             // cbw if the state of the xml file transitions from
79
// parsable to unparsable this could be due to a user
80
// change or cvs change. We would like to still
81
// receive events when the file is restored to normal
82
// so lets not set the original to null here but wait
83
// until the file becomes parsable again to do a merge
84
//ejbJarProxy.setOriginal(null);
85
} catch (Schema2BeansRuntimeException ex2){ // see #70286
86
ejbJarProxy.setStatus(EjbJar.STATE_INVALID_UNPARSABLE);
87             ejbJarProxy.setError(new SAXParseException JavaDoc(null, null, ex2));
88         }
89     }
90     
91     public static WebApp createWebApp(InputStream JavaDoc is, String JavaDoc version) throws IOException JavaDoc, SAXException JavaDoc {
92         try {
93             if (WebApp.VERSION_2_3.equals(version)) {
94                 return org.netbeans.modules.j2ee.dd.impl.web.model_2_3.WebApp.createGraph(is);
95             } else if (WebApp.VERSION_2_4.equals(version)) {
96                 return org.netbeans.modules.j2ee.dd.impl.web .model_2_4.WebApp.createGraph(is);
97             } else /*if (WebApp.VERSION_2_5.equals(version))*/ {
98                 return org.netbeans.modules.j2ee.dd.impl.web .model_2_5.WebApp.createGraph(is);
99             }
100         } catch (RuntimeException JavaDoc ex) {
101             throw new SAXException JavaDoc(ex);
102         }
103     }
104     
105     public static AppClient createAppClient(InputStream JavaDoc is, String JavaDoc version) throws IOException JavaDoc, SAXException JavaDoc {
106         try {
107             if (AppClient.VERSION_1_3.equals(version)) {
108                 return org.netbeans.modules.j2ee.dd.impl.client.model_1_3.ApplicationClient.createGraph(is);
109             } else if (AppClient.VERSION_1_4.equals(version)) {
110                 return org.netbeans.modules.j2ee.dd.impl.client.model_1_4.ApplicationClient.createGraph(is);
111             } else if (AppClient.VERSION_5_0.equals(version)) {
112                 return org.netbeans.modules.j2ee.dd.impl.client.model_5_0.ApplicationClient.createGraph(is);
113             }
114         } catch (RuntimeException JavaDoc ex) {
115             throw new SAXException JavaDoc(ex);
116         }
117         return null;
118     }
119 }
120
Popular Tags