001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PageSequence.java 554094 2007-07-07 00:04:25Z adelmelle $ */
019:
020: package org.apache.fop.fo.pagination;
021:
022: // Java
023: import java.util.Map;
024:
025: import org.xml.sax.Locator;
026:
027: import org.apache.fop.apps.FOPException;
028: import org.apache.fop.datatypes.Numeric;
029: import org.apache.fop.fo.FONode;
030: import org.apache.fop.fo.FObj;
031: import org.apache.fop.fo.PropertyList;
032: import org.apache.fop.fo.ValidationException;
033:
034: /**
035: * Implementation of the fo:page-sequence formatting object.
036: */
037: public class PageSequence extends FObj {
038:
039: // The value of properties relevant for fo:page-sequence.
040: private String country;
041: private String format;
042: private String language;
043: private int letterValue;
044: private char groupingSeparator;
045: private int groupingSize;
046: private Numeric initialPageNumber;
047: private int forcePageCount;
048: private String masterReference;
049: // End of property values
050:
051: /** The parent root object */
052: private Root root;
053:
054: // There doesn't seem to be anything in the spec requiring flows
055: // to be in the order given, only that they map to the regions
056: // defined in the page sequence, so all we need is this one hashmap
057: // the set of flows includes StaticContent flows also
058:
059: /** Map of flows to their flow name (flow-name, Flow) */
060: private Map flowMap;
061:
062: private int startingPageNumber = 0;
063: private PageNumberGenerator pageNumberGenerator;
064:
065: /**
066: * The currentSimplePageMaster is either the page master for the
067: * whole page sequence if master-reference refers to a simple-page-master,
068: * or the simple page master produced by the page sequence master otherwise.
069: * The pageSequenceMaster is null if master-reference refers to a
070: * simple-page-master.
071: */
072: private SimplePageMaster simplePageMaster;
073: private PageSequenceMaster pageSequenceMaster;
074:
075: /**
076: * The fo:title object for this page-sequence.
077: */
078: private Title titleFO;
079:
080: /**
081: * The fo:flow object for this page-sequence.
082: */
083: private Flow mainFlow = null;
084:
085: /**
086: * Create a page sequence FO node.
087: *
088: * @param parent the parent FO node
089: */
090: public PageSequence(FONode parent) {
091: super (parent);
092: }
093:
094: /**
095: * @see org.apache.fop.fo.FObj#bind(PropertyList)
096: */
097: public void bind(PropertyList pList) throws FOPException {
098: super .bind(pList);
099: country = pList.get(PR_COUNTRY).getString();
100: format = pList.get(PR_FORMAT).getString();
101: language = pList.get(PR_LANGUAGE).getString();
102: letterValue = pList.get(PR_LETTER_VALUE).getEnum();
103: groupingSeparator = pList.get(PR_GROUPING_SEPARATOR)
104: .getCharacter();
105: groupingSize = pList.get(PR_GROUPING_SIZE).getNumber()
106: .intValue();
107: initialPageNumber = pList.get(PR_INITIAL_PAGE_NUMBER)
108: .getNumeric();
109: forcePageCount = pList.get(PR_FORCE_PAGE_COUNT).getEnum();
110: masterReference = pList.get(PR_MASTER_REFERENCE).getString();
111:
112: if (masterReference == null || masterReference.equals("")) {
113: missingPropertyError("master-reference");
114: }
115: }
116:
117: /**
118: * @see org.apache.fop.fo.FONode#startOfNode()
119: */
120: protected void startOfNode() throws FOPException {
121: super .startOfNode();
122: this .root = (Root) parent;
123: flowMap = new java.util.HashMap();
124:
125: this .simplePageMaster = root.getLayoutMasterSet()
126: .getSimplePageMaster(masterReference);
127: if (this .simplePageMaster == null) {
128: this .pageSequenceMaster = root.getLayoutMasterSet()
129: .getPageSequenceMaster(masterReference);
130: if (this .pageSequenceMaster == null) {
131: throw new ValidationException(
132: "master-reference '"
133: + masterReference
134: + "' for fo:page-sequence matches no"
135: + " simple-page-master or page-sequence-master",
136: locator);
137: }
138: }
139:
140: this .pageNumberGenerator = new PageNumberGenerator(format,
141: groupingSeparator, groupingSize, letterValue);
142:
143: getFOEventHandler().startPageSequence(this );
144: }
145:
146: /** @see org.apache.fop.fo.FONode#endOfNode() */
147: protected void endOfNode() throws FOPException {
148: if (mainFlow == null) {
149: missingChildElementError("(title?,static-content*,flow)");
150: }
151:
152: getFOEventHandler().endPageSequence(this );
153: }
154:
155: /**
156: * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
157: XSL Content Model: (title?,static-content*,flow)
158: */
159: protected void validateChildNode(Locator loc, String nsURI,
160: String localName) throws ValidationException {
161: if (FO_URI.equals(nsURI)) {
162: if (localName.equals("title")) {
163: if (titleFO != null) {
164: tooManyNodesError(loc, "fo:title");
165: } else if (flowMap.size() > 0) {
166: nodesOutOfOrderError(loc, "fo:title",
167: "fo:static-content");
168: } else if (mainFlow != null) {
169: nodesOutOfOrderError(loc, "fo:title", "fo:flow");
170: }
171: } else if (localName.equals("static-content")) {
172: if (mainFlow != null) {
173: nodesOutOfOrderError(loc, "fo:static-content",
174: "fo:flow");
175: }
176: } else if (localName.equals("flow")) {
177: if (mainFlow != null) {
178: tooManyNodesError(loc, "fo:flow");
179: }
180: } else {
181: invalidChildError(loc, nsURI, localName);
182: }
183: } else {
184: invalidChildError(loc, nsURI, localName);
185: }
186: }
187:
188: /**
189: * @see org.apache.fop.fo.FONode#addChildNode(FONode)
190: * @todo see if addChildNode() should also be called for fo's other than
191: * fo:flow.
192: */
193: public void addChildNode(FONode child) throws FOPException {
194: int childId = child.getNameId();
195:
196: if (childId == FO_TITLE) {
197: this .titleFO = (Title) child;
198: } else if (childId == FO_FLOW) {
199: this .mainFlow = (Flow) child;
200: addFlow(mainFlow);
201: } else if (childId == FO_STATIC_CONTENT) {
202: addFlow((StaticContent) child);
203: String flowName = ((StaticContent) child).getFlowName();
204: flowMap.put(flowName, child);
205: }
206: }
207:
208: /**
209: * Add a flow or static content, mapped by its flow-name.
210: * The flow-name is used to associate the flow with a region on a page,
211: * based on the region-names given to the regions in the page-master
212: * used to generate that page.
213: */
214: private void addFlow(Flow flow) throws ValidationException {
215: String flowName = flow.getFlowName();
216:
217: if (hasFlowName(flowName)) {
218: throw new ValidationException("duplicate flow-name \""
219: + flowName + "\" found within fo:page-sequence",
220: flow.getLocator());
221: }
222:
223: if (!root.getLayoutMasterSet().regionNameExists(flowName)
224: && !flowName.equals("xsl-before-float-separator")
225: && !flowName.equals("xsl-footnote-separator")) {
226: throw new ValidationException("flow-name \"" + flowName
227: + "\" could not be mapped to a region-name in the"
228: + " layout-master-set", flow.getLocator());
229: }
230: }
231:
232: /**
233: * Initialize the current page number for the start of the page sequence.
234: */
235: public void initPageNumber() {
236: int pageNumberType = 0;
237:
238: if (initialPageNumber.getEnum() != 0) {
239: // auto | auto-odd | auto-even.
240: startingPageNumber = root
241: .getEndingPageNumberOfPreviousSequence() + 1;
242: pageNumberType = initialPageNumber.getEnum();
243: if (pageNumberType == EN_AUTO_ODD) {
244: if (startingPageNumber % 2 == 0) {
245: startingPageNumber++;
246: }
247: } else if (pageNumberType == EN_AUTO_EVEN) {
248: if (startingPageNumber % 2 == 1) {
249: startingPageNumber++;
250: }
251: }
252: } else { // <integer> for explicit page number
253: int pageStart = initialPageNumber.getValue();
254: startingPageNumber = (pageStart > 0) ? pageStart : 1; // spec rule
255: }
256: }
257:
258: // /**
259: // * Returns true when there is more flow elements left to lay out.
260: // */
261: // private boolean flowsAreIncomplete() {
262: // boolean isIncomplete = false;
263:
264: // for (Iterator e = flowMap.values().iterator(); e.hasNext(); ) {
265: // Flow flow = (Flow)e.next();
266: // if (flow instanceof StaticContent) {
267: // continue;
268: // }
269:
270: // Status status = flow.getStatus();
271: // isIncomplete |= status.isIncomplete();
272: // }
273: // return isIncomplete;
274: // }
275:
276: // /**
277: // * Returns the flow that maps to the given region class for the current
278: // * page master.
279: // */
280: // private Flow getCurrentFlow(String regionClass) {
281: // Region region = getCurrentSimplePageMaster().getRegion(regionClass);
282: // if (region != null) {
283: // Flow flow = (Flow)flowMap.get(region.getRegionName());
284: // return flow;
285:
286: // } else {
287:
288: // getLogger().error("flow is null. regionClass = '" + regionClass
289: // + "' currentSPM = "
290: // + getCurrentSimplePageMaster());
291:
292: // return null;
293: // }
294:
295: // }
296:
297: // private boolean isFlowForMasterNameDone(String masterName) {
298: // // parameter is master-name of PMR; we need to locate PM
299: // // referenced by this, and determine whether flow(s) are OK
300: // if (isForcing)
301: // return false;
302: // if (masterName != null) {
303:
304: // SimplePageMaster spm =
305: // root.getLayoutMasterSet().getSimplePageMaster(masterName);
306: // Region region = spm.getRegion(FO_REGION_BODY);
307:
308: // Flow flow = (Flow)flowMap.get(region.getRegionName());
309: // /*if ((null == flow) || flow.getStatus().isIncomplete())
310: // return false;
311: // else
312: // return true;*/
313: // }
314: // return false;
315: // }
316:
317: /**
318: * Get the starting page number for this page sequence.
319: *
320: * @return the starting page number
321: */
322: public int getStartingPageNumber() {
323: return startingPageNumber;
324: }
325:
326: // private void forcePage(AreaTree areaTree, int firstAvailPageNumber) {
327: // boolean makePage = false;
328: // if (this.forcePageCount == ForcePageCount.AUTO) {
329: // PageSequence nextSequence =
330: // this.root.getSucceedingPageSequence(this);
331: // if (nextSequence != null) {
332: // if (nextSequence.getIpnValue().equals("auto")) {
333: // // do nothing special
334: // }
335: // else if (nextSequence.getIpnValue().equals("auto-odd")) {
336: // if (firstAvailPageNumber % 2 == 0) {
337: // makePage = true;
338: // }
339: // } else if (nextSequence.getIpnValue().equals("auto-even")) {
340: // if (firstAvailPageNumber % 2 != 0) {
341: // makePage = true;
342: // }
343: // } else {
344: // int nextSequenceStartPageNumber =
345: // nextSequence.getCurrentPageNumber();
346: // if ((nextSequenceStartPageNumber % 2 == 0)
347: // && (firstAvailPageNumber % 2 == 0)) {
348: // makePage = true;
349: // } else if ((nextSequenceStartPageNumber % 2 != 0)
350: // && (firstAvailPageNumber % 2 != 0)) {
351: // makePage = true;
352: // }
353: // }
354: // }
355: // } else if ((this.forcePageCount == ForcePageCount.EVEN)
356: // && (this.pageCount % 2 != 0)) {
357: // makePage = true;
358: // } else if ((this.forcePageCount == ForcePageCount.ODD)
359: // && (this.pageCount % 2 == 0)) {
360: // makePage = true;
361: // } else if ((this.forcePageCount == ForcePageCount.END_ON_EVEN)
362: // && (firstAvailPageNumber % 2 == 0)) {
363: // makePage = true;
364: // } else if ((this.forcePageCount == ForcePageCount.END_ON_ODD)
365: // && (firstAvailPageNumber % 2 != 0)) {
366: // makePage = true;
367: // } else if (this.forcePageCount == ForcePageCount.NO_FORCE) {
368: // // do nothing
369: // }
370:
371: // if (makePage) {
372: // try {
373: // this.isForcing = true;
374: // this.currentPageNumber++;
375: // firstAvailPageNumber = this.currentPageNumber;
376: // currentPage = makePage(areaTree, firstAvailPageNumber, false,
377: // true);
378: // String formattedPageNumber =
379: // pageNumberGenerator.makeFormattedPageNumber(this.currentPageNumber);
380: // currentPage.setFormattedNumber(formattedPageNumber);
381: // currentPage.setPageSequence(this);
382: // formatStaticContent(areaTree);
383: // log.debug("[forced-" + firstAvailPageNumber + "]");
384: // areaTree.addPage(currentPage);
385: // this.root.setRunningPageNumberCounter(this.currentPageNumber);
386: // this.isForcing = false;
387: // } catch (FOPException fopex) {
388: // log.debug("'force-page-count' failure");
389: // }
390: // }
391: // }
392:
393: /**
394: * Get the static content FO node from the flow map.
395: * This gets the static content flow for the given flow name.
396: *
397: * @param name the flow name to find
398: * @return the static content FO node
399: */
400: public StaticContent getStaticContent(String name) {
401: return (StaticContent) flowMap.get(name);
402: }
403:
404: /**
405: * Accessor method for titleFO
406: * @return titleFO for this object
407: */
408: public Title getTitleFO() {
409: return titleFO;
410: }
411:
412: /**
413: * Public accessor for getting the MainFlow to which this PageSequence is
414: * attached.
415: * @return the MainFlow object to which this PageSequence is attached.
416: */
417: public Flow getMainFlow() {
418: return mainFlow;
419: }
420:
421: /**
422: * Determine if this PageSequence already has a flow with the given flow-name
423: * Used for validation of incoming fo:flow or fo:static-content objects
424: * @param flowName The flow-name to search for
425: * @return true if flow-name already defined within this page sequence,
426: * false otherwise
427: */
428: public boolean hasFlowName(String flowName) {
429: return flowMap.containsKey(flowName);
430: }
431:
432: /** @return the flow map for this page-sequence */
433: public Map getFlowMap() {
434: return this .flowMap;
435: }
436:
437: /**
438: * Public accessor for determining the next page master to use within this page sequence.
439: * @param page the page number of the page to be created
440: * @param isFirstPage indicator whether this page is the first page of the
441: * page sequence
442: * @param isLastPage indicator whether this page is the last page of the
443: * page sequence
444: * @param isBlank indicator whether the page will be blank
445: * @return the SimplePageMaster to use for this page
446: * @throws FOPException if there's a problem determining the page master
447: */
448: public SimplePageMaster getNextSimplePageMaster(int page,
449: boolean isFirstPage, boolean isLastPage, boolean isBlank)
450: throws FOPException {
451:
452: if (pageSequenceMaster == null) {
453: return simplePageMaster;
454: }
455: boolean isOddPage = ((page % 2) == 1);
456: if (log.isDebugEnabled()) {
457: log.debug("getNextSimplePageMaster(page=" + page
458: + " isOdd=" + isOddPage + " isFirst=" + isFirstPage
459: + " isLast=" + isLastPage + " isBlank=" + isBlank
460: + ")");
461: }
462: return pageSequenceMaster.getNextSimplePageMaster(isOddPage,
463: isFirstPage, isLastPage, isBlank);
464: }
465:
466: /**
467: * Used to set the "cursor position" for the page masters to the previous item.
468: * @return true if there is a previous item, false if the current one was the first one.
469: */
470: public boolean goToPreviousSimplePageMaster() {
471: if (pageSequenceMaster == null) {
472: return true;
473: } else {
474: return pageSequenceMaster.goToPreviousSimplePageMaster();
475: }
476: }
477:
478: /** @return true if the page-sequence has a page-master with page-position="last" */
479: public boolean hasPagePositionLast() {
480: if (pageSequenceMaster == null) {
481: return false;
482: } else {
483: return pageSequenceMaster.hasPagePositionLast();
484: }
485: }
486:
487: /**
488: * Retrieves the string representation of a page number applicable
489: * for this page sequence
490: * @param pageNumber the page number
491: * @return string representation of the page number
492: */
493: public String makeFormattedPageNumber(int pageNumber) {
494: return pageNumberGenerator.makeFormattedPageNumber(pageNumber);
495: }
496:
497: /**
498: * Public accessor for the ancestor Root.
499: * @return the ancestor Root
500: */
501: public Root getRoot() {
502: return root;
503: }
504:
505: /** @return the "master-reference" property. */
506: public String getMasterReference() {
507: return masterReference;
508: }
509:
510: /** @see org.apache.fop.fo.FONode#getLocalName() */
511: public String getLocalName() {
512: return "page-sequence";
513: }
514:
515: /** @see org.apache.fop.fo.FObj#getNameId() */
516: public int getNameId() {
517: return FO_PAGE_SEQUENCE;
518: }
519:
520: /** @return the force-page-count value */
521: public int getForcePageCount() {
522: return forcePageCount;
523: }
524:
525: /** @return the initial-page-number property value */
526: public Numeric getInitialPageNumber() {
527: return initialPageNumber;
528: }
529:
530: /** @return the country property value */
531: public String getCountry() {
532: return this .country;
533: }
534:
535: /** @return the language property value */
536: public String getLanguage() {
537: return this .language;
538: }
539:
540: /**
541: * Releases a page-sequence's children after the page-sequence has been fully processed.
542: */
543: public void releasePageSequence() {
544: this.mainFlow = null;
545: this.flowMap.clear();
546: }
547:
548: }
|