XML in PDF - ist mein Code okay?

Vokabulator

Mitglied
Hallo!

Ich hoffe, so eine Frage ist hier im Forum okay. Ich habe ein programm geschrieben, was per FileChooser eine xml-Datei einließt und sie dann in PDF umwandelt, wobei die PDF je nach Dateiname der XML anders heißt. Nun wollte ich fragen, ob mal jemand über den Code gucken könnte und mir sagen könnte, was da so alles für Fehler drin sind.
Funktionieren tut das Ganze, aber der Code ist ziemlich sicher nicht optimal.
Ich hab z. B. versucht, die main-Klasse vom Layout und von der Transformation zu trennen. Ist das so sinnvoll?

Vor allem interessiert mich Folgendes: Wie müsste ich vorgehen, um mehrere Dateien einlesen zu können und alle diese Dateien dann als PDF mit unterschiedlichen Dateinamen zu erhalten.

Ich hatte schon was mit einem File-Array probiert und einer ArrayList<File> aber ich bleibe da stecken. Das Problem ist vor allem, dass die Methoden .getPath() und getName() mit getselectedFiles() nicht funkionieren. Hier wäre ich für ein paar Schubser in die richtige Richtung dankbar.

Ein Problem gibt es noch: Wenn im Dateinamen der xml-Datei ein Umlaut ist, dann gibt eine Fehlermeldung:

Code:
Error 
  I/O error reported by XML parser processing C:\Users\z003a5bp\Desktop\FOP2\zz\püz.xml: unknown protocol: c
net.sf.saxon.trans.XPathException: I/O error reported by XML parser processing C:\Users\z003a5bp\Desktop\FOP2\MeinZeug\püz.xml: unknown protocol: c

Hier der Code:
Code:
public class MainFOP {

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Thread() {
			private String file;

			public void run() {
				File xsl = new File("stylesheet.xsl");
				String xslfile = xsl.toString();
				Layout layout = new Layout();

				try {
					layout.GUI();
					file = layout.fileInput();
				} catch (NullPointerException e) {
					System.out
							.println("Da ist etwas schiefgelaufen, nämlich:\n");
					e.printStackTrace();
					System.exit(0);
				}

				try {
					layout.fileNamePDF();
					layout.fileInput();
					XMLtoPDF.xmlToPdfPerXsl(xslfile, file, layout.fileNamePDF());
					
					XMLtoFO.XMLtoFOdo(xslfile, file);
				} catch (Exception e) {
					System.out.println("Fehler:\n");
					e.printStackTrace();
					System.exit(0);
				}
			}

		});
	}
}


Code:
public class Layout {

	private static  String dateiname;
	private String datei;
	private static  JFileChooser chooser;

	protected void GUI() {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		 FileFilter filter = new FileNameExtensionFilter("XML-Dateien", 
		            "xml");   
		chooser = new JFileChooser();
		chooser.addChoosableFileFilter(filter);
		chooser.showOpenDialog(null);
	}

	protected String fileInput() {
		datei = chooser.getSelectedFile().getPath();
		return datei;
	}

	protected String fileNamePDF() {
			dateiname = chooser.getSelectedFile().getName();
			StringBuilder b = new StringBuilder(dateiname);
			b.replace(dateiname.lastIndexOf("."), dateiname.lastIndexOf("l") + 1,
					"_result.pdf");
			dateiname = b.toString();
		return dateiname;
	}

	protected static String fileNameFO() {
			dateiname = chooser.getSelectedFile().getName();
			StringBuilder c = new StringBuilder(dateiname);
			c.replace(dateiname.lastIndexOf("."), dateiname.lastIndexOf("l") + 1,
					"_resultxml.fo");
			dateiname = c.toString();	
		return dateiname;
	}
}


Code:
public final class XMLtoPDF {
	
	protected static void xmlToPdfPerXsl(String inputXSL, String file,
			String string) throws Exception {
		if (true) {
			System.setProperty("javax.xml.transform.TransformerFactory",
					"net.sf.saxon.TransformerFactoryImpl");
		}
		OutputStream pdf = null;
		try {
				pdf = new FileOutputStream(string);
			

			Fop fop = FopFactory.newInstance().newFop(MimeConstants.MIME_PDF,
					pdf); // ?

				Source xml = new StreamSource(file);
			
			Source xsl = new StreamSource(inputXSL);

			Result sax = new SAXResult(fop.getDefaultHandler()); // ?

			Transformer transformer = TransformerFactory.newInstance()
					.newTransformer(xsl);
			transformer.transform(xml, sax);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			pdf.close();
		}
	}
}


