java-org.hwo.servicelink/src/org/hwo/servicelink/register/ServiceRegisterRegistry.java

105 lines
3.0 KiB
Java

package org.hwo.servicelink.register;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.hwo.xml.NodeListIterator;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ServiceRegisterRegistry {
RegisterTypeRegistry typeRegistry;
ServiceRegisterListRegistry
listRegistry;
List<String> loadedFiles;
XPath xpath;
public ServiceRegisterRegistry(){
xpath = XPathFactory.newInstance().newXPath();
loadedFiles = new ArrayList<String>();
typeRegistry = new RegisterTypeRegistry(this);
listRegistry = new ServiceRegisterListRegistry(this);
}
public boolean loadRegistryFile(String filename){
return loadRegistryFile(filename,new File(".").getAbsolutePath());
}
public boolean loadRegistryFile(String filename,String basePath){
File f = new File(filename);
if (!f.exists()){
f = new File(basePath,filename);
if (!f.exists()){
System.err.println(String.format("ServiceRegisterRegistry: file not found: %s",filename));
return false;
}
}
if (loadedFiles.contains(filename)){
return true;
}
loadedFiles.add(filename);
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document xmlDoc = db.parse( f );
NodeList externalRegistryFiles = (NodeList)xpath.evaluate("/ServiceRegisterFile/ExternalRefs/File",xmlDoc, XPathConstants.NODESET);
for (Node refFile: NodeListIterator.create(externalRegistryFiles)){
if (!loadRegistryFile(refFile.getFirstChild().getNodeValue(),f.getParent())){
System.err.println(String.format("ServiceRegisterRegistry: failed to load %s",refFile.getFirstChild().getNodeValue()));
return false;
}
}
NodeList typesList = (NodeList)xpath.evaluate("ServiceRegisterFile/Types/Type", xmlDoc, XPathConstants.NODESET);
for (Node typeNode: NodeListIterator.create(typesList)){
typeRegistry.createType(typeNode);
}
NodeList registerListsList = (NodeList)xpath.evaluate("ServiceRegisterFile/RegisterList", xmlDoc, XPathConstants.NODESET);
for (Node registerListNode: NodeListIterator.create(registerListsList)){
listRegistry.createServiceRegisterList(registerListNode);
}
} catch (Exception e){
e.printStackTrace();
return false;
}
System.err.println(String.format("ServiceRegisterRegistry: loaded %s", f.getAbsolutePath()));
return true;
}
public XPath getXpath() {
return xpath;
}
public RegisterTypeRegistry getTypeRegistry() {
return typeRegistry;
}
public ServiceRegisterListRegistry getListRegistry() {
return listRegistry;
}
public static void main(String[] args){
ServiceRegisterRegistry registry = new ServiceRegisterRegistry();
registry.loadRegistryFile("/data/src/hf3/service-registers/serviceregisters.xml");
}
}