Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » JET tag to Check if file exists
JET tag to Check if file exists [message #51640] Mon, 06 October 2008 11:58 Go to next message
justin Mising name is currently offline justin Mising nameFriend
Messages: 28
Registered: July 2009
Junior Member
Hi,

I need to check if a file exists in a project in the eclipse
workspace/file system .

If file does not exist, I need to create file which can be done using
<ws:file> .
If file exist,I will be loading it using <c:load> tag.

I could not find a JET tag that meets this requirement. Pls let me know if
there is Jet tag for the same.

I had used java scriptlet code for this reqt.

However,I am not able to get the eclipse workspace path dynamically.

My code snippet is below:
<\%@ page import="java.io.*" %>
<% int i=0;
java.io.File file = new java.io.File("..");

try{
java.io.File file1 = new
java.io.File(file.getCanonicalPath()+" FA/projects/credentials/credentials-business-service/src/mai n/resources/serviceContext.xml ");
boolean b = file1.exists();
String s=file.getCanonicalPath();
String s1=file.getAbsolutePath();
System.out.println("file1 can pth "+file.getCanonicalPath());%>
<c:log> can path is <%=s%> and abs <%=s1%> </c:log>
<% if(b){
System.out.print("exists");

%>

<c:load
url=" credentials/credentials-business-service/src/main/resources/ serviceContext.xml "
urlContext="workspace" var="oldSpringContext"/>

file.getCanonicalPath() gives the root hard disk drive name(like C:).
I have hardcoded the workspace to FA/projects which is workspace .
Is there a method to get the workspace path dynamically.
Pls let me know asap.

Regards,
Justin.
Re: JET tag to Check if file exists [message #51657 is a reply to message #51640] Mon, 06 October 2008 13:08 Go to previous messageGo to next message
Timothy Marc is currently offline Timothy MarcFriend
Messages: 547
Registered: July 2009
Senior Member
Hey,

imho, there is no xpath-function or tag for that functionality.
Either do it via Scriplet or implement your own function by using the
extension points. For the localization of the resources in the workspace,
have a look at the following link:

http://lmap.blogspot.com/2008/03/platform-scheme-uri.html

May be this helps.

Adios

Timothy


"justin " <justin_prabhu@yahoo.com> schrieb im Newsbeitrag
news:9b0af99a93c24fbdb0145ac20a20d169$1@www.eclipse.org...
> Hi,
>
> I need to check if a file exists in a project in the eclipse
> workspace/file system .
>
> If file does not exist, I need to create file which can be done using
> <ws:file> .
> If file exist,I will be loading it using <c:load> tag.
>
> I could not find a JET tag that meets this requirement. Pls let me know if
> there is Jet tag for the same.
>
> I had used java scriptlet code for this reqt.
>
> However,I am not able to get the eclipse workspace path dynamically.
>
> My code snippet is below:
> <\%@ page import="java.io.*" %> <% int i=0;
> java.io.File file = new java.io.File("..");
> try{ java.io.File file1 = new
> java.io.File(file.getCanonicalPath()+" FA/projects/credentials/credentials-business-service/src/mai n/resources/serviceContext.xml ");
> boolean b = file1.exists();
> String s=file.getCanonicalPath();
> String s1=file.getAbsolutePath();
> System.out.println("file1 can pth "+file.getCanonicalPath());%>
> <c:log> can path is <%=s%> and abs <%=s1%> </c:log>
> <% if(b){
> System.out.print("exists");
>
> %>
> <c:load
> url=" credentials/credentials-business-service/src/main/resources/ serviceContext.xml "
> urlContext="workspace" var="oldSpringContext"/>
>
> file.getCanonicalPath() gives the root hard disk drive name(like C:).
> I have hardcoded the workspace to FA/projects which is workspace . Is
> there a method to get the workspace path dynamically.
> Pls let me know asap.
>
> Regards,
> Justin.
>
>
Re: JET tag to Check if file exists [message #51735 is a reply to message #51657] Mon, 06 October 2008 13:41 Go to previous message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Justin, Timothy:

A couple of possibilities:

1) Detect the file with the 'org.eclipse.jet.resource' loader

<%-- this 'loads' the parent. Here $parent is an IResource --%>
<c:load var="parent" url="workspace relative path to parent"
urlContext="worksapce" loader="org.eclipse.jet.resource" />
<c:if test=" $parent/file[@name = ' the file name']" >
<%-- file exists --%>
</c:if>

The more obvious approach of using the actual file as an URL doesn't work,
as the loader will through an exception. You could still use the approach,
and then check for the non-existence of the variable, but then this isn't
much different than the above in temps of amount of code.

2) Use the replace='false' attribute to avoid overwritting some existing
files. The 'replace' attribute on ws:file and java:class checks for file
existance. If set to 'false', then if the file already exists, the template
is not run, and the results are not written. I'm not sure that it will help
in your situation, though.

3) Creating a conditional tag (e.g. <mylib:fileExists path="..."> ... handle
exists ...</mylib:fileExists> would be fairly easy. An XPath funciton might
be a bit more code (because you need to fiddle a bit to get the function
arguments, but is also pretty easy). If you feel that such a function would
be generally useful, feel free to submit a bugzilla. And, even better than a
bugzilla is attaching an implementation - provided your employer approves,
of course.

Paul

"Timothy Marc" <timothymarc@freenet.de> wrote in message
news:gcd2g5$iks$1@build.eclipse.org...
> Hey,
>
> imho, there is no xpath-function or tag for that functionality.
> Either do it via Scriplet or implement your own function by using the
> extension points. For the localization of the resources in the workspace,
> have a look at the following link:
>
> http://lmap.blogspot.com/2008/03/platform-scheme-uri.html
>
> May be this helps.
>
> Adios
>
> Timothy
>
>
> "justin " <justin_prabhu@yahoo.com> schrieb im Newsbeitrag
> news:9b0af99a93c24fbdb0145ac20a20d169$1@www.eclipse.org...
>> Hi,
>>
>> I need to check if a file exists in a project in the eclipse
>> workspace/file system .
>>
>> If file does not exist, I need to create file which can be done using
>> <ws:file> .
>> If file exist,I will be loading it using <c:load> tag.
>>
>> I could not find a JET tag that meets this requirement. Pls let me know
>> if there is Jet tag for the same.
>>
>> I had used java scriptlet code for this reqt.
>>
>> However,I am not able to get the eclipse workspace path dynamically.
>>
>> My code snippet is below:
>> <\%@ page import="java.io.*" %> <% int i=0;
>> java.io.File file = new java.io.File("..");
>> try{ java.io.File file1 = new
>> java.io.File(file.getCanonicalPath()+" FA/projects/credentials/credentials-business-service/src/mai n/resources/serviceContext.xml ");
>> boolean b = file1.exists();
>> String s=file.getCanonicalPath();
>> String s1=file.getAbsolutePath();
>> System.out.println("file1 can pth "+file.getCanonicalPath());%>
>> <c:log> can path is <%=s%> and abs <%=s1%> </c:log>
>> <% if(b){
>> System.out.print("exists");
>>
>> %>
>> <c:load
>> url=" credentials/credentials-business-service/src/main/resources/ serviceContext.xml "
>> urlContext="workspace" var="oldSpringContext"/>
>>
>> file.getCanonicalPath() gives the root hard disk drive name(like C:).
>> I have hardcoded the workspace to FA/projects which is workspace . Is
>> there a method to get the workspace path dynamically.
>> Pls let me know asap.
>>
>> Regards,
>> Justin.
>>
>>
>
>
Previous Topic:Error when setting "xsi:schemaLocation" attribute
Next Topic:JET var in scriptlets
Goto Forum:
  


Current Time: Thu Apr 25 14:11:34 GMT 2024

Powered by FUDForum. Page generated in 0.03081 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top