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;
021:
022: import java.io.IOException;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: import org.apache.xmlgraphics.ps.PSGenerator;
029: import org.apache.xmlgraphics.ps.PSResource;
030: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentNeededResources;
031: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentSuppliedResources;
032: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageResources;
033:
034: /**
035: * This class is used to track resources in a DSC-compliant PostScript file. The distinction is
036: * made between supplied and needed resources. For the details of this distinction, please see
037: * the DSC specification.
038: */
039: public class ResourceTracker {
040:
041: private Set documentSuppliedResources;
042: private Set documentNeededResources;
043: private Set usedResources;
044: private Set pageResources;
045:
046: /**
047: * Returns the set of supplied resources.
048: * @return the set of supplied resources
049: */
050: public Set getDocumentSuppliedResources() {
051: if (documentSuppliedResources != null) {
052: return Collections
053: .unmodifiableSet(documentSuppliedResources);
054: } else {
055: return Collections.EMPTY_SET;
056: }
057: }
058:
059: /**
060: * Returns the set of needed resources.
061: * @return the set of needed resources
062: */
063: public Set getDocumentNeededResources() {
064: if (documentNeededResources != null) {
065: return Collections.unmodifiableSet(documentNeededResources);
066: } else {
067: return Collections.EMPTY_SET;
068: }
069: }
070:
071: /**
072: * Notifies the resource tracker that a new page has been started and that the page resource
073: * set can be cleared.
074: */
075: public void notifyStartNewPage() {
076: if (pageResources != null) {
077: pageResources.clear();
078: }
079: }
080:
081: /**
082: * Registers a supplied resource. If the same resources is already in the set of needed
083: * resources, it is removed there.
084: * @param res the resource
085: */
086: public void registerSuppliedResource(PSResource res) {
087: if (documentSuppliedResources == null) {
088: documentSuppliedResources = new java.util.HashSet();
089: }
090: documentSuppliedResources.add(res);
091: if (documentNeededResources != null) {
092: documentNeededResources.remove(res);
093: }
094: }
095:
096: /**
097: * Registers a needed resource. If the same resources is already in the set of supplied
098: * resources, it is ignored, i.e. it is assumed to be supplied.
099: * @param res the resource
100: */
101: public void registerNeededResource(PSResource res) {
102: if (documentNeededResources == null) {
103: documentNeededResources = new java.util.HashSet();
104: }
105: if (!documentSuppliedResources.contains(res)) {
106: documentNeededResources.add(res);
107: }
108: }
109:
110: /**
111: * Notifies the resource tracker about the usage of a resource on the current page.
112: * @param res the resource being used
113: */
114: public void notifyResourceUsageOnPage(PSResource res) {
115: if (pageResources == null) {
116: pageResources = new java.util.HashSet();
117: }
118: pageResources.add(res);
119: }
120:
121: /**
122: * Notifies the resource tracker about the usage of resources on the current page.
123: * @param resources the resources being used
124: */
125: public void notifyResourceUsageOnPage(Collection resources) {
126: if (pageResources == null) {
127: pageResources = new java.util.HashSet();
128: }
129: pageResources.addAll(resources);
130: }
131:
132: /**
133: * Indicates whether a particular resource is supplied, rather than needed.
134: * @param res the resource
135: * @return true if the resource is registered as being supplied.
136: */
137: public boolean isResourceSupplied(PSResource res) {
138: return (documentSuppliedResources != null)
139: && documentSuppliedResources.contains(res);
140: }
141:
142: /**
143: * Writes a DSC comment for the accumulated used resources, either at page level or
144: * at document level.
145: * @param pageLevel true if the DSC comment for the page level should be generated,
146: * false for the document level (in the trailer)
147: * @param gen the PSGenerator to write the DSC comments with
148: * @exception IOException In case of an I/O problem
149: */
150: public void writeResources(boolean pageLevel, PSGenerator gen)
151: throws IOException {
152: if (pageLevel) {
153: new DSCCommentPageResources(pageResources).generate(gen);
154: if (usedResources == null) {
155: usedResources = new java.util.HashSet();
156: }
157: usedResources.addAll(pageResources);
158: } else {
159: if (usedResources != null) {
160: Iterator iter = usedResources.iterator();
161: while (iter.hasNext()) {
162: PSResource res = (PSResource) iter.next();
163: if (documentSuppliedResources == null
164: || !documentSuppliedResources.contains(res)) {
165: registerNeededResource(res);
166: }
167: }
168: }
169: new DSCCommentDocumentNeededResources(
170: documentNeededResources).generate(gen);
171: new DSCCommentDocumentSuppliedResources(
172: documentSuppliedResources).generate(gen);
173: }
174: }
175:
176: }
|