KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > LineMap


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26 import org.aspectj.compiler.base.ast.*;
27
28 import java.util.*;
29 import java.io.*;
30
31 import org.aspectj.tools.ide.SourceLine;
32 import org.aspectj.util.CollectionUtil;
33
34 public class LineMap {
35     private File outputFile;
36     private String JavaDoc outputDirectory;
37     private String JavaDoc outputFileName;
38
39     private int outputLine = 0;
40
41     private ASTObject sourceNode = null;
42     //private int sourceLine = -1;
43
//private CompilationUnit sourceCompilationUnit;
44

45     private Map outputToSource;
46     private Map sourceToOutput;
47
48     public LineMap(File outputFile, Map outputToSource, Map sourceToOutput) throws IOException {
49         this.outputFile = outputFile;
50         outputFileName = outputFile.getCanonicalPath();
51         outputDirectory = new File(outputFileName).getParent();
52         this.outputToSource = outputToSource;
53         this.sourceToOutput = sourceToOutput;
54     }
55
56
57     final String JavaDoc getOutputFile() {
58         return outputFileName;
59     }
60
61     public final boolean hasCurrentSourceLine() {
62         return sourceNode != null;
63     }
64
65     public final int getCurrentSourceLine() {
66         return sourceNode != null ? sourceNode.getBeginLine() : -1;
67     }
68
69     public final String JavaDoc getCurrentSourceFile() {
70         return sourceNode != null ? sourceNode.getSourceFileName() : null;
71     }
72
73     public final void addLineMappings(SourceLine sourceLine, SourceLine outputLine) {
74         Map map = CollectionUtil.getMapInMap(outputToSource, outputDirectory);
75         synchronized (map) {
76             map.put(outputLine, sourceLine);
77         }
78
79         map = CollectionUtil.getMapInMap(sourceToOutput, sourceNode.getSourceDirectoryName()); //sourceLine.getDirectory());
80
synchronized (map) {
81             map.put(sourceLine, outputLine);
82         }
83     }
84
85
86     public final void noteNewLine() {
87         if (sourceNode != null) {
88             addLineMappings(new SourceLine(sourceNode.getSourceFileName(), sourceNode.getBeginLine()),
89                             new SourceLine(getOutputFile(), outputLine));
90         }
91
92         outputLine += 1;
93         sourceNode = null;
94     }
95
96     public final void noteNode(ASTObject node) {
97         //if (true) return;
98

99         // This test shouldn't be needed, but it hides some untidiness
100
// that occurs in the parser (where a few nodes don't get context info)
101
if (node.getStartPosition() != -1) {
102             sourceNode = node;
103             //sourceCompilationUnit = node.getCompilationUnit();
104
//sourceLine = node.getBeginLine();
105
}
106     }
107 }
108
Popular Tags