📄 transformationutil.java
字号:
package it.itc.ectrl.normkit.common;
import org.apache.xpath.domapi.XPathEvaluatorImpl;
import org.w3c.dom.xpath.XPathEvaluator;
import org.w3c.dom.xpath.XPathNSResolver;
import org.w3c.dom.xpath.XPathResult;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Enumeration;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
//import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.schema.*;
import oracle.xml.parser.v2.XMLDocument;
import edu.unika.aifb.rdf.api.util.*;
import edu.unika.aifb.kaon.api.oimodel.OIModel;
import edu.unika.aifb.kaon.apionrdf.OIModelImpl;
/**
* General purpose utilities for all Normalisation Toolkit classes.
* @author Oliver Fodor (fodor@itc.it)
*/
public class TransformationUtil {
public static XPathResult executeXPathQuery(Node xmlNode, String xPath) throws Exception {
// Create an XPath evaluator and pass in the document.
XPathEvaluator evaluator = new XPathEvaluatorImpl();
XPathNSResolver resolver = evaluator.createNSResolver(xmlNode); // here a context node for namespace resolution should be put instead of xmlNode, it can be e.g. the NormMap document
// Evaluate the xpath expression
// XPathResult result = (XPathResult)evaluator.evaluate(xPath, xmlNode, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
XPathResult result = (XPathResult)evaluator.evaluate(xPath, xmlNode, resolver, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
return result;
}
public static boolean isTextNode(Node n) {
if (n == null)
return false;
short nodeType = n.getNodeType();
return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}
public static String extractElementName(String strXPathTargetEntity) {
if (strXPathTargetEntity == "text()")
return "";
if (isLiteralXPath(strXPathTargetEntity)) {
strXPathTargetEntity = strXPathTargetEntity.replaceAll("/text\\(", "");
strXPathTargetEntity = strXPathTargetEntity.replaceAll("\\)", "");
}
strXPathTargetEntity = strXPathTargetEntity.replaceAll("@", "");
String ret = strXPathTargetEntity.substring(strXPathTargetEntity.lastIndexOf("/")+1);
return ret;
}
public static boolean isLiteralXPath(String xpath) {
return xpath.endsWith("text()");
}
public static boolean isAttributeXPath(String xpath) {
return xpath.startsWith("@"); // !!!! correction necessary
}
public static URL createURL(String uri) throws Exception {
URL url = null;
url = new URL(uri);
return url;
}
public static int getHashCode(Object o) {
return o.toString().hashCode();
}
public static void saveOIModel( OIModel oimodel, String strPhysicalURI ) throws Exception {
if( oimodel == null ) return;
// testURIValidity( strPhysicalURI, "Mapping" );
strPhysicalURI = new URL( strPhysicalURI ).getFile();
PrintStream outputStream;
if(strPhysicalURI!=null)
outputStream = new PrintStream( new FileOutputStream(strPhysicalURI) );
else
outputStream = System.out;
// write the target instances
RDFUtil.writeModel(((OIModelImpl)oimodel).getModel(), outputStream, RDFUtil.DEFAULT_ENCODING);
}
public static void writeOIModelToOutputStream( OIModel oimodel, OutputStream outputStream ) throws Exception {
if( oimodel == null ) return;
// write the target instances
RDFUtil.writeModel(((OIModelImpl)oimodel).getModel(), outputStream, RDFUtil.DEFAULT_ENCODING);
outputStream.close();
}
public static void saveXML(XMLDocument xml, String xmlfile) throws Exception {
xmlfile = new URL( xmlfile ).getFile();
FileOutputStream ostream = new FileOutputStream(xmlfile);
xml.print(ostream);
ostream.close();
}
public static void writeXMLToOutputStream( XMLDocument xml, OutputStream outputStream ) throws Exception {
xml.print(outputStream);
outputStream.close();
}
public static XMLSchemaNode readXSD(String strSourceXSDPhysicalURI) throws NormalisationException {
try {
XSDBuilder builder = new XSDBuilder();
XMLSchema schemadoc = (XMLSchema) builder.build(TransformationUtil.createURL (strSourceXSDPhysicalURI));
Hashtable schemanodes = schemadoc.getXMLSchemaNodeTable();
return chooseSchemaNodeToProceed(schemanodes);
} catch (Exception e) {
throw new NormalisationException("Exception while reading the XSD at input", e);
}
}
private static XMLSchemaNode chooseSchemaNodeToProceed(Hashtable schemanodes) throws Exception {
// process all in the input file included schemata
for(Enumeration enumeration = schemanodes.elements(); enumeration.hasMoreElements();) {
XMLSchemaNode xmlschemanode = (XMLSchemaNode)enumeration.nextElement();
if(xmlschemanode.getTargetNS() != "http://www.w3.org/1999/XMLSchema" && xmlschemanode.getTargetNS() != "http://www.w3.org/2000/10/XMLSchema" && xmlschemanode.getTargetNS() != "http://www.w3.org/2001/XMLSchema")
return xmlschemanode;
}
return null;
}
public static Element createAndAppendXSLElement (Document xslDocument, Node currentNode, String nqName, String attName, String attValue) throws Exception {
Element node = xslDocument.createElementNS(CNOVocabulary.URI_XSL, CNOVocabulary.NS_PREFIX_XSL + ":" + nqName);
if (attName != null)
node.setAttribute(attName, attValue);
currentNode.appendChild(node);
return node;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -