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    public class BuilderModel {
032    
033      private static final Log LOG = LogFactory.getLog(BuilderModel.class);
034    
035      private String page = "builder/index.jsp";
036      private String source;
037    
038    
039      public String getPage() {
040        return page;
041      }
042    
043      public void setPage(String page) {
044        this.page = page;
045      }
046    
047      public String getSource() {
048        return source;
049      }
050    
051      public void setSource(String source) {
052        this.source = source;
053      }
054    
055      public String open() {
056        if (LOG.isDebugEnabled()) {
057          LOG.debug("invoke!!!");
058        }
059    
060        return "enterPagename";
061      }
062    
063      public String loadPage() {
064        if (LOG.isDebugEnabled()) {
065          LOG.debug("invoke!!!");
066        }
067    
068        FacesContext facesContext = FacesContext.getCurrentInstance();
069    
070        InputStream stream = null;
071        try {
072          stream = facesContext.getExternalContext().getResourceAsStream(page);
073          source = IOUtils.toString(stream);
074        } catch (IOException e) {
075          LOG.error("", e);
076          return "error"; // TODO: error message
077        } finally {
078          IOUtils.closeQuietly(stream);
079        }
080    
081        return "viewSource";
082      }
083    
084      public String savePage() {
085        if (LOG.isDebugEnabled()) {
086          LOG.debug("invoke!!!");
087        }
088    
089        FacesContext facesContext = FacesContext.getCurrentInstance();
090        // TODO ServletContext ???
091        ServletContext servletContext =
092            (ServletContext) facesContext.getExternalContext().getContext();
093    
094        String realPath = servletContext.getRealPath(page);
095        try {
096          // TODO: use IOUtils.write when commons-io 1.1 is released
097          FileUtils.writeStringToFile(new File(realPath), source, System.getProperty("file.encoding"));
098        } catch (IOException e) {
099          LOG.error("", e);
100          return "error"; // TODO: error message
101        }
102    
103        return "viewSource";
104      }
105    
106    }