1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
|
/* Derby - Class org.apache.derbyBuild.GeneratorBase
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyBuild;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
/**
*
*/
public class GeneratorBase extends Task {
/////////////////////////////////////////////////////////////////////////
//
// CONSTANTS
//
/////////////////////////////////////////////////////////////////////////
// header levels
protected static final int BANNER_LEVEL = 1;
protected static final int MAIN_SECTION_LEVEL = BANNER_LEVEL + 1;
protected static final int ISSUE_DETAIL_LEVEL = MAIN_SECTION_LEVEL + 1;
// headlines
protected static final String DESCRIPTION_HEADLINE = "Description";
protected static final String ISSUE_ID_HEADLINE = "Issue Id";
// formatting tags
private static final String ANCHOR = "a";
protected static final String BODY = "body";
private static final String BOLD = "b";
private static final String BORDER = "border";
private static final String COLUMN = "td";
private static final String COLUMN_HEADER = "th";
private static final String HORIZONTAL_LINE = "hr";
protected static final String HTML = "html";
private static final String DIVISION = "div";
private static final String LIST = "ul";
private static final String LIST_ELEMENT = "li";
private static final String NAME = "name";
protected static final String PARAGRAPH = "p";
private static final String ROW = "tr";
protected static final String SPAN = "span";
private static final String TABLE = "table";
// tags in summary xml
private static final String SUM_PREVIOUS_RELEASE_ID = "previousReleaseID";
private static final String SUM_RELEASE_ID = "releaseID";
private static final String SUM_EXCLUDE_RELEASE_ID = "excludeReleaseID";
// other html control
protected static final int DEFAULT_TABLE_BORDER_WIDTH = 2;
protected DocumentBuilder docBldr =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
/////////////////////////////////////////////////////////////////////////
//
// STATE
//
/////////////////////////////////////////////////////////////////////////
// set on the command line or by ant
// Summary file
protected String summaryFileName;
protected Document summaryDoc;
protected ElementFacade summary;
// Bug list file
protected String bugListFileName;
protected List bugList;
// Output file
private String outputFileName;
protected Document outputDoc = docBldr.newDocument();
protected File outputFile;
// computed at run time
protected String releaseID;
protected String previousReleaseID;
protected List<String> excludeReleaseIDList;
protected final String branch;
protected ArrayList<String> errors = new ArrayList<String>();
// misc
protected boolean _invokedByAnt = true;
/**
* Establishes state, including branch number.
* @throws java.lang.Exception
*/
public GeneratorBase() throws Exception {
Properties r = new Properties();
r.load(new FileInputStream("../ant/properties/release.properties"));
int maint = Integer.parseInt(r.getProperty("maint"));
branch = r.getProperty("eversion");
}
/////////////////////////////////////////////////////////////////////////
//
// ANT Task BEHAVIOR
//
/////////////////////////////////////////////////////////////////////////
/**
* Ant accessor to set the name of the summary file prepared by the
* Release Manager
* @param summaryFileName name of xml file to use for the summary
* @throws Exception
*/
public void setSummaryFileName(String summaryFileName) throws Exception {
this.summaryFileName = summaryFileName;
summaryDoc = docBldr.parse(new File(summaryFileName));
summary = new ElementFacade(summaryDoc);
previousReleaseID = summary.getTextByTagName(SUM_PREVIOUS_RELEASE_ID);
excludeReleaseIDList =
summary.getTextListByTagName(SUM_EXCLUDE_RELEASE_ID);
System.out.println("Summary file (input): " + summaryFileName);
}
/**
* Ant mutator to set the name of the JIRA-generated list of bugs
* addressed by this release
* @param bugListFileName name of an xml file from a Jira filter/query
* @throws Exception
*/
public void setBugListFileName(String bugListFileName) throws Exception {
this.bugListFileName = bugListFileName;
}
/**
* Ant mutator to set the name of the generated output file
* @param outputFileName name of file to use as pamphlet (output)
* @throws Exception
*/
public void setOutputFileName(String outputFileName) throws Exception {
this.outputFileName = outputFileName;
outputFile = new File(outputFileName);
}
/**
* Ant mutator to set the id of the release
*/
public void setReleaseId(String releaseID) throws Exception {
this.releaseID = releaseID;
}
/////////////////////////////////////////////////////////////////////////
//
// MINIONS
//
/////////////////////////////////////////////////////////////////////////
/**
* Note that this release is a delta from the previous one.
* @param parent
* @throws java.lang.Exception
*/
protected void buildDelta(Element parent)
throws Exception {
String deltaStatement =
"These notes describe the difference between Apache Derby release " +
releaseID + " and the preceding release " +
previousReleaseID + ".";
addParagraph(parent, deltaStatement);
}
/**
* Replace the known parameters with their corresponding text values.
* @throws Exception
*/
protected void replaceVariables()
throws Exception {
replaceTag(outputDoc, SUM_RELEASE_ID, releaseID);
replaceTag(outputDoc, SUM_PREVIOUS_RELEASE_ID, previousReleaseID);
}
/**
* Print the generated output document to the output file.
* @throws Exception
*/
protected void printOutput() throws Exception {
Source source = new DOMSource(outputDoc);
Result result = new StreamResult(outputFile);
Transformer transformer =
TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
}
/**
* Print accumulated errors.
* @throws Exception
*/
protected void printErrors() throws Exception {
if (errors.isEmpty()) {
return;
}
println("The following other errors occurred:");
for (Iterator i = errors.iterator(); i.hasNext();) {
println("\n" + i.next());
}
}
////////////////////////////////////////////////////////
//
// HTML MINIONS
//
////////////////////////////////////////////////////////
/**
* Create a section at the end of a parent element and link to it from a
* table of contents.
* @param parent in document tree
* @param sectionLevel in document
* @param toc table of content element
* @param sectionName for section being created
* @param tocEntry key into table of content
* @return resulting Element
* @throws Exception
*/
public static Element createSection(Element parent, int sectionLevel,
Element toc, String sectionName, String tocEntry)
throws Exception {
Document doc = parent.getOwnerDocument();
Text textNode = doc.createTextNode(tocEntry);
return createSection(parent, sectionLevel, toc, sectionName, textNode);
}
/**
* Create a section at the end of a parent element and link to it from a
* table of contents.
* @param parent in document tree
* @param sectionLevel in document
* @param toc table of content element
* @param sectionName for section being created
* @param visibleText text to show
* @return resulting Element
* @throws Exception
*/
public static Element createSection(Element parent, int sectionLevel,
Element toc, String sectionName, Node visibleText)
throws Exception {
Document doc = parent.getOwnerDocument();
Element link = createLocalLink(doc, sectionName, visibleText);
addListItem(toc, link);
return createHeader(parent, sectionLevel, sectionName);
}
/**
* Create a header at the end of the parent node. Return the block created
* to hold the text following this header.
* @param parent
* @param headerLevel
* @param text
* @return created header Element
* @throws Exception
*/
public static Element createHeader(Element parent, int headerLevel,
String text) throws Exception {
Document doc = parent.getOwnerDocument();
Text textNode = doc.createTextNode(text);
Element header = doc.createElement(makeHeaderTag(headerLevel));
Element anchor = doc.createElement(ANCHOR);
Element block = doc.createElement(DIVISION);
parent.appendChild(header);
anchor.setAttribute(NAME, text);
header.appendChild(anchor);
header.appendChild(textNode);
parent.appendChild(block);
return block;
}
/**
* Wraps the text content of the given node inside a div tag.
*
* @param node node currently containing the text
* @return The new div-element which has been appended to {@code node}.
* @throws DOMException
*/
private static Element wrapTextContentInDiv(Element node)
throws DOMException {
Document doc = node.getOwnerDocument();
Element div = doc.createElement(DIVISION);
div.setTextContent(node.getTextContent());
node.setTextContent("");
node.appendChild(div);
return div;
}
/**
* Sets/overwrites the specified attribute.
*
* @param node target node
* @param name attribute name
* @param value attribute value
* @throws DOMException
*/
private static void setAttribute(Element node, String name, String value)
throws DOMException {
Node attr = node.getOwnerDocument().createAttribute(name);
attr.setNodeValue(value);
node.getAttributes().setNamedItem(attr);
}
/**
* Create an html text element.
* @param doc
* @param tag
* @param text
* @return created text Element
* @throws Exception
*/
public static Element createTextElement(Document doc, String tag,
String text) throws Exception {
Element retval = doc.createElement(tag);
Text textNode = doc.createTextNode(text);
retval.appendChild(textNode);
return retval;
}
/**
* Create a standard link to a local label.
* @param doc
* @param anchor
* @param text
* @return created link Element
* @throws Exception
*/
public static Element createLocalLink(Document doc, String anchor,
String text) throws Exception {
Text textNode = doc.createTextNode(text);
return createLocalLink(doc, anchor, textNode);
}
/**
* Create a standard link to a local label.
* @param doc
* @param anchor
* @param visibleText
* @return created link Element
* @throws Exception
*/
public static Element createLocalLink(Document doc, String anchor,
Node visibleText) throws Exception {
return createLink(doc, "#" + anchor, visibleText);
}
/**
* Create a hotlink.
* @param doc
* @param label
* @param text
* @return created link Element
* @throws Exception
*/
public static Element createLink(Document doc, String label, String text)
throws Exception {
Text textNode = doc.createTextNode(text);
return createLink(doc, label, textNode);
}
/**
* Create a hotlink.
* @param doc
* @param label
* @param visibleText
* @return created anchor Element
* @throws Exception
*/
public static Element createLink(Document doc, String label,
Node visibleText) throws Exception {
Element hotlink = doc.createElement(ANCHOR);
hotlink.setAttribute("href", label);
hotlink.appendChild(visibleText);
return hotlink;
}
/**
* Insert a list at the end of the parent element.
* @param parent
* @return created list Element
* @throws Exception
*/
public static Element createList(Element parent)
throws Exception {
Document doc = parent.getOwnerDocument();
Element list = doc.createElement(LIST);
parent.appendChild(list);
return list;
}
/**
* Add an item with a bold name to the end of a list.
* @param list
* @param headline
* @param text
* @return created headline Element
* @throws Exception
*/
public static Element addHeadlinedItem(Element list, String headline,
String text) throws Exception {
Document doc = list.getOwnerDocument();
Element itemElement = doc.createElement(LIST_ELEMENT);
Element boldText = boldText(doc, headline);
Text textNode = doc.createTextNode(" - " + text);
list.appendChild(itemElement);
itemElement.appendChild(boldText);
itemElement.appendChild(textNode);
return itemElement;
}
/**
* Make some bold text.
* @param doc
* @param text
* @return created bold Element
* @throws Exception
*/
public static Element boldText(Document doc, String text)
throws Exception {
Element bold = createTextElement(doc, BOLD, text);
return bold;
}
/**
* Add an item to the end of a list.
* @param list
* @param item
* @return created item Element
* @throws Exception
*/
public static Element addListItem(Element list, Node item)
throws Exception {
Document doc = list.getOwnerDocument();
Element itemElement = doc.createElement(LIST_ELEMENT);
list.appendChild(itemElement);
itemElement.appendChild(item);
return itemElement;
}
/**
* Retrieve the indented block inside a section
* @param doc
* @param sectionLevel
* @param sectionName
* @return indented block Element
* @throws Exception
*/
public static Element getSection(Document doc, int sectionLevel,
String sectionName) throws Exception {
String headerTag = makeHeaderTag(sectionLevel);
Element root = doc.getDocumentElement();
NodeList sectionList = root.getElementsByTagName(headerTag);
int count = sectionList.getLength();
for (int i = 0; i < count; i++) {
Element section = (Element) sectionList.item(i);
Element sectionAnchor = getFirstChild(section, ANCHOR);
if (sectionName.equals(sectionAnchor.getAttribute(NAME))) {
// the next item after the section header, is the indented block
return (Element) section.getNextSibling();
}
}
return null;
}
/**
* Make the tag for a header, given its level
* @param headerLevel
* @return header tag at specified level
*/
public static String makeHeaderTag(int headerLevel) {
return "h" + Integer.toString(headerLevel);
}
/**
* Add a paragraph to the end of a parent element.
* @param parent
* @param text
* @return created paragraph Element
* @throws Exception
*/
public static Element addParagraph(Element parent, String text)
throws Exception {
Document doc = parent.getOwnerDocument();
Text textNode = doc.createTextNode(text);
Element paragraph = doc.createElement(PARAGRAPH);
parent.appendChild(paragraph);
paragraph.appendChild(textNode);
return paragraph;
}
/**
* Insert a table at the end of the parent element.
* @param parent
* @param borderWidth
* @param columnHeadings
* @return created table Element
* @throws Exception
*/
public static Element createTable(Element parent, int borderWidth,
String[] columnHeadings) throws Exception {
Document doc = parent.getOwnerDocument();
Element table = doc.createElement(TABLE);
Element headingRow = insertRow(table);
parent.appendChild(table);
table.setAttribute(BORDER, Integer.toString(borderWidth));
for (String headerText : columnHeadings) {
Element headingColumn = insertColumnHeader(headingRow);
headingColumn.setTextContent(headerText);
}
return table;
}
/**
* Sets the width of the first column in the given table.
*
* @param table target table
* @throws DOMException
*/
protected void fixWidthOfFirstColumn(Element table)
throws DOMException {
NodeList headers = table.getElementsByTagName(COLUMN_HEADER);
// Just fail if someone removes the th-elements.
Element th = (Element)headers.item(0);
Element div = wrapTextContentInDiv(th);
setAttribute(div, "style", "width:110px;");
th.appendChild(div);
}
/**
* Insert a row at the end of a table
* @param table
* @return created row Element
* @throws Exception
*/
public static Element insertRow(Element table)
throws Exception {
return insertTableElement(table, ROW);
}
/**
* Insert a column at the end of a row
* @param row
* @return created column Element
* @throws Exception
*/
public static Element insertColumn(Element row)
throws Exception {
return insertTableElement(row, COLUMN);
}
/**
* Insert a header column at the end of the row.
* @param row
* @return Created column Element
* @throws DOMException
*/
public static Element insertColumnHeader(Element row)
throws DOMException {
return insertTableElement(row, COLUMN_HEADER);
}
/**
* Inserts the specified element to the parent element.
*
* @param parent enclosing element, typically a table or a row
* @param type type of the new element to be inserted, typically a
* column value or a row
* @return The newly inserted element.
* @throws DOMException if modifying the DOM fails
*/
private static Element insertTableElement(Element parent, String type)
throws DOMException {
Document doc = parent.getOwnerDocument();
Element newElement = doc.createElement(type);
parent.appendChild(newElement);
return newElement;
}
/**
* Insert a horizontal line at the end of the parent element.
* @param parent
* @return created line Element
* @throws Exception
*/
public static Element insertLine(Element parent)
throws Exception {
Document doc = parent.getOwnerDocument();
Element line = doc.createElement(HORIZONTAL_LINE);
parent.appendChild(line);
return line;
}
////////////////////////////////////////////////////////
//
// XML MINIONS
//
////////////////////////////////////////////////////////
/**
* Search the tree rooted at <code>node</code> for nodes tagged with
* <code>childName</code>. Return the first such node, or throws an
* exception if none is found.
* @param node the node to search
* @param childName name of child node to get
* @return first child element
* @throws Exception
*/
public static Element getFirstChild(Element node, String childName)
throws Exception {
Element retval = getOptionalChild(node, childName);
if (retval == null) {
throw new BuildException("Could not find child element '" +
childName + "' in parent element '" +
node.getNodeName() + "'.");
}
return retval;
}
/**
* Search the tree rooted at <code>node</code> for nodes tagged with
* <code>childName</code>.
* Return the index'th such node.
* @param node parent
* @param childName tag name of qualifying child nodes
* @param index of child node to return
* @return selected child node
* @throws java.lang.Exception
*/
public static Element getNextChild(Element node, String childName,
int index) throws Exception {
Element retval = (Element) node.getElementsByTagName(childName).item(index);
if (retval == null) {
throw new BuildException("Could not find child element '" +
childName + "' in parent element '" +
node.getNodeName() + "'.");
}
return retval;
}
/**
* Search the tree rooted at <code>node</code> for nodes tagged with
* <code>childName</code>. Return the first such node, or null if tag is
* not found.
* @param node root of subtree
* @param childName type of child (tag) to search for
* @return corresponding child if it exitsts, null otherwise.
* @throws java.lang.Exception
*/
public static Element getOptionalChild(Element node, String childName)
throws Exception {
return (Element) node.getElementsByTagName(childName).item(0);
}
/**
* Squeeze the text out of an Element.
* @param node with text child
* @return String representation of node's first child
* @throws java.lang.Exception
*/
public static String squeezeText(Element node)
throws Exception {
return node.getFirstChild().getNodeValue();
}
/**
* Clone all of the children of a source node and add them as children
* of a target node.
* @param source
* @param target
* @throws Exception
*/
public static void cloneChildren(Node source, Node target)
throws Exception {
Document targetDoc = target.getOwnerDocument();
NodeList sourceChildren = source.getChildNodes();
int count = sourceChildren.getLength();
for (int i = 0; i < count; i++) {
Node sourceChild = sourceChildren.item(i);
Node targetChild = targetDoc.importNode(sourceChild, true);
target.appendChild(targetChild);
}
}
/**
* Replace all instances of the tag with the indicated text.
* @param doc
* @param tag
* @param replacementText
* @throws Exception
*/
public static void replaceTag(Document doc, String tag,
String replacementText) throws Exception {
NodeList sourceChildren = doc.getElementsByTagName(tag);
int count = sourceChildren.getLength();
for (int i = 0; i < count; i++) {
Node oldChild = sourceChildren.item(i);
Node newChild = doc.createTextNode(replacementText);
if (oldChild != null) {
Node parent = oldChild.getParentNode();
if (parent != null) {
parent.insertBefore(newChild, oldChild);
}
}
}
for (int i = count - 1; i > -1; i--) {
Node oldChild = sourceChildren.item(i);
if (oldChild != null) {
Node parent = oldChild.getParentNode();
if (parent != null) {
parent.removeChild(oldChild);
}
}
}
}
////////////////////////////////////////////////////////
//
// EXCEPTION PROCESSING MINIONS
//
////////////////////////////////////////////////////////
/**
* Format an error for later reporting.
* @param text description
* @param t execption that occured
* @return formatted error with stack trace
*/
public static String formatError(String text, Throwable t) {
return (text + ": " + t.toString() + "\n" + stringifyStackTrace(t));
}
/**
* Print a stack trace as a string.
* @param t exception to dump stack for
* @return String containing the stacl trace for the exception
*/
public static String stringifyStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
////////////////////////////////////////////////////////
//
// MISC MINIONS
//
////////////////////////////////////////////////////////
/**
* Format a line of text.
* @param text the line to format
*/
protected void println(String text) {
if (_invokedByAnt) {
log(text, Project.MSG_WARN);
} else {
System.out.println(text);
}
}
}
|