From cc2d46a0b278f5fc686ec849f44dea53878b1919 Mon Sep 17 00:00:00 2001 From: Salman Qureshi Date: Tue, 18 Sep 2012 10:15:37 +0500 Subject: [PATCH] Copy images for html output. WIP: Convert SVG to PNG for html output. --- .../rackspace/cloud/api/docs/WebHelpMojo.java | 1 + .../cloud/api/docs/builders/PDFBuilder.java | 1 - .../extensions/CopyAndTransformXProcStep.java | 6 +- .../extensions/CopyTransformImage.java | 105 +++++++++++++++++- .../calabash/extensions/RelativePath.java | 18 +-- src/main/resources/rackspace-library.xpl | 2 +- 6 files changed, 116 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/rackspace/cloud/api/docs/WebHelpMojo.java b/src/main/java/com/rackspace/cloud/api/docs/WebHelpMojo.java index 1dd6a60..307d399 100644 --- a/src/main/java/com/rackspace/cloud/api/docs/WebHelpMojo.java +++ b/src/main/java/com/rackspace/cloud/api/docs/WebHelpMojo.java @@ -519,6 +519,7 @@ public abstract class WebHelpMojo extends AbstractWebhelpMojo { int pos = targetExtQueryFile.lastIndexOf(File.separator); targetExtQueryFile = targetExtQueryFile.substring(0, pos); map.put("targetHtmlContentDir", baseDir+File.separator+targetExtQueryFile); + map.put("targetDir", baseDir.getAbsolutePath()+File.separator+"figures"); //makePdf is a POM configuration for generate-webhelp goal to control the execution of diff --git a/src/main/java/com/rackspace/cloud/api/docs/builders/PDFBuilder.java b/src/main/java/com/rackspace/cloud/api/docs/builders/PDFBuilder.java index d1b3bdc..6a7263c 100644 --- a/src/main/java/com/rackspace/cloud/api/docs/builders/PDFBuilder.java +++ b/src/main/java/com/rackspace/cloud/api/docs/builders/PDFBuilder.java @@ -135,7 +135,6 @@ public class PDFBuilder { // Extract all fonts into fonts directory // FileUtils.extractJaredDirectory("fonts",PDFBuilder.class,imageParentDirectory); - System.out.println("************************************** HELLO WORLD **************************************"); } public File processSources(Map map) throws MojoExecutionException{ diff --git a/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyAndTransformXProcStep.java b/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyAndTransformXProcStep.java index f438835..676e40b 100644 --- a/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyAndTransformXProcStep.java +++ b/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyAndTransformXProcStep.java @@ -10,6 +10,7 @@ import net.sf.saxon.s9api.QName; import net.sf.saxon.s9api.SaxonApiException; import net.sf.saxon.s9api.XdmNode; +import org.apache.commons.io.FilenameUtils; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.SystemStreamLog; @@ -77,7 +78,8 @@ public class CopyAndTransformXProcStep extends DefaultStep { private URI getTargetDirectoryURI() { RuntimeValue target = getOption(_target); - URI uri = target.getBaseURI().resolve(target.getString()); + URI uri = target.getBaseURI().resolve(FilenameUtils.normalizeNoEndSeparator(target.getString())); + return uri; } @@ -98,6 +100,8 @@ public class CopyAndTransformXProcStep extends DefaultStep { private XdmNode processInlineImages(XdmNode doc) { +// System.out.println("******************* getTargetDirectoryURI() = " + getTargetDirectoryURI()); +// System.out.println("******************* getTargetHtmlConteURI() = " + getTargetHtmlContentDirectoryURI()); CopyTransformImage xpathRepl = new CopyTransformImage( "//*:imagedata/@fileref", diff --git a/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyTransformImage.java b/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyTransformImage.java index c7bb6e8..314aa1b 100644 --- a/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyTransformImage.java +++ b/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/CopyTransformImage.java @@ -10,7 +10,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; -import java.util.Set; +import java.util.Random; import net.sf.saxon.s9api.SaxonApiException; import net.sf.saxon.s9api.XdmNode; @@ -18,6 +18,7 @@ import net.sf.saxon.s9api.XdmNode; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.image.PNGTranscoder; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.SystemStreamLog; @@ -36,6 +37,7 @@ import com.xmlcalabash.util.ProcessMatchingNodes; public class CopyTransformImage implements ProcessMatchingNodes { private String xpath; + private Map baseUriToDirMap = new HashMap(); private Map imageMap = new HashMap(); private ProcessMatch matcher; @@ -79,11 +81,13 @@ public class CopyTransformImage implements ProcessMatchingNodes { if (!file.exists()) { getLog().error("DocBook File: '" + inputDocbookName + "' - File: '" + sourceImageUri.getPath() + "' - Problem: File does not exist!"); //throw new XProcException(stepNode, "Cannot copy: file does not exist: " + file.getAbsolutePath()); + return null; } if (file.isDirectory()) { getLog().error("DocBook File: '" + inputDocbookName + "' - File: '" + sourceImageUri.getPath() + "' - Problem: File is a directory!"); //throw new XProcException(stepNode, "Cannot copy: file is a directory: " + file.getAbsolutePath()); + return null; } return file; @@ -107,7 +111,7 @@ public class CopyTransformImage implements ProcessMatchingNodes { throw new XProcException("Cannot copy: target file is a directory: " + target.getAbsolutePath()); } } - + try { FileInputStream src = new FileInputStream(file); FileOutputStream dst = new FileOutputStream(target); @@ -142,11 +146,94 @@ public class CopyTransformImage implements ProcessMatchingNodes { } private String processSelectedImage(XdmNode node) { + URI baseUri = node.getBaseURI(); + String fileRef = node.getStringValue(); + URI baseDirUri = baseUri.resolve("."); + + String srcImgFilePath = FilenameUtils.normalize(baseDirUri.getPath() + File.separator + fileRef); + File srcImgFile = getFileHandle(srcImgFilePath); + + if (outputType.equals("pdf")) { + //Need to check only for the existence of the image file + if (! fileExists(srcImgFile)) { + reportImageNotFoundError(baseUri, fileRef, srcImgFile); + return node.getStringValue(); + } else { + return node.getStringValue(); + } + } + else if (outputType.equals("html")) { + String targetDirPath = calculateTargetDirPath(baseUri.getPath(), fileRef); + File targetDir = makeDirs(targetDirPath); + //For HTML, we need a more elaborate check for missing images + if (fileExists(srcImgFile)) { + //simply copy the src file to the destination + File copiedFile = copyFile(srcImgFile, targetDir); + return RelativePath.getRelativePath(new File(targetHtmlContentDirectoryUri), copiedFile); + } + else if (fileExists(getFileHandle(FilenameUtils.removeExtension(srcImgFilePath) + ".svg"))) { + //convert the svg to the relevant type and copy + File copiedFile = copyFile(srcImgFile, targetDir); + return RelativePath.getRelativePath(new File(targetHtmlContentDirectoryUri), copiedFile); + } + else { + reportImageNotFoundError(baseUri, fileRef, srcImgFile); + return node.getStringValue(); + } + } + else { + //we only know how to handle "pdf" and "html" outputTypes so just return the value + return node.getStringValue(); + } + } + + private String calculateTargetDirPath(String baseUriPath, String fileRef) { + String targetDirForBaseUri = "" + (10 + new Random().nextInt(90)); + String targetDirForFileRef = FilenameUtils.getPathNoEndSeparator(fileRef.replaceAll("\\.\\.", "a")); + return targetDirForBaseUri + File.separator + + targetDirForFileRef; + } + + private File copyFile(File srcFile, File targetDir) { + try { + FileUtils.copyFileToDirectory(srcFile, targetDir); + } catch (IOException e) { + return null; + } + return new File(targetDir.getAbsolutePath() + File.separator + srcFile.getName()); + } + + private void reportImageNotFoundError(URI baseUri, String fileRef, File srcImgFile) { + System.out.println("**********************************************************"); + System.out.println("File NOT found : " + baseUri.getPath()); + } + + private boolean fileExists(File file) { + // TODO: Check for case sensitive file names + return file==null ? false : file.exists(); + } + + private File makeDirs(String relativePath) { + //TODO: handle cases where dirPath is path to a file + File dir = new File(targetDirectoryUri.getPath(), relativePath); + if (dir.mkdir() && dir.mkdirs()) { + + } + return dir; + } + + private File getFileHandle(String filePath) { + // TODO: Handle cases where the filePath refers to a directory + return new File(filePath); + } + + private String processSelectedImageOLD(XdmNode node) { + System.out.println("****** Processing node: " + node.toString() + " --- baseUri="+node.getBaseURI().toString()); String value = node.getStringValue(); if(imageMap.containsKey(value)) { - //System.out.println("\n\n*********** already processed **************** "+imageMap.get(value)); + System.out.println("\n\n*********** already processed **************** "+imageMap.get(value)); return imageMap.get(value); } @@ -182,6 +269,7 @@ public class CopyTransformImage implements ProcessMatchingNodes { imageMap.put(value, sourceImageFile.getAbsolutePath()); } else if(outputType.equals("html")) { + System.out.println("****** Entering elseif outputType=='html'"); //if the output type is html then we need to decide steps based on the image extension //if the image is an SVG then transform and save a png in the html target directory //if the image is a PNG then make a huge assumption that an SVG was specified in the Docbook before the png @@ -197,7 +285,7 @@ public class CopyTransformImage implements ProcessMatchingNodes { } sourceImageFile = getHandleToImageFile(sourceImageUri); if(sourceImageFile != null) { - + System.out.println("****** Case 1"); File copiedFile = performFileCopyAndTransformation(targetDirectoryUri, inputDocbookName, sourceImageFile); String correctRelativePathToImage = RelativePath.getRelativePath(new File(targetHtmlContentDirectoryUri), copiedFile); imageMap.put(value, correctRelativePathToImage); @@ -205,16 +293,21 @@ public class CopyTransformImage implements ProcessMatchingNodes { //File not found in source and target folders. } } else if(sourceImageFile != null && !isSVG) { - String correctRelativePathToImage = RelativePath.getRelativePath( new File(targetHtmlContentDirectoryUri), sourceImageFile); + System.out.println("****** Case 2: " + sourceImageFile); + File copiedFile = performFileCopyAndTransformation(targetDirectoryUri, inputDocbookName, sourceImageFile); + String correctRelativePathToImage = RelativePath.getRelativePath( new File(targetHtmlContentDirectoryUri), copiedFile); imageMap.put(value, correctRelativePathToImage); } else if(sourceImageFile != null && isSVG) { + System.out.println("****** Case 3"); //There really isn't any need to copy the SVG for html output but it is being done right now. We only care about transforming it to a PNG. File copiedFile = performFileCopyAndTransformation(targetDirectoryUri, inputDocbookName, sourceImageFile); String correctRelativePathToImage = RelativePath.getRelativePath( new File(targetHtmlContentDirectoryUri), copiedFile); imageMap.put(value, correctRelativePathToImage); - } + } + + System.out.println("****** Exiting elseif outputType=='html'"); } } catch (XProcException x) { //getLog().error(x.getMessage()); diff --git a/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/RelativePath.java b/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/RelativePath.java index 0bc01ab..eaf18ea 100644 --- a/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/RelativePath.java +++ b/src/main/java/com/rackspace/cloud/api/docs/calabash/extensions/RelativePath.java @@ -89,6 +89,11 @@ make sense homelist = getPathList(home); filelist = getPathList(f); s = matchPathLists(homelist,filelist); + + System.out.println("*************** Relative Path: ********************************"); + System.out.println("*** home: " + home.getAbsolutePath()); + System.out.println("*** file: " + f.getAbsolutePath()); + System.out.println("*** path: " + s); return s; } @@ -97,13 +102,10 @@ make sense * test the function */ public static void main(String args[]) { - if (args.length != 2) { - System.out.println("RelativePath "); - return; - } - System.out.println("home = " + args[0]); - System.out.println("file = " + args[1]); - System.out.println("path = " + getRelativePath(new File(args[0]),new - File(args[1]))); + String home = "/home/mtariq/content/myfolder"; + String file = "/home/myfolder/figures/fig.png"; + System.out.println("home = " + home); + System.out.println("file = " + file); + System.out.println("path = " + getRelativePath(new File(home),new File(file))); } } \ No newline at end of file diff --git a/src/main/resources/rackspace-library.xpl b/src/main/resources/rackspace-library.xpl index 6b61da3..ad8e197 100644 --- a/src/main/resources/rackspace-library.xpl +++ b/src/main/resources/rackspace-library.xpl @@ -824,7 +824,7 @@ - +