123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733 |
- /**
- Licensed Materials - Property of IBM
- IBM Cognos Products: DOCS
- (C) Copyright IBM Corp. 2005, 2010
- US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
- IBM Corp.
- */
- /**
- *
- * API.java
- *
- * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
- * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
- *
- * This class is used by a DOM parser to add the appropriate element
- * tags to a new report.
- */
- import java.io.ByteArrayInputStream;
- import java.util.List;
- import java.util.Vector;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.QName;
- import org.dom4j.io.SAXReader;
- public class API /* implements ReportBuilder */
- {
- private String sReportSpec;
- public static final String sREPORT = "report";
- private Document oDocument;
- public API(String sReportSpec)
- {
- try
- {
- // when creating a dom document, temporarily remove the default
- // namespace from the XML otherwise selecting nodes will fail.
- String start = null;
- String end = null;
- int index = sReportSpec.indexOf("xmlns=", 0);
- if (index >= 0)
- {
- start = sReportSpec.substring(0, index);
- end = sReportSpec.substring(sReportSpec.indexOf(
- "http://developer.cognos.com/schemas/report/15.0/") + 49);
- sReportSpec = start + end;
- }
- //load the spec into the DOM
- SAXReader xmlReader = new SAXReader();
- ByteArrayInputStream bais = new ByteArrayInputStream(sReportSpec
- .getBytes("UTF-8"));
- oDocument = xmlReader.read(bais);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public API() {
- Element reportElement = DocumentHelper.createElement("report");
-
- // In dom4j 1.6.1, can no longer add xmlns with addAttribute
- reportElement.addNamespace("", "http://developer.cognos.com/schemas/report/15.0/");
- reportElement.addAttribute("expressionLocale", "en-us");
- oDocument = DocumentHelper.createDocument(reportElement);
- }
- public void updateXMLNS() {
- Element e = oDocument.getRootElement();
-
- // In dom4j 1.6.1, can no longer add xmlns with addAttribute
- e.addNamespace("", "http://developer.cognos.com/schemas/report/15.0/");
- }
- public String getXML() {
- String strXML = oDocument.asXML();
-
- //strip out extraneous empty xmlns attributes added by use of addAttribute
- strXML = strXML.replaceAll(" xmlns=\"\"","");
-
- return strXML;
- }
- public void addLayouts() {
- Element e = DocumentHelper.createElement("layouts");
- oDocument.getRootElement().add(e);
- }
- public void addLayout() {
- Element n = (Element) oDocument.selectSingleNode("/report/layouts");
- if (n == null) {
- addLayouts();
- n = (Element) oDocument.selectSingleNode("/report/layouts");
- }
- Element e = DocumentHelper.createElement("layout");
- n.add(e);
- }
- public void addReportPages() {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout");
- if (n == null) {
- addLayout();
- n = (Element) oDocument.selectSingleNode("/report/layouts/layout");
- }
- Element e = DocumentHelper.createElement("reportPages");
- n.add(e);
- }
- public void addPage(String p_sName) {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages");
- if (n == null) {
- addReportPages();
- n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages");
- }
- Element e = DocumentHelper.createElement("page");
- Element eStyle = buildStyle("pg");
-
- e.addAttribute("name", p_sName);
- e.add(eStyle);
- n.add(e);
- }
- public void addPageBody() {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page");
- if (n == null) {
- addPage("Page1");
- n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page");
- }
- Element e = DocumentHelper.createElement("pageBody");
- Element eStyle = buildStyle("pb");
-
- e.add(eStyle);
- n.add(e);
- }
- public void addPageBodyContents() {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody");
- if (n == null) {
- addPageBody();
- n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody");
- }
- Element e = DocumentHelper.createElement("contents");
- n.add(e);
- }
- public void addList()
- {
- addList("Query1");
- }
- public void addList(String p_sName) {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody/contents");
- if (n == null) {
- addPageBodyContents();
- n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody/contents");
- }
- Element e = DocumentHelper.createElement("list");
-
- Element eStyle = buildStyle("ls");
-
- e.addAttribute("refQuery", p_sName);
- e.add(eStyle);
- n.add(e);
- }
- public void addStyle() {
- addStyle("Query1");
- }
- public void addStyle(String p_sName) {
- String sName = p_sName;
- String quotChar = "\'";
- if (sName.indexOf(quotChar) >= 0) {
- quotChar = "\"";
- }
- String elementString = "/report/layouts/layout/reportPages/page/pageBody/contents/list[@refQuery="
- + quotChar + sName + quotChar + "]";
- Element n = (Element) oDocument.selectSingleNode(elementString);
- if (n == null) {
- addList(p_sName);
- n = (Element) oDocument.selectSingleNode(elementString);
- }
- Element e = DocumentHelper.createElement("style");
- n.add(e);
- }
- public void addCSS(String p_sName) {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody/contents/list/style");
- if (n == null) {
- addStyle();
- n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody/contents/list/style");
- }
- Element e = DocumentHelper.createElement("CSS");
- e.addAttribute("value", p_sName);
- n.add(e);
- }
- public void addQueries() {
- Element e = DocumentHelper.createElement("queries");
- oDocument.getRootElement().add(e);
- }
- public Element buildStyle(String sName) {
- Element eStyle = DocumentHelper.createElement("style");
- Element eDefaultStyles = DocumentHelper.createElement("defaultStyles");
- Element eDefinedStyle = DocumentHelper.createElement("defaultStyle");
- eDefinedStyle.addAttribute("refStyle", sName);
-
- eDefaultStyles.add(eDefinedStyle);
- eStyle.add(eDefaultStyles);
-
- return eStyle;
- }
- public void getQueries() {
- List n = (List) oDocument.selectSingleNode("/report/queries/query");
- if (n != null) {
- for (int i = 0; i < n.size(); i++) {
- String x = ((Element) n.get(i)).getName();
- System.out.println(x);
- }
- }
- }
- public void addQuery()
- {
- addQuery("Query1");
- }
- public void addQuery(String p_sName) {
- Element n = (Element) oDocument.selectSingleNode("/report/queries");
- if (n == null)
- {
- addQueries();
- n = (Element) oDocument.selectSingleNode("/report/queries");
- }
- Element eModel = DocumentHelper.createElement("model");
- Element eSource = DocumentHelper.createElement("source");
- Element e = DocumentHelper.createElement("query");
- eSource.add(eModel);
- e.add(eSource);
- e.addAttribute("name", p_sName);
- n.add(e);
- }
- public void addSelection()
- {
- Element n = (Element) oDocument.selectSingleNode("/report/queries/query");
- if (n == null)
- {
- addQuery();
- n = (Element) oDocument.selectSingleNode("/report/queries/query");
- }
- Element eSelection = DocumentHelper.createElement("selection");
- n.add(eSelection);
- }
- /**
- * addDataItem
- *
- * @param p_sName
- * @param p_sExpression
- */
- public void addDataItem(String p_sName, String p_sExpression)
- {
- addDataItem(p_sName, p_sExpression, false);
- }
- public void addDataItem(String p_sName, String p_sExpression,
- boolean p_bAggregate)
- {
- addDataItem(p_sName, p_sExpression, p_bAggregate, null);
- }
- public void addDataItem(String p_sName, String p_sExpression,
- boolean p_bAggregate, String p_sSort)
- {
- Element nSelection = (Element) oDocument.selectSingleNode(
- "/report/queries/query/selection");
- if (nSelection == null)
- {
- addSelection();
- nSelection = (Element) oDocument.selectSingleNode(
- "/report/queries/query/selection");
- }
- //Create the dataItem element
- Element eDataItem = DocumentHelper.createElement("dataItem");
- eDataItem.addAttribute("name", p_sName);
- //Add an aggregation element, if necessary
- if (p_bAggregate) {
- eDataItem.addAttribute("aggregate", "true");
- }
- //Add the expression for the dataItem
- Element eExpression = DocumentHelper.createElement("expression");
- eExpression.setText(p_sExpression);
- eDataItem.add(eExpression);
- //Add the dataItem to the selection element
- nSelection.add(eDataItem);
- //add the sort item to the report, if necessary
- if (p_sSort != null && !p_sSort.equals(""))
- {
- addDataItemSort(p_sName, p_sSort);
- }
- }
- public void addModelPath(String p_sName)
- {
- Element n = (Element) oDocument.getRootElement();
- Element eModelPath = DocumentHelper.createElement("modelPath");
- eModelPath.setText(p_sName + "/model[@name='model']");
- n.add(eModelPath);
- }
- public void addModel()
- {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/queries/query");
- if (n == null)
- {
- addQuery();
- n = (Element) oDocument.selectSingleNode(
- "/report/queries/query");
- }
- Element e = DocumentHelper.createElement("model");
- n.add(e);
- }
- public void addListColumnRowSpan(String p_sName)
- {
- String sName = p_sName;
- String quotChar = "\'";
- if (sName.indexOf(quotChar) >= 0) {
- quotChar = "\"";
- }
- //Need to find the column corresponding to the referenced data item
- String elementString =
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/"
- + "listColumns/listColumn/listColumnBody/contents/textItem/dataSource/"
- + "dataItemValue[@refDataItem="
- + quotChar
- + sName
- + quotChar
- + "]";
- Element n = (Element) oDocument.selectSingleNode(elementString);
- //Need to add a listColumnRowSpan node to listColumnBody for that
- // column (4 levels up)
- Element eColumnBody = n.getParent().getParent().getParent().getParent();
- Element eListColumnRowSpan = DocumentHelper
- .createElement("listColumnRowSpan");
- eListColumnRowSpan.addAttribute("refDataItem", sName);
- }
- public void addListGroups()
- {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/");
- if (n == null)
- {
- addList();
- n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/");
- }
- Element eListGroups = DocumentHelper.createElement("listGroups");
- n.add(eListGroups);
- }
- public void addListGroup(String p_sName) {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/"
- + "listColumns/listGroups");
- if (n == null)
- {
- addListGroups();
- n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/"
- + "listColumns/listGroups");
- }
- //Add the listGroup node to listGroups
- Element eListGroup = DocumentHelper.createElement("listGroup");
- n.addAttribute("refDataItem", p_sName);
- n.add(eListGroup);
- }
- public void removeColumn(String sColumnReference, String sColumnExpression,
- String sColumnTitle) {
- Element nDataItem = null;
- Element nRefItem = null;
- Element nColumn = null;
- String quotChar = "\'";
- if (sColumnExpression.indexOf(quotChar) >= 0) {
- quotChar = "\"";
- }
- nDataItem = (Element) (oDocument.selectSingleNode(
- "/report/queries/query/selection/dataItem[@name="
- + quotChar
- + sColumnReference
- + quotChar
- + "]"));
- if (nDataItem != null) {
- nDataItem.detach();
- } else {
- System.out.println("Remove column failed for column "
- + sColumnExpression);
- //return;
- }
- quotChar = "\'";
- if (sColumnReference.indexOf(quotChar) >= 0) {
- quotChar = "\"";
- }
- quotChar = "\'";
- if (sColumnReference.indexOf(quotChar) >= 0) {
- quotChar = "\"";
- }
- // remove the list column
- nColumn = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/"
- + "list/listColumns/listColumn/listColumnTitle/contents/textItem/"
- + "dataSource/dataItemLabel[@refDataItem="
- + quotChar
- + sColumnReference
- + quotChar
- + "]");
- if (nColumn != null) {
- nColumn.getParent().getParent().getParent().getParent().getParent().detach();
- } else {
- System.out.println(
- "Remove list column failed for columnReference: " + sColumnReference);
- }
- }
- public void addDetailFilter()
- {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/queries/query");
- if (n == null)
- {
- addQuery();
- n = (Element) oDocument.selectSingleNode(
- "/report/queries/query");
- }
- Element eDetailFilter = DocumentHelper.createElement(
- "detailFilter");
- n.add(eDetailFilter);
- }
- public void addFilter() {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/queries/query/detailFilter");
- if (n == null) {
- addDetailFilter();
- n = (Element) oDocument.selectSingleNode(
- "/report/queries/query/detailFilter");
- }
- Element eFilter = DocumentHelper.createElement("filter");
- eFilter.addAttribute("use", "required");
- n.add(eFilter);
- }
- public void addFilterExpression(String filter)
- {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/queries/query/detailFilter/filter");
- if (n == null)
- {
- addFilter();
- n = (Element) oDocument.selectSingleNode(
- "/report/queries/query/detailFilter/filter");
- }
- Element eFilterExpression = DocumentHelper.createElement("filterExpression");
- eFilterExpression.setText(filter);
- n.add(eFilterExpression);
- }
- public void addSortList()
- {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list");
- if (n == null)
- {
- addList();
- n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list");
- }
- Element eSortList = DocumentHelper.createElement("sortList");
- n.add(eSortList);
- }
- public void addDataItemSort(String p_sElementName, String p_sSort)
- {
- Element n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/sortList");
- if (n == null) {
- addSortList();
- n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/sortList");
- }
- Element eSort = DocumentHelper.createElement("sortItem");
- eSort.addAttribute("refDataItem", p_sElementName);
- eSort.addAttribute("sortOrder", p_sSort);
- n.add(eSort);
- }
- public Vector getDataItemReferences() {
- Vector columnsList = new Vector();
- Vector fullNameColumnsList = new Vector();
- List columnList = (List) oDocument.selectNodes(
- "/report/queries/query/selection/dataItem[@name]");
- for (int i = 0; i < columnList.size(); i++) {
- Element eColumn = (Element) columnList.get(i);
- String sColumn = eColumn.attributeValue("name");
- columnsList.add(sColumn);
- }
- return columnsList;
- }
- public Vector getDataItemExpressions() {
- Vector fullNameColumnsList = new Vector();
- List columnList = (List) oDocument.selectNodes(
- "/report/queries/query/selection/dataItem/expression");
- for (int i = 0; i < columnList.size(); i++) {
- Element eColumn = (Element) columnList.get(i);
- String sColumn = eColumn.getText();
- fullNameColumnsList.add(sColumn);
- }
- return fullNameColumnsList;
- }
- public Vector getColumnTitles() {
- Vector columnTitles = new Vector();
- //Check for column titles based on dataItemLabel
- List columnTitleList = (List) oDocument.selectNodes(
- "/report/layouts/layout/reportPages/page/pageBody/contents/"
- + "list/listColumns/listColumn/listColumnTitle/"
- + "contents/textItem/dataSource/dataItemLabel");
- for (int i = 0; i < columnTitleList.size(); i++)
- {
- Element e = (Element) columnTitleList.get(i);
- String sColumnTitle = null;
- sColumnTitle = e.attributeValue(new QName("refDataItem"));
- columnTitles.add(sColumnTitle);
- }
- //Check for columnTitles base on staticValue
- List columnStaticTitleList = (List) oDocument.selectNodes(
- "/report/layouts/layout/reportPages/page/pageBody/contents/"
- + "list/listColumns/listColumn/listColumnTitle/"
- + "contents/textItem/dataSource/staticValue");
- for (int i = 0; i < columnStaticTitleList.size(); i++)
- {
- Element e = (Element) columnStaticTitleList.get(i);
- String sColumnTitle = null;
- sColumnTitle = e.getText();
- columnTitles.add(sColumnTitle);
- }
- return columnTitles;
- }
- public void modifyTitle(String title, String newTitle)
- {
- Element n = null;
- String quotChar = "\'";
- if (title.indexOf(quotChar) >= 0)
- {
- quotChar = "\"";
- }
- String nodeDataItemTitle =
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/listColumns/"
- + "listColumn/listColumnTitle/contents/textItem/dataSource/dataItemLabel[@refDataItem="
- + quotChar
- + title
- + quotChar
- + "]";
- String nodeStaticTitle =
- "/report/layouts/layout/reportPages/page/pageBody/contents/list/listColumns/"
- + "listColumn/listColumnTitle/contents/textItem/dataSource/staticValue";
- n = (Element) oDocument.selectSingleNode(nodeDataItemTitle);
- if (n == null)
- {
- n = (Element) oDocument.selectSingleNode(nodeStaticTitle);
- if (n == null)
- {
- System.out.println("Modify column title failed for "
- + title
- + " title not found.");
- return;
- }
- }
- Element nDataSource = n.getParent();
- n.detach();
- Element eTitle = DocumentHelper.createElement("staticValue");
- eTitle.setText(newTitle);
- nDataSource.add(eTitle);
- }
- /**
- * addListColumn
- *
- * @param p_sName
- * @param position
- * (to insert in the default position pass 0 to this method)
- * (default position = insert after the last child element.)
- */
- public void addListColumn(String p_sName, int position) {
- Element n = null;
- n = (Element) oDocument.selectSingleNode(
- "/report/layouts/layout/reportPages/page/pageBody/contents"
- + "/list/listColumns");
- //Create an empty column node
- Element eCol = DocumentHelper.createElement("listColumn");
- // Prepare all the bits to contain the column title
- Element eTitle = DocumentHelper.createElement("listColumnTitle");
- Element eStyleTitle = buildStyle("lt");
-
- Element eTContents = DocumentHelper.createElement("contents");
- Element eTText = DocumentHelper.createElement("textItem");
- Element eTSrc = DocumentHelper.createElement("dataSource");
- Element eLabel = DocumentHelper.createElement("dataItemLabel");
- eLabel.addAttribute("refDataItem", p_sName);
- //Prepare all the bits to contain the column data
- Element eBody = DocumentHelper.createElement("listColumnBody");
- Element eStyle = buildStyle("lm");
-
- Element eBContents = DocumentHelper.createElement("contents");
- Element eBText = DocumentHelper.createElement("textItem");
- Element eBSrc = DocumentHelper.createElement("dataSource");
- Element eValue = DocumentHelper.createElement("dataItemValue");
- eValue.addAttribute("refDataItem", p_sName);
- //Piece the Title together in the right order
- eTSrc.add(eLabel);
- eTText.add(eTSrc);
- eTContents.add(eTText);
- eTitle.add(eStyleTitle);
- eTitle.add(eTContents);
- //Piece the Body together
- eBSrc.add(eValue);
- eBText.add(eBSrc);
- eBContents.add(eBText);
- eBody.add(eStyle);
- eBody.add(eBContents);
- //Add the title and body to the column
- eCol.add(eTitle);
- eCol.add(eBody);
- if (position > 0) {
- n.content().add(position - 1, eCol);
- } else {
- n.add(eCol);
- }
- }
- public void addListColumns() {
- Element n = (Element) oDocument
- .selectSingleNode("/report/layouts/layout/reportPages/page/pageBody/contents/list");
- Element e = DocumentHelper.createElement("listColumns");
- n.add(e);
- }
- }
|