8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: NightlyIndexHtml.java,v 1.5 2008/06/25 05:44:13 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Portions Copyrighted [2011] [ForgeRock AS]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.tools.nightly;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.DataInputStream;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.File;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.FileWriter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.net.URL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.net.URLConnection;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.net.MalformedURLException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.text.MessageFormat;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Nightly Preparation Implementation.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class NightlyIndexHtml {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private final static String TEMPLATE =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "\n&raquo;&nbsp;<a href=\"{0}\">{0}</a><br />";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private final static String NEW_ENTRY = "<!-- new entry -->";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private final static String LATEST = "<a href=\"{0}\">latest</a>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates a new instance of <code>NightlyIndexHtml</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private NightlyIndexHtml() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static void create(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String baseDir,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String timestamp,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String indexURL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws IOException, MalformedURLException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URL url = new URL(indexURL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLConnection conn = url.openConnection();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DataInputStream dis = new DataInputStream(conn.getInputStream());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder buff = new StringBuilder();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String line = dis.readLine();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (line != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buff.append(line).append("\n");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster line = dis.readLine();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster dis.close();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String content = buff.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] param = {timestamp};
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster content = content.replaceAll(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NEW_ENTRY, NEW_ENTRY + MessageFormat.format(TEMPLATE, param));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster content = content.replaceAll(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "<a href=.+?>latest</a>",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MessageFormat.format(LATEST, param));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writeToFile(baseDir + "/top.index.html", content);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static void writeToFile(String filename, String content)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws IOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filename != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File fileHandle = new File(filename);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FileWriter out = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out = new FileWriter(filename);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.write(content);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (out != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.close();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void main(String[] args) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster create(args[0], args[1], args[2]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster e.printStackTrace();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}