Coverage Report - org.xembly.Arg
 
Classes in this File Line Coverage Branch Coverage Complexity
Arg
92%
65/70
72%
39/54
4.125
 
 1  
 /**
 2  
  * Copyright (c) 2013-2017, xembly.org
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the xembly.org nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package org.xembly;
 31  
 
 32  
 import lombok.EqualsAndHashCode;
 33  
 
 34  
 /**
 35  
  * Argument properly escaped.
 36  
  *
 37  
  * <p>The class is immutable and thread-safe.
 38  
  *
 39  
  * @author Yegor Bugayenko (yegor256@gmail.com)
 40  
  * @version $Id: 31dd86741382273f0a03a0c7393b0d6847601d17 $
 41  
  * @since 0.1
 42  
  */
 43  0
 @EqualsAndHashCode(of = "value")
 44  
 final class Arg {
 45  
 
 46  
     /**
 47  
      * Value of it.
 48  
      */
 49  
     private final transient String value;
 50  
 
 51  
     /**
 52  
      * Public ctor.
 53  
      * @param val Value of it
 54  
      * @throws XmlContentException If fails
 55  
      */
 56  616
     Arg(final String val) throws XmlContentException {
 57  3968
         for (final char chr : val.toCharArray()) {
 58  3357
             Arg.legal(chr);
 59  
         }
 60  613
         this.value = val;
 61  613
     }
 62  
 
 63  
     @Override
 64  
     public String toString() {
 65  45
         final String escaped = Arg.escape(this.value);
 66  90
         return new StringBuilder(this.value.length() + 2 + escaped.length())
 67  45
             .append('"').append(escaped).append('"').toString();
 68  
     }
 69  
 
 70  
     /**
 71  
      * Get it's raw value.
 72  
      * @return Value
 73  
      */
 74  
     public String raw() {
 75  571
         return this.value;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Un-escape all XML symbols.
 80  
      * @param text XML text
 81  
      * @return Clean text
 82  
      * @throws XmlContentException If fails
 83  
      */
 84  
     @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 85  
     public static String unescape(final String text)
 86  
         throws XmlContentException {
 87  130
         final char[] chars = text.toCharArray();
 88  130
         if (chars.length < 2) {
 89  0
             throw new IllegalArgumentException(
 90  
                 "internal error, argument can't be shorter than 2 chars"
 91  
             );
 92  
         }
 93  130
         final int len = chars.length - 1;
 94  130
         final StringBuilder output = new StringBuilder(text.length());
 95  844
         for (int idx = 1; idx < len; ++idx) {
 96  717
             if (chars[idx] == '&') {
 97  32
                 final StringBuilder sbuf = new StringBuilder(0);
 98  166
                 while (chars[idx] != ';') {
 99  
                     // @checkstyle ModifiedControlVariable (1 line)
 100  134
                     ++idx;
 101  134
                     if (idx == chars.length) {
 102  0
                         throw new XmlContentException(
 103  
                             "reached EOF while parsing XML symbol"
 104  
                         );
 105  
                     }
 106  134
                     sbuf.append(chars[idx]);
 107  
                 }
 108  32
                 output.append(Arg.symbol(sbuf.substring(0, sbuf.length() - 1)));
 109  29
             } else {
 110  685
                 output.append(chars[idx]);
 111  
             }
 112  
         }
 113  127
         return output.toString();
 114  
     }
 115  
 
 116  
     /**
 117  
      * Escape all unprintable characters.
 118  
      * @param text Raw text
 119  
      * @return Clean text
 120  
      */
 121  
     private static String escape(final String text) {
 122  45
         final StringBuilder output = new StringBuilder(text.length());
 123  368
         for (final char chr : text.toCharArray()) {
 124  323
             if (chr < ' ') {
 125  7
                 output.append("&#").append((int) chr).append(';');
 126  316
             } else if (chr == '"') {
 127  3
                 output.append("&quot;");
 128  313
             } else if (chr == '&') {
 129  2
                 output.append("&amp;");
 130  311
             } else if (chr == '\'') {
 131  3
                 output.append("&apos;");
 132  308
             } else if (chr == '<') {
 133  3
                 output.append("&lt;");
 134  305
             } else if (chr == '>') {
 135  3
                 output.append("&gt;");
 136  
             } else {
 137  302
                 output.append(chr);
 138  
             }
 139  
         }
 140  45
         return output.toString();
 141  
     }
 142  
 
 143  
     /**
 144  
      * Convert XML symbol to char.
 145  
      * @param symbol XML symbol, without leading ampersand
 146  
      * @return Character
 147  
      * @throws XmlContentException If fails
 148  
      */
 149  
     private static char symbol(final String symbol) throws XmlContentException {
 150  
         final char chr;
 151  32
         if ('#' == symbol.charAt(0)) {
 152  10
             final int num = Integer.parseInt(symbol.substring(1));
 153  10
             chr = Arg.legal((char) num);
 154  7
         } else if ("apos".equalsIgnoreCase(symbol)) {
 155  5
             chr = '\'';
 156  17
         } else if ("quot".equalsIgnoreCase(symbol)) {
 157  7
             chr = '"';
 158  10
         } else if ("lt".equalsIgnoreCase(symbol)) {
 159  3
             chr = '<';
 160  7
         } else if ("gt".equalsIgnoreCase(symbol)) {
 161  3
             chr = '>';
 162  4
         } else if ("amp".equalsIgnoreCase(symbol)) {
 163  4
             chr = '&';
 164  
         } else {
 165  0
             throw new XmlContentException(
 166  0
                 String.format("unknown XML symbol &%s;", symbol)
 167  
             );
 168  
         }
 169  29
         return chr;
 170  
     }
 171  
 
 172  
     /**
 173  
      * Validate char number and throw exception if it's not legal.
 174  
      * @param chr Char number
 175  
      * @return The same number
 176  
      * @throws XmlContentException If illegal
 177  
      */
 178  
     private static char legal(final char chr) throws XmlContentException {
 179  
         // @checkstyle MagicNumber (5 lines)
 180  3378
         Arg.range(chr, 0x00, 0x08);
 181  3377
         Arg.range(chr, 0x0B, 0x0C);
 182  3373
         Arg.range(chr, 0x0E, 0x1F);
 183  3365
         Arg.range(chr, 0x7F, 0x84);
 184  3357
         Arg.range(chr, 0x86, 0x9F);
 185  3354
         return chr;
 186  
     }
 187  
 
 188  
     /**
 189  
      * Throw if number is in the range.
 190  
      * @param chr Char number
 191  
      * @param left Left number (inclusive)
 192  
      * @param right Right number (inclusive)
 193  
      * @throws XmlContentException If illegal
 194  
      */
 195  
     private static void range(final char chr, final int left, final int right)
 196  
         throws XmlContentException {
 197  16665
         if (chr >= left && chr <= right) {
 198  5
             throw new XmlContentException(
 199  5
                 String.format(
 200  
                     // @checkstyle LineLength (1 line)
 201  
                     "Character #%02X is in the restricted XML range #%02X-#%02X, see http://www.w3.org/TR/2004/REC-xml11-20040204/#charsets",
 202  5
                     (int) chr, left, right
 203  
                 )
 204  
             );
 205  
         }
 206  16663
     }
 207  
 
 208  
 }