home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / pc / JAVA / MSJAVX86.EXE / xmldso.cab / com / ms / xml / parser / AttDef.class (.txt) next >
Encoding:
Java Class File  |  1998-11-05  |  7.4 KB  |  444 lines

  1. package com.ms.xml.parser;
  2.  
  3. import com.ms.xml.om.Element;
  4. import com.ms.xml.om.ElementImpl;
  5. import com.ms.xml.util.Atom;
  6. import com.ms.xml.util.Name;
  7. import com.ms.xml.util.XMLOutputStream;
  8. import java.io.IOException;
  9. import java.util.Enumeration;
  10. import java.util.Vector;
  11.  
  12. public class AttDef {
  13.    public static final int CDATA = 0;
  14.    // $FF: renamed from: ID int
  15.    public static final int field_0 = 1;
  16.    public static final int IDREF = 2;
  17.    public static final int IDREFS = 3;
  18.    public static final int ENTITY = 4;
  19.    public static final int ENTITIES = 5;
  20.    public static final int NMTOKEN = 6;
  21.    public static final int NMTOKENS = 7;
  22.    public static final int NOTATION = 8;
  23.    public static final int ENUMERATION = 9;
  24.    public static final int DEFAULT = 0;
  25.    public static final int REQUIRED = 1;
  26.    public static final int IMPLIED = 2;
  27.    public static final int FIXED = 3;
  28.    Name name;
  29.    byte type;
  30.    byte presence;
  31.    Object def;
  32.    Vector values;
  33.    static Name nameATTRIBUTE = Name.create("ATTRIBUTE", "XML");
  34.    static Name nameID = Name.create("ID", "XML");
  35.    static Name nameDEFAULT = Name.create("DEFAULT", "XML");
  36.    static Name namePRESENCE = Name.create("PRESENCE", "XML");
  37.    static Name nameVALUES = Name.create("VALUES", "XML");
  38.    static Name nameTYPE = Name.create("TYPE", "XML");
  39.  
  40.    private void reportEmpty(Parser var1, String var2) throws ParseException {
  41.       var1.error("Expected " + var1.tokenString(-4, var2) + " insteadof " + var1.tokenString(var1.token, (String)null));
  42.    }
  43.  
  44.    final Object parseAttValue(Element var1, Parser var2, boolean var3) throws ParseException {
  45.       Object var6 = null;
  46.       Object var7;
  47.       switch (this.type) {
  48.          case 0:
  49.             var2.scanString(var2.quote, 38, 38, 60);
  50.             var7 = var2.text;
  51.             if (!var3 && this.presence == 3 && !this.def.toString().equals(var2.text)) {
  52.                this.reportMismatch(var2);
  53.             }
  54.             break;
  55.          case 1:
  56.             ++var2.simplename;
  57.             var2.parseToken(-4, "ID");
  58.             var2.simplename += -1;
  59.             var2.parseToken(39, "string");
  60.             if (!var3) {
  61.                if (this.presence == 3 && (Name)this.def != var2.name) {
  62.                   this.reportMismatch(var2);
  63.                }
  64.  
  65.                Element var9 = var2.dtd.findID(var2.name);
  66.                if (var9 != null) {
  67.                   var2.error("ID " + var6 + " is already used on element " + var9.getTagName());
  68.                }
  69.  
  70.                var2.dtd.addID(var2.name, var1);
  71.             }
  72.  
  73.             var7 = var2.name;
  74.             break;
  75.          case 2:
  76.          case 3:
  77.             Vector var17 = new Vector();
  78.             StringBuffer var21 = new StringBuffer();
  79.             ++var2.simplename;
  80.             int var14 = var2.parseNames(var17, 0, var21);
  81.             var2.simplename += -1;
  82.             if (var14 == 0) {
  83.                this.reportEmpty(var2, "IDREF");
  84.             }
  85.  
  86.             if (var14 > 1 && this.type == 2) {
  87.                var2.error("IDREF type attribute \"" + this.name + "\" cannot refer to more than one ID.");
  88.             }
  89.  
  90.             if (!var3) {
  91.                --var14;
  92.  
  93.                for(; var14 >= 0; --var14) {
  94.                   Name var19 = (Name)var17.elementAt(var14);
  95.                   if (this.presence == 3) {
  96.                      if (this.type == 3) {
  97.                         this.checkFixed(var19, var2);
  98.                      } else if ((Name)this.def != var19) {
  99.                         this.reportMismatch(var2);
  100.                      }
  101.                   }
  102.  
  103.                   if (var2.dtd.findID(var19) == null) {
  104.                      var2.dtd.addIDCheck(var19, var2.reader.line, var2.reader.column - 1, var2.reader.owner);
  105.                   }
  106.                }
  107.             }
  108.  
  109.             var7 = this.type == 2 ? var17.elementAt(0) : var17;
  110.             break;
  111.          case 4:
  112.          case 5:
  113.             StringBuffer var20 = new StringBuffer();
  114.             Vector var16 = new Vector();
  115.             ++var2.nouppercase;
  116.             int var12 = var2.parseNames(var16, 0, var20);
  117.             var2.nouppercase += -1;
  118.             if (var12 == 0) {
  119.                this.reportEmpty(var2, "ENTITY name");
  120.             }
  121.  
  122.             if (var12 > 1 && this.type == 4) {
  123.                var2.error("ENTITY type attribute \"" + this.name + "\" cannot refer to more than one entity.");
  124.             }
  125.  
  126.             --var12;
  127.  
  128.             for(; var12 >= 0; --var12) {
  129.                Name var18 = (Name)var16.elementAt(var12);
  130.                if (var2.dtd.findEntity(var18) == null) {
  131.                   var2.error("Couldn't find entity '" + var18 + "'");
  132.                }
  133.  
  134.                if (!var3 && this.presence == 3) {
  135.                   if (this.type == 5) {
  136.                      this.checkFixed(var18, var2);
  137.                   } else if (var18 != (Name)this.def) {
  138.                      this.reportMismatch(var2);
  139.                   }
  140.                }
  141.             }
  142.  
  143.             var7 = this.type == 4 ? var16.elementAt(0) : var16;
  144.             break;
  145.          case 6:
  146.          case 7:
  147.             StringBuffer var8 = new StringBuffer();
  148.             Vector var5 = new Vector();
  149.             ++var2.nametoken;
  150.             int var10 = var2.parseNames(var5, 0, var8);
  151.             var2.nametoken += -1;
  152.             if (var10 == 0) {
  153.                this.reportEmpty(var2, "NMTOKEN");
  154.             }
  155.  
  156.             if (var10 > 1 && this.type == 6) {
  157.                var2.error("NMTOKEN type attribute \"" + this.name + "\" cannot refer to more than one name token.");
  158.             }
  159.  
  160.             if (!var3 && this.presence == 3) {
  161.                if (this.type == 6) {
  162.                   if ((Name)this.def != (Name)var5.elementAt(0)) {
  163.                      this.reportMismatch(var2);
  164.                   }
  165.                } else {
  166.                   for(int var11 = var5.size() - 1; var11 >= 0; --var11) {
  167.                      this.checkFixed((Name)var5.elementAt(var11), var2);
  168.                   }
  169.                }
  170.             }
  171.  
  172.             var7 = this.type == 6 ? var5.elementAt(0) : var5;
  173.             break;
  174.          case 8:
  175.          case 9:
  176.             if (this.type == 9) {
  177.                ++var2.nametoken;
  178.             }
  179.  
  180.             var2.parseToken(-4, "name");
  181.             if (this.type == 9) {
  182.                var2.nametoken += -1;
  183.             }
  184.  
  185.             var2.parseToken(39, "string");
  186.  
  187.             int var4;
  188.             for(var4 = this.values.size() - 1; var4 >= 0 && (Name)this.values.elementAt(var4) != var2.name; --var4) {
  189.             }
  190.  
  191.             if (var4 < 0) {
  192.                var2.error("Attribute value '" + var2.name + "' is  not in the allowed set.");
  193.             }
  194.  
  195.             if (!var3 && this.presence == 3 && var2.name != this.def) {
  196.                this.reportMismatch(var2);
  197.             }
  198.  
  199.             var7 = var2.name;
  200.             break;
  201.          default:
  202.             var7 = null;
  203.       }
  204.  
  205.       return var7;
  206.    }
  207.  
  208.    AttDef(Name var1, int var2) {
  209.       this(var1, var2, (Object)null, 0, (Vector)null);
  210.    }
  211.  
  212.    AttDef(Name var1, int var2, Vector var3) {
  213.       this(var1, var2, (Object)null, 0, var3);
  214.    }
  215.  
  216.    AttDef(Name var1, int var2, Object var3, int var4) {
  217.       this(var1, var2, var3, var4, (Vector)null);
  218.    }
  219.  
  220.    AttDef(Name var1, int var2, Object var3, int var4, Vector var5) {
  221.       this.name = var1;
  222.       this.type = (byte)var2;
  223.       this.def = var3;
  224.       this.presence = (byte)var4;
  225.       this.values = var5;
  226.    }
  227.  
  228.    String typeToString() {
  229.       switch (this.type) {
  230.          case 0:
  231.          default:
  232.             return "CDATA";
  233.          case 1:
  234.             return "ID";
  235.          case 2:
  236.             return "IDREF";
  237.          case 3:
  238.             return "IDREFS";
  239.          case 4:
  240.             return "ENTITY";
  241.          case 5:
  242.             return "ENTITIES";
  243.          case 6:
  244.             return "NMTOKEN";
  245.          case 7:
  246.             return "NMTOKENS";
  247.          case 8:
  248.             return "NOTATION";
  249.          case 9:
  250.             return "ENUMERATION";
  251.       }
  252.    }
  253.  
  254.    private void reportMismatch(Parser var1) throws ParseException {
  255.       var1.error("Attribute " + this.name + " should have the fixed value \"" + this.def + "\"");
  256.    }
  257.  
  258.    private void checkFixed(Name var1, Parser var2) throws ParseException {
  259.       Vector var3 = (Vector)this.def;
  260.  
  261.       int var4;
  262.       for(var4 = var3.size() - 1; var4 >= 0 && (Name)var3.elementAt(var4) != var1; --var4) {
  263.       }
  264.  
  265.       if (var4 < 0) {
  266.          var2.error("Attribue value " + var1 + " is not in the fixed value set.");
  267.       }
  268.  
  269.    }
  270.  
  271.    Element toSchema() {
  272.       ElementImpl var1 = new ElementImpl(nameATTRIBUTE, 0);
  273.       var1.setAttribute(nameID, this.name.getName());
  274.       var1.setAttribute(nameTYPE, this.typeToString());
  275.       if (this.presence != 0) {
  276.          var1.setAttribute(namePRESENCE, this.presenceToString());
  277.       }
  278.  
  279.       if (this.type == 9) {
  280.          String var2 = "";
  281.  
  282.          for(int var3 = 0; var3 < this.values.size(); ++var3) {
  283.             Name var4 = (Name)this.values.elementAt(var3);
  284.             var2 = var2 + var4.toString();
  285.             if (var3 < this.values.size() - 1) {
  286.                var2 = var2 + " ";
  287.             }
  288.          }
  289.  
  290.          var1.setAttribute(nameVALUES, var2);
  291.       }
  292.  
  293.       if (this.def != null) {
  294.          var1.setAttribute(nameDEFAULT, this.def);
  295.       }
  296.  
  297.       return var1;
  298.    }
  299.  
  300.    void save(Atom var1, XMLOutputStream var2) throws IOException {
  301.       if (this.name == Parser.nameXMLSpace) {
  302.          var2.writeChars(this.name.getName());
  303.       } else {
  304.          var2.writeQualifiedName(this.name, var1);
  305.       }
  306.  
  307.       switch (this.type) {
  308.          case 0:
  309.             var2.writeChars(" CDATA");
  310.             break;
  311.          case 1:
  312.             var2.writeChars(" ID");
  313.             break;
  314.          case 2:
  315.             var2.writeChars(" IDREF");
  316.             break;
  317.          case 3:
  318.             var2.writeChars(" IDREFS");
  319.             break;
  320.          case 4:
  321.             var2.writeChars(" ENTITY");
  322.             break;
  323.          case 5:
  324.             var2.writeChars(" ENTITIES");
  325.             break;
  326.          case 6:
  327.             var2.writeChars(" NMTOKEN");
  328.             break;
  329.          case 7:
  330.             var2.writeChars(" NMTOKENS");
  331.             break;
  332.          case 8:
  333.             var2.writeChars(" NOTATION");
  334.          case 9:
  335.             var2.writeChars(" (");
  336.  
  337.             for(int var3 = 0; var3 < this.values.size(); ++var3) {
  338.                Name var4 = (Name)this.values.elementAt(var3);
  339.                if (this.type == 8) {
  340.                   var2.writeQualifiedName(var4, var1);
  341.                } else {
  342.                   var2.writeChars(var4.toString());
  343.                }
  344.  
  345.                if (var3 < this.values.size() - 1) {
  346.                   var2.write(124);
  347.                }
  348.             }
  349.  
  350.             var2.write(41);
  351.       }
  352.  
  353.       switch (this.presence) {
  354.          case 0:
  355.          case 3:
  356.             if (this.presence == 3) {
  357.                var2.writeChars(" #FIXED");
  358.             }
  359.  
  360.             if (this.def != null) {
  361.                var2.writeChars(" ");
  362.                switch (this.type) {
  363.                   case 0:
  364.                   case 1:
  365.                   case 2:
  366.                   case 6:
  367.                   case 9:
  368.                      var2.writeQuotedString(this.def.toString());
  369.                      return;
  370.                   case 3:
  371.                   case 5:
  372.                   case 7:
  373.                      var2.write(34);
  374.                      int var5 = 0;
  375.                      Enumeration var6 = ((Vector)this.def).elements();
  376.  
  377.                      while(var6.hasMoreElements()) {
  378.                         if (var5++ > 0) {
  379.                            var2.write(124);
  380.                         }
  381.  
  382.                         if (this.type == 5) {
  383.                            var2.writeQualifiedName((Name)var6.nextElement(), var1);
  384.                         } else {
  385.                            var2.writeChars(var6.nextElement().toString());
  386.                         }
  387.                      }
  388.  
  389.                      var2.write(34);
  390.                      return;
  391.                   case 4:
  392.                   case 8:
  393.                      var2.write(34);
  394.                      var2.writeQualifiedName((Name)this.def, var1);
  395.                      var2.write(34);
  396.                      return;
  397.                }
  398.             }
  399.          default:
  400.             return;
  401.          case 1:
  402.             var2.writeChars(" #REQUIRED");
  403.             return;
  404.          case 2:
  405.             var2.writeChars(" #IMPLIED");
  406.       }
  407.    }
  408.  
  409.    public Object getDefault() {
  410.       return this.def;
  411.    }
  412.  
  413.    public Name getName() {
  414.       return this.name;
  415.    }
  416.  
  417.    public int getType() {
  418.       return this.type;
  419.    }
  420.  
  421.    public int getPresence() {
  422.       return this.presence;
  423.    }
  424.  
  425.    final Object parseAttribute(Element var1, Parser var2) throws ParseException {
  426.       var2.parseToken(39, "string");
  427.       return this.parseAttValue(var1, var2, false);
  428.    }
  429.  
  430.    String presenceToString() {
  431.       switch (this.presence) {
  432.          case 0:
  433.          default:
  434.             return "DEFAULT";
  435.          case 1:
  436.             return "REQUIRED";
  437.          case 2:
  438.             return "IMPLIED";
  439.          case 3:
  440.             return "FIXED";
  441.       }
  442.    }
  443. }
  444.