Package anvil.core.runtime

Source Code of anvil.core.runtime.AnyDoc$DocEnumeration

/*
* $Id: AnyDoc.java,v 1.18 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.runtime;

import java.io.IOException;
import java.util.Enumeration;
import anvil.java.util.BindingEnumeration;
import anvil.core.Serializer;
import anvil.core.Unserializer;
import anvil.core.UnserializationException;
import anvil.core.Any;
import anvil.core.AnyString;
import anvil.core.Array;
import anvil.core.AnyTuple;
import anvil.core.AnyList;
import anvil.core.AnyAbstractClass;
import anvil.doc.Doc;
import anvil.script.Context;
import anvil.script.Type;

/// @class Doc
/// Doc is a node in documentation tree, consisting
/// of type, identifier and text.
/// <p>Type is one of
/// <ul>
/// <li>attribute (*)
/// <li>author
/// <li>category
/// <li>class (*)
/// <li>const (*)
/// <li>constructor (*)
/// <li>default
/// <li>deprecated
/// <li>exclude
/// <li>function (*)
/// <li>interface (*)
/// <li>member (*)
/// <li>method (*)
/// <li>module (*)
/// <li>namespace (*)
/// <li>obsolete
/// <li>operator (*)
/// <li>param (*)
/// <li>reference (*)
/// <li>return
/// <li>see
/// <li>since
/// <li>synopsis
/// <li>throws (*)
/// <li>type
/// <li>var (*)
/// <li>version
/// <li>define (*)
/// </ul>
/// </p>
/// <p>If document is one marked with asterisk (*) then
/// it will also contain identifier which indicates
/// the name of documented entity.</p>
///
/// <p>Doc may also contain children docs.</p>

/**
* class AnyDoc
*
* @author: Jani Lehtim�ki
*/
public class AnyDoc extends AnyAbstractClass
{

 

  private Doc _doc;


  public AnyDoc(Doc doc)
  {
    _doc = doc;
  }
 
 
  public final anvil.script.ClassType classOf() {
    return __class__;
  }


  public Object toObject()
  {
    return _doc;
  }


  public String toString()
  {
    StringBuffer buffer = new StringBuffer(32);
    buffer.append("Doc{@");
    buffer.append(_doc.getName());
    String s = _doc.getIdent();
    if (s != null) {
      buffer.append(' ');
      buffer.append(s);
    }
    buffer.append('}');
    return buffer.toString();
  }


