• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
UnmodifiableData(Data delegate)30   public UnmodifiableData(Data delegate) {
31     super(delegate);
32   }
33 
34   @Override
newInstance(Data newDelegate)35   protected DelegatedData newInstance(Data newDelegate) {
36     return newDelegate == null ? null : new UnmodifiableData(newDelegate);
37   }
38 
39   @Override
copy(Data from)40   public void copy(Data from) {
41     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
42   }
43 
44   @Override
copy(String toPath, Data from)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
createChild(String path)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 {
UnmodifiableIterator(Iterator<? extends Data> iterator)67     UnmodifiableIterator(Iterator<? extends Data> iterator) {
68       super(iterator);
69     }
70 
remove()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
newChildIterator()80   protected Iterator<DelegatedData> newChildIterator() {
81     return new UnmodifiableIterator(getDelegate().getChildren().iterator());
82   }
83 
84   // Below we disable modification operations.
85 
86   @Override
setSymlink(String sourcePath, Data destination)87   public void setSymlink(String sourcePath, Data destination) {
88     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
89   }
90 
91   @Override
setSymlink(String sourcePath, String destinationPath)92   public void setSymlink(String sourcePath, String destinationPath) {
93     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
94   }
95 
96   @Override
setSymlink(Data symLink)97   public void setSymlink(Data symLink) {
98     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
99   }
100 
101   @Override
setAttribute(String key, String value)102   public void setAttribute(String key, String value) {
103     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
104   }
105 
106   @Override
removeTree(String path)107   public void removeTree(String path) {
108     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
109   }
110 
111   @Override
setValue(String path, String value)112   public void setValue(String path, String value) {
113     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
114   }
115 
116   @Override
setValue(String value)117   public void setValue(String value) {
118     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
119   }
120 
121   @Override
setEscapeMode(EscapeMode mode)122   public void setEscapeMode(EscapeMode mode) {
123     throw new UnsupportedOperationException(MODIFICATION_ERROR_MSG);
124   }
125 }
126