Java Swing HTML loadStyleSheet(URL url)

Here you can find the source of loadStyleSheet(URL url)

Description

Loads a set of rules that have been specified in terms of CSS1 grammar.

License

Open Source License

Parameter

Parameter Description
url location to the css file

Exception

Parameter Description
IOException if file doesn't exist or some problem occurs during readingdata from file

Return

style sheet (css1)

Declaration

public static StyleSheet loadStyleSheet(URL url) throws IOException 

Method Source Code

//package com.java2s;
/*//www  .ja va 2  s  .  c o  m
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

import java.net.URL;

import javax.swing.text.html.StyleSheet;

public class Main {
    /**
     * Loads a set of rules that have been specified in terms of CSS1 grammar.
     * 
     * @param path
     *            absolute path to the css file.
     * @return style sheet (css1)
     * @throws IOException
     *             if file doesn't exist or some problem occurs during reading
     *             data from file
     */
    public static StyleSheet loadStyleSheet(String path) throws IOException {
        StyleSheet ss = new StyleSheet();
        FileReader fr = new FileReader(path);
        BufferedReader reader = new BufferedReader(fr);
        ss.loadRules(reader, null);
        reader.close();
        return ss;
    }

    /**
     * Loads a set of rules that have been specified in terms of CSS1 grammar.
     * 
     * @param url
     *            location to the css file
     * @return style sheet (css1)
     * @throws IOException
     *             if file doesn't exist or some problem occurs during reading
     *             data from file
     */
    public static StyleSheet loadStyleSheet(URL url) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                url.openStream()));
        StyleSheet ss = new StyleSheet();
        ss.loadRules(reader, null);
        return ss;
    }

    /**
     * Closes the streams and resources.
     * 
     * @param resource
     *            to be closed.
     */
    public static void close(Object resource) {
        try {
            if (resource instanceof InputStream) {
                ((InputStream) resource).close();
            } else if (resource instanceof OutputStream) {
                ((OutputStream) resource).close();
            } else if (resource instanceof Reader) {
                ((Reader) resource).close();
            } else if (resource instanceof Writer) {
                ((Writer) resource).close();
            } else if (resource instanceof RandomAccessFile) {
                ((RandomAccessFile) resource).close();
            } else if (resource != null) {
                throw new IllegalArgumentException("Unknown resource: "
                        + resource);
            }
        } catch (IOException e) {
        }
    }
}

Related

  1. getImgs(final String html)
  2. getRowIndex(final Element cell)
  3. hasClass(AttributeSet attr, String className)
  4. htmlToPlain(String html)
  5. isUnderline(Element element)
  6. makeStyleSheet(String name)
  7. nameOf(Element element)
  8. plainToHtml(String plain)
  9. rtfToBody(String rtf)