001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: *
024: */
025: package com.bostechcorp.cbesb.common.util.project;
026:
027: import java.io.File;
028: import java.util.Stack;
029:
030: import javax.xml.parsers.SAXParser;
031: import javax.xml.parsers.SAXParserFactory;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.xml.sax.Attributes;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: import com.bostechcorp.cbesb.common.util.ErrorUtil;
040:
041: public class ProjectInfoParser extends DefaultHandler {
042:
043: private Stack<Object> loadStack;
044:
045: private ProjectInfoDocument mdlDoc;
046:
047: protected static transient Log logger = LogFactory
048: .getLog(ProjectInfoParser.class);
049:
050: public ProjectInfoParser() {
051: loadStack = new Stack<Object>();
052: mdlDoc = new ProjectInfoDocument();
053: }
054:
055: /**
056: * Static method to instantiate an MDLDocument object from a File.
057: *
058: * @param filename The File to load
059: * @return The loaded MDLDocument
060: * @throws MDLException If any errors occur.
061: */
062: public static ProjectInfoDocument load(File filename) {
063: ProjectInfoParser parserInstance = new ProjectInfoParser();
064: return parserInstance.parse(filename);
065: }
066:
067: /**
068: * Perform cleanup so GC can free up some memory
069: */
070: public void finalize() {
071: loadStack.clear();
072: loadStack = null;
073: mdlDoc = null;
074: }
075:
076: /* (non-Javadoc)
077: * SAX Handler Methods
078: * @see org.xml.sax.helpers.DefaultHandler#startDocument()
079: */
080: public void startDocument() throws SAXException {
081: super .startDocument();
082: }
083:
084: /* (non-Javadoc)
085: * SAX Handler Methods
086: * @see org.xml.sax.helpers.DefaultHandler#endDocument()
087: */
088: public void endDocument() throws SAXException {
089: super .endDocument();
090: }
091:
092: /* (non-Javadoc)
093: * SAX Handler Methods
094: * @see org.xml.sax.helpers.DefaultHandler#startPrefixMapping(java.lang.String, java.lang.String)
095: */
096: public void startPrefixMapping(String prefix, String uri)
097: throws SAXException {
098: super .startPrefixMapping(prefix, uri);
099: }
100:
101: /* (non-Javadoc)
102: * SAX Handler Methods
103: * @see org.xml.sax.helpers.DefaultHandler#endPrefixMapping(java.lang.String)
104: */
105: public void endPrefixMapping(String prefix) throws SAXException {
106: super .endPrefixMapping(prefix);
107: }
108:
109: /* (non-Javadoc)
110: * SAX Handler Methods
111: * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
112: */
113: public void startElement(String uri, String localName,
114: String qName, Attributes attributes) throws SAXException {
115: super .startElement(uri, localName, qName, attributes);
116: // Ignore elements that do not belong to the MDL Namespace
117: // if (uri.equals(MDL_NAMESPACE)) {
118: try {
119: if (localName.equals("project")) {
120: startProjectNode(attributes);
121: } else if (localName.equals("subproject")
122: || localName.equals("superproject")) {
123: startSubProjectNode(attributes);
124: }
125:
126: } catch (Exception e) {
127: ErrorUtil.printError("Exception in startElement(): ", e);
128: // SAX API forces us to wrap the exception in SAXException
129: throw new SAXException(e);
130: }
131: }
132:
133: /* (non-Javadoc)
134: * SAX Handler Methods
135: * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
136: */
137: public void endElement(String uri, String localName, String qName)
138: throws SAXException {
139: super .endElement(uri, localName, qName);
140: // Ignore elements that do not belong to the MDL Namespace
141: }
142:
143: private ProjectInfoDocument parse(File filename) {
144: try {
145: SAXParserFactory factory = SAXParserFactory.newInstance();
146: // System.out.println(filename.getAbsolutePath());
147: factory.setNamespaceAware(true);
148: SAXParser saxParser = factory.newSAXParser();
149: saxParser.parse(filename, this );
150: // resolveRefs();
151: } catch (Exception e) {
152: logger.error("Exception in parse(): " + e.getMessage());
153: if (logger.isDebugEnabled()) {
154: logger.debug("Exception in parse():", e);
155: }
156: }
157: return mdlDoc;
158:
159: }
160:
161: /**
162: * MDL Specific Methods
163: *
164: * @param attributes Attributes
165: * @throws MDLException MDLException
166: */
167: private void startProjectNode(Attributes attributes) {
168: String projectName = attributes.getValue("", "name");
169: String projectType = attributes.getValue("", "type");
170: ProjectBean projectBean = new ProjectBean();
171: if (projectName != null) {
172: projectBean.setName(projectName);
173: projectBean.setType(projectType);
174: }
175: mdlDoc.setProject(projectBean);
176:
177: loadStack.push(mdlDoc);
178: }
179:
180: private void startSubProjectNode(Attributes attributes) {
181: String projectName = attributes.getValue("", "name");
182: String projectType = attributes.getValue("", "type");
183: SubProjectBean subProjectBean = new SubProjectBean();
184: if (projectName != null) {
185: subProjectBean.setName(projectName);
186: subProjectBean.setType(projectType);
187: }
188: mdlDoc.getProject().addNewSubProject(subProjectBean);
189:
190: loadStack.push(mdlDoc);
191: }
192: }
|