public static String getFooter(String context) { try { FileReader fileReader = new FileReader(findResourceOnFileSystem("servletResponseTemplate.htm")); BufferedReader buffread = new BufferedReader(fileReader); String templateFile = "", line; StringBuffer SBreader = new StringBuffer(); while ((line = buffread.readLine()) != null) { SBreader.append(line).append("\n"); } fileReader.close(); buffread.close(); templateFile = SBreader.toString(); templateFile = templateFile.replaceAll( "BOTTOMGRAPHIC", CommonConfiguration.getURLToFooterGraphic(context)); int end_header = templateFile.indexOf("INSERT_HERE"); return (templateFile.substring(end_header + 11)); } catch (Exception e) { // out.println("I couldn't find the template file to read from."); e.printStackTrace(); String error = "An error occurred while attempting to read from an HTML template file. This probably will not affect the success of the operation you were trying to perform.</p></body></html>"; return error; } }
// Logs a new entry in the library RSS file public static synchronized void addRSSEntry( String title, String link, String description, File rssFile) { // File rssFile=new File("nofile.xml"); try { System.out.println("Looking for RSS file: " + rssFile.getCanonicalPath()); if (rssFile.exists()) { SAXReader reader = new SAXReader(); Document document = reader.read(rssFile); Element root = document.getRootElement(); Element channel = root.element("channel"); List items = channel.elements("item"); int numItems = items.size(); items = null; if (numItems > 9) { Element removeThisItem = channel.element("item"); channel.remove(removeThisItem); } Element newItem = channel.addElement("item"); Element newTitle = newItem.addElement("title"); Element newLink = newItem.addElement("link"); Element newDescription = newItem.addElement("description"); newTitle.setText(title); newDescription.setText(description); newLink.setText(link); Element pubDate = channel.element("pubDate"); pubDate.setText((new java.util.Date()).toString()); // now save changes FileWriter mywriter = new FileWriter(rssFile); OutputFormat format = OutputFormat.createPrettyPrint(); format.setLineSeparator(System.getProperty("line.separator")); XMLWriter writer = new XMLWriter(mywriter, format); writer.write(document); writer.close(); } } catch (IOException ioe) { System.out.println("ERROR: Could not find the RSS file."); ioe.printStackTrace(); } catch (DocumentException de) { System.out.println("ERROR: Could not read the RSS file."); de.printStackTrace(); } catch (Exception e) { System.out.println("Unknown exception trying to add an entry to the RSS file."); e.printStackTrace(); } }
// Loads a String of text from a specified file. // This is generally used to load an email template for automated emailing public static String getText(String fileName) { try { StringBuffer SBreader = new StringBuffer(); String line; FileReader fileReader = new FileReader(findResourceOnFileSystem(fileName)); BufferedReader buffread = new BufferedReader(fileReader); while ((line = buffread.readLine()) != null) { SBreader.append(line + "\n"); } line = SBreader.toString(); fileReader.close(); buffread.close(); return line; } catch (Exception e) { e.printStackTrace(); return ""; } }
public static String getHeader(HttpServletRequest request) { try { FileReader fileReader = new FileReader(findResourceOnFileSystem("servletResponseTemplate.htm")); BufferedReader buffread = new BufferedReader(fileReader); String templateFile = "", line; StringBuffer SBreader = new StringBuffer(); while ((line = buffread.readLine()) != null) { SBreader.append(line).append("\n"); } fileReader.close(); buffread.close(); templateFile = SBreader.toString(); String context = getContext(request); // process the CSS string templateFile = templateFile.replaceAll( "CSSURL", CommonConfiguration.getCSSURLLocation(request, context)); // set the top header graphic templateFile = templateFile.replaceAll( "TOPGRAPHIC", CommonConfiguration.getURLToMastheadGraphic(context)); int end_header = templateFile.indexOf("INSERT_HERE"); return (templateFile.substring(0, end_header)); } catch (Exception e) { // out.println("I couldn't find the template file to read from."); e.printStackTrace(); String error = "<html><body><p>An error occurred while attempting to read from the template file servletResponseTemplate.htm. This probably will not affect the success of the operation you were trying to perform."; return error; } }
// Logs a new ATOM entry public static synchronized void addATOMEntry( String title, String link, String description, File atomFile, String context) { try { if (atomFile.exists()) { // System.out.println("ATOM file found!"); /** Namespace URI for content:encoded elements */ String CONTENT_NS = "http://www.w3.org/2005/Atom"; /** Parses RSS or Atom to instantiate a SyndFeed. */ SyndFeedInput input = new SyndFeedInput(); /** Transforms SyndFeed to RSS or Atom XML. */ SyndFeedOutput output = new SyndFeedOutput(); // Load the feed, regardless of RSS or Atom type SyndFeed feed = input.build(new XmlReader(atomFile)); // Set the output format of the feed feed.setFeedType("atom_1.0"); List<SyndEntry> items = feed.getEntries(); int numItems = items.size(); if (numItems > 9) { items.remove(0); feed.setEntries(items); } SyndEntry newItem = new SyndEntryImpl(); newItem.setTitle(title); newItem.setLink(link); newItem.setUri(link); SyndContent desc = new SyndContentImpl(); desc.setType("text/html"); desc.setValue(description); newItem.setDescription(desc); desc.setType("text/html"); newItem.setPublishedDate(new java.util.Date()); List<SyndCategory> categories = new ArrayList<SyndCategory>(); if (CommonConfiguration.getProperty("htmlTitle", context) != null) { SyndCategory category2 = new SyndCategoryImpl(); category2.setName(CommonConfiguration.getProperty("htmlTitle", context)); categories.add(category2); } newItem.setCategories(categories); if (CommonConfiguration.getProperty("htmlAuthor", context) != null) { newItem.setAuthor(CommonConfiguration.getProperty("htmlAuthor", context)); } items.add(newItem); feed.setEntries(items); feed.setPublishedDate(new java.util.Date()); FileWriter writer = new FileWriter(atomFile); output.output(feed, writer); writer.toString(); } } catch (IOException ioe) { System.out.println("ERROR: Could not find the ATOM file."); ioe.printStackTrace(); } catch (Exception e) { System.out.println("Unknown exception trying to add an entry to the ATOM file."); e.printStackTrace(); } }