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;
043:
044: import java.io.DataInput;
045: import java.io.DataOutput;
046: import java.io.IOException;
047: import org.netbeans.modules.cnd.api.model.*;
048: import org.netbeans.modules.cnd.modelimpl.csm.core.OffsetableIdentifiableBase;
049: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectImpl;
050: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
051: import org.netbeans.modules.cnd.modelimpl.textcache.FileNameCache;
052: import org.netbeans.modules.cnd.modelimpl.uid.UIDCsmConverter;
053: import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
054: import org.netbeans.modules.cnd.modelimpl.uid.UIDUtilities;
055: import org.netbeans.modules.cnd.utils.cache.CharSequenceKey;
056:
057: /**
058: * Implements CsmInclude
059: * @author Vladimir Kvasihn,
060: * Vladimir Voskresensky
061: */
062: public class IncludeImpl extends OffsetableIdentifiableBase<CsmInclude>
063: implements CsmInclude {
064: private final CharSequence name;
065: private final boolean system;
066:
067: private CsmUID<CsmFile> includeFileUID;
068:
069: public IncludeImpl(String name, boolean system,
070: CsmFile includeFile, CsmFile containingFile,
071: CsmOffsetable inclPos) {
072: super (containingFile, inclPos);
073: this .name = FileNameCache.getManager().getString(name);
074: this .system = system;
075: this .includeFileUID = UIDCsmConverter.fileToUID(includeFile);
076: assert (includeFileUID != null || includeFile == null);
077: }
078:
079: public CsmFile getIncludeFile() {
080: return _getIncludeFile();
081: }
082:
083: public CharSequence getIncludeName() {
084: return name;
085: }
086:
087: public boolean isSystem() {
088: return system;
089: }
090:
091: @Override
092: public String toString() {
093: char beg = isSystem() ? '<' : '"';
094: char end = isSystem() ? '>' : '"';
095: String error = "";
096: if (getContainingFile() == null) {
097: error = "<NO CONTAINER INFO> "; // NOI18N
098: }
099: return error
100: + beg
101: + getIncludeName()
102: + end
103: + (getIncludeFile() == null ? " <FAILED inclusion>"
104: : "")
105: + // NOI18N
106: " [" + getStartPosition() + "-" + getEndPosition()
107: + "]"; // NOI18N
108: }
109:
110: @Override
111: public boolean equals(Object obj) {
112: boolean retValue;
113: if (obj == null || !(obj instanceof IncludeImpl)) {
114: retValue = false;
115: } else {
116: IncludeImpl other = (IncludeImpl) obj;
117: retValue = IncludeImpl.equals(this , other);
118: }
119: return retValue;
120: }
121:
122: private static final boolean equals(IncludeImpl one,
123: IncludeImpl other) {
124: // compare only name, type and start offset
125: return (CharSequenceKey.Comparator.compare(
126: one.getIncludeName(), other.getIncludeName()) == 0)
127: && (one.system == other.system)
128: && (one.getStartOffset() == other.getStartOffset());
129: }
130:
131: @Override
132: public int hashCode() {
133: int retValue = 17 * (isSystem() ? 1 : -1);
134: retValue = 31 * retValue + getStartOffset();
135: retValue = 31 * retValue + getIncludeName().hashCode();
136: return retValue;
137: }
138:
139: private CsmFile _getIncludeFile() {
140: CsmFile file = UIDCsmConverter.UIDtoFile(includeFileUID);
141: if (file == null && includeFileUID != null) {
142: // include file was removed
143: includeFileUID = null;
144: }
145: if (TraceFlags.NEED_TO_TRACE_UNRESOLVED_INCLUDE) {
146: if (file == null
147: && "yes"
148: .equals(System
149: .getProperty("cnd.modelimpl.trace.trace_now"))) { //NOI18N
150: CsmFile container = getContainingFile();
151: if (container != null) {
152: CsmProject prj = container.getProject();
153: if (prj instanceof ProjectImpl) {
154: System.out.println("File "
155: + container.getAbsolutePath()); // NOI18N
156: ProjectImpl impl = (ProjectImpl) prj;
157: boolean find = false;
158: for (CsmFile top : impl.getGraph()
159: .getTopParentFiles(container)) {
160: if (container != top) {
161: System.out.println(" icluded from "
162: + top.getAbsolutePath()); //NOI18N
163: find = true;
164: }
165: }
166: if (!find) {
167: System.out
168: .println(" there are no files included the file"); //NOI18N
169: }
170: }
171: }
172: }
173: }
174: return file;
175: }
176:
177: protected CsmUID createUID() {
178: return UIDUtilities.createIncludeUID(this );
179: }
180:
181: @Override
182: public void write(DataOutput output) throws IOException {
183: super .write(output);
184: assert this .name != null;
185: output.writeUTF(this .name.toString());
186: output.writeBoolean(this .system);
187: UIDObjectFactory.getDefaultFactory().writeUID(
188: this .includeFileUID, output);
189: }
190:
191: public IncludeImpl(DataInput input) throws IOException {
192: super(input);
193: this.name = FileNameCache.getManager().getString(
194: input.readUTF());
195: assert this.name != null;
196: this.system = input.readBoolean();
197: this.includeFileUID = UIDObjectFactory.getDefaultFactory()
198: .readUID(input);
199: }
200: }
|