3

I'm just getting started with Clojure and would like to call a Java method that takes as arguments an input file to read and an output file to which to write. Both appear to be of type java.io.File. The method I would like to call is "parse" in this class:

http://htmltolatex.sourceforge.net/javadoc/cz/kebrt/html2latex/Parser.html

However, because I will be calling the method repeatedly, I would prefer to use in-memory objects rather than files on disk.

I have successfully loaded an instance of the Parser class with:

(def my_parser (cz.kebrt.html2latex.Parser.))

I believe that I have successfully created an in-memory file-like object from which to read using this command:

(def input-object (java.io.StringBufferInputStream. "this is a test"))

However, what kind of file like object should I pass to capture the output? (For the sake of completeness, I should mention that this output file is first used to construct an instance of ParserHandler, which is then passed to the parser created above. http://htmltolatex.sourceforge.net/javadoc/cz/kebrt/html2latex/ParserHandler.html)

Thank you for any advice.

1 Answer 1

3

I'm pretty sure this (badly-designed) API is for an old version of the software which doesn't allow what you want to do. (i.e. you can't create a File whose contents is in-memory. That's not what the class is for.)

The latest version appears to have a constructor to which you can pass either a java.io.File or a String. The latter should be what you're after.

edit: I think it might be nice to clear up some stuff for you, since you seem to be coming from a Python background (given your repeated use of the term "file-like"). java.io.File is a misleading name. Its actually more like a path. For example, if you want to check if a file exists, you'd do (.exists (java.io.File. "my/path")). A File can also be a directory. It's dumb I know, but hey, it's java. (If you want to know more, look here). What this Parser class should really be providing is the ability to pass a java.io.Reader, which is like an abstract view over a sequence of characters. Internally they convert both the String option and the File option to a Reader, so its really bad design that they don't just accept a reader and cut out the middle man.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the clarification, D.J. And yes, I am coming from Python.
I reinstalled the java program from svn into my Clojure project as you suggested. Does this call look correct now? (It is throwing errors, for me): (cz.kebrt.html2latex.Parser. "this is a test")
Well first, the class has been moved to cz.kebrt.html2latex.parser.Parser, and second, I had a proper poke around the source and I'm not sure that it does what you want when constructed with a String. In this case, it creates a default ParserHandler, which, as far as I can tell, won't be outputting anything to anywhere. My best guess is that they use it for unit tests. I don't think you can do what you want without modifying the source. If you do want to modify the source, it might be OK to just create a new constructor that takes a String and a ParserHandler. I can't guarantee it though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.