|
What this is
Other links
The source code/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.xsl.utils;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.security.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.openide.filesystems.*;
import org.openide.loaders.DataObject;
import org.openide.execution.*;
import org.netbeans.api.xml.cookies.*;
import org.netbeans.api.xml.services.UserCatalog;
import org.netbeans.spi.xml.cookies.*;
import org.netbeans.modules.xml.core.XMLDataObject;
import org.netbeans.modules.xsl.XSLDataObject;
/**
* Transformation utilities.
*
* @author Libor Kramolis
* @version 0.1
*/
public class TransformUtil {
private static final String SAX_FEATURES_NAMESPACES = "http://xml.org/sax/features/namespaces"; // NOI18N
public static final String DEFAULT_OUTPUT_EXT = "html";
private static TransformerFactory transformerFactory;
private static SAXParserFactory saxParserFactory;
public static boolean isXSLTransformation (DataObject dataObject) {
return ( dataObject instanceof XSLDataObject );
// return dataObject.getPrimaryFile().getMIMEType().equals (XSLDataObject.MIME_TYPE);
}
public static String getURLName (FileObject fileObject) throws MalformedURLException, FileStateInvalidException {
URL fileURL = null;
File file = FileUtil.toFile (fileObject);
if ( file != null ) {
if ( Util.THIS.isLoggable() ) /* then */ {
try {
Util.THIS.debug ("[TransformUtil.getURLName]");
Util.THIS.debug (" file = " + file);
Util.THIS.debug (" file.getCanonicalPath = " + file.getCanonicalPath());
Util.THIS.debug (" file.getAbsolutePath = " + file.getAbsolutePath());
Util.THIS.debug (" file.toString = " + file.toString());
Util.THIS.debug (" file.toURL = " + file.toURL());
} catch (Exception exc) {
Util.THIS.debug ("DEBUG Exception", exc);
}
}
fileURL = file.toURL();
} else {
fileURL = fileObject.getURL();
}
return fileURL.toExternalForm();
}
public static URL createURL (URL baseURL, String fileName) throws MalformedURLException, FileStateInvalidException {
if ( Util.THIS.isLoggable() ) /* then */ {
Util.THIS.debug ("TransformUtil.createURL:");
Util.THIS.debug (" baseURL = " + baseURL);
Util.THIS.debug (" fileName = " + fileName);
}
URL url = new URL (baseURL, fileName);
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" return URL = " + url);
return url;
}
public static Source createSource (URL baseURL, String fileName) throws IOException, MalformedURLException, FileStateInvalidException, ParserConfigurationException, SAXException {
URL url = createURL (baseURL, fileName);
// test right url
InputStream is = url.openStream();
is.close();
XMLReader reader = TransformUtil.newXMLReader();
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TransformUtil.createSource: XMLReader (http://xml.org/sax/features/namespaces) : "
+ reader.getFeature (SAX_FEATURES_NAMESPACES));
reader.setEntityResolver (TransformUtil.getEntityResolver());
Source source = new SAXSource (reader, new InputSource (url.toExternalForm()));
return source;
}
public static URIResolver getURIResolver () {
UserCatalog catalog = UserCatalog.getDefault();
URIResolver res = (catalog == null ? null : catalog.getURIResolver());
return res;
}
public static EntityResolver getEntityResolver () {
UserCatalog catalog = UserCatalog.getDefault();
EntityResolver res = (catalog == null ? null : catalog.getEntityResolver());
return res;
}
private static TransformerFactory getTransformerFactory () {
if ( transformerFactory == null ) {
transformerFactory = TransformerFactory.newInstance();
transformerFactory.setURIResolver (getURIResolver()); //!!! maybe that it should be set every call if UsersCatalog instances are dynamic
}
return transformerFactory;
}
private static SAXParserFactory getSAXParserFactory () throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
if ( saxParserFactory == null ) {
saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature (SAX_FEATURES_NAMESPACES, true);
}
return saxParserFactory;
}
public static Transformer newTransformer (Source xsl) throws TransformerConfigurationException {
Transformer transformer = getTransformerFactory().newTransformer (xsl);
if ( Util.THIS.isLoggable() ) /* then */ transformer.setParameter ("transformer", xsl); // debug
return transformer;
}
public static XMLReader newXMLReader () throws ParserConfigurationException, SAXException {
SAXParser parser = getSAXParserFactory().newSAXParser();
return parser.getXMLReader();
}
/* @return associated stylesheet or
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.