001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.xml.txw2;
022:
023: import javax.xml.namespace.QName;
024:
025: /**
026: * Pluggable datatype writer.
027: *
028: * @author Kohsuke Kawaguchi
029: */
030: public interface DatatypeWriter<DT> {
031:
032: /**
033: * Gets the Java class that this writer can write.
034: *
035: * @return
036: * must not be null. Must be the same value always.
037: */
038: Class<DT> getType();
039:
040: /**
041: * Prints the given datatype object and appends that result
042: * into the given buffer.
043: *
044: * @param dt
045: * the datatype object to be printed.
046: * @param resolver
047: * allows the converter to declare additional namespace prefixes.
048: */
049: void print(DT dt, NamespaceResolver resolver, StringBuilder buf);
050:
051: static final DatatypeWriter<?>[] BUILDIN = new DatatypeWriter<?>[] {
052: new DatatypeWriter<String>() {
053: public Class<String> getType() {
054: return String.class;
055: }
056:
057: public void print(String s, NamespaceResolver resolver,
058: StringBuilder buf) {
059: buf.append(s);
060: }
061: }, new DatatypeWriter<Integer>() {
062: public Class<Integer> getType() {
063: return Integer.class;
064: }
065:
066: public void print(Integer i,
067: NamespaceResolver resolver, StringBuilder buf) {
068: buf.append(i);
069: }
070: }, new DatatypeWriter<Float>() {
071: public Class<Float> getType() {
072: return Float.class;
073: }
074:
075: public void print(Float f, NamespaceResolver resolver,
076: StringBuilder buf) {
077: buf.append(f);
078: }
079: }, new DatatypeWriter<Double>() {
080: public Class<Double> getType() {
081: return Double.class;
082: }
083:
084: public void print(Double d, NamespaceResolver resolver,
085: StringBuilder buf) {
086: buf.append(d);
087: }
088: }, new DatatypeWriter<QName>() {
089: public Class<QName> getType() {
090: return QName.class;
091: }
092:
093: public void print(QName qn, NamespaceResolver resolver,
094: StringBuilder buf) {
095: String p = resolver.getPrefix(qn.getNamespaceURI());
096: if (p.length() != 0)
097: buf.append(p).append(':');
098: buf.append(qn.getLocalPart());
099: }
100: } };
101: }
|