001: /*
002: * Copyright (c) 1998-2007 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 Emil Ong
027: */
028:
029: package com.caucho.xml.schema;
030:
031: import java.io.*;
032: import java.util.*;
033: import static javax.xml.XMLConstants.*;
034: import javax.xml.bind.annotation.*;
035: import javax.xml.namespace.QName;
036:
037: import com.caucho.java.JavaWriter;
038:
039: import com.caucho.jaxb.JAXBUtil;
040:
041: /**
042: * JAXB annotated Schema data structure.
043: */
044: @XmlAccessorType(XmlAccessType.FIELD)
045: @XmlType(name="element",namespace=W3C_XML_SCHEMA_NS_URI)
046: public class Element {
047: @XmlAttribute(name="name")
048: private String _name;
049:
050: @XmlAttribute(name="type")
051: private QName _type;
052:
053: @XmlAttribute(name="minOccurs")
054: private Integer _minOccurs;
055:
056: @XmlAttribute(name="maxOccurs")
057: private String _maxOccurs;
058:
059: @XmlTransient
060: private String _className;
061:
062: @XmlTransient
063: private Schema _schema;
064:
065: @XmlTransient
066: public Schema getSchema() {
067: return _schema;
068: }
069:
070: public void setSchema(Schema schema) {
071: _schema = schema;
072: }
073:
074: public String getName() {
075: return _name;
076: }
077:
078: public QName getType() {
079: return _type;
080: }
081:
082: public Integer getMinOccurs() {
083: return _minOccurs;
084: }
085:
086: public String getMaxOccurs() {
087: return _maxOccurs;
088: }
089:
090: public void generateJavaField(JavaWriter out) throws IOException {
091: out.println("@XmlTransient");
092: out.print("public ");
093: out.print(getClassname());
094: out.print(" _");
095: out.print(getName());
096: out.println(";");
097: out.println();
098:
099: out.println("@XmlElement(name=\"" + getName() + "\")");
100: out.print("public ");
101: out.print(getClassname());
102:
103: if ("Boolean".equals(getClassname())
104: || "boolean".equals(getClassname()))
105: out.print(" is");
106: else
107: out.print(" get");
108:
109: out.print(JAXBUtil.xmlNameToClassName(getName()));
110: out.println("()");
111: out.println("{");
112: out.pushDepth();
113: out.println("return _" + getName() + ";");
114: out.popDepth();
115: out.println("}");
116: out.println();
117:
118: out.print("public void set");
119: out.print(JAXBUtil.xmlNameToClassName(getName()));
120: out.print("(");
121: out.print(getClassname());
122: out.print(" ");
123: out.print(getName());
124: out.println(")");
125: out.println("{");
126: out.pushDepth();
127: out.println("_" + getName() + " = " + getName() + ";");
128: out.popDepth();
129: out.println("}");
130: out.println();
131: }
132:
133: public String getClassname() {
134: if (_className == null) {
135: Class cl = JAXBUtil.getClassForDatatype(getType());
136: String name = null;
137:
138: if (cl != null) {
139: name = cl.getName();
140: } else if (_schema != null) {
141: Type type = _schema.getType(getType());
142:
143: if (type != null)
144: name = type.getClassname();
145: } else {
146: return null;
147: }
148:
149: if ("unbounded".equals(getMaxOccurs())) {
150: if (cl != null && cl.isPrimitive())
151: name = JAXBUtil.primitiveToWrapperName(cl);
152:
153: _className = "List<" + name + ">";
154: } else if (getMinOccurs() != null && getMinOccurs() == 0
155: && cl != null && cl.isPrimitive()) {
156: _className = JAXBUtil.primitiveToWrapperName(cl);
157: } else if (cl != null && cl.isArray()) {
158: if (cl.getComponentType().equals(Byte.class))
159: _className = "byte[]";
160: else
161: _className = cl.getComponentType().getName() + "[]";
162: } else
163: _className = name;
164: }
165:
166: return _className;
167: }
168: }
|