Snippet: JAXB and CDATA ContentHandler with CharacterEscapeHandler

Main program:

package com.tutego.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.PrintWriter;

public class App {
  public static void main( String[] args ) throws Exception {
    Dog dog = new Dog();
    dog.name = "Wüffi";
    Flea flea = new Flea();
    flea.name = "<><> Böser Floh <><>";
    dog.flea = flea;

    JAXBContext jaxbContext = JAXBContext.newInstance( dog.getClass() );
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    jaxbMarshaller.marshal( dog, new CDATAContentHandler( new PrintWriter( System.out ) ) );
  }
}

Dog and Flea:

package com.tutego.jaxb;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Dog {
  public String name;
  public Flea flea;
}

class Flea {
  public String name;
}

CDATAContentHandler:

package com.tutego.jaxb;

import com.sun.xml.txw2.output.CharacterEscapeHandler;
import com.sun.xml.txw2.output.DataWriter;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.Writer;

public class CDATAContentHandler extends DataWriter {
  public CDATAContentHandler( Writer writer ) {
    super( writer, "UTF-8", MinimumEscapeHandler.theInstance );
  }

  @Override
  public void characters( char[] ch, int start, int length ) throws SAXException {
    boolean useCData = false;

    loop:
    for ( int i = start; i < start + length; i++ )
      switch ( ch[ i ] ) {
        case '<': case '>': case '&': useCData = true;
        break loop;
      }

    if ( useCData ) super.startCDATA();
    super.characters( ch, start, length );
    if ( useCData ) super.endCDATA();
  }
}

/**
 * Performs no character escaping. Usable only when the output encoding
 * is UTF, but this handler gives the maximum performance.
 *
 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
 */
class MinimumEscapeHandler implements CharacterEscapeHandler {

  private MinimumEscapeHandler() {
  }  // no instanciation please

  public static final CharacterEscapeHandler theInstance = new MinimumEscapeHandler();

  public void escape( char[] ch, int start, int length, boolean isAttVal, Writer out )
      throws IOException {
    // avoid calling the Writerwrite method too much by assuming
    // that the escaping occurs rarely.
    // profiling revealed that this is faster than the naive code.
    int limit = start + length;
    for ( int i = start; i < limit; i++ ) {
      char c = ch[ i ];
      if ( c == '&' || c == '<' || c == '>' || c == '\r' || (c == '\n' && isAttVal) || (c == '\"' && isAttVal) ) {
        if ( i != start )
          out.write( ch, start, i - start );
        start = i + 1;
        switch ( ch[ i ] ) {
          case '&':
            out.write( "&amp;" );
            break;
          case '<':
            out.write( "&lt;" );
            break;
          case '>':
            out.write( "&gt;" );
            break;
          case '\"':
            out.write( "&quot;" );
            break;
          case '\n':
          case '\r':
            out.write( "&#" );
            out.write( Integer.toString( c ) );
            out.write( ';' );
            break;
          default:
            throw new IllegalArgumentException( "Cannot escape: '" + c + "'" );
        }
      }
    }

    if ( start != limit )
      out.write( ch, start, limit - start );
  }
}

Quarkus 1.0.0 erschienen

Das „Kubernetes Native Java stack tailored for OpenJDK HotSpot and GraalVM, crafted from the best of breed Java libraries and standards.“ ist in der ersten Version erschienen und positioniert sich gegen Spring Boot und Jakarata EE.

Zum Weiterlesen und schauen:

this für kaskadierte Methoden und Builder

Die append(…)-Methoden bei StringBuilder liefern die this-Referenz, sodass sich Folgendes schreiben lässt:

StringBuilder sb = new StringBuilder();
sb.append( "Android oder iPhone" ).append( '?' );

Jedes append(…) liefert das StringBuilder-Objekt, auf dem es aufgerufen wird – wir können also Methoden kaskadiert anhängen oder es bleiben lassen.

Wir wollen diese Möglichkeit bei einem Zauberer (Klasse Wizard) programmieren, sodass die Methoden name(String) und age(int) Spielername und Alter zuweisen. Beide Methoden liefern ihr eigenes Wizard-Objekt über die this-Referenz zurück:

class Wizard {



  String name = "";

  int age;




  Wizard name( String name ) { this.name = name; return this; }

  String name() { return name; }




  Wizard age( int item ) { this.age = item; return this; }

  int age() { return age; }




  String format() {

    return name + " ist " + age;

  }

}

Erzeugen wir einen Wizard, und kaskadieren wir einige Methoden:

Wizard gundalf = new Wizard().name( "Gundalf" ).age( 60 );

System.out.println( gundalf.name() );         // Gundalf

System.out.println( gundalf.format() );       // Gundalf ist 60

Der Ausdruck new Wizard() liefert eine Referenz, die wir sofort für den Methodenaufruf nutzen. Da name(String) wiederum eine Objektreferenz vom Typ Wizard liefert, ist dahinter direkt .age(int) möglich. Die Verschachtelung von name(„Gunalf“).age(60) bewirkt, dass Name und Alter gesetzt werden und der jeweils nächste Methodenaufruf in der Kette über this eine Referenz auf dasselbe Objekt, aber mit verändertem internem Zustand bekommt.

Beispiele dieser Bauart sind in der Java-Bibliothek an einigen Stellen zu finden. Sie werden auch Builder genannt.

Hinweis: Die Methode Wizard name(String) ist mit ihrer Rückgabe praktisch, verstößt aber aus zwei Gründen gegen die JavaBeans-Konvention: Setter dürfen keine Rückgabe haben und müssen immer mit set beginnen. JavaBeans sind also nicht so dieser kompakten Builder-Schreibweise „kompatibel“.

Microsoft arbeitet am OpenJDK mit

https://mail.openjdk.java.net/pipermail/discuss/2019-October/005173.html:

Subject: Microsoft’s Ready do Contribute to OpenJDK

Hi OpenJDK Community,

In the past week Microsoft formally signed the Oracle Contributor Agreement, in which Oracle Inc. promptly acknowledged and welcomed us to the project. On behalf of the Microsoft Java Engineering Team, I’d like to say that we are thrilled to officially join the OpenJDK project and be ready to work with you.

As many of you may know, Microsoft and its subsidiaries are heavily dependent on Java in many aspects, and also offers Java runtimes in its Microsoft Azure cloud to its customers. Microsoft recognizes the immense value that Oracle’s successful and effective stewardship of the OpenJDK project has bought Java and the wider software ecosystem and we look forward to playing our part in contributing back!

The team will initially be working on smaller bug fixes and backports so that we can learn how to be good citizens within OpenJDK. For example, we already understand that discussing changes first before posting patches is preferred and I'm sure there's more for us to learn as well.

The Java engineering team led by Martijn Verburg [1] is already engaged with other Microsoft groups and its subsidiaries who are using Java, as well as its partners in the Java ecosystem such as Azul Systems, Oracle, Pivotal, Red Hat, Intel, SAP and others, and the overall team will be joining the many OpenJDK mailing lists to start conversations and participating.

We look forward to participating in the future of Java.

[1] martijn.verburg at microsoft.com<mailto:martijn.verburg at microsoft.com>

Best regards
Bruno Borges
Product Management for Java,
Microsoft Developer Division

Mehr zu der Geschichte unter

  • https://jaxenter.com/microsoft-ready-contribute-openjdk-163550.html