alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

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.api.xml.parsers;

import java.io.*;
import java.net.URL;
import java.util.*;

import javax.swing.text.Document;

import org.xml.sax.InputSource;

import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;

/**
 * Integrate NetBeans widely used Swing's {@link Document} with SAX API's.
 * Let it look like {@link InputSource}.
 *
 * @author  Petr Kuzel
 */
public final class DocumentInputSource extends InputSource {
    
    private final Document doc;
     
    /** 
     * Creates new instance of DocumentInputSource. Client should
     * set system ID if available otherwise default one is derived.
     * @param doc Swing document used to be wrapped
     * @see   #getSystemId()
     */
    public DocumentInputSource(Document doc) {
        this.doc = doc;
    }

    // inherit JavaDoc
    public Reader getCharacterStream() {
        String text = documentToString(doc);
        return new StringReader(text);
    }

    /**
     * This InputSource is backended by Swing's Document.
     * Consequently its character stream is read-only, it
     * always reads content of associted Document.
     */
    public final void setCharacterStream(Reader reader) {
        // do nothing
    }

    /**
     * Get InputSource system ID. Use ordered logic:
     * 
    *
  • use client's setSystemId(), or *
  • try to derive it from Document *

    e.g. look at Document.StreamDescriptionProperty for * {@link DataObject} and use URL of its primary file. *

* @return entity system Id or null */ public String getSystemId() { String system = super.getSystemId();; // XML module specifics property, promote into this API // String system = (String) doc.getProperty(TextEditorSupport.PROP_DOCUMENT_URL); if (system == null) { Object obj = doc.getProperty(Document.StreamDescriptionProperty); if (obj instanceof DataObject) { try { DataObject dobj = (DataObject) obj; FileObject fo = dobj.getPrimaryFile(); URL url = fo.getURL(); system = url.toExternalForm(); } catch (IOException io) { ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class); emgr.notify(io); } } else { ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class); emgr.log("XML:DocumentInputSource:Unknown stream description:" + obj); } } return system; } /** * @return current state of Document as string */ private static String documentToString(final Document doc) { final String[] str = new String[1]; // safely take the text from the document Runnable run = new Runnable() { public void run () { try { str[0] = doc.getText(0, doc.getLength()); } catch (javax.swing.text.BadLocationException e) { // impossible e.printStackTrace(); } } }; doc.render(run); return str[0]; } /** * For debugging purposes only. */ public String toString() { return "DocumentInputSource SID:" + getSystemId(); } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.