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: * WeblogicXmlParser.java
043: *
044: * Created on June 25, 2004, 11:55 AM
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: * It is used to parse weblogic specific deployment descriptor
066: * - weblogic-ejb-jar.xml
067: *
068: * @author cao
069: */
070: public class WeblogicDeploymentDescriptorParser extends DefaultHandler {
071:
072: private static final String EJB_TAG = "weblogic-enterprise-bean";
073: private static final String EJB_NAME_TAG = "ejb-name";
074: private static final String JNDI_NAME_TAG = "jndi-name";
075:
076: private String xmlFileName;
077:
078: private String jndiName;
079: private String ejbName;
080:
081: private String currentTag;
082: private String data;
083:
084: private Map nameMapping = new HashMap();
085:
086: public WeblogicDeploymentDescriptorParser(String vendorXml) {
087: this .xmlFileName = vendorXml;
088: }
089:
090: public Map parse() throws EjbLoadException {
091: try {
092: SAXParserFactory factory = SAXParserFactory.newInstance();
093: factory.setNamespaceAware(true);
094: factory.setValidating(false);
095: SAXParser parser = factory.newSAXParser();
096: parser.parse(xmlFileName, this );
097:
098: return nameMapping;
099: } catch (java.io.IOException e) {
100: // Log error
101: String logMsg = "Error occured when trying to parse the vendor EJB deployment descriptor. Cannot read file "
102: + xmlFileName;
103: ErrorManager
104: .getDefault()
105: .getInstance(
106: "org.netbeans.modules.visualweb.ejb.load.WeblogicDeploymentDescriptorParser")
107: .log(ErrorManager.WARNING, logMsg);
108: e.printStackTrace();
109:
110: // Throw up as SYSTEM_ERROR. Should never happen
111: throw new EjbLoadException(e.getMessage());
112: } catch (ParserConfigurationException e) {
113: // Log error
114: String logMsg = "Error occured when trying to parse the vendor EJB deployment descriptor file "
115: + xmlFileName;
116: ErrorManager
117: .getDefault()
118: .getInstance(
119: "org.netbeans.modules.visualweb.ejb.load.WeblogicDeploymentDescriptorParser")
120: .log(ErrorManager.WARNING, logMsg);
121: e.printStackTrace();
122:
123: String errMsg = NbBundle.getMessage(
124: StdDeploymentDescriptorParser.class,
125: "CANNOT_PARSE_VENDOR_DD");
126: throw new EjbLoadException(EjbLoadException.USER_ERROR,
127: errMsg);
128: } catch (SAXException e) {
129: // Log error
130: String logMsg = "Error occured when trying to parse the vendor EJB deployment descriptor file "
131: + xmlFileName;
132: ErrorManager
133: .getDefault()
134: .getInstance(
135: "org.netbeans.modules.visualweb.ejb.load.WeblogicDeploymentDescriptorParser")
136: .log(ErrorManager.WARNING, logMsg);
137: e.printStackTrace();
138:
139: String errMsg = NbBundle.getMessage(
140: StdDeploymentDescriptorParser.class,
141: "CANNOT_PARSE_VENDOR_DD");
142: throw new EjbLoadException(EjbLoadException.USER_ERROR,
143: errMsg);
144: }
145: }
146:
147: public void startElement(String uri, String localName,
148: String qName, Attributes attributes) throws SAXException {
149: currentTag = qName;
150: }
151:
152: public void endElement(String uri, String localName, String qName)
153: throws SAXException {
154: if (currentTag != null)
155: setData();
156:
157: if (qName.equalsIgnoreCase(EJB_TAG) && ejbName != null
158: && jndiName != null) {
159: nameMapping.put(ejbName, jndiName);
160: ejbName = null;
161: jndiName = null;
162: }
163:
164: currentTag = null;
165: data = null;
166: }
167:
168: public void characters(char buf[], int offset, int len)
169: throws SAXException {
170: if (data == null)
171: data = new String(buf, offset, len);
172: else
173: data = data + new String(buf, offset, len);
174: }
175:
176: private void setData() {
177: if (data != null)
178: data = data.trim();
179:
180: if (currentTag.equalsIgnoreCase(JNDI_NAME_TAG))
181: jndiName = data;
182: else if (currentTag.equalsIgnoreCase(EJB_NAME_TAG))
183: ejbName = data;
184: }
185:
186: public InputSource resolveEntity(String publicId, String systemId) {
187: // Ignore any external entities
188: return new InputSource(new StringReader(""));
189: }
190:
191: }
|