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: SimplePageMaster.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.fo.pagination;
021:
022: // Java
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.xml.sax.Locator;
028:
029: import org.apache.fop.apps.FOPException;
030: import org.apache.fop.datatypes.Length;
031: import org.apache.fop.datatypes.Numeric;
032: import org.apache.fop.fo.FONode;
033: import org.apache.fop.fo.FObj;
034: import org.apache.fop.fo.PropertyList;
035: import org.apache.fop.fo.ValidationException;
036: import org.apache.fop.fo.properties.CommonMarginBlock;
037:
038: /**
039: * A simple-page-master formatting object.
040: * This creates a simple page from the specified regions
041: * and attributes.
042: */
043: public class SimplePageMaster extends FObj {
044: // The value of properties relevant for fo:simple-page-master.
045: private CommonMarginBlock commonMarginBlock;
046: private String masterName;
047: private Length pageHeight;
048: private Length pageWidth;
049: private Numeric referenceOrientation;
050: private int writingMode;
051: // End of property values
052:
053: /**
054: * Page regions (regionClass, Region)
055: */
056: private Map regions;
057:
058: // used for node validation
059: private boolean hasRegionBody = false;
060: private boolean hasRegionBefore = false;
061: private boolean hasRegionAfter = false;
062: private boolean hasRegionStart = false;
063: private boolean hasRegionEnd = false;
064:
065: /**
066: * @see org.apache.fop.fo.FONode#FONode(FONode)
067: */
068: public SimplePageMaster(FONode parent) {
069: super (parent);
070: }
071:
072: /**
073: * @see org.apache.fop.fo.FObj#bind
074: */
075: public void bind(PropertyList pList) throws FOPException {
076: commonMarginBlock = pList.getMarginBlockProps();
077: masterName = pList.get(PR_MASTER_NAME).getString();
078: pageHeight = pList.get(PR_PAGE_HEIGHT).getLength();
079: pageWidth = pList.get(PR_PAGE_WIDTH).getLength();
080: referenceOrientation = pList.get(PR_REFERENCE_ORIENTATION)
081: .getNumeric();
082: writingMode = pList.getWritingMode();
083:
084: if (masterName == null || masterName.equals("")) {
085: missingPropertyError("master-name");
086: }
087: }
088:
089: /**
090: * @see org.apache.fop.fo.FONode#startOfNode
091: */
092: protected void startOfNode() throws FOPException {
093: LayoutMasterSet layoutMasterSet = (LayoutMasterSet) parent;
094:
095: if (masterName == null) {
096: missingPropertyError("master-name");
097: } else {
098: layoutMasterSet.addSimplePageMaster(this );
099: }
100:
101: //Well, there are only 5 regions so we can save a bit of memory here
102: regions = new HashMap(5);
103: }
104:
105: /**
106: * Make sure content model satisfied.
107: * @see org.apache.fop.fo.FONode#endOfNode
108: */
109: protected void endOfNode() throws FOPException {
110: if (!hasRegionBody) {
111: missingChildElementError("(region-body, region-before?, region-after?, region-start?, region-end?)");
112: }
113: }
114:
115: /**
116: * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
117: * XSL Content Model: (region-body,region-before?,region-after?,region-start?,region-end?)
118: */
119: protected void validateChildNode(Locator loc, String nsURI,
120: String localName) throws ValidationException {
121: if (FO_URI.equals(nsURI) && localName.equals("region-body")) {
122: if (hasRegionBody) {
123: tooManyNodesError(loc, "fo:region-body");
124: } else {
125: hasRegionBody = true;
126: }
127: } else if (FO_URI.equals(nsURI)
128: && localName.equals("region-before")) {
129: if (!hasRegionBody) {
130: nodesOutOfOrderError(loc, "fo:region-body",
131: "fo:region-before");
132: } else if (hasRegionBefore) {
133: tooManyNodesError(loc, "fo:region-before");
134: } else if (hasRegionAfter) {
135: nodesOutOfOrderError(loc, "fo:region-before",
136: "fo:region-after");
137: } else if (hasRegionStart) {
138: nodesOutOfOrderError(loc, "fo:region-before",
139: "fo:region-start");
140: } else if (hasRegionEnd) {
141: nodesOutOfOrderError(loc, "fo:region-before",
142: "fo:region-end");
143: } else {
144: hasRegionBody = true;
145: }
146: } else if (FO_URI.equals(nsURI)
147: && localName.equals("region-after")) {
148: if (!hasRegionBody) {
149: nodesOutOfOrderError(loc, "fo:region-body",
150: "fo:region-after");
151: } else if (hasRegionAfter) {
152: tooManyNodesError(loc, "fo:region-after");
153: } else if (hasRegionStart) {
154: nodesOutOfOrderError(loc, "fo:region-after",
155: "fo:region-start");
156: } else if (hasRegionEnd) {
157: nodesOutOfOrderError(loc, "fo:region-after",
158: "fo:region-end");
159: } else {
160: hasRegionAfter = true;
161: }
162: } else if (FO_URI.equals(nsURI)
163: && localName.equals("region-start")) {
164: if (!hasRegionBody) {
165: nodesOutOfOrderError(loc, "fo:region-body",
166: "fo:region-start");
167: } else if (hasRegionStart) {
168: tooManyNodesError(loc, "fo:region-start");
169: } else if (hasRegionEnd) {
170: nodesOutOfOrderError(loc, "fo:region-start",
171: "fo:region-end");
172: } else {
173: hasRegionStart = true;
174: }
175: } else if (FO_URI.equals(nsURI)
176: && localName.equals("region-end")) {
177: if (!hasRegionBody) {
178: nodesOutOfOrderError(loc, "fo:region-body",
179: "fo:region-end");
180: } else if (hasRegionEnd) {
181: tooManyNodesError(loc, "fo:region-end");
182: } else {
183: hasRegionEnd = true;
184: }
185: } else {
186: invalidChildError(loc, nsURI, localName);
187: }
188: }
189:
190: /**
191: * @see org.apache.fop.fo.FObj#generatesReferenceAreas()
192: */
193: public boolean generatesReferenceAreas() {
194: return true;
195: }
196:
197: /**
198: * @see org.apache.fop.fo.FONode#addChildNode(FONode)
199: */
200: protected void addChildNode(FONode child) throws FOPException {
201: if (child instanceof Region) {
202: addRegion((Region) child);
203: } else {
204: super .addChildNode(child);
205: }
206: }
207:
208: /**
209: * Adds a region to this simple-page-master.
210: * @param region region to add
211: */
212: protected void addRegion(Region region) {
213: String key = String.valueOf(region.getNameId());
214: regions.put(key, region);
215: }
216:
217: /**
218: * Returns the region for a given region class.
219: * @param regionId Constants ID of the FO representing the region
220: * @return the region, null if it doesn't exist
221: */
222: public Region getRegion(int regionId) {
223: return (Region) regions.get(String.valueOf(regionId));
224: }
225:
226: /**
227: * Returns a Map of regions associated with this simple-page-master
228: * @return the regions
229: */
230: public Map getRegions() {
231: return regions;
232: }
233:
234: /**
235: * Indicates if a region with a given name exists in this
236: * simple-page-master.
237: * @param regionName name of the region to lookup
238: * @return True if a region with this name exists
239: */
240: protected boolean regionNameExists(String regionName) {
241: for (Iterator regenum = regions.values().iterator(); regenum
242: .hasNext();) {
243: Region r = (Region) regenum.next();
244: if (r.getRegionName().equals(regionName)) {
245: return true;
246: }
247: }
248: return false;
249: }
250:
251: /** @return the Common Margin Properties-Block. */
252: public CommonMarginBlock getCommonMarginBlock() {
253: return commonMarginBlock;
254: }
255:
256: /** @return "master-name" property. */
257: public String getMasterName() {
258: return masterName;
259: }
260:
261: /** @return the "page-width" property. */
262: public Length getPageWidth() {
263: return pageWidth;
264: }
265:
266: /** @return the "page-height" property. */
267: public Length getPageHeight() {
268: return pageHeight;
269: }
270:
271: /** @return the "writing-mode" property. */
272: public int getWritingMode() {
273: return writingMode;
274: }
275:
276: /** @return the "reference-orientation" property. */
277: public int getReferenceOrientation() {
278: return referenceOrientation.getValue();
279: }
280:
281: /** @see org.apache.fop.fo.FONode#getLocalName() */
282: public String getLocalName() {
283: return "simple-page-master";
284: }
285:
286: /**
287: * @see org.apache.fop.fo.FObj#getNameId()
288: */
289: public int getNameId() {
290: return FO_SIMPLE_PAGE_MASTER;
291: }
292: }
|