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.plugins.repository.query.expression; 017 018import org.ametys.plugins.repository.RepositoryConstants; 019 020/** 021 * Constructs an {@link Expression} testing the existence of a metadata. 022 */ 023public class MetadataExpression implements Expression 024{ 025 private String _metadata; 026 private String _path; 027 private ExpressionContext _context; 028 029 /** 030 * Creates the comparison Expression. 031 * @param metadata the metadata name 032 */ 033 public MetadataExpression(String metadata) 034 { 035 this(metadata, ExpressionContext.newInstance()); 036 } 037 038 /** 039 * Creates the comparison Expression. 040 * @param metadata the metadata name 041 * @param context the context of the expression 042 */ 043 public MetadataExpression(String metadata, ExpressionContext context) 044 { 045 int index = metadata.lastIndexOf('/'); 046 boolean isComposite = index != -1; 047 _path = isComposite ? metadata.substring(0, index) : null; 048 _metadata = isComposite ? metadata.substring(index + 1) : metadata; 049 _context = context; 050 } 051 052 public String build() 053 { 054 StringBuilder buff = new StringBuilder(); 055 String repositoryPrefix = _context.internal() ? RepositoryConstants.NAMESPACE_PREFIX_INTERNAL : RepositoryConstants.NAMESPACE_PREFIX; 056 057 if (_context.unversioned()) 058 { 059 buff.append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(":unversioned/"); 060 } 061 if (_path != null) 062 { 063 String[] parts = _path.split("/"); 064 for (String part : parts) 065 { 066 if ("*".equals(part)) 067 { 068 buff.append(part).append('/'); 069 } 070 else 071 { 072 buff.append(repositoryPrefix).append(':').append(part).append('/'); 073 } 074 075 } 076 } 077 buff.append('@').append(repositoryPrefix).append(':').append(_metadata); 078 079 return buff.toString(); 080 } 081}