Skip to content

Commit b0a9c0b

Browse files
committed
通过装饰器修饰字体路径校验
1 parent ce58f42 commit b0a9c0b

14 files changed

+150
-41
lines changed

data/txt2.pdf

-138 KB
Binary file not shown.

src/main/java/com/liumapp/workable/converter/WorkableConverter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import com.liumapp.workable.converter.core.Converter;
66
import com.liumapp.workable.converter.core.Parameter;
77
import com.liumapp.workable.converter.decorators.CheckPrefixFormatDecorator;
8+
import com.liumapp.workable.converter.decorators.CheckingParamsForTextConverterDecorator;
89
import com.liumapp.workable.converter.decorators.ConnectAndStartLocalLibreOfficeDecorator;
910
import com.liumapp.workable.converter.exceptions.ConvertFailedException;
1011
import com.liumapp.workable.converter.factory.ConverterConfigManager;
12+
import com.liumapp.workable.converter.strategies.TextConverter;
1113
import lombok.Getter;
1214
import lombok.Setter;
1315

@@ -36,7 +38,12 @@ public WorkableConverter() {
3638
*/
3739
@Override
3840
public boolean convert(Parameter require) throws ConvertFailedException {
39-
Converter converter = new ConnectAndStartLocalLibreOfficeDecorator(new CheckPrefixFormatDecorator(converterType));
41+
Converter converter;
42+
if (converterType instanceof TextConverter) {
43+
converter = new ConnectAndStartLocalLibreOfficeDecorator(new CheckPrefixFormatDecorator(new CheckingParamsForTextConverterDecorator(converterType)));
44+
} else {
45+
converter = new ConnectAndStartLocalLibreOfficeDecorator(new CheckPrefixFormatDecorator(converterType));
46+
}
4047
return converter.convert(require);
4148
}
4249
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.liumapp.workable.converter.decorators;
2+
3+
import com.liumapp.workable.converter.config.ConvertRequire;
4+
import com.liumapp.workable.converter.core.Converter;
5+
import com.liumapp.workable.converter.core.Parameter;
6+
import com.liumapp.workable.converter.exceptions.ConvertFailedException;
7+
import com.liumapp.workable.converter.factory.ConverterConfigManager;
8+
import com.liumapp.workable.converter.templates.ConverterStrategyTemplates;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* file CheckingParamsForTextConverterDecorator.java
14+
* author liumapp
15+
* github https://github.com/liumapp
16+
17+
* homepage http://www.liumapp.com
18+
* date 2019/5/9
19+
*/
20+
public class CheckingParamsForTextConverterDecorator extends ConverterStrategyTemplates {
21+
22+
23+
public CheckingParamsForTextConverterDecorator(Converter converterStrategy) {
24+
super(converterStrategy);
25+
}
26+
27+
@Override
28+
protected boolean accordingRequire(ConvertRequire require) throws ConvertFailedException {
29+
return super.accordingRequire(require);
30+
}
31+
32+
@Override
33+
public boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
34+
return false;
35+
}
36+
37+
38+
@Override
39+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
40+
return false;
41+
}
42+
43+
@Override
44+
public boolean byStream(ConvertRequire require) throws ConvertFailedException {
45+
return false;
46+
}
47+
48+
@Override
49+
public boolean byBase64(ConvertRequire require) throws ConvertFailedException {
50+
return false;
51+
}
52+
53+
@Override
54+
protected String saveTmpFileByBase64(String fileBase64, String prefix) throws IOException {
55+
return super.saveTmpFileByBase64(fileBase64, prefix);
56+
}
57+
58+
@Override
59+
protected String saveTmpFileByBytes(byte[] fileBytes, String prefix) throws IOException {
60+
return super.saveTmpFileByBytes(fileBytes, prefix);
61+
}
62+
63+
@Override
64+
public boolean convert(Parameter require) throws ConvertFailedException {
65+
String ttfPath = ConverterConfigManager.getInstance().getParams().getFontPath();
66+
67+
if (ttfPath == null) {
68+
throw new ConvertFailedException("fonts path must need");
69+
}
70+
return super.convert(require);
71+
}
72+
}

src/main/java/com/liumapp/workable/converter/strategies/CommonConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public boolean convert(Parameter require) throws ConvertFailedException {
3838
}
3939

