Search in sources :

Example 6 with CharacterEscapeHandler

use of com.sun.xml.bind.marshaller.CharacterEscapeHandler in project ANNIS by korpling.

the class GuavaEscaperMarshallerProvider method getContext.

@Override
public Marshaller getContext(Class<?> type) {
    JAXBContext context = contextCache.computeIfAbsent(type, new Function<Class<?>, JAXBContext>() {

        @Override
        public JAXBContext apply(Class<?> t) {
            try {
                return JAXBContext.newInstance(t);
            } catch (JAXBException ex) {
                log.error("Can't create JAXB context", ex);
            }
            return null;
        }
    });
    if (context != null) {
        try {
            Marshaller m = context.createMarshaller();
            m.setProperty("com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() {

                @Override
                public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
                    String asString = new String(ch, start, length);
                    Escaper e = isAttVal ? XmlEscapers.xmlAttributeEscaper() : XmlEscapers.xmlContentEscaper();
                    out.write(e.escape(asString));
                }
            });
            return m;
        } catch (JAXBException ex) {
            log.error("Can't create XML marshaller", ex);
        }
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) CharacterEscapeHandler(com.sun.xml.bind.marshaller.CharacterEscapeHandler) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Escaper(com.google.common.escape.Escaper) Writer(java.io.Writer)

Example 7 with CharacterEscapeHandler

use of com.sun.xml.bind.marshaller.CharacterEscapeHandler in project entando-core by entando.

the class CDataCharacterEscapeHandler method useStandardEscape.

private void useStandardEscape(char[] ch, int start, int length, boolean isAttVal, Writer writer) throws IOException {
    CharacterEscapeHandler escapeHandler = StandardEscapeHandler.getInstance();
    escapeHandler.escape(ch, start, length, isAttVal, writer);
}
Also used : CharacterEscapeHandler(com.sun.xml.bind.marshaller.CharacterEscapeHandler)

Aggregations

CharacterEscapeHandler (com.sun.xml.bind.marshaller.CharacterEscapeHandler)7 DataWriter (com.sun.xml.bind.marshaller.DataWriter)5 XMLWriter (com.sun.xml.bind.marshaller.XMLWriter)5 BufferedWriter (java.io.BufferedWriter)5 Escaper (com.google.common.escape.Escaper)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1