1 26 27 package it.stefanochizzolini.clown.objects; 28 29 import it.stefanochizzolini.clown.files.File; 30 31 import java.util.regex.Matcher ; 32 import java.util.regex.Pattern ; 33 34 37 44 48 public class PdfLiteral 49 extends PdfAtomicObject<String > 50 implements IPdfString 51 { 52 66 private static final Pattern UnescapedPattern = Pattern.compile("[\\n\\r\\t\\u0008\\f\\(\\)\\\\]"); 67 69 74 public static String toPdf( 75 String value 76 ) 77 { 78 81 StringBuilder buffer = new StringBuilder ("("); 82 83 int index = 0; 84 Matcher unescapedMatcher = UnescapedPattern.matcher(value); 85 while(unescapedMatcher.find()) 86 { 87 int start = unescapedMatcher.start(); 88 if(start > index) 89 {buffer.append(value.substring(index,start));} 90 91 buffer.append('\\'); 92 char unescapedChar = unescapedMatcher.group(0).charAt(0); 93 switch(unescapedChar) 94 { 95 case '\n': 96 buffer.append('n'); 97 break; 98 case '\r': 99 buffer.append('r'); 100 break; 101 case '\t': 102 buffer.append('t'); 103 break; 104 case '\b': 105 buffer.append('b'); 106 break; 107 case '\f': 108 buffer.append('f'); 109 break; 110 case '(': 111 case ')': 112 case '\\': 113 buffer.append(unescapedChar); 114 break; 115 } 116 117 index = unescapedMatcher.end(); 118 } 119 if(index < value.length()) 120 {buffer.append(value.substring(index));} 121 122 buffer.append(")"); 123 124 return buffer.toString(); 125 } 126 130 public PdfLiteral( 133 ) 134 {} 135 136 public PdfLiteral( 137 String value 138 ) 139 {setValue(value);} 140 142 @Override 145 public Object clone( 146 File context 147 ) 148 { 149 PdfLiteral clone = (PdfLiteral)super.clone(); 151 152 154 155 return clone; 156 } 157 158 @Override 159 public String toPdf( 160 ) 161 {return PdfLiteral.toPdf(getValue());} 162 163 public String getStringValue( 165 ) 166 {return getValue();} 167 168 public void setStringValue( 169 String value 170 ) 171 {setValue(value);} 172 } | Popular Tags |