--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.codec;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import java.util.regex.Pattern;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+
+class CompiledPatternContext {
+
+ private final Pattern pattern;
+ private final String errorMessage;
+
+ CompiledPatternContext(final PatternConstraint yangConstraint) {
+ pattern = Pattern.compile("^" + yangConstraint.getRegularExpression() + "$");
+ final String yangMessage = yangConstraint.getErrorMessage();
+ if (Strings.isNullOrEmpty(yangMessage)) {
+ errorMessage = "Value %s does not match regular expression <" + pattern.pattern() + ">";
+ } else {
+ errorMessage = yangMessage;
+ }
+ }
+
+ public void validate(final String s) {
+ Preconditions.checkArgument(pattern.matcher(s).matches(), errorMessage, s);
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.codec;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.regex.PatternSyntaxException;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class StringPatternCheckingCodec extends StringStringCodec {
+
+ private static final Logger LOG = LoggerFactory.getLogger(StringPatternCheckingCodec.class);
+ private final Collection<CompiledPatternContext> patterns;
+
+ protected StringPatternCheckingCodec(final StringTypeDefinition typeDef) {
+ super(typeDef);
+ patterns = new ArrayList<>(typeDef.getPatternConstraints().size());
+ for (final PatternConstraint yangPattern : typeDef.getPatternConstraints()) {
+ try {
+ patterns.add(new CompiledPatternContext(yangPattern));
+ } catch (final PatternSyntaxException e) {
+ LOG.debug("Unable to compile {} pattern, excluding it from validation.", yangPattern, e);
+ }
+ }
+ }
+
+ @Override
+ protected void validate(final String s) {
+ super.validate(s);
+ for (final CompiledPatternContext pattern : patterns) {
+ pattern.validate(s);
+ }
+ }
+
+}
\ No newline at end of file
class StringStringCodec extends TypeDefinitionAwareCodec<String, StringTypeDefinition> implements
StringCodec<String> {
- protected StringStringCodec(final Optional<StringTypeDefinition> typeDef) {
- super(typeDef, String.class);
+ protected StringStringCodec(final StringTypeDefinition typeDef) {
+ super(Optional.of(typeDef), String.class);
+ typeDef.getLengthConstraints();
}
- static TypeDefinitionAwareCodec<?,StringTypeDefinition> from(final StringTypeDefinition normalizedType) {
- return new StringStringCodec(Optional.fromNullable(normalizedType));
+ static TypeDefinitionAwareCodec<?, StringTypeDefinition> from(final StringTypeDefinition normalizedType) {
+ if (normalizedType.getPatternConstraints().isEmpty()) {
+ return new StringStringCodec(normalizedType);
+ }
+
+ return new StringPatternCheckingCodec(normalizedType);
}
@Override
public final String deserialize(final String stringRepresentation) {
- return stringRepresentation == null ? "" : stringRepresentation;
+ if (stringRepresentation == null) {
+ // FIXME: These seems buggy, but someone may be using this behaviour
+ return "";
+ }
+ validate(stringRepresentation);
+ return stringRepresentation;
}
@Override
public final String serialize(final String data) {
return data == null ? "" : data;
}
+
+ protected void validate(final String s) {
+
+ }
}
\ No newline at end of file
@Deprecated
public static class StringCodecStringImpl extends StringStringCodec {
protected StringCodecStringImpl(final Optional<StringTypeDefinition> typeDef) {
- super(typeDef);
+ super(typeDef.get());
}
}