]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryString.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryString.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryString.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryString.java
new file mode 100644 (file)
index 0000000..4f730c2
--- /dev/null
@@ -0,0 +1,202 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.accessor.binary;
+
+import java.io.IOException;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.StringAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.error.ReferenceException;\r
+import org.simantics.databoard.accessor.event.Event;\r
+import org.simantics.databoard.accessor.event.ValueAssigned;\r
+import org.simantics.databoard.accessor.file.FileStringAccessor;\r
+import org.simantics.databoard.accessor.impl.AccessorParams;\r
+import org.simantics.databoard.accessor.impl.ListenerEntry;\r
+import org.simantics.databoard.accessor.interestset.StringInterestSet;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.StringBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.StringType;\r
+import org.simantics.databoard.util.binary.Blob;\r
+import org.simantics.databoard.util.binary.Endian;\r
+import org.simantics.databoard.util.binary.UTF8;\r
+
+public class BinaryString extends BinaryObject implements StringAccessor, FileStringAccessor {
+
+       public BinaryString(BinaryObject parent, Blob blob, StringType type, AccessorParams params) 
+       throws AccessorConstructionException {
+               super(parent, blob, type, params);
+       }
+
+       @Override
+       public StringType type() {
+               return (StringType) type;
+       }
+
+       @Override
+       Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
+               Event rollback = makeRollback ? new ValueAssigned( Bindings.STRING, getValue() ) : null;                \r
+               if (e instanceof ValueAssigned) {\r
+                       ValueAssigned va = (ValueAssigned) e;\r
+                       if (va.newValue == null) throw new AccessorException("String value expected, got null");                        \r
+                       setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());\r
+                       return rollback;\r
+               } else {\r
+                       throw new AccessorException("Cannot apply "+e.getClass().getName()+" to String");\r
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <T extends Accessor> T getComponent(ChildReference reference)
+                       throws AccessorConstructionException {
+               if (reference==null) return (T) this;           
+               throw new ReferenceException(reference.getClass()+" is not a subreference of StringType");      
+       }
+       
+       @Override
+       public Object getValue(Binding binding) throws AccessorException {\r
+               try {
+                       StringBinding bb = (StringBinding) binding; 
+                       String v = getValue();
+                       return bb.create(v);
+               } catch(BindingException e) {           
+                       throw new AccessorException(e);
+               }
+       }\r
+// MODIFIED UTF-8      \r
+       @Override\r
+       public String getValue() throws AccessorException {\r
+               assert b.isOpen();\r
+               readLock();\r
+               try {\r
+                       b.position(0L);         \r
+                       \r
+                       int utflen = Endian.readDynamicUInt32(b);\r
+                       return UTF8.readModifiedUTF(b, utflen);\r
+               } catch (IOException e) {\r
+                       throw new AccessorException(e);\r
+               } finally {\r
+                       readUnlock();\r
+               }\r
+       }\r
+       \r
+       public void setValueNoflush(String string) throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();\r
+               try {\r
+                       // Write\r
+                       b.position(0);\r
+                       int strlen = UTF8.getModifiedUTF8EncodingByteLength(string);\r
+                       int lenlen = Endian.getDynamicUInt32Length(strlen);\r
+                       b.setLength(strlen+lenlen);\r
+                       Endian.writeDynamicUInt32(b, strlen);\r
+                       UTF8.writeModifiedUTF(b, string);\r
+                       \r
+                       // Notify\r
+                       ListenerEntry le = listeners;\r
+                       while (le!=null) {\r
+                               StringInterestSet is = le.getInterestSet();\r
+                               if (is.inNotifications()) {                                     \r
+                                       Event e = new ValueAssigned( Bindings.STRING, is.inValues() ? string : null );\r
+                                       emitEvent(le, e);\r
+                               }\r
+                               le = le.next;\r
+                       }\r
+                       \r
+               } catch (IOException e) {\r
+                       throw new AccessorException(e);\r
+               } finally {\r
+                       writeUnlock();\r
+               }\r
+       }\r
+       
+/* REAL UTF-8\r
+       @Override\r
+       public String getValue() throws AccessorException {\r
+               readLock();\r
+               try {\r
+                       b.position(0L);\r
+                       int length = UTF8StringSerializer.getLength(b); \r
+                       byte[] bytes = new byte[length];\r
+                       b.readFully(bytes);\r
+                       return new String(bytes, UTF8StringSerializer.UTF8);\r
+               } catch (IOException e) {\r
+                       throw new AccessorException(e);\r
+               } finally {\r
+                       readUnlock();\r
+               }\r
+       }\r
+       
+       public void setValueNoflush(String string) throws AccessorException {\r
+               writeLock();
+               try {
+                       // Write
+                       int stringByteLength = UTF8StringSerializer.getUTF8EncodingByteLength( string ); 
+                       int lengthLength = UTF8StringSerializer.getSizeOfPutLength( stringByteLength );
+                       int len = stringByteLength + lengthLength; 
+                       
+                       b.position(0);
+                       b.setLength(len);
+                       byte[] bytes = string.getBytes( UTF8StringSerializer.UTF8 );
+                       UTF8StringSerializer.putLength(b, bytes.length);
+                       b.put(bytes);                   
+                       
+                       // Notify
+                       ListenerEntry le = listeners;
+                       while (le!=null) {
+                               StringInterestSet is = le.getInterestSet();
+                               if (is.inNotifications()) {                                     
+                                       Event e = new ValueAssigned( Bindings.STRING, is.inValues() ? string : null );
+                                       emitEvent(le, e);
+                               }
+                               le = le.next;
+                       }
+                       
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+*/     
+       @Override
+       public void setValue(String string) throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       setValueNoflush(string);
+                       b.flush();
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+       
+       @Override
+       public void setValueNoflush(Binding binding, Object newValue)
+                       throws AccessorException {
+               try {
+                       String nv = ((StringBinding)binding).getValue(newValue);
+                       setValueNoflush(nv);
+               } catch (BindingException e) {
+                       throw new AccessorException(e);
+               }
+       }
+       
+}
+