Skip to content

Commit

Permalink
Refactor and merge util classes in legacy plugin
Browse files Browse the repository at this point in the history
* org.teiid.designer.legacy includes util classes duplicated in
  org.teiid.core.designer.

* Moves classes to core plugin and merges duplicate classes
  • Loading branch information
Paul Richardson committed Nov 15, 2012
1 parent a6df02c commit 18b161d
Show file tree
Hide file tree
Showing 27 changed files with 182 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* MetaMatrix, Inc - repackaging and updates for use as a metadata store
*******************************************************************************/

package org.teiid.designer.core.util;
package org.teiid.core.designer.util;

/**
* This class is a collection of helper methods to manipulate char arrays.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
*/
package org.teiid.core.designer.util;

import java.io.BufferedReader;
import java.io.File;

import org.teiid.core.util.FileUtils.Constants;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import org.teiid.core.designer.TeiidDesignerRuntimeException;
import org.teiid.core.designer.util.FileUtils.Constants;

/**
* @since 8.0
Expand Down Expand Up @@ -104,6 +110,64 @@ public static String getExtension( String theFileName ) {

return result;
}

/**
* Read a file, extract its contents, ensuring the file reader is closed.
*
* @param file
*
* @return contents of file as a {@link String}
*
* @throws FileNotFoundException
*/
public static String readSafe(File file) throws FileNotFoundException {
String result;
FileReader reader = null;
try {
reader = new FileReader(file);
result = read(reader);
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
}
}
}

return result;
}

/**
* Read the given {@link Reader} and return its contents
* as a {@link String}
*
* @param reader
*
* @return contents as a {@link String}
*/
public static String read(Reader reader) {
StringWriter writer = new StringWriter();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(reader);
while (bufferedReader.ready()) {
String line = bufferedReader.readLine();
writer.write(line);
writer.write(CoreStringUtil.LINE_SEPARATOR);
}
} catch (IOException e) {
throw new TeiidDesignerRuntimeException(e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception e) {
}
}
}
return writer.toString();
}

