Java tutorial
/* * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Revision History * Author Date Description * --------------- ---------------- ------------ * Sang-cheon Park 2012. 10. 3. First Draft. */ package com.athena.chameleon.engine.core.analyzer.parser; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.athena.chameleon.common.utils.ThreadLocalUtil; import com.athena.chameleon.engine.constant.ChameleonConstants; import com.athena.chameleon.engine.entity.pdf.AnalyzeDefinition; import com.athena.chameleon.engine.entity.pdf.ExceptionInfo; /** * <pre> * * </pre> * * @author Sang-cheon Park * @version 1.0 */ public abstract class Parser { protected static final Logger logger = LoggerFactory.getLogger(Parser.class); protected AnalyzeDefinition analyzeDefinition; protected ExceptionInfo exceptionInfo; protected String location; protected String stackTrace; protected String comments; /** * <pre> * * </pre> * @param file * @param analyzeDefinition * @return */ public abstract Object parse(File file, AnalyzeDefinition analyzeDefinition); /** * <pre> * sourceFile? targetFile . * web.xml ?? encoding filter ? filter-mapping? ? /WEB-INF/lib ? osc-filters.jar ? ? ? * </pre> * @param sourceFile * @param targetFile * @throws IOException */ protected void fileCopy(File sourceFile, File targetFile) throws IOException { if (!targetFile.getParentFile().exists()) { targetFile.getParentFile().mkdirs(); } FileInputStream inputStream = new FileInputStream(sourceFile); FileOutputStream outputStream = new FileOutputStream(targetFile); IOUtils.copy(inputStream, outputStream); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(inputStream); }//end of fileCopy /** * <pre> * XML ? ?? . * </pre> * @param file * @param xmlData * @throws IOException */ protected void rewrite(File file, String xmlData) throws IOException { OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); output.write(xmlData); IOUtils.closeQuietly(output); }//end of rewrite() /** * <pre> * ?? ? ? . * </pre> * @param file * @return * @throws IOException */ protected String fileToString(String file) throws IOException { // return IOUtils.toString(file.toURI()); String result = null; DataInputStream in = null; File f = new File(file); byte[] buffer = new byte[(int) f.length()]; in = new DataInputStream(new FileInputStream(f)); in.readFully(buffer); result = new String(buffer); IOUtils.closeQuietly(in); return result; }//end of fileToString() /** * <pre> * ?? ? ? . * </pre> * @param fullPath * @param key * @return */ protected String removeTempDir(String fullPath, String key) { String tempPath = (String) ThreadLocalUtil.get(key); String earPath = (String) ThreadLocalUtil.get(ChameleonConstants.DEPLOY_DIR_IN_EAR); if (earPath == null) { earPath = ""; } else if (!earPath.endsWith(File.separator)) { earPath += File.separator; } if (StringUtils.isEmpty(tempPath) || fullPath.equals(tempPath)) { return earPath + fullPath; } else { return earPath + fullPath.substring(tempPath.length() + 1); } }//end of removeTempDir() /** * <pre> * <!DOCTYPE >? ? dtd DTD ? * ?? ? ? ?? UnmarshalException? ?. * ? file ? <!DOCTYPE >? ? . * </pre> * @param file */ protected void removeDoctype(File file) { try { String xmlData = fileToString(file.getAbsolutePath()); int idx1 = xmlData.indexOf("<!DOCTYPE"); int idx2 = xmlData.indexOf(">", idx1); if (idx1 > -1 && idx2 > -1) { rewrite(file, xmlData.substring(0, idx1) + xmlData.substring(idx2 + 1)); } } catch (IOException e) { logger.error("IOException has occurred.", e); } }//end of removeDoctype() }//end of Parser.java