src/share/classes/com/sun/xml/internal/txw2/DatatypeWriter.java

Mon, 04 May 2009 21:10:41 -0700

author
tbell
date
Mon, 04 May 2009 21:10:41 -0700
changeset 50
42dfec6871f6
parent 45
31822b475baa
permissions
-rw-r--r--

6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy

duke@1 1 /*
tbell@45 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
tbell@45 25
duke@1 26 package com.sun.xml.internal.txw2;
duke@1 27
tbell@50 28 import java.util.AbstractList;
tbell@50 29 import java.util.Collections;
tbell@50 30 import java.util.List;
duke@1 31 import javax.xml.namespace.QName;
duke@1 32
duke@1 33 /**
duke@1 34 * Pluggable datatype writer.
duke@1 35 *
duke@1 36 * @author Kohsuke Kawaguchi
duke@1 37 */
duke@1 38 public interface DatatypeWriter<DT> {
duke@1 39
duke@1 40 /**
duke@1 41 * Gets the Java class that this writer can write.
duke@1 42 *
duke@1 43 * @return
duke@1 44 * must not be null. Must be the same value always.
duke@1 45 */
duke@1 46 Class<DT> getType();
duke@1 47
duke@1 48 /**
duke@1 49 * Prints the given datatype object and appends that result
duke@1 50 * into the given buffer.
duke@1 51 *
duke@1 52 * @param dt
duke@1 53 * the datatype object to be printed.
duke@1 54 * @param resolver
duke@1 55 * allows the converter to declare additional namespace prefixes.
duke@1 56 */
duke@1 57 void print(DT dt, NamespaceResolver resolver, StringBuilder buf);
duke@1 58
tbell@50 59 static final List<DatatypeWriter<?>> BUILTIN = Collections.unmodifiableList(new AbstractList() {
duke@1 60
tbell@50 61 private DatatypeWriter<?>[] BUILTIN_ARRAY = new DatatypeWriter<?>[] {
tbell@50 62 new DatatypeWriter<String>() {
tbell@50 63 public Class<String> getType() {
tbell@50 64 return String.class;
tbell@50 65 }
tbell@50 66 public void print(String s, NamespaceResolver resolver, StringBuilder buf) {
tbell@50 67 buf.append(s);
tbell@50 68 }
tbell@50 69 },
tbell@50 70 new DatatypeWriter<Integer>() {
tbell@50 71 public Class<Integer> getType() {
tbell@50 72 return Integer.class;
tbell@50 73 }
tbell@50 74 public void print(Integer i, NamespaceResolver resolver, StringBuilder buf) {
tbell@50 75 buf.append(i);
tbell@50 76 }
tbell@50 77 },
tbell@50 78 new DatatypeWriter<Float>() {
tbell@50 79 public Class<Float> getType() {
tbell@50 80 return Float.class;
tbell@50 81 }
tbell@50 82 public void print(Float f, NamespaceResolver resolver, StringBuilder buf) {
tbell@50 83 buf.append(f);
tbell@50 84 }
tbell@50 85 },
tbell@50 86 new DatatypeWriter<Double>() {
tbell@50 87 public Class<Double> getType() {
tbell@50 88 return Double.class;
tbell@50 89 }
tbell@50 90 public void print(Double d, NamespaceResolver resolver, StringBuilder buf) {
tbell@50 91 buf.append(d);
tbell@50 92 }
tbell@50 93 },
tbell@50 94 new DatatypeWriter<QName>() {
tbell@50 95 public Class<QName> getType() {
tbell@50 96 return QName.class;
tbell@50 97 }
tbell@50 98 public void print(QName qn, NamespaceResolver resolver, StringBuilder buf) {
tbell@50 99 String p = resolver.getPrefix(qn.getNamespaceURI());
tbell@50 100 if(p.length()!=0)
tbell@50 101 buf.append(p).append(':');
tbell@50 102 buf.append(qn.getLocalPart());
tbell@50 103 }
duke@1 104 }
tbell@50 105 };
tbell@50 106
tbell@50 107 public DatatypeWriter<?> get(int n) {
tbell@50 108 return BUILTIN_ARRAY[n];
duke@1 109 }
tbell@50 110
tbell@50 111 public int size() {
tbell@50 112 return BUILTIN_ARRAY.length;
tbell@50 113 }
tbell@50 114 });
duke@1 115 }

mercurial