collada 파일의 유효성 검사하기
/**
* Collada 유효성 검사
*/
package egovframework.pst.main.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import egovframework.pst.util.FileUtil;
import egovframework.rte.fdl.string.EgovStringUtil;
public class ColladaValidate {
private static final Logger LOGGER = LoggerFactory.getLogger(ColladaValidate.class);
private Map colladaSchema;
private String colladaSchemaFilename;
/**
* Collada Schema 정보 초기화
*/
public ColladaValidate() {
colladaSchema = new HashMap();
colladaSchema.put("1.4.1", "collada_schema_1_4_1.xsd");
colladaSchema.put("1.5", "collada_schema_1_5.xsd");
colladaSchemaFilename = colladaSchema.get("1.4.1"); //default 지정
}
/**
* 유효성 검사 실행
* @param mXSDFileFolder
* @param mXMLFileName
* @return
*/
public String test(String mXSDFileFolder, String mXMLFileName) {
String message = null;
setColladaVersion(mXMLFileName); //콜라다 파일 버전 세팅
String mXSDFileName = mXSDFileFolder + colladaSchemaFilename;
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File XSDFile = new File(mXSDFileName);
File XMLFile = new File(mXMLFileName);
InputStream inputStream = null;
try {
Schema schema = factory.newSchema(XSDFile);
Validator validator = schema.newValidator();
inputStream = new FileInputStream(XMLFile);
Source source = new StreamSource(inputStream);
try {
validator.validate(source);
LOGGER.debug(mXMLFileName + " validates.");
message = null;
}
catch (SAXException ex) {
//유효성 검사에 실패
LOGGER.debug(mXMLFileName + " fails to validate because: \n");
LOGGER.debug(ex.getMessage());
message = "* " + FileUtil.getFileName(mXMLFileName, "/") + " 파일의 유효성 검사에 실패하였습니다."
+ "\n" + ex.getMessage();
}
catch (IOException io) {
LOGGER.debug("Error reading XML source: " + mXMLFileName);
LOGGER.debug(io.getMessage());
message = "* " + FileUtil.getFileName(mXMLFileName, "/") + " 파일 읽기에 실패하였습니다.";
}
} catch (SAXException sch) {
LOGGER.debug("Error reading XML Schema: " + mXSDFileName);
message = "* " + FileUtil.getFileName(mXMLFileName, "/") + " 파일 읽기에 실패하였습니다.";
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return message;
}
/**
* Collada 파일의 version 확인하기
* Collada 파일을 열어 version 속성 값을 조회한다
* @param physicalPath
* @return
*/
private void setColladaVersion(String physicalPath){
String version = "";
InputStream inStreamXml = null;
try{
inStreamXml = new FileInputStream(physicalPath);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(inStreamXml);
doc.normalize();
NodeList nodeList = doc.getElementsByTagName("COLLADA"); //COLLADA 노드 목록 읽음
if (nodeList.getLength() > 0){
Node node = nodeList.item(0);
Element e = (Element)node; //COLLADA 요소로 변경
version = e.getAttribute("version"); //version 속성값 취득
}
} catch ( FileNotFoundException e ){
e.printStackTrace();
} catch ( ParserConfigurationException e ){
e.printStackTrace();
} catch ( SAXException e ){
e.printStackTrace();
} catch ( IOException e ){
e.printStackTrace();
} finally {
if (inStreamXml != null) {
try {
inStreamXml.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (!EgovStringUtil.isNull(version) && colladaSchema.containsKey(version)) {
colladaSchemaFilename = colladaSchema.get(version);
}
LOGGER.debug(">>> COLLADA file name " + colladaSchemaFilename);
}
}
아래의 자료를 참고하였다.
참고자료 주소 : https://github.com/amouat/xsd-validator