// Decompiled by JAD v1.5.5. Copyright 1997-98 Pavel Kouznetsov.
// JAD Home Page:      http://web.unicom.com.cy/~kpd/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SetContentHandler.java

package org.xmldb.api.sdk.modules;

import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Hashtable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmldb.api.base.Resource;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.base.XMLDBException;

public class SetContentHandler extends DefaultHandler
{

    public SetContentHandler(XMLResource xmlresource)
    {
        resource = null;
        newContent = null;
        namespaces = null;
        resource = xmlresource;
        namespaces = new Hashtable();
    }

    public void characters(char ac[], int i, int j)
        throws SAXException
    {
        for(int k = 0; k < j; k++)
        {
            char c = ac[i + k];
            switch(c)
            {
            case 38: /* '&' */
                newContent.append("&amp;");
                break;

            case 60: /* '<' */
                newContent.append("&lt;");
                break;

            case 62: /* '>' */
                newContent.append("&gt;");
                break;

            case 34: /* '"' */
                newContent.append("&quot;");
                break;

            case 39: /* '\'' */
                newContent.append("&apos;");
                break;

            default:
                if(c > '\177')
                    newContent.append("&#" + (int)c + ";");
                else
                    newContent.append(c);
                break;

            }
        }

    }

    public void endDocument()
        throws SAXException
    {
    	try {
        	resource.setContent(newContent.toString());
        }
        catch(XMLDBException e) {
        	throw new SAXException (e.getMessage());
        }
        //System.out.println(newContent.toString());
    }

    public void endElement(String s, String s1, String s2)
        throws SAXException
    {
        newContent.append("</");
        newContent.append(s2);
        newContent.append(">");
    }

    public void endPrefixMapping(String s)
        throws SAXException
    {
        namespaces.remove(s);
    }

    public void ignorableWhitespace(char ac[], int i, int j)
        throws SAXException
    {
        for(int k = 0; k < j; k++)
            newContent.append(ac[i + k]);

    }

    public void processingInstruction(String s, String s1)
        throws SAXException
    {
        newContent.append("<?");
        newContent.append(s);
        newContent.append(" ");
        if(s1 != null)
            newContent.append(s1);
        newContent.append("?>");
    }

    public void skippedEntity(String s)
        throws SAXException
    {
    }

    public void startDocument()
        throws SAXException
    {
        newContent = new StringBuffer();
        newContent.append("<?xml version=\"1.0\"?>");
    }

    public void startElement(String s, String s1, String s2, Attributes attributes)
        throws SAXException
    {
        newContent.append("<");
        newContent.append(s2);
        for(int i = 0; i < attributes.getLength(); i++)
        {
            newContent.append(" ");
            newContent.append(attributes.getQName(i));
            newContent.append("=");
            newContent.append("\"");
            newContent.append(attributes.getValue(i));
            newContent.append("\"");
        }

        String s3;
        for(Enumeration enumeration = namespaces.keys(); enumeration.hasMoreElements(); namespaces.remove(s3))
        {
            s3 = (String)enumeration.nextElement();
            newContent.append(" xmlns:");
            newContent.append(s3);
            newContent.append("=");
            newContent.append("\"");
            newContent.append(namespaces.get(s3));
            newContent.append("\"");
        }

        newContent.append(">");
    }

    public void startPrefixMapping(String s, String s1)
        throws SAXException
    {
        namespaces.put(s, s1);
    }

    protected XMLResource resource;
    protected StringBuffer newContent;
    protected Hashtable namespaces;
}
