1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.binary;
14 import java.io.IOException;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.accessor.Accessor;
18 import org.simantics.databoard.accessor.StringAccessor;
19 import org.simantics.databoard.accessor.error.AccessorConstructionException;
20 import org.simantics.databoard.accessor.error.AccessorException;
21 import org.simantics.databoard.accessor.error.ReferenceException;
22 import org.simantics.databoard.accessor.event.Event;
23 import org.simantics.databoard.accessor.event.ValueAssigned;
24 import org.simantics.databoard.accessor.file.FileStringAccessor;
25 import org.simantics.databoard.accessor.impl.AccessorParams;
26 import org.simantics.databoard.accessor.impl.ListenerEntry;
27 import org.simantics.databoard.accessor.interestset.StringInterestSet;
28 import org.simantics.databoard.accessor.reference.ChildReference;
29 import org.simantics.databoard.binding.Binding;
30 import org.simantics.databoard.binding.StringBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.type.StringType;
33 import org.simantics.databoard.util.binary.Blob;
34 import org.simantics.databoard.util.binary.Endian;
35 import org.simantics.databoard.util.binary.UTF8;
37 public class BinaryString extends BinaryObject implements StringAccessor, FileStringAccessor {
39 public BinaryString(BinaryObject parent, Blob blob, StringType type, AccessorParams params)
40 throws AccessorConstructionException {
41 super(parent, blob, type, params);
45 public StringType type() {
46 return (StringType) type;
50 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
51 Event rollback = makeRollback ? new ValueAssigned( Bindings.STRING, getValue() ) : null;
52 if (e instanceof ValueAssigned) {
53 ValueAssigned va = (ValueAssigned) e;
54 if (va.newValue == null) throw new AccessorException("String value expected, got null");
55 setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());
58 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to String");
62 @SuppressWarnings("unchecked")
64 public <T extends Accessor> T getComponent(ChildReference reference)
65 throws AccessorConstructionException {
66 if (reference==null) return (T) this;
67 throw new ReferenceException(reference.getClass()+" is not a subreference of StringType");
71 public Object getValue(Binding binding) throws AccessorException {
73 StringBinding bb = (StringBinding) binding;
74 String v = getValue();
76 } catch(BindingException e) {
77 throw new AccessorException(e);
82 public String getValue() throws AccessorException {
88 int utflen = Endian.readDynamicUInt32(b);
89 return UTF8.readModifiedUTF(b, utflen);
90 } catch (IOException e) {
91 throw new AccessorException(e);
97 public void setValueNoflush(String string) throws AccessorException {
103 int strlen = UTF8.getModifiedUTF8EncodingByteLength(string);
104 int lenlen = Endian.getDynamicUInt32Length(strlen);
105 b.setLength(strlen+lenlen);
106 Endian.writeDynamicUInt32(b, strlen);
107 UTF8.writeModifiedUTF(b, string);
110 ListenerEntry le = listeners;
112 StringInterestSet is = le.getInterestSet();
113 if (is.inNotifications()) {
114 Event e = new ValueAssigned( Bindings.STRING, is.inValues() ? string : null );
120 } catch (IOException e) {
121 throw new AccessorException(e);
129 public String getValue() throws AccessorException {
133 int length = UTF8StringSerializer.getLength(b);
134 byte[] bytes = new byte[length];
136 return new String(bytes, UTF8StringSerializer.UTF8);
137 } catch (IOException e) {
138 throw new AccessorException(e);
144 public void setValueNoflush(String string) throws AccessorException {
148 int stringByteLength = UTF8StringSerializer.getUTF8EncodingByteLength( string );
149 int lengthLength = UTF8StringSerializer.getSizeOfPutLength( stringByteLength );
150 int len = stringByteLength + lengthLength;
154 byte[] bytes = string.getBytes( UTF8StringSerializer.UTF8 );
155 UTF8StringSerializer.putLength(b, bytes.length);
159 ListenerEntry le = listeners;
161 StringInterestSet is = le.getInterestSet();
162 if (is.inNotifications()) {
163 Event e = new ValueAssigned( Bindings.STRING, is.inValues() ? string : null );
169 } catch (IOException e) {
170 throw new AccessorException(e);
177 public void setValue(String string) throws AccessorException {
181 setValueNoflush(string);
183 } catch (IOException e) {
184 throw new AccessorException(e);
191 public void setValueNoflush(Binding binding, Object newValue)
192 throws AccessorException {
194 String nv = ((StringBinding)binding).getValue(newValue);
196 } catch (BindingException e) {
197 throw new AccessorException(e);