Re: eogen file on Windows

From: Henrique Prange (hprang..mail.com)
Date: Sat Jan 13 2007 - 17:27:51 EST

  • Next message: Lachlan Deck: "Re: [OS-JIRA] Created: (WOL-359) Add option for using relative paths within the EOGenerator editor"

    Hi everybody,

    I've done some fixes on EOGenerator plug-in for Windows users to solve
    the following issues:

    (WOL-361) Added models using EOGenerator editor have a wrong "C:" prefix
    on Windows.
    (WOL-362) EOGeneration doesn't work on Windows.

    The patch is attached in the e-mail.

    I've also included the support to variables for EOGenerator path and
    templates folder. Now, during the eogen file creation, EOGenerator
    editor writes ${eogenerator_path} and ${eogenerator_template_dir} by
    default as values for these properties. These variables are replaced by
    the values in WOLips preferences at execution time.

    I don't know if this resolves the problem described on issue WOL-359
    (Add option for using relative paths within the EOGenerator editor).
    Maybe it can solve part of the problem. I also think we could use
    variables to define the path of models outside the project that must be
    referenced.

    I've created issues on JIRA just for documentation purposes.

    Cheers,

    Henrique

    PS.: I've tested the modifications only on Windows.

    Mike Schrag wrote:
    > I don't have a Windows box with Eclipse on it, but if anyone wants to
    > take a stab at fixing this, it's probably pretty easy ...
    >
    > On Jan 8, 2007, at 1:17 PM, Henrique Prange wrote:
    >
    >> Hi John,
    >>
    >> It doesn't work on Windows.
    >>
    >> You can correct the eogen file using text editor, copy and paste, and
    >> run via command line.
    >>
    >> Or you can add a builder to your project that call EOGenerator with
    >> the specified parameters (seems to be the best solution at the moment
    >> for Windows users).
    >>
    >> Cheers,
    >>
    >> Hentique
    >>
    >> John Huss wrote:
    >>> I'm trying to use eogenerator within WOLips on Windows. It shows the
    >>> path to the EOModel incorrectly like:
    >>> C:Resources/Test.eomodeld
    >>> instead of
    >>> Resources/Test.eomodeld
    >>> So eogenerator is not generating any files. Any help would be greatly
    >>> appreciated!
    >>> John
    >>> ------------------------------------------------------------------------
    >>
    >> --
    >> \o/ Henrique Prange, Moleque de Idéias Educação e Tecnologia Ltda
    >> | Phone: 55-21-2710-0178 E-mail: hprang..oleque.com.br
    >> / \ http://www.moleque.com.br
    >
    >
    >

    -- 
    

    \o/ Henrique Prange, Moleque de Idéias Educação e Tecnologia Ltda | Phone: 55-21-2710-0178 E-mail: hprang..oleque.com.br / \ http://www.moleque.com.br

    Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/CommandLineTokenizer.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/CommandLineTokenizer.java (revision 3744) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/CommandLineTokenizer.java (working copy) ..-67,11 +67,6 @@ reset(); } - public void reset() { - myState = 1; - myIterator.first(); - } - public boolean hasMoreElements() { return hasMoreTokens(); } ..-94,10 +89,14 @@ public String nextToken() throws ParseException { boolean escapeNext = false; boolean wasQuoted = myWasQuoted; + // 1 = Whitespace, 2 = Text, 3 = Quoted; StringBuffer token = new StringBuffer(); + char c = myIterator.current(); + boolean done = false; + while (!done && c != CharacterIterator.DONE) { if (escapeNext) { switch (c) { ..-132,7 +131,7 @@ case '\\': escapeNext = true; - c = myIterator.next(); + c = '\\'; break; default: ..-154,13 +153,14 @@ break; // case '\"': - // throw new ParseException("Unexpected quote '\"' in + // throw new ParseException("Unexpected quote '\"' + // in // string.", // myIterator.getIndex()); case '\\': escapeNext = true; - c = myIterator.next(); + c = '\\'; break; default: ..-179,7 +179,7 @@ case '\\': escapeNext = true; - c = myIterator.next(); + c = '\\'; break; default: ..-198,4 +198,9 @@ return token.toString(); } + + public void reset() { + myState = 1; + myIterator.first(); + } } Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/CommandLineTokenizerTest.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/CommandLineTokenizerTest.java (revision 0) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/CommandLineTokenizerTest.java (revision 0) ..-0,0 +1,21 @@ +package org.objectstyle.wolips.eogenerator.model; + +import junit.framework.TestCase; + +/** + *..uthor <a href="mailto:hprange@moleque.com.br">Henrique Prange</a> + */ +public class CommandLineTokenizerTest extends TestCase { + + public void testCommandWithBackslash() throws Exception { + CommandLineTokenizer tokenizer = new CommandLineTokenizer("C:\\Test"); + + assertEquals("C:\\Test", tokenizer.nextToken()); + } + + public void testCommandWithSpace() throws Exception { + CommandLineTokenizer tokenizer = new CommandLineTokenizer("\"C:/Program Files/\""); + + assertEquals("C:/Program Files/", tokenizer.nextToken()); + } +} Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGenerateWorkspaceJob.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGenerateWorkspaceJob.java (revision 3744) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGenerateWorkspaceJob.java (working copy) ..-51,13 +51,11 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; -import org.eclipse.core.internal.resources.ResourceException; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; ..-69,6 +67,7 @@ import org.eclipse.core.runtime.Status; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; +import org.objectstyle.wolips.eogenerator.ui.Activator; import org.objectstyle.wolips.eogenerator.ui.dialogs.EOGeneratorResultsDialog; import org.objectstyle.wolips.preferences.Preferences; ..-85,18 +84,24 @@ public IStatus runInWorkspace(IProgressMonitor _monitor) throws CoreException { StringBuffer output = new StringBuffer(); + for (int eogenFileNum = 0; eogenFileNum < myEOGenFiles.length; eogenFileNum++) { try { setName("EOGenerating " + myEOGenFiles[eogenFileNum].getName() + " ..."); + EOGeneratorModel eogenModel = EOGeneratorModel.createModelFromFile(myEOGenFiles[eogenFileNum]); - String eogenFileContents = eogenModel.writeToString(Preferences.getEOGeneratorPath(), Preferences.getEOGeneratorTemplateDir(), Preferences.getEOGeneratorJavaTemplate(), Preferences.getEOGeneratorSubclassJavaTemplate()); + + String eogenFileContents = eogenModel.writeToDecodedString(Preferences.getEOGeneratorPath(), Preferences.getEOGeneratorTemplateDir()); + List commandsList = new LinkedList(); + CommandLineTokenizer tokenizer = new CommandLineTokenizer(eogenFileContents); + while (tokenizer.hasMoreTokens()) { commandsList.add(tokenizer.nextToken()); } String[] tokens = (String[]) commandsList.toArray(new String[commandsList.size()]); - if (!new File(eogenModel.getEOGeneratorPath(Preferences.getEOGeneratorPath())).exists()) { + if (!new File(tokens[0]).exists()) { output.append("You have either not set the path to your EOGenerator executable, or the current path is incorrect."); myShowResults = true; break; ..-135,7 +140,9 @@ project.getFolder(new Path(eogenModel.getDestination())).refreshLocal(IResource.DEPTH_INFINITE, _monitor); project.getFolder(new Path(eogenModel.getSubclassDestination())).refreshLocal(IResource.DEPTH_INFINITE, _monitor); } catch (Throwable t) { - throw new ResourceException(IStatus.ERROR, myEOGenFiles[eogenFileNum].getFullPath(), "Failed to generate.", t); + IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 40, "Cannot execute EOGenerator from file " + myEOGenFiles[eogenFileNum].getFullPath() + ".", t); + + throw new CoreException(status); } } ..-148,7 +155,7 @@ } }); } - + return new Status(IStatus.OK, org.objectstyle.wolips.eogenerator.ui.Activator.PLUGIN_ID, IStatus.OK, "Done", null); } } \ No newline at end of file Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGeneratorModel.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGeneratorModel.java (revision 3744) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGeneratorModel.java (working copy) ..-74,52 +74,125 @@ import org.objectstyle.wolips.preferences.Preferences; public class EOGeneratorModel { - private IProject myProject; + public static class Define { + private String myName; - private String myEOGeneratorPath; + private String myValue; - private List myModels; + public Define(String _name, String _value) { + myName = _name; + myValue = _value; + } - private List myRefModels; + public boolean equals(Object _obj) { + return (_obj instanceof Define && ((Define) _obj).myName.equals(myName)); + } - private String myDestination; + public String getName() { + return myName; + } - private String mySubclassDestination; + public String getValue() { + return myValue; + } - private String myTemplateDir; + public int hashCode() { + return myName.hashCode(); + } + } - private String myJavaTemplate; + public static final String EOGENERATOR_PATH_VARIABLE = "${eogenerator_path}"; - private String mySubclassJavaTemplate; + public static final String TEMPLATE_DIR_VARIABLE = "${eogenerator_template_dir}"; + public static EOGeneratorModel createDefaultModel(IProject _project) { + EOGeneratorModel model = new EOGeneratorModel(_project); + model.setJava(Boolean.TRUE); + model.setPackageDirs(Boolean.TRUE); + model.setVerbose(Boolean.TRUE); + // model.setEOGeneratorPath(Preferences.getEOGeneratorPath()); + model.setJavaTemplate(Preferences.getEOGeneratorJavaTemplate()); + // model.setTemplateDir(Preferences.getEOGeneratorTemplateDir()); + model.setSubclassJavaTemplate(Preferences.getEOGeneratorSubclassJavaTemplate()); + try { + IJavaProject javaProject = JavaCore.create(_project); + if (javaProject != null) { + IClasspathEntry[] classpathEntry = javaProject.getRawClasspath(); + for (int classpathEntryNum = 0; classpathEntryNum < classpathEntry.length; classpathEntryNum++) { + IClasspathEntry entry = classpathEntry[classpathEntryNum]; + if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { + IPath path = entry.getPath(); + if (path != null) { + IFolder sourceFolder = _project.getWorkspace().getRoot().getFolder(path); + IPath projectRelativePath = sourceFolder.getProjectRelativePath(); + String projectRelativePathStr = projectRelativePath.toPortableString(); + model.setDestination(projectRelativePathStr); + model.setSubclassDestination(projectRelativePathStr); + } + } + } + } + } catch (JavaModelException e) { + e.printStackTrace(); + } + return model; + } + + public static EOGeneratorModel createModelFromFile(IFile _file) throws ParseException, CoreException, IOException { + _file.refreshLocal(IResource.DEPTH_INFINITE, null); + InputStream eogenFileStream = _file.getContents(); + try { + StringBuffer sb = new StringBuffer(); + BufferedReader br = new BufferedReader(new InputStreamReader(eogenFileStream)); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + String string = sb.toString(); + string.replace('\\', '/'); + EOGeneratorModel model = new EOGeneratorModel(_file.getProject(), string); + return model; + } finally { + eogenFileStream.close(); + } + } + + private List myCustomSettings; + private List myDefines; - private Boolean myPackageDirs; + private String myDestination; + private boolean myDirty; + + private String myEOGeneratorPath; + + private String myFilenameTemplate; + private Boolean myJava; private Boolean myJavaClient; - private Boolean myVerbose; + private String myJavaTemplate; + private List myModels; + + private Boolean myPackageDirs; + private String myPrefix; - private String myFilenameTemplate; + private IProject myProject; - private List myCustomSettings; + private List myRefModels; - private boolean myDirty; + private String mySubclassDestination; - public EOGeneratorModel(IProject _project, String _lineInfo) throws ParseException { - this(_project); - readFromString(_lineInfo); - } + private String mySubclassJavaTemplate; - public EOGeneratorModel(IProject _project) { - this(); - myProject = _project; - } + private String myTemplateDir; + private Boolean myVerbose; + public EOGeneratorModel() { myModels = new LinkedList(); myRefModels = new LinkedList(); ..-127,60 +200,232 @@ myCustomSettings = new LinkedList(); } - public void writeToFile(IFile _file, IProgressMonitor _monitor) throws CoreException, IOException { - String eogenFileContents = writeToString(Preferences.getEOGeneratorPath(), Preferences.getEOGeneratorTemplateDir(), Preferences.getEOGeneratorJavaTemplate(), Preferences.getEOGeneratorSubclassJavaTemplate()); - InputStream stream = new ByteArrayInputStream(eogenFileContents.getBytes("UTF-8")); - if (_file.exists()) { - _file.setContents(stream, true, true, _monitor); + public EOGeneratorModel(IProject _project) { + this(); + myProject = _project; + } + + public EOGeneratorModel(IProject _project, String _lineInfo) throws ParseException { + this(_project); + readFromString(_lineInfo); + } + + public void addModel(EOModelReference _modelReference) { + myModels.add(_modelReference); + myDirty = true; + } + + public void addRefModel(EOModelReference _modelReference) { + myRefModels.add(_modelReference); + myDirty = true; + } + + protected void append(StringBuffer _buffer, String _name, Boolean _value) { + if (_value != null && _value.booleanValue()) { + _buffer.append(" "); + _buffer.append(_name); + } + } + + protected void append(StringBuffer _buffer, String _name, String _value) { + if (_value != null && _value.trim().length() > 0) { + _buffer.append(" "); + _buffer.append(_name); + _buffer.append(" "); + _buffer.append(escape(_value, true)); + } + } + + protected String escape(String _value, boolean _quotes) { + String value; + if (_value == null) { + value = null; + } else if (_value.indexOf(' ') == -1 && _value.trim().length() > 0) { + value = _value; + } else if (_quotes) { + StringBuffer valueBuffer = new StringBuffer(); + valueBuffer.append("\""); + valueBuffer.append(_value); + valueBuffer.append("\""); + value = valueBuffer.toString(); } else { - _file.create(stream, true, _monitor); + value = _value.replaceAll(" ", "\\ "); } - stream.close(); - setDirty(false); + return value; } - public String writeToString(String _defaultEOGeneratorPath, String _defaultTemplateDir, String _defaultJavaTemplate, String _defaultSubclassJavaTemplate) { - StringBuffer sb = new StringBuffer(); + public List getDefines() { + return myDefines; + } - sb.append(escape(getEOGeneratorPath(_defaultEOGeneratorPath), false)); + public String getDestination() { + return myDestination; + } - append(sb, "-destination", myDestination); - append(sb, "-filenameTemplate", myFilenameTemplate); - append(sb, "-java", myJava); - append(sb, "-javaclient", myJavaClient); - append(sb, "-javaTemplate", getJavaTemplate(_defaultJavaTemplate)); + public String getEOGeneratorPath() { + if (myEOGeneratorPath == null || myEOGeneratorPath.trim().length() == 0) { + myEOGeneratorPath = EOGENERATOR_PATH_VARIABLE; + } - Iterator modelsIter = myModels.iterator(); - while (modelsIter.hasNext()) { - EOModelReference model = (EOModelReference) modelsIter.next(); - append(sb, "-model", model.getPath(myProject)); + return myEOGeneratorPath; + } + + /** + *..eprecated Use the getter without parameter instead. + *..aram _default + *..eturn + */ + public String getEOGeneratorPath(String _default) { + String eoGeneratorPath = myEOGeneratorPath; + if (myEOGeneratorPath == null || myEOGeneratorPath.trim().length() == 0) { + eoGeneratorPath = _default; } + return eoGeneratorPath; + } - append(sb, "-packagedirs", myPackageDirs); - append(sb, "-prefix", myPrefix); + public String getFilenameTemplate() { + return myFilenameTemplate; + } - Iterator refModelsIter = myRefModels.iterator(); - while (refModelsIter.hasNext()) { - EOModelReference refModel = (EOModelReference) refModelsIter.next(); - append(sb, "-refmodel", refModel.getPath(myProject)); + public String getJavaTemplate() { + return myJavaTemplate; + } + + /** + *..eprecated + *..aram _default + *..eturn + */ + public String getJavaTemplate(String _default) { + String javaTemplate = myJavaTemplate; + if (myJavaTemplate == null || myJavaTemplate.trim().length() == 0) { + javaTemplate = _default; } + return javaTemplate; + } - append(sb, "-subclassDestination", mySubclassDestination); - append(sb, "-subclassJavaTemplate", getSubclassJavaTemplate(_defaultSubclassJavaTemplate)); - append(sb, "-templatedir", getTemplateDir(_defaultTemplateDir)); - append(sb, "-verbose", myVerbose); + public List getModels() { + return myModels; + } - Iterator definesIter = myDefines.iterator(); - while (definesIter.hasNext()) { - Define define = (Define) definesIter.next(); - String name = define.getName(); - String value = define.getValue(); - append(sb, "-define-" + name, value); + public String getPrefix() { + return myPrefix; + } + + public IProject getProject() { + return myProject; + } + + public String getQuotedEOGeneratorPath() { + return escape(getEOGeneratorPath(), true); + } + + public List getRefModels() { + return myRefModels; + } + + public String getSubclassDestination() { + return mySubclassDestination; + } + + public String getSubclassJavaTemplate() { + return mySubclassJavaTemplate; + } + + /** + *..eprecated + *..aram _default + *..eturn + */ + public String getSubclassJavaTemplate(String _default) { + String subclassJavaTemplate = mySubclassJavaTemplate; + if (mySubclassJavaTemplate == null || mySubclassJavaTemplate.trim().length() == 0) { + subclassJavaTemplate = _default; } + return subclassJavaTemplate; + } - return sb.toString(); + public String getTemplateDir() { + if (myTemplateDir == null || myTemplateDir.trim().length() == 0) { + myTemplateDir = "${eogenerator_template_dir}"; + } + return myTemplateDir; } + /** + *..eprecated Use the getter without parameter instead. + *..aram _default + *..eturn + */ + public String getTemplateDir(String _default) { + String templateDir = myTemplateDir; + if (myTemplateDir == null || myTemplateDir.trim().length() == 0) { + templateDir = _default; + } + if (templateDir != null) { + templateDir = PathUtils.getRelativePath(myProject, new Path(templateDir)); + } + return templateDir; + } + + public boolean isDirty() { + return myDirty; + } + + public Boolean isJava() { + return myJava; + } + + public Boolean isJavaClient() { + return myJavaClient; + } + + public boolean isModelReferenced(EOModelReference _modelReference) { + boolean modelReferenced = false; + String eomodelName = _modelReference.getName(); + Iterator modelsIter = myModels.iterator(); + while (!modelReferenced && modelsIter.hasNext()) { + EOModelReference model = (EOModelReference) modelsIter.next(); + modelReferenced = model.getName().equals(eomodelName); + } + if (!modelReferenced) { + Iterator refModelsIter = myRefModels.iterator(); + while (!modelReferenced && refModelsIter.hasNext()) { + EOModelReference model = (EOModelReference) refModelsIter.next(); + modelReferenced = model.getName().equals(eomodelName); + } + } + return modelReferenced; + } + + protected boolean isNew(String _oldValue, String _newValue) { + boolean isNew; + if (_oldValue == null && _newValue != null && _newValue.trim().length() > 0) { + isNew = true; + } else if (_oldValue != null && !_oldValue.equals(_newValue)) { + isNew = true; + } else { + isNew = false; + } + return isNew; + } + + public Boolean isPackageDirs() { + return myPackageDirs; + } + + public Boolean isVerbose() { + return myVerbose; + } + + protected String nextTokenValue(String _token, CommandLineTokenizer _tokenizer) throws ParseException { + if (!_tokenizer.hasMoreTokens()) { + throw new ParseException(_token + " must be followed by a value.", -1); + } + String token = _tokenizer.nextToken(); + return token; + } + public void readFromString(String _str) throws ParseException { myModels.clear(); myRefModels.clear(); ..-235,65 +480,11 @@ myDirty = false; } - protected void append(StringBuffer _buffer, String _name, Boolean _value) { - if (_value != null && _value.booleanValue()) { - _buffer.append(" "); - _buffer.append(_name); - } - } - - protected void append(StringBuffer _buffer, String _name, String _value) { - if (_value != null && _value.trim().length() > 0) { - _buffer.append(" "); - _buffer.append(_name); - _buffer.append(" "); - _buffer.append(escape(_value, true)); - } - } - - protected String escape(String _value, boolean _quotes) { - String value; - if (_value == null) { - value = null; - } else if (_value.indexOf(' ') == -1 && _value.trim().length() > 0) { - value = _value; - } else if (_quotes) { - StringBuffer valueBuffer = new StringBuffer(); - valueBuffer.append("\""); - valueBuffer.append(_value); - valueBuffer.append("\""); - value = valueBuffer.toString(); - } else { - value = _value.replaceAll(" ", "\\ "); - } - return value; - } - - protected String nextTokenValue(String _token, CommandLineTokenizer _tokenizer) throws ParseException { - if (!_tokenizer.hasMoreTokens()) { - throw new ParseException(_token + " must be followed by a value.", -1); - } - String token = _tokenizer.nextToken(); - return token; - } - - public IProject getProject() { - return myProject; - } - - public List getDefines() { - return myDefines; - } - public void setDefines(List _defines) { myDefines = _defines; myDirty = true; } - public String getDestination() { - return myDestination; - } - public void setDestination(String _destination) { if (isNew(myDestination, _destination)) { myDestination = _destination; ..-301,12 +492,8 @@ } } - public String getEOGeneratorPath(String _default) { - String eoGeneratorPath = myEOGeneratorPath; - if (myEOGeneratorPath == null || myEOGeneratorPath.trim().length() == 0) { - eoGeneratorPath = _default; - } - return eoGeneratorPath; + public void setDirty(boolean _dirty) { + myDirty = _dirty; } public void setEOGeneratorPath(String _generatorPath) { ..-320,30 +507,21 @@ } } - public Boolean isJavaClient() { - return myJavaClient; + public void setFilenameTemplate(String _filenameTemplate) { + if (isNew(myFilenameTemplate, _filenameTemplate)) { + myFilenameTemplate = _filenameTemplate; + myDirty = true; + } } - public void setJavaClient(Boolean _javaClient) { - myJavaClient = _javaClient; - myDirty = true; - } - - public Boolean isJava() { - return myJava; - } - public void setJava(Boolean _java) { myJava = _java; myDirty = true; } - public String getJavaTemplate(String _default) { - String javaTemplate = myJavaTemplate; - if (myJavaTemplate == null || myJavaTemplate.trim().length() == 0) { - javaTemplate = _default; - } - return javaTemplate; + public void setJavaClient(Boolean _javaClient) { + myJavaClient = _javaClient; + myDirty = true; } public void setJavaTemplate(String _javaTemplate) { ..-353,43 +531,21 @@ } } - protected boolean isNew(String _oldValue, String _newValue) { - boolean isNew; - if (_oldValue == null && _newValue != null && _newValue.trim().length() > 0) { - isNew = true; - } else if (_oldValue != null && !_oldValue.equals(_newValue)) { - isNew = true; - } else { - isNew = false; - } - return isNew; - } - - public List getModels() { - return myModels; - } - public void setModels(List _models) { myModels = _models; myDirty = true; } - public void addModel(EOModelReference _modelReference) { - myModels.add(_modelReference); - myDirty = true; - } - - public Boolean isPackageDirs() { - return myPackageDirs; - } - public void setPackageDirs(Boolean _packageDirs) { myPackageDirs = _packageDirs; myDirty = true; } - public List getRefModels() { - return myRefModels; + public void setPrefix(String _prefix) { + if (isNew(myPrefix, _prefix)) { + myPrefix = _prefix; + myDirty = true; + } } public void setRefModels(List _refModels) { ..-397,15 +553,6 @@ myDirty = true; } - public void addRefModel(EOModelReference _modelReference) { - myRefModels.add(_modelReference); - myDirty = true; - } - - public String getSubclassDestination() { - return mySubclassDestination; - } - public void setSubclassDestination(String _subclassDestination) { if (isNew(mySubclassDestination, _subclassDestination)) { mySubclassDestination = _subclassDestination; ..-413,14 +560,6 @@ } } - public String getSubclassJavaTemplate(String _default) { - String subclassJavaTemplate = mySubclassJavaTemplate; - if (mySubclassJavaTemplate == null || mySubclassJavaTemplate.trim().length() == 0) { - subclassJavaTemplate = _default; - } - return subclassJavaTemplate; - } - public void setSubclassJavaTemplate(String _subclassJavaTemplate) { if (isNew(mySubclassJavaTemplate, _subclassJavaTemplate)) { mySubclassJavaTemplate = _subclassJavaTemplate; ..-428,17 +567,6 @@ } } - public String getTemplateDir(String _default) { - String templateDir = myTemplateDir; - if (myTemplateDir == null || myTemplateDir.trim().length() == 0) { - templateDir = _default; - } - if (templateDir != null) { - templateDir = PathUtils.getRelativePath(myProject, new Path(templateDir)); - } - return templateDir; - } - public void setTemplateDir(String _templateDir) { if (isNew(myTemplateDir, _templateDir)) { myTemplateDir = _templateDir; ..-446,140 +574,139 @@ } } - public void setPrefix(String _prefix) { - if (isNew(myPrefix, _prefix)) { - myPrefix = _prefix; - myDirty = true; - } + public void setVerbose(Boolean _verbose) { + myVerbose = _verbose; + myDirty = true; } - public String getPrefix() { - return myPrefix; - } + public String writeToDecodedString(String _defaultEOGeneratorPath, String _defaultTemplateDir) { + String eogeneratorPath; - public void setFilenameTemplate(String _filenameTemplate) { - if (isNew(myFilenameTemplate, _filenameTemplate)) { - myFilenameTemplate = _filenameTemplate; - myDirty = true; + if (EOGENERATOR_PATH_VARIABLE.equals(getEOGeneratorPath())) { + eogeneratorPath = escape(_defaultEOGeneratorPath, true); + } else { + eogeneratorPath = getQuotedEOGeneratorPath(); } - } - public String getFilenameTemplate() { - return myFilenameTemplate; - } + String templateDir; - public Boolean isVerbose() { - return myVerbose; - } + if (TEMPLATE_DIR_VARIABLE.equals(getTemplateDir())) { + templateDir = _defaultTemplateDir; + } else { + templateDir = getTemplateDir(); + } - public void setVerbose(Boolean _verbose) { - myVerbose = _verbose; - myDirty = true; + return writeToString(eogeneratorPath, templateDir); } - public void setDirty(boolean _dirty) { - myDirty = _dirty; + public void writeToFile(IFile _file, IProgressMonitor _monitor) throws CoreException, IOException { + String eogenFileContents = writeToString(); + InputStream stream = new ByteArrayInputStream(eogenFileContents.getBytes("UTF-8")); + if (_file.exists()) { + _file.setContents(stream, true, true, _monitor); + } else { + _file.create(stream, true, _monitor); + } + stream.close(); + setDirty(false); } - public boolean isDirty() { - return myDirty; - } + private String writeToString(String _eogeneratorPath, String _templateDir) { + StringBuffer buffer = new StringBuffer(); - public boolean isModelReferenced(EOModelReference _modelReference) { - boolean modelReferenced = false; - String eomodelName = _modelReference.getName(); + buffer.append(_eogeneratorPath); + + append(buffer, "-destination", myDestination); + append(buffer, "-filenameTemplate", myFilenameTemplate); + append(buffer, "-java", myJava); + append(buffer, "-javaclient", myJavaClient); + append(buffer, "-javaTemplate", getJavaTemplate()); + Iterator modelsIter = myModels.iterator(); - while (!modelReferenced && modelsIter.hasNext()) { + + while (modelsIter.hasNext()) { EOModelReference model = (EOModelReference) modelsIter.next(); - modelReferenced = model.getName().equals(eomodelName); + append(buffer, "-model", model.getPath(myProject)); } - if (!modelReferenced) { - Iterator refModelsIter = myRefModels.iterator(); - while (!modelReferenced && refModelsIter.hasNext()) { - EOModelReference model = (EOModelReference) refModelsIter.next(); - modelReferenced = model.getName().equals(eomodelName); - } - } - return modelReferenced; - } - public static class Define { - private String myName; + append(buffer, "-packagedirs", myPackageDirs); + append(buffer, "-prefix", myPrefix); - private String myValue; + Iterator refModelsIter = myRefModels.iterator(); - public Define(String _name, String _value) { - myName = _name; - myValue = _value; + while (refModelsIter.hasNext()) { + EOModelReference refModel = (EOModelReference) refModelsIter.next(); + append(buffer, "-refmodel", refModel.getPath(myProject)); } - public int hashCode() { - return myName.hashCode(); - } + append(buffer, "-subclassDestination", mySubclassDestination); + append(buffer, "-subclassJavaTemplate", getSubclassJavaTemplate()); + append(buffer, "-templatedir", _templateDir); + append(buffer, "-verbose", myVerbose); - public boolean equals(Object _obj) { - return (_obj instanceof Define && ((Define) _obj).myName.equals(myName)); + Iterator definesIter = myDefines.iterator(); + + while (definesIter.hasNext()) { + Define define = (Define) definesIter.next(); + String name = define.getName(); + String value = define.getValue(); + append(buffer, "-define-" + name, value); } - public String getName() { - return myName; + return buffer.toString(); + } + + public String writeToString() { + return writeToString(getQuotedEOGeneratorPath(), getTemplateDir()); + } + + /** + *..eprecated + *..aram _defaultEOGeneratorPath + *..aram _defaultTemplateDir + *..aram _defaultJavaTemplate + *..aram _defaultSubclassJavaTemplate + *..eturn + */ + public String writeToString(String _defaultEOGeneratorPath, String _defaultTemplateDir, String _defaultJavaTemplate, String _defaultSubclassJavaTemplate) { + StringBuffer sb = new StringBuffer(); + + sb.append(escape(getEOGeneratorPath(_defaultEOGeneratorPath), false)); + + append(sb, "-destination", myDestination); + append(sb, "-filenameTemplate", myFilenameTemplate); + append(sb, "-java", myJava); + append(sb, "-javaclient", myJavaClient); + append(sb, "-javaTemplate", getJavaTemplate(_defaultJavaTemplate)); + + Iterator modelsIter = myModels.iterator(); + while (modelsIter.hasNext()) { + EOModelReference model = (EOModelReference) modelsIter.next(); + append(sb, "-model", model.getPath(myProject)); } - public String getValue() { - return myValue; + append(sb, "-packagedirs", myPackageDirs); + append(sb, "-prefix", myPrefix); + + Iterator refModelsIter = myRefModels.iterator(); + while (refModelsIter.hasNext()) { + EOModelReference refModel = (EOModelReference) refModelsIter.next(); + append(sb, "-refmodel", refModel.getPath(myProject)); } - } - public static EOGeneratorModel createModelFromFile(IFile _file) throws ParseException, CoreException, IOException { - _file.refreshLocal(IResource.DEPTH_INFINITE, null); - InputStream eogenFileStream = _file.getContents(); - try { - StringBuffer sb = new StringBuffer(); - BufferedReader br = new BufferedReader(new InputStreamReader(eogenFileStream)); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - String string = sb.toString(); - string.replace('\\', '/'); - EOGeneratorModel model = new EOGeneratorModel(_file.getProject(), string); - model.setEOGeneratorPath(Preferences.getEOGeneratorPath(), false); - return model; - } finally { - eogenFileStream.close(); + append(sb, "-subclassDestination", mySubclassDestination); + append(sb, "-subclassJavaTemplate", getSubclassJavaTemplate(_defaultSubclassJavaTemplate)); + append(sb, "-templatedir", getTemplateDir(_defaultTemplateDir)); + append(sb, "-verbose", myVerbose); + + Iterator definesIter = myDefines.iterator(); + while (definesIter.hasNext()) { + Define define = (Define) definesIter.next(); + String name = define.getName(); + String value = define.getValue(); + append(sb, "-define-" + name, value); } - } - public static EOGeneratorModel createDefaultModel(IProject _project) { - EOGeneratorModel model = new EOGeneratorModel(_project); - model.setJava(Boolean.TRUE); - model.setPackageDirs(Boolean.TRUE); - model.setVerbose(Boolean.TRUE); - // model.setEOGeneratorPath(Preferences.getEOGeneratorPath()); - // model.setJavaTemplate(Preferences.getEOGeneratorJavaTemplate()); - // model.setTemplateDir(Preferences.getEOGeneratorTemplateDir()); - // model.setSubclassJavaTemplate(Preferences.getEOGeneratorSubclassJavaTemplate()); - try { - IJavaProject javaProject = JavaCore.create(_project); - if (javaProject != null) { - IClasspathEntry[] classpathEntry = javaProject.getRawClasspath(); - for (int classpathEntryNum = 0; classpathEntryNum < classpathEntry.length; classpathEntryNum++) { - IClasspathEntry entry = classpathEntry[classpathEntryNum]; - if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { - IPath path = entry.getPath(); - if (path != null) { - IFolder sourceFolder = _project.getWorkspace().getRoot().getFolder(path); - IPath projectRelativePath = sourceFolder.getProjectRelativePath(); - String projectRelativePathStr = projectRelativePath.toPortableString(); - model.setDestination(projectRelativePathStr); - model.setSubclassDestination(projectRelativePathStr); - } - } - } - } - } catch (JavaModelException e) { - e.printStackTrace(); - } - return model; + return sb.toString(); } } Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGeneratorModelTest.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGeneratorModelTest.java (revision 0) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/EOGeneratorModelTest.java (revision 0) ..-0,0 +1,58 @@ +package org.objectstyle.wolips.eogenerator.model; + +import junit.framework.TestCase; + +/** + *..uthor <a href="mailto:hprange@moleque.com.br">Henrique Prange</a> + */ +public class EOGeneratorModelTest extends TestCase { + protected static final String DESTINATION = "src"; + + protected static final String EOGENERATOR_PATH = "C:/EOGenerator Path"; + + private static final String JAVA_TEMPLATE = "JavaSourceEOF52.eotemplate"; + + private static final String SUBCLASS_TEMPLATE = "JavaSubclassSourceEOF5.eotemplate"; + + private static final String TEMPLATE_DIR = EOGENERATOR_PATH + "/Templates"; + + protected EOGeneratorModel model; + + protected void setUp() throws Exception { + super.setUp(); + + model = new EOGeneratorModel(); + + model.setJavaTemplate(JAVA_TEMPLATE); + model.setSubclassJavaTemplate(SUBCLASS_TEMPLATE); + model.setDestination(DESTINATION); + model.setSubclassDestination(DESTINATION); + model.setPackageDirs(Boolean.TRUE); + model.setVerbose(Boolean.TRUE); + } + + public void testInitializationWithVariables() throws Exception { + assertEquals(EOGeneratorModel.EOGENERATOR_PATH_VARIABLE, model.getEOGeneratorPath()); + assertEquals(EOGeneratorModel.TEMPLATE_DIR_VARIABLE, model.getTemplateDir()); + } + + public void testQuotedEOGeneratorPath() throws Exception { + model.setEOGeneratorPath(EOGENERATOR_PATH); + + String path = model.getQuotedEOGeneratorPath(); + + assertEquals("\"" + EOGENERATOR_PATH + "\"", path); + } + + public void testQuotedVariable() throws Exception { + assertEquals(EOGeneratorModel.EOGENERATOR_PATH_VARIABLE, model.getQuotedEOGeneratorPath()); + } + + public void testWriteToDecodedString() throws Exception { + assertEquals("\"" + EOGENERATOR_PATH + "\" -destination " + DESTINATION + " -javaTemplate " + JAVA_TEMPLATE + " -packagedirs -subclassDestination " + DESTINATION + " -subclassJavaTemplate " + SUBCLASS_TEMPLATE + " -templatedir \"" + TEMPLATE_DIR + "\" -verbose", model.writeToDecodedString(EOGENERATOR_PATH, TEMPLATE_DIR)); + } + + public void testWriteToString() throws Exception { + assertEquals(EOGeneratorModel.EOGENERATOR_PATH_VARIABLE + " -destination " + DESTINATION + " -javaTemplate " + JAVA_TEMPLATE + " -packagedirs -subclassDestination " + DESTINATION + " -subclassJavaTemplate " + SUBCLASS_TEMPLATE + " -templatedir " + EOGeneratorModel.TEMPLATE_DIR_VARIABLE + " -verbose", model.writeToString()); + } +} Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/PathUtils.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/PathUtils.java (revision 3744) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/PathUtils.java (working copy) ..-54,37 +54,42 @@ import org.eclipse.core.runtime.Path; public class PathUtils { - public static String getRelativePath(IProject _projectContext, IPath _path) { - IPath projectPath = _projectContext.getLocation(); - return PathUtils.getRelativePath(projectPath, _path); - } - public static String getRelativePath(IPath _projectPath, IPath _path) { - String modelPathStr; + IPath relativeModelPath = _path; + if (_path.isAbsolute()) { if (_projectPath.isPrefixOf(_path)) { - modelPathStr = _path.removeFirstSegments(_projectPath.segmentCount()).toPortableString(); + relativeModelPath = _path.removeFirstSegments(_projectPath.segmentCount()); } else { int matchingSegmentCount = _projectPath.matchingFirstSegments(_path); - if (matchingSegmentCount == 0) { - modelPathStr = _path.toPortableString(); - } else { + + if (matchingSegmentCount != 0) { int relativePathSegmentCount = _projectPath.segmentCount() - matchingSegmentCount; - IPath relativeModelPath = new Path(""); + + relativeModelPath = new Path(""); + for (int i = 0; i < relativePathSegmentCount; i++) { relativeModelPath = relativeModelPath.append(".." + IPath.SEPARATOR); } + relativeModelPath = relativeModelPath.append(_path.removeFirstSegments(matchingSegmentCount)); - modelPathStr = relativeModelPath.toPortableString(); + } else { + // It is really an absolute path + return relativeModelPath.toPortableString(); } } - } else { - modelPathStr = _path.toPortableString(); } - return modelPathStr; + + return removeDeviceId(relativeModelPath).toPortableString(); } - - public static void main(String[] args) { - System.out.println("PathUtils.main: " + getRelativePath(new Path("C:\\grantparent"), new Path("C:\\grantparent\\parent\\child.txt"))); + + public static String getRelativePath(IProject _projectContext, IPath _path) { + IPath projectPath = _projectContext.getLocation(); + + return PathUtils.getRelativePath(projectPath, _path); } + + static IPath removeDeviceId(IPath path) { + return path.setDevice(null); + } } Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/PathUtilsTest.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/PathUtilsTest.java (revision 0) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/model/PathUtilsTest.java (revision 0) ..-0,0 +1,70 @@ +package org.objectstyle.wolips.eogenerator.model; + +import junit.framework.TestCase; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; + +/** + *..uthor <a href="mailto:hprange@moleque.com.br">Henrique Prange</a> + */ +public class PathUtilsTest extends TestCase { + protected static final String MODEL_NAME = "MyEOModel.eomodeld"; + + protected static final String SUBFOLDER = "models/"; + + protected static final String UNIX_LIKE_PATH = "/Users/JohnDoe/workspace/my-project/"; + + protected static final String WINDOWS_LIKE_PATH = "C:/Projetos/runtime-EclipseApplication(4)/Teste/"; + + public void testAbsolutePathWithoutProjectMatch() throws Exception { + IPath[] projectPaths = { new Path(WINDOWS_LIKE_PATH) }; + + String modelOutsideProject = "C:/" + SUBFOLDER + MODEL_NAME; + + IPath[] paths = { new Path(modelOutsideProject) }; + + verifyRelativePathsEquals(modelOutsideProject, projectPaths, paths); + } + + public void testRemoveDeviceId() throws Exception { + IPath pathWithDeviceId = new Path(WINDOWS_LIKE_PATH); + IPath expectedPath = new Path(WINDOWS_LIKE_PATH.substring(2)); + + assertEquals(expectedPath, PathUtils.removeDeviceId(pathWithDeviceId)); + } + + public void testWithAbsolutePath() throws Exception { + IPath[] projectPaths = { new Path(WINDOWS_LIKE_PATH), new Path(UNIX_LIKE_PATH) }; + IPath[] paths = { new Path(WINDOWS_LIKE_PATH + MODEL_NAME), new Path(UNIX_LIKE_PATH + MODEL_NAME) }; + + verifyRelativePathsEquals(MODEL_NAME, projectPaths, paths); + } + + public void testWithAbsolutePathInsideSubfolder() throws Exception { + IPath[] projectPaths = { new Path(WINDOWS_LIKE_PATH), new Path(UNIX_LIKE_PATH) }; + IPath[] paths = { new Path(WINDOWS_LIKE_PATH + SUBFOLDER + MODEL_NAME), new Path(UNIX_LIKE_PATH + SUBFOLDER + MODEL_NAME) }; + + verifyRelativePathsEquals(SUBFOLDER + MODEL_NAME, projectPaths, paths); + } + + public void testWithRelativePath() throws Exception { + IPath[] projectPaths = { new Path("").makeRelative() }; + IPath[] paths = { new Path(MODEL_NAME) }; + + verifyRelativePathsEquals(MODEL_NAME, projectPaths, paths); + } + + public void testWithRelativePathInsideSubfolder() throws Exception { + IPath[] projectPaths = { new Path("").makeRelative() }; + IPath[] paths = { new Path(SUBFOLDER + MODEL_NAME) }; + + verifyRelativePathsEquals(SUBFOLDER + MODEL_NAME, projectPaths, paths); + } + + protected void verifyRelativePathsEquals(String expectedPath, IPath[] projectPaths, IPath[] paths) { + for (int i = 0; i < projectPaths.length; i++) { + assertEquals(expectedPath, PathUtils.getRelativePath(projectPaths[i], paths[i])); + } + } +} Index: C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/ui/editors/EOGeneratorFormPage.java =================================================================== --- C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/ui/editors/EOGeneratorFormPage.java (revision 3744) +++ C:/Projetos/workspace/woproject/wolips/core/plugins/org.objectstyle.wolips.eogenerator.ui/java/org/objectstyle/wolips/eogenerator/ui/editors/EOGeneratorFormPage.java (working copy) ..-345,7 +345,7 @@ } }); - Composite templatesSection = createSection(_toolkit, _parent, "Templates", "These paths specify the templates that will be used to generate files. If left blank, the defaults from the EOGenerator preference page will be used.", 1, 3); + Composite templatesSection = createSection(_toolkit, _parent, "Templates", "These paths specify the templates that will be used to generate files.", 1, 3); GridLayout templatesSectionLayout = (GridLayout) templatesSection.getLayout(); templatesSectionLayout.horizontalSpacing = 10; ..-388,7 +388,7 @@ }); myTemplatesFolderEntry = new FormEntry(templatesSection, _toolkit, "Templates Folder", "Browse...", false); - myTemplatesFolderEntry.setValue(myModel.getTemplateDir(null)); + myTemplatesFolderEntry.setValue(myModel.getTemplateDir()); myTemplatesFolderEntry.setFormEntryListener(new EOFormEntryAdapter() { public void textValueChanged(FormEntry _entry) { EOGeneratorFormPage.this.getModel().setTemplateDir(_entry.getValue()); ..-413,7 +413,7 @@ }); myTemplateEntry = new FormEntry(templatesSection, _toolkit, "Template", "Browse...", false); - myTemplateEntry.setValue(myModel.getJavaTemplate(null)); + myTemplateEntry.setValue(myModel.getJavaTemplate()); myTemplateEntry.setFormEntryListener(new EOFormEntryAdapter() { public void textValueChanged(FormEntry _entry) { EOGeneratorFormPage.this.getModel().setJavaTemplate(_entry.getValue()); ..-432,7 +432,7 @@ }); mySubclassTemplateEntry = new FormEntry(templatesSection, _toolkit, "Subclass Template", "Browse...", false); - mySubclassTemplateEntry.setValue(myModel.getSubclassJavaTemplate(null)); + mySubclassTemplateEntry.setValue(myModel.getSubclassJavaTemplate()); mySubclassTemplateEntry.setFormEntryListener(new EOFormEntryAdapter() { public void textValueChanged(FormEntry _entry) { EOGeneratorFormPage.this.getModel().setSubclassJavaTemplate(_entry.getValue()); ..-525,9 +525,9 @@ myDefinesTableViewer.setInput(myModel); myDestinationEntry.setValue(myModel.getDestination()); mySubclassDestinationEntry.setValue(myModel.getSubclassDestination()); - mySubclassTemplateEntry.setValue(myModel.getSubclassJavaTemplate(null)); - myTemplateEntry.setValue(myModel.getJavaTemplate(null)); - myTemplatesFolderEntry.setValue(myModel.getTemplateDir(null)); + mySubclassTemplateEntry.setValue(myModel.getSubclassJavaTemplate()); + myTemplateEntry.setValue(myModel.getJavaTemplate()); + myTemplatesFolderEntry.setValue(myModel.getTemplateDir()); } }



    This archive was generated by hypermail 2.0.0 : Sat Jan 13 2007 - 17:28:14 EST