Преглед на файлове

去除对自定义字体com.uas.report.msyahei的依赖,编译时修改jrxml源码中的字体

sunyj преди 8 години
родител
ревизия
5f0946379a
променени са 3 файла, в които са добавени 69 реда и са изтрити 41 реда
  1. 0 5
      pom.xml
  2. 40 36
      src/main/java/com/uas/report/service/impl/PrintServiceImpl.java
  3. 29 0
      src/main/java/com/uas/report/util/FileUtils.java

+ 0 - 5
pom.xml

@@ -177,11 +177,6 @@
 		</dependency>
 
 		<!-- 自制中文字体 - 微软雅黑 -->
-		<dependency>
-			<groupId>com.uas.report</groupId>
-			<artifactId>msyahei</artifactId>
-			<version>1.0.0</version>
-		</dependency>
 		<dependency>
 			<groupId>com.uas.report</groupId>
 			<artifactId>microsoft-yahei-ui</artifactId>

+ 40 - 36
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -1,11 +1,9 @@
 package com.uas.report.service.impl;
 
-import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -48,6 +46,7 @@ import com.uas.report.model.Master;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.CollectionUtils;
+import com.uas.report.util.FileUtils;
 import com.uas.report.util.MasterManager;
 import com.uas.report.util.Platform;
 import com.uas.report.util.ReportConstants;
