1 package gnu.lists; 2 import java.io.*; 3 import gnu.text.SourceLocator; 4 5 6 7 public class PairWithPosition extends Pair 8 implements gnu.text.SourceLocator 9 { 10 String filename; 11 13 int position; 14 15 public final void setFile (String filename) 16 { 17 this.filename = filename; 18 } 19 20 public final void setLine (int lineno, int colno) 21 { 22 if (lineno < 0) 23 lineno = 0; 24 if (colno < 0) 25 colno = 0; 26 position = (lineno << 12) + colno; 27 } 28 29 public final void setLine (int lineno) 30 { 31 setLine (lineno, 0); 32 } 33 34 public final String getFileName () 35 { 36 return filename; 37 } 38 39 public String getPublicId () 40 { 41 return null; 42 } 43 44 public String getSystemId () 45 { 46 return filename; 47 } 48 49 51 public final int getLineNumber() 52 { 53 int line = position >> 12; 54 return line == 0 ? -1 : line; 55 } 56 57 public final int getColumnNumber() 58 { 59 int column = position & ((1 << 12) - 1); 60 return column == 0 ? -1 : column; 61 } 62 63 public boolean isStableSourceLocation() { return true; } 64 65 66 public PairWithPosition () 67 { 68 } 69 70 public PairWithPosition (SourceLocator where, 71 Object car, Object cdr) 72 { 73 super (car, cdr); 74 filename = where.getFileName(); 75 setLine(where.getLineNumber(), where.getColumnNumber()); 76 } 77 78 public PairWithPosition (Object car, Object cdr) 79 { 80 super (car, cdr); 81 } 82 83 public static PairWithPosition make(Object car, Object cdr, 84 String filename, int line, int column) 85 { 86 PairWithPosition pair = new PairWithPosition(car, cdr); 87 pair.filename = filename; 88 pair.setLine(line, column); 89 return pair; 90 } 91 92 public static PairWithPosition make(Object car, Object cdr, 93 String filename, int position) 94 { 95 PairWithPosition pair = new PairWithPosition(car, cdr); 96 pair.filename = filename; 97 pair.position = position; 98 return pair; 99 } 100 101 106 public void writeExternal(ObjectOutput out) throws IOException 107 { 108 out.writeObject(car); 109 out.writeObject(cdr); 110 out.writeObject(filename); 111 out.writeInt(position); 112 } 113 114 public void readExternal(ObjectInput in) 115 throws IOException, ClassNotFoundException 116 { 117 car = in.readObject(); 118 cdr = in.readObject(); 119 filename = (String ) in.readObject(); 120 position = in.readInt(); 121 } 122 } 123 | Popular Tags |