SWT Browser widget: html source inside jar? - java

I want to implement a help system for my tiny SWT desktop application.
I've thought about a SWT browser widget containing a single html markup page and a set of anchors to navigate (there are only very few things to explain).
Everything works fine, but how do I load the html file from a jar?
I know aboutgetClass().getClassLoader().getResourceAsStream("foo");, but what is the best practice when reading from the input stream? The answer to Load a resource contained in a jar dissuades using a FileInputStream.
Thanks in advance

Well, I found a rather simple solution that obviously just works:
InputStream in = getClass().getClassLoader().getResourceAsStream("html/index.html");
Scanner scanner = new Scanner(in);
StringBuffer buffer = new StringBuffer();
while(scanner.hasNextLine()) {
buffer.append(scanner.nextLine());
}
browser.setText(buffer.toString());

i tend to use commons-io for such a task giving me simple abstraction methods like IOUtils.toString(InputStream in); and leaving the choice of best implementations to the able people at apache ;)
commons-io: http://commons.apache.org/io/
apidocs: http://commons.apache.org/io/api-release/index.html

Related

Apache FOP and Java Image Issues - Combining multiple sources

I am trying to "automate" the building of a PDF using Apache FOP and Java. I want to minimize the hard coding since I don't know in advance all the file combinations I am going to need to support. In addition I want to try and not save files on the hard drive. Files on the HD introduces security, performance, threading and cleanup considerations I would rather not handle.
The test case I am using right now has 1 FO and 2 PNG files. One of the PNG files is over 1MB.
Ideally I would create 3 sources:
InputStream fo = new InputStream(new File("C:\\Temp\\FOP\\Test\\blah.fo"));
InputStream png1 = new InputStream(new File("C:\\Temp\\FOP\\Test\\image-1.png"));
InputStream png2 = new InputStream(new File("C:\\Temp\\FOP\\Test\\image-2.png"));
Source foSrc = new StreamSource(fo);
Source png1Src = new StreamSource(png1);
Source png2Src = new StreamSource(png2);
and then combine them all together to generate the PDF. I can't find a way using the API to do that.
The FO files refers to the images via:
<fo:external-graphic src="file:image-1.png"/>
<fo:external-graphic src="file:image-2.png"/>
When I use the command line FOP tools, it builds the PDF as I would expect. As long as the two images are in the same directory as the FO file, then all is good. Using the command line, there is no need to point out the existence or location of the images.
When using Java, I have tried a number of configurations, but none of them fit my need:
I saved the FO file and the 2 images into the same directory and referred to them using the following FopFactory constructor:
private static final FopFactory fopFactory = FopFactory.newInstance(new File("C:\\Temp\\FOP\\test").toURI());
This code base only finds the smaller of the two images. It seems like the larger one is being ignored since it is bigger than some limit.
I have tried the above constructor using various relative and absolute paths.
I have tried constructing FopFactory using the default "fop.xconf" file and adding the "C:\Temp\FOP\Test" directory to the classpath.
I have "hardcoded" the files and their locations in the FO file.
I have tried using intermediate files structure (IFDocumentHandler, IFSerializer and IFConcatenator) for the images and get errors that way. Seems the intermediate files are not intended for images.
I have been able to embed the file into the FO file using base64 encoding and the syntax:
<fo:external-graphic src="url('data:image/png;base64,iVBORw...ggg==')"/>
The last one seems like the best solution other than taking 3 sources and using all 3 to generate the PDF. Any suggestions on how to use the API to combine the 3 sources?
Thanks.

.jpg out of .cgi with java (IP Webcam)

hy there, i hope i can explain my question:
i´ve an ip webcam and i want to read&save a .jpg file out of the path
webacm-ip-adr:8084/snapshot.cgi
i´ve little java experience and would like to program it in processing to keep it simple:
i´ve found this link:
https://www.java.net/node/702486
but its a slight overkill for me to understand it would be great if i can work with the 2 processing examples: web/loadingimages and net/httpClient
or do i make an logic mistake and its not solveable this way ?
You can use java lib "Apache Commons IO" to done it.
My Simple Code:
URL url = new URL("http://webacm-ip-adr:8084/snapshot.cgi");
InputStream input = url.openStream();
String jpg = "sample.jpg";
FileOutputStream output = new FileOutputStream(jpg);
IOUtils.copy(input, output);
Class "IOUtils" is a common tool for IO stream operation in commons-io jar.

