How do I load a javascript file as text (String)? - lift

I know can load html (NodeSeq) by using Templates("file" :: "address" :: Nil). Is there a similar way to load a JavaScript file from /src/main/webapp/ as InputStream or String?

OK, I found the solution, let it stay in history...
One can obtain resource files from inside /src/main/webapp/ by using LiftRules.loadResourceAsString("/index.html").openOr("")
For example, to load a JS file as JcCmd one can use JsCmds.Run(LiftRules.loadResourceAsString("/js/my.js").openOr(""))

Related

"How to embed resources" or "How to access a Resource"

I am struggling with embedded resources or resources in general with Dynamics365. My goal is to add a xml-file as resource to a model and use that resource in some testcode.
I tried to add the xml as resource-element but it seems this does not embedd the xml into the compiled dll so i don't know how to pick up that xml-file in my testcode. Currently my testcode loads the xml from "C:\Temp\test.xml" where i copied my xml to, but thats not a viable solution and i thought adding the xml as resource would be ok. Or is there a better approach to this scenario ?
You can use class SysResource to interact with resources. I used the following code in one of my unit tests to load the content of a file resource into a file and create a CommaStreamIo instance from that file. You should be able to modify that to do your stuff with an xml file.
ResourceNode textFileResourceNode = SysResource::getResourceNode(resourceStr(MyTextFileResourceName));
str textFilename = SysResource::saveToTempFile(textFileResourceNode);
CommaStreamIo commaStreamIo = CommaStreamIo::constructForRead(File::UseFileFromURL(textFilename));
Also take a look at reading a resource into a string.
You could also take a look at how some of the standard resources are used. For example, there are several .xslt file resources that are used to transform bank statement formats.

How to load a template text file in SAPUI5?

I have a template fragment file, which is an xml file in fact.
I want to load it in my controller, do some modification on that, and then use it to render some part of the view.
I only need to read this xml file as text file and put its content in a string.
I cannot find any object for doing this in SAPUI5 api.
Please note the file is placed in my view folder in server side.
I need some kind of promise that read the file and after reading the file run a successor function.
Thanks in advance
There can be multiple ways to to this.
1.Either you can load your XML in an XML Model "sap.ui.model.xml.XMLModel()"
var oModelX = new sap.ui.model.xml.XMLModel();
oModelX.attachRequestCompleted(function(){
var xmlStr = oModelX.getXML();
console.log(xmlStr); // Do what ever you want with your xml string
});
oModelX.loadData("../view/st.fragment.xml");
2.You can also read the content of that XML file using AJAX and do your parsing in AJAX response.

How to use custom language highlight syntax in Gtk SourceView?

I'm trying to create my own language definition, and use it for highlighting the syntax in my app.
The issue I have is that, when trying to access the language definition from my app's data folder (/usr/share/myapp/), even using the c.lang file copied from /usr/share/gtksourceview-3.0/language-specs/, just to test, I get this error at runtime:
GtkSourceView-WARNING **: Failed to load '/usr/share/myapp/c.lang': could not find the RelaxNG schema file
So it's asking for some schema file? So I went forward and copied every file from the language-specs folder that isn't a lang file, which includes: language.dtd, language.rng and language2.rng.
Now, when I run again, I get these errors:
GtkSourceView-WARNING **: in file /usr/share/com.github.aleksandar-stefanovic.urmsimulator/c.lang: style 'def:comment' not defined
GtkSourceView-WARNING **: Failed to load '/usr/share/com.github.aleksandar-stefanovic.urmsimulator/c.lang': unable to resolve language 'def'
What does that even mean? Is that something namespace-related? It is very peculiar, because the exact same file is working perfectly when in gtksourceview folder.
Any clues? Do these "RelaxNG" files work only in their original directories? If so, how can I change that? I've looked into the files, but failed to find any reference to their original folder...
This is the source-code (in Vala) related to the issue:
var manager = Gtk.SourceLanguageManager.get_default ();
string search_paths[] = {"/usr/share/myapp", null};
manager.set_search_path (search_paths);
var buffer = new Gtk.SourceBuffer.with_language (manager.get_language ("c"));
The application does in fact find the language "c", I've checked by debugging.
You have to preserve the Gtk.SourceLanguageManager's original search path when you add your own. Append to it instead of replacing it.

Issue serving static files in spray

I'm having some problems serving static files from a route that at his core look like this using spray 1.3.2:
ctx.withHttpResponseHeadersMapped(hm => hm ++ List(`Content-Disposition`("attachment",
Map(("filename", attachment.name),
`Content-Length`(attachment.bytes.length)))
.withHttpResponseEntityMapped { e =>
HttpEntity(new ContentType(
MediaTypes.forExtension(attachment.name.split('.').last.toLowerCase).getOrElse(MediaTypes.`application/octet-stream`), None), e.data) //also tried with just MediaTypes.`application/octet-stream` as default
}
.complete(attachment.bytes)
where attachment.name is just a string and attachment.bytes an Array[Byte].
In this way I'm able to download the file represented by the Array[Byte] from the browser interface, the problem is that when the file is in excel formats (xls, xlsx) the downloaded file seem to get corrupted during the process and wont open.
The strange thing is that for those corrupted files the dimension of the downloaded copy is bigger then the original and that in the browser console is possible to notice that the transfer-encoding is set to chunked with the Content-Length header removed.
Removing the specific media type has no effect.
I can't find a solution to make those excel files working smooth with this piece of code.
I don't understand where the transfer-encoding is set to chunked , file size seems not to be the cause because it's possible to serve big file without this issue for other formats (pdf, txt, jpeg, ...)
If someone can give an hint on what is happening I would appreciate it, thanks.

Exporting an JAR file in Eclipse and referencing a file

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.