// Path: src/main/java/org/fuin/utils4j/jaxb/JaxbUtils.java
// public static <T> String marshal(final T data, final Class<?>... classesToBeBound) {
//     return marshal(data, null, classesToBeBound);
// }
// 
// Path: src/main/java/org/fuin/utils4j/jaxb/JaxbUtils.java
// public static <T> T unmarshal(final String xmlData, final Class<?>... classesToBeBound) {
//     final Unmarshaller unmarshaller = new UnmarshallerBuilder().addClassesToBeBound(classesToBeBound).build();
//     return unmarshal(unmarshaller, xmlData);
// }
// 
// Path: src/main/java/org/fuin/utils4j/jaxb/CDataXmlAdapter.java
// public final class CDataXmlAdapter extends XmlAdapter<String, String> {
// 
//     @Override
//     public final String marshal(final String value) throws Exception {
//         if (value == null) {
//             return null;
//         }
//         return "<![CDATA[" + value + "]]>";
//     }
// 
//     @Override
//     public final String unmarshal(final String value) throws Exception {
//         if (value == null) {
//             return null;
//         }
//         return value;
//     }
// 
// }
// 
// Path: src/main/java/org/fuin/utils4j/jaxb/CDataXmlStreamWriter.java
// public final class CDataXmlStreamWriter extends XMLStreamWriterAdapter {
// 
//     /**
//      * Constructor with delegate.
//      * 
//      * @param delegate
//      *            All method calls are delegated to this writer.
//      */
//     public CDataXmlStreamWriter(final XMLStreamWriter delegate) {
//         super(delegate);
//     }
// 
//     @Override
//     public final void writeCharacters(final String text) throws XMLStreamException {
//         writeText(text);
//     }
// 
//     @Override
//     public final void writeCharacters(final char[] text, final int start, final int len) throws XMLStreamException {
//         writeText(new String(text, start, len));
//     }
// 
//     private void writeText(final String text) throws XMLStreamException {
//         if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
//             final String str = text.substring(9, text.length() - 3);
//             super.writeCData(str);
//         } else {
//             super.writeCharacters(text);
//         }
//     }
// 
// }
// 
// Path: src/test/java/org/fuin/utils4j/jaxb/MyClassWithCData.java
// @XmlRootElement(name = "my-class-with-cdata")
// public final class MyClassWithCData {
// 
//     @XmlValue
//     @XmlJavaTypeAdapter(CDataXmlAdapter.class)
//     private String content;
// 
//     /**
//      * Default constructor for JAXB.
//      */
//     protected MyClassWithCData() {
//         super();
//     }
// 
//     /**
//      * Constructor with mandatory data.
//      * 
//      * @param content
//      *            CDATA content.
//      */
//     public MyClassWithCData(final String content) {
//         super();
//         this.content = content;
//     }
// 
//     /**
//      * Returns the content.
//      * 
//      * @return Content.
//      */
//     public String getContent() {
//         return content;
//     }
// 
// }


import static org.fuin.utils4j.jaxb.JaxbUtils.marshal;
import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal;
import java.io.StringWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import jakarta.xml.bind.JAXBContext;
import org.fuin.utils4j.jaxb.CDataXmlAdapter;
import org.fuin.utils4j.jaxb.CDataXmlStreamWriter;
import org.fuin.utils4j.jaxb.MyClassWithCData;
/**
 * Copyright (C) 2015 Michael Schnell. All rights reserved. 
 * http://www.fuin.org/
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see http://www.gnu.org/licenses/.
 */
package org.fuin.utils4j.examples;






/**
 * Short example for using the {@link CDataXmlStreamWriter} together with the {@link CDataXmlAdapter}.
 */
// CHECKSTYLE:OFF
public class CDataJaxbExample {

