Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » append to an existing file using ws:file
append to an existing file using ws:file [message #54747] Tue, 23 December 2008 15:49 Go to next message
Edward Mui is currently offline Edward MuiFriend
Messages: 4
Registered: July 2009
Junior Member
I would like some help on how to appending to a file.

My problem is I have XML diameter AVP definition files in a different
directory. I want to create an ASN1 format file based on the AVP
definition.
(i.e. loading each avp.xml file via some iterate loop and "combine" the
avp.xml information into a single asn1 file)

My current solution involves some manual appending because I don't see
ws:file support append. Here's are high level steps of my solution.

Use ws:file to create the first file that contents header information
(file.asn)
Then iterate on loading(then xpath to get the information) on individual
XML diamter AVP files. Then use ws:file and asn1.jet file and a different
file name to create one file for each avp file. Then cat the
loop generated files and append to file.asn.

Any thought on how I can eliminate this manual step.
I read a little bit about BufferedJET2Writer but have not able to get that
working yet.

Thanks for any help.
Edward




(Sorry for the previous accidental partial post. The text area to enter
this post is kind of hard to get the indentation correctly. Didn't
realized the curser was focused on the Post button when hitting Tabs and
Enters on the key board)
Re: append to an existing file using ws:file [message #54828 is a reply to message #54747] Wed, 07 January 2009 10:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: johannes.nel.gmail.com

My approach to a similar problem was to create a custom tag which picks
up the existing (previously generated) file and having this tag return
the specified sections to a template. Obviously this means that you
cannot use ws:file.



Edward Mui wrote:
> I would like some help on how to appending to a file.
>
> My problem is I have XML diameter AVP definition files in a different
> directory. I want to create an ASN1 format file based on the AVP
> definition.
> (i.e. loading each avp.xml file via some iterate loop and "combine" the
> avp.xml information into a single asn1 file)
>
> My current solution involves some manual appending because I don't see
> ws:file support append. Here's are high level steps of my solution.
>
> Use ws:file to create the first file that contents header information
> (file.asn)
> Then iterate on loading(then xpath to get the information) on individual
> XML diamter AVP files. Then use ws:file and asn1.jet file and a
> different file name to create one file for each avp file. Then cat the
> loop generated files and append to file.asn.
>
> Any thought on how I can eliminate this manual step.
> I read a little bit about BufferedJET2Writer but have not able to get
> that working yet.
>
> Thanks for any help.
> Edward
>
>
>
>
> (Sorry for the previous accidental partial post. The text area to enter
> this post is kind of hard to get the indentation correctly. Didn't
> realized the curser was focused on the Post button when hitting Tabs and
> Enters on the key board)
>
>
Re: append to an existing file using ws:file [message #55125 is a reply to message #54747] Mon, 12 January 2009 14:27 Go to previous message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Edward:

Sorry for the late reply.

Do you really want to append? This would imply that your final ASN1 file
would get bigger everytime you ran the transformation. Or are you looking
for a way to concatenate a number of information sources (templates,
existing files, etc...)?

JET doesn't currently have an append or concat facility. The best approach I
can think of would be to create a tag that read a file into the buffer
contents:

<x:readFile url="...''/>

Then, in your situation, your ASN1 template might look something like this:

<c:include template="templates/ans1.header.jet"/>
<c:iterate select="..." var="avpInfo">
<c:include template="template/ans1.from.avp.jet"/>
</c:iterate>
<ws:readFile src=" ... location of the final portion of the ANS1 file..."/>


Creating a readFile tag would not be difficult. I would model it very much
on the current ws:copyFile tag in terms of tag attributes to support. Here
is a quick and dirty implementation, stealing a bit from CopyFileTag.

Snippet from the plugin.xml defining the tag...
<emptyTag
class="org.eclipse.jet.examples.tags.control.ReadFileTag"
name="readFile"
whenContainingLineIsEmpty="preserve">
<attribute
name="src"
type="string"
use="required">
</attribute>
<attribute
name="context"
type="string"
use="optional">
</attribute>
<attribute
name="encoding"
type="string"
use="optional">
</attribute>
</emptyTag>



And the implementation class:
/**
* Implement a tag &lt;x:readFile src="relative or absolute URL"
context="transform|workspace" encoding="..."/&gt;.
* The context and encoding tags are optional. Default for context is
'transform'. Default for 'encoding' is the
* JRE default encoding.
*
*/
public class ReadFileTag extends AbstractEmptyTag {

/* (non-Javadoc)
* @see
org.eclipse.jet.taglib.EmptyTag#doAction(org.eclipse.jet.tag lib.TagInfo,
org.eclipse.jet.JET2Context, org.eclipse.jet.JET2Writer)
*/
public void doAction(TagInfo td, JET2Context context, JET2Writer out)
throws JET2TagException {
final String src = getAttribute("src");
final String srcContext = getAttribute("context");

final URL url = getSourceURL(src, srcContext, context);
final String encoding = getAttribute("encoding");
final String contents = ActionsUtil.readTextFile(url, encoding);

out.write(contents);

}

private URL getSourceURL(String url, String urlContext, JET2Context
context) throws JET2TagException
{
TransformContextExtender tce =
TransformContextExtender.getInstance(context);

final URL baseURL = tce.getBaseURL(urlContext);

if (url.startsWith("/")) { //$NON-NLS-1$
url = url.substring(1);
}

URL sourceURL;
try
{
sourceURL = new URL(baseURL, url);
}
catch (MalformedURLException e)
{
throw new JET2TagException(e);
}
return sourceURL;
}

}


I have submitted a bugzilla to track this enhancement:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=260709

Paul
Previous Topic:MTL & GMF
Next Topic:JET, custom XPAth function and iterating through the nodes which get passed in.
Goto Forum:
  


Current Time: Thu Apr 25 09:43:20 GMT 2024

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

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

Back to the top