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$ */
019:
020: package org.apache.xmlgraphics.ps.dsc.events;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.xmlgraphics.ps.DSCConstants;
027: import org.apache.xmlgraphics.ps.PSGenerator;
028:
029: /**
030: * Represents a %%Page DSC comment.
031: */
032: public class DSCCommentPage extends AbstractDSCComment {
033:
034: private String pageName;
035: private int pagePosition = -1;
036:
037: /**
038: * Creates a new instance.
039: */
040: public DSCCommentPage() {
041: }
042:
043: /**
044: * Creates a new instance.
045: * @param pageName the name of the page
046: * @param pagePosition the position of the page within the file (1-based)
047: */
048: public DSCCommentPage(String pageName, int pagePosition) {
049: setPageName(pageName);
050: setPagePosition(pagePosition);
051: }
052:
053: /**
054: * Creates a new instance. The page name will be set to the same value as the page position.
055: * @param pagePosition the position of the page within the file (1-based)
056: */
057: public DSCCommentPage(int pagePosition) {
058: this (Integer.toString(pagePosition), pagePosition);
059: }
060:
061: /**
062: * Resturns the name of the page.
063: * @return the page name
064: */
065: public String getPageName() {
066: return this .pageName;
067: }
068:
069: /**
070: * Sets the page name.
071: * @param name the page name
072: */
073: public void setPageName(String name) {
074: this .pageName = name;
075: }
076:
077: /**
078: * Returns the page position.
079: * @return the page position (1-based)
080: */
081: public int getPagePosition() {
082: return this .pagePosition;
083: }
084:
085: /**
086: * Sets the page position.
087: * @param position the page position (1-based)
088: */
089: public void setPagePosition(int position) {
090: if (position <= 0) {
091: throw new IllegalArgumentException(
092: "position must be 1 or above");
093: }
094: this .pagePosition = position;
095: }
096:
097: /**
098: * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName()
099: */
100: public String getName() {
101: return DSCConstants.PAGE;
102: }
103:
104: /**
105: * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues()
106: */
107: public boolean hasValues() {
108: return true;
109: }
110:
111: /**
112: * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String)
113: */
114: public void parseValue(String value) {
115: List params = splitParams(value);
116: Iterator iter = params.iterator();
117: this .pageName = (String) iter.next();
118: this .pagePosition = Integer.parseInt((String) iter.next());
119: }
120:
121: /**
122: * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(
123: * org.apache.xmlgraphics.ps.PSGenerator)
124: */
125: public void generate(PSGenerator gen) throws IOException {
126: gen.writeDSCComment(getName(), new Object[] { getPageName(),
127: new Integer(getPagePosition()) });
128: }
129:
130: }
|