diff options
author | waqas.ikram <waqas.ikram@ericsson.com> | 2018-05-30 09:48:40 +0100 |
---|---|---|
committer | waqas.ikram <waqas.ikram@ericsson.com> | 2018-05-30 16:29:30 +0100 |
commit | 8bad2ad828fad2f5f99a9a291ca0f5c51b62e4ca (patch) | |
tree | 7b02ec8dd5dcda881b9133169a753fc6671e44bf /model/basic-model/src/main/java | |
parent | 55d93a12cc5575c872724f48585304b5eec77fea (diff) |
Fixing Sonar bugs and code smells
Change-Id: I661cd409d01320fe11a29b7ea72b9e8f70e72a6c
Issue-ID: POLICY-856
Signed-off-by: waqas.ikram <waqas.ikram@ericsson.com>
Diffstat (limited to 'model/basic-model/src/main/java')
3 files changed, 42 insertions, 37 deletions
diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java index 2dcbf7bcf..896f72a89 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java @@ -29,7 +29,7 @@ public class ApexException extends Exception { private static final long serialVersionUID = -8507246953751956974L; // The object on which the exception was thrown - private transient Object object = null; + private final transient Object object; /** * Instantiates a new apex exception. @@ -38,6 +38,7 @@ public class ApexException extends Exception { */ public ApexException(final String message) { super(message); + this.object = null; } /** @@ -59,6 +60,7 @@ public class ApexException extends Exception { */ public ApexException(final String message, final Exception e) { super(message, e); + this.object = null; } /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java index f3db3ae60..c3ecd2fd8 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * ================================================================================ @@ -29,7 +29,7 @@ public class ApexRuntimeException extends RuntimeException { private static final long serialVersionUID = -8507246953751956974L; // The object on which the exception was thrown - private transient Object object = null; + private final transient Object object; /** * Instantiates a new apex runtime exception. @@ -38,6 +38,7 @@ public class ApexRuntimeException extends RuntimeException { */ public ApexRuntimeException(final String message) { super(message); + this.object = null; } /** @@ -59,6 +60,7 @@ public class ApexRuntimeException extends RuntimeException { */ public ApexRuntimeException(final String message, final Exception e) { super(message, e); + this.object = null; } /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java index 180c2447a..7e56b18ae 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * ================================================================================ @@ -33,6 +33,7 @@ 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.dom.DOMSource; @@ -55,11 +56,11 @@ import org.w3c.dom.Document; * @param <C> the type of Apex concept to write, must be a sub class of {@link AxConcept} */ public class ApexModelWriter<C extends AxConcept> { - private static final String CONCEPT_MAY_NOT_BE_NULL = "concept may not be null"; + private static final String CONCEPT_MAY_NOT_BE_NULL = "concept may not be null"; private static final String CONCEPT_WRITER_MAY_NOT_BE_NULL = "concept writer may not be null"; - private static final String CONCEPT_STREAM_MAY_NOT_BE_NULL = "concept stream may not be null"; + private static final String CONCEPT_STREAM_MAY_NOT_BE_NULL = "concept stream may not be null"; - // Get a reference to the logger + // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelWriter.class); // Writing as JSON or XML @@ -91,8 +92,7 @@ public class ApexModelWriter<C extends AxConcept> { marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); - } - catch (final JAXBException e) { + } catch (final JAXBException e) { LOGGER.error("JAXB marshaller creation exception", e); throw new ApexModelException("JAXB marshaller creation exception", e); } @@ -130,17 +130,14 @@ public class ApexModelWriter<C extends AxConcept> { try { marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); - } - catch (final Exception e) { + } catch (final Exception e) { LOGGER.warn("JAXB error setting marshaller for JSON output", e); throw new ApexModelException("JAXB error setting marshaller for JSON output", e); } - } - else { + } else { try { marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); - } - catch (final Exception e) { + } catch (final Exception e) { LOGGER.warn("JAXB error setting marshaller for XML output", e); throw new ApexModelException("JAXB error setting marshaller for XML output", e); } @@ -157,7 +154,7 @@ public class ApexModelWriter<C extends AxConcept> { public void write(final C concept, final OutputStream apexConceptStream) throws ApexModelException { Assertions.argumentNotNull(concept, CONCEPT_MAY_NOT_BE_NULL); Assertions.argumentNotNull(apexConceptStream, CONCEPT_STREAM_MAY_NOT_BE_NULL); - + this.write(concept, new OutputStreamWriter(apexConceptStream)); } @@ -185,8 +182,7 @@ public class ApexModelWriter<C extends AxConcept> { if (jsonOutput) { writeJSON(concept, apexConceptWriter); - } - else { + } else { writeXML(concept, apexConceptWriter); } } @@ -204,38 +200,44 @@ public class ApexModelWriter<C extends AxConcept> { LOGGER.debug("writing Apex concept XML . . ."); try { - // Write the concept into a DOM document, then transform to add CDATA fields and pretty print, then write out the result + // Write the concept into a DOM document, then transform to add CDATA fields and pretty + // print, then write out the result final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); final Document document = docBuilderFactory.newDocumentBuilder().newDocument(); // Marshal the concept into the empty document. marshaller.marshal(concept, document); - // Transform the DOM to the output stream - final TransformerFactory transformerFactory = TransformerFactory.newInstance(); - final Transformer domTransformer = transformerFactory.newTransformer(); - - // Pretty print - try { - domTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); - // May fail if not using XALAN XSLT engine. But not in any way vital - domTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); - } - catch (final Exception ignore) { - // We ignore exceptions here and catch errors below - } + final Transformer domTransformer = getTransformer(); // Convert the cDataFieldSet into a space delimited string - domTransformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, cDataFieldSet.toString().replaceAll("[\\[\\]\\,]", " ")); + domTransformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, + cDataFieldSet.toString().replaceAll("[\\[\\]\\,]", " ")); domTransformer.transform(new DOMSource(document), new StreamResult(apexConceptWriter)); - } - catch (JAXBException | TransformerException | ParserConfigurationException e) { + } catch (JAXBException | TransformerException | ParserConfigurationException e) { LOGGER.warn("Unable to marshal Apex concept XML", e); throw new ApexModelException("Unable to marshal Apex concept XML", e); } LOGGER.debug("wrote Apex concept XML"); } + + private Transformer getTransformer() throws TransformerConfigurationException { + // Transform the DOM to the output stream + final TransformerFactory transformerFactory = TransformerFactory.newInstance(); + final Transformer domTransformer = transformerFactory.newTransformer(); + + // Pretty print + try { + domTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); + // May fail if not using XALAN XSLT engine. But not in any way vital + domTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); + } catch (final Exception ignore) { + LOGGER.trace("Unable to set indent property..."); + } + return domTransformer; + } + /** * This method writes the Apex concept into a writer in JSON format. * @@ -250,8 +252,7 @@ public class ApexModelWriter<C extends AxConcept> { try { marshaller.marshal(concept, apexConceptWriter); - } - catch (final JAXBException e) { + } catch (final JAXBException e) { LOGGER.warn("Unable to marshal Apex concept JSON", e); throw new ApexModelException("Unable to marshal Apex concept JSON", e); } |