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