Skip to content

Commit

Permalink
feat: **reword** internal refactoring
Browse files Browse the repository at this point in the history
- unify ContentChecker/DocumentValidator into Checker
- refactor Checkers into AbstractChecker +  PublicationResourceChecker
  • Loading branch information
rdeltour committed Nov 14, 2021
1 parent 9435936 commit 39888e2
Show file tree
Hide file tree
Showing 62 changed files with 647 additions and 1,217 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/adobe/epubcheck/api/EpubCheck.java
Expand Up @@ -32,13 +32,14 @@
import java.util.Properties;
import java.util.zip.ZipFile;

import org.w3c.epubcheck.constants.MIMEType;
import org.w3c.epubcheck.core.Checker;

import com.adobe.epubcheck.ctc.CheckManager;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.ocf.OCFChecker;
import com.adobe.epubcheck.ocf.OCFPackage;
import com.adobe.epubcheck.ocf.OCFZipPackage;
import com.adobe.epubcheck.opf.DocumentValidator;
import com.adobe.epubcheck.opf.OPFData;
import com.adobe.epubcheck.opf.ValidationContext.ValidationContextBuilder;
import com.adobe.epubcheck.util.DefaultReportImpl;
import com.adobe.epubcheck.util.EPUBVersion;
Expand All @@ -48,7 +49,7 @@
/**
* Public interface to epub validator.
*/
public class EpubCheck implements DocumentValidator
public class EpubCheck implements Checker
{
private static String VERSION = null;
private static String BUILD_DATE = null;
Expand Down Expand Up @@ -199,10 +200,9 @@ public void setLocale(Locale locale)
/**
* Validate the file. Return true if no errors or warnings found.
*/
public boolean validate()
public void check()
{
int validateResult = doValidate();
return validateResult == 0;
doValidate();
}

public int doValidate()
Expand All @@ -221,7 +221,7 @@ public int doValidate()
OCFPackage ocf = new OCFZipPackage(zip);
OCFChecker checker = new OCFChecker(new ValidationContextBuilder()
.path(epubFile.getAbsolutePath()).ocf(ocf).report(report).profile(profile).build());
checker.runChecks();
checker.check();

String extension = ResourceUtil.getExtension(epubFile.getName());
checkExtension(ocf, extension);
Expand Down Expand Up @@ -267,7 +267,7 @@ void checkExtension(OCFPackage ocf, String extension)
}
else
{
List<String> opfPaths = ocf.getOcfData().getEntries(OPFData.OPF_MIME_TYPE);
List<String> opfPaths = ocf.getOcfData().getEntries(MIMEType.PACKAGE_DOC.toString());
if (!opfPaths.isEmpty()) {
if(ocf.getOpfData().get(opfPaths.get(0)).getVersion() == EPUBVersion.VERSION_3) {
report.message(MessageId.PKG_024, EPUBLocation.create(epubFile.getName(), extension));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/adobe/epubcheck/api/EpubCheckFactory.java
Expand Up @@ -25,11 +25,11 @@
import java.io.File;
import java.io.IOException;

import com.adobe.epubcheck.opf.DocumentValidator;
import com.adobe.epubcheck.opf.DocumentValidatorFactory;
import org.w3c.epubcheck.core.Checker;

import com.adobe.epubcheck.opf.ValidationContext;

public class EpubCheckFactory implements DocumentValidatorFactory
public class EpubCheckFactory
{
static private final EpubCheckFactory instance = new EpubCheckFactory();

Expand All @@ -38,7 +38,7 @@ static public EpubCheckFactory getInstance()
return instance;
}

public DocumentValidator newInstance(ValidationContext context)
public Checker newInstance(ValidationContext context)
{
if (context.path.startsWith("http://") || context.path.startsWith("https://"))
{
Expand Down
88 changes: 30 additions & 58 deletions src/main/java/com/adobe/epubcheck/bitmap/BitmapChecker.java
Expand Up @@ -34,33 +34,27 @@
import javax.imageio.stream.ImageInputStream;

import com.adobe.epubcheck.api.EPUBLocation;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.ocf.OCFPackage;
import com.adobe.epubcheck.ocf.OCFZipPackage;
import com.adobe.epubcheck.opf.ContentChecker;
import com.adobe.epubcheck.opf.PublicationResourceChecker;
import com.adobe.epubcheck.opf.ValidationContext;
import com.adobe.epubcheck.util.CheckUtil;

public class BitmapChecker implements ContentChecker
public class BitmapChecker extends PublicationResourceChecker
{
private final OCFPackage ocf;
private final Report report;
private final String path;
private final String mimeType;
private static final int HEIGHT_MAX = 2 * 1080;
private static final int WIDTH_MAX = 2 * 1920;
private static final long IMAGESIZE_MAX = 4 * 1024 * 1024;

BitmapChecker(OCFPackage ocf, Report report, String path, String mimeType)
public BitmapChecker(ValidationContext context)
{
this.ocf = ocf;
this.report = report;
this.path = path;
this.mimeType = mimeType;
super(context);
}

private void checkHeader(byte[] header)
{
String mimeType = context.mimeType;
boolean passed;
if (mimeType.equals("image/jpeg"))
{
Expand All @@ -77,7 +71,7 @@ else if (mimeType.equals("image/gif"))
}
if (!passed)
{
report.message(MessageId.OPF_029, EPUBLocation.create(this.ocf.getName()), path, mimeType);
report.message(MessageId.OPF_029, EPUBLocation.create(context.ocf.get().getName()), context.path, mimeType);
}
}

Expand All @@ -92,6 +86,7 @@ else if (mimeType.equals("image/gif"))
public ImageHeuristics getImageSizes(String imgFileName) throws
IOException
{
OCFPackage ocf = context.ocf.get();
int pos = imgFileName.lastIndexOf(".");
if (pos == -1)
{
Expand Down Expand Up @@ -186,7 +181,7 @@ public ImageHeuristics getImageSizes(String imgFileName) throws
}
}

private File getImageFile(OCFPackage ocf, String imgFileName) throws IOException
private static File getImageFile(OCFPackage ocf, String imgFileName) throws IOException
{
if (ocf.getClass() == OCFZipPackage.class)
{
Expand All @@ -212,7 +207,7 @@ public ImageHeuristics(int width, int height, long length)
}
}

private File getTempImageFile(OCFZipPackage ocf, String imgFileName) throws IOException
private static File getTempImageFile(OCFZipPackage ocf, String imgFileName) throws IOException
{
File file = null;
FileOutputStream os = null;
Expand Down Expand Up @@ -283,56 +278,33 @@ private void checkImageDimensions(String imageFileName)
}
}

public void runChecks()
@Override
protected boolean checkContent()
{
if (!ocf.hasEntry(path))
try (InputStream in = context.resourceProvider.getInputStream(context.path))
{
report.message(MessageId.RSC_001, EPUBLocation.create(this.ocf.getName()), path);
}
else if (!ocf.canDecrypt(path))
{
report.message(MessageId.RSC_004, EPUBLocation.create(this.ocf.getName()), path);
}
else
{
InputStream in = null;
try
if (in == null)
{
in = ocf.getInputStream(path);
if (in == null)
{
report.message(MessageId.RSC_001, EPUBLocation.create(this.ocf.getName()), path);
}
byte[] header = new byte[4];
int rd = CheckUtil.readBytes(in, header, 0, 4);
if (rd < 4)
{
report.message(MessageId.MED_004, EPUBLocation.create(path));
}
else
{
checkHeader(header);
}
checkImageDimensions(path);
report.message(MessageId.RSC_001, EPUBLocation.create(context.path));
return false;
}
catch (IOException e)
byte[] header = new byte[4];
int rd = CheckUtil.readBytes(in, header, 0, 4);
if (rd < 4)
{
report.message(MessageId.PKG_021, EPUBLocation.create(path, path));
report.message(MessageId.MED_004, EPUBLocation.create(context.path));
}
finally
else
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException ignored)
{
// eat it
}
checkHeader(header);
}
checkImageDimensions(context.path);
return true;
}
}
catch (IOException e)
{
report.message(MessageId.PKG_021, EPUBLocation.create(context.path, context.path));
return false;
}
}
}
41 changes: 0 additions & 41 deletions src/main/java/com/adobe/epubcheck/bitmap/BitmapCheckerFactory.java

This file was deleted.

44 changes: 21 additions & 23 deletions src/main/java/com/adobe/epubcheck/css/CSSChecker.java
Expand Up @@ -32,17 +32,12 @@
import org.idpf.epubcheck.util.css.CssSource;

import com.adobe.epubcheck.api.EPUBLocation;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.opf.ContentChecker;
import com.adobe.epubcheck.opf.PublicationResourceChecker;
import com.adobe.epubcheck.opf.ValidationContext;

public class CSSChecker implements ContentChecker
public class CSSChecker extends PublicationResourceChecker
{
private final ValidationContext context;
private final Report report;
private final String path; // css file path when Mode.FILE, host path when
// Mode.STRING
private final Mode mode;

// Below only used when checking css strings
Expand Down Expand Up @@ -75,27 +70,29 @@ public CSSChecker(ValidationContext context, String value, int line, boolean isS
private CSSChecker(ValidationContext context, Mode mode, String value, int line,
boolean isStyleAttribute)
{
this.context = context;
this.report = context.report;
this.path = context.path;
super(context);
this.mode = mode;
this.value = value;
this.line = line;
this.isStyleAttribute = isStyleAttribute;
}

@Override
protected boolean checkPublicationBeforeContent()
{
if (this.mode == Mode.FILE) {
return super.checkPublicationBeforeContent();
}
return true;
}

public void runChecks()
@Override
protected boolean checkContent()
{
CssSource source = null;

try
{
if (this.mode == Mode.FILE && !context.ocf.get().hasEntry(path))
{
report.message(MessageId.RSC_001, EPUBLocation.create(context.ocf.get().getName()),
path);
return;
}

CSSHandler handler = new CSSHandler(context);
if (this.mode == Mode.STRING && this.line > -1)
Expand All @@ -109,7 +106,7 @@ public void runChecks()
this.line = -1;
} catch (Exception e)
{
report.message(MessageId.PKG_008, EPUBLocation.create(path), e.getMessage());
report.message(MessageId.PKG_008, EPUBLocation.create(context.path), e.getMessage());
} finally
{
if (source != null)
Expand All @@ -127,6 +124,7 @@ public void runChecks()
}
}
}
return true;
}

CssSource getCssSource()
Expand All @@ -135,22 +133,22 @@ CssSource getCssSource()
CssSource source = null;
if (this.mode == Mode.FILE)
{
source = new CssSource(this.path, context.resourceProvider.getInputStream(this.path));
source = new CssSource(context.path, context.resourceProvider.getInputStream(context.path));
String charset;
if (source.getInputStream().getBomCharset().isPresent())
{
charset = source.getInputStream().getBomCharset().get().toLowerCase(Locale.ROOT);
if (!charset.equals("utf-8") && !charset.startsWith("utf-16"))
{
report.message(MessageId.CSS_004, EPUBLocation.create(path), charset);
report.message(MessageId.CSS_004, EPUBLocation.create(context.path), charset);
}
}
if (source.getInputStream().getCssCharset().isPresent())
{
charset = source.getInputStream().getCssCharset().get().toLowerCase(Locale.ROOT);
if (!charset.equals("utf-8") && !charset.startsWith("utf-16"))
{
report.message(MessageId.CSS_003, EPUBLocation.create(path, ""), charset);
report.message(MessageId.CSS_003, EPUBLocation.create(context.path, ""), charset);
}
}
}
Expand All @@ -169,13 +167,13 @@ void parseItem(CssSource source, CSSHandler handler)
}
else
{
new CssParser(context.locale).parse(new StringReader(this.value), this.path, handler, handler);
new CssParser(context.locale).parse(new StringReader(this.value), context.path, handler, handler);
}
}
else
{
new CssParser(context.locale)
.parseStyleAttribute(new StringReader(this.value), this.path, handler, handler);
.parseStyleAttribute(new StringReader(this.value), context.path, handler, handler);
}
}
}

0 comments on commit 39888e2

Please sign in to comment.