X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/php/TemplateSet.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/php/TemplateSet.java index 3476386..a0d83fa 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/php/TemplateSet.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/php/TemplateSet.java @@ -57,22 +57,27 @@ import org.xml.sax.SAXException; */ public class TemplateSet { - private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$ - private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$ - private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$ + private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$ + + private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$ + + private static final String CONTEXT_ATTRIBUTE = "context"; //$NON-NLS-1$ + + private List fTemplates = new ArrayList(); - private List fTemplates= new ArrayList(); private String fTemplateTag; - - private static final int TEMPLATE_PARSE_EXCEPTION= 10002; - private static final int TEMPLATE_IO_EXCEPTION= 10005; + + private static final int TEMPLATE_PARSE_EXCEPTION = 10002; + + private static final int TEMPLATE_IO_EXCEPTION = 10005; + private ContextTypeRegistry fRegistry; - + public TemplateSet(String templateTag, ContextTypeRegistry registry) { - fTemplateTag= templateTag; - fRegistry= registry; + fTemplateTag = templateTag; + fRegistry = registry; } - + /** * Convenience method for reading templates from a file. * @@ -82,11 +87,12 @@ public class TemplateSet { * @see #addFromStream(InputStream, boolean, boolean, ResourceBundle) * @throws CoreException */ - public void addFromFile(File file, boolean allowDuplicates, ResourceBundle bundle) throws CoreException { - InputStream stream= null; + public void addFromFile(File file, boolean allowDuplicates, + ResourceBundle bundle) throws CoreException { + InputStream stream = null; try { - stream= new FileInputStream(file); + stream = new FileInputStream(file); addFromStream(stream, allowDuplicates, false, bundle); } catch (IOException e) { @@ -96,14 +102,14 @@ public class TemplateSet { try { if (stream != null) stream.close(); - } catch (IOException e) {} - } + } catch (IOException e) { + } + } } - + public String getTemplateTag() { return fTemplateTag; } - /** * Reads templates from a XML stream and adds them to the templates @@ -114,59 +120,65 @@ public class TemplateSet { * @param doTranslations * @see #addFromStream(InputStream, boolean, boolean, ResourceBundle) * @throws CoreException - */ - public void addFromStream(InputStream stream, boolean allowDuplicates, boolean doTranslations, ResourceBundle bundle) throws CoreException { + */ + public void addFromStream(InputStream stream, boolean allowDuplicates, + boolean doTranslations, ResourceBundle bundle) throws CoreException { try { - DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); - DocumentBuilder parser= factory.newDocumentBuilder(); - Document document= parser.parse(new InputSource(stream)); - - NodeList elements= document.getElementsByTagName(getTemplateTag()); - - int count= elements.getLength(); - for (int i= 0; i != count; i++) { - Node node= elements.item(i); - NamedNodeMap attributes= node.getAttributes(); + DocumentBuilderFactory factory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder parser = factory.newDocumentBuilder(); + Document document = parser.parse(new InputSource(stream)); + + NodeList elements = document.getElementsByTagName(getTemplateTag()); + + int count = elements.getLength(); + for (int i = 0; i != count; i++) { + Node node = elements.item(i); + NamedNodeMap attributes = node.getAttributes(); if (attributes == null) continue; - String name= getAttributeValue(attributes, NAME_ATTRIBUTE); - String description= getAttributeValue(attributes, DESCRIPTION_ATTRIBUTE); + String name = getAttributeValue(attributes, NAME_ATTRIBUTE); + String description = getAttributeValue(attributes, + DESCRIPTION_ATTRIBUTE); if (name == null || description == null) continue; - + if (doTranslations) { - description= translateString(description, bundle); - } - String context= getAttributeValue(attributes, CONTEXT_ATTRIBUTE); + description = translateString(description, bundle); + } + String context = getAttributeValue(attributes, + CONTEXT_ATTRIBUTE); if (name == null || description == null || context == null) - throw new SAXException(JavaTemplateMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$ + throw new SAXException(JavaTemplateMessages + .getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$ - StringBuffer buffer= new StringBuffer(); - NodeList children= node.getChildNodes(); - for (int j= 0; j != children.getLength(); j++) { - String value= children.item(j).getNodeValue(); + StringBuffer buffer = new StringBuffer(); + NodeList children = node.getChildNodes(); + for (int j = 0; j != children.getLength(); j++) { + String value = children.item(j).getNodeValue(); if (value != null) buffer.append(value); } - String pattern= buffer.toString().trim(); + String pattern = buffer.toString().trim(); if (doTranslations) { - pattern= translateString(pattern, bundle); - } + pattern = translateString(pattern, bundle); + } + + Template template = new Template(name, description, context, + pattern); - Template template= new Template(name, description, context, pattern); - - String message= validateTemplate(template); + String message = validateTemplate(template); if (message == null) { if (!allowDuplicates) { - Template[] templates= getTemplates(name); - for (int k= 0; k < templates.length; k++) { + Template[] templates = getTemplates(name); + for (int k = 0; k < templates.length; k++) { remove(templates[k]); } } - add(template); + add(template); } else { throwReadException(null); } @@ -179,27 +191,28 @@ public class TemplateSet { throwReadException(e); } } - + private String translateString(String str, ResourceBundle bundle) { - int idx= str.indexOf('%'); + int idx = str.indexOf('%'); if (idx == -1) { return str; } - StringBuffer buf= new StringBuffer(); - int k= 0; + StringBuffer buf = new StringBuffer(); + int k = 0; while (idx != -1) { buf.append(str.substring(k, idx)); - for (k= idx + 1; k < str.length() && !Character.isWhitespace(str.charAt(k)); k++) { + for (k = idx + 1; k < str.length() + && !Character.isWhitespace(str.charAt(k)); k++) { // loop } - String key= str.substring(idx + 1, k); + String key = str.substring(idx + 1, k); buf.append(getBundleString(key, bundle)); - idx= str.indexOf('%', k); + idx = str.indexOf('%', k); } buf.append(str.substring(k)); return buf.toString(); } - + private String getBundleString(String key, ResourceBundle bundle) { if (bundle != null) { try { @@ -212,7 +225,8 @@ public class TemplateSet { } protected String validateTemplate(Template template) { - TemplateContextType type= fRegistry.getContextType(template.getContextTypeId()); + TemplateContextType type = fRegistry.getContextType(template + .getContextTypeId()); if (type == null) { return "Unknown context type: " + template.getContextTypeId(); //$NON-NLS-1$ } @@ -223,13 +237,11 @@ public class TemplateSet { return e.getMessage(); } } - + private String getAttributeValue(NamedNodeMap attributes, String name) { - Node node= attributes.getNamedItem(name); + Node node = attributes.getNamedItem(name); - return node == null - ? null - : node.getNodeValue(); + return node == null ? null : node.getNodeValue(); } /** @@ -238,10 +250,10 @@ public class TemplateSet { * @see #saveToStream(OutputStream) */ public void saveToFile(File file) throws CoreException { - OutputStream stream= null; + OutputStream stream = null; try { - stream= new FileOutputStream(file); + stream = new FileOutputStream(file); saveToStream(stream); } catch (IOException e) { @@ -251,48 +263,51 @@ public class TemplateSet { try { if (stream != null) stream.close(); - } catch (IOException e) {} + } catch (IOException e) { + } } } - + /** * Saves the template set as XML. */ public void saveToStream(OutputStream stream) throws CoreException { try { - DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); - DocumentBuilder builder= factory.newDocumentBuilder(); - Document document= builder.newDocument(); + DocumentBuilderFactory factory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.newDocument(); - Node root= document.createElement("templates"); //$NON-NLS-1$ + Node root = document.createElement("templates"); //$NON-NLS-1$ document.appendChild(root); - - for (int i= 0; i != fTemplates.size(); i++) { - Template template= (Template) fTemplates.get(i); - - Node node= document.createElement(getTemplateTag()); + + for (int i = 0; i != fTemplates.size(); i++) { + Template template = (Template) fTemplates.get(i); + + Node node = document.createElement(getTemplateTag()); root.appendChild(node); - - NamedNodeMap attributes= node.getAttributes(); - - Attr name= document.createAttribute(NAME_ATTRIBUTE); + + NamedNodeMap attributes = node.getAttributes(); + + Attr name = document.createAttribute(NAME_ATTRIBUTE); name.setValue(template.getName()); attributes.setNamedItem(name); - - Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE); + + Attr description = document + .createAttribute(DESCRIPTION_ATTRIBUTE); description.setValue(template.getDescription()); attributes.setNamedItem(description); - - Attr context= document.createAttribute(CONTEXT_ATTRIBUTE); + + Attr context = document.createAttribute(CONTEXT_ATTRIBUTE); context.setValue(template.getContextTypeId()); - attributes.setNamedItem(context); - - Text pattern= document.createTextNode(template.getPattern()); - node.appendChild(pattern); - } - - - Transformer transformer=TransformerFactory.newInstance().newTransformer(); + attributes.setNamedItem(context); + + Text pattern = document.createTextNode(template.getPattern()); + node.appendChild(pattern); + } + + Transformer transformer = TransformerFactory.newInstance() + .newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$ DOMSource source = new DOMSource(document); @@ -304,25 +319,35 @@ public class TemplateSet { throwWriteException(e); } catch (TransformerException e) { throwWriteException(e); - } + } } private static void throwReadException(Throwable t) throws CoreException { int code; if (t instanceof SAXException) - code= TEMPLATE_PARSE_EXCEPTION; + code = TEMPLATE_PARSE_EXCEPTION; else - code= TEMPLATE_IO_EXCEPTION; -// IStatus status= JavaUIStatus.createError(code, TemplateMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$ -// throw new JavaUIException(status); - throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.jface.text", code, JavaTemplateMessages.getString("TemplateSet.error.read"), t)); //$NON-NLS-1$ //$NON-NLS-2$ + code = TEMPLATE_IO_EXCEPTION; + // IStatus status= JavaUIStatus.createError(code, + // TemplateMessages.getString("TemplateSet.error.read"), t); + // //$NON-NLS-1$ + // throw new JavaUIException(status); + throw new CoreException( + new Status( + IStatus.ERROR, + "org.eclipse.jface.text", code, JavaTemplateMessages.getString("TemplateSet.error.read"), t)); //$NON-NLS-1$ //$NON-NLS-2$ } - + private static void throwWriteException(Throwable t) throws CoreException { -// IStatus status= JavaUIStatus.createError(IJavaStatusConstants.TEMPLATE_IO_EXCEPTION, -// TemplateMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$ -// throw new JavaUIException(status); - throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.jface.text", TEMPLATE_IO_EXCEPTION, JavaTemplateMessages.getString("TemplateSet.error.write"), t)); //$NON-NLS-1$ //$NON-NLS-2$ + // IStatus status= + // JavaUIStatus.createError(IJavaStatusConstants.TEMPLATE_IO_EXCEPTION, + // TemplateMessages.getString("TemplateSet.error.write"), t); + // //$NON-NLS-1$ + // throw new JavaUIException(status); + throw new CoreException( + new Status( + IStatus.ERROR, + "org.eclipse.jface.text", TEMPLATE_IO_EXCEPTION, JavaTemplateMessages.getString("TemplateSet.error.write"), t)); //$NON-NLS-1$ //$NON-NLS-2$ } /** @@ -331,7 +356,7 @@ public class TemplateSet { public void add(Template template) { if (exists(template)) return; // ignore duplicate - + fTemplates.add(template); } @@ -342,57 +367,56 @@ public class TemplateSet { if (template.equals(anotherTemplate)) return true; } - + return false; } - + /** * Removes a template to the set. - */ + */ public void remove(Template template) { fTemplates.remove(template); } /** * Empties the set. - */ + */ public void clear() { fTemplates.clear(); } - + /** * Returns all templates. */ public Template[] getTemplates() { return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]); } - + /** * Returns all templates with a given name. */ public Template[] getTemplates(String name) { - ArrayList res= new ArrayList(); - for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) { - Template curr= (Template) iterator.next(); + ArrayList res = new ArrayList(); + for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) { + Template curr = (Template) iterator.next(); if (curr.getName().equals(name)) { res.add(curr); } } return (Template[]) res.toArray(new Template[res.size()]); } - + /** * Returns the first templates with the given name. */ public Template getFirstTemplate(String name) { - for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) { - Template curr= (Template) iterator.next(); + for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) { + Template curr = (Template) iterator.next(); if (curr.getName().equals(name)) { return curr; } } return null; - } - -} + } +}