diff options
Diffstat (limited to 'jpa')
6 files changed, 251 insertions, 60 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java index 5d828c4f64..3576799b41 100644 --- a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java +++ b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java @@ -11,15 +11,17 @@ package org.eclipse.jpt.eclipselink.jaxb.core.schemagen; import java.io.File; import java.io.IOException; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; -import org.eclipse.persistence.jaxb.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.SchemaOutputResolver; +import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.Result; +import org.eclipse.jpt.eclipselink.jaxb.core.schemagen.internal.JptEclipseLinkJaxbCoreMessages; +import org.eclipse.persistence.jaxb.JAXBContext; /** * Generate a EclipseLink JAXB Schema @@ -37,7 +39,6 @@ public class Main static public String NO_FACTORY_CLASS = "doesnt contain ObjectFactory.class"; //$NON-NLS-1$ static public String CANNOT_BE_CAST_TO_JAXBCONTEXT = "cannot be cast to org.eclipse.persistence.jaxb.JAXBContext"; //$NON-NLS-1$ - static public String JAXB_PROPERTIES_FILE_NOT_FOUND = "\nEclipseLink JAXBContextFactory must be specified in the jaxb.properties file to use EclipseLink MOXy for schema generation.\njavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory "; // ********** static methods ********** @@ -70,23 +71,21 @@ public class Main } private void generate() { - System.out.println("MOXy generating schema..."); //$NON-NLS-1$ - // Create the JAXBContext JAXBContext jaxbContext = this.buildJaxbContext(); // Generate an XML Schema if(jaxbContext != null) { - SchemaOutputResolver schemaOutputResolver = - new JptSchemaOutputResolver(this.targetSchemaName); - - jaxbContext.generateSchema(schemaOutputResolver); - - System.out.println("\nSchema " + this.targetSchemaName + " generated"); //$NON-NLS-1$ - } - } + this.generateSchema(jaxbContext); + } + String result = (jaxbContext != null) ? + this.bind(JptEclipseLinkJaxbCoreMessages.SCHEMA_GENERATED, this.targetSchemaName) : + this.bind(JptEclipseLinkJaxbCoreMessages.SCHEMA_NOT_CREATED, this.targetSchemaName); + System.out.println(result); + } private JAXBContext buildJaxbContext() { + System.out.println(this.getString(JptEclipseLinkJaxbCoreMessages.LOADING_CLASSES)); JAXBContext jaxbContext = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); @@ -96,28 +95,29 @@ public class Main jaxbContext = (JAXBContext)JAXBContext.newInstance(sourceClasses); } - catch (JAXBException e) { - String message = e.getMessage(); - if(message.indexOf(NO_FACTORY_CLASS) > -1) { - System.err.println(message); - } - else { - e.printStackTrace(); - } - System.err.println("\nSchema " + this.targetSchemaName + " not created"); //$NON-NLS-1$ + catch (JAXBException ex) { + this.handleJaxbException(ex); } - catch (ClassCastException e) { - String message = e.getMessage(); - if(message.indexOf(CANNOT_BE_CAST_TO_JAXBCONTEXT) > -1) { - System.err.println(JAXB_PROPERTIES_FILE_NOT_FOUND); - } - else { - e.printStackTrace(); - } - System.err.println("\nSchema " + this.targetSchemaName + " not created"); //$NON-NLS-1$ + catch (ClassCastException ex) { + this.handleClassCastException(ex); } return jaxbContext; } + + private void generateSchema(JAXBContext jaxbContext) { + System.out.println(this.getString(JptEclipseLinkJaxbCoreMessages.GENERATING_SCHEMA)); + System.out.flush(); + + SchemaOutputResolver schemaOutputResolver = + new JptSchemaOutputResolver(this.targetSchemaName); + + try { + jaxbContext.generateSchema(schemaOutputResolver); + } + catch(Exception e) { + e.printStackTrace(); + } + } private Class[] buildSourceClasses(String[] classNames, ClassLoader loader) { @@ -125,14 +125,49 @@ public class Main for(String className: classNames) { try { sourceClasses.add(loader.loadClass(className)); - System.out.println(className); //$NON-NLS-1$ + System.out.println(className); } catch (ClassNotFoundException e) { - System.err.println("\n\tNot found: " + className); //$NON-NLS-1$ + System.err.println(this.bind(JptEclipseLinkJaxbCoreMessages.NOT_FOUND, className)); } } + System.out.flush(); return sourceClasses.toArray(new Class[0]); } + + private void handleJaxbException(JAXBException ex) { + String message = ex.getMessage(); + Throwable linkedEx = ex.getLinkedException(); + if(message != null && message.indexOf(NO_FACTORY_CLASS) > -1) { + System.err.println(message); + } + else if(linkedEx != null && linkedEx instanceof ClassNotFoundException) { + String errorMessage = this.bind( + JptEclipseLinkJaxbCoreMessages.CONTEXT_FACTORY_NOT_FOUND, linkedEx.getMessage()); + System.err.println(errorMessage); + } + else { + ex.printStackTrace(); + } + } + + private void handleClassCastException(ClassCastException ex) { + String message = ex.getMessage(); + if(message != null && message.indexOf(CANNOT_BE_CAST_TO_JAXBCONTEXT) > -1) { + System.err.println(this.getString(JptEclipseLinkJaxbCoreMessages.PROPERTIES_FILE_NOT_FOUND)); + } + else { + ex.printStackTrace(); + } + } + + private String getString(String key) { + return JptEclipseLinkJaxbCoreMessages.getString(key); + } + + private String bind(String key, Object argument) { + return MessageFormat.format(this.getString(key), argument); + } // ********** argument queries ********** diff --git a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/JptEclipseLinkJaxbCoreMessages.java b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/JptEclipseLinkJaxbCoreMessages.java new file mode 100644 index 0000000000..62f89faba3 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/JptEclipseLinkJaxbCoreMessages.java @@ -0,0 +1,43 @@ +/******************************************************************************* +* Copyright (c) 2010 Oracle. All rights reserved. +* This program and the accompanying materials are made available under the +* terms of the Eclipse Public License v1.0, which accompanies this distribution +* and is available at http://www.eclipse.org/legal/epl-v10.html. +* +* Contributors: +* Oracle - initial API and implementation +*******************************************************************************/ +package org.eclipse.jpt.eclipselink.jaxb.core.schemagen.internal; + +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * Localized messages used by Dali EclipseLink JAXB core. + */ +public class JptEclipseLinkJaxbCoreMessages +{ + public static final String LOADING_CLASSES = "LOADING_CLASSES"; + public static final String GENERATING_SCHEMA = "GENERATING_SCHEMA"; + public static final String SCHEMA_GENERATED = "SCHEMA_GENERATED"; + public static final String SCHEMA_NOT_CREATED = "SCHEMA_NOT_CREATED"; + public static final String NOT_FOUND = "NOT_FOUND"; + public static final String CONTEXT_FACTORY_NOT_FOUND = "CONTEXT_FACTORY_NOT_FOUND"; + public static final String PROPERTIES_FILE_NOT_FOUND = "PROPERTIES_FILE_NOT_FOUND"; + + + private static final String BUNDLE_NAME = "org.eclipse.jpt.eclipselink.jaxb.core.schemagen.internal.jpt_eclipselink_jaxb_core"; //$NON-NLS-1$ + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + + private JptEclipseLinkJaxbCoreMessages() { + } + + public static String getString(String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } + catch (MissingResourceException e) { + return '!' + key + '!'; + } + } +}
\ No newline at end of file diff --git a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties new file mode 100644 index 0000000000..047632e98a --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties @@ -0,0 +1,21 @@ +################################################################################ +# Copyright (c) 2010 Oracle. All rights reserved. +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License v1.0, which accompanies this distribution +# and is available at http://www.eclipse.org/legal/epl-v10.html. +# +# Contributors: +# Oracle - initial API and implementation +################################################################################ + +LOADING_CLASSES = loading... +GENERATING_SCHEMA = \nMOXy generating schema... +SCHEMA_GENERATED = \nSchema {0} generated +SCHEMA_NOT_CREATED = \nSchema {0} not created +NOT_FOUND = \n\tNot found: {0} +PROPERTIES_FILE_NOT_FOUND = \nEclipseLink JAXBContextFactory must be specified in the jaxb.properties file to use EclipseLink MOXy for schema generation.\n\ +javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory +CONTEXT_FACTORY_NOT_FOUND = \nThe JAXBContextFactory {0} \n\ +configured in the jaxb.properties file could not be located on the project classpath. \n\ +The JAXB provider that defines this factory should be added to the project classpath, \n\ +or the JAXBContextFactory property should be removed to use the default provider. diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java index 8589e81754..17a36d09de 100644 --- a/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java +++ b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java @@ -11,15 +11,17 @@ package org.eclipse.jpt.jaxb.core.schemagen; import java.io.File; import java.io.IOException; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.SchemaOutputResolver; +import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.Result; +import org.eclipse.jpt.jaxb.core.schemagen.internal.JptJaxbCoreMessages; /** * Generate a JAXB Schema @@ -70,26 +72,22 @@ public class Main } private void generate() { - System.out.println("generating schema..."); //$NON-NLS-1$ // Create the JAXBContext JAXBContext jaxbContext = this.buildJaxbContext(); - + // Generate an XML Schema - SchemaOutputResolver schemaOutputResolver = - new JptSchemaOutputResolver(this.targetSchemaName); - - try { - jaxbContext.generateSchema(schemaOutputResolver); + if(jaxbContext != null) { + this.generateSchema(jaxbContext); } - catch (IOException e) { - e.printStackTrace(); - return; - } - System.out.println("\nSchema " + this.targetSchemaName + " generated"); + String result = (jaxbContext != null) ? + this.bind(JptJaxbCoreMessages.SCHEMA_GENERATED, this.targetSchemaName) : + this.bind(JptJaxbCoreMessages.SCHEMA_NOT_CREATED, this.targetSchemaName); + System.out.println(result); } private JAXBContext buildJaxbContext() { - JAXBContext jaxbContext = null; + System.out.println(this.getString(JptJaxbCoreMessages.LOADING_CLASSES)); + JAXBContext jaxbContext = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); @@ -97,18 +95,26 @@ public class Main jaxbContext = JAXBContext.newInstance(sourceClasses); } - catch (JAXBException e) { - String message = e.getMessage(); - if(message.indexOf(NO_FACTORY_CLASS) > -1) { - System.err.println(message); - } - else { - e.printStackTrace(); - } - System.err.println("\nSchema " + this.targetSchemaName + " not created"); + catch(JAXBException ex) { + this.handleException(ex); } return jaxbContext; } + + private void generateSchema(JAXBContext jaxbContext) { + System.out.println(this.getString(JptJaxbCoreMessages.GENERATING_SCHEMA)); + System.out.flush(); + + SchemaOutputResolver schemaOutputResolver = + new JptSchemaOutputResolver(this.targetSchemaName); + + try { + jaxbContext.generateSchema(schemaOutputResolver); + } + catch (IOException e) { + e.printStackTrace(); + } + } private Class[] buildSourceClasses(String[] classNames, ClassLoader loader) { @@ -119,27 +125,51 @@ public class Main System.out.println(className); } catch (ClassNotFoundException e) { - System.err.println("\n\tNot found: " + className); + System.err.println(this.bind(JptJaxbCoreMessages.NOT_FOUND, className)); } } + System.out.flush(); return sourceClasses.toArray(new Class[0]); } + + private void handleException(JAXBException ex) { + String message = ex.getMessage(); + Throwable linkedEx = ex.getLinkedException(); + if(message != null && message.indexOf(NO_FACTORY_CLASS) > -1) { + System.err.println(message); + } + else if(linkedEx != null && linkedEx instanceof ClassNotFoundException) { + String errorMessage = this.bind(JptJaxbCoreMessages.CONTEXT_FACTORY_NOT_FOUND, linkedEx.getMessage()); + System.err.println(errorMessage); + } + else { + ex.printStackTrace(); + } + } + + private String getString(String key) { + return JptJaxbCoreMessages.getString(key); + } + + private String bind(String key, Object argument) { + return MessageFormat.format(this.getString(key), argument); + } // ********** argument queries ********** private String[] getSourceClassNames(String[] args) { - return this.getAllArgumentValue("-c", args); + return this.getAllArgumentValue("-c", args); //$NON-NLS-1$ } private String getTargetSchemaName(String[] args) { - return this.getArgumentValue("-s", args); + return this.getArgumentValue("-s", args); //$NON-NLS-1$ } private boolean getDebugMode(String[] args) { - return this.argumentExists("-debug", args); + return this.argumentExists("-debug", args); //$NON-NLS-1$ } private String getArgumentValue(String argName, String[] args) { diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/JptJaxbCoreMessages.java b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/JptJaxbCoreMessages.java new file mode 100644 index 0000000000..49170533dd --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/JptJaxbCoreMessages.java @@ -0,0 +1,43 @@ +/******************************************************************************* +* Copyright (c) 2010 Oracle. All rights reserved. +* This program and the accompanying materials are made available under the +* terms of the Eclipse Public License v1.0, which accompanies this distribution +* and is available at http://www.eclipse.org/legal/epl-v10.html. +* +* Contributors: +* Oracle - initial API and implementation +*******************************************************************************/ +package org.eclipse.jpt.jaxb.core.schemagen.internal; + +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * Localized messages used by Dali JAXB core. + */ +public class JptJaxbCoreMessages +{ + + public static final String LOADING_CLASSES = "LOADING_CLASSES"; + public static final String GENERATING_SCHEMA = "GENERATING_SCHEMA"; + public static final String SCHEMA_GENERATED = "SCHEMA_GENERATED"; + public static final String SCHEMA_NOT_CREATED = "SCHEMA_NOT_CREATED"; + public static final String NOT_FOUND = "NOT_FOUND"; + public static final String CONTEXT_FACTORY_NOT_FOUND = "CONTEXT_FACTORY_NOT_FOUND"; + + + private static final String BUNDLE_NAME = "org.eclipse.jpt.jaxb.core.schemagen.internal.jpt_jaxb_core"; //$NON-NLS-1$ + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + + private JptJaxbCoreMessages() { + } + + public static String getString(String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } + catch (MissingResourceException e) { + return '!' + key + '!'; + } + } +}
\ No newline at end of file diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties new file mode 100644 index 0000000000..4176246858 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties @@ -0,0 +1,19 @@ +################################################################################ +# Copyright (c) 2010 Oracle. All rights reserved. +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License v1.0, which accompanies this distribution +# and is available at http://www.eclipse.org/legal/epl-v10.html. +# +# Contributors: +# Oracle - initial API and implementation +################################################################################ + +LOADING_CLASSES = loading... +GENERATING_SCHEMA = \ngenerating schema... +SCHEMA_GENERATED = \nSchema {0} generated +SCHEMA_NOT_CREATED = \nSchema {0} not created +NOT_FOUND = \n\tNot found: {0} +CONTEXT_FACTORY_NOT_FOUND = \nThe JAXBContextFactory {0} \n\ +configured in the jaxb.properties file could not be located on the project classpath. \n\ +The JAXB provider that defines this factory should be added to the project classpath, \n\ +or the JAXBContextFactory property should be removed to use the default provider. |