我正在尝试使用saxon进行xsl转换,我遇到了文件名的问题:
package com.fop;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XMLtoFO {
private static StreamResult out;
private static StreamSource xml;
private static StreamSource xsl;
private static Transformer transformer;
// TODO
protected static void xmlToFO(String inputXSL, String[] inputxml,
String[] fofilname) throws Exception {
if (true) {
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
}
for (int i = 0; i < inputxml.length; i++) {
try {
out = new StreamResult( fofilname[i] );
xml = new StreamSource( inputxml[i] );
xsl = new StreamSource( inputXSL );
transformer = TransformerFactory.newInstance().newTransformer( xsl );
transformer.transform( xml, out );
System.err.println("\n--------------------------------------------------------------------------------------------\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Error on line 12 of root-template.xsl:
java.net.URISyntaxException: Illegal character in path at index 19: 3-Leiter-Steuerung,Methode 2_result.fo.xml
; SystemID: file:/C:/Users/z003a5bp/Desktop/root-template.xsl; Line#: 12; Column#: -1
net.sf.saxon.trans.XPathException: java.net.URISyntaxException: Illegal character in path at index 19: 3-Leiter-Steuerung, Methode 2_result.fo.xml
at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:200)
at net.sf.saxon.serialize.Emitter.makeWriter(Emitter.java:160)
at net.sf.saxon.serialize.XMLEmitter.openDocument(XMLEmitter.java:145)
at net.sf.saxon.serialize.XMLEmitter.startElement(XMLEmitter.java:309)
at net.sf.saxon.event.NamespaceReducer.startElement(NamespaceReducer.java:73)
at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:558)
at net.sf.saxon.event.ComplexContentOutputter.startElement(ComplexContentOutputter.java:183)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:424)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:660)
at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:138)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:431)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:239)
at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1057)
at net.sf.saxon.Controller.transformDocument(Controller.java:2080)
at net.sf.saxon.Controller.transform(Controller.java:1903)
at com.fop.XMLtoFO.xmlToFO(XMLtoFO.java:29)
at com.fop.Layout.output(Layout.java:151)
at com.fop.Layout.actionPerformed(Layout.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.URISyntaxException: Illegal character in path at index 19: 3-Leiter-Steuerung, Methode 2_result.fo.xml
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:172)
... 55 more
问题是许多文件名都有空格或逗号或类似的东西。处理这个问题的最佳方法是什么?是否所有输入文件都通过了字符转义方法?
我在看这个并猜测我的问题是类似的: http://sourceforge.net/p/saxon/discussion/94027/thread/b43bb749/
但那次讨论有点过头了。
更新:我相应地更改了构造函数。现在我明白了:
Error
I/O error reported by XML parser processing
file:/C:/Users/z003a5bp/Desktop/Workspace/FOP/file:C:/Users/z003a5bp/Desktop/3-Leiter-Steuerung,%20Methode%202.xml: C:\Users\z003a5bp\Desktop\Workspace\FOP\file:C:\Users\z003a5bp\Desktop\3-Leiter-Steuerung, Methode 2.xml (the filename, directory name or volume label syntax is incorrect)
net.sf.saxon.trans.XPathException: I/O error reported by XML parser processing file:/C:/Users/z003a5bp/Desktop/Workspace/FOP/file:C:/Users/z003a5bp/Desktop/3-Leiter-Steuerung,%20Methode%202.xml: C:\Users\z003a5bp\Desktop\Workspace\FOP\file:C:\Users\z003a5bp\Desktop\3-Leiter-Steuerung, Methode 2.xml (the filename, directory name or volume label syntax is incorrect)
at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:427)
at net.sf.saxon.event.Sender.send(Sender.java:169)
at net.sf.saxon.Controller.transform(Controller.java:1890)
at com.fop.XMLtoFO.xmlToFO(XMLtoFO.java:29)
at com.fop.Layout.output(Layout.java:151)
at com.fop.Layout.actionPerformed(Layout.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\Users\z003a5bp\Desktop\Workspace\FOP\file:C:\Users\z003a5bp\Desktop\3-Leiter-Steuerung, Methode 2.xml (the filename, directory name or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:396)
... 41 more
这是我阅读文件的方式:
protected static void openXMLChooser() {
setUIManager();
FileFilter filter = new FileNameExtensionFilter("XML files", "xml");
chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.addChoosableFileFilter(filter);
chooser.showOpenDialog(null);
if (chooser.getSelectedFile() != null
&& chooser.getSelectedFiles() != null)
infoarea.append("No file(s)\n");
}
这就是我改变文件名的方式:
protected String[] fileName(String exten) {
file = chooser.getSelectedFiles();
pdffilename = new String[file .length];
for (int i = 0; i < file .length; i++) {
pdffilename[i] = file [i].getName();
b = new StringBuilder(pdffilename[i]);
b.replace(pdffilename[i].lastIndexOf("."),
pdffilename[i].lastIndexOf("l") + 1, "_result"+exten+"");
pdffilename[i] = b.toString();
}
return pdffilename;
}
在这里我称之为功能:
XMLtoFO.xmlToFO(inputxsl, inputxml, this.fileName("fo.xml"));
答案 0 :(得分:2)
文件名不是URI;您使用的文件名是预期的URI。
最简单的答案是使用期望文件而不是URI的StreamSource构造函数。所以改变
new StreamSource(X)
到
new StreamSource(new File(X))
因为“new File()”构造函数使用文件名。 (在内部,文件名将自动转换为URI,但您无需担心这一点。)