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.Collection;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.apache.xmlgraphics.ps.PSGenerator;
030: import org.apache.xmlgraphics.ps.PSProcSet;
031: import org.apache.xmlgraphics.ps.PSResource;
032:
033: /**
034: * Abstract base class for Resource DSC comments (DocumentNeededResources,
035: * DocumentSuppliedResources and PageResources).
036: */
037: public abstract class AbstractResourcesDSCComment extends
038: AbstractDSCComment {
039:
040: private Set resources;
041:
042: /**
043: * Creates a new instance.
044: */
045: public AbstractResourcesDSCComment() {
046: super ();
047: }
048:
049: /**
050: * Creates a new instance.
051: * @param resources a Collection of PSResource instances
052: */
053: public AbstractResourcesDSCComment(Collection resources) {
054: addResources(resources);
055: }
056:
057: /**
058: * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues()
059: */
060: public boolean hasValues() {
061: return true;
062: }
063:
064: /**
065: * Adds a new resource.
066: * @param res the resource
067: */
068: public void addResource(PSResource res) {
069: if (this .resources == null) {
070: this .resources = new java.util.HashSet();
071: }
072: this .resources.add(res);
073: }
074:
075: /**
076: * Adds a collection of resources.
077: * @param resources a Collection of PSResource instances.
078: */
079: public void addResources(Collection resources) {
080: if (resources != null) {
081: if (this .resources == null) {
082: this .resources = new java.util.HashSet();
083: }
084: this .resources.addAll(resources);
085: }
086: }
087:
088: /**
089: * Returns the set of resources associated with this DSC comment.
090: * @return the set of resources
091: */
092: public Set getResources() {
093: return Collections.unmodifiableSet(this .resources);
094: }
095:
096: /**
097: * Defines the known resource types (font, procset, file, pattern etc.).
098: */
099: protected static final Set RESOURCE_TYPES = new java.util.HashSet();
100:
101: static {
102: RESOURCE_TYPES.add(PSResource.TYPE_FONT);
103: RESOURCE_TYPES.add(PSResource.TYPE_PROCSET);
104: RESOURCE_TYPES.add(PSResource.TYPE_FILE);
105: RESOURCE_TYPES.add(PSResource.TYPE_PATTERN);
106: RESOURCE_TYPES.add(PSResource.TYPE_FORM);
107: RESOURCE_TYPES.add(PSResource.TYPE_ENCODING);
108: }
109:
110: /**
111: * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String)
112: */
113: public void parseValue(String value) {
114: List params = splitParams(value);
115: String currentResourceType = null;
116: Iterator iter = params.iterator();
117: while (iter.hasNext()) {
118: String name = (String) iter.next();
119: if (RESOURCE_TYPES.contains(name)) {
120: currentResourceType = name;
121: }
122: if (currentResourceType == null) {
123: throw new IllegalArgumentException(
124: "<resources> must begin with a resource type. Found: "
125: + name);
126: }
127: if (PSResource.TYPE_FONT.equals(currentResourceType)) {
128: String fontname = (String) iter.next();
129: addResource(new PSResource(name, fontname));
130: } else if (PSResource.TYPE_FORM.equals(currentResourceType)) {
131: String formname = (String) iter.next();
132: addResource(new PSResource(name, formname));
133: } else if (PSResource.TYPE_PROCSET
134: .equals(currentResourceType)) {
135: String procname = (String) iter.next();
136: String version = (String) iter.next();
137: String revision = (String) iter.next();
138: addResource(new PSProcSet(procname, Float
139: .parseFloat(version), Integer
140: .parseInt(revision)));
141: } else if (PSResource.TYPE_FILE.equals(currentResourceType)) {
142: String filename = (String) iter.next();
143: addResource(new PSResource(name, filename));
144: } else {
145: throw new IllegalArgumentException(
146: "Invalid resource type: " + currentResourceType);
147: }
148: }
149: }
150:
151: /**
152: * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(
153: * org.apache.xmlgraphics.ps.PSGenerator)
154: */
155: public void generate(PSGenerator gen) throws IOException {
156: if (resources == null || resources.size() == 0) {
157: return;
158: }
159: StringBuffer sb = new StringBuffer();
160: sb.append("%%").append(getName()).append(": ");
161: boolean first = true;
162: Iterator i = resources.iterator();
163: while (i.hasNext()) {
164: if (!first) {
165: gen.writeln(sb.toString());
166: sb.setLength(0);
167: sb.append("%%+ ");
168: }
169: PSResource res = (PSResource) i.next();
170: sb.append(res.getResourceSpecification());
171: first = false;
172: }
173: gen.writeln(sb.toString());
174: }
175:
176: }
|