001: package spoon.support.reflect.cu;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.util.ArrayList;
006: import java.util.HashSet;
007: import java.util.List;
008: import java.util.Set;
009:
010: import spoon.processing.FactoryAccessor;
011: import spoon.reflect.Factory;
012: import spoon.reflect.cu.CompilationUnit;
013: import spoon.reflect.cu.Import;
014: import spoon.reflect.cu.SourceCodeFragment;
015: import spoon.reflect.declaration.CtSimpleType;
016:
017: public class CompilationUnitImpl implements CompilationUnit,
018: FactoryAccessor {
019:
020: Factory factory;
021:
022: List<CtSimpleType<?>> declaredTypes = new ArrayList<CtSimpleType<?>>();
023:
024: public List<CtSimpleType<?>> getDeclaredTypes() {
025: return declaredTypes;
026: }
027:
028: File file;
029:
030: public File getFile() {
031: return file;
032: }
033:
034: public CtSimpleType<?> getMainType() {
035: if (getFile() == null)
036: return getDeclaredTypes().get(0);
037: for (CtSimpleType<?> t : getDeclaredTypes()) {
038: String name = getFile().getName();
039: name = name.substring(0, name.lastIndexOf("."));
040: if (t.getSimpleName().equals(name))
041: return t;
042: }
043: throw new RuntimeException("inconsistent compilation unit: '"
044: + file + "': declared types are " + getDeclaredTypes());
045: }
046:
047: public void setDeclaredTypes(List<CtSimpleType<?>> types) {
048: this .declaredTypes = types;
049: }
050:
051: public void setFile(File file) {
052: this .file = file;
053: }
054:
055: List<SourceCodeFragment> fragments;
056:
057: public void addSourceCodeFragment(SourceCodeFragment fragment) {
058: if (fragments == null) {
059: fragments = new ArrayList<SourceCodeFragment>();
060: }
061: int i = 0;
062: for (SourceCodeFragment f : fragments) {
063: if (fragment.position <= f.position) {
064: break;
065: }
066: i++;
067: }
068: fragments.add(i, fragment);
069: }
070:
071: public List<SourceCodeFragment> getSourceCodeFraments() {
072: return fragments;
073: }
074:
075: String originalSourceCode;
076:
077: public String getOriginalSourceCode() {
078: try {
079: if (originalSourceCode == null) {
080: FileInputStream s = new FileInputStream(getFile());
081: byte[] elementBytes = new byte[s.available()];
082: s.read(elementBytes);
083: s.close();
084: originalSourceCode = new String(elementBytes);
085: }
086: } catch (Exception e) {
087: throw new RuntimeException(e);
088: }
089: return originalSourceCode;
090: }
091:
092: public int beginOfLineIndex(int index) {
093: int cur = index;
094: while (cur >= 0 && getOriginalSourceCode().charAt(cur) != '\n') {
095: cur--;
096: }
097: return cur + 1;
098: }
099:
100: public int nextLineIndex(int index) {
101: int cur = index;
102: while (cur < getOriginalSourceCode().length()
103: && getOriginalSourceCode().charAt(cur) != '\n') {
104: cur++;
105: }
106: return cur + 1;
107: }
108:
109: public int getTabCount(int index) {
110: int cur = index;
111: int tabCount = 0;
112: int whiteSpaceCount = 0;
113: while (cur < getOriginalSourceCode().length()
114: && (getOriginalSourceCode().charAt(cur) == ' ' || getOriginalSourceCode()
115: .charAt(cur) == '\t')) {
116: if (getOriginalSourceCode().charAt(cur) == '\t')
117: tabCount++;
118: if (getOriginalSourceCode().charAt(cur) == ' ')
119: whiteSpaceCount++;
120: cur++;
121: }
122: tabCount += whiteSpaceCount
123: / getFactory().getEnvironment().getTabulationSize();
124: return tabCount;
125: }
126:
127: public Factory getFactory() {
128: return factory;
129: }
130:
131: public void setFactory(Factory factory) {
132: this .factory = factory;
133: }
134:
135: boolean autoImport = true;
136:
137: Set<Import> manualImports = new HashSet<Import>();
138:
139: public boolean isAutoImport() {
140: return autoImport;
141: }
142:
143: public void setAutoImport(boolean autoImport) {
144: this .autoImport = autoImport;
145: }
146:
147: public Set<Import> getManualImports() {
148: return manualImports;
149: }
150:
151: public void setManualImports(Set<Import> manualImports) {
152: this.manualImports = manualImports;
153: }
154:
155: }
|