Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Monday, August 31, 2009

How to save an XML file in Java and Eclipse

One problem I faced today was saving the content of an XML document (org.w3c.dom.Document) to a newly created file.
The file I was outputting to was an Eclipse abstraction (precisely org.eclipse.core.resources.IFile) of a regular one and the method offered by this IFile for setting its contents is "IFile.setContents(InputStream source, boolean force, boolean keepHistory, IProgressMonitor monitor)".
I needed to bridge the gap between that org.w3c.dom.Document and the InputStream required by "IFile.setContents()", and here's the way I did it:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try
{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();

transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

PrintWriter writer =
new PrintWriter(outputStream);

transformer.transform(new DOMSource(document),
new XMLWriter(writer).toXMLResult());

writer.flush();
}
catch (TransformerConfigurationException exc)
{
// DEBUG
exc.printStackTrace();
}
catch (TransformerException exc)
{
// DEBUG
exc.printStackTrace();
}
InputStream source = new ByteArrayInputStream(outputStream.toByteArray());

The main classes being used here are from package "javax.xml.transform", with the method "Transformer.transform(javax.xml.transform.dom.Source source, Result result)" basically taking care of all the stuff.

I'm going to package this one into a library called "LifeSavingUtilities"...

Monday, February 23, 2009

Installing CubicTest

Have you ever been in need of an advanced testing tool that makes it easy to:
  • Test your web-applications by interacting with them as an human user would do
  • Let you define test-steps by drawing simple flow-chart-like diagrams
  • Auto-draw the test-steps by recording your actions from the browser window
  • Automatically re-execute so-recorded texts against a web-browser
Well, this kind of tool really exists: it's CubicTest and in the last days it really lived up to its promises.

In just one day I wrote a complete test-plan and linked each one of the tests planned to a testcase created with CubicTest.
And not only I "wrote" the tests by simply recording my activities inside the browser, but I also left CubicTest the task of executing them automatically, presenting me with the exact step where the failures occurred.

This way I obtained a "real" test-plan, consisting of human-readable, auto-generated and repeatable acceptance-tests.
Something I could easily bundle into an Eclipse application that could be used by my customers when they will try out my project.

Here's how to install CubicTest:
  1. Download and install Eclipse at http://download.eclipse.org
  2. Add the following update site to Eclipse: http://boss.bekk.no/cubictest/update/ (or download it from http://boss.bekk.no/cubictest/download.html)
  3. Select the CubicTest plugins and install them
  4. When asked whether to apply the changes or restart click "Apply Changes" and then manually shut-down Eclipse
  5. Open the eclipse.ini configuration file (/path/to/eclipse/eclipse.ini or C:\Program Files\Eclipse\eclipse.ini) and add the following line:
  6. On Linux: -DfirefoxDefaultPath=/path/to/firefox_installation/<firefox_executable>
    On Windows: -DfirefoxDefaultPath=C:\path\to\firefox_installation\firefox.exe
  7. Start Eclipse
  8. Follow the tutorials provided by the CubicTest Team and start using it

Let me know if you like this tool (by clicking the title of this post and leaving me a comment).

Wednesday, February 13, 2008

Overcome fear

Have you ever wondered about importing the Voice XML Schema (see http://www.w3.org/TR/voicexml21/) into an Eclipse EMF model? Well, I have, and the result was a bunch of really annoying errors that came out after about 3 minutes of CPU intense work.

Errors I wasn't prepared to. Just for being sure of what my eyes saw I validated the VXML XSD file (using XSV)... no errors found... mmmmhhh...

What to do next? Two options:
  • find why the Eclipse XML parser (I suspect it is Xerces) isn't working and fix it (I can only imagine the divine punishment I deserved by only thinking of it...) or try to adjust the XML Schema to avoid the errors (if you wanna give it a try I warn you that XML Schema is very hard not only to write but just to understand);
  • google the web to find a Good Samaritan who already solved this

You know? Only one person can solve this. His name is Tim Myer and I want to thank him for his really helpful post. Thank you Tim!

And you folks, whatareyouwaitingfor?!?! Go check Tim's post to make out of this dreadful situation and easily overcome your fear!

Wednesday, September 5, 2007

Eclox: Eclipse code documentation made easy.

Eclox is perfect if you need to add complex code documentation to your projects by using the Eclipse IDE and the Doxygen multi-language documentation tool.

You need to separately install Doxygen in order to use Eclox.

Here are some Screenshots (click to view full-size):

Sunday, August 12, 2007

Ubuntu: Switch from Gnu GCJ and GIJ to Sun JRE

If you experienced (I did, grrrrr...) some problems like Eclipse not running very well (e.g. CPU at 100% all the time) or Java-based apps stopping with errors, well Alec the Geek has a possible solution (a definitive solution won't be born until the guys at the GNU Foundation will decide to stop messing around with "Free as in speach..." or "Free as in beach..." or "Libertè, Egalitè, Penguinitè" and maybe will turn back to writing code and FIX GIGANTIC BUGS).

The idea is simple and useful even for other needs: you point the wrappers to the Java compiler and the Java bytecode interpreter to the official Sun JRE, instead of using the GNU implementation (which is the default Ubuntu behavior).
You do that by using the following command, sudo update-alternatives --config java , and by choosing the Sun JRE from the list (only if you have installed it, of course...).

Guys, repeat with me: "Thaaanks Aleeec"