KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > properties > StoreKey


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
22  *
23  */

24 public class StoreKey {
25     protected byte[] value = null;
26     protected boolean matchPrefix = false;
27     protected ResourceName resourceName = null;
28     protected String JavaDoc qualifier = null;
29     protected String JavaDoc localName = null;
30
31     public StoreKey(byte[] bytes) throws CoreException {
32         super();
33         value = bytes;
34         initializeObjects();
35     }
36
37     /*
38      * The key is a full key
39      */

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     /*
49      * The key is a resource name -- either a full resource name
50      * or a resource name prefix (for deep matching)
51      * (i.e. no property name)
52      */

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 JavaDoc getLocalName() {
61         return localName;
62     }
63
64     public QualifiedName getPropertyName() {
65         return new QualifiedName(qualifier, localName);
66     }
67
68     public String JavaDoc 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             // Step 0: the resource name prefix -- always considered in full
80
writeNullTerminated(buffer, resourceName.getQualifier());
81             String JavaDoc path = resourceName.getPath().toString();
82             // Step 1: the resource path
83
if (matchPrefix) {
84                 // don't null terminate resource name
85
writeBytes(buffer, path);
86                 // If prefix matching, cannot allow other fields to be specified
87
if (qualifier != null || localName != null) {
88                     String JavaDoc message = NLS.bind(CompatibilityMessages.properties_invalidPropName, qualifier, localName);
89                     throw new ResourceException(IResourceStatus.INVALID_VALUE, null, message, null);
90                 }
91             } else {
92                 // Zero depth requires full path matching including null
93
writeNullTerminated(buffer, path);
94             }
95             // Step 2: the name space (no prefix matching)
96
if (qualifier != null) {
97                 writeNullTerminated(buffer, qualifier);
98                 // Step 3: the local name (no prefix matching)
99
if (localName != null)
100                     writeNullTerminated(buffer, localName);
101             } else if (localName != null) {
102                 // Specifying a local name without a qualifier is illegal
103
String JavaDoc 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             // should never happen
109
throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, CompatibilityMessages.properties_storeProblem, e);
110         }
111     }
112
113     /**
114      * Assumes value bytes are fully defined -- i.e. initializing
115      * from a stored key.
116      */

117     protected void initializeObjects() throws CoreException {
118         try {
119             ByteArrayInputStream stream = new ByteArrayInputStream(value);
120             String JavaDoc prefix = readNullTerminated(stream);
121             String JavaDoc path = readNullTerminated(stream);
122             resourceName = new ResourceName(prefix, new Path(path));
123             qualifier = readNullTerminated(stream);
124             localName = readNullTerminated(stream);
125         } catch (IOException e) {
126             // should never happen
127
throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, CompatibilityMessages.properties_storeProblem, e);
128         }
129     }
130
131     private String JavaDoc readNullTerminated(ByteArrayInputStream stream) throws IOException {
132         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
133         int b;
134         while ((b = stream.read()) > 0)
135             buffer.write(b);
136         //should not reach end of stream without first hitting null (0) byte
137
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     //For debugging
147
public String JavaDoc toString() {
148         return new String JavaDoc(toBytes());
149     }
150
151     private void writeBytes(ByteArrayOutputStream stream, String JavaDoc value) throws IOException {
152         byte[] bytes = Convert.toUTF8(value);
153         stream.write(bytes);
154     }
155
156     private void writeNullTerminated(ByteArrayOutputStream stream, String JavaDoc value) throws IOException {
157         writeBytes(stream, value);
158         stream.write(0);
159     }
160 }
161
Popular Tags