/**
* Prevents instantiation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.core.util;
package org.teiid.core.designer.util;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
*/
public abstract class ResourceNameUtil {
private static final String INVALID_EXTENSION_ERROR_ID = "ResourceNameUtil.invalidFileExtensionError"; //$NON-NLS-1$
private static final String COMMA_SPACE = ", "; //$NON-NLS-1$

// STATIC RESERVED NAME CONSTANTS
public static final String XMI_FILE_EXTENSION = "xmi"; //$NON-NLS-1$
public static final String VDB_FILE_EXTENSION = "vdb"; //$NON-NLS-1$
public static final String XSD_FILE_EXTENSION = "xsd"; //$NON-NLS-1$
public static final String XML_FILE_EXTENSION = "xml"; //$NON-NLS-1$
public static final String WSDL_FILE_EXTENSION = "wsdl"; //$NON-NLS-1$
public static final String MED_FILE_EXTENSION = "mxd"; //$NON-NLS-1$

public static final String DOT_XMI_FILE_EXTENSION = ".xmi"; //$NON-NLS-1$
public static final String DOT_VDB_FILE_EXTENSION = ".vdb"; //$NON-NLS-1$
public static final String DOT_XSD_FILE_EXTENSION = ".xsd"; //$NON-NLS-1$
public static final String DOT_XML_FILE_EXTENSION = ".xml"; //$NON-NLS-1$
public static final String DOT_WSDL_FILE_EXTENSION = ".wsdl"; //$NON-NLS-1$

public static final String ADMIN_NAME = "Admin"; //$NON-NLS-1$
public static final String BUILTINDATATYPES_NAME = "builtInDataTypes"; //$NON-NLS-1$
Expand Down Expand Up @@ -59,6 +63,8 @@ public abstract class ResourceNameUtil {
public static final String SYSTEM_NAME = "System"; //$NON-NLS-1$
public static final String SYSTEMSCHEMA_NAME = "SystemSchema"; //$NON-NLS-1$
public static final String SYSTEMVIRTUALDATABASE_NAME = "SystemVirtualDatabase"; //$NON-NLS-1$
public static final String SYSTEMODBCMODEL = "System.ODBC"; //$NON-NLS-1$
public static final String TRANSFORMATION_NAME = "Transformation"; //$NON-NLS-1$
public static final String UML2_NAME = "Uml2"; //$NON-NLS-1$
public static final String WEBSERVICE_NAME = "Webservice"; //$NON-NLS-1$
public static final String WSDL1_1_NAME = "WSDL1_1"; //$NON-NLS-1$
Expand All @@ -70,6 +76,8 @@ public abstract class ResourceNameUtil {

public static final String[] RESERVED_VDB_NAMES = {ADMIN_NAME, HELP_NAME, SYSTEM_NAME, SYSTEMVIRTUALDATABASE_NAME,};

public static final String USERFILES_FOLDERNAME = "user-files"; //$NON-NLS-1$

public static final String[] RESERVED_XMI_NAMES = {CORE_NAME, BUILTINRELATIONALTYPES_NAME, DATAACCESS_NAME,
DATASERVICESYSTEMMODEL_NAME, DTCBASE_NAME, ECORE_NAME, ENTERPRISEDATATYPES_NAME, EXTENSION_NAME, FUNCTION_NAME,
JDBC_NAME, JDBCMODEL_NAME, JDBCSYSTEM_NAME, MANIFEST_NAME, MAPPING_NAME, MBR_NAME, METAMODELRELATIONALMODEL_NAME,
Expand All @@ -79,6 +87,8 @@ public abstract class ResourceNameUtil {
public static final String[] RESERVED_XSD_NAMES = {BUILTINDATATYPES_NAME, ENTERPRISEDATATYPES_NAME, MAGICXMLSCHEMA_NAME,
NAMESPACE_NAME, SIMPLEDATATYPES_INSTANCE_NAME, SYSTEMSCHEMA_NAME, XML_NAME, XMLSCHEMA_NAME, XMLSCHEMA_INSTANCE_NAME,};

public static final String[] RESERVED_PROJECT_NAMES = {USERFILES_FOLDERNAME,};

/**
* This method checks whether or not a proposed vdb name is reserved or not. It will return false if the proposed name
* includes an extension AND one or more "." characters.
Expand Down Expand Up @@ -207,4 +217,76 @@ public static boolean isReservedSchemaName( String proposedName ) throws Illegal

return false;
}

/**
* This method checks whether or not a proposed project name is reserved or not.
*
* @param proposedName may or may not include the file extension
* @return true if it is reserved, false if not.
* @since 5.5.3
*/
public static boolean isReservedProjectName( final String proposedName ) {
if (proposedName == null || proposedName.length() <= 0) return false;

for (final String element : RESERVED_PROJECT_NAMES)
if (proposedName.equalsIgnoreCase(element)) return true;

return false;
}

/**
* This method checks whether or not a proposed name is reserved or not. It will check all reserved resource names including
* vdb, xmi and xsd resources. It will return false if the proposed name includes an extension AND one or more "." characters.
*
* @param proposedName may or may not inlude the file extension
* @return true if it is reserved, false if not.
* @throws IllegalArgumentException if proposed name contains an apparent file extension (one or more '.' characters) and it is
* NOT a ".xmi, .xsd, or .vdb" extension
* @since 5.0
*/
public static boolean isReservedResourceName( String proposedName ) throws IllegalArgumentException {
boolean result = false;

if (proposedName == null || proposedName.length() <= 0) return false;

// Check the extension
if (proposedName.indexOf('.') != -1) {
// Check ends with
if (!proposedName.endsWith(DOT_XSD_FILE_EXTENSION) && !proposedName.endsWith(DOT_XMI_FILE_EXTENSION)
&& !proposedName.endsWith(DOT_VDB_FILE_EXTENSION)) {
final String allExtensions = XMI_FILE_EXTENSION + COMMA_SPACE + XSD_FILE_EXTENSION + COMMA_SPACE
+ VDB_FILE_EXTENSION;
throw new IllegalArgumentException(CoreModelerPlugin.Util.getString(INVALID_EXTENSION_ERROR_ID,
proposedName,
allExtensions));
}

// So, let's take the extension off
if (proposedName.endsWith(DOT_XSD_FILE_EXTENSION)) {
proposedName = proposedName.substring(0, proposedName.lastIndexOf(DOT_XSD_FILE_EXTENSION));
if (proposedName.indexOf('.') != -1) result = false;
else result = isReservedSchemaName(proposedName);
}

if (!result) if (proposedName.endsWith(DOT_XMI_FILE_EXTENSION)) {
proposedName = proposedName.substring(0, proposedName.lastIndexOf(DOT_XMI_FILE_EXTENSION));
if (proposedName.indexOf('.') != -1) result = false;
else result = isReservedModelName(proposedName);
}

if (!result) if (proposedName.endsWith(DOT_VDB_FILE_EXTENSION)) {
proposedName = proposedName.substring(0, proposedName.lastIndexOf(DOT_VDB_FILE_EXTENSION));
if (proposedName.indexOf('.') != -1) result = false;
else result = isReservedVdbName(proposedName);
}
} else {
result = isReservedSchemaName(proposedName);

if (!result) result = isReservedModelName(proposedName);

if (!result) result = isReservedVdbName(proposedName);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/

package org.teiid.designer.core.util;
package org.teiid.core.designer.util;

import java.io.File;
import java.util.Random;

import org.teiid.core.designer.util.FileUtils;
import org.teiid.designer.common.util.LogConstants;
import org.teiid.logging.LogManager;
import org.eclipse.core.runtime.IStatus;
import org.teiid.core.designer.CoreModelerPlugin;

/**
* Creates and deletes temporary directories.
Expand Down Expand Up @@ -97,7 +95,7 @@ public static synchronized TempDirectory getTempDirectory( String parentDirector

TempDirectory tempDirectory = new TempDirectory(absolutePath, System.currentTimeMillis(), RANDOM.nextLong());
while (new File(tempDirectory.getPath()).exists()) {
LogManager.logInfo(LogConstants.CTX_CONFIG,
CoreModelerPlugin.Util.log(IStatus.INFO,
"Temporary Folder " + tempDirectory.getPath() + " already exists; Creating new folder..."); //$NON-NLS-1$ //$NON-NLS-2$
try {
Thread.sleep(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/

package org.teiid.designer.core.util;
package org.teiid.core.designer.util;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
import org.teiid.core.designer.CoreModelerPlugin;
import org.teiid.core.designer.I18n;
import org.teiid.core.designer.exception.EmptyArgumentException;
import org.teiid.core.designer.util.FileUtil;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.util.FileUtil;
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelUtil;
Expand Down Expand Up @@ -416,7 +416,8 @@ void ddlFileModified() {
generateModelName = true;
}
try {
ddlFileContentsBox.setText(new FileUtil(ddlFileName).readSafe());
File ddlFile = new File(ddlFileName);
ddlFileContentsBox.setText(FileUtil.readSafe(ddlFile));
} catch (final IOException error) {
throw CoreModelerPlugin.toRuntimeException(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import org.eclipse.xsd.util.XSDParser;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.FileUtils;
import org.teiid.core.designer.util.TempDirectory;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.types.DatatypeConstants;
import org.teiid.designer.core.util.TempDirectory;
import org.teiid.designer.core.workspace.ModelUtil;
import org.teiid.designer.runtime.ui.wizards.webservices.WarDeploymentInfoPanel;
import org.teiid.designer.sdt.ModelerSdtPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.eclipse.core.runtime.Status;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.FileUtils;
import org.teiid.designer.core.util.TempDirectory;
import org.teiid.core.designer.util.TempDirectory;
import org.teiid.designer.webservice.WebServicePlugin;
import org.teiid.designer.webservice.util.AntTasks;

Expand Down
1 change: 0 additions & 1 deletion plugins/org.teiid.designer.legacy/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Export-Package:
org.teiid.designer.core.index,
org.teiid.designer.core.log,
org.teiid.designer.core.types,
org.teiid.designer.core.util,
org.teiid.designer.core.workspace,
org.teiid.designer.metadata.runtime,
org.teiid.designer.metadata.runtime.api,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.designer.common.util.ErrorMessageKeys;
import org.teiid.designer.common.util.I18nUtil;

/**
* The BaseID class serves as the abstract base class for identifiers of objects. This class provides the method signatures common
Expand Down Expand Up @@ -80,10 +79,10 @@ public BaseID( String fullName ) {
protected BaseID( String fullName,
int checkLevel ) {
if (fullName == null) {
throw new IllegalArgumentException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0001));
throw new IllegalArgumentException(ErrorMessageKeys.NAMEDOBJECT_ERR_0001);
}
if (fullName.trim().length() == 0) {
throw new IllegalArgumentException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0002));
throw new IllegalArgumentException(ErrorMessageKeys.NAMEDOBJECT_ERR_0002);
}
this.fullName = fullName;
this.updateHashCode();
Expand Down Expand Up @@ -210,7 +209,7 @@ public boolean equals( Object obj ) {
public int compareTo( Object obj ) {
BaseID that = (BaseID)obj; // May throw ClassCastException
if (obj == null) {
throw new IllegalArgumentException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0003));
throw new IllegalArgumentException(ErrorMessageKeys.NAMEDOBJECT_ERR_0003);
}

int diff = this.hashCode() - that.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
package org.teiid.designer.common.namedobject;

import java.io.Serializable;

import org.teiid.core.designer.TeiidDesignerRuntimeException;
import org.teiid.designer.common.util.ErrorMessageKeys;
import org.teiid.designer.common.util.I18nUtil;

/**
* This class represents the basic implementation of MetadataObject, which is the foundation for all classes that are used to
Expand Down Expand Up @@ -63,7 +61,7 @@ public abstract class BasicObject implements BaseObject, Serializable {
*/
protected BasicObject( BaseID id ) {
if (id == null) {
throw new IllegalArgumentException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0004));
throw new IllegalArgumentException(ErrorMessageKeys.NAMEDOBJECT_ERR_0004);
}
this.id = id;
}
Expand Down Expand Up @@ -181,7 +179,7 @@ public int compareTo( Object obj ) {
}

// Otherwise not comparable ...
throw new ClassCastException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0005, obj.getClass(), this.getClass()));
throw new ClassCastException(ErrorMessageKeys.NAMEDOBJECT_ERR_0005);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

import org.teiid.designer.common.util.ErrorMessageKeys;
import org.teiid.designer.common.util.I18nUtil;

/**
* This class contains several helper methods that check the validity of BaseID instances.
Expand Down Expand Up @@ -81,7 +79,7 @@ public class IDVerifier {
*/
public static boolean isValid( BaseID id ) {
if (id == null) {
throw new IllegalArgumentException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0006));
throw new IllegalArgumentException(ErrorMessageKeys.NAMEDOBJECT_ERR_0006);
}
return IDVerifier.performCheck(id.getFullName()) == NONE;
}
Expand All @@ -98,7 +96,7 @@ public static boolean isValid( BaseID id ) {
*/
public static int performCheck( BaseID id ) {
if (id == null) {
throw new IllegalArgumentException(I18nUtil.getString(ErrorMessageKeys.NAMEDOBJECT_ERR_0007));
throw new IllegalArgumentException(ErrorMessageKeys.NAMEDOBJECT_ERR_0007);
}
return IDVerifier.performCheck(id.getFullName());
}
Expand Down

0 comments on commit 18b161d

Please sign in to comment.