View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.compiler;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELContext;
24  import javax.el.ELException;
25  import javax.el.ExpressionFactory;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  
29  import org.apache.myfaces.view.facelets.el.ELText;
30  
31  final class AttributeInstruction implements Instruction
32  {
33      private final String _alias;
34  
35      private final String _attr;
36  
37      private final ELText _txt;
38  
39      public AttributeInstruction(String alias, String attr, ELText txt)
40      {
41          _alias = alias;
42          _attr = attr;
43          _txt = txt;
44      }
45  
46      public void write(FacesContext context) throws IOException
47      {
48          ResponseWriter out = context.getResponseWriter();
49          try
50          {
51              out.writeAttribute(_attr, _txt.toString(context.getELContext()), null);
52          }
53          catch (ELException e)
54          {
55              throw new ELException(_alias + ": " + e.getMessage(), e.getCause());
56          }
57          catch (Exception e)
58          {
59              throw new ELException(_alias + ": " + e.getMessage(), e);
60          }
61      }
62  
63      public Instruction apply(ExpressionFactory factory, ELContext ctx)
64      {
65          ELText nt = _txt.apply(factory, ctx);
66          if (nt == _txt)
67          {
68              return this;
69          }
70  
71          return new AttributeInstruction(_alias, _attr, nt);
72      }
73  
74      public boolean isLiteral()
75      {
76          return _txt.isLiteral();
77      }
78  }