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-2006 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: * PPLine.java
044: *
045: * Created on August 12, 2005, 10:22 AM
046: */
047: package org.netbeans.mobility.antext.preprocessor;
048:
049: import java.text.MessageFormat;
050: import java.util.*;
051:
052: /**
053: * @author Adam Sotona
054: */
055: public final class PPLine {
056:
057: public final class Error {
058:
059: public final String message;
060: public final PPToken token;
061: public final boolean warning;
062:
063: Error(String message, PPToken token, boolean warning) {
064: this .warning = warning;
065: this .message = message;
066: this .token = token;
067: }
068:
069: public String toString() {
070: final StringBuffer sb = new StringBuffer();
071: for (int i = 0; i < token.getColumn(); i++)
072: sb.append(' ');
073: sb.append('^');
074: return MessageFormat.format(bundle
075: .getString(warning ? "FMT_WARNING_OUTPUT"
076: : "FMT_ERROR_OUTPUT"), new Object[] {
077: String.valueOf(PPLine.this .getLineNumber()),
078: message, PPLine.this .toString(), sb.toString() }); //NOI18N
079: }
080: }
081:
082: public static final int IF = 0;
083: public static final int ENDIF = 1;
084: public static final int IFDEF = 2;
085: public static final int IFNDEF = 3;
086: public static final int ELSE = 4;
087: public static final int ELIF = 5;
088: public static final int ELIFDEF = 6;
089: public static final int ELIFNDEF = 7;
090: public static final int DEBUG = 8;
091: public static final int MDEBUG = 9;
092: public static final int ENDDEBUG = 10;
093: public static final int DEFINE = 11;
094: public static final int UNDEFINE = 12;
095: public static final int OLDIF = 13;
096: public static final int OLDENDIF = 14;
097: public static final int COMMENTED = 15;
098: public static final int UNCOMMENTED = 16;
099: public static final int CONDITION = 17;
100: public static final int UNKNOWN = -1;
101:
102: protected final ResourceBundle bundle = ResourceBundle
103: .getBundle("org.netbeans.mobility.antext.preprocessor.Bundle"); //NOI18N
104:
105: private final ArrayList<PPToken> tokens;
106: private int type;
107: private ArrayList<Error> errors = null;
108: private boolean hasValue;
109: private boolean hasErrors;
110: private boolean value;
111: private int lineNumber;
112: private PPBlockInfo block;
113:
114: PPLine() {
115: this .tokens = new ArrayList<PPToken>();
116: this .type = UNKNOWN;
117: this .value = false;
118: this .hasValue = false;
119: this .lineNumber = -1;
120: this .hasErrors = false;
121: }
122:
123: public List<PPToken> getTokens() {
124: return Collections.unmodifiableList(tokens);
125: }
126:
127: public int getType() {
128: return type;
129: }
130:
131: public PPBlockInfo getBlock() {
132: return block;
133: }
134:
135: public List<Error> getErrors() {
136: return errors == null ? (List<Error>) Collections.EMPTY_LIST
137: : Collections.unmodifiableList(errors);
138: }
139:
140: public boolean hasErrors() {
141: return hasErrors;
142: }
143:
144: public boolean hasValue() {
145: return hasValue;
146: }
147:
148: public boolean getValue() {
149: return value;
150: }
151:
152: public int getLineNumber() {
153: return lineNumber;
154: }
155:
156: public String toString() {
157: final StringBuffer sb = new StringBuffer();
158: final Iterator<PPToken> it = tokens.iterator();
159: while (it.hasNext()) {
160: final PPToken t = it.next();
161: sb.append(t.getPadding());
162: if (t.getType() != LineParserTokens.END_OF_LINE)
163: sb.append(t.getText());
164: }
165: return sb.toString();
166: }
167:
168: /* package-private methods to setup PPLine during parsing */
169:
170: void addToken(final PPToken token) {
171: if (lineNumber < 0)
172: lineNumber = token.getLine();
173: tokens.add(token);
174: }
175:
176: void addError(final String bundleKey) {
177: addError(bundleKey, tokens.get(0));
178: }
179:
180: void addError(final String bundleKey, final PPToken token) {
181: if (errors == null)
182: errors = new ArrayList<Error>();
183: int i = 0, c = token.getColumn();
184: final Iterator<Error> it = errors.iterator();
185: while (it.hasNext() && it.next().token.getColumn() <= c)
186: i++;
187: errors.add(i, new Error(bundle.getString(bundleKey), token,
188: false));
189: hasErrors = true;
190: }
191:
192: void addWarning(final String bundleKey, final PPToken token) {
193: if (errors == null)
194: errors = new ArrayList<Error>();
195: errors.add(new Error(bundle.getString(bundleKey), token, true));
196: }
197:
198: void setType(final int type) {
199: this .type = type;
200: }
201:
202: void setValue(final boolean value) {
203: this .value = value;
204: this .hasValue = true;
205: }
206:
207: void setBlock(final PPBlockInfo block) {
208: this.block = block;
209: }
210: }
|