package com.ocuf.oais.common;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.iso_relax.verifier.Schema;
import org.iso_relax.verifier.Verifier;
import org.iso_relax.verifier.VerifierFactory;
import org.iso_relax.verifier.VerifierHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
import com.ocuf.oais.module.ErrorModule;
/**
* 档案信息文件的验证
* @author CaoXiang
*
*/
public class ValidatXML {
private String realPath = this.getClass().getClassLoader().getResource("/").getPath();
/**
* 检测文件格式是否正确
* @param filename
* @return
*/
public List<ErrorModule> run(String filename) {
//保存错误信息的集合对象
List<ErrorModule> list = null;
try {
FileInputStream stream = new FileInputStream(realPath+"com//ocuf//oais//metadata//file//filetemp//"+filename);
//InputStream stream = this.getClass().getResourceAsStream(filename);
SAXReader reader = new SAXReader();
Document document = reader.read(stream);
list = process(document);
} catch (DocumentException e) {
System.out.println("发生错误: " + e);
Throwable nestedException = e.getNestedException();
if (nestedException != null) {
System.out.println("发生错误: " + nestedException);
nestedException.printStackTrace();
} else {
e.printStackTrace();
}
} catch (Throwable t) {
System.out.println("发生错误: " + t);
t.printStackTrace();
}
//返回
return list;
}
/**
* 检测文件格式是否有效
* @param document
* @return
* @throws Exception
*/
protected List<ErrorModule> process(Document document)
throws Exception {
//保存错误信息的集合对象
final List<ErrorModule> errorList = new ArrayList<ErrorModule>();
FileInputStream schemaStr = new FileInputStream(realPath+"com//ocuf//oais//metadata//file//filemodule//Archive.xsd");
//this.getClass().getResourceAsStream("com//ocuf//oais//metadata//file//filetemp//Archive.xsd");
VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
Schema schema = factory.compileSchema(schemaStr);
Verifier verifier = schema.newVerifier();
//各种类型的错误输出
verifier.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException e) {
ErrorModule errorModule = new ErrorModule();
errorModule.setId(String.valueOf(e.getLineNumber()));
errorModule.setDesctipt(e.getMessage());
errorModule.setLeavel("top");
errorModule.setType("error");
errorList.add(errorModule);
//System.out.println("错误: " + e);
}
public void fatalError(SAXParseException e) {
ErrorModule errorModule = new ErrorModule();
errorModule.setId(String.valueOf(e.getLineNumber()));
errorModule.setDesctipt(e.getMessage());
errorModule.setLeavel("middle");
errorModule.setType("fatal");
errorList.add(errorModule);
//System.out.println("失败: " + e);
}
public void warning(SAXParseException e) {
ErrorModule errorModule = new ErrorModule();
errorModule.setId(String.valueOf(e.getLineNumber()));
errorModule.setDesctipt(e.getMessage());
errorModule.setLeavel("button");
errorModule.setType("warning");
errorList.add(errorModule);
//System.out.println("警告: " + e);
}
});
//System.out.println("正在验证XML文件...");
VerifierHandler handler = verifier.getVerifierHandler();
SAXWriter writer = new SAXWriter(handler);
writer.write(document);
for(int i=0;i<errorList.size();i++) {
ErrorModule tempModule = errorList.get(i);
String tempStr = tempModule.getDesctipt();
//标签为"标识码"项不被允许,请尝试标签为"对应号"的项
if(tempStr.startsWith("tag") && tempStr.endsWith(">")) {
int fitstIndex = tempStr.indexOf("e");
fitstIndex+=2;
int secondIndex = tempStr.indexOf("i");
secondIndex-=2;
String firstStr = tempStr.substring(fitstIndex+1, secondIndex);
int threeIndex = tempStr.indexOf("<");
int fourIndex = tempStr.indexOf(">");
String secondStr = tempStr.substring(threeIndex+1,fourIndex);
String totalStr = firstStr + secondStr;
tempModule.setDesctipt(firstStr);
tempModule.setBecause("不被允许");
tempModule.setWheredo("对应号的项");
}
//发现"内容信息"项没有被正确使用
else if(tempStr.startsWith("element") && tempStr.endsWith("occur")) {
int fitstIndex = tempStr.indexOf("t");
fitstIndex+=2;
int secondIndex = tempStr.indexOf("w");
secondIndex-=2;
String firstStr = tempStr.substring(fitstIndex+1, secondIndex);
String totalStr = firstStr;
tempModule.setDesctipt(firstStr);
tempModule.setBecause("没有被正确使用");
tempModule.setWheredo("正确使用");
}
}
return errorList;
}
}