  public Any getAttribute(Context context, String name)
  {
    Doc doc = _doc.findFirst(name, null);
    return (doc != null) ? new AnyDoc(doc) : UNDEFINED;
  }

 
  public Any checkAttribute(Context context, String name)
  {
    return getAttribute(context, name);
  }

 
  public Any getReference(Context context, Any index)
  {
    Doc doc = _doc.getChild(index.toInt());
    return (doc != null) ? new AnyDoc(doc) : UNDEFINED;
 


  public Any checkReference(Context context, Any index)
  {
    return getReference(context, index);
 


  public BindingEnumeration enumeration()
  {
    return new DocEnumeration(_doc.getChilds());
  }
 
 
  private final void serialize(Serializer serializer, Doc doc) throws IOException
  {
    serializer.write('H');
    serializer.write(doc.getType());
    serializer.write(':');
    String ident = doc.getIdent();
    serializer.write(ident == null ? "" : ident);
    serializer.write(doc.getText());
    serializer.write(doc.getChildCount());
    serializer.write(':');
    if (doc.hasChilds()) {
      doc = doc.getFirstChild();
      while(doc != null) {
        serialize(serializer, doc);
        doc = doc.getNext();
      }
    }
  }


  public final void serialize(Serializer serializer) throws IOException
  {
    if (serializer.register(this)) {
      return;
    }
    serialize(serializer, _doc);
  }


  public static final Doc unserializeDoc(Unserializer unserializer) throws UnserializationException
  {
    int type = (int)unserializer.getLong();
    unserializer.consume('s');
    String ident = unserializer.getUTF16String();
    unserializer.consume('s');
    String text = unserializer.getUTF16String();
    Doc[] childs = null;
    int size = (int)unserializer.getLong();
    if (size > 0) {
      childs = new Doc[size];
      for (int i=0; i<size; i++) {
        unserializer.consume('H');
        childs[i] = unserializeDoc(unserializer);
      }
    }
    return new Doc(type, ident, text, childs);
  }


  public static final Any unserialize(Unserializer unserializer) throws UnserializationException
  {
    AnyDoc doc = new AnyDoc(unserializeDoc(unserializer));
    unserializer.register(doc);
    return doc;
  }


  /// @method getName
  /// Returns document's name (same getType).
  /// @synopsis string getName()
  public Any m_getName()
  {
    return Any.create(_doc.getName());
  }

  /// @method getType
  /// Returns document's type (same getName).
  /// @synopsis string getType()
  public Any m_getType()
  {
    return Any.create(_doc.getName());
  }



  /// @method getIdent
  /// Returns document's identitier.
  /// @synopsis string getIdent()
  public Any m_getIdent()
  {
    return Any.create(_doc.getIdent());
  }


  /// @method getText
  /// @synopsis string getText() ;
  /// Returns this document's text.
  /// @synopsis string getText(string type) ;
  /// Returns text of first child document matching given type.
  /// @synopsis string getText(string type, string ident) ;
  /// Returns text of first child document matching given type and identifier.
  /// @param type Type of document to search for
  /// @param ident Identifier of document to search for
  public static final Object[] p_getText = { "*type", null, "*ident", null };
  public Any m_getText(String type, String ident)
  {
    if (type != null) {
      Doc doc = _doc.findFirst(type, ident);
      return (doc != null) ? Any.create(doc.getText()) : UNDEFINED;
    } else {
      return Any.create(_doc.getText());
    }
  }


  /// @method getHead
  /// @synopsis string getHead() ;
  /// Returns first sentence from this document's text.
  /// @synopsis string getHead(string type) ;
  /// Returns first sentence from first child document that
  /// matches given type.
  /// @synopsis string getHead(string type, string ident) ;
  /// Returns first sentence from first child document that
  /// matches given type and identifier.
  /// @param type Type of document to search for
  /// @param ident Identifier of document to search for
  public static final Object[] p_getHead = { "*type", null, "*ident", null };
  public Any m_getHead(String type, String ident)
  {
    if (type != null) {
      Doc doc = _doc.findFirst(type, ident);
      return (doc != null) ? Any.create(doc.getFirstSentence()) : UNDEFINED;
    } else {
      return Any.create(_doc.getFirstSentence());
    }
  }



  /// @method hasChilds
  /// Checks if this document has children.
  /// @synopsis boolean hasChilds()
  public Any m_hasChilds()
  {
    return _doc.hasChilds() ? TRUE : FALSE;
  }


  /// @method getChild
  /// @synopsis Doc getChild() ;
  /// Returns first child from this document.
  /// @synopsis Doc getChild(string type) ;
  /// Returns first child from this document matching given type.
  /// @synopsis Doc getChild(string type, string ident) ;
  /// Returns first child from this document matching given type and identifier.
  /// @param type Type of document to search for
  /// @param ident Identifier of document to search for
  /// @return Doc or undefined, if there were no matching documents.
  public static final Object[] p_getChild = { "*type", null, "*ident", null };
  public Any m_getChild(String type, String ident)
  {
    Doc doc;
    if (type != null) {
      doc = _doc.findFirst(type, ident);
    } else {
      doc = _doc.getFirstChild();
    }
    return (doc != null) ? new AnyDoc(doc) : UNDEFINED;
  }


  /// @method getNext
  /// Returns next document (sibling).
  /// @synopsis Doc getNext()
  /// @return Doc or undefined, if there is no next sibling.
  public Any m_getNext()
  {
    Doc next = _doc.getNext();
    return (next != null) ? new AnyDoc(next) : UNDEFINED;
  }
 
 
  /// @method getChilds
  /// @synopsis list getChilds() ;
  /// Returns children of this document.
  /// @synopsis list getChilds(string type) ;
  /// Returns children of this document matching given type.
  /// @synopsis list getChilds(string type, string ident) ;
  /// Returns children of this document matching given type and identifier.
  /// @param type Type of document to search for
  /// @param ident Identifier of document to search for
  /// @return list of Doc's
  public static final Object[] p_getChilds = { "*type", null, "*ident", null };
  public Any m_getChilds(String type, String ident)
  {
    Doc[] docs;
    if (type != null) {
      docs = _doc.find(type, ident);
    } else {
      docs = _doc.getChilds();
    }
    int n = docs.length;
    Any[] list = new Any[n];
    for(int i=0; i<n; i++) {
      list[i] = new AnyDoc(docs[i]);
    }
    return new AnyList(list);
 


  private class DocEnumeration implements BindingEnumeration
  {
    private Doc[] _docs;
    private int   _index = 0;

    public DocEnumeration(Doc[] docs)
    {
      _docs = docs;
    }
   
    public boolean hasMoreElements()
    {
      return _index < _docs.length;
    }
   
    public Object nextKey()
    {
      return Any.create(_index);
    }

    public Object nextElement()
    {
      if (_index<_docs.length) {
        return new AnyDoc(_docs[_index++]);
      } else {
        return UNDEFINED;
      }
    }
  }


  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Doc", AnyDoc.class,
    //DOC{{
    ""+
      " @class Doc\n" +
      " Doc is a node in documentation tree, consisting\n" +
      " of type, identifier and text.\n" +
      " <p>Type is one of\n" +
      " <ul>\n" +
      " <li>attribute (*)\n" +
      " <li>author\n" +
      " <li>category\n" +
      " <li>class (*)\n" +
      " <li>const (*)\n" +
      " <li>constructor (*)\n" +
      " <li>default\n" +
      " <li>deprecated\n" +
      " <li>exclude\n" +
      " <li>function (*)\n" +
      " <li>interface (*)\n" +
      " <li>member (*)\n" +
      " <li>method (*)\n" +
      " <li>module (*)\n" +
      " <li>namespace (*)\n" +
      " <li>obsolete\n" +
      " <li>operator (*)\n" +
      " <li>param (*)\n" +
      " <li>reference (*)\n" +
      " <li>return\n" +
      " <li>see\n" +
      " <li>since\n" +
      " <li>synopsis\n" +
      " <li>throws (*)\n" +
      " <li>type\n" +
      " <li>var (*)\n" +
      " <li>version\n" +
      " <li>define (*)\n" +
      " </ul>\n" +
      " </p>\n" +
      " <p>If document is one marked with asterisk (*) then\n" +
      " it will also contain identifier which indicates\n" +
      " the name of documented entity.</p>\n" +
      "\n" +
      " <p>Doc may also contain children docs.</p>\n" +
      " @method getName\n" +
      " Returns document's name (same getType).\n" +
      " @synopsis string getName()\n" +
      " @method getType\n" +
      " Returns document's type (same getName).\n" +
      " @synopsis string getType()\n" +
      " @method getIdent\n" +
      " Returns document's identitier.\n" +
      " @synopsis string getIdent()\n" +
      " @method getText\n" +
      " @synopsis string getText() ;\n" +
      " Returns this document's text.\n" +
      " @synopsis string getText(string type) ;\n" +
      " Returns text of first child document matching given type.\n" +
      " @synopsis string getText(string type, string ident) ;\n" +
      " Returns text of first child document matching given type and identifier.\n" +
      " @param type Type of document to search for\n" +
      " @param ident Identifier of document to search for\n" +
      " @method getHead\n" +
      " @synopsis string getHead() ;\n" +
      " Returns first sentence from this document's text.\n" +
      " @synopsis string getHead(string type) ;\n" +
      " Returns first sentence from first child document that\n" +
      " matches given type.\n" +
      " @synopsis string getHead(string type, string ident) ;\n" +
      " Returns first sentence from first child document that\n" +
      " matches given type and identifier.\n" +
      " @param type Type of document to search for\n" +
      " @param ident Identifier of document to search for\n" +
      " @method hasChilds\n" +
      " Checks if this document has children.\n" +
      " @synopsis boolean hasChilds()\n" +
      " @method getChild\n" +
      " @synopsis Doc getChild() ;\n" +
      " Returns first child from this document.\n" +
      " @synopsis Doc getChild(string type) ;\n" +
      " Returns first child from this document matching given type.\n" +
      " @synopsis Doc getChild(string type, string ident) ;\n" +
      " Returns first child from this document matching given type and identifier.\n" +
      " @param type Type of document to search for\n" +
      " @param ident Identifier of document to search for\n" +
      " @return Doc or undefined, if there were no matching documents.\n" +
      " @method getNext\n" +
      " Returns next document (sibling).\n" +
      " @synopsis Doc getNext()\n" +
      " @return Doc or undefined, if there is no next sibling.\n" +
      " @method getChilds\n" +
      " @synopsis list getChilds() ;\n" +
      " Returns children of this document.\n" +
      " @synopsis list getChilds(string type) ;\n" +
      " Returns children of this document matching given type.\n" +
      " @synopsis list getChilds(string type, string ident) ;\n" +
      " Returns children of this document matching given type and identifier.\n" +
      " @param type Type of document to search for\n" +
      " @param ident Identifier of document to search for\n" +
      " @return list of Doc's\n"
    //}}DOC
    );
  static {
    RuntimeModule.class.getName();
 
 
}
TOP

Related Classes of anvil.core.runtime.AnyDoc$DocEnumeration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.