@@ -179,8 +178,8 @@ public class PrintServiceImpl implements PrintService {
 					jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
 
 				} else if (exportFileType.equals(ReportConstants.FILE_TYPE_WORD)) {
-					// 导出word时需要对字体、行距等进行调整
-					InputStream inputStream = new ByteArrayInputStream(replaceFont(jrxmlFilePath).getBytes());
+					// 导出word时需要对行距等进行调整
+					InputStream inputStream = new ByteArrayInputStream(modifyLineSpacing(jrxmlFilePath).getBytes());
 					JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
 					jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
 					inputStream.close();
@@ -232,8 +231,11 @@ public class PrintServiceImpl implements PrintService {
 	 * @param otherParameters
 	 * @param connection
 	 * @return 编译后的jasper文件路径
+	 * @throws IOException
+	 * @throws FileNotFoundException
 	 */
-	private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection) {
+	private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection)
+			throws FileNotFoundException, IOException {
 		if (StringUtils.isEmpty(jrxmlFilePath)) {
 			logger.error("参数不能为空:" + jrxmlFilePath);
 			return null;
@@ -263,12 +265,11 @@ public class PrintServiceImpl implements PrintService {
 			if (!jasperFile.exists()) {
 				logger.info("正在编译报表模板... " + jrxmlFilePath);
 				compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
-			} else {
-				// 如果在编译之后,报表模板有更改过 ,重新编译
-				if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
-					logger.info("正在重新编译报表模板... " + jrxmlFilePath);
-					compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
-				}
+			}
+			// 如果在编译之后,报表模板有更改过 ,重新编译
+			else if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
+				logger.info("正在重新编译报表模板... " + jrxmlFilePath);
+				compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
 			}
 		} catch (JRException e) {
 			logger.error("", e);
@@ -317,9 +318,12 @@ public class PrintServiceImpl implements PrintService {
 	 * @param connection
 	 *            数据库连接
 	 * @throws JRException
+	 * @throws IOException
+	 * @throws FileNotFoundException
 	 */
 	private void compileReportToFile(String jrxmlFilePath, String jasperFilePath, String otherParameters,
-			Connection connection) throws JRException {
+			Connection connection) throws JRException, FileNotFoundException, IOException {
+		replaceFont(jrxmlFilePath);
 		processFields(jrxmlFilePath, otherParameters, connection);
 		JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
 	}
@@ -413,31 +417,42 @@ public class PrintServiceImpl implements PrintService {
 	 * @throws IOException
 	 */
 	private Set<String> getUsedFields(String jrxmlFilePath) throws IOException {
-		BufferedReader bufferedReader = new BufferedReader(new FileReader(jrxmlFilePath));
-		String line = null;
+		String xml = FileUtils.readAsString(new File(jrxmlFilePath));
 		Set<String> result = new HashSet<>();
-		while ((line = bufferedReader.readLine()) != null) {
-			// 格式为$F{PD_TOTAL}
-			Pattern pattern = Pattern.compile("\\$F\\{([^\\$]*)\\}");
-			Matcher matcher = pattern.matcher(line);
-			while (matcher.find()) {
-				result.add(matcher.group(1).toUpperCase());
-			}
+		// 格式为$F{PD_TOTAL}
+		Pattern pattern = Pattern.compile("\\$F\\{([^\\$]*)\\}");
+		Matcher matcher = pattern.matcher(xml);
+		while (matcher.find()) {
+			result.add(matcher.group(1).toUpperCase());
 		}
-		bufferedReader.close();
 		return result;
 	}
 
 	/**
-	 * 替换jrxml中的字体等信息
+	 * 替换字体
 	 * 
 	 * @param jrxmlFilePath
-	 * @return
+	 * @throws IOException
+	 * @throws FileNotFoundException
+	 */
+	private void replaceFont(String jrxmlFilePath) throws FileNotFoundException, IOException {
+		File jrxmlFile = new File(jrxmlFilePath);
+		String xml = FileUtils.readAsString(jrxmlFile);
+		xml = xml.replace("MSYahei", ReportConstants.MICROSOFT_YAHEI_UI_FONT_NAME);
+		FileUtils.write(jrxmlFile, xml);
+	}
+
+	/**
+	 * 更改可换行的textField的行距
+	 * 
+	 * @param jrxmlFilePath
+	 * @return 修改后的jrxml源码
 	 * @throws FileNotFoundException
 	 * @throws IOException
 	 * @throws DocumentException
 	 */
-	private String replaceFont(String jrxmlFilePath) throws FileNotFoundException, IOException, DocumentException {
+	private String modifyLineSpacing(String jrxmlFilePath)
+			throws FileNotFoundException, IOException, DocumentException {
 		File jrxmlFile = new File(jrxmlFilePath);
 		SAXReader saxReader = new SAXReader();
 		Document document = saxReader.read(jrxmlFile);
@@ -445,17 +460,6 @@ public class PrintServiceImpl implements PrintService {
 		// 获取所有textElement节点(jrxml的XPath格式为:/*[name()='jasperReport']/*[name()='columnHeader']/*[name()='band']/*[name()='textField'])
 		List<Element> textElements = document.selectNodes("//*[name()='textElement']");
 		for (Element textElement : textElements) {
-			// 获取font节点
-			Element fontElement = (Element) textElement.selectSingleNode("*[name()='font']");
-			if (fontElement == null) {
-				continue;
-			}
-			// 将字体MSYahei改为Microsoft YaHei UI
-			Attribute fontName = fontElement.attribute("fontName");
-			if (Objects.equals(fontName.getText(), "MSYahei")) {
-				fontName.setText(ReportConstants.MICROSOFT_YAHEI_UI_FONT_NAME);
-			}
-
 			// 更改可换行的textField的行距
 			Element parent = textElement.getParent();
 			if (parent != null && Objects.equals(parent.getName(), "textField")) {

+ 29 - 0
src/main/java/com/uas/report/util/FileUtils.java

@@ -316,6 +316,35 @@ public class FileUtils {
 		return fileName.substring(0, fileName.lastIndexOf("."));
 	}
 
+	/**
+	 * 读取文件内容输出字符串
+	 * 
+	 * @param file
+	 *            文件
+	 * @return 读取的内容
+	 */
+	public static String readAsString(File file) throws FileNotFoundException, IOException {
+		if (!file.exists() || file.isDirectory()) {
+			return null;
+		}
+		FileInputStream stream = new FileInputStream(file);
+		return readAsString(stream);
+	}
+
+	/**
+	 * 读取输入流输出字符串
+	 * 
+	 * @param stream
+	 *            将要读的输入流
+	 * @return 读取的内容
+	 */
+	public static String readAsString(InputStream stream) throws FileNotFoundException, IOException {
+		// 以UTF-8编码方式读取文件
+		String result = new String(readData(stream), "UTF-8");
+		stream.close();
+		return result;
+	}
+
 	/**
 	 * Read the supplied InputStream and return as a byte array.
 	 *