    /**
     * Executes the example.
     * 
     * @param args
     *            Not used.
     */
    public static void main(String[] args) throws Exception {

        // Create writers
        final StringWriter writer = new StringWriter();
        final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
        final CDataXmlStreamWriter cdataWriter = new CDataXmlStreamWriter(xmlWriter);

        // Create JAXB context with example class
        final JAXBContext ctx = JAXBContext.newInstance(MyClassWithCData.class);
        final MyClassWithCData testee = new MyClassWithCData("<whatever this=\"is\"/>");

        // Convert instance to XML
        marshal(ctx, testee, null, cdataWriter);
        final String xml = writer.toString();

        // Prints out the result
// Path: src/main/java/org/fuin/utils4j/jaxb/JaxbUtils.java
// public static <T> String marshal(final T data, final Class<?>... classesToBeBound) {
//     return marshal(data, null, classesToBeBound);
// }
// 
// Path: src/main/java/org/fuin/utils4j/jaxb/JaxbUtils.java
// public static <T> T unmarshal(final String xmlData, final Class<?>... classesToBeBound) {
//     final Unmarshaller unmarshaller = new UnmarshallerBuilder().addClassesToBeBound(classesToBeBound).build();
//     return unmarshal(unmarshaller, xmlData);
// }
// 
// Path: src/main/java/org/fuin/utils4j/jaxb/CDataXmlAdapter.java
// public final class CDataXmlAdapter extends XmlAdapter<String, String> {
// 
//     @Override
//     public final String marshal(final String value) throws Exception {
//         if (value == null) {
//             return null;
//         }
//         return "<![CDATA[" + value + "]]>";
//     }
// 
//     @Override
//     public final String unmarshal(final String value) throws Exception {
//         if (value == null) {
//             return null;
//         }
//         return value;
//     }
// 
// }
// 
// Path: src/main/java/org/fuin/utils4j/jaxb/CDataXmlStreamWriter.java
// public final class CDataXmlStreamWriter extends XMLStreamWriterAdapter {
// 
//     /**
//      * Constructor with delegate.
//      * 
//      * @param delegate
//      *            All method calls are delegated to this writer.
//      */
//     public CDataXmlStreamWriter(final XMLStreamWriter delegate) {
//         super(delegate);
//     }
// 
//     @Override
//     public final void writeCharacters(final String text) throws XMLStreamException {
//         writeText(text);
//     }
// 
//     @Override
//     public final void writeCharacters(final char[] text, final int start, final int len) throws XMLStreamException {
//         writeText(new String(text, start, len));
//     }
// 
//     private void writeText(final String text) throws XMLStreamException {
//         if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
//             final String str = text.substring(9, text.length() - 3);
//             super.writeCData(str);
//         } else {
//             super.writeCharacters(text);
//         }
//     }
// 
// }
// 
// Path: src/test/java/org/fuin/utils4j/jaxb/MyClassWithCData.java
// @XmlRootElement(name = "my-class-with-cdata")
// public final class MyClassWithCData {
// 
//     @XmlValue
//     @XmlJavaTypeAdapter(CDataXmlAdapter.class)
//     private String content;
// 
//     /**
//      * Default constructor for JAXB.
//      */
//     protected MyClassWithCData() {
//         super();
//     }
// 
//     /**
//      * Constructor with mandatory data.
//      * 
//      * @param content
//      *            CDATA content.
//      */
//     public MyClassWithCData(final String content) {
//         super();
//         this.content = content;
//     }
// 
//     /**
//      * Returns the content.
//      * 
//      * @return Content.
//      */
//     public String getContent() {
//         return content;
//     }
// 
// }


// Path: src/test/java/org/fuin/utils4j/examples/CDataJaxbExample.java
import static org.fuin.utils4j.jaxb.JaxbUtils.marshal;
import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal;
import java.io.StringWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import jakarta.xml.bind.JAXBContext;
import org.fuin.utils4j.jaxb.CDataXmlAdapter;
import org.fuin.utils4j.jaxb.CDataXmlStreamWriter;
import org.fuin.utils4j.jaxb.MyClassWithCData;

/**
 * Copyright (C) 2015 Michael Schnell. All rights reserved. 
 * http://www.fuin.org/
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see http://www.gnu.org/licenses/.
 */
package org.fuin.utils4j.examples;






/**
 * Short example for using the {@link CDataXmlStreamWriter} together with the {@link CDataXmlAdapter}.
 */
// CHECKSTYLE:OFF
public class CDataJaxbExample {

    /**
     * Executes the example.
     * 
     * @param args
     *            Not used.
     */
    public static void main(String[] args) throws Exception {

        // Create writers
        final StringWriter writer = new StringWriter();
        final XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
        final CDataXmlStreamWriter cdataWriter = new CDataXmlStreamWriter(xmlWriter);

        // Create JAXB context with example class
        final JAXBContext ctx = JAXBContext.newInstance(MyClassWithCData.class);
        final MyClassWithCData testee = new MyClassWithCData("<whatever this=\"is\"/>");

        // Convert instance to XML
        marshal(ctx, testee, null, cdataWriter);
        final String xml = writer.toString();

        // Prints out the result
        System.out.println(xml);