You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CopyAndInlineStylesheet.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.internal.tools.ant.taskdefs;
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.FileReader;
  18. import java.io.IOException;
  19. import java.io.PrintStream;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.taskdefs.Mkdir;
  23. public class CopyAndInlineStylesheet extends Task {
  24. private File file;
  25. public void setFile(String file) {
  26. this.file = project.resolveFile(file);
  27. }
  28. private File todir;
  29. public void setTodir(String todir) {
  30. this.todir = project.resolveFile(todir);
  31. }
  32. public void execute() throws BuildException {
  33. try {
  34. if (todir == null) {
  35. throw new BuildException("must set 'todir' attribute");
  36. }
  37. if (file == null) {
  38. throw new BuildException("must set 'file' attribute");
  39. }
  40. log("copying html from" + file + " to " + todir.getAbsolutePath());
  41. File toFile = new File(todir, file.getName());
  42. Mkdir mkdir = (Mkdir) project.createTask("mkdir");
  43. mkdir.setDir(todir);
  44. mkdir.execute();
  45. BufferedReader in = new BufferedReader(new FileReader(file));
  46. PrintStream out = new PrintStream(new FileOutputStream(toFile));
  47. outer:
  48. while (true) {
  49. String line = in.readLine();
  50. if (line == null) break;
  51. if (isStyleSheet(line)) {
  52. doStyleSheet(line, out, file);
  53. while (true) {
  54. String line2 = in.readLine();
  55. if (line2 == null) break outer;
  56. out.println(line2);
  57. }
  58. } else {
  59. out.println(line);
  60. }
  61. }
  62. in.close();
  63. out.close();
  64. } catch (IOException e) {
  65. throw new BuildException(e.getMessage());
  66. }
  67. }
  68. private static void doStyleSheet(String line, PrintStream out, File file) throws IOException {
  69. int srcIndex = line.indexOf("href");
  70. int startQuotIndex = line.indexOf('"', srcIndex);
  71. int endQuotIndex = line.indexOf('"', startQuotIndex + 1);
  72. String stylesheetLocation = line.substring(startQuotIndex + 1, endQuotIndex);
  73. File styleSheetFile = new File(file.getParent(), stylesheetLocation);
  74. out.println("<style type=\"text/css\">");
  75. out.println("<!--");
  76. BufferedReader inStyle = new BufferedReader(new FileReader(styleSheetFile));
  77. while (true) {
  78. String line2 = inStyle.readLine();
  79. if (line2 == null) break;
  80. out.println(line2);
  81. }
  82. inStyle.close();
  83. out.println("-->");
  84. out.println("</style>");
  85. }
  86. private static boolean isStyleSheet(String line) throws IOException {
  87. line = line.toLowerCase();
  88. int len = line.length();
  89. int i = 0;
  90. while (true) {
  91. if (i == len) return false;
  92. if (! Character.isWhitespace(line.charAt(i))) break;
  93. }
  94. return line.startsWith("<link", i);
  95. }
  96. }