Logo Search packages:      
Sourcecode: afnix version File versions

XmlAttlist.cpp

// ---------------------------------------------------------------------------
// - XmlAttlist.cpp                                                          -
// - afnix:xml module - xml attribute list node class implementation         -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - 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.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2007 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "XmlSystem.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"
#include "XmlAttlist.hpp"

namespace afnix {

  // -------------------------------------------------------------------------
  // - private section                                                       -
  // -------------------------------------------------------------------------

  // the attribute list node name
  static const String XML_ATTR_NAME = "!ATTLIST";

  // the xml attribute types
  static const String XML_ATTR_FIXD = "#FIXED";
  static const String XML_ATTR_IMPL = "#IMPLIED";
  static const String XML_ATTR_REQR = "#REQUIRED";


  // this procedure parse a string and returns an attribute type
  static XmlAttlist::t_attr to_type (const String& type) {
    // check for defined type
    if (type == "CDATA")    return XmlAttlist::CDATA;
    if (type == "ID")       return XmlAttlist::ID;
    if (type == "IDREF")    return XmlAttlist::IDREF;
    if (type == "IDREFS")   return XmlAttlist::IDREFS;
    if (type == "ENTITY")   return XmlAttlist::ENTITY;
    if (type == "ENTITIES") return XmlAttlist::ENTITIES;
    if (type == "NMTOKEN")  return XmlAttlist::NMTOKEN;
    if (type == "NMTOKENS") return XmlAttlist::NMTOKENS;
    // invalid type
    throw Exception ("xml-error", "invalid attribute type", type);
  }

  // this procedure creates an enumeration from a string vector
  static String to_enum (const Strvec& xenm) {
    long len = xenm.length ();
    String result = "(";
    for (long i = 0; i < len; i++) {
      result += xenm.get (i);
      if (i < len - 1) result += " | ";
    }
    result += ")";
    return result;
  }

  // this procedure returns a string representation of a type
  static String to_type (const XmlAttlist::t_attr& type, const Strvec& xenm) {
    String result;
    switch (type) {
    case XmlAttlist::CDATA:
      result = "CDATA";
      break;
    case XmlAttlist::ID:
      result = "ID";
      break;
    case XmlAttlist::IDREF:
      result = "IDREF";
      break;
    case XmlAttlist::IDREFS:
      result = "IDREFS";
      break;
    case XmlAttlist::ENTITY:
      result = "ENTITY";
      break;
    case XmlAttlist::ENTITIES:
      result = "ENTITIES";
      break;
    case XmlAttlist::NMTOKEN:
      result = "NMTOKEN";
      break;
    case XmlAttlist::NMTOKENS:
      result = "NMTOKENS";
      break;
    case XmlAttlist::ENUMTYPE:
      result = to_enum (xenm);
      break;
    case XmlAttlist::NOTATION:
      result  = "NOTATION ";
      result += to_enum (xenm);
      break;
    }
    return result;
  }

  // this procedure returns a string representation of a default
  static String to_xdef (const XmlAttlist::t_xdef adef, const String& xdef) {
    String result;
    switch (adef) {
    case XmlAttlist::VALUE:
      result = xdef.toliteral ();
      break;
    case XmlAttlist::FIXED:
      result  = "#FIXED ";
      result += xdef.toliteral ();
      break;
    case XmlAttlist::IMPLIED:
      result = "#IMPLIED";
      break;
    case XmlAttlist::REQUIRED:
      result = "#REQUIRED";
      break;
    }
    return result;
  }

  // -------------------------------------------------------------------------
  // - class section                                                         -
  // -------------------------------------------------------------------------

  // create an attribute list by names

00130   XmlAttlist::XmlAttlist (const String& name, const String& attr) {
    d_eflg = true;
    d_name = name;
    d_attr = attr;
  }

  // return the document class name

00138   String XmlAttlist::repr (void) const {
    return "XmlAttlist";
  }

