From eb91ff4a03a18a0e5767c813a6322d3b4358be21 Mon Sep 17 00:00:00 2001 From: tle Date: Thu, 28 Oct 2010 04:10:07 +0000 Subject: 327151 - Schema gen doesn't handle multiple namespace --- .../org/eclipse/jpt/jaxb/core/schemagen/Main.java | 75 ++++++++++----- .../jpt/jaxb/core/schemagen/internal/Tools.java | 104 +++++++++++++++++++++ .../jpt/jaxb/eclipselink/core/schemagen/Main.java | 71 +++++++++----- .../eclipselink/core/schemagen/internal/Tools.java | 104 +++++++++++++++++++++ .../org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF | 2 +- .../wizards/schemagen/NewSchemaFileWizardPage.java | 21 ++++- 6 files changed, 326 insertions(+), 51 deletions(-) create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/Tools.java create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/internal/Tools.java diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java index b469e47b82..56f1695e92 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/Main.java @@ -11,7 +11,6 @@ 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; @@ -22,6 +21,7 @@ import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; import org.eclipse.jpt.jaxb.core.schemagen.internal.JptJaxbCoreMessages; +import org.eclipse.jpt.jaxb.core.schemagen.internal.Tools; /** * Generate a JAXB Schema @@ -79,14 +79,13 @@ public class Main if(jaxbContext != null) { this.generateSchema(jaxbContext); } - String result = (jaxbContext != null) ? - this.bind(JptJaxbCoreMessages.SCHEMA_GENERATED, this.targetSchemaName) : - this.bind(JptJaxbCoreMessages.SCHEMA_NOT_CREATED, this.targetSchemaName); - System.out.println(result); + else { + System.out.println(Tools.bind(JptJaxbCoreMessages.SCHEMA_NOT_CREATED, this.targetSchemaName)); + } } private JAXBContext buildJaxbContext() { - System.out.println(this.getString(JptJaxbCoreMessages.LOADING_CLASSES)); + System.out.println(Tools.getString(JptJaxbCoreMessages.LOADING_CLASSES)); JAXBContext jaxbContext = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); @@ -102,7 +101,7 @@ public class Main } private void generateSchema(JAXBContext jaxbContext) { - System.out.println(this.getString(JptJaxbCoreMessages.GENERATING_SCHEMA)); + System.out.println(Tools.getString(JptJaxbCoreMessages.GENERATING_SCHEMA)); System.out.flush(); SchemaOutputResolver schemaOutputResolver = @@ -125,7 +124,7 @@ public class Main System.out.println(className); } catch (ClassNotFoundException e) { - System.err.println(this.bind(JptJaxbCoreMessages.NOT_FOUND, className)); + System.err.println(Tools.bind(JptJaxbCoreMessages.NOT_FOUND, className)); } } System.out.flush(); @@ -139,21 +138,13 @@ public class Main System.err.println(message); } else if(linkedEx != null && linkedEx instanceof ClassNotFoundException) { - String errorMessage = this.bind(JptJaxbCoreMessages.CONTEXT_FACTORY_NOT_FOUND, linkedEx.getMessage()); + String errorMessage = Tools.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 ********** @@ -216,26 +207,60 @@ public class Main class JptSchemaOutputResolver extends SchemaOutputResolver { - private final String targetSchemaName; + private final String defaultSchemaName; - protected JptSchemaOutputResolver(String targetSchemaName) { - this.targetSchemaName = targetSchemaName; + protected JptSchemaOutputResolver(String defaultSchemaName) { + this.defaultSchemaName = defaultSchemaName; } @Override public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException { - String canonicalName = this.canonicalFileName(this.targetSchemaName); - File file = new File(canonicalName); + + String filePath = (Tools.stringIsEmpty(namespaceURI)) ? + this.buildFileNameFrom(this.defaultSchemaName, suggestedFileName) : + this.modifyFileName(namespaceURI); + + filePath = this.canonicalFileName(filePath); + File file = new File(filePath); StreamResult result = new StreamResult(file); result.setSystemId(file.toURL().toExternalForm()); + + System.out.print(Tools.bind(JptJaxbCoreMessages.SCHEMA_GENERATED, file)); return result; } - public String canonicalFileName(String fileName) { - return canonicalFile(new File(fileName)).getAbsolutePath(); + private String buildFileNameFrom(String fileName, String suggestedFileName) { + + fileName = Tools.stripExtension(fileName); + + if(Tools.stringIsEmpty(fileName)) { + return suggestedFileName; + } + + String number = Tools.extractFileNumber(suggestedFileName); + number = Tools.appendXsdExtension(number); + + return fileName + number; + } + + private String modifyFileName(String namespaceURI) throws IOException { + + String dir = Tools.extractDirectory(this.defaultSchemaName); + + String fileName = Tools.stripProtocol(namespaceURI); + fileName = fileName.replaceAll("/", "_"); //$NON-NLS-1$ + fileName = Tools.appendXsdExtension(fileName); + + String result = (Tools.stringIsEmpty(dir)) ? fileName : dir + File.separator + fileName; + + return result; + } + + private String canonicalFileName(String fileName) { + return this.canonicalFile(new File(fileName)).getAbsolutePath(); } - public File canonicalFile(File file) { + private File canonicalFile(File file) { try { return file.getCanonicalFile(); } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/Tools.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/Tools.java new file mode 100644 index 0000000000..6427661dcd --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/Tools.java @@ -0,0 +1,104 @@ +/******************************************************************************* +* 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.io.File; +import java.text.MessageFormat; + +/** + * Tools + */ +public final class Tools +{ + /** default file name used by the schemagen */ + static public String GEN_DEFAULT_NAME = "schema"; //$NON-NLS-1$ + + /** empty string */ + public static final String EMPTY_STRING = ""; //$NON-NLS-1$ + + // ********** queries ********** + + /** + * Return whether the specified string is null, empty, or contains + * only whitespace characters. + */ + public static boolean stringIsEmpty(String string) { + if (string == null) { + return true; + } + int len = string.length(); + if (len == 0) { + return true; + } + return stringIsEmpty_(string.toCharArray(), len); + } + + private static boolean stringIsEmpty_(char[] s, int len) { + for (int i = len; i-- > 0; ) { + if ( ! Character.isWhitespace(s[i])) { + return false; + } + } + return true; + } + + // ********** short name manipulation ********** + + /** + * Strip the extension from the specified file name + * and return the result. If the file name has no + * extension, it is returned unchanged + * File#basePath() + */ + public static String stripExtension(String fileName) { + int index = fileName.lastIndexOf('.'); + if (index == -1) { + return fileName; + } + return fileName.substring(0, index); + } + + public static String stripProtocol(String uri) { + + return uri.replaceFirst("http://", EMPTY_STRING); + } + + + public static String appendXsdExtension(String name) { + + return name + ".xsd"; //$NON-NLS-1$ + } + + public static String extractFileNumber(String fileName) { + + String result = stripExtension(fileName); + if(Tools.stringIsEmpty(result)) { + return EMPTY_STRING; + } + return result.replaceFirst(GEN_DEFAULT_NAME, EMPTY_STRING); + } + + public static String extractDirectory(String path) { + if( ! path.contains(File.separator)) { + return EMPTY_STRING; + } + return path.substring(0, path.lastIndexOf(File.separator)); + } + + // ********** NLS utilities ********** + + public static String getString(String key) { + return JptJaxbCoreMessages.getString(key); + } + + public static String bind(String key, Object argument) { + return MessageFormat.format(getString(key), argument); + } +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/Main.java b/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/Main.java index 3bcf4eac26..2d706c2376 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/Main.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/Main.java @@ -11,7 +11,6 @@ package org.eclipse.jpt.jaxb.eclipselink.core.schemagen; import java.io.File; import java.io.IOException; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -22,6 +21,7 @@ import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; import org.eclipse.jpt.jaxb.eclipselink.core.schemagen.internal.JptEclipseLinkJaxbCoreMessages; +import org.eclipse.jpt.jaxb.eclipselink.core.schemagen.internal.Tools; import org.eclipse.persistence.jaxb.JAXBContext; import org.eclipse.persistence.jaxb.JAXBContextFactory; @@ -80,14 +80,13 @@ public class Main if(jaxbContext != null) { 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); + else { + Tools.bind(JptEclipseLinkJaxbCoreMessages.SCHEMA_NOT_CREATED, this.targetSchemaName); + } } private JAXBContext buildJaxbContext() { - System.out.println(this.getString(JptEclipseLinkJaxbCoreMessages.LOADING_CLASSES)); + System.out.println(Tools.getString(JptEclipseLinkJaxbCoreMessages.LOADING_CLASSES)); JAXBContext jaxbContext = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); @@ -108,7 +107,7 @@ public class Main } private void generateSchema(JAXBContext jaxbContext) { - System.out.println(this.getString(JptEclipseLinkJaxbCoreMessages.GENERATING_SCHEMA)); + System.out.println(Tools.getString(JptEclipseLinkJaxbCoreMessages.GENERATING_SCHEMA)); System.out.flush(); SchemaOutputResolver schemaOutputResolver = @@ -131,7 +130,7 @@ public class Main System.out.println(className); } catch (ClassNotFoundException e) { - System.err.println(this.bind(JptEclipseLinkJaxbCoreMessages.NOT_FOUND, className)); + System.err.println(Tools.bind(JptEclipseLinkJaxbCoreMessages.NOT_FOUND, className)); } } System.out.flush(); @@ -145,7 +144,7 @@ public class Main System.err.println(message); } else if(linkedEx != null && linkedEx instanceof ClassNotFoundException) { - String errorMessage = this.bind( + String errorMessage = Tools.bind( JptEclipseLinkJaxbCoreMessages.CONTEXT_FACTORY_NOT_FOUND, linkedEx.getMessage()); System.err.println(errorMessage); } @@ -157,20 +156,12 @@ public class Main 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)); + System.err.println(Tools.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 ********** @@ -233,18 +224,52 @@ public class Main class JptSchemaOutputResolver extends SchemaOutputResolver { - private final String targetSchemaName; + private String defaultSchemaName; - protected JptSchemaOutputResolver(String targetSchemaName) { - this.targetSchemaName = targetSchemaName; + protected JptSchemaOutputResolver(String defaultSchemaName) { + this.defaultSchemaName = defaultSchemaName; } - + @Override public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException { - File file = new File(this.targetSchemaName ); + String filePath = (Tools.stringIsEmpty(namespaceURI)) ? + this.buildFileNameFrom(this.defaultSchemaName, suggestedFileName) : + this.modifyFileName(namespaceURI); + + File file = new File(filePath); StreamResult result = new StreamResult(file); result.setSystemId(file.toURI().toURL().toString()); + + System.out.print(Tools.bind(JptEclipseLinkJaxbCoreMessages.SCHEMA_GENERATED, file)); return result; } + + private String buildFileNameFrom(String fileName, String suggestedFileName) { + + fileName = Tools.stripExtension(fileName); + + if(Tools.stringIsEmpty(fileName)) { + return suggestedFileName; + } + + String number = Tools.extractFileNumber(suggestedFileName); + number = Tools.appendXsdExtension(number); + + return fileName + number; + } + + private String modifyFileName(String namespaceURI) throws IOException { + + String dir = Tools.extractDirectory(this.defaultSchemaName); + + String fileName = Tools.stripProtocol(namespaceURI); + fileName = fileName.replaceAll("/", "_"); //$NON-NLS-1$ + fileName = Tools.appendXsdExtension(fileName); + + String result = (Tools.stringIsEmpty(dir)) ? fileName : dir + File.separator + fileName; + + return result; + } + } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/internal/Tools.java b/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/internal/Tools.java new file mode 100644 index 0000000000..5a32e4c09c --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.eclipselink.core.schemagen/src/org/eclipse/jpt/jaxb/eclipselink/core/schemagen/internal/Tools.java @@ -0,0 +1,104 @@ +/******************************************************************************* +* 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.eclipselink.core.schemagen.internal; + +import java.io.File; +import java.text.MessageFormat; + +/** + * Tools + */ +public final class Tools +{ + /** default file name used by the schemagen */ + static public String GEN_DEFAULT_NAME = "schema"; //$NON-NLS-1$ + + /** empty string */ + public static final String EMPTY_STRING = ""; //$NON-NLS-1$ + + // ********** queries ********** + + /** + * Return whether the specified string is null, empty, or contains + * only whitespace characters. + */ + public static boolean stringIsEmpty(String string) { + if (string == null) { + return true; + } + int len = string.length(); + if (len == 0) { + return true; + } + return stringIsEmpty_(string.toCharArray(), len); + } + + private static boolean stringIsEmpty_(char[] s, int len) { + for (int i = len; i-- > 0; ) { + if ( ! Character.isWhitespace(s[i])) { + return false; + } + } + return true; + } + + // ********** short name manipulation ********** + + /** + * Strip the extension from the specified file name + * and return the result. If the file name has no + * extension, it is returned unchanged + * File#basePath() + */ + public static String stripExtension(String fileName) { + int index = fileName.lastIndexOf('.'); + if (index == -1) { + return fileName; + } + return fileName.substring(0, index); + } + + public static String stripProtocol(String uri) { + + return uri.replaceFirst("http://", EMPTY_STRING); + } + + + public static String appendXsdExtension(String name) { + + return name + ".xsd"; //$NON-NLS-1$ + } + + public static String extractFileNumber(String fileName) { + + String result = stripExtension(fileName); + if(Tools.stringIsEmpty(result)) { + return EMPTY_STRING; + } + return result.replaceFirst(GEN_DEFAULT_NAME, EMPTY_STRING); + } + + public static String extractDirectory(String path) { + if( ! path.contains(File.separator)) { + return EMPTY_STRING; + } + return path.substring(0, path.lastIndexOf(File.separator)); + } + + // ********** NLS utilities ********** + + public static String getString(String key) { + return JptEclipseLinkJaxbCoreMessages.getString(key); + } + + public static String bind(String key, Object argument) { + return MessageFormat.format(getString(key), argument); + } +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF index 5004e1c329..5a652de563 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF @@ -12,8 +12,8 @@ Bundle-RequiredExecutionEnvironment: J2SE-1.5 Require-Bundle: org.eclipse.debug.core;bundle-version="[3.4.0,4.0.0)", org.eclipse.jdt.core;bundle-version="[3.4.0,4.0.0)", org.eclipse.jdt.ui;bundle-version="[3.4.0,4.0.0)", - org.eclipse.jpt.jaxb.core;bundle-version="[1.0.0,2.0.0)", org.eclipse.jpt.core;bundle-version="[2.3.0,3.0.0)", + org.eclipse.jpt.jaxb.core;bundle-version="[1.0.0,2.0.0)", org.eclipse.jpt.ui;bundle-version="[2.0.0,3.0.0)", org.eclipse.jpt.utility;bundle-version="[1.2.0,2.0.0)", org.eclipse.jst.common.project.facet.core;bundle-version="[1.4.200,2.0.0)", diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/NewSchemaFileWizardPage.java b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/NewSchemaFileWizardPage.java index aa53811e71..abe7e0c3d2 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/NewSchemaFileWizardPage.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/NewSchemaFileWizardPage.java @@ -9,7 +9,10 @@ *******************************************************************************/ package org.eclipse.jpt.jaxb.ui.internal.wizards.schemagen; -import static org.eclipse.jpt.core.internal.operations.JpaFileCreationDataModelProperties.*; +import static org.eclipse.jpt.core.internal.operations.JpaFileCreationDataModelProperties.CONTAINER_PATH; +import static org.eclipse.jpt.core.internal.operations.JpaFileCreationDataModelProperties.FILE_NAME; +import static org.eclipse.jpt.core.internal.operations.JpaFileCreationDataModelProperties.PROJECT; + import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; @@ -22,6 +25,7 @@ import org.eclipse.jpt.jaxb.ui.internal.JptJaxbUiMessages; import org.eclipse.jpt.utility.internal.StringTools; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.dialogs.WizardNewFileCreationPage; +import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; /** @@ -65,6 +69,7 @@ public class NewSchemaFileWizardPage extends WizardNewFileCreationPage { public void createControl(Composite parent) { super.createControl(parent); + this.setAllowExistingResources(true); this.setFileName(DEFAULT_SCHEMA_NAME); } @@ -90,7 +95,8 @@ public class NewSchemaFileWizardPage extends WizardNewFileCreationPage { if( ! valid) { return valid; } - + this.overrideFileExistsWarning(); + valid = this.projectIsJavaProject(this.getProject()); if( ! valid) { this.setErrorMessage(JptJaxbUiMessages.NewSchemaFileWizardPage_errorNotJavaProject); @@ -142,4 +148,15 @@ public class NewSchemaFileWizardPage extends WizardNewFileCreationPage { return containerName; } + private void overrideFileExistsWarning() { + String existsString= IDEWorkbenchMessages.ResourceGroup_nameExists; + existsString.toString(); + + existsString = existsString.substring("''{0}''".length(), existsString.length()); //$NON-NLS-1$ + String message = this.getMessage(); + if(message != null && message.endsWith(existsString)) { + this.setMessage(null); + } + } + } -- cgit v1.2.3