Epubcheck 3.0 for .Net

I was motivated to figure out if I could add EPUB 3.0 validation to a program I’d written in C#, so did some tinkering over the weekend to see what I could discover. I thought I’d jot down a few notes about getting epubcheck to work as a library inside of a C# program, in case it’s of any use to anyone thinking their only option is to shell out to the command line.

First off, I obviously didn’t try to port the program to C#, but used IKVM to wrap the program as a DLL. Of course, I didn’t really bother to read the documentation, and the result was getting all the way to an integrated solution only to discover that I’d missed all the dependencies epubcheck relies on when I ran it.

Assuming you’re looking for the step-by-step process to avoid just that kind of fun, and not interested in my ramblings and irritations, here’s the command line you need to use to compile epubcheck as a DLL:

ikvmc -target:library -sharedclassloader epubcheck-3.0.1.jar lib\commons-compress-1.4.1.jar lib\guava-13.0.1.jar lib\jing-20120724.0.0.jar lib\sac-1.3.jar lib\Saxon-HE-9.4.0.6.jar

(If you’re not using 3.0.1, check the actual dependencies in the lib directory.)

That’s it for getting a DLL. You can grab it from the output directory where you ran the command and add it to your project.

You’ll undoubtedly see a lot of warnings during the conversion, but so far I haven’t found a tangible impact on the running of epubcheck. At least some (the xerces warnings, for example), are solved by the inclusion of the IKVM DLLs in your project. For the ASP.NET program I was tinkering with, I found I needed to add these DLLs from the distro to the bin in order to get the program to run:

  • IKVM.OpenJDK.Core.dll
  • IKVM.OpenJDK.Security.dll
  • IKVM.OpenJDK.Text.dll
  • IKVM.OpenJDK.Util.dll
  • IKVM.OpenJDK.XML.API.dll
  • IKVM.OpenJDK.XML.Parse.dll
  • IKVM.Runtime.dll

The only remaining step was to add code to validate EPUBs, but to do that you only need to include the com.adobe.epubcheck.api class and create an EpubCheck object, just like in the epubcheck site examples:

using com.adobe.epubcheck.api;

java.io.File epubFile = new java.io.File("c:/epub-samples/childrens-media-query-20130726.epub");
EpubCheck epubcheck = new EpubCheck(epubFile);

Boolean result = epubcheck.validate();

At this point, all warning, errors and info are written to STDOUT, which is probably not what you want. To capture the information, you need to write your own implementation of the Report interface.

I’m not going to paste the code into this post, but you can download a direct port of epubcheck’s DefaultReportImpl class I wrote to get started. This interface can then be passed in to get information about the validation run:

EPCReportImpl report = new EPCReportImpl();

java.io.File epubFile = new java.io.File("c:/epub-samples/childrens-media-query-20130726.epub");
EpubCheck epubcheck = new EpubCheck(epubFile, report);

Boolean validates = epubcheck.validate();

if (!validates) {
   Console.WriteLine("Total Errors: " + report.getErrorCount().ToString());
}

Of course, you’ll probably want to go much further and stop writing everything to STDOUT and instead collect the info into your program, either as it’s generated or storing the messages and later accessing them.

But that’s just how easy it ended up being, to my surprise…

Caveat Emptor: I wrote the above code quickly and tested it thinly, so there’s always the possibility it might have some glitches. Feel free to post a comment below if you find anything.

Tags: , , ,

  1. adam k.’s avatar

    Thank you for this wonderful article. I just came across this and really amazed someone has figured it out.
    I am trying to run this within an ASP.NET app but whatever epub file i feed the result always returns 1 from the epubcheck.validate();

    Followed the steps, created epubcheck-3.0.1.dll, added references, compiled the class into EPCReportImpl.dll and finally the code Im running from asp.net/mvc page is:

    @using com.adobe.epubcheck.api;

    @{
    EPCReportImpl report = new EPCReportImpl();

    java.io.File epubFile = new java.io.File(“G:\\DDownloads\\2014January(17).epub”);
    EpubCheck epubcheck = new EpubCheck(epubFile, report);

    Boolean validates = epubcheck.validate();

    if (!validates) {
    Response.Write(“Total Errors: ” + report.getErrorCount().ToString());
    }
    }

    Not sure where Im making the mistake. Would you be able to e-mail me your project files if yours is working fine?

    Many thanks in advance!
    Adam

    Reply

    1. matt.garrish’s avatar

      If you want, you can download the dlls I compiled and see if they work better for you. I actually had to shelve what I was doing shortly after writing the article, so haven’t gone any deeper, but I was getting accurate validation reports. The only other code I have is in the post, as I was playing with a really basic asp.net page at the time.

      Reply

    2. adam k.’s avatar

      Thanks for the download link. The dll that I compiled matches them exactly.
      I figured the error was converting the Console.Writes for the ASPX equivalent.
      Added getErrors(), getWarnings() method to the EPCReportImpl class where methods contact (like _errors=_errors + “ERROR….”)
      Not sure if its the optimal way (Im no expert) but seems to work.

      Would like to thank you again for writing this article and being very helpful.
      Many thanks!

      Reply

    3. adam k.’s avatar

      While reading IBM developerworks site, i noticed one can run epubcheck against a single file (like nav file).
      Just out of curiosity, and thinking along the lines of editing an xhtml file online and using epubcheck mode param like a compiler/validation, would you know how to do this (i.e. pass the param –mode xxx) with the epubcheck(file,reportclass) call?

      reference from:http://www.ibm.com/developerworks/web/library/x-richlayoutepub/index.html

      $ java -jar ~/src/epubcheck-3.0b3.jar sample-toc.xhtml -mode nav
      Epubcheck Version 3.0b3

      WARNING: sample-toc.xhtml: File is validated as a single file of type nav and version 3!
      Only a subset of the available tests is run!

      No errors or warnings detected.

      Reply

    4. Henrik’s avatar

      Hi
      I’m getting a lot of compiler warnings about missing classes when trying to run your ikvmc command, I have all the jar files in place.
      What am I missing ?
      Br
      Henrik

      Reply

Reply

Your email address will not be published. Required fields are marked *