英文:
Oxygen XML Editor and Saxon extension functions
问题
我在Oxygen XML Editor中使用Saxon扩展函数时遇到问题,希望能够得到帮助。我已经创建了用于函数定义、函数调用以及初始化的Java类。我将函数定义和函数调用放在一个JAR文件中,初始化器放在一个单独的JAR文件中。我将这些JAR文件以及包含所有必需库的“lib”文件夹放在XSL文件所在的目录中:
我已将它们列在转换方案窗口的“Extensions”对话框中:
并尝试列出初始化器:
但是当我选择“Choose”按钮,然后选择以下对话框中的“Detect”按钮时,它无法检测到类:
当我尝试进行转换时,我收到以下问题消息:
以下是ExtensionDefinition类的代码:
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.value.SequenceType;
/**
*
* @author tfurst
*/
public class CreateXmlMapDefinition extends ExtensionFunctionDefinition{
@Override
public StructuredQName getFunctionQname() {
return new StructuredQName("rcmt", "http://rcmt.com/saxon-extension", "create-map");
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_STRING};
}
@Override
public SequenceType getResultType(SequenceType[] sts) {
return SequenceType.ANY_SEQUENCE;
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new CreateXmlExtension();
}
}
以下是函数调用的代码:
import com.rcmt.mapProcessor.ExcelMapProcessor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.trans.XPathException;
import org.w3c.dom.Document;
public class CreateXmlExtension extends ExtensionFunctionCall{
@Override
public Sequence call(XPathContext xpc, Sequence[] args) throws XPathException
{
String excelPath = args[0].head().getStringValue();
DocumentInfo doc = getXml(xpc, excelPath);
return doc;
}
private DocumentInfo getXml(XPathContext context, String path)
{
DocumentInfo xml = null;
try
{
Document d = ExcelMapProcessor.makeXml(path);
xml = (DocumentInfo)context.getConfiguration().buildDocumentTree(makeSource(d));
}
catch (XPathException ex)
{
Logger.getLogger(CreateXmlExtension.class.getName()).log(Level.SEVERE, null, ex);
}
return xml;
}
private Source makeSource(Document doc)
{
Source result = null;
try
{
TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl();
Transformer trans = transformerFactory.newTransformer();
DOMSource src = new DOMSource(doc);
StreamResult res = new StreamResult(new ByteArrayOutputStream ());
trans.transform(src, res);
ByteArrayOutputStream so = (ByteArrayOutputStream)res.getOutputStream();
result = new StreamSource(new ByteArrayInputStream(so.toByteArray()));
so.close();
}
catch (TransformerException | IOException ex)
{
Logger.getLogger(CreateXmlExtension.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
}
以下是初始化器的代码:
import javax.xml.transform.TransformerException;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.Initializer;
import net.sf.saxon.om.NamePool;
import org.expath.httpclient.saxon.SendRequestFunction;
import com.rcmt.map.CreateXmlMapDefinition;
/**
*
* @author tfurst
*/
public class SaxonExtInitializer implements Initializer{
@Override
public void initialize(Configuration config) throws TransformerException {
config.registerExtensionFunction(new CreateXmlMapDefinition());
SendRequestFunction expathHttpClient = new SendRequestFunction();
config.setNamePool(new NamePool());
expathHttpClient.setConfiguration(config);
config.registerExtensionFunction(expathHttpClient);
}
}
最后,以下是XSL中的用法:
英文:
I am posting to ask for help in getting my Saxon Extension functions to work in Oxygen XML Editor. I have created Java classes for the function definition, the function call, as well as an initializer class. I have the definition and function call in one JAR file and the initializer in a separate JAR file. I have placed the JAR files and a ‘lib’ folder containing all required libraries in the directory that the XSL is stored in:
I have listed them in the ‘Extensions’ dialog of the Transformation Scenario window:
And I have attempted to list the initializer:
When I select the “Choose” button and then the “Detect” button on the following dialog, it does not detect the class:
When I attempt to transform I receive these problem messages:
Here is the code to the ExtensionDefinition class:
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.value.SequenceType;
/**
*
* @author tfurst
*/
public class CreateXmlMapDefinition extends ExtensionFunctionDefinition{
@Override
public StructuredQName getFunctionQName() {
return new StructuredQName("rcmt", "http://rcmt.com/saxon-extension", "create-map");
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_STRING};
}
@Override
public SequenceType getResultType(SequenceType[] sts) {
return SequenceType.ANY_SEQUENCE;
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new CreateXmlExtension();
}
}
Here is the code to the function call:
import com.rcmt.mapProcessor.ExcelMapProcessor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.trans.XPathException;
import org.w3c.dom.Document;
public class CreateXmlExtension extends ExtensionFunctionCall{
@Override
public Sequence call(XPathContext xpc, Sequence[] args) throws XPathException
{
String excelPath = args[0].head().getStringValue();
DocumentInfo doc = getXml(xpc, excelPath);
return doc;
}
private DocumentInfo getXml(XPathContext context, String path)
{
DocumentInfo xml = null;
try
{
Document d = ExcelMapProcessor.makeXml(path);
xml = (DocumentInfo)context.getConfiguration().buildDocumentTree(makeSource(d));
}
catch (XPathException ex)
{
Logger.getLogger(CreateXmlExtension.class.getName()).log(Level.SEVERE, null, ex);
}
return xml;
}
private Source makeSource(Document doc)
{
Source result = null;
try
{
TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl();
Transformer trans = transformerFactory.newTransformer();
DOMSource src = new DOMSource(doc);
StreamResult res = new StreamResult(new ByteArrayOutputStream ());
trans.transform(src, res);
ByteArrayOutputStream so = (ByteArrayOutputStream)res.getOutputStream();
result = new StreamSource(new ByteArrayInputStream(so.toByteArray()));
so.close();
}
catch (TransformerException | IOException ex)
{
Logger.getLogger(CreateXmlExtension.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
}
This the code to my initializer:
import javax.xml.transform.TransformerException;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.Initializer;
import net.sf.saxon.om.NamePool;
import org.expath.httpclient.saxon.SendRequestFunction;
import com.rcmt.map.CreateXmlMapDefinition;
/**
*
* @author tfurst
*/
public class SaxonExtInitializer implements Initializer{
@Override
public void initialize(Configuration config) throws TransformerException {
config.registerExtensionFunction(new CreateXmlMapDefinition());
SendRequestFunction expathHttpClient = new SendRequestFunction();
config.setNamePool(new NamePool());
expathHttpClient.setConfiguration(config);
config.registerExtensionFunction(expathHttpClient);
}
}
And finally, the usage in the XSL:
答案1
得分: 2
如果您有一个包含扩展函数的ExtensionFunctionDefinition
和ExtensionFunctionCall
的JAR文件,那么在该JAR文件中的META-INF
文件夹中,创建一个名为services
的子文件夹,在该文件夹内创建一个名为net.sf.saxon.lib.ExtensionFunctionDefinition
的文本文件,每行列出相关的类,例如CreateXmlMapDefinition
(前缀为其所在的任何命名空间)。
它的外观类似于这样:
然后在转换场景对话框中将该JAR文件作为扩展提供。这种方式下您不需要任何初始化程序。我已经在Oxygen 22.1和Saxon 9.9上测试过。
英文:
If you have a JAR file containing the ExtensionFunctionDefinition
and ExtensionFunctionCall
of your extension function(s), then, in that JAR file, in the META-INF
folder, create a subfolder named services
and inside of that folder create a text file named net.sf.saxon.lib.ExtensionFunctionDefinition
that on each line lists the relevant classes, e.g., CreateXmlMapDefinition
(prefixed by any namespace it's in).
It looks like this:
Then provide that JAR file as an extension in the transformation scenario dialogue. You don't need any initializer that way. I have tested that Oxygen 22.1 and Saxon 9.9.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论