com.adobe.epubcheck.ctc.xml.XMLContentDocParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of epubcheck Show documentation
Show all versions of epubcheck Show documentation
EpubCheck is a tool to validate IDPF EPUB files. It can detect many types of errors in EPUB.
OCF container structure, OPF and OPS mark-up, and internal reference consistency are checked.
EpubCheck can be run as a standalone command-line tool, installed as a Java server-side web application
or used as a Java library.
package com.adobe.epubcheck.ctc.xml;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.messages.MessageLocation;
import com.adobe.epubcheck.ocf.EncryptionFilter;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class XMLContentDocParser
{
private final ZipFile zip;
private final Hashtable enc;
private final Report report;
public XMLContentDocParser(ZipFile zip, Report report)
{
this.zip = zip;
this.enc = new Hashtable();
this.report = report;
}
public void parseDoc(String fileEntry, DefaultHandler handler)
{
InputStream is = null;
try
{
is = getInputStream(fileEntry);
SAXParserFactory factory = SAXParserFactory.newInstance();
//factory.setValidating(false);
//factory.setFeature("resolve-dtd-uris", false);
SAXParser saxParser = factory.newSAXParser();
final XMLReader parser = saxParser.getXMLReader();
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
parser.setFeature("http://xml.org/sax/features/validation", false);
parser.setDTDHandler(handler);
saxParser.parse(is, handler);
}
catch (FileNotFoundException e)
{
String message = e.getMessage();
message = new File(message).getName();
int p = message.indexOf("(");
if (p > 0)
{
message = message.substring(0, message.indexOf("("));
}
message = message.trim();
report.message(MessageId.RSC_001, new MessageLocation(fileEntry, -1, -1), message);
}
catch (IOException e)
{
report.message(MessageId.PKG_008, new MessageLocation(fileEntry, -1, -1), fileEntry);
}
catch (SAXException e)
{
report.message(MessageId.RSC_005, new MessageLocation(fileEntry, -1, -1), e.getMessage());
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (Exception ignore)
{
}
}
}
}
InputStream getInputStream(String name) throws
IOException
{
ZipEntry entry = zip.getEntry(name);
if (entry == null)
{
return null;
}
InputStream in = zip.getInputStream(entry);
EncryptionFilter filter = enc.get(name);
if (filter == null)
{
return in;
}
if (filter.canDecrypt())
{
return filter.decrypt(in);
}
return null;
}
}