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.binding;
14 import java.util.IdentityHashMap;
16 import java.util.regex.Pattern;
18 import org.simantics.databoard.accessor.reference.ChildReference;
19 import org.simantics.databoard.binding.error.BindingException;
20 import org.simantics.databoard.binding.error.RuntimeBindingException;
21 import org.simantics.databoard.binding.impl.BindingPrintContext;
22 import org.simantics.databoard.type.StringType;
23 import org.simantics.databoard.util.IdentityPair;
24 import org.simantics.databoard.util.Range;
27 * This is a binding of String Type and a Java Object.
30 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
32 public abstract class StringBinding extends Binding {
34 public StringBinding(StringType type) {
38 public StringType type() {
39 return (StringType) type;
42 public abstract Object create(String value) throws BindingException;
44 public Object createUnchecked(String value) throws RuntimeBindingException {
47 } catch (BindingException e) {
48 throw new RuntimeBindingException(e);
53 public void readFrom(Binding srcBinding, Object src, Object dst)
54 throws BindingException {
55 String v = ((StringBinding)srcBinding).getValue(src);
59 public abstract String getValue(Object o) throws BindingException;
60 public abstract void setValue(Object o, String newValue) throws BindingException;
61 public abstract boolean isInstance(Object obj);
64 public void accept(Visitor1 v, Object obj) {
69 public <T> T accept(Visitor<T> v) {
74 * Assert obj is a valid StringType
78 * 2. mime type is valid
81 * @throws BindingException
84 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
85 if (!isInstance(obj)) throw new BindingException("Not a correct instance");
87 StringType type = type();
88 Pattern p = type.getCompiledPattern();
89 String mimeType = type.getMimeType();
90 Range length = type.getLength();
91 if (p==null && length==null && mimeType==null) return;
92 String str = getValue(obj);
94 // 1. Assert the pattern is valid
95 if (p!=null && !p.matcher(str).matches()) {
96 throw new BindingException("The value ("+str+") doesn't match the pattern ("+p.toString()+").");
99 // 2. Assert the mime type is valid
100 if (mimeType!=null) {
102 // MimeType mt = new MimeType(mimeType);
103 // if (!mt.match(str))
104 // throw new BindingException("The object is not "+mimeType);
105 // } catch (MimeTypeParseException mtpe) {
106 // throw new BindingException(mtpe);
110 // 3. Assert the length is valid
111 if (length!=null && !length.contains(str.length())) {
112 throw new BindingException("Length ("+str.length()+") doesn't fit into the range "+length);
117 public int deepCompare(Object o1, Object o2,
118 Set<IdentityPair<Object, Object>> compareHistory)
119 throws BindingException {
120 String v1 = getValue(o1);
121 String v2 = getValue(o2);
122 return v1.compareTo(v2);
126 public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
127 return getValue(value).hashCode();
131 protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
133 ctx.b.append(getValue(value));
138 public Binding getComponentBinding(ChildReference path) {
139 if (path==null) return this;
140 throw new IllegalArgumentException();
144 public int getComponentCount() {
149 public Binding getComponentBinding(int index) {
150 throw new IllegalArgumentException();