KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > SourcePositionImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.util;
13
14 import com.sun.mirror.util.SourcePosition;
15 import java.io.File JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.jdt.apt.core.internal.declaration.AnnotationMirrorImpl;
21 import org.eclipse.jdt.apt.core.internal.declaration.AnnotationValueImpl;
22 import org.eclipse.jdt.apt.core.internal.declaration.EclipseDeclarationImpl;
23 import org.eclipse.jdt.apt.core.internal.declaration.EclipseMirrorObject;
24
25 public class SourcePositionImpl implements SourcePosition
26 {
27     private final int _startingOffset;
28     private final int _length;
29     private final int _line;
30     private final int _column;
31     /** the back pointer to the declaration that created this object */
32     private final EclipseMirrorObject _decl;
33
34     public SourcePositionImpl(final int startingOffset,
35                               final int length,
36                               final int line,
37                               final int column,
38                               final EclipseDeclarationImpl decl)
39     {
40         _startingOffset = startingOffset;
41         _length = length;
42         _line = line < 1 ? 1 : line;
43         _column = column < 0 ? 0 : column;
44         _decl = decl;
45         assert decl != null : "missing declaration [decl] == null."; //$NON-NLS-1$
46
}
47     
48     public SourcePositionImpl(final int startingOffset,
49                               final int length,
50                               final int line,
51                               final int column,
52                               final AnnotationValueImpl decl )
53     {
54         _startingOffset = startingOffset;
55         _length = length;
56         _line = line < 1 ? 1 : line;
57         _column = column < 0 ? 0 : column;
58         _decl = decl;
59         assert decl != null : "missing declaration [decl] == null."; //$NON-NLS-1$
60
}
61     
62     public SourcePositionImpl(final int startingOffset,
63                               final int length,
64                               final int line,
65                               final int column,
66                               final AnnotationMirrorImpl decl )
67     {
68         _startingOffset = startingOffset;
69         _length = length;
70         _line = line < 1 ? 1 : line;
71         _column = column < 0 ? 0 : column;
72         _decl = decl;
73         assert decl != null : "missing declaration [decl] == null."; //$NON-NLS-1$
74
}
75     
76     public int line(){ return _line; }
77     public int column(){ return _column; }
78     public File JavaDoc file(){
79         IResource resource = getResource();
80         if( resource == null ) return null;
81         final IPath absPath = resource.getRawLocation();
82         if(absPath == null) return null;
83         return new File JavaDoc( absPath.toOSString() );
84     }
85
86     // for use in IDE mode for squiggling.
87
public int getStartingOffset(){ return _startingOffset; }
88     public int getEndingOffset(){ return _startingOffset + _length; }
89     public int getLength(){ return _length; }
90     public IFile getResource(){
91         if( _decl instanceof EclipseDeclarationImpl )
92             return ((EclipseDeclarationImpl)_decl).getResource();
93         else if( _decl instanceof AnnotationMirrorImpl )
94             return ((AnnotationMirrorImpl)_decl).getResource();
95         else if( _decl instanceof AnnotationValueImpl )
96             return ((AnnotationValueImpl)_decl).getResource();
97         
98         throw new IllegalStateException JavaDoc();
99     }
100     
101     public String JavaDoc toString()
102     {
103         StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
104         buffer.append("offset = "); //$NON-NLS-1$
105
buffer.append(_startingOffset);
106         buffer.append(" line = "); //$NON-NLS-1$
107
buffer.append( _line );
108         buffer.append(" column = "); //$NON-NLS-1$
109
buffer.append( _column );
110         buffer.append(" length = "); //$NON-NLS-1$
111
buffer.append( _length );
112         
113         return buffer.toString();
114     }
115 }
116
Popular Tags