All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.tools.xjc.generator.util.WhitespaceNormalizer Maven / Gradle / Ivy

Go to download

JAXB Binding Compiler. Contains source code needed for binding customization files into java sources. In other words: the *tool* to generate java classes for the given xml representation.

The newest version!
/*
 * Copyright (c) 1997, 2021 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 cn.lzgabel.jaxb.core.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() {
        @Override
        public JExpression generate( JCodeModel codeModel, JExpression literal ) {
            return literal;
        }
    };

    public static final WhitespaceNormalizer REPLACE = new WhitespaceNormalizer() {
        @Override
        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() {
        @Override
        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);
        }
    };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy