001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * SingleRepositoryURLRewriter.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.output.table.html;
030:
031: import java.util.ArrayList;
032:
033: import org.jfree.repository.ContentEntity;
034: import org.jfree.repository.ContentLocation;
035:
036: /**
037: * This URL-Rewriter assumes that both the content and data entity have been created from the same repository. This one
038: * builds a relative URL connecting the data entity with the content.
039: *
040: * @author Thomas Morgner
041: */
042: public class SingleRepositoryURLRewriter implements URLRewriter {
043: public SingleRepositoryURLRewriter() {
044: }
045:
046: public String rewrite(final ContentEntity sourceDocument,
047: final ContentEntity dataEntity) throws URLRewriteException {
048: if (sourceDocument.getRepository().equals(
049: dataEntity.getRepository()) == false) {
050: // cannot proceed ..
051: throw new URLRewriteException(
052: "Content and data repository must be the same.");
053: }
054:
055: final ArrayList entityNames = new ArrayList();
056: entityNames.add(dataEntity.getName());
057:
058: ContentLocation location = dataEntity.getParent();
059: while (location != null) {
060: entityNames.add(location.getName());
061: location = location.getParent();
062: }
063:
064: final ArrayList contentNames = new ArrayList();
065: if (sourceDocument instanceof ContentLocation) {
066: location = (ContentLocation) sourceDocument;
067: } else {
068: location = sourceDocument.getParent();
069: }
070:
071: while (location != null) {
072: contentNames.add(location.getName());
073: location = location.getParent();
074: }
075:
076: // now remove all path elements that are equal ..
077: while (contentNames.isEmpty() == false
078: && entityNames.isEmpty() == false) {
079: final String lastEntity = (String) entityNames
080: .get(entityNames.size() - 1);
081: final String lastContent = (String) contentNames
082: .get(contentNames.size() - 1);
083: if (lastContent.equals(lastEntity) == false) {
084: break;
085: }
086: entityNames.remove(entityNames.size() - 1);
087: contentNames.remove(contentNames.size() - 1);
088: }
089:
090: final StringBuffer b = new StringBuffer();
091: for (int i = entityNames.size() - 1; i >= 0; i--) {
092: final String name = (String) entityNames.get(i);
093: b.append(name);
094: if (i != 0) {
095: b.append('/');
096: }
097: }
098: return b.toString();
099: }
100: }
|