com.zhangyue.we.anoprocesser.xml.Attr2FuncReader Maven / Gradle / Ivy
The newest version!
package com.zhangyue.we.anoprocesser.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.util.HashMap;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* @author chengwei 2018/8/25
*/
public class Attr2FuncReader {
private final File mFile;
private SAXParser mParser;
private final HashMap mAttr2Funcs = new HashMap<>();
public Attr2FuncReader(File file) {
this.mFile = file;
}
public HashMap parse() {
try {
this.mParser = SAXParserFactory.newInstance().newSAXParser();
if (mFile.exists()) {
// 解析X2C_CONFIG.XML
mParser.parse(mFile, new Attr2FuncHandler());
return new AttrReader(mAttr2Funcs).parse();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private class Attr2FuncHandler extends DefaultHandler {
private static final String KEY_ATTR = "name";
private static final String VALUE_ATTR = "toFunc";
private String mName;
private String mToFunc;
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
mName = attributes.getValue(KEY_ATTR);
mToFunc = attributes.getValue(VALUE_ATTR);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
mAttr2Funcs.put(mName, new Func(mToFunc.trim()));
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
mParser.reset();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy