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.parser.apt;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.IOException;
047: import java.util.logging.Level;
048: import org.netbeans.modules.cnd.apt.structure.APTFile;
049: import org.netbeans.modules.cnd.apt.structure.APTInclude;
050: import org.netbeans.modules.cnd.apt.support.APTAbstractWalker;
051: import org.netbeans.modules.cnd.apt.support.APTPreprocHandler;
052: import org.netbeans.modules.cnd.apt.support.ResolvedPath;
053: import org.netbeans.modules.cnd.apt.utils.APTUtils;
054: import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl;
055: import org.netbeans.modules.cnd.modelimpl.csm.core.LibraryManager;
056: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectBase;
057: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
058: import org.openide.filesystems.FileUtil;
059:
060: /**
061: * base walker to visit project files based APTs
062: * @author Vladimir Voskresensky
063: */
064: public abstract class APTProjectFileBasedWalker extends
065: APTAbstractWalker {
066: private final FileImpl file;
067: private final ProjectBase startProject;
068: private int mode;
069:
070: public APTProjectFileBasedWalker(ProjectBase startProject,
071: APTFile apt, FileImpl file, APTPreprocHandler preprocHandler) {
072: super (apt, preprocHandler);
073: this .mode = ProjectBase.GATHERING_MACROS;
074: this .file = file;
075: this .startProject = startProject;
076: assert startProject != null : "null start project for "
077: + file.getAbsolutePath();
078: }
079:
080: ////////////////////////////////////////////////////////////////////////////
081: // impl of abstract methods
082:
083: protected void include(ResolvedPath resolvedPath, APTInclude apt) {
084: FileImpl included = null;
085: if (resolvedPath != null) {
086: String path = resolvedPath.getPath();
087: if (path.indexOf("..") > 0) { // NOI18N
088: path = FileUtil.normalizeFile(new File(path))
089: .getAbsolutePath();
090: resolvedPath = new ResolvedPath(resolvedPath
091: .getFolder(), path, resolvedPath
092: .isDefaultSearchPath(), resolvedPath.getIndex());
093: }
094: if (getIncludeHandler().pushInclude(path,
095: apt.getToken().getLine(), resolvedPath.getIndex())) {
096: ProjectBase startProject = this .getStartProject();
097: if (startProject != null) {
098: ProjectBase inclFileOwner = LibraryManager
099: .getInstance().resolveFileProjectOnInclude(
100: startProject, getFile(),
101: resolvedPath);
102: try {
103: included = includeAction(inclFileOwner, path,
104: mode, apt);
105: } catch (FileNotFoundException ex) {
106: APTUtils.LOG
107: .log(
108: Level.WARNING,
109: "APTProjectFileBasedWalker: file {0} not found",
110: new Object[] { path });// NOI18N
111: DiagnosticExceptoins.register(ex);
112: } catch (IOException ex) {
113: APTUtils.LOG
114: .log(
115: Level.SEVERE,
116: "APTProjectFileBasedWalker: error on including {0}:\n{1}",
117: new Object[] { path, ex });
118: DiagnosticExceptoins.register(ex);
119: }
120: } else {
121: APTUtils.LOG
122: .log(
123: Level.SEVERE,
124: "APTProjectFileBasedWalker: file {0} without project!!!",
125: new Object[] { file });// NOI18N
126: getIncludeHandler().popInclude();
127: }
128: }
129: }
130: postInclude(apt, included);
131: }
132:
133: abstract protected FileImpl includeAction(
134: ProjectBase inclFileOwner, String inclPath, int mode,
135: APTInclude apt) throws IOException;
136:
137: protected void postInclude(APTInclude apt, FileImpl included) {
138: }
139:
140: protected FileImpl getFile() {
141: return this .file;
142: }
143:
144: protected ProjectBase getStartProject() {
145: return this .file.getProjectImpl();
146: }
147:
148: protected void setMode(int mode) {
149: this.mode = mode;
150: }
151:
152: }
|