001package org.maltparser.core.feature.spec.reader; 002 003import java.io.IOException; 004import java.net.URL; 005 006import javax.xml.parsers.DocumentBuilder; 007import javax.xml.parsers.DocumentBuilderFactory; 008import javax.xml.parsers.ParserConfigurationException; 009 010import org.maltparser.core.exception.MaltChainedException; 011import org.maltparser.core.feature.FeatureException; 012import org.maltparser.core.feature.spec.SpecificationModels; 013import org.w3c.dom.Element; 014import org.w3c.dom.NodeList; 015import org.xml.sax.SAXException; 016import org.xml.sax.SAXParseException; 017/** 018* 019* 020* @author Johan Hall 021*/ 022public class XmlReader implements FeatureSpecReader{ 023 024 public XmlReader() { } 025 026 public void load(URL specModelURL, SpecificationModels featureSpecModels) throws MaltChainedException { 027 try { 028 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 029 DocumentBuilder db = dbf.newDocumentBuilder(); 030 Element root = null; 031 032 root = db.parse(specModelURL.openStream()).getDocumentElement(); 033 034 if (root == null) { 035 throw new FeatureException("The feature specification file '"+specModelURL.getFile()+"' cannot be found. "); 036 } 037 038 readFeatureModels(root, featureSpecModels); 039 } catch (IOException e) { 040 throw new FeatureException("The feature specification file '"+specModelURL.getFile()+"' cannot be found. ", e); 041 } catch (SAXParseException e) { 042 throw new FeatureException("Problem parsing the feature specification XML-file "+specModelURL.getFile()+". ", e); 043 } catch (ParserConfigurationException e) { 044 throw new FeatureException("Problem parsing the feature specification XML-file "+specModelURL.getFile()+". ", e); 045 } catch (SAXException e) { 046 throw new FeatureException("Problem parsing the feature specification XML-file "+specModelURL.getFile()+". ", e); 047 } 048 } 049 050 private void readFeatureModels(Element featuremodels, SpecificationModels featureSpecModels) throws MaltChainedException { 051 NodeList featureModelList = featuremodels.getElementsByTagName("featuremodel"); 052 for (int i = 0; i < featureModelList.getLength(); i++) { 053 readFeatureModel((Element)featureModelList.item(i), featureSpecModels); 054 } 055 } 056 057 private void readFeatureModel(Element featuremodel, SpecificationModels featureSpecModels) throws MaltChainedException { 058 int specModelIndex = featureSpecModels.getNextIndex(); 059 NodeList submodelList = featuremodel.getElementsByTagName("submodel"); 060 if (submodelList.getLength() == 0) { 061 NodeList featureList = featuremodel.getElementsByTagName("feature"); 062 for (int i = 0; i < featureList.getLength(); i++) { 063 String featureText = ((Element)featureList.item(i)).getTextContent().trim(); 064 if (featureText.length() > 1) { 065 featureSpecModels.add(specModelIndex, featureText); 066 } 067 } 068 } else { 069 for (int i = 0; i < submodelList.getLength(); i++) { 070 String name = ((Element)submodelList.item(i)).getAttribute("name"); 071 NodeList featureList = ((Element)submodelList.item(i)).getElementsByTagName("feature"); 072 for (int j = 0; j < featureList.getLength(); j++) { 073 String featureText = ((Element)featureList.item(j)).getTextContent().trim(); 074 if (featureText.length() > 1) { 075 featureSpecModels.add(specModelIndex, name, featureText); 076 } 077 } 078 } 079 } 080 } 081}