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: package org.netbeans.modules.uml.core.reverseengineering.reframework;
043:
044: import java.util.List;
045:
046: import org.dom4j.Document;
047: import org.dom4j.Element;
048: import org.dom4j.Node;
049:
050: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ITokenDescriptor;
051: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
052: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
053: import org.netbeans.modules.uml.core.support.umlutils.ETList;
054:
055: /**
056: */
057: public class ParserData implements IParserData {
058: private Node m_EventData;
059:
060: /**
061: * Retrieves the event data in a pure XMI format. All TokenDescriptors
062: * will be removed from the string.
063: * @return The XMI data.
064: */
065: public String getXMIString() {
066: // Commented out in C++ code.
067: return null;
068: }
069:
070: /**
071: * The event data in its pure format. All TokenDescriptors will
072: * still exist in the string.
073: * @return The data stream.
074: */
075: public String getRawData() {
076: Node node = getEventData();
077: return node != null ? node.asXML() : null;
078: }
079:
080: /**
081: * Retrieves the event data. The data should only be set by the sender of the event.
082: * @param pVal [OUT] The XML document that contains the event data.
083: */
084: public Node getEventData() {
085: return m_EventData instanceof Document ? ((Document) m_EventData)
086: .getRootElement()
087: : m_EventData;
088: }
089:
090: /**
091: * Sets the event data. The data should only be set by the sender of the event.
092: * @param newValue [IN] The XML document that contains the event data.
093: */
094: public void setEventData(Node value) {
095: m_EventData = value;
096: }
097:
098: /**
099: * Retrieves the token descriptors for the parser event.
100: * @param pVal The token descriptors for the parser data.
101: * @return S_OK if successful.
102: */
103: public ETList<ITokenDescriptor> getTokenDescriptors() {
104: Node node = getEventData();
105: return node != null ? getTokenDescriptors(node) : null;
106: }
107:
108: /**
109: * Retrieves the token descriptors for the parser event. The token descriptors will
110: * be contained by the specified node.
111: * @param node The DOM node to search.
112: * @param pVal The token descriptors for the parser data.
113: * @return S_OK if successful.
114: */
115: private ETList<ITokenDescriptor> getTokenDescriptors(Node node) {
116: Node descriptorNode = node.selectSingleNode("TokenDescriptors");
117: if (descriptorNode != null) {
118: ETList<ITokenDescriptor> descriptors = new ETArrayList<ITokenDescriptor>();
119: List nodes = descriptorNode.selectNodes("TDescriptor");
120: for (int i = 0, nc = nodes.size(); i < nc; ++i) {
121: IXMLTokenDescriptor desc = new XMLTokenDescriptor();
122: desc.setTokenDescriptorNode((Node) nodes.get(i));
123: descriptors.add(desc);
124: }
125: return descriptors;
126: }
127: return null;
128: }
129:
130: /**
131: * Retrieves a node from the XML data. The XML node contained by the
132: * parser data instance will be searched.
133: * @param query [in] The query used to find the node.
134: * @param pVal [out] The XML node found.
135: */
136: protected Node getXMLNode(String query) {
137: Node data = getEventData();
138: return data != null ? XMLManip.selectSingleNode(data, query)
139: : null;
140: }
141:
142: /**
143: * Retrieves a token descriptor of a specified type.
144: * @param type The type of the descriptor.
145: * @param pVal The descriptor of NULL if a token descriptor or the specified
146: * type is not found.
147: */
148: public ITokenDescriptor getTokenDescriptor(String type) {
149: Node data = getEventData();
150: return data != null ? getTokenDescriptor(data, type) : null;
151: }
152:
153: /**
154: * Retrieves the value of a token descriptor of a specified type.
155: * @param type The type of the descriptor.
156: * @return The value of the descriptor if found, else <code>null</code>.
157: */
158: protected String getDescriptorValue(String type) {
159: ITokenDescriptor desc = getTokenDescriptor(type);
160: return desc != null ? desc.getValue() : null;
161: }
162:
163: /**
164: * Retrieves a token descriptor of a specified type.
165: * @param pNode The XML node to search.
166: * @param type The type of the descriptor.
167: * @param pVal The descriptor of NULL if a token descriptor or the specified
168: * type is not found.
169: */
170: private ITokenDescriptor getTokenDescriptor(Node node, String type) {
171: ETList<ITokenDescriptor> descriptors = getTokenDescriptors(node);
172: if (descriptors != null) {
173: int descCount = descriptors.size();
174: for (int i = 0; i < descCount; ++i) {
175: ITokenDescriptor desc = descriptors.get(i);
176: if (desc == null)
177: continue;
178:
179: String curType = desc.getType();
180: if (curType != null && curType.equals(type))
181: return desc;
182: }
183: }
184: return null;
185: }
186:
187: /**
188: * Retrieves the name of the file that contained the source code element.
189: * @param pVal The file name.
190: */
191: public String getFilename() {
192: return getFilename(getEventData());
193: }
194:
195: /**
196: * Retrieves the name of the file that contained the source code element.
197: * The specified XML node is search for a token descriptor with the type
198: * of "Filename". If the token descriptor is not found then the parent
199: * XML node is searched.
200: * @param pNode The XML node to search.
201: * @param pVal The file name.
202: */
203: protected String getFilename(Node node) {
204: if (node == null)
205: return null;
206: ITokenDescriptor desc = getTokenDescriptor(node, "Filename");
207: if (desc != null)
208: return desc.getValue();
209: Node n = XMLManip.selectSingleNode(node,
210: "UML:Element.ownedElement/UML:SourceFileArtifact");
211: if (n != null)
212: return XMLManip.getAttributeValue(n, "sourcefile");
213: return getFilename(node.getParent());
214: }
215:
216: /**
217: * Returns the value of the token descriptor whose type is @a type
218: *
219: * @param type[in] the type of token descriptor
220: * @param pVal[in] the value of the token descriptor
221: */
222: protected String getTokenDescriptorValue(String type) {
223: ITokenDescriptor desc = getTokenDescriptor(type);
224: return desc != null ? desc.getValue() : null;
225: }
226: }
|