Mit dieser Klasse lasse ich mir noch das FO-Zwischenformat ausgeben:
Code:
public final class XMLtoFO {
	protected static void XMLtoFOdo(String inputXSL, String file) throws Exception { 
		if(true) {
			System.setProperty(
					"javax.xml.transform.TransformerFactory",
					"net.sf.saxon.TransformerFactoryImpl");
		}
		TransformerFactory f = TransformerFactory.newInstance();
		Source stylesheet = new StreamSource(inputXSL);
		try {
			Transformer t = f.newTransformer(stylesheet);
				Source sourcexml = new StreamSource(file);
				Result result = new StreamResult(new File(Layout.fileNameFO()));
				t.transform(sourcexml, result);
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

}

Vielen Dank schon mal an alle, die sich die Zeit nehmen!
 
Zuletzt bearbeitet:

ARadauer

Top Contributor
sorry ohne jetzt irgendetwas von deinem post gelesen zu haben ist mir das püz ins auge gesprungen.
nenn sie mal puz... wenn das nix ändert, dann schau ich mir deinen post genauer an.
versprochen ;-)
 

Vokabulator

Mitglied
Hallo!

Das püz steht stellvertretend für eine Datei mit a,ö.ü - ich habe keinen Einfluss darauf, wie die Dateien heißen. Das sollte möglichst mit allen möglichen Dateinamen funktionieren.
 

Vokabulator

Mitglied
SO, ein Update: Es klappt jetzt, dass ich mehrere Dateien verarbeite. ABER: Das Programm stürzt immer noch ab, wenn ich einen Dateinamen mit ä.ö oder ü einlesen. Die Fehlermeldung lautet:

Code:
Error 
  I/O error reported by XML parser processing
  C:\Users\z003a5bp\Desktop\FOP2\MeinZeug\püz.xml: unknown protocol: c
net.sf.saxon.trans.XPathException: I/O error reported by XML parser processing C:\Users\z003a5bp\Desktop\FOP2\MeinZeug\püz.xml: unknown protocol: c
	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 fop.XMLtoPDF.xmlToPdf(XMLtoPDF.java:42)
	at fop.MainFOP$1.run(MainFOP.java:21)
	at java.awt.event.InvocationEvent.dispatch(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.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.MalformedURLException: unknown protocol: c
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(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)
	... 18 more
---------
java.net.MalformedURLException: unknown protocol: c
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(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)
	at net.sf.saxon.event.Sender.send(Sender.java:169)
	at net.sf.saxon.Controller.transform(Controller.java:1890)
	at com.siemens.automation.khe.fop.XMLtoPDF.xmlToPdf(XMLtoPDF.java:42)
	at com.siemens.automation.khe.fop.MainFOP$1.run(MainFOP.java:21)
	at java.awt.event.InvocationEvent.dispatch(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.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)
Error 
  I/O error reported by XML parser processing
  C:\Users\z003a5bp\Desktop\FOP2\MeinZeug\püz.xml: unknown protocol: c
net.sf.saxon.trans.XPathException: I/O error reported by XML parser processing C:\Users\z003a5bp\Desktop\FOP2\MeinZeug\püz.xml: unknown protocol: c
	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 fop.XMLtoFO.XMLtoFOdo(XMLtoFO.java:27)
	at fop.MainFOP$1.run(MainFOP.java:24)
	at java.awt.event.InvocationEvent.dispatch(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.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.MalformedURLException: unknown protocol: c
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(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)
	... 18 more
---------
java.net.MalformedURLException: unknown protocol: c
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(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)
	at net.sf.saxon.event.Sender.send(Sender.java:169)
	at net.sf.saxon.Controller.transform(Controller.java:1890)
	at fop.XMLtoFO.XMLtoFOdo(XMLtoFO.java:27)
	at fop.MainFOP$1.run(MainFOP.java:24)
	at java.awt.event.InvocationEvent.dispatch(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.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)
 

Ähnliche Java Themen

Neue Themen


Oben