1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.internal.xjc.generator.util; 27 28 import com.sun.codemodel.internal.JCodeModel; 29 import com.sun.codemodel.internal.JExpr; 30 import com.sun.codemodel.internal.JExpression; 31 import com.sun.codemodel.internal.JStringLiteral; 32 import com.sun.xml.internal.bind.WhiteSpaceProcessor; 33 34 /** 35 * Generates code that performs the whitespace normalization. 36 */ 37 public abstract class WhitespaceNormalizer 38 { 39 /** 40 * Generates the expression that normalizes 41 * the given expression (which evaluates to java.lang.String). 42 * 43 * @param codeModel 44 * The owner code model object under which a new expression 45 * will be created. 46 */ 47 public abstract JExpression generate( JCodeModel codeModel, JExpression literal ); 48 49 /** 50 * Parses "preserve","replace" or "collapse" into 51 * the corresponding WhitespaceNormalizer object. 52 * 53 * @param method 54 * Either "preserve", "replace", or "collapse" 55 * 56 * @exception IllegalArgumentException 57 * when the specified method is invalid. 58 */ 59 public static WhitespaceNormalizer parse( String method ) { 60 if( method.equals("preserve") ) 61 return PRESERVE; 62 63 if( method.equals("replace") ) 64 return REPLACE; 65 66 if( method.equals("collapse") ) 67 return COLLAPSE; 68 69 throw new IllegalArgumentException(method); 70 } 71 72 public static final WhitespaceNormalizer PRESERVE = new WhitespaceNormalizer() { 73 public JExpression generate( JCodeModel codeModel, JExpression literal ) { 74 return literal; 75 } 76 }; 77 78 public static final WhitespaceNormalizer REPLACE = new WhitespaceNormalizer() { 79 public JExpression generate( JCodeModel codeModel, JExpression literal ) { 80 // WhitespaceProcessor.replace(<literal>); 81 if( literal instanceof JStringLiteral ) 82 // optimize 83 return JExpr.lit( WhiteSpaceProcessor.replace(((JStringLiteral)literal).str) ); 84 else 85 return codeModel.ref(WhiteSpaceProcessor.class) 86 .staticInvoke("replace").arg(literal); 87 } 88 }; 89 90 public static final WhitespaceNormalizer COLLAPSE = new WhitespaceNormalizer() { 91 public JExpression generate( JCodeModel codeModel, JExpression literal ) { 92 // WhitespaceProcessor.replace(<literal>); 93 if( literal instanceof JStringLiteral ) 94 // optimize 95 return JExpr.lit( WhiteSpaceProcessor.collapse(((JStringLiteral)literal).str) ); 96 else 97 return codeModel.ref(WhiteSpaceProcessor.class) 98 .staticInvoke("collapse").arg(literal); 99 } 100 }; 101 }