001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.tool;
021    
022    import org.apache.commons.io.FileUtils;
023    import org.apache.commons.io.IOUtils;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    import javax.faces.context.FacesContext;
028    import javax.servlet.ServletContext;
029    import java.io.File;
030    import java.io.IOException;
031    import java.io.InputStream;
032    
033    /**
034     * @deprecated
035     */
036    @Deprecated
037    public class BuilderModel {
038    
039      private static final Log LOG = LogFactory.getLog(BuilderModel.class);
040    
041      private String page = "builder/index.jsp";
042      private String source;
043    
044    
045      public String getPage() {
046        return page;
047      }
048    
049      public void setPage(String page) {
050        this.page = page;
051      }
052    
053      public String getSource() {
054        return source;
055      }
056    
057      public void setSource(String source) {
058        this.source = source;
059      }
060    
061      public String open() {
062        if (LOG.isDebugEnabled()) {
063          LOG.debug("invoke!!!");
064        }
065    
066        return "enterPagename";
067      }
068    
069      public String loadPage() {
070        if (LOG.isDebugEnabled()) {
071          LOG.debug("invoke!!!");
072        }
073    
074        FacesContext facesContext = FacesContext.getCurrentInstance();
075    
076        InputStream stream = null;
077        try {
078          stream = facesContext.getExternalContext().getResourceAsStream(page);
079          source = IOUtils.toString(stream);
080        } catch (IOException e) {
081          LOG.error("", e);
082          return "error"; // TODO: error message
083        } finally {
084          IOUtils.closeQuietly(stream);
085        }
086    
087        return "viewSource";
088      }
089    
090      public String savePage() {
091        if (LOG.isDebugEnabled()) {
092          LOG.debug("invoke!!!");
093        }
094    
095        FacesContext facesContext = FacesContext.getCurrentInstance();
096        // TODO ServletContext ???
097        ServletContext servletContext =
098            (ServletContext) facesContext.getExternalContext().getContext();
099    
100        String realPath = servletContext.getRealPath(page);
101        try {
102          // TODO: use IOUtils.write when commons-io 1.1 is released
103          FileUtils.writeStringToFile(new File(realPath), source, System.getProperty("file.encoding"));
104        } catch (IOException e) {
105          LOG.error("", e);
106          return "error"; // TODO: error message
107        }
108    
109        return "viewSource";
110      }
111    
112    }