/*
 * This file is part of the DITA Open Toolkit project hosted on
 * Sourceforge.net. See the accompanying license.txt file for 
 * applicable licenses.
 */

/*
 * (c) Copyright IBM Corp. 2005, 2006 All Rights Reserved.
 */
package org.dita.dost.util;

import java.io.File;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;


/**
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments.
 *
 * @author Zhang, Yuan Peng
 */
public class CatalogParser implements ContentHandler{
    private String catalogDir;


    private String dtdBase;
    private String schemaBase; 
    private HashMap map;
    
    /**
     * Automatically generated constructor: CatalogParser.
     */
    public CatalogParser() {
    	this(null, null);
    }
           

    /**
     * Default constructor of CatalogParser class.
     * 
     * @param catalogMap catalogMap
     * @param ditaDir ditaDir
     */
    public CatalogParser(HashMap catalogMap, String ditaDir) {
        map = catalogMap;
        catalogDir = ditaDir;
        dtdBase = null;
    }
    
    /** 
     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
     */
    public void characters(char[] ch, int start, int length)
            throws SAXException {
    }
    
    /** 
     * @see org.xml.sax.ContentHandler#endDocument()
     */
    public void endDocument() throws SAXException {
    }
    
    /** 
     * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if ("group".equals(qName)){
            dtdBase = null;
        }
    }
    
    /**
     * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
     */
    public void endPrefixMapping(String prefix) throws SAXException {
    }
    
    /**
     * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
     */
    public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException {
    }
    
    /**
     * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
     */
    public void processingInstruction(String target, String data)
            throws SAXException {
    }
    
    /**
     * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
     */
    public void setDocumentLocator(Locator locator) {
    }
    /**
     * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
     */
    public void skippedEntity(String name) throws SAXException {
    }
    /**
     * @see org.xml.sax.ContentHandler#startDocument()
     */
    public void startDocument() throws SAXException {
    }
    /**
     * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        if ("group".equals(qName)){
        	String xmlBase = atts.getValue("xml:base");
        	if(xmlBase!=null){
        		if (xmlBase.indexOf("dtd")!=-1){
        			dtdBase = atts.getValue("xml:base");
        		}else if (xmlBase.indexOf("schema")!=-1){
        			schemaBase = atts.getValue("xml:base");
        		}
        	}
        }
        
        if ("public".equals(qName)){
            String localURI;
            String absoluteLocalURI;
            localURI = (dtdBase != null) ? dtdBase+File.separatorChar+atts.getValue("uri") : atts.getValue("uri");
            absoluteLocalURI = (catalogDir != null) ? catalogDir + File.separatorChar + localURI : localURI;
            map.put(atts.getValue("publicId"), absoluteLocalURI);
        }else if("system".equals(qName)){
        	String localURI;
            String absoluteLocalURI;
            localURI = (schemaBase != null) ? schemaBase+File.separatorChar+atts.getValue("uri") : atts.getValue("uri");
            absoluteLocalURI = (catalogDir != null) ? catalogDir + File.separatorChar + localURI : localURI;
            map.put(atts.getValue("systemId"), absoluteLocalURI);
        }
    }
    /**
     * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
     */
    public void startPrefixMapping(String prefix, String uri)
            throws SAXException {
    }
}
@@ -600,16 +600,17 @@
 eng%0A */%0A
+%0A
 public c
@@ -3171,32 +3171,34 @@
 ption %7B%0A        
+/*
 if (%22group%22.equa
@@ -3201,32 +3201,32 @@
 equals(qName))%7B%0A
-
         %09String 
@@ -3512,24 +3512,26 @@
 %09%7D%0A        %7D
+*/
 %0A        %0A  
@@ -3557,32 +3557,237 @@
 equals(qName))%7B%0A
+        %09%0A        %09String xmlBase = atts.getValue(%22xml:base%22);%0A        %09if(xmlBase!=null)%7B%0A        %09%09if (xmlBase.indexOf(%22dtd%22)!=-1)%7B%0A        %09%09%09dtdBase = atts.getValue(%22xml:base%22);%0A        %09%09%7D%0A        %09%7D%0A
             Stri
@@ -4118,32 +4118,32 @@
 oluteLocalURI);%0A
-
         %7Delse if
@@ -4160,32 +4160,253 @@
 equals(qName))%7B%0A
+        %09%0A        %09String xmlBase = atts.getValue(%22xml:base%22);%0A        %09if(xmlBase!=null)%7B%0A        %09%09if (xmlBase.indexOf(%22schema%22)!=-1)%7B%0A        %09%09%09schemaBase = atts.getValue(%22xml:base%22);%0A        %09%09%7D%0A        %09%7D%0A        %09%0A
         %09String 

Refactor java code for change of catalog-dita_template.xml