Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Accessing text file resource using Xtend
Accessing text file resource using Xtend [message #1706761] Sun, 30 August 2015 14:15
Elvis Dowson is currently offline Elvis DowsonFriend
Messages: 65
Registered: December 2011
Member
Hi,

I have a text file located in my src folder, that I would like to access and use within a multi-line template expression, but I'm having difficulty figuring out how to access it.

Text file location:
src/org/xtext/example/catkindsl/template/copyright_notice.txt

This is the extension method to retrive the contents of a file as a string:
	// Extension method to retrieve the contents of a file as a string.
	def fileContent(String filename){
		'''
	«val url = class.classLoader.getResource(filename)»
	«val File file = new File(url.path)»
	«IF file.exists()»
	«FOR String line : new FileReader(file).readLines»
	«line»
	«ENDFOR»
	«ENDIF»
		'''
	}


This is an extension method to generate a readme.txt file, with a license header, where I call the fileContent extension method.

	// Workspace: Generate readme.txt file for the catkin workspace.
	def generateReadmeFile(Workspace it) {
		'''
// The MIT License (MIT)
//
// «fileContent('org/xtext/example/catkindsl/template/copyright_notice.txt')»
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
		'''
	}


A the moment is that the above code cannot locate the file, if I specify the path relative to the src folder:

«fileContent('org/xtext/example/catkindsl/template/copyright_notice.txt')»

It works, if I specify an absolute path to the file.

How can I access the text file relative to the plug-in classpath?

Regards,

Elvis Dowson
Previous Topic:Combining two grammars problem
Next Topic:Xtext for Extended C language with Graphiti Editor
Goto Forum:
  


Current Time: Fri Apr 26 06:55:16 GMT 2024

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

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

Back to the top