com.sun.tools.xjc.generator.util.WhitespaceNormalizer Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.tools.xjc.generator.util;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JStringLiteral;
import com.sun.xml.bind.WhiteSpaceProcessor;
/**
* Generates code that performs the whitespace normalization.
*/
public abstract class WhitespaceNormalizer
{
/**
* Generates the expression that normalizes
* the given expression (which evaluates to java.lang.String).
*
* @param codeModel
* The owner code model object under which a new expression
* will be created.
*/
public abstract JExpression generate( JCodeModel codeModel, JExpression literal );
/**
* Parses "preserve","replace" or "collapse" into
* the corresponding WhitespaceNormalizer object.
*
* @param method
* Either "preserve", "replace", or "collapse"
*
* @exception IllegalArgumentException
* when the specified method is invalid.
*/
public static WhitespaceNormalizer parse( String method ) {
if( method.equals("preserve") )
return PRESERVE;
if( method.equals("replace") )
return REPLACE;
if( method.equals("collapse") )
return COLLAPSE;
throw new IllegalArgumentException(method);
}
public static final WhitespaceNormalizer PRESERVE = new WhitespaceNormalizer() {
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
return literal;
}
};
public static final WhitespaceNormalizer REPLACE = new WhitespaceNormalizer() {
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
// WhitespaceProcessor.replace();
if( literal instanceof JStringLiteral )
// optimize
return JExpr.lit( WhiteSpaceProcessor.replace(((JStringLiteral)literal).str) );
else
return codeModel.ref(WhiteSpaceProcessor.class)
.staticInvoke("replace").arg(literal);
}
};
public static final WhitespaceNormalizer COLLAPSE = new WhitespaceNormalizer() {
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
// WhitespaceProcessor.replace();
if( literal instanceof JStringLiteral )
// optimize
return JExpr.lit( WhiteSpaceProcessor.collapse(((JStringLiteral)literal).str) );
else
return codeModel.ref(WhiteSpaceProcessor.class)
.staticInvoke("collapse").arg(literal);
}
};
}