diff options
author | tle | 2010-10-28 04:10:07 +0000 |
---|---|---|
committer | tle | 2010-10-28 04:10:07 +0000 |
commit | eb91ff4a03a18a0e5767c813a6322d3b4358be21 (patch) | |
tree | f14bd428f9a5d8e176e6dcfb36f9b1763e00a2a3 /jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen | |
parent | f237f3d1528d4c52608ed6219920b1b105ccaefd (diff) | |
download | webtools.dali-eb91ff4a03a18a0e5767c813a6322d3b4358be21.tar.gz webtools.dali-eb91ff4a03a18a0e5767c813a6322d3b4358be21.tar.xz webtools.dali-eb91ff4a03a18a0e5767c813a6322d3b4358be21.zip |
327151 - Schema gen doesn't handle multiple namespace
Diffstat (limited to 'jaxb/plugins/org.eclipse.jpt.jaxb.core.schemagen')
2 files changed, 154 insertions, 25 deletions
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); + } +} |