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.io.CharConversionException;
import java.io.Writer;
import org.openide.xml.XMLUtil;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
 * A method or a static function.
 *
 * @author Tim Lebedkov
 */
public class XMLSerializer implements ContentHandler {
    private IndentedWriter w;
    private boolean firstElement = true;
    private boolean lastElement = false;
    private String publicID;
    private String systemID;
    
    /**
     * Constructor
     *
     * @param w output stream
     */
    public XMLSerializer(Writer w) {
        this.w = new IndentedWriter(w);
    }
    
    /**
     * Sets new public ID
     *
     * @param id new id or null if not DTD
     */
    public void setPublicID(String id) {
        this.publicID = id;
    }
    
    /**
     * Sets new system ID
     *
     * @param id new id or null if not DTD
     */
    public void setSystemID(String id) {
        this.systemID = id;
    }
    
    public void characters(char[] ch, int start, int length) throws SAXException {
        try {
            w.print(XMLUtil.toElementContent(new String(ch, start, length)));
        } catch (CharConversionException e) {
            throw new SAXException(e.getMessage());
        }
        lastElement = false;
    }
    
    public void endDocument() throws SAXException {
        // todo ????
    }
    
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if(lastElement)
            w.println();
        
        w.unindent();
        
        // todo ???
        w.print("");
        lastElement = true;
    }
    
    public void endPrefixMapping(String prefix) throws SAXException {
        // todo not implemented
    }
    
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        // todo not implemented
    }
    
    public void processingInstruction(String target, String data) throws SAXException {
        // todo not implemented
    }
    
    public void setDocumentLocator(Locator locator) {
        // todo ???
    }
    
    public void skippedEntity(String name) throws SAXException {
        // todo ???
    }
    
    public void startDocument() throws SAXException {
        // todo ?
        w.println("");
        w.println();
    }
    
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if(firstElement) {
            if(publicID != null && systemID != null) {
                w.println("");
                w.println();
            }
            
            firstElement = false;
        }
        
        if(lastElement)
            w.println();
        
        // todo escape names?
        w.print("<" + qName);
        for(int i = 0; i < atts.getLength(); i++) {
            w.print(" ");
            w.print(atts.getQName(i) + "=\"");
            w.print(atts.getValue(i) + "\"");
        }
        w.print(">");
        w.indent();
        lastElement = true;
    }
    
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        // todo not implemented
    }
}

... 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.