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