com.davidbracewell.io.structured.xml.XMLWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango Show documentation
Show all versions of mango Show documentation
A set of utilities and tools to speed up and ease programming in Java.
/*
* (c) 2005 David B. Bracewell
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.davidbracewell.io.structured.xml;
import com.davidbracewell.DynamicEnum;
import com.davidbracewell.collection.Counter;
import com.davidbracewell.collection.MultiCounter;
import com.davidbracewell.conversion.Cast;
import com.davidbracewell.conversion.Convert;
import com.davidbracewell.io.resource.Resource;
import com.davidbracewell.io.structured.ElementType;
import com.davidbracewell.io.structured.StructuredWriter;
import com.davidbracewell.io.structured.Writable;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Multimap;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
/**
* An implementation of a StructuredWriter that writes xml.
*
* @author David B. Bracewell
*/
public class XMLWriter extends StructuredWriter {
private final String documentTag;
private final Stack stack;
private final OutputStream os;
private final XMLStreamWriter writer;
private boolean documentIsArray = false;
/**
* Creates an XML writer with the document tag "document"
*
* @param resource The resource to write xml to
* @throws IOException Something went wrong writing
*/
public XMLWriter(Resource resource) throws IOException {
this("document", resource);
}
/**
* Creates an XML writer with a given document tag
*
* @param documentTag The document tag to use for the xml document
* @param resource The resource to write xml to
* @throws IOException Something went wrong writing
*/
public XMLWriter(String documentTag, Resource resource) throws IOException {
try {
Preconditions.checkArgument(!Strings.isNullOrEmpty(documentTag));
stack = new Stack<>();
this.documentTag = documentTag;
this.os = resource.outputStream();
this.writer = XMLOutputFactory.newFactory().createXMLStreamWriter(os, "UTF-8");
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public XMLWriter beginDocument(boolean inArray) throws IOException {
try {
writer.writeStartDocument();
writer.writeStartElement(documentTag);
if (inArray) {
writer.writeAttribute("type", "array");
documentIsArray = true;
}
stack.push(ElementType.BEGIN_DOCUMENT);
} catch (XMLStreamException e) {
throw new IOException(e);
}
return this;
}
/**
* Specific to XML is the ability to write attributes.
*
* @param name the name of the attribute
* @param value The value of the attribute
* @return This XMLWriter
* @throws IOException Something went wrong writing
*/
public XMLWriter writeAttribute(String name, String value) throws IOException {
try {
writer.writeAttribute(name, value);
} catch (XMLStreamException e) {
throw new IOException(e);
}
return this;
}
@Override
public void endDocument() throws IOException {
try {
writer.writeEndElement();
writer.writeEndDocument();
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
@Override
protected StructuredWriter writeNull() throws IOException {
try {
writer.writeCharacters("null");
} catch (XMLStreamException e) {
throw new IOException(e);
}
return this;
}
@Override
protected StructuredWriter writeNumber(Number number) throws IOException {
try {
writer.writeCharacters(number.toString());
} catch (XMLStreamException e) {
throw new IOException(e);
}
return this;
}
@Override
protected StructuredWriter writeString(String string) throws IOException {
try {
writer.writeCharacters(string);
} catch (XMLStreamException e) {
throw new IOException(e);
}
return this;
}
@Override
protected StructuredWriter writeBoolean(boolean value) throws IOException {
try {
writer.writeCharacters(Boolean.toString(value));
} catch (XMLStreamException e) {
throw new IOException(e);
}
return this;
}
@Override
public XMLWriter writeKeyValue(String key, Object object) throws IOException {
try {
if (object == null ||
object instanceof Number ||
object instanceof String ||
object instanceof Boolean ||
object instanceof Enum ||
object instanceof DynamicEnum) {
writer.writeStartElement(key);
writeValue(object);
writer.writeEndElement();
} else if (object instanceof Collection) {
writeCollection(key, Cast.as(object));
} else if (object instanceof Map) {
writeMap(key, Cast.as(object));
} else if (object.getClass().isArray()) {
writeArray(key, Cast.as(object));
} else if (object instanceof Multimap) {
writeMap(key, Cast.as(object).asMap());
} else if (object instanceof Counter) {
writeMap(key, Cast.as(object).asMap());
} else if (object instanceof MultiCounter) {
writeMap(key, Cast.as(object).asMap());
} else if (object instanceof Iterable) {
writeCollection(key, new AbstractCollection © 2015 - 2025 Weber Informatics LLC | Privacy Policy