aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.txw2; aoqi@0: aoqi@0: import java.util.AbstractList; aoqi@0: import java.util.Collections; aoqi@0: import java.util.List; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: /** aoqi@0: * Pluggable datatype writer. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public interface DatatypeWriter
{ aoqi@0: aoqi@0: /** aoqi@0: * Gets the Java class that this writer can write. aoqi@0: * aoqi@0: * @return aoqi@0: * must not be null. Must be the same value always. aoqi@0: */ aoqi@0: Class
getType(); aoqi@0: aoqi@0: /** aoqi@0: * Prints the given datatype object and appends that result aoqi@0: * into the given buffer. aoqi@0: * aoqi@0: * @param dt aoqi@0: * the datatype object to be printed. aoqi@0: * @param resolver aoqi@0: * allows the converter to declare additional namespace prefixes. aoqi@0: */ aoqi@0: void print(DT dt, NamespaceResolver resolver, StringBuilder buf); aoqi@0: aoqi@0: static final List> BUILTIN = Collections.unmodifiableList(new AbstractList() { aoqi@0: aoqi@0: private DatatypeWriter[] BUILTIN_ARRAY = new DatatypeWriter[] { aoqi@0: new DatatypeWriter() { aoqi@0: public Class getType() { aoqi@0: return String.class; aoqi@0: } aoqi@0: public void print(String s, NamespaceResolver resolver, StringBuilder buf) { aoqi@0: buf.append(s); aoqi@0: } aoqi@0: }, aoqi@0: new DatatypeWriter() { aoqi@0: public Class getType() { aoqi@0: return Integer.class; aoqi@0: } aoqi@0: public void print(Integer i, NamespaceResolver resolver, StringBuilder buf) { aoqi@0: buf.append(i); aoqi@0: } aoqi@0: }, aoqi@0: new DatatypeWriter() { aoqi@0: public Class getType() { aoqi@0: return Float.class; aoqi@0: } aoqi@0: public void print(Float f, NamespaceResolver resolver, StringBuilder buf) { aoqi@0: buf.append(f); aoqi@0: } aoqi@0: }, aoqi@0: new DatatypeWriter() { aoqi@0: public Class getType() { aoqi@0: return Double.class; aoqi@0: } aoqi@0: public void print(Double d, NamespaceResolver resolver, StringBuilder buf) { aoqi@0: buf.append(d); aoqi@0: } aoqi@0: }, aoqi@0: new DatatypeWriter() { aoqi@0: public Class getType() { aoqi@0: return QName.class; aoqi@0: } aoqi@0: public void print(QName qn, NamespaceResolver resolver, StringBuilder buf) { aoqi@0: String p = resolver.getPrefix(qn.getNamespaceURI()); aoqi@0: if(p.length()!=0) aoqi@0: buf.append(p).append(':'); aoqi@0: buf.append(qn.getLocalPart()); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: public DatatypeWriter get(int n) { aoqi@0: return BUILTIN_ARRAY[n]; aoqi@0: } aoqi@0: aoqi@0: public int size() { aoqi@0: return BUILTIN_ARRAY.length; aoqi@0: } aoqi@0: }); aoqi@0: }