From b487e09a7c823d378c66edca39146e4a286955fe Mon Sep 17 00:00:00 2001 From: Richard Richter Date: Mon, 2 Mar 2020 14:40:44 +0100 Subject: [PATCH] PrettyPrinter, JaxbTestUtil: code cleanup and reformat --- .../prism/impl/util/JaxbTestUtil.java | 82 +++++++------------ .../evolveum/midpoint/util/PrettyPrinter.java | 74 ++++++++--------- 2 files changed, 62 insertions(+), 94 deletions(-) diff --git a/infra/prism-impl/src/main/java/com/evolveum/midpoint/prism/impl/util/JaxbTestUtil.java b/infra/prism-impl/src/main/java/com/evolveum/midpoint/prism/impl/util/JaxbTestUtil.java index eb8cf9fdb34..e6e797d4b97 100644 --- a/infra/prism-impl/src/main/java/com/evolveum/midpoint/prism/impl/util/JaxbTestUtil.java +++ b/infra/prism-impl/src/main/java/com/evolveum/midpoint/prism/impl/util/JaxbTestUtil.java @@ -6,48 +6,28 @@ */ package com.evolveum.midpoint.prism.impl.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; +import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; +import javax.xml.bind.*; import javax.xml.namespace.QName; -import com.evolveum.midpoint.prism.Containerable; -import com.evolveum.midpoint.prism.PrismContainer; -import com.evolveum.midpoint.prism.PrismContainerDefinition; - -import com.evolveum.midpoint.prism.impl.schema.SchemaRegistryImpl; -import com.evolveum.midpoint.prism.util.PrismTestUtil; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.Validate; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; -import com.evolveum.midpoint.prism.Objectable; -import com.evolveum.midpoint.prism.PrismContainerValue; -import com.evolveum.midpoint.prism.PrismContext; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismObjectDefinition; +import com.evolveum.midpoint.prism.*; +import com.evolveum.midpoint.prism.impl.schema.SchemaRegistryImpl; import com.evolveum.midpoint.prism.schema.SchemaDescription; import com.evolveum.midpoint.prism.schema.SchemaRegistry; +import com.evolveum.midpoint.prism.util.PrismTestUtil; import com.evolveum.midpoint.prism.xml.DynamicNamespacePrefixMapper; import com.evolveum.midpoint.util.DOMUtil; -import com.evolveum.midpoint.util.JAXBUtil; import com.evolveum.midpoint.util.exception.SchemaException; import com.evolveum.midpoint.util.exception.SystemException; import com.evolveum.midpoint.util.logging.Trace; @@ -58,16 +38,15 @@ * (The last drop was the inability to hide RawType from externally-visible model service WSDL. * The solution devised, based on @Raw type, prevents JAXB from correctly parsing any structures that * contain RawType elements.) - * + *

* So, don't use JaxbTestUtil even for testing purposes. Use PrismTestUtil instead. - * - * === + *

* Original description: - * + *

* JAXB testing util. Only for use in tests. DO NOT USE IN PRODUCTION CODE. * This util is used to test the ablility of prism JAXB representation to be used by * native (Sun) JAXB code. - * + *

