1 16 17 package de.gulden.util.javasource; 18 19 import de.gulden.util.javasource.jjt.Node; 20 import de.gulden.util.javasource.jjt.*; 21 import de.gulden.util.xml.XMLToolbox; 22 import de.gulden.util.Toolbox; 23 import javax.xml.parsers.*; 24 import org.w3c.dom.*; 25 import java.io.*; 26 import java.util.*; 27 28 34 public class Code extends Implementation { 35 36 40 43 protected String raw; 44 45 46 50 53 public Code() { 54 super(); 55 } 56 57 58 62 65 public String getRaw() { 66 return raw; 67 } 68 69 72 public void setRaw(String s) { 73 raw=s; 74 } 75 76 82 public Element buildXML(Document d) { 83 Element e=d.createElement("code"); 84 String raw=getRaw(); 85 e.appendChild(d.createTextNode(raw)); 86 return e; 87 } 88 89 95 public void initFromXML(Element element) throws IOException { 96 raw=XMLToolbox.getText(element); 97 if (raw==null) { 98 raw=""; 99 } 100 } 101 102 107 void initFromAST(Node rootnode) { 108 StringBuffer sb=new StringBuffer (); 113 Token t=rootnode.getStartToken(); 115 while ((t.kind!=ParserConstants.LBRACE)&&(t.kind!=ParserConstants.ASSIGN)) { 117 t=t.next; 118 } 119 Token endT; 120 if (t.kind==ParserConstants.LBRACE) { 122 endT=findCodeEnd(t); 123 } 124 else { 126 endT=rootnode.getEndToken(); 127 } 128 raw=rootnode.getTextImage().getRange(t.beginLine,t.beginColumn,endT.endLine,endT.endColumn); 129 raw=raw.trim(); 130 raw=raw.substring(1,raw.length()-1); int npos = raw.indexOf('\n'); 133 if (npos != -1) { 134 String left = raw.substring(0, npos); 135 if (left.trim().length()==0) { 136 raw = raw.substring(npos+1); 137 } 138 } 139 if (raw.indexOf("...fields")!=-1) { 140 System.out.println("AAA"); 141 } 142 raw = unindent(raw); 143 raw = SourceParser.workaroundRestoreUnicodeSingleChar(raw); } 145 146 147 151 static Token findCodeEnd(Token t) { 152 int cnt=1; 153 while (cnt>0) { 154 t=t.next; 155 switch (t.kind) { 156 case ParserConstants.LBRACE: cnt++; 157 break; 158 case ParserConstants.RBRACE: cnt--; 159 break; 160 } 161 } 162 return t; 163 } 164 165 static Token findCodeEndInitializer(Token t) { 166 while ((t.kind!=ParserConstants.SEMICOLON)&&(t.kind!=ParserConstants.COMMA)) { 167 t=t.next; 168 if (t.kind==ParserConstants.LBRACE) { 169 t=findCodeEnd(t).next; 170 } 171 } 172 return t; 173 } 174 175 182 static String unindent(String code) { 183 List l = Toolbox.getLines(code); 184 StringBuffer sb = new StringBuffer (); 185 String nl = System.getProperty("line.separator"); 186 int shift = -1; 187 for (ListIterator it = l.listIterator(); it.hasNext(); ) { 188 String line = (String )it.next(); 189 if (line.trim().length()==0) { if ( it.hasNext() ) { sb.append( nl ); 192 } 193 } else { 194 String newLine; 195 if (shift == -1) { newLine = Toolbox.trimLeft(line); 197 shift = line.length() - newLine.length(); 198 } else { 199 if (line.length() > shift) { 200 String cutoff = line.substring(0, shift); 201 if (cutoff.trim().length()==0) { newLine = line.substring(shift); 203 } else { 204 newLine = Toolbox.trimLeft(line); } 206 } else { 207 newLine = Toolbox.trimLeft(line); } 209 } 210 sb.append(Toolbox.trimRight(newLine)); 211 if (it.hasNext() ) { 217 sb.append(nl); 218 } 219 } 220 } 221 String s = sb.toString(); 222 if (s.endsWith(nl)) { s = s.substring(0, s.length()-nl.length()); 224 } 225 return s; 226 } 227 228 private static boolean dummy(Object o) { 229 return true; 231 } 232 233 } | Popular Tags |