001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.repository; 017 018import org.ametys.plugins.repository.AmetysRepositoryException; 019import org.ametys.plugins.repository.RepositoryConstants; 020import org.ametys.plugins.repository.query.expression.Expression; 021 022/** 023 * Constructs an {@link Expression} testing the content type. 024 */ 025public class ContentTypeOrMixinExpression implements Expression 026{ 027 private Operator _operator; 028 private String[] _values; 029 030 /** 031 * Creates the expression. 032 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 033 * @param value the content type value 034 */ 035 public ContentTypeOrMixinExpression(Operator operator, String... value) 036 { 037 if (Operator.EQ != operator && Operator.NE != operator) 038 { 039 throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression."); 040 } 041 042 _operator = operator; 043 _values = value; 044 } 045 046 public String build() 047 { 048 StringBuilder xpath = new StringBuilder(); 049 050 if (_values.length == 1) 051 { 052 xpath.append("("); 053 xpath.append('@').append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(':').append(DefaultContent.METADATA_CONTENTTYPE) 054 .append(' ').append(_operator).append(" '").append(_values[0].replaceAll("'", "''")).append("'"); 055 056 xpath.append(" or "); 057 058 xpath.append('@').append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(':').append(DefaultContent.METADATA_MIXINCONTENTTYPES) 059 .append(' ').append(_operator).append(" '").append(_values[0].replaceAll("'", "''")).append("'"); 060 xpath.append(")"); 061 return xpath.toString(); 062 } 063 064 xpath.append(Operator.EQ.equals(_operator) ? '(' : "not("); 065 066 for (int i = 0; i < _values.length; i++) 067 { 068 if (i > 0) 069 { 070 xpath.append(" or "); 071 } 072 073 xpath.append('@').append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(':').append(DefaultContent.METADATA_CONTENTTYPE) 074 .append(" = '").append(_values[i].replaceAll("'", "''")).append("'"); 075 xpath.append(" or "); 076 xpath.append('@').append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(':').append(DefaultContent.METADATA_MIXINCONTENTTYPES) 077 .append(" = '").append(_values[i].replaceAll("'", "''")).append("'"); 078 } 079 080 xpath.append(')'); 081 082 return xpath.toString(); 083 } 084}