1 19 20 package org.netbeans.test.editor.indentation.programmatic; 21 22 import java.io.Writer ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 30 import javax.swing.JEditorPane ; 31 import javax.swing.text.Document ; 32 import javax.swing.text.StyledDocument ; 33 import javax.swing.text.EditorKit ; 34 35 import org.openide.text.CloneableEditorSupport; 36 import org.openide.text.IndentEngine; 37 import java.util.Map ; 38 import java.lang.reflect.Method ; 39 import java.beans.BeanInfo ; 40 import java.beans.Introspector ; 41 import java.beans.PropertyDescriptor ; 42 import java.util.HashMap ; 43 import java.beans.PropertyEditor ; 44 import java.beans.PropertyEditorManager ; 45 47 91 public class IndentCore extends java.lang.Object { 92 93 94 public IndentCore() { 95 } 96 97 private static void setProperties(IndentEngine engine, Map indentationProperties) throws Exception { 98 System.err.println(engine.getClass()); 99 100 BeanInfo info = Introspector.getBeanInfo(engine.getClass()); 101 PropertyDescriptor [] descriptors = info.getPropertyDescriptors(); 102 103 System.err.println(descriptors.length); 104 105 for (int cntr = 0; cntr < descriptors.length; cntr++) { 106 System.err.println("testing property=" + descriptors[cntr].getName()); 107 108 Object value = indentationProperties.get(descriptors[cntr].getName()); 109 110 if (value == null) 111 continue; 112 113 if (value.getClass() == String .class && descriptors[cntr].getPropertyType() != String .class) { 114 PropertyEditor editor = PropertyEditorManager.findEditor(descriptors[cntr].getPropertyType()); 115 116 editor.setAsText((String )value); 117 value = editor.getValue(); 118 } 119 120 System.err.println("setting property=" + descriptors[cntr].getName()); 121 Method write = descriptors[cntr].getWriteMethod(); 122 123 write.invoke(engine, new Object [] {value}); 124 } 125 126 } 128 129 134 private static Writer findIndentWriter(Document doc, int offset, Writer writer, Map indentationProperties) throws Exception { 135 IndentEngine engine = IndentEngine.find(doc); 138 setProperties(engine, indentationProperties); 139 140 Writer indentWriter = engine.createWriter(doc, offset, writer); 141 142 return indentWriter; 143 } 144 145 private static EditorKit createEditorKit(String mimeType) { 146 return CloneableEditorSupport.getEditorKit(mimeType); 147 } 148 149 private static StyledDocument createDocument(EditorKit kit) { 150 Document doc = kit.createDefaultDocument(); 151 152 if (doc instanceof StyledDocument ) { 153 return (StyledDocument )doc; 154 } else { 155 return new org.openide.text.FilterDocument(doc); 156 } 157 } 158 159 private static String formatText(StyledDocument doc, int pos, String text, Map indentationProperties) throws Exception { 160 StringWriter stringWriter = new StringWriter (); 161 Writer indentWriter = findIndentWriter(doc, pos, stringWriter, indentationProperties); 162 indentWriter.write(text); 163 indentWriter.close(); 164 165 return stringWriter.toString(); 166 } 167 168 173 public static String indent(String what, String mimeType, Map indentationProperties) throws Exception { 174 Map oldProperties = saveIndentEngineProperties(mimeType); 175 EditorKit kit = createEditorKit(mimeType); 176 StyledDocument doc = createDocument(kit); 177 178 String result = formatText(doc, 0, what, indentationProperties); 179 180 loadIndentEngineProperties(mimeType, oldProperties); 181 return result; 182 } 183 184 private static Map saveIndentEngineProperties(String mimeType) throws Exception { 185 IndentEngine engine = IndentEngine.find(createDocument(createEditorKit(mimeType))); 186 187 if (engine == null) 188 throw new NullPointerException ("No indent engine for MIMEType=" + mimeType + " found."); 189 190 BeanInfo info = Introspector.getBeanInfo(engine.getClass()); 191 PropertyDescriptor [] descriptor = info.getPropertyDescriptors(); 192 Map result = new HashMap (); 193 194 for (int cntr = 0; cntr < descriptor.length; cntr++) { 195 Method read = descriptor[cntr].getReadMethod(); 196 Object value = read.invoke(engine, new Object [] {}); 197 198 result.put(descriptor[cntr].getName(), value); 200 } 201 202 return result; 203 } 204 205 private static void loadIndentEngineProperties(String mimeType, Map properties) throws Exception { 206 IndentEngine engine = IndentEngine.find(createDocument(createEditorKit(mimeType))); 207 208 if (engine == null) 209 throw new NullPointerException ("No indent engine for MIMEType=" + mimeType + " found."); 210 211 setProperties(engine, properties); 212 } 213 214 219 public static void main(String [] args) throws Exception { 220 IndentCore eng = new IndentCore(); 221 222 Map indentationProperties = new HashMap (); 223 indentationProperties.put("expandTabs", "true"); 224 indentationProperties.put("spacesPerTab", "11"); 225 indentationProperties.put("javaFormatSpaceBeforeParenthesis", "true"); 226 indentationProperties.put("javaFormatLeadingStarInComment", "true"); 227 indentationProperties.put("javaFormatNewlineBeforeBrace", "true"); 228 229 eng.run(new PrintWriter (System.err), 230 new PrintWriter (System.out), 231 "data/testfiles/wholeMethod.txt", 232 "text/x-java", 233 indentationProperties); 234 System.err.flush(); 235 System.out.flush(); 236 } 237 238 247 public void run(PrintWriter log, 248 PrintWriter ref, 249 String testFileName, 250 String testFileMIMEType, 251 Map indentationProperties) throws Exception { 252 try { 253 StringBuffer result = new StringBuffer (); 254 InputStream in = null; 255 256 try { 257 int read; 258 259 in = this.getClass().getResourceAsStream(testFileName); 260 261 while ((read = in.read()) != (-1)) { 262 result.append((char)read); 263 } 264 } finally { 265 if (in != null) { 266 try { 267 in.close(); 268 } catch (IOException e) { 269 } 270 } 271 } 272 ref.print(indent(result.toString(), testFileMIMEType, indentationProperties)); 273 } catch (Exception e) { 274 e.printStackTrace(log); 275 throw e; 276 } 277 } 278 279 } 280 | Popular Tags |