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: /*
043: * File : NewLineCommentGather.java
044: * Created on : Oct 27, 2003
045: * Author : Aztec
046: */
047: package org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework;
048:
049: import antlr.CommonASTWithHiddenTokens;
050: import antlr.CommonHiddenStreamToken;
051: import antlr.collections.AST;
052:
053: /**
054: * @author Aztec
055: */
056: public class NewLineCommentGather extends CommentGather implements
057: INewLineCommentGather {
058: private int m_NewLineType;
059:
060: public NewLineCommentGather() {
061: }
062:
063: public NewLineCommentGather(int slComment, int mlComment,
064: int newline) {
065: super (slComment, mlComment);
066: m_NewLineType = newline;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.INewLineCommentGather#gather(antlr.collections.AST, org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ITokenDescriptor)
071: */
072: public ITokenDescriptor gather(AST pAST, ITokenDescriptor pDesc) {
073: if (pAST == null)
074: return pDesc;
075:
076: if (pDesc == null)
077: pDesc = new TokenDescriptor();
078:
079: CommonASTWithHiddenTokens pToken = null;
080:
081: try {
082: pToken = (CommonASTWithHiddenTokens) pAST;
083: } catch (ClassCastException ignored) {
084: }
085:
086: if (pToken == null)
087: return null;
088:
089: CommonHiddenStreamToken pHiddenToken = pToken.getHiddenBefore();
090:
091: if (pHiddenToken != null
092: && pHiddenToken.getType() == getNewLineType()) {
093: return gatherFromNode(pHiddenToken, pDesc);
094: }
095: return null;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.INewLineCommentGather#getNewLineType()
100: */
101: public int getNewLineType() {
102: return m_NewLineType;
103: }
104:
105: private ITokenDescriptor gatherFromNode(
106: CommonHiddenStreamToken pHiddenToken, ITokenDescriptor pDesc) {
107: int startLine = -1;
108: int startColumn = -1;
109: int startPos = -1;
110: int length = -1;
111:
112: boolean processingComment = false;
113: String comment = "";
114: int lastType = -1;
115:
116: while (pHiddenToken != null) {
117: int type = pHiddenToken.getType();
118:
119: if (type == getSingleLineType()
120: || type == getMultiLineType()) {
121: // The problem is two fold.
122: // One: since the new line token is not included
123: // in the comment we must manually add the line characters to the return string.
124: // Two: Since there can be NEWLINE tokens that are not associated with comments
125: // then we do not want to manually add the new line characters to the return string.
126: if (lastType == getNewLineType()) {
127: comment = "\r\n" + comment;
128: }
129: comment = pHiddenToken.getText() + comment;
130:
131: startLine = pHiddenToken.getLine();
132: startColumn = pHiddenToken.getColumn();
133: // TODO: Aztec - Need to modify source if we want this
134: //startPos = pHiddenToken.getPosition();
135:
136: String value = pHiddenToken.getText();
137: length += value.length();
138:
139: processingComment = true;
140: }
141:
142: if (pHiddenToken != null && type != getMultiLineType()) {
143: pHiddenToken = pHiddenToken.getHiddenBefore();
144: } else if (type == getMultiLineType()) {
145: // If we have found a multiline comment there is no reason to continue.
146: // basically we only want to continue if the comment is a single line
147: // comment.
148: pHiddenToken = null;
149: } else if (pHiddenToken != null
150: && pHiddenToken.getType() != getNewLineType()) {
151: // When we are handling comments that do not process the new line comment (because
152: // the newline character is used as the line terminator) a newline token type must
153: // be present to continue. REASON: The comment will always be a hidden comment of a
154: // new line comment.
155: pHiddenToken = null;
156:
157: }
158: lastType = type;
159: }
160: // If found a comment I want to add it to the token descriptors property. Since the
161: // comment is optional for a token I am making it a property. The idea is that only
162: // required elements are fields on token descriptors. Also a comment is not really a
163: // entity of the specific token. I is really property of a state (class, operation, or attribute).
164: // However it is only able to be retrieved by a token.
165: if (comment != null && comment.trim().length() > 0) {
166: // If we fail I do not want to worry about it.
167: pDesc.addProperty("Comment", comment);
168: pDesc.addProperty("CommentStartLine", String
169: .valueOf(startLine));
170: pDesc.addProperty("CommentStartColumn", String
171: .valueOf(startColumn));
172: pDesc.addProperty("CommentStartPos", String
173: .valueOf(startPos));
174: pDesc.addProperty("CommentLength", String
175: .valueOf(length + 1));
176: }
177:
178: return pDesc;
179: }
180:
181: }
|