Oxygen XML Editor和Saxon扩展函数

huangapple go评论65阅读模式
英文:

Oxygen XML Editor and Saxon extension functions

问题

我在Oxygen XML Editor中使用Saxon扩展函数时遇到问题,希望能够得到帮助。我已经创建了用于函数定义、函数调用以及初始化的Java类。我将函数定义和函数调用放在一个JAR文件中,初始化器放在一个单独的JAR文件中。我将这些JAR文件以及包含所有必需库的“lib”文件夹放在XSL文件所在的目录中:

Oxygen XML Editor和Saxon扩展函数

我已将它们列在转换方案窗口的“Extensions”对话框中:

Oxygen XML Editor和Saxon扩展函数

并尝试列出初始化器:

Oxygen XML Editor和Saxon扩展函数

但是当我选择“Choose”按钮,然后选择以下对话框中的“Detect”按钮时,它无法检测到类:

Oxygen XML Editor和Saxon扩展函数

当我尝试进行转换时,我收到以下问题消息:

Oxygen XML Editor和Saxon扩展函数

以下是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中的用法:

Oxygen XML Editor和Saxon扩展函数

英文:

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:

Oxygen XML Editor和Saxon扩展函数

I have listed them in the ‘Extensions’ dialog of the Transformation Scenario window:

Oxygen XML Editor和Saxon扩展函数

And I have attempted to list the initializer:

Oxygen XML Editor和Saxon扩展函数

When I select the “Choose” button and then the “Detect” button on the following dialog, it does not detect the class:

Oxygen XML Editor和Saxon扩展函数

When I attempt to transform I receive these problem messages:

Oxygen XML Editor和Saxon扩展函数

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:

Oxygen XML Editor和Saxon扩展函数

答案1

得分: 2

如果您有一个包含扩展函数的ExtensionFunctionDefinitionExtensionFunctionCall的JAR文件,那么在该JAR文件中的META-INF文件夹中,创建一个名为services的子文件夹,在该文件夹内创建一个名为net.sf.saxon.lib.ExtensionFunctionDefinition的文本文件,每行列出相关的类,例如CreateXmlMapDefinition(前缀为其所在的任何命名空间)。

它的外观类似于这样:

Oxygen XML Editor和Saxon扩展函数

然后在转换场景对话框中将该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:

Oxygen XML Editor和Saxon扩展函数

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.

huangapple
  • 本文由 发表于 2020年9月24日 20:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64046027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定