001    /*
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.wcms;
025    
026    import java.io.File;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.ListIterator;
030    import java.util.Vector;
031    
032    import javax.servlet.ServletOutputStream;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    import org.jdom.Comment;
037    import org.jdom.DocType;
038    import org.jdom.Document;
039    import org.jdom.Element;
040    import org.jdom.Namespace;
041    import org.jdom.Text;
042    import org.jdom.input.SAXBuilder;
043    import org.jdom.output.Format;
044    import org.jdom.output.XMLOutputter;
045    
046    public class MCRWCMSGetStaticHTMLServlet extends MCRWCMSServlet {
047        private Namespace ns = Namespace.XML_NAMESPACE; // xml Namespace for the
048    
049        // language attribute lang
050        File hrefFile = null;
051    
052        char fs = File.separatorChar;
053    
054        String href = null;
055    
056        String lang = null;
057    
058        String error = null;
059    
060        List contentList;
061    
062        List contentOutput;
063    
064        boolean validXHTML = true;
065    
066        /**
067         * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
068         * methods.
069         * 
070         * @param request
071         *            servlet request
072         * @param response
073         *            servlet response
074         */
075        protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
076            href = request.getParameter("href");
077            lang = request.getParameter("lang");
078            contentOutput = new Vector();
079    
080            try {
081                SAXBuilder sax = new SAXBuilder();
082                Document ed = sax.build(getServletContext().getRealPath("") + fs + href);
083                Element begin = ed.getRootElement();
084                List contentElements = begin.getChildren("section");
085                Iterator contentElementsIterator = contentElements.iterator();
086    
087                while (contentElementsIterator.hasNext()) {
088                    Element content = (Element) contentElementsIterator.next();
089    
090                    if (content.getAttributeValue("lang", ns) != null) {
091                        // System.out.println("�bergebene Sprache: "+ lang + "
092                        // gefundene Sprache " + content.getAttributeValue("lang",
093                        // ns));
094                        contentList = content.getContent();
095                        validXHTML = true;
096    
097                        if (content.getAttributeValue("lang", ns).equals(lang)) {
098                            // System.out.println("drinn");
099                            for (int i = (contentList.size() - 1); i >= 0; i--) {
100                                Object o = contentList.get(i);
101    
102                                // String u = null;
103                                if (o instanceof Element) {
104                                    ((Element) o).detach();
105                                }
106    
107                                if (o instanceof Text) {
108                                    ((Text) o).detach();
109                                }
110    
111                                if (o instanceof Comment) {
112                                    ((Comment) o).detach();
113                                }
114    
115                                contentOutput.add(o);
116                            }
117    
118                            reverse(contentOutput);
119    
120                            // break;
121                        }
122                    }
123    
124                    contentList.clear();
125                }
126    
127                if ((contentOutput != null) && validXHTML) {
128                    Document doc = new Document();
129                    doc.setDocType(new DocType("html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"));
130    
131                    Element root = new Element("html");
132                    Element meta1 = new Element("meta").setAttribute("http-equiv", "Pragma").setAttribute("content", "no-cache");
133                    Element meta2 = new Element("meta").setAttribute("http-equiv", "Cache-Control").setAttribute("content", "no-cache, must-revalidate");
134                    root.addContent(new Element("head").addContent(meta1).addContent(meta2)).addContent(new Element("body").addContent(contentOutput));
135                    doc.setRootElement(root);
136    
137                    XMLOutputter xmlout = new XMLOutputter(Format.getRawFormat().setEncoding(OUTPUT_ENCODING));
138                    ServletOutputStream sos = response.getOutputStream();
139    
140                    response.setContentType("text/html; charset=" + OUTPUT_ENCODING);
141                    xmlout.output(doc, sos);
142                    sos.flush();
143                    sos.close();
144                }
145            } catch (Exception e) {
146                e.printStackTrace();
147            }
148        }
149    
150        public void reverse(List l) {
151            ListIterator fwd = l.listIterator();
152            ListIterator rev = l.listIterator(l.size());
153    
154            for (int i = 0, n = l.size() / 2; i < n; i++) {
155                Object tmp = fwd.next();
156                fwd.set(rev.previous());
157                rev.set(tmp);
158            }
159        }
160    }