[FOray-commit] SF.net SVN: foray: [7784] trunk/foray/foray-font/src/java/org/foray/font
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2006-07-15 23:09:38
|
Revision: 7784 Author: victormote Date: 2006-07-15 16:09:14 -0700 (Sat, 15 Jul 2006) ViewCVS: http://svn.sourceforge.net/foray/?rev=7784&view=rev Log Message: ----------- Move and rename URL utility class. Modified Paths: -------------- trunk/foray/foray-font/src/java/org/foray/font/FontConfigParser.java trunk/foray/foray-font/src/java/org/foray/font/format/TTFReader.java trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtURI.java trunk/foray/foray-graphic/src/java/org/foray/graphic/FOrayGraphicServer.java Added Paths: ----------- trunk/foray/foray-common/src/java/org/foray/common/url/URLUtil.java Removed Paths: ------------- trunk/foray/foray-common/src/java/org/foray/common/URLBuilder.java Deleted: trunk/foray/foray-common/src/java/org/foray/common/URLBuilder.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/URLBuilder.java 2006-07-15 22:29:14 UTC (rev 7783) +++ trunk/foray/foray-common/src/java/org/foray/common/URLBuilder.java 2006-07-15 23:09:14 UTC (rev 7784) @@ -1,256 +0,0 @@ -/* - * Copyright 2004 The FOray Project. - * http://www.foray.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * This work is in part derived from the following work(s), used with the - * permission of the licensor: - * Apache FOP, licensed by the Apache Software Foundation - * - */ - -/* $Id$ */ - -package org.foray.common; - -import org.foray.common.url.URLFactory; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -/** - * This utility class is used to build URLs from Strings. The String can be - * normal URLs but also just filenames. The filenames get converted to a - * file URL. - * - * @author Jeremias Maerki - */ -public class URLBuilder { - - /** Private constructor (should not be instantiated). */ - private URLBuilder() { - } - - /** - * Build an URL based on a String. The String can be a normal URL or a - * filename. Filenames get automatically converted to to URLs. - * - * @param spec A URL or a filename - * @return The requested URL - * @throws MalformedURLException If spec cannot be converted to a URL. - */ - public static URL buildURL(String spec) throws MalformedURLException { - if (spec == null) { - throw new NullPointerException("spec must not be null"); - } - File f = new File(spec); - if (f.exists()) { - return f.toURL(); - } - return URLFactory.createURL(spec); - } - - - /** - * Build an URL based on a String. The String can be a normal URL or a - * filename. Filenames get automatically converted to to URLs. - * - * @param baseURL Base URL for relative paths - * @param spec A URL or a filename - * @return The requested URL - * @throws MalformedURLException If spec cannot be converted to a URL. - */ - public static URL buildURL(URL baseURL, String spec) - throws MalformedURLException { - if (spec == null) { - throw new NullPointerException("spec must not be null"); - } - try { - URL u1 = buildURL(spec); - return u1; - } catch (MalformedURLException mfue) { - if (baseURL == null) { - throw mfue; - } - URL u2 = URLFactory.createURL(baseURL, spec); - return u2; - } - } - - /** - * <p>This method is an attempt to standardize the way that URLs are - * constructed, and, more importantly, how they are converted into - * Streams or other i/o resources. There is no way to know when a URL - * is constructed whether it can actually be opened or not. So it is - * frequently the case that resources identified by URL must be tried - * several times as various possibilities are attempted. To standardize - * this, three possibilities were considered:</p> - * - * <ol> - * <li>Loop through the possibilities here, attempting to open each - * one. When one is successfully opened, return the URL.</li> - * <li>Set up something stateful that would create the next possible URL - * on demand.</li> - * <li>Build and return a collection of possibilities & let the client - * manage the process.</li> - * </ol> - * - * <p>Of these three, 1 forces the URL to be opened twice, which was deemed - * wasteful. 2 seems like way too much trouble for such a simple task. - * An object would have to be created to manage the state information. - * 3, while somewhat wasteful, seems much less wasteful than the other two, - * and is the plan that was adopted.</p> - * - * @param baseURLs Array of URLs containing possible paths that should be - * tried used as the base for relative URLs. - * @param urlSpecified String containing the name of the URL. - * @return A List of URLs that the client should try to open, in the order - * that they should be tried. - * @throws MalformedURLException If it is unable to create even one - * URL instance. - */ - public static List buildURLList(URL[] baseURLs, String urlSpecified) - throws MalformedURLException { - if (urlSpecified == null) { - return null; - } - ArrayList list = new ArrayList(); - URL newURL = null; - - // Try the raw urlSpecified first. - try { - newURL = URLFactory.createURL(urlSpecified); - } catch (MalformedURLException e) { - // Ignore. - } - if (newURL != null) { - list.add(newURL); - } - - // Try it with the "file" protocol prepended. - newURL = null; - try { - newURL = URLFactory.createURL("file:" + urlSpecified); - } catch (MalformedURLException e) { - // Ignore. - } - if (newURL != null) { - list.add(newURL); - } - - // Now try it as a relative URL - newURL = null; - /* - * The following piece of code is based on the following statement in - * RFC2396 section 5.2: - * - * 3) If the scheme component is defined, indicating that the - * reference starts with a scheme name, then the reference is - * interpreted as an absolute URI and we are done. Otherwise, - * the reference URI's scheme is inherited from the base URI's - * scheme component. - * - * Due to a loophole in prior specifications [RFC1630], some - * parsers allow the scheme name to be present in a relative - * URI if it is the same as the base URI scheme. Unfortunately, - * this can conflict with the correct parsing of - * non-hierarchical URI. For backwards compatibility, an - * implementation may work around such references by removing - * the scheme if it matches that of the base URI and the scheme - * is known to always use the <hier_part> syntax. - * - * The URL class does not implement this work around, so we do. - */ - if (baseURLs != null) { - for (int i = 0; i < baseURLs.length; i++) { - URL baseURL = baseURLs[i]; - if (baseURL == null) { - continue; - } - String scheme = baseURL.getProtocol() + ":"; - // Copy the string to avoid side effects - String relativeURL = new String(urlSpecified); - if (urlSpecified.startsWith(scheme)) { - relativeURL = relativeURL.substring(scheme.length()); - } - try { - newURL = URLFactory.createURL(baseURL, relativeURL); - } catch (MalformedURLException e1) { - // Ignore - } - if (newURL != null) { - list.add(newURL); - } - } - } - - if (list.size() < 1) { - throw new MalformedURLException("Cannot build a valid URL " - + "from " + urlSpecified + "."); - } - return list; - } - - /** - * Converts standard-based uri-specification to a normalized String from - * which a java.net.URL instance can be constructed. Some standards, - * XSL-FO among them, require that uri-specification consists of the URI - * preceded by "url(" and followed by ")". This method validates and - * normalizes that input. - * @param input The uri-specification as defined in the XSL-FO Standard 1.0, - * Section 5.11. - * @return The normalized String, or null if the input is not valid. - */ - public static String normalizeURISpecification(String input) { - if (input == null) { - return null; - } - // Make a copy - String useInput = new String(input); - useInput = useInput.trim(); - if (! (useInput.startsWith("url("))) { - return null; - } - if (! (useInput.endsWith(")"))) { - return null; - } - // Strip off the leading "url(", the trailing ")". - useInput = useInput.substring(4, useInput.indexOf(")")); - // Strip off the optional whitespace - useInput = useInput.trim(); - boolean doubleQuotesUsed = false; - if (useInput.startsWith("'")) { - if (useInput.endsWith("'")) { - useInput = useInput.substring(1, useInput.length() - 1); - } else { - return null; - } - } else if (useInput.startsWith("\"")) { - if (useInput.endsWith("\"")) { - useInput = useInput.substring(1, useInput.length() - 1); - doubleQuotesUsed = true; - } else { - return null; - } - } - if (useInput.indexOf("'") != -1 && ! doubleQuotesUsed) { - return null; - } - return useInput; - } - -} Added: trunk/foray/foray-common/src/java/org/foray/common/url/URLUtil.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/url/URLUtil.java (rev 0) +++ trunk/foray/foray-common/src/java/org/foray/common/url/URLUtil.java 2006-07-15 23:09:14 UTC (rev 7784) @@ -0,0 +1,255 @@ +/* + * Copyright 2004 The FOray Project. + * http://www.foray.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This work is in part derived from the following work(s), used with the + * permission of the licensor: + * Apache FOP, licensed by the Apache Software Foundation + * + */ + +/* $Id$ */ + +package org.foray.common.url; + + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +/** + * This utility class is used to build URLs from Strings. The String can be + * normal URLs but also just filenames. The filenames get converted to a + * file URL. + * + * @author Jeremias Maerki + */ +public class URLUtil { + + /** Private constructor (should not be instantiated). */ + private URLUtil() { + } + + /** + * Build an URL based on a String. The String can be a normal URL or a + * filename. Filenames get automatically converted to to URLs. + * + * @param spec A URL or a filename + * @return The requested URL + * @throws MalformedURLException If spec cannot be converted to a URL. + */ + public static URL buildURL(String spec) throws MalformedURLException { + if (spec == null) { + throw new NullPointerException("spec must not be null"); + } + File f = new File(spec); + if (f.exists()) { + return f.toURL(); + } + return URLFactory.createURL(spec); + } + + + /** + * Build an URL based on a String. The String can be a normal URL or a + * filename. Filenames get automatically converted to to URLs. + * + * @param baseURL Base URL for relative paths + * @param spec A URL or a filename + * @return The requested URL + * @throws MalformedURLException If spec cannot be converted to a URL. + */ + public static URL buildURL(URL baseURL, String spec) + throws MalformedURLException { + if (spec == null) { + throw new NullPointerException("spec must not be null"); + } + try { + URL u1 = buildURL(spec); + return u1; + } catch (MalformedURLException mfue) { + if (baseURL == null) { + throw mfue; + } + URL u2 = URLFactory.createURL(baseURL, spec); + return u2; + } + } + + /** + * <p>This method is an attempt to standardize the way that URLs are + * constructed, and, more importantly, how they are converted into + * Streams or other i/o resources. There is no way to know when a URL + * is constructed whether it can actually be opened or not. So it is + * frequently the case that resources identified by URL must be tried + * several times as various possibilities are attempted. To standardize + * this, three possibilities were considered:</p> + * + * <ol> + * <li>Loop through the possibilities here, attempting to open each + * one. When one is successfully opened, return the URL.</li> + * <li>Set up something stateful that would create the next possible URL + * on demand.</li> + * <li>Build and return a collection of possibilities & let the client + * manage the process.</li> + * </ol> + * + * <p>Of these three, 1 forces the URL to be opened twice, which was deemed + * wasteful. 2 seems like way too much trouble for such a simple task. + * An object would have to be created to manage the state information. + * 3, while somewhat wasteful, seems much less wasteful than the other two, + * and is the plan that was adopted.</p> + * + * @param baseURLs Array of URLs containing possible paths that should be + * tried used as the base for relative URLs. + * @param urlSpecified String containing the name of the URL. + * @return A List of URLs that the client should try to open, in the order + * that they should be tried. + * @throws MalformedURLException If it is unable to create even one + * URL instance. + */ + public static List buildURLList(URL[] baseURLs, String urlSpecified) + throws MalformedURLException { + if (urlSpecified == null) { + return null; + } + ArrayList list = new ArrayList(); + URL newURL = null; + + // Try the raw urlSpecified first. + try { + newURL = URLFactory.createURL(urlSpecified); + } catch (MalformedURLException e) { + // Ignore. + } + if (newURL != null) { + list.add(newURL); + } + + // Try it with the "file" protocol prepended. + newURL = null; + try { + newURL = URLFactory.createURL("file:" + urlSpecified); + } catch (MalformedURLException e) { + // Ignore. + } + if (newURL != null) { + list.add(newURL); + } + + // Now try it as a relative URL + newURL = null; + /* + * The following piece of code is based on the following statement in + * RFC2396 section 5.2: + * + * 3) If the scheme component is defined, indicating that the + * reference starts with a scheme name, then the reference is + * interpreted as an absolute URI and we are done. Otherwise, + * the reference URI's scheme is inherited from the base URI's + * scheme component. + * + * Due to a loophole in prior specifications [RFC1630], some + * parsers allow the scheme name to be present in a relative + * URI if it is the same as the base URI scheme. Unfortunately, + * this can conflict with the correct parsing of + * non-hierarchical URI. For backwards compatibility, an + * implementation may work around such references by removing + * the scheme if it matches that of the base URI and the scheme + * is known to always use the <hier_part> syntax. + * + * The URL class does not implement this work around, so we do. + */ + if (baseURLs != null) { + for (int i = 0; i < baseURLs.length; i++) { + URL baseURL = baseURLs[i]; + if (baseURL == null) { + continue; + } + String scheme = baseURL.getProtocol() + ":"; + // Copy the string to avoid side effects + String relativeURL = new String(urlSpecified); + if (urlSpecified.startsWith(scheme)) { + relativeURL = relativeURL.substring(scheme.length()); + } + try { + newURL = URLFactory.createURL(baseURL, relativeURL); + } catch (MalformedURLException e1) { + // Ignore + } + if (newURL != null) { + list.add(newURL); + } + } + } + + if (list.size() < 1) { + throw new MalformedURLException("Cannot build a valid URL " + + "from " + urlSpecified + "."); + } + return list; + } + + /** + * Converts standard-based uri-specification to a normalized String from + * which a java.net.URL instance can be constructed. Some standards, + * XSL-FO among them, require that uri-specification consists of the URI + * preceded by "url(" and followed by ")". This method validates and + * normalizes that input. + * @param input The uri-specification as defined in the XSL-FO Standard 1.0, + * Section 5.11. + * @return The normalized String, or null if the input is not valid. + */ + public static String normalizeURISpecification(String input) { + if (input == null) { + return null; + } + // Make a copy + String useInput = new String(input); + useInput = useInput.trim(); + if (! (useInput.startsWith("url("))) { + return null; + } + if (! (useInput.endsWith(")"))) { + return null; + } + // Strip off the leading "url(", the trailing ")". + useInput = useInput.substring(4, useInput.indexOf(")")); + // Strip off the optional whitespace + useInput = useInput.trim(); + boolean doubleQuotesUsed = false; + if (useInput.startsWith("'")) { + if (useInput.endsWith("'")) { + useInput = useInput.substring(1, useInput.length() - 1); + } else { + return null; + } + } else if (useInput.startsWith("\"")) { + if (useInput.endsWith("\"")) { + useInput = useInput.substring(1, useInput.length() - 1); + doubleQuotesUsed = true; + } else { + return null; + } + } + if (useInput.indexOf("'") != -1 && ! doubleQuotesUsed) { + return null; + } + return useInput; + } + +} Property changes on: trunk/foray/foray-common/src/java/org/foray/common/url/URLUtil.java ___________________________________________________________________ Name: svn:keywords + "Author Id Rev Date URL" Name: svn:eol-style + native Modified: trunk/foray/foray-font/src/java/org/foray/font/FontConfigParser.java =================================================================== --- trunk/foray/foray-font/src/java/org/foray/font/FontConfigParser.java 2006-07-15 22:29:14 UTC (rev 7783) +++ trunk/foray/foray-font/src/java/org/foray/font/FontConfigParser.java 2006-07-15 23:09:14 UTC (rev 7784) @@ -26,7 +26,7 @@ import org.foray.common.Environment; import org.foray.common.RandomReader; -import org.foray.common.URLBuilder; +import org.foray.common.url.URLUtil; import org.foray.common.url.URLFactory; import org.foray.ps.encode.EncodingParser; import org.foray.ps.encode.EncodingVector; @@ -367,7 +367,7 @@ if (xmlBase != null && !xmlBase.equals("")) { try { /* Update the current xml base. */ - this.currentXMLBase = URLBuilder.buildURL(this.previousXMLBase, + this.currentXMLBase = URLUtil.buildURL(this.previousXMLBase, xmlBase); } catch (MalformedURLException e) { logError("Unable to create URL from " @@ -562,7 +562,7 @@ } try { /* Update the current xml base. */ - this.currentXMLBase = URLBuilder.buildURL(this.currentXMLBase, + this.currentXMLBase = URLUtil.buildURL(this.currentXMLBase, xmlBase); } catch (MalformedURLException e) { logError("Unable to create URL from " Modified: trunk/foray/foray-font/src/java/org/foray/font/format/TTFReader.java =================================================================== --- trunk/foray/foray-font/src/java/org/foray/font/format/TTFReader.java 2006-07-15 22:29:14 UTC (rev 7783) +++ trunk/foray/foray-font/src/java/org/foray/font/format/TTFReader.java 2006-07-15 23:09:14 UTC (rev 7784) @@ -25,7 +25,7 @@ package org.foray.font.format; import org.foray.common.Logging; -import org.foray.common.URLBuilder; +import org.foray.common.url.URLUtil; import org.apache.commons.logging.Log; @@ -79,7 +79,7 @@ } FontFileReader reader = null; try { - URL url = URLBuilder.buildURL(fileName); + URL url = URLUtil.buildURL(fileName); reader = new FontFileReader(logger, url); } catch (MalformedURLException e1) { logger.error("Malformed URL: " + fileName); Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtURI.java =================================================================== --- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtURI.java 2006-07-15 22:29:14 UTC (rev 7783) +++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtURI.java 2006-07-15 23:09:14 UTC (rev 7784) @@ -24,7 +24,7 @@ package org.foray.fotree.value; -import org.foray.common.URLBuilder; +import org.foray.common.url.URLUtil; import java.util.ArrayList; @@ -57,7 +57,7 @@ * @return The created UriDT instance, or null if input is invalid. */ public static DtURI makeUriDT(String input) { - String useInput = URLBuilder.normalizeURISpecification(input); + String useInput = URLUtil.normalizeURISpecification(input); if (useInput == null) { return null; } Modified: trunk/foray/foray-graphic/src/java/org/foray/graphic/FOrayGraphicServer.java =================================================================== --- trunk/foray/foray-graphic/src/java/org/foray/graphic/FOrayGraphicServer.java 2006-07-15 22:29:14 UTC (rev 7783) +++ trunk/foray/foray-graphic/src/java/org/foray/graphic/FOrayGraphicServer.java 2006-07-15 23:09:14 UTC (rev 7784) @@ -25,7 +25,7 @@ package org.foray.graphic; import org.foray.common.Logging; -import org.foray.common.URLBuilder; +import org.foray.common.url.URLUtil; import org.foray.graphic.factory.BMPFactory; import org.foray.graphic.factory.EPSFactory; import org.foray.graphic.factory.GIFFactory; @@ -144,7 +144,7 @@ List urlList; // An exception was thrown in buildURLList if urlList is null. try { - urlList = URLBuilder.buildURLList(baseURLs, href); + urlList = URLUtil.buildURLList(baseURLs, href); } catch (MalformedURLException e) { throw new GraphicException(e.getMessage()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |