1 11 package org.eclipse.core.internal.properties; 12 13 import java.io.*; 14 import org.eclipse.core.internal.resources.CompatibilityMessages; 15 import org.eclipse.core.internal.resources.ResourceException; 16 import org.eclipse.core.internal.utils.Convert; 17 import org.eclipse.core.resources.IResourceStatus; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.osgi.util.NLS; 20 21 24 public class StoreKey { 25 protected byte[] value = null; 26 protected boolean matchPrefix = false; 27 protected ResourceName resourceName = null; 28 protected String qualifier = null; 29 protected String localName = null; 30 31 public StoreKey(byte[] bytes) throws CoreException { 32 super(); 33 value = bytes; 34 initializeObjects(); 35 } 36 37 40 public StoreKey(ResourceName resourceName, QualifiedName propertyName) throws CoreException { 41 super(); 42 this.resourceName = resourceName; 43 qualifier = propertyName.getQualifier(); 44 localName = propertyName.getLocalName(); 45 initializeBytes(); 46 } 47 48 53 public StoreKey(ResourceName resourceName, boolean matchPrefix) throws CoreException { 54 super(); 55 this.resourceName = resourceName; 56 this.matchPrefix = matchPrefix; 57 initializeBytes(); 58 } 59 60 public String getLocalName() { 61 return localName; 62 } 63 64 public QualifiedName getPropertyName() { 65 return new QualifiedName(qualifier, localName); 66 } 67 68 public String getQualifier() { 69 return qualifier; 70 } 71 72 public ResourceName getResourceName() { 73 return resourceName; 74 } 75 76 private void initializeBytes() throws CoreException { 77 try { 78 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 79 writeNullTerminated(buffer, resourceName.getQualifier()); 81 String path = resourceName.getPath().toString(); 82 if (matchPrefix) { 84 writeBytes(buffer, path); 86 if (qualifier != null || localName != null) { 88 String message = NLS.bind(CompatibilityMessages.properties_invalidPropName, qualifier, localName); 89 throw new ResourceException(IResourceStatus.INVALID_VALUE, null, message, null); 90 } 91 } else { 92 writeNullTerminated(buffer, path); 94 } 95 if (qualifier != null) { 97 writeNullTerminated(buffer, qualifier); 98 if (localName != null) 100 writeNullTerminated(buffer, localName); 101 } else if (localName != null) { 102 String message = NLS.bind(CompatibilityMessages.properties_invalidPropName, qualifier, localName); 104 throw new ResourceException(IResourceStatus.INVALID_VALUE, null, message, null); 105 } 106 value = buffer.toByteArray(); 107 } catch (IOException e) { 108 throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, CompatibilityMessages.properties_storeProblem, e); 110 } 111 } 112 113 117 protected void initializeObjects() throws CoreException { 118 try { 119 ByteArrayInputStream stream = new ByteArrayInputStream(value); 120 String prefix = readNullTerminated(stream); 121 String path = readNullTerminated(stream); 122 resourceName = new ResourceName(prefix, new Path(path)); 123 qualifier = readNullTerminated(stream); 124 localName = readNullTerminated(stream); 125 } catch (IOException e) { 126 throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, CompatibilityMessages.properties_storeProblem, e); 128 } 129 } 130 131 private String readNullTerminated(ByteArrayInputStream stream) throws IOException { 132 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 133 int b; 134 while ((b = stream.read()) > 0) 135 buffer.write(b); 136 if (b == -1) 138 throw new EOFException(); 139 return Convert.fromUTF8(buffer.toByteArray()); 140 } 141 142 public byte[] toBytes() { 143 return value; 144 } 145 146 public String toString() { 148 return new String (toBytes()); 149 } 150 151 private void writeBytes(ByteArrayOutputStream stream, String value) throws IOException { 152 byte[] bytes = Convert.toUTF8(value); 153 stream.write(bytes); 154 } 155 156 private void writeNullTerminated(ByteArrayOutputStream stream, String value) throws IOException { 157 writeBytes(stream, value); 158 stream.write(0); 159 } 160 } 161 | Popular Tags |