All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.java.org.sakaiproject.entitybroker.util.TemplateParseUtil Maven / Gradle / Ivy

The newest version!
/**
 * $Id$
 * $URL$
 * TemplateParseUtil.java - entity-broker - Apr 10, 2008 9:57:29 AM - azeckoski
 **************************************************************************
 * Copyright (c) 2008, 2009 The Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.sakaiproject.entitybroker.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.sakaiproject.entitybroker.EntityView;
import org.sakaiproject.entitybroker.entityprovider.extension.Formats;

/**
 * Utility class to handle the URL template parsing (entity template parsing)
 * 
 * @author Aaron Zeckoski ([email protected])
 */
public class TemplateParseUtil {

   public static final char SEPARATOR = EntityView.SEPARATOR;
   public static final char PERIOD = EntityView.PERIOD;
   public static final String BRACES = "[\\{\\}]";

   /**
    * The entity prefix marker (Example value: "myprefix")
    */
   public static final String PREFIX = EntityView.PREFIX;
   /**
    * The entity ID marker (Example value: "123")
    */
   public static final String ID = EntityView.ID;
   /**
    * The entity extension (format) marker (Example value: "xml")
    */
   public static final String EXTENSION = "extension";
   /**
    * The extension with a period in front marker (Example value: ".xml")
    */
   public static final String DOT_EXTENSION = "dot-extension";
   /**
    * The value in the query string (without a leading ?), '' if non available (Example value: "auto=true")
    */
   public static final String QUERY_STRING = "query-string";
   /**
    * The value in the query string (with a leading ?), '' if non available (Example value: "?auto=true")
    */
   public static final String QUESTION_QUERY_STRING = "question-query-string";
   public static final String PREFIX_VARIABLE = "{"+PREFIX+"}";
   public static final String TEMPLATE_PREFIX = SEPARATOR + PREFIX_VARIABLE;
   public static final String DIRECT_PREFIX = EntityView.DIRECT_PREFIX;
   public static final String DIRECT_PREFIX_SLASH = DIRECT_PREFIX + SEPARATOR;


   /**
    * Defines the valid chars for a replacement variable
    */
   public static final String VALID_VAR_CHARS = "[\\p{L}}\\p{N}\\\\(\\\\)\\+\\*\\.\\-_=,:;!~@% ]";
   /**
    * Defines the valid chars for a parser input (e.g. entity reference)
    */
   public static final String VALID_INPUT_CHARS = "[\\p{L}\\p{N}\\\\\\(\\\\)\\+\\*\\.\\-_=,:;!~@% "+SEPARATOR+"]";
   /**
    * Defines the valid chars for a template
    */
   public static final String VALID_TEMPLATE_CHARS = "[\\p{L}\\p{N}\\\\\\(\\\\)\\+\\*\\.\\-_=,:;&!~@%"+SEPARATOR+"\\{\\}]";
   /**
    * Defines the valid template chars for an outgoing template (allows ?)
    */
   public static final String VALID_TEMPLATE_CHARS_OUTGOING = "[\\p{L}\\p{N}\\\\\\(\\\\)\\+\\*\\.\\-_=,:;&!~@%"+SEPARATOR+"\\{\\}\\?]";


   /**
    * Defines the parse template for the "list" operation,
    * return a list of all records,
    * typically /{prefix}
    */
   public static final String TEMPLATE_LIST = EntityView.VIEW_LIST;
   /**
    * Defines the parse template for the "show" operation,
    * access a record OR POST operations related to a record,
    * typically /{prefix}/{id}
    */
   public static final String TEMPLATE_SHOW = EntityView.VIEW_SHOW;
   /**
    * Defines the parse template for the "new" operation,
    * return a form for creating a new record,
    * typically /{prefix}/new
    */
   public static final String TEMPLATE_NEW  = EntityView.VIEW_NEW;
   /**
    * Defines the parse template for the "edit" operation,
    * access the data to modify a record,
    * typically /{prefix}/{id}/edit
    */
   public static final String TEMPLATE_EDIT = EntityView.VIEW_EDIT;
   /**
    * Defines the parse template for the "delete" operation,
    * access the data to remove a record,
    * typically /{prefix}/{id}/delete
    */
   public static final String TEMPLATE_DELETE = EntityView.VIEW_DELETE;

   /**
    * Defines the order that parse templates will be processed in and
    * the set of parse template types (keys) which must be defined,
    * the first one to match will be used when parsing in a path
    */
   public static final String[] PARSE_TEMPLATE_KEYS = {
      TEMPLATE_EDIT,
      TEMPLATE_DELETE,
      TEMPLATE_NEW,
      TEMPLATE_SHOW,
      TEMPLATE_LIST
   };

   /**
    * Stores the preloaded default templates
    */
   public static final List