tmf: extract common elements in custom txt/xml trace definitions
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / parsers / custom / CustomXmlTraceDefinition.java
index 8da891695931afa1704152bdc1e9eb1412accf32..a8124f5f2bba0cb1c2266b6d6b253bb2efaef3ea 100644 (file)
@@ -30,7 +30,6 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.TransformerFactoryConfigurationError;
@@ -69,16 +68,17 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
 
     /** Path to the XML definitions file */
     protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME =
-            Platform.getInstallLocation().getURL().getPath() +
-            "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
-            CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME;
+            Platform.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
+                    CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME;
 
     /** Path to the XML definitions file */
     protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =
             Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
 
-    /** Legacy path to the XML definitions file (in the UI plug-in)
-     * TODO Remove once we feel the transition phase is over. */
+    /**
+     * Legacy path to the XML definitions file (in the UI plug-in) TODO Remove
+     * once we feel the transition phase is over.
+     */
     private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY =
             Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
                     .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
@@ -324,31 +324,10 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
             DocumentBuilder db = dbf.newDocumentBuilder();
 
             // The following allows xml parsing without access to the dtd
-            EntityResolver resolver = new EntityResolver() {
-                @Override
-                public InputSource resolveEntity(String publicId, String systemId) {
-                    String empty = ""; //$NON-NLS-1$
-                    ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
-                    return new InputSource(bais);
-                }
-            };
-            db.setEntityResolver(resolver);
+            db.setEntityResolver(createEmptyEntityResolver());
 
             // The following catches xml parsing exceptions
-            db.setErrorHandler(new ErrorHandler() {
-                @Override
-                public void error(SAXParseException saxparseexception) throws SAXException {
-                }
-
-                @Override
-                public void warning(SAXParseException saxparseexception) throws SAXException {
-                }
-
-                @Override
-                public void fatalError(SAXParseException saxparseexception) throws SAXException {
-                    throw saxparseexception;
-                }
-            });
+            db.setErrorHandler(createErrorHandler());
 
             Document doc = null;
             File file = new File(path);
@@ -409,17 +388,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
 
             TmfTraceType.addCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
 
-        } catch (ParserConfigurationException e) {
-            Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (TransformerConfigurationException e) {
-            Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (TransformerFactoryConfigurationError e) {
-            Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (TransformerException e) {
-            Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (IOException e) {
-            Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (SAXException e) {
+        } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException | IOException | SAXException e) {
             Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
         }
     }
@@ -512,31 +481,10 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
             DocumentBuilder db = dbf.newDocumentBuilder();
 
             // The following allows xml parsing without access to the dtd
-            EntityResolver resolver = new EntityResolver() {
-                @Override
-                public InputSource resolveEntity(String publicId, String systemId) {
-                    String empty = ""; //$NON-NLS-1$
-                    ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
-                    return new InputSource(bais);
-                }
-            };
-            db.setEntityResolver(resolver);
+            db.setEntityResolver(createEmptyEntityResolver());
 
             // The following catches xml parsing exceptions
-            db.setErrorHandler(new ErrorHandler() {
-                @Override
-                public void error(SAXParseException saxparseexception) throws SAXException {
-                }
-
-                @Override
-                public void warning(SAXParseException saxparseexception) throws SAXException {
-                }
-
-                @Override
-                public void fatalError(SAXParseException saxparseexception) throws SAXException {
-                    throw saxparseexception;
-                }
-            });
+            db.setErrorHandler(createErrorHandler());
 
             File file = new File(path);
             if (!file.canRead()) {
@@ -561,11 +509,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                 }
             }
             return defList.toArray(new CustomXmlTraceDefinition[0]);
-        } catch (ParserConfigurationException e) {
-            Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (SAXException e) {
-            Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
-        } catch (IOException e) {
+        } catch (ParserConfigurationException | SAXException | IOException e) {
             Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
         }
         return new CustomXmlTraceDefinition[0];
@@ -615,11 +559,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                 value = lookupXmlDefinition(definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME);
             }
             return value;
-        } catch (ParserConfigurationException e) {
-            Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (SAXException e) {
-            Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (IOException e) {
+        } catch (ParserConfigurationException | SAXException | IOException e) {
             Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
         }
         return null;
@@ -804,17 +744,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
             // Check if default definition needs to be reloaded
             TmfTraceType.addCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
 
-        } catch (ParserConfigurationException e) {
-            Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (SAXException e) {
-            Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (IOException e) {
-            Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (TransformerConfigurationException e) {
-            Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (TransformerFactoryConfigurationError e) {
-            Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
-        } catch (TransformerException e) {
+        } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) {
             Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
         }
     }
This page took 0.02646 seconds and 5 git commands to generate.