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.xml2;
030:
031: import org.w3c.dom.Node;
032:
033: import javax.xml.namespace.QName;
034: import java.io.IOException;
035: import java.util.ArrayList;
036:
037: class QElementDef extends QNode {
038: String _name;
039: Object _content;
040: ArrayList<QAttributeDef> _attr;
041: boolean _hasDefault;
042: QDocumentType _dtd;
043:
044: QElementDef(String name) {
045: _name = name;
046: }
047:
048: public String getNodeName() {
049: return "#element";
050: }
051:
052: public String getTagName() {
053: return "#element";
054: }
055:
056: public short getNodeType() {
057: return Node.ELEMENT_NODE;
058: }
059:
060: Node importNode(QDocument owner, boolean deep) {
061: QElementDef def = new QElementDef(_name);
062:
063: return def;
064: }
065:
066: public void addAttribute(String name, String type,
067: ArrayList enumeration, String qualifier, String deflt) {
068: if (_attr == null)
069: _attr = new ArrayList<QAttributeDef>();
070:
071: if (deflt != null) {
072: _hasDefault = true;
073: _dtd.setAttributeDefaults();
074: }
075:
076: _attr.add(new QAttributeDef(name, type, enumeration, qualifier,
077: deflt));
078: }
079:
080: void fillDefaults(QElement element) {
081: if (!_hasDefault)
082: return;
083:
084: for (int i = 0; i < _attr.size(); i++) {
085: QAttributeDef attrDef = _attr.get(i);
086: if (attrDef._deflt != null
087: && element.getAttribute(attrDef._name).equals("")) {
088: QAttr attr = (QAttr) element._owner.createAttribute(
089: attrDef._name, attrDef._deflt);
090: attr._owner = element._owner;
091: attr.setSpecified(false);
092: element.setAttributeNode(attr);
093: }
094: }
095: }
096:
097: void fillDefaults(QAttributes attributes) {
098: if (!_hasDefault)
099: return;
100:
101: for (int i = 0; i < _attr.size(); i++) {
102: QAttributeDef attrDef = _attr.get(i);
103: if (attrDef._deflt != null
104: && attributes.getIndex(attrDef._name) < 0) {
105: attributes.add(new QName(null, attrDef._name, null),
106: attrDef._deflt);
107: }
108: }
109: }
110:
111: public void print(XmlPrinter os) throws IOException {
112: if (_content != null) {
113: os.print("<!ELEMENT ");
114: os.print(_name);
115: os.print(" ");
116: if (_content instanceof QContentParticle)
117: ((QContentParticle) _content).print(os);
118: else
119: os.print(String.valueOf(_content));
120: os.println(">");
121: }
122:
123: if (_attr != null) {
124: os.print("<!ATTLIST ");
125: os.print(_name);
126:
127: for (int i = 0; i < _attr.size(); i++) {
128: QAttributeDef attribute = _attr.get(i);
129:
130: if (_attr.size() == 1)
131: os.print(" ");
132: else
133: os.print("\n ");
134: os.print(attribute._name);
135: if (attribute._type.equals("#ENUM")) {
136: os.print(" (");
137: for (int j = 0; j < attribute._enumeration.size(); j++) {
138: String enumType = attribute._enumeration.get(j);
139:
140: if (j != 0)
141: os.print(" | ");
142: os.print(enumType);
143: }
144: os.print(")");
145: } else if (attribute._type.equals("NOTATION")) {
146: os.print(" NOTATION (");
147: for (int j = 0; j < attribute._enumeration.size(); j++) {
148: String enumType = attribute._enumeration.get(j);
149:
150: if (j != 0)
151: os.print(" | ");
152: os.print(enumType);
153: }
154: os.print(")");
155: } else {
156: os.print(" ");
157: os.print(attribute._type);
158: }
159:
160: if (attribute._qualifier != null) {
161: os.print(" ");
162: os.print(attribute._qualifier);
163: }
164: if (attribute._deflt != null) {
165: os.print(" \"");
166: os.print(attribute._deflt);
167: os.print("\"");
168: }
169: }
170: os.println(">");
171: }
172: }
173: }
|