1 20 21 package org.apache.directory.ldapstudio.browser.core.model.ldif.lines; 22 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 28 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 29 30 31 public class LdifValueLineBase extends LdifNonEmptyLineBase 32 { 33 34 private static final long serialVersionUID = -7030930374861554147L; 35 36 private String rawValueType; 37 38 private String rawValue; 39 40 41 protected LdifValueLineBase() 42 { 43 } 44 45 46 public LdifValueLineBase( int offset, String rawLineStart, String rawValueType, String rawValue, String rawNewLine ) 47 { 48 super( offset, rawLineStart, rawNewLine ); 49 50 this.rawValueType = rawValueType; 51 this.rawValue = rawValue; 52 } 53 54 55 public String getRawValueType() 56 { 57 return getNonNull( this.rawValueType ); 58 } 59 60 61 public String getUnfoldedValueType() 62 { 63 return unfold( this.getRawValueType() ); 64 } 65 66 67 public String getRawValue() 68 { 69 return getNonNull( this.rawValue ); 70 } 71 72 73 public String getUnfoldedValue() 74 { 75 return unfold( this.getRawValue() ); 76 } 77 78 79 public String toRawString() 80 { 81 return this.getRawLineStart() + this.getRawValueType() + this.getRawValue() + this.getRawNewLine(); 82 } 83 84 85 public boolean isValid() 86 { 87 return super.isValid() && this.rawValueType != null && this.rawValue != null; 88 } 89 90 91 public String getInvalidString() 92 { 93 if ( this.rawValueType == null ) 94 { 95 return "Missing value type ':', '::' or ':<'"; 96 } 97 else if ( this.rawValue == null ) 98 { 99 return "Missing value"; 100 } 101 else 102 { 103 return super.getInvalidString(); 104 } 105 } 106 107 108 112 public final String getValueAsString() 113 { 114 Object o = getValueAsObject(); 115 if ( o instanceof String ) 116 { 117 return ( String ) o; 118 } 119 else if ( o instanceof byte[] ) 120 { 121 return LdifUtils.utf8decode( ( byte[] ) o ); 122 } 123 else 124 { 125 return ""; 126 } 127 } 128 129 130 135 public final byte[] getValueAsBinary() 136 { 137 Object o = getValueAsObject(); 138 if ( o instanceof String ) 139 { 140 return LdifUtils.utf8encode( ( String ) o ); 141 } 142 else if ( o instanceof byte[] ) 143 { 144 return ( byte[] ) o; 145 } 146 else 147 { 148 return new byte[0]; 149 } 150 } 151 152 153 166 public final Object getValueAsObject() 167 { 168 if ( this.isValueTypeSafe() ) 169 { 170 return this.getUnfoldedValue(); 171 } 172 else if ( this.isValueTypeBase64() ) 173 { 174 return LdifUtils.base64decodeToByteArray( this.getUnfoldedValue() ); 175 } 176 else if ( this.isValueTypeURL() ) 177 { 178 try 179 { 180 File file = new File ( this.getUnfoldedValue() ); 181 byte[] data = new byte[( int ) file.length()]; 182 FileInputStream fis = new FileInputStream ( file ); 183 fis.read( data ); 184 return data; 185 } 186 catch ( IOException ioe ) 187 { 188 return null; 189 } 190 } 191 else 192 { 193 return null; 194 } 195 } 196 197 198 public boolean isValueTypeURL() 199 { 200 return this.getUnfoldedValueType().startsWith( ":<" ); 201 } 202 203 204 public boolean isValueTypeBase64() 205 { 206 return this.getUnfoldedValueType().startsWith( "::" ); 207 } 208 209 210 public boolean isValueTypeSafe() 211 { 212 return this.getUnfoldedValueType().startsWith( ":" ) && !this.isValueTypeBase64() && !this.isValueTypeURL(); 213 } 214 215 } 216 | Popular Tags |