package com.dbxml.db.core.security;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Access.java,v 1.3 2004/02/12 00:17:57 bradford Exp $
 */

import com.dbxml.xml.XMLSerializable;
import com.dbxml.xml.dom.DOMUtils;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Access
 */

public class Access implements XMLSerializable {
   private static final String[] EmptyStrings = new String[0];

   private static final String ACCESS = "access";
   private static final String PATH = "path";
   private static final String ROLE = "role";
   private static final String PERMISSIONS = "permissions";

   public static final int NONE = 0;
   public static final int READ = 1;
   public static final int WRITE = 2;
   public static final int EXECUTE = 4;
   public static final int CREATE = 8;

   public static final int MASK = 0xF;

   private String path = "";
   private Map roles = Collections.synchronizedMap(new TreeMap()); // String to Integer

   public Element streamToXML(Document doc) throws DOMException {
      Element contentElem = doc.createElement(ACCESS);
      contentElem.setAttribute(PATH, path);

      Iterator iter = roles.entrySet().iterator();
      while ( iter.hasNext() ) {
         Map.Entry entry = (Map.Entry)iter.next();
         String role = (String)entry.getKey();
         Integer permissions = (Integer)entry.getValue();
         Element roleElem = DOMUtils.addElement(contentElem, ROLE);
         roleElem.setAttribute(PERMISSIONS, permissions.toString());
         DOMUtils.setText(roleElem, role);
      }

      return contentElem;
   }

   public void streamFromXML(Element element) throws DOMException {
      path = element.getAttribute(PATH);

      NodeList nl = element.getElementsByTagName(ROLE);
      for ( int i = 0; i < nl.getLength(); i++ ) {
         Element roleElem = (Element)nl.item(i);
         String role = DOMUtils.getText(roleElem);
         Integer permission = new Integer(roleElem.getAttribute(PERMISSIONS));
         roles.put(role, permission);
      }
   }

   public void setPath(String path) {
      this.path = path;
   }

   public String getPath() {
      return path;
   }

   public int getPermissions(Role role) {
      Integer permissions = (Integer)roles.get(role.getId());
      if ( permissions != null )
         return permissions.intValue();
      else
         return 0;
   }

   public void setPermissions(Role role, int permissions) {
      permissions &= MASK;
      if ( permissions != 0 )
         roles.put(role.getId(), new Integer(permissions));
      else
         roles.remove(role.getId());
   }

   public void removePermissions(Role role) {
      setPermissions(role, 0);
   }

   public String[] listRoles() {
      return (String[])roles.keySet().toArray(EmptyStrings);
   }
}
