1 // This file is part of TagSoup and is Copyright 2002-2008 by John Cowan. 2 // 3 // TagSoup is licensed under the Apache License, 4 // Version 2.0. You may obtain a copy of this license at 5 // http://www.apache.org/licenses/LICENSE-2.0 . You may also have 6 // additional legal rights not granted by this license. 7 // 8 // TagSoup is distributed in the hope that it will be useful, but 9 // unless required by applicable law or agreed to in writing, TagSoup 10 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 11 // OF ANY KIND, either express or implied; not even the implied warranty 12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 // 14 // 15 // Model of document 16 17 package org.ccil.cowan.tagsoup; 18 import java.util.HashMap; 19 20 /** 21 Abstract class representing a TSSL schema. 22 Actual TSSL schemas are compiled into concrete subclasses of this class. 23 **/ 24 25 public abstract class Schema { 26 27 public static final int M_ANY = 0xFFFFFFFF; 28 public static final int M_EMPTY = 0; 29 public static final int M_PCDATA = 1 << 30; 30 public static final int M_ROOT = 1 << 31; 31 32 33 public static final int F_RESTART = 1; 34 public static final int F_CDATA = 2; 35 public static final int F_NOFORCE = 4; 36 37 private HashMap theEntities = 38 new HashMap(); // String -> Character 39 private HashMap theElementTypes = 40 new HashMap(); // String -> ElementType 41 42 private String theURI = ""; 43 private String thePrefix = ""; 44 private ElementType theRoot = null; 45 46 /** 47 Add or replace an element type for this schema. 48 @param name Name (Qname) of the element 49 @param model Models of the element's content as a vector of bits 50 @param memberOf Models the element is a member of as a vector of bits 51 @param flags Flags for the element 52 **/ 53 54 public void elementType(String name, int model, int memberOf, int flags) { 55 ElementType e = new ElementType(name, model, memberOf, flags, this); 56 theElementTypes.put(name.toLowerCase(), e); 57 if (memberOf == M_ROOT) theRoot = e; 58 } 59 60 /** 61 Get the root element of this schema 62 **/ 63 64 public ElementType rootElementType() { 65 return theRoot; 66 } 67 68 /** 69 Add or replace a default attribute for an element type in this schema. 70 @param elemName Name (Qname) of the element type 71 @param attrName Name (Qname) of the attribute 72 @param type Type of the attribute 73 @param value Default value of the attribute; null if no default 74 **/ 75 76 public void attribute(String elemName, String attrName, 77 String type, String value) { 78 ElementType e = getElementType(elemName); 79 if (e == null) { 80 throw new Error("Attribute " + attrName + 81 " specified for unknown element type " + 82 elemName); 83 } 84 e.setAttribute(attrName, type, value); 85 } 86 87 /** 88 Specify natural parent of an element in this schema. 89 @param name Name of the child element 90 @param parentName Name of the parent element 91 **/ 92 93 public void parent(String name, String parentName) { 94 ElementType child = getElementType(name); 95 ElementType parent = getElementType(parentName); 96 if (child == null) { 97 throw new Error("No child " + name + " for parent " + parentName); 98 } 99 if (parent == null) { 100 throw new Error("No parent " + parentName + " for child " + name); 101 } 102 child.setParent(parent); 103 } 104 105 /** 106 Add to or replace a character entity in this schema. 107 @param name Name of the entity 108 @param value Value of the entity 109 **/ 110 111 public void entity(String name, int value) { 112 theEntities.put(name, new Integer(value)); 113 } 114 115 /** 116 Get an ElementType by name. 117 @param name Name (Qname) of the element type 118 @return The corresponding ElementType 119 **/ 120 121 public ElementType getElementType(String name) { 122 return (ElementType)(theElementTypes.get(name.toLowerCase())); 123 } 124 125 /** 126 Get an entity value by name. 127 @param name Name of the entity 128 @return The corresponding character, or 0 if none 129 **/ 130 131 public int getEntity(String name) { 132 // System.err.println("%% Looking up entity " + name); 133 Integer ch = (Integer)theEntities.get(name); 134 if (ch == null) return 0; 135 return ch.intValue(); 136 } 137 138 /** 139 Return the URI (namespace name) of this schema. 140 **/ 141 142 public String getURI() { 143 return theURI; 144 } 145 146 /** 147 Return the prefix of this schema. 148 **/ 149 150 public String getPrefix() { 151 return thePrefix; 152 } 153 154 /** 155 Change the URI (namespace name) of this schema. 156 **/ 157 158 public void setURI(String uri) { 159 theURI = uri; 160 } 161 162 /** 163 Change the prefix of this schema. 164 **/ 165 166 public void setPrefix(String prefix) { 167 thePrefix = prefix; 168 } 169 170 } 171