* Note: this is what used to be PrismJaxbProcessor. Therefore there may be still a lot of junk to clean up. * * @author Radovan Semancik @@ -143,7 +122,7 @@ public boolean isJaxbClass(Class clazz) { // not a JAXB class return false; } - for (Package jaxbPackage: getSchemaRegistry().getCompileTimePackages()) { + for (Package jaxbPackage : getSchemaRegistry().getCompileTimePackages()) { if (jaxbPackage.equals(clazz.getPackage())) { return true; } @@ -246,9 +225,9 @@ public String marshalElementToString(Object element, Map propert if (element instanceof Element) { return DOMUtil.serializeDOMToString((Element) element); } else if (element instanceof JAXBElement) { - return marshalElementToString((JAXBElement)element, properties); + return marshalElementToString((JAXBElement) element, properties); } else { - throw new IllegalArgumentException("Unsupported element type "+element.getClass().getName()); + throw new IllegalArgumentException("Unsupported element type " + element.getClass().getName()); } } @@ -262,7 +241,7 @@ public String marshalElementToString(Object element, QName elementName) throws J if (element instanceof Element) { return DOMUtil.serializeDOMToString((Element) element); } else if (element instanceof JAXBElement) { - return marshalElementToString((JAXBElement)element); + return marshalElementToString((JAXBElement) element); } else { JAXBElement jaxbElement = new JAXBElement<>(elementName, Object.class, element); return marshalElementToString(jaxbElement); @@ -309,7 +288,7 @@ public Element marshalObjectToDom(T jaxbObject, QName elementQName, Document } JAXBElement jaxbElement = new JAXBElement<>(elementQName, (Class) jaxbObject.getClass(), - jaxbObject); + jaxbObject); Element element = doc.createElementNS(elementQName.getNamespaceURI(), elementQName.getLocalPart()); marshalElementToDom(jaxbElement, element); @@ -319,7 +298,7 @@ public Element marshalObjectToDom(T jaxbObject, QName elementQName, Document public void marshalObjectToDom(T jaxbObject, QName elementQName, Element parentElement) throws JAXBException { JAXBElement jaxbElement = new JAXBElement<>(elementQName, (Class) jaxbObject.getClass(), - jaxbObject); + jaxbObject); marshalElementToDom(jaxbElement, parentElement); } @@ -328,7 +307,7 @@ public Element toDomElement(Object element) throws JAXBException { } public Element toDomElement(Object jaxbElement, Document doc) throws JAXBException { - return toDomElement(jaxbElement,doc,false,false,false); + return toDomElement(jaxbElement, doc, false, false, false); } @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -356,7 +335,6 @@ public Element toDomElement(Object jaxbElement, Document doc, boolean adopt, boo } } - public JAXBElement unmarshalElement(String xmlString, Class type) throws JAXBException, SchemaException { if (xmlString == null) { return null; @@ -439,12 +417,12 @@ public T unmarshalObject(File file, Class type) throws JAXBException, Sch T value = element.getValue(); // adopt not needed, already adopted in unmarshalElement call above if (!type.isAssignableFrom(value.getClass())) { - throw new IllegalArgumentException("Unmarshalled "+value.getClass()+" from file "+file+" while "+type+" was expected"); + throw new IllegalArgumentException("Unmarshalled " + value.getClass() + " from file " + file + " while " + type + " was expected"); } return value; } - public Object unmarshalObjects(File file) throws JAXBException{ + public Object unmarshalObjects(File file) throws JAXBException { return createUnmarshaller().unmarshal(file); } @@ -488,19 +466,18 @@ private PrismContainer unmarshalSingleValueContaine return container; } - public T unmarshalObject(Object domOrJaxbElement, Class type) throws SchemaException { JAXBElement element; if (domOrJaxbElement instanceof JAXBElement) { element = (JAXBElement) domOrJaxbElement; } else if (domOrJaxbElement instanceof Node) { try { - element = unmarshalElement((Node)domOrJaxbElement, type); + element = unmarshalElement((Node) domOrJaxbElement, type); } catch (JAXBException e) { - throw new SchemaException(e.getMessage(),e); + throw new SchemaException(e.getMessage(), e); } } else { - throw new IllegalArgumentException("Unknown element type "+domOrJaxbElement); + throw new IllegalArgumentException("Unknown element type " + domOrJaxbElement); } if (element == null) { return null; @@ -521,7 +498,7 @@ public JAXBElement unmarshalElement(File file, Class type) throws Sche JAXBElement element = (JAXBElement) getUnmarshaller().unmarshal(is); adopt(element); return element; - } catch (RuntimeException ex){ + } catch (RuntimeException ex) { throw new SystemException(ex); } finally { if (is != null) { @@ -627,7 +604,6 @@ private boolean compareElement(Object a, Object b) { return DOMUtil.compareElement(ae, be, true); } - public T fromElement(Object element, Class type) throws SchemaException { if (element == null) { @@ -649,14 +625,14 @@ public T fromElement(Object element, Class type) throws SchemaException { if (element instanceof Element) { try { - JAXBElement unmarshalledElement = unmarshalElement((Element)element, type); + JAXBElement unmarshalledElement = unmarshalElement((Element) element, type); return unmarshalledElement.getValue(); } catch (JAXBException e) { - throw new IllegalArgumentException("Unmarshall failed: " + e.getMessage(),e); + throw new IllegalArgumentException("Unmarshall failed: " + e.getMessage(), e); } } - throw new IllegalArgumentException("Unknown element type "+element.getClass().getName()); + throw new IllegalArgumentException("Unknown element type " + element.getClass().getName()); } private QName determineElementQName(Objectable objectable) { @@ -670,7 +646,7 @@ private QName determineElementQName(Objectable objectable) { return definition.getItemName(); } } - throw new IllegalStateException("Cannot determine element name of "+objectable); + throw new IllegalStateException("Cannot determine element name of " + objectable); } private QName determineElementQName(Containerable containerable) { @@ -690,15 +666,15 @@ private boolean isObjectable(Class type) { private void adopt(T object, Class type) throws SchemaException { if (object instanceof Objectable) { - getPrismContext().adopt(((Objectable)object)); + getPrismContext().adopt(((Objectable) object)); } } private void adopt(Object object) throws SchemaException { if (object instanceof JAXBElement) { - adopt(((JAXBElement)object).getValue()); + adopt(((JAXBElement) object).getValue()); } else if (object instanceof Objectable) { - getPrismContext().adopt(((Objectable)(object))); + getPrismContext().adopt(((Objectable) (object))); } } diff --git a/infra/util/src/main/java/com/evolveum/midpoint/util/PrettyPrinter.java b/infra/util/src/main/java/com/evolveum/midpoint/util/PrettyPrinter.java index f3393da61a5..72fd94ecb11 100644 --- a/infra/util/src/main/java/com/evolveum/midpoint/util/PrettyPrinter.java +++ b/infra/util/src/main/java/com/evolveum/midpoint/util/PrettyPrinter.java @@ -9,14 +9,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.List; +import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; - import javax.xml.XMLConstants; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; @@ -28,7 +22,6 @@ /** * @author semancik - * */ public class PrettyPrinter { @@ -53,7 +46,7 @@ static String prettyPrintElementAsProperty(Object element) { sb.append(prettyPrint(elementName)); sb.append(">"); if (element instanceof Element) { - Element domElement = (Element)element; + Element domElement = (Element) element; // TODO: this is too simplistic, expand later sb.append(domElement.getTextContent()); } else { @@ -94,9 +87,9 @@ public static String prettyPrint(QName qname) { } if (qname.getNamespaceURI() != null) { if (qname.getNamespaceURI().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) { - return "{"+DOMUtil.NS_W3C_XML_SCHEMA_PREFIX+":}"+qname.getLocalPart(); + return "{" + DOMUtil.NS_W3C_XML_SCHEMA_PREFIX + ":}" + qname.getLocalPart(); } else if (defaultNamespacePrefix != null && qname.getNamespaceURI().startsWith(defaultNamespacePrefix)) { - return "{..."+qname.getNamespaceURI().substring(defaultNamespacePrefix.length())+"}"+qname.getLocalPart(); + return "{..." + qname.getNamespaceURI().substring(defaultNamespacePrefix.length()) + "}" + qname.getLocalPart(); } } return qnameToString(qname); // avoiding qname.toString because recursive call to prettyPrint from ItemName.toString @@ -216,7 +209,6 @@ public static String prettyPrint(Element element, boolean displayTag) { return sb.toString(); } - public static String prettyPrint(Date date) { if (date == null) { return "null"; @@ -235,7 +227,7 @@ public static String prettyPrint(byte[] value) { if (printlen > BYTE_ARRAY_MAX_LEN) { printlen = BYTE_ARRAY_MAX_LEN; } - for(int i=0; i < printlen; i++) { + for (int i = 0; i < printlen; i++) { sb.append(String.format("%02x", value[i] & 0xff)); } if (value.length > BYTE_ARRAY_MAX_LEN) { @@ -252,10 +244,10 @@ public static String prettyPrint(Object value) { return "null"; } if (value instanceof JAXBElement) { - Object elementValue = ((JAXBElement)value).getValue(); + Object elementValue = ((JAXBElement) value).getValue(); String attempt = tryPrettyPrint(elementValue); if (attempt != null) { - return ("JAXBElement("+((JAXBElement)value).getName()+","+attempt+")"); + return ("JAXBElement(" + ((JAXBElement) value).getName() + "," + attempt + ")"); } } String attempt = tryPrettyPrint(value); @@ -268,32 +260,32 @@ public static String prettyPrint(Object value) { private static String tryPrettyPrint(Object value) { if (value instanceof Class) { - Class c = (Class)value; + Class c = (Class) value; if (c.getPackage().getName().equals("com.evolveum.midpoint.xml.ns._public.common.common_3")) { return c.getSimpleName(); } return c.getName(); } if (value instanceof Collection) { - return PrettyPrinter.prettyPrint((Collection)value); + return PrettyPrinter.prettyPrint((Collection) value); } if (value.getClass().isArray()) { Class cclass = value.getClass().getComponentType(); if (cclass.isPrimitive()) { if (cclass == byte.class) { - return PrettyPrinter.prettyPrint((byte[])value); + return PrettyPrinter.prettyPrint((byte[]) value); } else { // TODO: something better } } else { - return PrettyPrinter.prettyPrint((Object[])value); + return PrettyPrinter.prettyPrint((Object[]) value); } } if (value instanceof Node) { // This is interface, won't catch it using reflection - return PrettyPrinter.prettyPrint((Node)value); + return PrettyPrinter.prettyPrint((Node) value); } - for (Class prettyPrinterClass: prettyPrinters) { + for (Class prettyPrinterClass : prettyPrinters) { String printerValue = tryPrettyPrint(value, prettyPrinterClass); if (printerValue != null) { return printerValue; @@ -309,15 +301,15 @@ private static String tryPrettyPrint(Object value, Class prettyPrinterClass) Class[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1 && parameterTypes[0].equals(value.getClass())) { try { - return (String)method.invoke(null, value); + return (String) method.invoke(null, value); } catch (IllegalArgumentException e) { - return "###INTERNAL#ERROR### Illegal argument: "+e.getMessage() + "; prettyPrint method for value "+value; + return "###INTERNAL#ERROR### Illegal argument: " + e.getMessage() + "; prettyPrint method for value " + value; } catch (IllegalAccessException e) { - return "###INTERNAL#ERROR### Illegal access: "+e.getMessage() + "; prettyPrint method for value "+value; + return "###INTERNAL#ERROR### Illegal access: " + e.getMessage() + "; prettyPrint method for value " + value; } catch (InvocationTargetException e) { - return "###INTERNAL#ERROR### Illegal target: "+e.getMessage() + "; prettyPrint method for value "+value; + return "###INTERNAL#ERROR### Illegal target: " + e.getMessage() + "; prettyPrint method for value " + value; } catch (Throwable e) { - return "###INTERNAL#ERROR### "+e.getClass().getName()+": "+e.getMessage() + "; prettyPrint method for value "+value; + return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrint method for value " + value; } } } @@ -331,10 +323,10 @@ public static String debugDump(Object value, int indent) { } String out = null; if (value instanceof DebugDumpable) { - return ((DebugDumpable)value).debugDump(indent); + return ((DebugDumpable) value).debugDump(indent); } if (value instanceof Collection) { - return DebugUtil.debugDump((Collection)value, indent); + return DebugUtil.debugDump((Collection) value, indent); } out = tryDebugDumpMethod(value, indent); if (out != null) { @@ -346,7 +338,7 @@ public static String debugDump(Object value, int indent) { } private static String tryDebugDumpMethod(Object value, int indent) { - for (Class prettyPrinterClass: prettyPrinters) { + for (Class prettyPrinterClass : prettyPrinters) { String printerValue = tryDebugDumpMethod(value, indent, prettyPrinterClass); if (printerValue != null) { return printerValue; @@ -362,15 +354,15 @@ private static String tryDebugDumpMethod(Object value, int indent, Class pret Class[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 2 && parameterTypes[0].equals(value.getClass())) { try { - return (String)method.invoke(null, value, indent); + return (String) method.invoke(null, value, indent); } catch (IllegalArgumentException e) { - return "###INTERNAL#ERROR### Illegal argument: "+e.getMessage() + "; debugDump method for value "+value; + return "###INTERNAL#ERROR### Illegal argument: " + e.getMessage() + "; debugDump method for value " + value; } catch (IllegalAccessException e) { - return "###INTERNAL#ERROR### Illegal access: "+e.getMessage() + "; debugDump method for value "+value; + return "###INTERNAL#ERROR### Illegal access: " + e.getMessage() + "; debugDump method for value " + value; } catch (InvocationTargetException e) { - return "###INTERNAL#ERROR### Illegal target: "+e.getMessage() + "; debugDump method for value "+value; + return "###INTERNAL#ERROR### Illegal target: " + e.getMessage() + "; debugDump method for value " + value; } catch (Throwable e) { - return "###INTERNAL#ERROR### "+e.getClass().getName()+": "+e.getMessage() + "; debugDump method for value "+value; + return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; debugDump method for value " + value; } } } @@ -384,11 +376,11 @@ public static void shortDump(StringBuilder sb, Object value) { return; } if (value instanceof ShortDumpable) { - ((ShortDumpable)value).shortDump(sb); + ((ShortDumpable) value).shortDump(sb); return; } if (value instanceof Collection) { - DebugUtil.shortDump(sb, (Collection)value); + DebugUtil.shortDump(sb, (Collection) value); return; } if (tryShortDumpMethod(sb, value)) { @@ -398,7 +390,7 @@ public static void shortDump(StringBuilder sb, Object value) { } private static boolean tryShortDumpMethod(StringBuilder sb, Object value) { - for (Class prettyPrinterClass: prettyPrinters) { + for (Class prettyPrinterClass : prettyPrinters) { if (tryShortDumpMethod(sb, value, prettyPrinterClass)) { return true; } @@ -416,13 +408,13 @@ private static boolean tryShortDumpMethod(StringBuilder sb, Object value, Class< method.invoke(null, sb, value); return true; } catch (IllegalArgumentException e) { - sb.append("###INTERNAL#ERROR### Illegal argument: "+e.getMessage() + "; shortDump method for value "+value); + sb.append("###INTERNAL#ERROR### Illegal argument: " + e.getMessage() + "; shortDump method for value " + value); } catch (IllegalAccessException e) { - sb.append("###INTERNAL#ERROR### Illegal access: "+e.getMessage() + "; shortDump method for value "+value); + sb.append("###INTERNAL#ERROR### Illegal access: " + e.getMessage() + "; shortDump method for value " + value); } catch (InvocationTargetException e) { - sb.append("###INTERNAL#ERROR### Illegal target: "+e.getMessage() + "; shortDump method for value "+value); + sb.append("###INTERNAL#ERROR### Illegal target: " + e.getMessage() + "; shortDump method for value " + value); } catch (Throwable e) { - sb.append("###INTERNAL#ERROR### "+e.getClass().getName()+": "+e.getMessage() + "; shortDump method for value "+value); + sb.append("###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; shortDump method for value " + value); } } }