JEditorPane syntax highlighting using jsyntaxpane

I have a JEditorPane in my application and I was loading java files into it using jsyntaxpane and the following code and it was working perfectly:
to highlight
jsyntaxpane.DefaultSyntaxKit.initKit();
textarea.setContentType("text/java");
to load file in
int a = filesToCompileList.getSelectedIndex();
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
textarea.read.read(br, index);
br.close();
textarea.requestFocus();
but know i had to change the way i was loading the file in and i am currently loading the files in like
File file = new File(filePath);
textarea.setPage(file.toURI().toURL());
this is loading the files in the way i want but isn't highlighting the text for java files anymore! does anybody know how i can fix this or get java highlighting a different way?
AFAIK, it can not be done if you don't implement your own version of the jeditorpane. From the javadoc
The setPage method can be used to initialize the component from a URL.
In this case, the content type will be determined from the URL, and
the registered EditorKit for that content type will be set.
So, the mime type of the content will be inherit from the mime type of the url. Call setContentyType later will have no effect, as this will change the model of the jeditorpane, cleaning the content. Again from the Javadoc
NOTE: This has the side effect of changing the model, because the
EditorKit is the source of how a particular type of content is
modeled. This method will cause setDocument to be called on behalf of
the caller to ensure integrity of the internal state.
So you must keep using the read method.

How do I "import" a HTML file from a shared drive into a JSP from my war file?

Preface: I am an inexperienced java programmer handed one of his first assignments. If I do not ask the question correctly or do not give enough detail, please let me know.
I am trying to import a HTML page that is saved on my C drive. I am trying to import it to the content portion (div id="content") of a JSP file that exists in a war file. I have already figured out that I can not use jsp:include, #include, #include file because the file exists outside the war file. I also figured out that c:import and iFrame do not work.
My goal is to make the contents of the html file that is saved in on my c drive appear in the contents of the jsp (visible on the web page).
Am I on the right track with this <% File f = new File("c:\\temp\\filename.html").......%>
I have searched stackoverflow and the only topic that came close was "How to Include a file outside the application (war) using jsp include." It did not really get me where I needed to go. Maybe the answer is right in front of me but I couldnt see it.
JSP/JSTL does not offer tags which support this. You'd need to do it using pure Java. You just have to write it to the response yourself.
Here's one of the simplest ways:
<%
Reader reader = new FileReader("c:/path/to/external/file.html");
try {
for (int i = 0; (i = reader.read()) != -1;) {
out.write(i);
}
} finally {
try { reader.close(); } catch (IOException ignore) {}
}
%>
You could wrap it in a custom tag to keep your JSP free of scriptlet clutter, or you could read it into a String in a servlet and pass it to JSP EL scope.
Do you want clients to see the contents of filename.html located on your server? If so, why don't you just get it inside your project/war?
Or do you want clients to see the contents of a filename.html they have on their computers? If so, you might be able to just add an iframe with that source... but you'll run into many security-related problems, since browsers won't ordinarily let you do that.
Try
<% File f = new File("c:\\temp\\filename.html");
BufferedReader in = new BufferedReader(new FileReader(f));
while (in.readLine() != null) {
out.println(blah blah blah);
}
in.close();
%>
reading the File and Printing it to the JSP should work,
My suggestion is to use http form upload in your jsp application. In that case your file can be in any place that is accesible in your filesystem instead of hardcoding it to be in a certain place. Usage http://commons.apache.org/fileupload/using.html
Good tutorial on http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html
Some useful hints can also be found in the video, http://www.youtube.com/watch?v=BLamJlRg9Ws

What is the best(least impacting?) method to read a file that is being written by another process?

I'm looking for the least impacting method to read a few different log files. By "least impacting" I mean the read wont affect log rotation, wont cause undue I/O activity, etc. I'm looking to accomplish this with Java...
Google afforded me the following little snippit of code:
FileStream fs = new FileStream("C:\foo.txt",
FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
This should be failry trivial to implement, but, I'm none too sure of my Java "cut and paste" abilities and would like to know if there is a much better way to do this. The big picture here is to troll some apache and tomcat logs looking for a few key entries based on application activity and take some action based on the entry. Anyway, any tips? Thank you!
Pick one from here
:)
I haven't try them all but there are some useful comments from Jon Skeet on how not to do it.
My bet will be for the nio one.
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return CharSet.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
See details in that question.

Categories