1 11 package org.eclipse.jdt.internal.corext.template.java; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.OutputStream ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.transform.OutputKeys ; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.dom.DOMSource ; 31 import javax.xml.transform.stream.StreamResult ; 32 33 import org.eclipse.core.runtime.CoreException; 34 import org.eclipse.core.runtime.IStatus; 35 import org.eclipse.core.runtime.Status; 36 37 import org.eclipse.jface.text.templates.ContextTypeRegistry; 38 import org.eclipse.jface.text.templates.Template; 39 import org.eclipse.jface.text.templates.TemplateContextType; 40 import org.eclipse.jface.text.templates.TemplateException; 41 42 import org.w3c.dom.Attr ; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.NamedNodeMap ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 import org.w3c.dom.Text ; 48 import org.xml.sax.InputSource ; 49 import org.xml.sax.SAXException ; 50 51 58 public class TemplateSet { 59 60 private static final String NAME_ATTRIBUTE= "name"; private static final String DESCRIPTION_ATTRIBUTE= "description"; private static final String CONTEXT_ATTRIBUTE= "context"; 64 private List fTemplates= new ArrayList (); 65 private String fTemplateTag; 66 67 private static final int TEMPLATE_PARSE_EXCEPTION= 10002; 68 private static final int TEMPLATE_IO_EXCEPTION= 10005; 69 private ContextTypeRegistry fRegistry; 70 71 public TemplateSet(String templateTag, ContextTypeRegistry registry) { 72 fTemplateTag= templateTag; 73 fRegistry= registry; 74 } 75 76 84 public void addFromFile(File file, boolean allowDuplicates) throws CoreException { 85 InputStream stream= null; 86 87 try { 88 stream= new FileInputStream (file); 89 addFromStream(stream, allowDuplicates); 90 91 } catch (IOException e) { 92 throwReadException(e); 93 94 } finally { 95 try { 96 if (stream != null) 97 stream.close(); 98 } catch (IOException e) { 99 } 101 } 102 } 103 104 public String getTemplateTag() { 105 return fTemplateTag; 106 } 107 108 109 116 public void addFromStream(InputStream stream, boolean allowDuplicates) throws CoreException { 117 try { 118 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 119 DocumentBuilder parser= factory.newDocumentBuilder(); 120 Document document= parser.parse(new InputSource (stream)); 121 122 NodeList elements= document.getElementsByTagName(getTemplateTag()); 123 124 int count= elements.getLength(); 125 for (int i= 0; i != count; i++) { 126 Node node= elements.item(i); 127 NamedNodeMap attributes= node.getAttributes(); 128 129 if (attributes == null) 130 continue; 131 132 String name= getAttributeValue(attributes, NAME_ATTRIBUTE); 133 String description= getAttributeValue(attributes, DESCRIPTION_ATTRIBUTE); 134 if (name == null || description == null) 135 continue; 136 137 String context= getAttributeValue(attributes, CONTEXT_ATTRIBUTE); 138 139 if (name == null || description == null || context == null) 140 throw new SAXException (JavaTemplateMessages.TemplateSet_error_missing_attribute); 141 142 StringBuffer buffer= new StringBuffer (); 143 NodeList children= node.getChildNodes(); 144 for (int j= 0; j != children.getLength(); j++) { 145 String value= children.item(j).getNodeValue(); 146 if (value != null) 147 buffer.append(value); 148 } 149 String pattern= buffer.toString().trim(); 150 151 Template template= new Template(name, description, context, pattern); 152 153 String message= validateTemplate(template); 154 if (message == null) { 155 if (!allowDuplicates) { 156 Template[] templates= getTemplates(name); 157 for (int k= 0; k < templates.length; k++) { 158 remove(templates[k]); 159 } 160 } 161 add(template); 162 } else { 163 throwReadException(null); 164 } 165 } 166 } catch (ParserConfigurationException e) { 167 throwReadException(e); 168 } catch (IOException e) { 169 throwReadException(e); 170 } catch (SAXException e) { 171 throwReadException(e); 172 } 173 } 174 175 protected String validateTemplate(Template template) { 176 TemplateContextType type= fRegistry.getContextType(template.getContextTypeId()); 177 if (type == null) { 178 return "Unknown context type: " + template.getContextTypeId(); } 180 try { 181 type.validate(template.getPattern()); 182 return null; 183 } catch (TemplateException e) { 184 return e.getMessage(); 185 } 186 } 187 188 private String getAttributeValue(NamedNodeMap attributes, String name) { 189 Node node= attributes.getNamedItem(name); 190 191 return node == null 192 ? null 193 : node.getNodeValue(); 194 } 195 196 203 public void saveToFile(File file) throws CoreException { 204 OutputStream stream= null; 205 206 try { 207 stream= new FileOutputStream (file); 208 saveToStream(stream); 209 210 } catch (IOException e) { 211 throwWriteException(e); 212 213 } finally { 214 try { 215 if (stream != null) 216 stream.close(); 217 } catch (IOException e) { 218 } 220 } 221 } 222 223 229 public void saveToStream(OutputStream stream) throws CoreException { 230 try { 231 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 232 DocumentBuilder builder= factory.newDocumentBuilder(); 233 Document document= builder.newDocument(); 234 235 Node root= document.createElement("templates"); document.appendChild(root); 237 238 for (int i= 0; i != fTemplates.size(); i++) { 239 Template template= (Template) fTemplates.get(i); 240 241 Node node= document.createElement(getTemplateTag()); 242 root.appendChild(node); 243 244 NamedNodeMap attributes= node.getAttributes(); 245 246 Attr name= document.createAttribute(NAME_ATTRIBUTE); 247 name.setValue(template.getName()); 248 attributes.setNamedItem(name); 249 250 Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE); 251 description.setValue(template.getDescription()); 252 attributes.setNamedItem(description); 253 254 Attr context= document.createAttribute(CONTEXT_ATTRIBUTE); 255 context.setValue(template.getContextTypeId()); 256 attributes.setNamedItem(context); 257 258 Text pattern= document.createTextNode(template.getPattern()); 259 node.appendChild(pattern); 260 } 261 262 263 Transformer transformer=TransformerFactory.newInstance().newTransformer(); 264 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); DOMSource source = new DOMSource (document); 267 StreamResult result = new StreamResult (stream); 268 269 transformer.transform(source, result); 270 271 } catch (ParserConfigurationException e) { 272 throwWriteException(e); 273 } catch (TransformerException e) { 274 throwWriteException(e); 275 } 276 } 277 278 private static void throwReadException(Throwable t) throws CoreException { 279 int code; 280 if (t instanceof SAXException ) 281 code= TEMPLATE_PARSE_EXCEPTION; 282 else 283 code= TEMPLATE_IO_EXCEPTION; 284 throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.jface.text", code, JavaTemplateMessages.TemplateSet_error_read, t)); } 288 289 private static void throwWriteException(Throwable t) throws CoreException { 290 throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.jface.text", TEMPLATE_IO_EXCEPTION, JavaTemplateMessages.TemplateSet_error_write, t)); } 295 296 301 public void add(Template template) { 302 if (exists(template)) 303 return; 305 fTemplates.add(template); 306 } 307 308 private boolean exists(Template template) { 309 for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) { 310 Template anotherTemplate = (Template) iterator.next(); 311 312 if (template.equals(anotherTemplate)) 313 return true; 314 } 315 316 return false; 317 } 318 319 324 public void remove(Template template) { 325 fTemplates.remove(template); 326 } 327 328 331 public void clear() { 332 fTemplates.clear(); 333 } 334 335 340 public Template[] getTemplates() { 341 return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]); 342 } 343 344 350 public Template[] getTemplates(String name) { 351 ArrayList res= new ArrayList (); 352 for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) { 353 Template curr= (Template) iterator.next(); 354 if (curr.getName().equals(name)) { 355 res.add(curr); 356 } 357 } 358 return (Template[]) res.toArray(new Template[res.size()]); 359 } 360 361 367 public Template getFirstTemplate(String name) { 368 for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) { 369 Template curr= (Template) iterator.next(); 370 if (curr.getName().equals(name)) { 371 return curr; 372 } 373 } 374 return null; 375 } 376 377 } 378 379 | Popular Tags |