001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml;
030:
031: import org.w3c.dom.Node;
032:
033: import java.io.IOException;
034: import java.util.ArrayList;
035:
036: class QElementDef extends QNode {
037: String _name;
038: Object _content;
039: ArrayList<QAttributeDef> _attr;
040: boolean _hasDefault;
041: QDocumentType _dtd;
042:
043: QElementDef(String name) {
044: _name = name;
045: }
046:
047: public String getNodeName() {
048: return "#element";
049: }
050:
051: public String getTagName() {
052: return "#element";
053: }
054:
055: public short getNodeType() {
056: return Node.ELEMENT_NODE;
057: }
058:
059: Node importNode(QDocument owner, boolean deep) {
060: QElementDef def = new QElementDef(_name);
061:
062: return def;
063: }
064:
065: public void addAttribute(String name, String type,
066: ArrayList enumeration, String qualifier, String deflt) {
067: if (_attr == null)
068: _attr = new ArrayList<QAttributeDef>();
069:
070: if (deflt != null) {
071: _hasDefault = true;
072: _dtd.setAttributeDefaults();
073: }
074:
075: _attr.add(new QAttributeDef(name, type, enumeration, qualifier,
076: deflt));
077: }
078:
079: void fillDefaults(QElement element) {
080: if (!_hasDefault)
081: return;
082:
083: for (int i = 0; i < _attr.size(); i++) {
084: QAttributeDef attrDef = _attr.get(i);
085: if (attrDef._deflt != null
086: && element.getAttribute(attrDef._name).equals("")) {
087: QAttr attr = (QAttr) element._owner.createAttribute(
088: attrDef._name, attrDef._deflt);
089: attr._owner = element._owner;
090: attr.setSpecified(false);
091: element.setAttributeNode(attr);
092: }
093: }
094: }
095:
096: void fillDefaults(QAttributes attributes) {
097: if (!_hasDefault)
098: return;
099:
100: for (int i = 0; i < _attr.size(); i++) {
101: QAttributeDef attrDef = _attr.get(i);
102: if (attrDef._deflt != null
103: && attributes.getIndex(attrDef._name) < 0) {
104: attributes.add(new QName(null, attrDef._name, null),
105: attrDef._deflt);
106: }
107: }
108: }
109:
110: public void print(XmlPrinter os) throws IOException {
111: if (_content != null) {
112: os.print("<!ELEMENT ");
113: os.print(_name);
114: os.print(" ");
115: if (_content instanceof QContentParticle)
116: ((QContentParticle) _content).print(os);
117: else
118: os.print(String.valueOf(_content));
119: os.println(">");
120: }
121:
122: if (_attr != null) {
123: os.print("<!ATTLIST ");
124: os.print(_name);
125:
126: for (int i = 0; i < _attr.size(); i++) {
127: QAttributeDef attribute = _attr.get(i);
128:
129: if (_attr.size() == 1)
130: os.print(" ");
131: else
132: os.print("\n ");
133: os.print(attribute._name);
134: if (attribute._type.equals("#ENUM")) {
135: os.print(" (");
136: for (int j = 0; j < attribute._enumeration.size(); j++) {
137: String enumType = attribute._enumeration.get(j);
138:
139: if (j != 0)
140: os.print(" | ");
141: os.print(enumType);
142: }
143: os.print(")");
144: } else if (attribute._type.equals("NOTATION")) {
145: os.print(" NOTATION (");
146: for (int j = 0; j < attribute._enumeration.size(); j++) {
147: String enumType = attribute._enumeration.get(j);
148:
149: if (j != 0)
150: os.print(" | ");
151: os.print(enumType);
152: }
153: os.print(")");
154: } else {
155: os.print(" ");
156: os.print(attribute._type);
157: }
158:
159: if (attribute._qualifier != null) {
160: os.print(" ");
161: os.print(attribute._qualifier);
162: }
163: if (attribute._deflt != null) {
164: os.print(" \"");
165: os.print(attribute._deflt);
166: os.print("\"");
167: }
168: }
169: os.println(">");
170: }
171: }
172: }
|