# 1
i'm using jdk 1.5.0_11 and here is my java code.
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
// For JFileChooser
import javax.swing.*;
// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class StylizerMio2 {
// Global value so it can be ref'd by the tree-adapter
static Document document;
// Constants you'll use when configuring the factory
static final String JAXP_SCHEMA_LANGUAGE="http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA="http://www.w3.org/2001/XMLSchema";
//Constants used to specify the schema in the application
static final String schemaSource="COLLADASchema_141.xsd";
static final String JAXP_SCHEMA_SOURCE="http://java.sun.com/xml/jaxp/properties/schemaSource";
public static void main(String[] argv) {
/*if (argv.length != 2) {
System.err.println("Usage: java StylizerMio stylesheet xmlfile");
System.exit(1);
}*/
// Generating a parser
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Configure the factory to generate a namespace-aware,validating parser
factory.setValidating(true);
factory.setNamespaceAware(true);
try{
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
// Associate a document with a schema
factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));
}
catch (IllegalArgumentException x) {
// Happens if the parser does not support JAXP 1.2
x.printStackTrace();
}
// Associating the document with the schema
try {
JFileChooser chooser = new JFileChooser();
ExampleFileFilter daeFilter = new ExampleFileFilter();
daeFilter.addExtension("dae");
daeFilter.setDescription("documenti istanza COLLADA");
chooser.setFileFilter(daeFilter);
chooser.setCurrentDirectory(new File("."));
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
File stylesheet = new File("trasformazione.xsl");
//"Moon Buggy/lunar_vehicle_tris.dae"
File datafile = chooser.getSelectedFile();
DocumentBuilder builder = factory.newDocumentBuilder();
//Handling validation errors
builder.setErrorHandler(
new org.xml.sax.ErrorHandler(){
// ignore fatal errors (an exception is garaunteed)
public void fatalError(SAXParseException exception)
throws SAXException{}
//treat validation errors as fatal
public void error(SAXParseException e)
throws SAXParseException
{
throw e;
}
// dump warnigs too
public void warning(SAXParseException err)
throws SAXParseException
{
System.out.println("** Warning"
+ ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId());
System.out.println("" + err.getMessage());
}
}
);
document = builder.parse(datafile);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = tFactory.newTransformer(stylesource);
DOMSource source = new DOMSource(document);
// SaveDialogue JFileChooser
ExampleFileFilter htmlFilter = new ExampleFileFilter();
htmlFilter.addExtension("html");
htmlFilter.setDescription("documento html");
chooser.setFileFilter(htmlFilter);
chooser.showSaveDialog(null);
//"Moon Buggy/lunar_vehicle_tris.html"
File htmlFile= chooser.getSelectedFile();
StreamResult result = new StreamResult(htmlFile);
transformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println("\n** Transformer Factory error");
System.out.println("" + tce.getMessage());
// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null) {
x = tce.getException();
}
x.printStackTrace();
} catch (TransformerException te) {
// Error generated by the parser
System.out.println("\n** Transformation error");
System.out.println("" + te.getMessage());
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null) {
x = sxe.getException();
}
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
} // main
}
pao_a at 2007-7-11 22:28:44 >
