Skip to content

Package: SAXUtil

SAXUtil

nameinstructionbranchcomplexitylinemethod
executeContentHandler(Reader, ContentHandler)
M: 6 C: 19
76%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 8
73%
M: 0 C: 1
100%
log(Throwable)
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.spi.view.migrator;
15:
16: import java.io.IOException;
17: import java.io.Reader;
18:
19: import org.eclipse.emfforms.spi.common.report.AbstractReport;
20: import org.eclipse.emfforms.spi.common.report.ReportService;
21: import org.osgi.framework.Bundle;
22: import org.osgi.framework.BundleContext;
23: import org.osgi.framework.FrameworkUtil;
24: import org.osgi.framework.ServiceReference;
25: import org.xml.sax.ContentHandler;
26: import org.xml.sax.InputSource;
27: import org.xml.sax.SAXException;
28: import org.xml.sax.XMLReader;
29: import org.xml.sax.helpers.XMLReaderFactory;
30:
31: /**
32: * Util class for SAX related methods.
33: *
34: * @author Johannes Faltermeier
35: * @since 1.8
36: *
37: */
38: public final class SAXUtil {
39:
40:         private SAXUtil() {
41:         }
42:
43:         /**
44:          * Creates an {@link XMLReader} based on the given model-reader and sets the given content handler.
45:          *
46:          * @param modelReader the input
47:          * @param contentHandler the {@link ContentHandler}
48:          */
49:         public static void executeContentHandler(Reader modelReader, final ContentHandler contentHandler) {
50:                 try {
51:                         final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
52:                         xmlReader.setContentHandler(contentHandler);
53:                         xmlReader.parse(new InputSource(modelReader));
54:                 } catch (final SAXException e) {
55:                         // do nothing. we use this as a break during parsing
56:                 } catch (final IOException ex) {
57:                         log(ex);
58:                 } finally {
59:                         try {
60:•                                if (modelReader != null) {
61:                                         modelReader.close();
62:                                 }
63:                         } catch (final IOException e) {
64:                                 log(e);
65:                         }
66:                 }
67:         }
68:
69:         private static void log(Throwable ex) {
70:                 final Bundle bundle = FrameworkUtil.getBundle(SAXUtil.class);
71:•                if (bundle == null) {
72:                         return;
73:                 }
74:                 final BundleContext bundleContext = bundle.getBundleContext();
75:                 final ServiceReference<ReportService> serviceReference = bundleContext.getServiceReference(ReportService.class);
76:•                if (serviceReference == null) {
77:                         return;
78:                 }
79:                 final ReportService reportService = bundleContext.getService(serviceReference);
80:                 reportService.report(new AbstractReport(ex));
81:                 bundleContext.ungetService(serviceReference);
82:         }
83: }