mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-08-07 17:48:35 +00:00
ATS-532 : Code improvements (#89)
- move startup message from controllers to the Application classes (SpringBoot configuration beans) - added static imports for most static variables and static methods - simplified a few nested *if*s - replaced Arrays.asList() with explicit immutable collections - fixed a few IntelliJ code inspection warnings
This commit is contained in:
@@ -26,13 +26,17 @@
|
||||
*/
|
||||
package org.alfresco.transformer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
@@ -40,6 +44,8 @@ import io.micrometer.core.instrument.MeterRegistry;
|
||||
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
|
||||
public class Application
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
@Value("${container.name}")
|
||||
private String containerName;
|
||||
|
||||
@@ -53,4 +59,16 @@ public class Application
|
||||
{
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void startup()
|
||||
{
|
||||
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
|
||||
logger.info("The transformers in this project use libraries from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\\\ 2.0.txt");
|
||||
logger.info("Additional libraries used:");
|
||||
logger.info("* htmlparser http://htmlparser.sourceforge.net/license.html");
|
||||
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
|
||||
|
||||
logger.info("Starting application components... Done");
|
||||
}
|
||||
}
|
||||
|
@@ -64,18 +64,6 @@ public class MiscController extends AbstractTransformerController
|
||||
@Autowired
|
||||
private SelectingTransformer transformer;
|
||||
|
||||
public MiscController()
|
||||
{
|
||||
logger.info(
|
||||
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
|
||||
logger.info(
|
||||
"The transformers in this project use libraries from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\\\ 2.0.txt");
|
||||
logger.info("Additional libraries used:");
|
||||
logger.info("* htmlparser http://htmlparser.sourceforge.net/license.html");
|
||||
logger.info(
|
||||
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTransformerName()
|
||||
{
|
||||
|
@@ -26,6 +26,7 @@
|
||||
*/
|
||||
package org.alfresco.transformer.transformers;
|
||||
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IMAGE_JPEG;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_KEYNOTE;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_NUMBERS;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_PAGES;
|
||||
@@ -36,17 +37,17 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.transform.client.model.Mimetype;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
* Converts Apple iWorks files to JPEGs for thumbnailing & previewing.
|
||||
* The transformer will only work for iWorks 2013/14 files. Support for iWorks 2008/9 has been dropped as we cannot
|
||||
@@ -64,9 +65,9 @@ public class AppleIWorksContentTransformer implements SelectableTransformer
|
||||
AppleIWorksContentTransformer.class);
|
||||
|
||||
// Apple's zip entry names for previews in iWorks have changed over time.
|
||||
private static final List<String> PDF_PATHS = Arrays.asList(
|
||||
private static final List<String> PDF_PATHS = ImmutableList.of(
|
||||
"QuickLook/Preview.pdf"); // iWorks 2008/9
|
||||
private static final List<String> JPG_PATHS = Arrays.asList(
|
||||
private static final List<String> JPG_PATHS = ImmutableList.of(
|
||||
"QuickLook/Thumbnail.jpg", // iWorks 2008/9
|
||||
"preview.jpg"); // iWorks 2013/14 (720 x 552) We use the best quality image. Others are:
|
||||
// (225 x 173) preview-web.jpg
|
||||
@@ -76,10 +77,9 @@ public class AppleIWorksContentTransformer implements SelectableTransformer
|
||||
public boolean isTransformable(String sourceMimetype, String targetMimetype,
|
||||
Map<String, String> parameters)
|
||||
{
|
||||
boolean transformable = MIMETYPE_IWORK_KEYNOTE.equals(sourceMimetype)
|
||||
|| MIMETYPE_IWORK_NUMBERS.equals(sourceMimetype)
|
||||
|| MIMETYPE_IWORK_PAGES.equals(sourceMimetype);
|
||||
return transformable;
|
||||
return MIMETYPE_IWORK_KEYNOTE.equals(sourceMimetype) ||
|
||||
MIMETYPE_IWORK_NUMBERS.equals(sourceMimetype) ||
|
||||
MIMETYPE_IWORK_PAGES.equals(sourceMimetype);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,19 +88,16 @@ public class AppleIWorksContentTransformer implements SelectableTransformer
|
||||
final String sourceMimetype = parameters.get(SOURCE_MIMETYPE);
|
||||
final String targetMimetype = parameters.get(TARGET_MIMETYPE);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Performing IWorks to jpeg transform with sourceMimetype=" + sourceMimetype
|
||||
+ " targetMimetype=" + targetMimetype);
|
||||
}
|
||||
logger.debug("Performing IWorks to jpeg transform with sourceMimetype={} targetMimetype={}",
|
||||
sourceMimetype, targetMimetype);
|
||||
|
||||
// iWorks files are zip (or package) files.
|
||||
// If it's not a zip file, the resultant ZipException will be caught as an IOException below.
|
||||
try (ZipArchiveInputStream iWorksZip = new ZipArchiveInputStream(
|
||||
new BufferedInputStream(new FileInputStream(sourceFile))))
|
||||
{
|
||||
// Look through the zip file entries for the preview/thumbnail.
|
||||
List<String> paths = Mimetype.MIMETYPE_IMAGE_JPEG.equals(
|
||||
targetMimetype) ? JPG_PATHS : PDF_PATHS;
|
||||
List<String> paths = MIMETYPE_IMAGE_JPEG.equals(targetMimetype) ? JPG_PATHS : PDF_PATHS;
|
||||
ZipArchiveEntry entry;
|
||||
boolean found = false;
|
||||
while ((entry = iWorksZip.getNextZipEntry()) != null)
|
||||
|
@@ -85,8 +85,8 @@ public class HtmlParserContentTransformer implements SelectableTransformer
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
|
||||
Exception
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters)
|
||||
throws Exception
|
||||
{
|
||||
String sourceEncoding = parameters.get(SOURCE_ENCODING);
|
||||
checkEncodingParameter(sourceEncoding, SOURCE_ENCODING);
|
||||
@@ -113,20 +113,20 @@ public class HtmlParserContentTransformer implements SelectableTransformer
|
||||
}
|
||||
}
|
||||
|
||||
private void checkEncodingParameter(String encoding, String paramterName)
|
||||
private void checkEncodingParameter(String encoding, String parameterName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (encoding != null && !Charset.isSupported(encoding))
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
paramterName + "=" + encoding + " is not supported by the JVM.");
|
||||
parameterName + "=" + encoding + " is not supported by the JVM.");
|
||||
}
|
||||
}
|
||||
catch (IllegalCharsetNameException e)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
paramterName + "=" + encoding + " is not a valid encoding.");
|
||||
parameterName + "=" + encoding + " is not a valid encoding.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ public class HtmlParserContentTransformer implements SelectableTransformer
|
||||
String previousURL = getURL();
|
||||
String newURL = file.getAbsolutePath();
|
||||
|
||||
if ((previousURL == null) || (!newURL.equals(previousURL)))
|
||||
if (previousURL == null || !newURL.equals(previousURL))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@@ -26,17 +26,36 @@
|
||||
*/
|
||||
package org.alfresco.transformer.transformers;
|
||||
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IMAGE_JPEG;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_ADDIN;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_SPREADSHEET;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_ADDIN_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING_MACRO;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE;
|
||||
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.transform.client.model.Mimetype;
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.openxml4j.opc.PackagePart;
|
||||
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
||||
@@ -45,6 +64,8 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
* Extracts out Thumbnail JPEGs from OOXML files for thumbnailing & previewing.
|
||||
* This transformer will only work for OOXML files where thumbnailing was enabled,
|
||||
@@ -58,38 +79,39 @@ public class OOXMLThumbnailContentTransformer implements SelectableTransformer
|
||||
private static final Logger logger = LoggerFactory.getLogger(
|
||||
OOXMLThumbnailContentTransformer.class);
|
||||
|
||||
private static final List<String> OOXML_MIMETYPES = Arrays.asList(Mimetype.MIMETYPE_OPENXML_WORDPROCESSING,
|
||||
Mimetype.MIMETYPE_OPENXML_WORDPROCESSING_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE,
|
||||
Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_ADDIN,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE,
|
||||
Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_SPREADSHEET,
|
||||
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE,
|
||||
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_ADDIN_MACRO,
|
||||
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO);
|
||||
private static final List<String> OOXML_MIMETYPES = ImmutableList.of(
|
||||
MIMETYPE_OPENXML_WORDPROCESSING,
|
||||
MIMETYPE_OPENXML_WORDPROCESSING_MACRO,
|
||||
MIMETYPE_OPENXML_WORD_TEMPLATE,
|
||||
MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO,
|
||||
MIMETYPE_OPENXML_PRESENTATION,
|
||||
MIMETYPE_OPENXML_PRESENTATION_MACRO,
|
||||
MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW,
|
||||
MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO,
|
||||
MIMETYPE_OPENXML_PRESENTATION_TEMPLATE,
|
||||
MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO,
|
||||
MIMETYPE_OPENXML_PRESENTATION_ADDIN,
|
||||
MIMETYPE_OPENXML_PRESENTATION_SLIDE,
|
||||
MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO,
|
||||
MIMETYPE_OPENXML_SPREADSHEET,
|
||||
MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE,
|
||||
MIMETYPE_OPENXML_SPREADSHEET_MACRO,
|
||||
MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO,
|
||||
MIMETYPE_OPENXML_SPREADSHEET_ADDIN_MACRO,
|
||||
MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO);
|
||||
|
||||
@Override
|
||||
public boolean isTransformable(String sourceMimetype, String targetMimetype,
|
||||
Map<String, String> parameters)
|
||||
{
|
||||
// only support [OOXML] -> JPEG
|
||||
return Mimetype.MIMETYPE_IMAGE_JPEG.equals(targetMimetype) && OOXML_MIMETYPES.contains(
|
||||
sourceMimetype);
|
||||
return MIMETYPE_IMAGE_JPEG.equals(targetMimetype) &&
|
||||
OOXML_MIMETYPES.contains(sourceMimetype);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
|
||||
Exception
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters)
|
||||
throws Exception
|
||||
{
|
||||
final String sourceMimetype = parameters.get(SOURCE_MIMETYPE);
|
||||
final String targetMimetype = parameters.get(TARGET_MIMETYPE);
|
||||
|
@@ -49,8 +49,8 @@ public interface SelectableTransformer
|
||||
* @param parameters
|
||||
* @throws Exception
|
||||
*/
|
||||
void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
|
||||
Exception;
|
||||
void transform(File sourceFile, File targetFile, Map<String, String> parameters)
|
||||
throws Exception;
|
||||
|
||||
/**
|
||||
* Determine whether this transformer is applicable for the given MIME types.
|
||||
|
@@ -30,7 +30,6 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
@@ -42,6 +41,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
* The SelectingTransformer selects a registered {@link SelectableTransformer}
|
||||
* and delegates the transformation to its implementation.
|
||||
@@ -53,15 +54,17 @@ public class SelectingTransformer
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(SelectingTransformer.class);
|
||||
|
||||
private List<SelectableTransformer> transformers = new LinkedList<>();
|
||||
private final List<SelectableTransformer> transformers;
|
||||
|
||||
public SelectingTransformer()
|
||||
{
|
||||
transformers.add(new AppleIWorksContentTransformer());
|
||||
transformers.add(new HtmlParserContentTransformer());
|
||||
transformers.add(new StringExtractingContentTransformer());
|
||||
transformers.add(new TextToPdfContentTransformer());
|
||||
// transformers.add(new OOXMLThumbnailContentTransformer()); // Doesn't work with java 11, transformer and test disabled
|
||||
transformers = ImmutableList.of(
|
||||
new AppleIWorksContentTransformer(),
|
||||
new HtmlParserContentTransformer(),
|
||||
new StringExtractingContentTransformer(),
|
||||
new TextToPdfContentTransformer()
|
||||
// new OOXMLThumbnailContentTransformer()); // Doesn't work with java 11, transformer and test disabled
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,8 +80,8 @@ public class SelectingTransformer
|
||||
{
|
||||
try
|
||||
{
|
||||
SelectableTransformer transformer = selectTransformer(sourceMimetype, targetMimetype,
|
||||
parameters);
|
||||
final SelectableTransformer transformer = selectTransformer(sourceMimetype,
|
||||
targetMimetype, parameters);
|
||||
logOptions(sourceFile, targetFile, parameters);
|
||||
transformer.transform(sourceFile, targetFile, parameters);
|
||||
}
|
||||
@@ -109,11 +112,8 @@ public class SelectingTransformer
|
||||
{
|
||||
if (transformer.isTransformable(sourceMimetype, targetMimetype, parameters))
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Using " + transformer.getClass().getName()
|
||||
+ " to transform from " + sourceMimetype + " to " + targetMimetype);
|
||||
}
|
||||
logger.debug("Using {} to transform from {} to {}",
|
||||
transformer.getClass().getName(), sourceMimetype, targetMimetype);
|
||||
return transformer;
|
||||
}
|
||||
}
|
||||
@@ -139,9 +139,8 @@ public class SelectingTransformer
|
||||
|
||||
private String getExtension(File file)
|
||||
{
|
||||
String name = file.getName();
|
||||
final String name = file.getName();
|
||||
int i = name.lastIndexOf('.');
|
||||
String ext = i == -1 ? "???" : name.substring(i + 1);
|
||||
return ext;
|
||||
return i == -1 ? "???" : name.substring(i + 1);
|
||||
}
|
||||
}
|
||||
|
@@ -68,11 +68,10 @@ public class StringExtractingContentTransformer implements SelectableTransformer
|
||||
public boolean isTransformable(String sourceMimetype, String targetMimetype,
|
||||
Map<String, String> parameters)
|
||||
{
|
||||
boolean transformable = (sourceMimetype.startsWith("text/")
|
||||
|| MIMETYPE_JAVASCRIPT.equals(sourceMimetype)
|
||||
|| MIMETYPE_DITA.equals(sourceMimetype))
|
||||
&& MIMETYPE_TEXT_PLAIN.equals(targetMimetype);
|
||||
return transformable;
|
||||
return (sourceMimetype.startsWith("text/")
|
||||
|| MIMETYPE_JAVASCRIPT.equals(sourceMimetype)
|
||||
|| MIMETYPE_DITA.equals(sourceMimetype))
|
||||
&& MIMETYPE_TEXT_PLAIN.equals(targetMimetype);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,8 +83,8 @@ public class StringExtractingContentTransformer implements SelectableTransformer
|
||||
* be unformatted but valid.
|
||||
*/
|
||||
@Override
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
|
||||
Exception
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters)
|
||||
throws Exception
|
||||
{
|
||||
|
||||
String sourceEncoding = parameters.get(SOURCE_ENCODING);
|
||||
|
@@ -71,7 +71,7 @@ public class TextToPdfContentTransformer implements SelectableTransformer
|
||||
|
||||
public static final String PAGE_LIMIT = "pageLimit";
|
||||
|
||||
private PagedTextToPDF transformer;
|
||||
private final PagedTextToPDF transformer;
|
||||
|
||||
public TextToPdfContentTransformer()
|
||||
{
|
||||
@@ -108,18 +108,16 @@ public class TextToPdfContentTransformer implements SelectableTransformer
|
||||
public boolean isTransformable(String sourceMimetype, String targetMimetype,
|
||||
Map<String, String> parameters)
|
||||
{
|
||||
boolean transformable = ((MIMETYPE_TEXT_PLAIN.equals(sourceMimetype)
|
||||
|| MIMETYPE_TEXT_CSV.equals(sourceMimetype)
|
||||
|| MIMETYPE_DITA.equals(sourceMimetype)
|
||||
|| MIMETYPE_XML.equals(sourceMimetype))
|
||||
&& MIMETYPE_PDF.equals(targetMimetype));
|
||||
|
||||
return transformable;
|
||||
return (MIMETYPE_TEXT_PLAIN.equals(sourceMimetype) ||
|
||||
MIMETYPE_TEXT_CSV.equals(sourceMimetype) ||
|
||||
MIMETYPE_DITA.equals(sourceMimetype) ||
|
||||
MIMETYPE_XML.equals(sourceMimetype)) &&
|
||||
MIMETYPE_PDF.equals(targetMimetype);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
|
||||
Exception
|
||||
public void transform(File sourceFile, File targetFile, Map<String, String> parameters)
|
||||
throws Exception
|
||||
{
|
||||
String sourceEncoding = parameters.get(SOURCE_ENCODING);
|
||||
String stringPageLimit = parameters.get(PAGE_LIMIT);
|
||||
@@ -184,7 +182,7 @@ public class TextToPdfContentTransformer implements SelectableTransformer
|
||||
return STANDARD_14.get(name);
|
||||
}
|
||||
|
||||
private static final Map<String, PDType1Font> STANDARD_14 = new HashMap<String, PDType1Font>();
|
||||
private static final Map<String, PDType1Font> STANDARD_14 = new HashMap<>();
|
||||
|
||||
static
|
||||
{
|
||||
@@ -227,7 +225,7 @@ public class TextToPdfContentTransformer implements SelectableTransformer
|
||||
height = height * getFontSize() * 1.05f;
|
||||
doc = new PDDocument();
|
||||
BufferedReader data = new BufferedReader(text);
|
||||
String nextLine = null;
|
||||
String nextLine;
|
||||
PDPage page = new PDPage();
|
||||
PDPageContentStream contentStream = null;
|
||||
float y = -1;
|
||||
@@ -249,7 +247,7 @@ public class TextToPdfContentTransformer implements SelectableTransformer
|
||||
int lineIndex = 0;
|
||||
while (lineIndex < lineWords.length)
|
||||
{
|
||||
StringBuffer nextLineToDraw = new StringBuffer();
|
||||
final StringBuilder nextLineToDraw = new StringBuilder();
|
||||
float lengthIfUsingNextWord = 0;
|
||||
do
|
||||
{
|
||||
@@ -289,8 +287,7 @@ public class TextToPdfContentTransformer implements SelectableTransformer
|
||||
contentStream.setFont(getFont(), getFontSize());
|
||||
contentStream.beginText();
|
||||
y = page.getMediaBox().getHeight() - margin + height;
|
||||
contentStream.moveTextPositionByAmount(
|
||||
margin, y);
|
||||
contentStream.moveTextPositionByAmount(margin, y);
|
||||
}
|
||||
//System.out.println( "Drawing string at " + x + "," + y );
|
||||
|
||||
|
@@ -66,13 +66,12 @@ import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilde
|
||||
@Import({SelectingTransformer.class})
|
||||
public class MiscControllerTest extends AbstractTransformerControllerTest
|
||||
{
|
||||
|
||||
@Autowired
|
||||
private MiscController controller;
|
||||
|
||||
private String sourceEncoding = "UTF-8";
|
||||
private String targetEncoding = "UTF-8";
|
||||
private String targetMimetype = MIMETYPE_TEXT_PLAIN;
|
||||
private final String sourceEncoding = "UTF-8";
|
||||
private final String targetEncoding = "UTF-8";
|
||||
private final String targetMimetype = MIMETYPE_TEXT_PLAIN;
|
||||
|
||||
@Before
|
||||
public void before() throws Exception
|
||||
@@ -157,8 +156,8 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
|
||||
@Test
|
||||
public void testStringToString() throws Exception
|
||||
{
|
||||
String expected = null;
|
||||
byte[] content = null;
|
||||
String expected;
|
||||
byte[] content;
|
||||
try
|
||||
{
|
||||
content = "azAz10!<21>$%^&*()\t\r\n".getBytes(UTF_8);
|
||||
@@ -204,14 +203,13 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
|
||||
public void textToPdf() throws Exception
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String expected = null;
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
sb.append(i);
|
||||
sb.append(" I must not talk in class or feed my homework to my cat.\n");
|
||||
}
|
||||
sb.append("\nBart\n");
|
||||
expected = sb.toString();
|
||||
String expected = sb.toString();
|
||||
|
||||
MvcResult result = sendText("txt",
|
||||
"UTF-8",
|
||||
@@ -301,13 +299,12 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
|
||||
.param("sourceEncoding", sourceEncoding)
|
||||
.param("sourceMimetype", sourceMimetype);
|
||||
|
||||
MvcResult result = mockMvc
|
||||
return mockMvc
|
||||
.perform(requestBuilder)
|
||||
.andExpect(status().is(OK.value()))
|
||||
.andExpect(header().string("Content-Disposition",
|
||||
"attachment; filename*= " + targetEncoding + "''test_file." + targetExtension))
|
||||
.andReturn();
|
||||
return result;
|
||||
}
|
||||
|
||||
private String clean(String text)
|
||||
|
@@ -159,10 +159,8 @@ public class HtmlParserContentTransformerTest
|
||||
}
|
||||
}
|
||||
|
||||
private String readFromFile(File file, String encoding) throws Exception
|
||||
private String readFromFile(File file, final String encoding) throws Exception
|
||||
{
|
||||
String content = "wrong content";
|
||||
content = new String(Files.readAllBytes(file.toPath()), encoding);
|
||||
return content;
|
||||
return new String(Files.readAllBytes(file.toPath()), encoding);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user