4040
@Override
41-
protected boolean byFilePath (ConvertRequire require) throws ConvertFailedException {
41+
public boolean byFilePath (ConvertRequire require) throws ConvertFailedException {
4242
logger.info("input file path is : " + require.getWaitingFilePath());
4343
logger.info("output file path is : " + require.getResultFilePath());
4444
File inputFile = new File(require.getWaitingFilePath());
@@ -54,12 +54,12 @@ protected boolean byFilePath (ConvertRequire require) throws ConvertFailedExcept
5454

5555
// todo
5656
@Override
57-
protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
57+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
5858
throw new ConvertFailedException("common converter do not support convert by file to folder pattern right now");
5959
}
6060

6161
@Override
62-
protected boolean byStream (ConvertRequire require) throws ConvertFailedException {
62+
public boolean byStream (ConvertRequire require) throws ConvertFailedException {
6363
try {
6464
JodConverter.convert(require.getSrcStream()).as(require.getSrcFormat())
6565
.to(require.getDestStream()).as(require.getDestFormat()).execute();
@@ -73,7 +73,7 @@ protected boolean byStream (ConvertRequire require) throws ConvertFailedExceptio
7373
}
7474

7575
@Override
76-
protected boolean byBase64 (ConvertRequire require) throws ConvertFailedException {
76+
public boolean byBase64 (ConvertRequire require) throws ConvertFailedException {
7777
ByteArrayInputStream inputStream = null;
7878
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
7979
try {

src/main/java/com/liumapp/workable/converter/strategies/ConverterStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ protected boolean accordingRequire(ConvertRequire require) throws ConvertFailedE
3333
throw new ConvertFailedException("can not found convert patterns .");
3434
};
3535

36-
protected abstract boolean byFilePath (ConvertRequire require) throws ConvertFailedException;
36+
public abstract boolean byFilePath (ConvertRequire require) throws ConvertFailedException;
3737

38-
protected abstract boolean byFileFolder (ConvertRequire require) throws ConvertFailedException;
38+
public abstract boolean byFileFolder (ConvertRequire require) throws ConvertFailedException;
3939

40-
protected abstract boolean byStream (ConvertRequire require) throws ConvertFailedException;
40+
public abstract boolean byStream (ConvertRequire require) throws ConvertFailedException;
4141

42-
protected abstract boolean byBase64 (ConvertRequire require) throws ConvertFailedException;
42+
public abstract boolean byBase64 (ConvertRequire require) throws ConvertFailedException;
4343

4444

4545
protected String saveTmpFileByBase64 (String fileBase64, String prefix) throws IOException {

src/main/java/com/liumapp/workable/converter/strategies/DocToPdfConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected boolean accordingRequire(ConvertRequire require) throws ConvertFailedE
4343
}
4444

4545
@Override
46-
protected boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
46+
public boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
4747
logger.info("get waiting convert file : " + require.getWaitingFilePath());
4848
logger.info("get result file path : " + require.getResultFilePath());
4949
File inputFile = new File(require.getWaitingFilePath());
@@ -57,17 +57,17 @@ protected boolean byFilePath(ConvertRequire require) throws ConvertFailedExcepti
5757
}
5858

5959
@Override
60-
protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
60+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
6161
return false;
6262
}
6363

6464
@Override
65-
protected boolean byStream(ConvertRequire require) throws ConvertFailedException {
65+
public boolean byStream(ConvertRequire require) throws ConvertFailedException {
6666
return false;
6767
}
6868

6969
@Override
70-
protected boolean byBase64(ConvertRequire require) throws ConvertFailedException {
70+
public boolean byBase64(ConvertRequire require) throws ConvertFailedException {
7171
return false;
7272
}
7373
}

src/main/java/com/liumapp/workable/converter/strategies/DocToPngConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ protected boolean accordingRequire(ConvertRequire require) throws ConvertFailedE
3131
}
3232

3333
@Override
34-
protected boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
34+
public boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
3535
return false;
3636
}
3737

3838
@Override
39-
protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
39+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
4040
return false;
4141
}
4242

4343
@Override
44-
protected boolean byStream(ConvertRequire require) throws ConvertFailedException {
44+
public boolean byStream(ConvertRequire require) throws ConvertFailedException {
4545
return false;
4646
}
4747

4848
@Override
49-
protected boolean byBase64(ConvertRequire require) throws ConvertFailedException {
49+
public boolean byBase64(ConvertRequire require) throws ConvertFailedException {
5050
return false;
5151
}
5252
}

src/main/java/com/liumapp/workable/converter/strategies/HtmlToPdfConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected boolean accordingRequire(ConvertRequire require) throws ConvertFailedE
3939
}
4040

4141
@Override
42-
protected boolean byFilePath (ConvertRequire require) {
42+
public boolean byFilePath (ConvertRequire require) {
4343
logger.info("get waiting convert html file path : " + require.getWaitingFilePath());
4444
logger.info("get result file path : " + require.getResultFilePath());
4545
File inputFile = new File(require.getWaitingFilePath());
@@ -53,17 +53,17 @@ protected boolean byFilePath (ConvertRequire require) {
5353
}
5454

5555
@Override
56-
protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
56+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
5757
return false;
5858
}
5959

6060
@Override
61-
protected boolean byStream(ConvertRequire require) throws ConvertFailedException {
61+
public boolean byStream(ConvertRequire require) throws ConvertFailedException {
6262
return false;
6363
}
6464

6565
@Override
66-
protected boolean byBase64(ConvertRequire require) throws ConvertFailedException {
66+
public boolean byBase64(ConvertRequire require) throws ConvertFailedException {
6767
return false;
6868
}
6969

src/main/java/com/liumapp/workable/converter/strategies/ItextConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,22 @@ private PdfDocument createPdfDoc(PdfWriter pdfWriter) {
114114
}
115115

116116
@Override
117-
protected boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
117+
public boolean byFilePath(ConvertRequire require) throws ConvertFailedException {
118118
throw new ConvertFailedException("text converter do not support by file path");
119119
}
120120

121121
@Override
122-
protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
122+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
123123
throw new ConvertFailedException("text converter do not support by file folder");
124124
}
125125

126126
@Override
127-
protected boolean byStream(ConvertRequire require) throws ConvertFailedException {
127+
public boolean byStream(ConvertRequire require) throws ConvertFailedException {
128128
throw new ConvertFailedException("text converter do not support by file stream");
129129
}
130130

131131
@Override
132-
protected boolean byBase64(ConvertRequire require) throws ConvertFailedException {
132+
public boolean byBase64(ConvertRequire require) throws ConvertFailedException {
133133
String content = require.getEditorRequire().getContent();
134134
String tempFile = ItextConverter.RESOURCE_PREFIX_INDEX;
135135
createDirs(tempFile);

src/main/java/com/liumapp/workable/converter/strategies/PdfBoxConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public boolean convert (Parameter require) throws ConvertFailedException {
4646
*/
4747
@Deprecated
4848
@Override
49-
protected boolean byFilePath (ConvertRequire require) throws ConvertFailedException {
49+
public boolean byFilePath (ConvertRequire require) throws ConvertFailedException {
5050
throw new ConvertFailedException("pdf box converter do not support by file path convert pattern right now");
5151
}
5252

5353
@Override
54-
protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
54+
public boolean byFileFolder(ConvertRequire require) throws ConvertFailedException {
5555
logger.info("pdfbox convert by file folder begin(src file must be a pdf file) :");
5656
try {
5757
File srcFile = new File(require.getWaitingFilePath());
@@ -75,15 +75,15 @@ protected boolean byFileFolder(ConvertRequire require) throws ConvertFailedExcep
7575
*/
7676
@Deprecated
7777
@Override
78-
protected boolean byStream (ConvertRequire require) throws ConvertFailedException {
78+
public boolean byStream (ConvertRequire require) throws ConvertFailedException {
7979
throw new ConvertFailedException("pdf box converter do not support by stream right now");
8080
}
8181

8282
/**
8383
* convert a pdf base64 file to png pics
8484
*/
8585
@Override
86-
protected boolean byBase64 (ConvertRequire require) throws ConvertFailedException {
86+
public boolean byBase64 (ConvertRequire require) throws ConvertFailedException {
8787
logger.info("pdfbox convert by base64 begin(src file must be a pdf file):");
8888
try {
8989
String srcFileTmpName = StrRandomTool.getUuid(true) + ".pdf";

0 commit comments

Comments
 (0)