  // set the element name

00144   void XmlAttlist::setname (const String& name) {
    wrlock ();
    try {
      d_name = name;
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // get the element name

00157   String XmlAttlist::getname (void) const {
    rdlock ();
    String result = d_name;
    unlock ();
    return result;
  }


  // set the attribute name

00167   void XmlAttlist::setattr (const String& attr) {
    wrlock ();
    try {
      d_attr = attr;
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // get the attribute name

00180   String XmlAttlist::getattr (void) const {
    rdlock ();
    String result = d_attr;
    unlock ();
    return result;
  }

  // set the attribute type

00189   void XmlAttlist::settype (const String& type) {
    wrlock ();
    try {
      // reset the enumeration vector
      d_xenm.reset ();
      // check the type
      d_type = to_type (type);
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // set the enumeration type

00205   void XmlAttlist::settype (const Strvec& xenm, const bool xflg) {
    wrlock ();
    try {
      // set the type
      d_type = xflg ? NOTATION : ENUMTYPE;
      // save the enumeration vector
      d_xenm = xenm;
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // set the enumeration type

00221   void XmlAttlist::settype (Vector* xvec, const bool xflg) {
    // check for nil vector
    long argc = (xvec == nilp) ? 0 : xvec->length ();
    if (argc == 0) return;
    // convert the vector
    Strvec xenm (argc);
    for (long i = 0; i < argc; i++) xenm.add (xvec->getstring (i));
    // set the enumeration type
    settype (xenm, xflg);
  }

  // set the attribute default value

00234   void XmlAttlist::setxdef (const String& xdef) {
    wrlock ();
    try {
      if (xdef == XML_ATTR_IMPL) {
      d_adef = IMPLIED;
      d_xdef = "";
      } else if (xdef == XML_ATTR_REQR) {
      d_adef = REQUIRED;
      d_xdef = "";
      } else if (xdef == XML_ATTR_FIXD) {
      throw Exception ("xml-error", "fixed type without value");
      } else {
      d_adef = VALUE;
      d_xdef = xdef;
      }
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

00256   void XmlAttlist::setfixd (const String& xdef) {
    wrlock ();
    try {
      d_adef = FIXED;
      d_xdef = xdef;
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // write an attribute node to a buffer

00270   void XmlAttlist::write (Buffer& buf) const {
    rdlock ();
    try {
      // add the node name
      buf.add ("<");
      buf.add (XML_ATTR_NAME);
      // add the element name
      buf.add (' ');
      buf.add (d_name);
      // add the attribute name
      buf.add (' ');
      buf.add (d_attr);
      //add the attribute type
      buf.add (' ');
      buf.add (to_type (d_type, d_xenm));
      // add the attribute default
      buf.add (' ');
      buf.add (to_xdef (d_adef, d_xdef));
      // add the terminaison
      buf.add (">");
      buf.add (eolq);
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // write an element node to an output stream

00300   void XmlAttlist::write (Output& os) const {
    rdlock ();
    try {
      // add the node name
      os.write ("<");
      os.write (XML_ATTR_NAME);
      os.write (' ');
      os.write (d_name);
      // add the attribute name
      os.write (' ');
      os.write (d_attr);
      //add the attribute type
      os.write (' ');
      os.write (to_type (d_type, d_xenm));
      // add the attribute default
      os.write (' ');
      os.write (to_xdef (d_adef, d_xdef));
      // add the terminaison
      os.write   (">");
      os.newline ();
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // -------------------------------------------------------------------------
  // - object section                                                        -
  // -------------------------------------------------------------------------

  // the quark zone
  static const long QUARK_ZONE_LENGTH = 7;
  static QuarkZone  zone (QUARK_ZONE_LENGTH);

  // the object supported quarks
  static const long QUARK_SETTYPE = zone.intern ("set-type");
  static const long QUARK_SETFIXD = zone.intern ("set-fixed");
  static const long QUARK_SETXDEF = zone.intern ("set-default");
  static const long QUARK_SETNAME = zone.intern ("set-element-name");
  static const long QUARK_GETNAME = zone.intern ("get-element-name");
  static const long QUARK_SETATTR = zone.intern ("set-attribute-name");
  static const long QUARK_GETATTR = zone.intern ("get-attribute-name");

  // create a new object in a generic way

00346   Object* XmlAttlist::mknew (Vector* argv) {
    long argc = (argv == nilp) ? 0 : argv->length ();
    // check for 2 arguments
    if (argc == 2) {
      String name = argv->getstring (0);
      String attr = argv->getstring (1);
      return new XmlAttlist (name, attr);
    }
    throw Exception ("argument-error",
                     "too many arguments with xml attribute list constructor");
  }

  // return true if the given quark is defined

00360   bool XmlAttlist::isquark (const long quark, const bool hflg) const {
    rdlock ();
    if (zone.exists (quark) == true) {
      unlock ();
      return true;
    }
    bool result = hflg ? XmlNode::isquark (quark, hflg) : false;
    unlock ();
    return result;
  }

  // apply this object with a set of arguments and a quark

00373   Object* XmlAttlist::apply (Runnable* robj, Nameset* nset, const long quark,
                  Vector* argv) {
    // get the number of arguments
    long argc = (argv == nilp) ? 0 : argv->length ();

    // check for 0 argument
    if (argc == 0) {
      if (quark == QUARK_GETNAME) return new String (getname ());
      if (quark == QUARK_GETATTR) return new String (getattr ());
    }
    // check for 1 argument
    if (argc == 1) {
      if (quark == QUARK_SETNAME) {
      String name = argv->getstring (0);
      setname (name);
      return nilp;
      }
      if (quark == QUARK_SETATTR) {
      String attr = argv->getstring (0);
      setattr (attr);
      return nilp;
      }
      if (quark == QUARK_SETTYPE) {
      String type = argv->getstring (0);
      settype (type);
      return nilp;
      }
      if (quark == QUARK_SETXDEF) {
      String xdef = argv->getstring (0);
      setxdef (xdef);
      return nilp;
      }
      if (quark == QUARK_SETFIXD) {
      String xdef = argv->getstring (0);
      setfixd (xdef);
      return nilp;
      }
    }
    // check for 2 arguments
    if (argc == 2) {
      if (quark == QUARK_SETTYPE) {
        Object*  obj = argv->get (0);
        Vector* vobj = dynamic_cast <Vector*> (obj);
        if (vobj == nilp) {
          throw Exception ("type-error", "invalid vector object",
                           Object::repr (obj));
        }
      bool xflg = argv->getbool (1);
      settype (vobj, xflg);
        return nilp;
      }
    }
    // call the xml node method
    return XmlNode::apply (robj, nset, quark, argv);
  }
}

Generated by  Doxygen 1.6.0   Back to index