001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.api.asn1.util.Oid; 024import org.apache.directory.api.i18n.I18n; 025import org.apache.directory.api.ldap.model.constants.SchemaConstants; 026import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 027import org.apache.directory.api.util.Strings; 028 029 030/** 031 * A SyntaxChecker which verifies that a value is a numeric oid 032 * according to RFC 4512. 033 * <p> 034 * From RFC 4512 : 035 * <pre> 036 * numericoid = number 1*( DOT number ) 037 * number = DIGIT | ( LDIGIT 1*DIGIT ) 038 * DIGIT = %x30 | LDIGIT ; "0"-"9" 039 * LDIGIT = %x31-39 ; "1"-"9" 040 * DOT = %x2E ; period (".") 041 * </pre> 042 * 043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 044 */ 045@SuppressWarnings("serial") 046public final class NumericOidSyntaxChecker extends SyntaxChecker 047{ 048 /** 049 * A static instance of NumericOidSyntaxChecker 050 */ 051 public static final NumericOidSyntaxChecker INSTANCE = 052 new NumericOidSyntaxChecker( SchemaConstants.NUMERIC_OID_SYNTAX ); 053 054 /** 055 * A static Builder for this class 056 */ 057 public static final class Builder extends SCBuilder<NumericOidSyntaxChecker> 058 { 059 /** 060 * The Builder constructor 061 */ 062 private Builder() 063 { 064 super( SchemaConstants.NUMERIC_OID_SYNTAX ); 065 } 066 067 068 /** 069 * Create a new instance of NumericOidSyntaxChecker 070 * @return A new instance of NumericOidSyntaxChecker 071 */ 072 @Override 073 public NumericOidSyntaxChecker build() 074 { 075 return new NumericOidSyntaxChecker( oid ); 076 } 077 } 078 079 080 /** 081 * Creates a new instance of NumericOidSyntaxChecker. 082 * 083 * @param oid The OID to use for this SyntaxChecker 084 */ 085 private NumericOidSyntaxChecker( String oid ) 086 { 087 super( oid ); 088 } 089 090 091 /** 092 * @return An instance of the Builder for this class 093 */ 094 public static Builder builder() 095 { 096 return new Builder(); 097 } 098 099 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public boolean isValidSyntax( Object value ) 105 { 106 String strValue; 107 108 if ( value == null ) 109 { 110 if ( LOG.isDebugEnabled() ) 111 { 112 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) ); 113 } 114 115 return false; 116 } 117 118 if ( value instanceof String ) 119 { 120 strValue = ( String ) value; 121 } 122 else if ( value instanceof byte[] ) 123 { 124 strValue = Strings.utf8ToString( ( byte[] ) value ); 125 } 126 else 127 { 128 strValue = value.toString(); 129 } 130 131 if ( strValue.length() == 0 ) 132 { 133 if ( LOG.isDebugEnabled() ) 134 { 135 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) ); 136 } 137 138 return false; 139 } 140 141 // Just check that the value is a valid OID 142 boolean result = Oid.isOid( strValue ); 143 144 if ( LOG.isDebugEnabled() ) 145 { 146 if ( result ) 147 { 148 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) ); 149 } 150 else 151 { 152 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) ); 153 } 154 } 155 156 return result; 157 } 158}