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: * SunXmlParser.java
043: *
044: * Created on April 28, 2004, 11:04 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: * This class is used to parse the Sun specific deployment
066: * descriptor - sun-ejb-jar.xml.
067: */
068: public class SunDeploymentDescriptorParser extends DefaultHandler {
069:
070: private static final String EJB_TAG = "ejb";
071: private static final String EJB_NAME_TAG = "ejb-name";
072: private static final String JNDI_NAME_TAG = "jndi-name";
073:
074: private String xmlFileName;
075:
076: private String jndiName;
077: private String ejbName;
078:
079: private String currentTag;
080: private String data;
081:
082: private Map nameMapping = new HashMap();
083:
084: /** Creates a new instance of VendorXmlParser */
085: public SunDeploymentDescriptorParser(String vendorXml) {
086: this .xmlFileName = vendorXml;
087: }
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.SunDeploymentDescriptorParser")
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.SunDeploymentDescriptorParser")
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.SunDeploymentDescriptorParser")
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: currentTag = qName;
149: }
150:
151: public void endElement(String uri, String localName, String qName)
152: throws SAXException {
153: if (currentTag != null)
154: setData();
155:
156: if (ejbName != null && jndiName != null) {
157: nameMapping.put(ejbName, jndiName);
158: ejbName = null;
159: jndiName = null;
160: }
161:
162: currentTag = null;
163: data = null;
164: }
165:
166: public void characters(char buf[], int offset, int len)
167: throws SAXException {
168: if (data == null)
169: data = new String(buf, offset, len);
170: else
171: data = data + new String(buf, offset, len);
172: }
173:
174: private void setData() {
175: if (data != null)
176: data = data.trim();
177:
178: if (currentTag.equalsIgnoreCase(JNDI_NAME_TAG))
179: jndiName = data;
180: else if (currentTag.equalsIgnoreCase(EJB_NAME_TAG))
181: ejbName = data;
182: }
183:
184: public InputSource resolveEntity(String publicId, String systemId) {
185: // Ignore any external entities
186: return new InputSource(new StringReader(""));
187: }
188: }
|