Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.clearsilver.jsilver.data;
     18 
     19 import com.google.clearsilver.jsilver.autoescape.EscapeMode;
     20 
     21 import java.util.Iterator;
     22 
     23 /**
     24  * Data wrapper that prevents modifying the delegated Data node or its tree.
     25  */
     26 public class UnmodifiableData extends DelegatedData {
     27 
     28   private static final String MODIFICATION_ERROR_MSG = "Cannot modify this Data object.";
     29 
     30   public UnmodifiableData(Data delegate) {
     31     super(delegate);
     32   }
     33 
     34   @Override
     35   protected DelegatedData newInstance(Data newDelegate) {
     36     return newDelegate == null ? null : new UnmodifiableData(newDelegate);
     37   }
     38 
     39   @Override
     40   public void copy(Data from) {
     41     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     42   }
     43 
     44   @Override
     45   public void copy(String toPath, Data from) {
     46     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     47   }
     48 
     49   /**
     50    * {@link #createChild} calls {@link #getChild} and throws {@link UnsupportedOperationException}
     51    * if no object was found.
     52    */
     53   @Override
     54   public Data createChild(String path) {
     55     // Check if child already exists
     56     Data child = getChild(path);
     57 
     58     if (child == null) {
     59       // If the child described by path does not exist we throw
     60       // an exception as we cannot create a new one.
     61       throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     62     }
     63     return child;
     64   }
     65 
     66   protected class UnmodifiableIterator extends DelegatedIterator {
     67     UnmodifiableIterator(Iterator<? extends Data> iterator) {
     68       super(iterator);
     69     }
     70 
     71     public void remove() {
     72       throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     73     }
     74   }
     75 
     76   /**
     77    * Override in order to not allow modifying children with remove().
     78    */
     79   @Override
     80   protected Iterator<DelegatedData> newChildIterator() {
     81     return new UnmodifiableIterator(getDelegate().getChildren().iterator());
     82   }
     83 
     84   // Below we disable modification operations.
     85 
     86   @Override
     87   public void setSymlink(String sourcePath, Data destination) {
     88     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     89   }
     90 
     91   @Override
     92   public void setSymlink(String sourcePath, String destinationPath) {
     93     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     94   }
     95 
     96   @Override
     97   public void setSymlink(Data symLink) {
     98     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
     99   }
    100 
    101   @Override
    102   public void setAttribute(String key, String value) {
    103     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
    104   }
    105 
    106   @Override
    107   public void removeTree(String path) {
    108     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
    109   }
    110 
    111   @Override
    112   public void setValue(String path, String value) {
    113     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
    114   }
    115 
    116   @Override
    117   public void setValue(String value) {
    118     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
    119   }
    120 
    121   @Override
    122   public void setEscapeMode(EscapeMode mode) {
    123     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
    124   }
    125 }
    126