home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / classes / com / ms / xml / parser / Entity.java < prev    next >
Encoding:
Java Source  |  1997-11-03  |  6.0 KB  |  263 lines

  1. /*
  2.  * @(#)Entity.java 1.0 6/3/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7.  
  8. package com.ms.xml.parser;
  9.  
  10. import com.ms.xml.util.Name;
  11. import com.ms.xml.util.Atom;
  12. import com.ms.xml.util.XMLOutputStream;
  13. import com.ms.xml.om.Element;
  14. import com.ms.xml.om.ElementImpl;
  15. import com.ms.xml.util.EnumWrapper;
  16. import com.ms.xml.util.StringInputStream;
  17.  
  18. import java.io.*;
  19. import java.util.Enumeration;
  20. import java.util.Vector;
  21.  
  22. /**
  23.  * This class implements an <code>Entity</code> object representing an XML internal 
  24.  * or external entity as defined in the XML Document Type Definition (DTD).
  25.  *
  26.  * @version 1.0, 6/3/97
  27.  */
  28. public class Entity extends ElementImpl
  29. {
  30.     static String nameENTITY = "ENTITY";
  31.     static Atom nameXML = Atom.create("XML");
  32.     static Atom nameSpaceID = Atom.create("//XML/NAMESPACE");
  33.  
  34.     Entity(Name name, boolean par)
  35.     {
  36.         super(Name.create(nameENTITY, nameXML), Element.ENTITY);
  37.         this.name = name;
  38.         this.par = par;
  39.         this.parsed = false;
  40.     }
  41.  
  42.     Entity(Name name, boolean par, String text)
  43.     {
  44.         this(name, par);
  45.         setText(text);
  46.         setPosition(0, 0);
  47.     }
  48.     
  49.     Entity(Name name, boolean par, int c)
  50.     {
  51.         this(name, par);
  52.         cdata = (char)c;
  53.         super.setText(String.valueOf(cdata));
  54.         setPosition(0, 0);
  55.     }
  56.     
  57.     EntityReader getReader(EntityReader prev)
  58.     {
  59.         return new EntityReader(
  60.                 new StringInputStream(text),
  61.                 line, column, prev, this);
  62.     }
  63.  
  64.     void setURL(String url)
  65.     {
  66.         this.url = url;
  67.         sys = true;
  68.     }
  69.    
  70.     String getURL()
  71.     {
  72.         return url;
  73.     }
  74.  
  75.     void setNDATA(Name name)
  76.     {
  77.         ndata = name;
  78.     }
  79.  
  80.     /**
  81.     * Changes the text of entity.
  82.     * @param text  The new text of the entity.
  83.     */
  84.     public void setText(String text)
  85.     {
  86.         this.text = text;
  87.         sys = false;
  88.     }
  89.  
  90.     void setPosition(int line, int column)
  91.     {
  92.         this.line = line;
  93.         this.column = column;
  94.     }
  95.  
  96.     int getLength()
  97.     {
  98.         if (cdata > 0)
  99.             return -1;
  100.         else if (text == null)
  101.             return 0;
  102.         else return text.length();
  103.     }
  104.  
  105.     char getChar(int index)
  106.     {
  107.         if (text == null)
  108.             return cdata;
  109.         else
  110.             return text.charAt(index);
  111.     }
  112.  
  113.     public Object getAttribute(Name attName)
  114.     {
  115.         // lazy construct the ElementImpl set of attributes.
  116.         getAttributes();
  117.         return super.getAttribute(attName);
  118.     }
  119.  
  120.     public Enumeration getAttributes()
  121.     {
  122.         // lazy construct the ElementImpl set of attributes.
  123.         if (super.getAttribute(nameNAME) == null) {
  124.              setAttribute(nameNAME,name);
  125.              if (pubid != null) setAttribute(namePUBLICID, pubid);
  126.              if (url != null) setAttribute(nameSYSTEMID, url);
  127.              if (ndata != null) setAttribute(nameNOTATION, ndata);
  128.              // setAttribute(namePAR, (par == true) ? "TRUE" : "FALSE");
  129.         }
  130.         return super.getAttributes();
  131.     }
  132.  
  133.     /**
  134.     * Saves the entity to the given output stream with indenting and new lines.
  135.     * @param o  The output stream.
  136.     * @exception IOException if there is a problem writing to the output stream. 
  137.     */
  138.     public void save(XMLOutputStream o) throws IOException
  139.     {
  140.         if (o.savingDTD) {
  141.             o.writeIndent();
  142.             saveEntity(o);
  143.             o.writeNewLine();
  144.         } else {
  145.             super.save(o);
  146.         }
  147.     }
  148.  
  149.     /**
  150.     * Saves the entity to the given output stream.
  151.     * @param o  The output stream.
  152.     * @exception  IOException if there is a problem writing to the output 
  153.       * stream. 
  154.     */
  155.     public void saveEntity(XMLOutputStream o) throws IOException
  156.     {
  157.         o.writeChars("<!ENTITY ");
  158.         if (par) o.writeChars("% ");
  159.         o.writeQualifiedName(name, null);
  160.         o.writeChars(" ");
  161.  
  162.         if (url != null) {
  163.             if (pubid == null)
  164.                 o.writeChars("SYSTEM ");
  165.             else 
  166.             {
  167.                 o.writeChars("PUBLIC ");
  168.                 o.writeQuotedString(pubid);
  169.                 o.write(' ');
  170.             }
  171.             o.writeQuotedString(url.toString());
  172.             if (ndata != null) 
  173.             {
  174.                 o.writeChars(" NDATA ");
  175.                 o.writeQualifiedName(ndata, name.getNameSpace());
  176.             }
  177.         }
  178.         else if (text != null) o.writeQuotedString(text);
  179.  
  180.         o.write('>');
  181.     }
  182.  
  183.    /**
  184.     * Retrieves the name of the entity.
  185.     * @return the <code>Name</code> object containing the entity name.
  186.     */
  187.     public Name getName()
  188.     {
  189.         return name;
  190.     }
  191.  
  192.     public Name getTagName()
  193.     {
  194.         if( sys )
  195.             return nameEXTENTITYDCL;
  196.         else
  197.             return nameINTENTITYDCL;
  198.     }
  199.  
  200.     /**
  201.      * Name of entity
  202.      */
  203.     Name name;
  204.  
  205.     /**
  206.      * Url for external entity
  207.      */
  208.     String url;
  209.  
  210.     /**
  211.      * Pubid for external entity
  212.      */
  213.     String pubid;
  214.  
  215.     /**
  216.      * Text for internal entity
  217.      */
  218.     String text;
  219.  
  220.     /**
  221.      * Char for internal entity
  222.      */
  223.     char cdata;
  224.  
  225.     /**
  226.      * NDATA identifier
  227.      */    
  228.     Name ndata;
  229.  
  230.     /**
  231.      * line number
  232.      */    
  233.     int line;
  234.     
  235.     /**
  236.      * character pos
  237.      */    
  238.     int column;
  239.     
  240.     /**
  241.      * set if paramater entity
  242.      */    
  243.     boolean par;
  244.     
  245.     /**
  246.      * set if external entity
  247.      */    
  248.     boolean sys;
  249.  
  250.     /**
  251.      * whether this entity has been used (i.e. it has been
  252.      * included in document some place, like &foo;).
  253.      */
  254.     boolean parsed;
  255.  
  256.     static Name nameNAME         = Name.create("NAME","XML");
  257.     static Name namePUBLICID     = Name.create("PUBLICID","XML");
  258.     static Name nameSYSTEMID     = Name.create("SYSTEMID","XML");
  259.       static Name nameINTENTITYDCL = Name.create("INTENTITYDCL","XML");
  260.     static Name nameEXTENTITYDCL = Name.create("EXTENTITYDCL","XML");
  261.     static Name nameNOTATION     = Name.create("NOTATION","XML");
  262.     static Name namePAR          = Name.create("PAR","XML");
  263. }