1 package freemarker.eclipse.editors; 2 3 import java.io.Reader ; 4 import java.io.StringReader ; 5 import java.util.Enumeration ; 6 import java.util.Vector ; 7 8 import org.eclipse.core.resources.IFile; 9 import org.eclipse.core.resources.IMarker; 10 import org.eclipse.core.resources.IResource; 11 import org.eclipse.jface.text.IDocument; 12 import org.eclipse.jface.text.IRegion; 13 import org.eclipse.jface.text.reconciler.DirtyRegion; 14 import org.eclipse.jface.text.reconciler.IReconcilingStrategy; 15 import org.eclipse.swt.widgets.Display; 16 import org.eclipse.ui.IFileEditorInput; 17 18 import freemarker.core.ParseException; 19 import freemarker.core.TemplateElement; 20 import freemarker.template.Configuration; 21 import freemarker.template.Template; 22 23 24 28 public class ReconcilingStrategy implements IReconcilingStrategy { 29 private FreemarkerEditor fEditor; 30 private Template fTemplate; 31 private Template fLastTemplate; 32 private String fError; 33 34 public ReconcilingStrategy(FreemarkerEditor anEditor) { 35 fEditor = anEditor; 36 } 37 38 public void setDocument(IDocument aDocument) { 39 parse(); 40 } 41 42 public void reconcile(DirtyRegion aDirtyRegion, IRegion aRegion) { 43 parse(); 44 } 45 46 public void reconcile(IRegion aPartition) { 47 parse(); 48 } 49 50 private void parse() { 51 IFile file = ((IFileEditorInput)fEditor.getEditorInput()).getFile(); 52 String name = file.getName(); 53 Template template = null; 54 int fErrorLine = 0; 55 String fErrorExpected = ""; 56 try { 57 file.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); 58 Reader reader = new StringReader (fEditor.getDocument().get()); 59 Configuration cfg = new Configuration(); 60 template = new Template(file.getName(), reader, cfg); 61 reader.close(); 62 fError = ""; 63 } catch (ParseException e) { 64 if (e.getMessage() != null) { 65 fError = e.getMessage(); 66 67 68 69 try { 70 fErrorLine = e.getLineNumber(); 71 } catch (NullPointerException npe) { 72 fErrorLine = 0; 73 } 74 75 } else { 76 fError = ""; 77 } 78 } catch (Exception e) { 79 80 } 81 synchronized (this) { 82 if (template != null) { 83 fTemplate = template; 84 85 fLastTemplate = template; 87 88 } else { 89 fTemplate = null; 90 try { 91 fEditor.addProblemMarker(fError,fErrorLine); 92 } catch (Exception e) { 93 94 } 95 } 96 } 97 98 Display.getDefault().syncExec(new Runnable () { 100 public void run(){ 101 fEditor.updateOutlinePage(); 102 fEditor.displayErrorMessage(fError); 103 } 104 }); 105 } 106 107 public Object [] getTemplateRoot() { 108 if(null==fTemplate) { 109 return new Object [0]; 110 } else { 111 if("root element".equals(fTemplate.getRootTreeNode().getDescription())) { 112 return enum2array(fTemplate.getRootTreeNode().children()); 113 } else { 114 return new Object []{fTemplate.getRootTreeNode()}; 115 } 116 } 117 } 118 119 public Template getTemplate() { 120 return fTemplate; 121 } 122 123 private Object [] enum2array(Enumeration e) { 124 Vector temp = new Vector (); 125 TemplateElement t; 126 while(e.hasMoreElements()) { 127 t = (TemplateElement)e.nextElement(); 128 temp.add(t); 130 } 132 return temp.toArray(); 133 } 134 } 135 136 | Popular Tags |