package com.simonstl.namespace.attributes;

import com.simonstl.namespace.*;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.XMLFilter;
import org.xml.sax.helpers.XMLFilterImpl;
import java.util.*;

/*
The contents of this file are subject to the Mozilla Public License  Version 1.1 (the "License"); you may not use this file except in  compliance with the License. You may obtain a copy of the License at 
http://www.mozilla.org/MPL/ 

Software distributed under the License is distributed on an "AS IS"  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the  License for the specific language governing rights and limitations under  the License. 

The Original Code is available at  http://simonstl.com/projects/fragment/original. 

The Initial Developer of the Original Code is Simon St.Laurent. Portions  created by Simon St.Laurent are Copyright (C) 2001. All Rights Reserved. 

Contributor(s): 
*/

/**
*
* <p>A filter which replaces namespaces according to a set of rules. If no rules are set, no namespaces will be modified.</p>
*
* @version 0.01  1 August 2001
* @author Simon St.Laurent
*/

public class AttribReplNamespaceFilter extends XMLFilterImpl { 

    private ReplaceNamespaceRules rules=null;   //set of rules

/**
* An empty constructor that requires the use of setParent before starting
* I don't think this works.
*/
public AttribReplNamespaceFilter () {

 } 

/**
* A constructor that takes the parser which will feed it SAX events
*/

public AttribReplNamespaceFilter (XMLReader parent) {
    super(parent); 
} 

/**
* sets up the rules, read in from a config file by RulesLoader or
* otherwise concocted in Java using the {@link com.simonstl.namespace.ReplaceNamespaceRules ReplaceNamespaceRules} class.
*
* @param newRules  the set of rules the NamespaceFilter will apply to content
*/
public void setRules (ReplaceNamespaceRules newRules) {
  rules=newRules;
//rules.print();
}

/**
* in case you ever need to get the rules out of NamespaceFilter 
*/
public ReplaceNamespaceRules getRules () {
  return rules;
}

/** 
* Check at startElement for names which should be replaced. 
* if the context namespace URI matches a rule, 
* the namespace will be changed according to the rule.
*/ 

 
public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException {
  if (rules!=null) {
      atts=processAttributes(uri, localName, qName, atts);
  }

  super.startElement(uri, localName, qName, atts); 
}

public Attributes processAttributes (String uri, String localName, String qName, Attributes atts) {
    //copy into new object to permit setting values
    AttributesImpl attsWork=new AttributesImpl(atts);
    int numAtts=attsWork.getLength();
    for (int i=0; i<numAtts; i++) {
    String nsURI=attsWork.getURI(i);
    if (!( nsURI.equals("") )){
        if (rules.isRule(nsURI)) {
         ReplaceNamespaceRule rule=rules.getRule(nsURI);
         attsWork.setURI(i, rule.getResultURI());

         String prefix=rule.getResultPrefix();
         if (prefix!=null){
           String attQName=attsWork.getLocalName(i);
           if (!(prefix.equals(""))) {
             attQName=prefix+":"+attsWork.getLocalName(i);
           }

           attsWork.setQName(i, attQName);

         }
       }

  }

    } //end for
    return (Attributes) attsWork;
}


} 
