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.cnd.modelimpl.csm.core;
043:
044: import java.io.DataOutput;
045: import java.io.IOException;
046: import org.netbeans.modules.cnd.api.model.*;
047: import antlr.collections.AST;
048: import java.io.DataInput;
049: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
050: import org.netbeans.modules.cnd.modelimpl.parser.CsmAST;
051: import org.netbeans.modules.cnd.modelimpl.uid.UIDCsmConverter;
052: import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
053:
054: /**
055: * Base class for CsmOffsetable
056: * @author Vladimir Kvashin
057: */
058: public class OffsetableBase implements CsmOffsetable, Disposable {
059: // only one of fileRef/fileUID must be used (USE_UID_TO_CONTAINER)
060: private/*final*/CsmFile fileRef; // can be set in onDispose or contstructor only
061: private final CsmUID<CsmFile> fileUID;
062:
063: private final int startPosition;
064: private final int endPosition;
065:
066: protected OffsetableBase(AST ast, CsmFile file) {
067: this .fileUID = UIDCsmConverter.fileToUID(file);
068: this .fileRef = null;// to prevent error with "final"
069: //this.ast = ast;
070: CsmAST startAST = getStartAst(ast);
071: startPosition = (startAST == null) ? 0 : startAST.getOffset();
072: CsmAST endAST = getEndAst(ast);
073: endPosition = (endAST == null) ? startPosition : endAST
074: .getEndOffset();
075: }
076:
077: protected OffsetableBase(CsmFile containingFile, CsmOffsetable pos) {
078: this (containingFile, pos != null ? pos.getStartOffset() : 0,
079: pos != null ? pos.getEndOffset() : 0);
080: }
081:
082: public OffsetableBase(CsmFile file, int start, int end) {
083: this .fileUID = UIDCsmConverter.fileToUID(file);
084: this .fileRef = null;// to prevent error with "final"
085: this .startPosition = start;
086: this .endPosition = end;
087: }
088:
089: public int getStartOffset() {
090: return startPosition;
091: }
092:
093: public int getEndOffset() {
094: return endPosition;
095: }
096:
097: public Position getStartPosition() {
098: return new LazyOffsPositionImpl((FileImpl) this
099: .getContainingFile(), startPosition);
100: }
101:
102: public Position getEndPosition() {
103: return new LazyOffsPositionImpl((FileImpl) this
104: .getContainingFile(), endPosition);
105: }
106:
107: private CsmAST getStartAst(AST node) {
108: if (node != null) {
109: CsmAST csmAst = AstUtil.getFirstCsmAST(node);
110: if (csmAst != null) {
111: return csmAst;
112: }
113: }
114: return null;
115: }
116:
117: protected CsmAST getEndAst(AST node) {
118: if (node != null) {
119: AST lastChild = AstUtil.getLastChildRecursively(node);
120: if (lastChild instanceof CsmAST) {
121: return ((CsmAST) lastChild);
122: }
123: }
124: return null;
125: }
126:
127: public CsmFile getContainingFile() {
128: return _getFile();
129: }
130:
131: public CharSequence getText() {
132: return getContainingFile().getText(getStartOffset(),
133: getEndOffset());
134: }
135:
136: public void dispose() {
137: onDispose();
138: }
139:
140: private void onDispose() {
141: if (TraceFlags.RESTORE_CONTAINER_FROM_UID) {
142: // restore container from it's UID
143: this .fileRef = UIDCsmConverter.UIDtoFile(fileUID);
144: assert this .fileRef != null : "no object for UID "
145: + fileUID;
146: }
147: }
148:
149: private CsmFile _getFile() {
150: CsmFile file = this .fileRef;
151: if (file == null) {
152: file = UIDCsmConverter.UIDtoFile(fileUID);
153: assert file != null : "no object for UID " + fileUID;
154: }
155: return file;
156: }
157:
158: protected void write(DataOutput output) throws IOException {
159: output.writeInt(startPosition);
160: output.writeInt(endPosition);
161: // not null UID
162: assert this .fileUID != null;
163: UIDObjectFactory.getDefaultFactory().writeUID(this .fileUID,
164: output);
165: }
166:
167: protected OffsetableBase(DataInput input) throws IOException {
168: startPosition = input.readInt();
169: endPosition = input.readInt();
170:
171: this .fileUID = UIDObjectFactory.getDefaultFactory().readUID(
172: input);
173: // not null UID
174: assert this .fileUID != null;
175: this .fileRef = null;
176: }
177:
178: // test trace method
179: protected String getOffsetString() {
180: return "[" + getStartOffset() + "-" + getEndOffset() + "]"; // NOI18N
181: }
182: }
|