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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.core.translators;

import java.util.Collection;
import java.util.List;
import java.util.Stack;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Helps to create XML.
 *
 * @author Tim Lebedkov
 * @version $Id: XMLCreator.java,v 1.3 2004/02/20 11:44:32 pkuzel Exp $
 */
public class XMLCreator {
    private ContentHandler ch;
    private AttributesImpl atts = new AttributesImpl();
    private Stack elements = new Stack();
    
    /**
     * Constructor
     *
     * @param ch XML content handler
     */
    public XMLCreator(ContentHandler ch) {
        this.ch = ch;
    }
    
    /**
     * Receive notification of character data.
     *
     * @param s The characters from the XML document. if null nothing will
     * be printed
     * @exception org.xml.sax.SAXException Any SAX exception, possibly
     *            wrapping another exception.
     */
    public void ch(String s) throws SAXException {
        if(s == null) return;
        char[] ch = s.toCharArray();
        this.ch.characters(ch, 0, ch.length);
    }
    
    /**
     * Starts an element
     *
     * @param qName The qualified name (with prefix), or the
     *        empty string if qualified names are not available.
     */
    public void e(String qName) throws SAXException {
        atts.clear();
        ch.startElement("", "", qName, atts);
        elements.push(qName);
    }
    
    /**
     * Starts an element
     *
     * @param qName The qualified name (with prefix), or the
     *        empty string if qualified names are not available.
     * @param attrs Attributes {name, value, name, value}. String[]
     * if null is used as a value attribute will not be created
     */
    public void e(String qName, Collection attrs) throws SAXException {
        e(qName, (String[]) attrs.toArray(new String[attrs.size()]));
    }
    
    /**
     * Starts an element
     *
     * @param qName The qualified name (with prefix), or the
     *        empty string if qualified names are not available.
     * @param attrs Attributes {name, value, name, value}
     * if null is used as a value attribute will not be created
     */
    private void e(String qName, String[] attrs) throws SAXException {
        atts.clear();
        for(int i = 0; i < attrs.length; i += 2) {
            if(attrs[i + 1] != null)
                atts.addAttribute("", "", attrs[i], "", attrs[i + 1]);
        }
        ch.startElement("", "", qName, atts);
        elements.push(qName);
    }
    
    /**
     * Closes an element
     */
    public void ee() throws SAXException {
        ch.endElement("", "", (String) elements.pop());
    }
    
    /**
     * Receive notification of the beginning of a document.
     *
     * 

The SAX parser will invoke this method only once, before any * other methods in this interface or in {@link org.xml.sax.DTDHandler * DTDHandler} (except for {@link #setDocumentLocator * setDocumentLocator}).

* * @exception org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @see #endDocument */ public void startDocument() throws SAXException { ch.startDocument(); } /** * Receive notification of the end of a document. * *

The SAX parser will invoke this method only once, and it will * be the last method invoked during the parse. The parser shall * not invoke this method until it has either abandoned parsing * (because of an unrecoverable error) or reached the end of * input.

* * @exception org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @see #startDocument */ public void endDocument() throws SAXException { ch.endDocument(); } }
... 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.