Skip to content

Commit b80323b

Browse files
committed
refactor(core): align logging/Throwable handling with 3.0.x (logic parity)
- SampleDocument: catch Throwable + LOG.warn (absorb macOS FOP font AssertionError) - WordprocessingMLTemplate: System.out.println -> LOG.debug (add slf4j LOG field) - PackageBuilder: printStackTrace -> LOG.warn (add LOG field) Verified (JDK 8): core 352 tests green.
1 parent 282ea7b commit b80323b

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎easydoc-core/src/main/java/io/github/easy4j/doc/SampleDocument.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313

1414
import org.docx4j.XmlUtils;
1515
import org.docx4j.fonts.PhysicalFont;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1618
import org.docx4j.fonts.PhysicalFonts;
1719
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
1820

1921
/** This class generates a sample document in case no
2022
* document has been passed.
2123
*/
2224
public class SampleDocument {
25+
26+
private static final Logger LOG = LoggerFactory.getLogger(SampleDocument.class);
2327

2428
public static void createContent(MainDocumentPart wordDocumentPart ) {
2529
/*
@@ -63,8 +67,10 @@ public static void createContent(MainDocumentPart wordDocumentPart ) {
6367
}
6468

6569
}
66-
} catch (Exception e) {
67-
e.printStackTrace();
70+
} catch (Throwable e) {
71+
// 捕获 Throwable(含 AssertionError):macOS 上 FOP 字体解析
72+
// 会对特定系统字体抛 AssertionError,不能让其中断文档生成
73+
LOG.warn("Failed to create sample document content", e);
6874
}
6975

7076
}

‎easydoc-core/src/main/java/io/github/easy4j/doc/WordprocessingMLTemplate.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Map;
2222

2323
import org.docx4j.Docx4J;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
2527

2628
/**
@@ -29,6 +31,9 @@
2931
* @author <a href="https://github.com/loong10k">Loong Wan</a>
3032
*/
3133
public interface WordprocessingMLTemplate {
34+
35+
Logger LOG = LoggerFactory.getLogger(WordprocessingMLTemplate.class);
36+
3237

3338
/**
3439
* Contract interface for WordprocessingMLTemplate operations.
@@ -40,11 +45,11 @@ default WordprocessingMLPackage process(File template, Map<String, Object> varia
4045
WordprocessingMLPackage wordMLPackage;
4146
if (template == null || !template.exists() || !template.isFile() ) {
4247
// Create a docx
43-
System.out.println("No imput path passed, creating dummy document");
48+
LOG.debug("No imput path passed, creating dummy document");
4449
wordMLPackage = WordprocessingMLPackage.createPackage();
4550
SampleDocument.createContent(wordMLPackage.getMainDocumentPart());
4651
} else {
47-
System.out.println("Loading file from " + template.getAbsolutePath());
52+
LOG.debug("Loading file from {}", template.getAbsolutePath());
4853
wordMLPackage = Docx4J.load(template);
4954
}
5055
return wordMLPackage;
@@ -60,11 +65,11 @@ default WordprocessingMLPackage process(InputStream template, Map<String, Object
6065
WordprocessingMLPackage wordMLPackage;
6166
if (template == null) {
6267
// Create a docx
63-
System.out.println("No imput path passed, creating dummy document");
68+
LOG.debug("No imput path passed, creating dummy document");
6469
wordMLPackage = WordprocessingMLPackage.createPackage();
6570
SampleDocument.createContent(wordMLPackage.getMainDocumentPart());
6671
} else {
67-
System.out.println("Loading file from InputStream");
72+
LOG.debug("Loading file from InputStream");
6873
wordMLPackage = Docx4J.load(template);
6974
}
7075
return wordMLPackage;

‎easydoc-xhtml/src/main/java/io/github/easy4j/doc/xhtml/io/WordprocessingMLPackageBuilder.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
public class WordprocessingMLPackageBuilder {
4242

43+
private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(WordprocessingMLPackageBuilder.class);
44+
45+
4346
protected DocumentHandler docHandler = XHTMLDocumentHandler.getDocumentHandler();
4447

4548
private static final WordprocessingMLPackageBuilder WML_PACKAGE_BUILDER = new WordprocessingMLPackageBuilder();
@@ -75,7 +78,7 @@ public WordprocessingMLPackageBuilder configDefaultFont(WordprocessingMLPackage
7578
PhysicalFontUtils.setDefaultFont(wmlPackage, fontName);
7679
} catch (Exception e) {
7780
// TODO Auto-generated catch block
78-
e.printStackTrace();
81+
LOG.warn("Failed to set fonts", e);
7982
}
8083
//返回WordprocessingMLPackage对象
8184
return this;

0 commit comments

Comments
 (0)