001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * WebsphereDeploymentDescriptorParser.java
043: *
044: * Created on August 3, 2004, 4:24 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import java.io.StringReader;
050: import java.util.HashMap;
051: import java.util.Map;
052:
053: import javax.xml.parsers.ParserConfigurationException;
054: import javax.xml.parsers.SAXParser;
055: import javax.xml.parsers.SAXParserFactory;
056:
057: import org.openide.ErrorManager;
058: import org.openide.util.NbBundle;
059: import org.xml.sax.Attributes;
060: import org.xml.sax.InputSource;
061: import org.xml.sax.SAXException;
062: import org.xml.sax.helpers.DefaultHandler;
063:
064: /**
065: * To parse the websphere specific deployment descriptor - ibm-ejb-jar-bnd.xmi
066: * @author cao
067: */
068: public class WebsphereDeploymentDescriptorParser extends DefaultHandler {
069:
070: private static final String EJB_BINDINGS_TAG = "ejbBindings";
071: private static final String ENTERPRISE_BEAN_TAG = "enterpriseBean";
072: private static final String JNDI_NAME_ATTR = "jndiName";
073: private static final String HREF_ATTR = "href";
074:
075: private String xmlFileName;
076:
077: private String jndiName;
078: private String beanId;
079:
080: private Map nameMapping = new HashMap();
081:
082: public WebsphereDeploymentDescriptorParser(String vendorXml) {
083: this .xmlFileName = vendorXml;
084: }
085:
086: /**
087: * @return a map of (bean id, jndi name ) pairs
088: */
089: public Map parse() throws EjbLoadException {
090: try {
091: SAXParserFactory factory = SAXParserFactory.newInstance();
092: factory.setNamespaceAware(true);
093: factory.setValidating(false);
094: SAXParser parser = factory.newSAXParser();
095: parser.parse(xmlFileName, this );
096:
097: return nameMapping;
098: } catch (java.io.IOException e) {
099: // Log error
100: String logMsg = "Error occured when trying to parse the vendor EJB deployment descriptor. Cannot read file "
101: + xmlFileName;
102: ErrorManager
103: .getDefault()
104: .getInstance(
105: "org.netbeans.modules.visualweb.ejb.load.WebsphereDeploymentDescriptorParser")
106: .log(ErrorManager.WARNING, logMsg);
107: e.printStackTrace();
108:
109: // Throw up as SYSTEM_ERROR. Should never happen
110: throw new EjbLoadException(e.getMessage());
111: } catch (ParserConfigurationException e) {
112: // Log error
113: String logMsg = "Error occured when trying to parse the vendor EJB deployment descriptor file "
114: + xmlFileName;
115: ErrorManager
116: .getDefault()
117: .getInstance(
118: "org.netbeans.modules.visualweb.ejb.load.WebsphereDeploymentDescriptorParser")
119: .log(ErrorManager.WARNING, logMsg);
120: e.printStackTrace();
121:
122: String errMsg = NbBundle.getMessage(
123: StdDeploymentDescriptorParser.class,
124: "CANNOT_PARSE_VENDOR_DD");
125: throw new EjbLoadException(EjbLoadException.USER_ERROR,
126: errMsg);
127: } catch (SAXException e) {
128: // Log error
129: String logMsg = "Error occured when trying to parse the vendor EJB deployment descriptor file "
130: + xmlFileName;
131: ErrorManager
132: .getDefault()
133: .getInstance(
134: "org.netbeans.modules.visualweb.ejb.load.WebsphereDeploymentDescriptorParser")
135: .log(ErrorManager.WARNING, logMsg);
136: e.printStackTrace();
137:
138: String errMsg = NbBundle.getMessage(
139: StdDeploymentDescriptorParser.class,
140: "CANNOT_PARSE_VENDOR_DD");
141: throw new EjbLoadException(EjbLoadException.USER_ERROR,
142: errMsg);
143: }
144: }
145:
146: public void startElement(String uri, String localName,
147: String qName, Attributes attributes) throws SAXException {
148: if (qName.equalsIgnoreCase(EJB_BINDINGS_TAG)) {
149: jndiName = attributes.getValue(JNDI_NAME_ATTR);
150: }
151:
152: if (qName.equalsIgnoreCase(ENTERPRISE_BEAN_TAG)) {
153: String href = attributes.getValue(HREF_ATTR);
154:
155: // The id will be something like "META-INF/ejb-jar.xml#Greeter"
156: // We only need the one after the #
157:
158: beanId = href.substring(href.indexOf('#') + 1);
159: }
160: }
161:
162: public void endElement(String uri, String localName, String qName)
163: throws SAXException {
164: if (qName.equalsIgnoreCase(EJB_BINDINGS_TAG)) {
165: nameMapping.put(beanId, jndiName);
166: beanId = null;
167: jndiName = null;
168: }
169: }
170:
171: public void characters(char buf[], int offset, int len)
172: throws SAXException {
173: }
174:
175: public InputSource resolveEntity(String publicId, String systemId) {
176: // Ignore any external entities
177: return new InputSource(new StringReader(""));
178: }
179: }
|