001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.txw2;
026:
027: import javax.xml.namespace.QName;
028:
029: /**
030: * Pluggable datatype writer.
031: *
032: * @author Kohsuke Kawaguchi
033: */
034: public interface DatatypeWriter<DT> {
035:
036: /**
037: * Gets the Java class that this writer can write.
038: *
039: * @return
040: * must not be null. Must be the same value always.
041: */
042: Class<DT> getType();
043:
044: /**
045: * Prints the given datatype object and appends that result
046: * into the given buffer.
047: *
048: * @param dt
049: * the datatype object to be printed.
050: * @param resolver
051: * allows the converter to declare additional namespace prefixes.
052: */
053: void print(DT dt, NamespaceResolver resolver, StringBuilder buf);
054:
055: static final DatatypeWriter<?>[] BUILDIN = new DatatypeWriter<?>[] {
056: new DatatypeWriter<String>() {
057: public Class<String> getType() {
058: return String.class;
059: }
060:
061: public void print(String s, NamespaceResolver resolver,
062: StringBuilder buf) {
063: buf.append(s);
064: }
065: }, new DatatypeWriter<Integer>() {
066: public Class<Integer> getType() {
067: return Integer.class;
068: }
069:
070: public void print(Integer i,
071: NamespaceResolver resolver, StringBuilder buf) {
072: buf.append(i);
073: }
074: }, new DatatypeWriter<Float>() {
075: public Class<Float> getType() {
076: return Float.class;
077: }
078:
079: public void print(Float f, NamespaceResolver resolver,
080: StringBuilder buf) {
081: buf.append(f);
082: }
083: }, new DatatypeWriter<Double>() {
084: public Class<Double> getType() {
085: return Double.class;
086: }
087:
088: public void print(Double d, NamespaceResolver resolver,
089: StringBuilder buf) {
090: buf.append(d);
091: }
092: }, new DatatypeWriter<QName>() {
093: public Class<QName> getType() {
094: return QName.class;
095: }
096:
097: public void print(QName qn, NamespaceResolver resolver,
098: StringBuilder buf) {
099: String p = resolver.getPrefix(qn.getNamespaceURI());
100: if (p.length() != 0)
101: buf.append(p).append(':');
102: buf.append(qn.getLocalPart());
103: }
104: } };
105: }
|