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.util.List;
023:
024: /**
025: * Abstract base class for DSC comments.
026: */
027: public abstract class AbstractDSCComment extends AbstractEvent
028: implements DSCComment {
029:
030: private final boolean isWhitespace(char c) {
031: return c == ' ' || c == '\t';
032: }
033:
034: private int parseNextParam(String value, int pos, List lst) {
035: int startPos = pos;
036: pos++;
037: while (pos < value.length() && !isWhitespace(value.charAt(pos))) {
038: pos++;
039: }
040: String param = value.substring(startPos, pos);
041: lst.add(param);
042: return pos;
043: }
044:
045: private int parseNextParentheseString(String value, int pos,
046: List lst) {
047: int nestLevel = 1;
048: pos++;
049: StringBuffer sb = new StringBuffer();
050: while (pos < value.length() && nestLevel > 0) {
051: final char c = value.charAt(pos);
052: switch (c) {
053: case '(':
054: nestLevel++;
055: if (nestLevel > 1) {
056: sb.append(c);
057: }
058: break;
059: case ')':
060: if (nestLevel > 1) {
061: sb.append(c);
062: }
063: nestLevel--;
064: break;
065: case '\\':
066: pos++;
067: char cnext = value.charAt(pos);
068: switch (cnext) {
069: case '\\':
070: sb.append(cnext);
071: break;
072: case 'n':
073: sb.append('\n');
074: break;
075: case 'r':
076: sb.append('\r');
077: break;
078: case 't':
079: sb.append('\t');
080: break;
081: case 'b':
082: sb.append('\b');
083: break;
084: case 'f':
085: sb.append('\f');
086: break;
087: case '(':
088: sb.append('(');
089: break;
090: case ')':
091: sb.append(')');
092: break;
093: default:
094: int code = Integer.parseInt(value.substring(pos,
095: pos + 3), 8);
096: sb.append((char) code);
097: pos += 2;
098: }
099: break;
100: default:
101: sb.append(c);
102: }
103: pos++;
104: }
105: lst.add(sb.toString());
106: pos++;
107: return pos;
108: }
109:
110: /**
111: * Splits the params of the DSC comment value in to a List.
112: * @param value the DSC comment value
113: * @return the List of values
114: */
115: protected List splitParams(String value) {
116: List lst = new java.util.ArrayList();
117: int pos = 0;
118: value = value.trim();
119: while (pos < value.length()) {
120: if (isWhitespace(value.charAt(pos))) {
121: pos++;
122: continue;
123: }
124: if (value.charAt(pos) == '(') {
125: pos = parseNextParentheseString(value, pos, lst);
126: } else {
127: pos = parseNextParam(value, pos, lst);
128: }
129: }
130: return lst;
131: }
132:
133: /**
134: * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#isAtend()
135: */
136: public boolean isAtend() {
137: return false;
138: }
139:
140: /**
141: * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#asDSCComment()
142: */
143: public DSCComment asDSCComment() {
144: return this ;
145: }
146:
147: /**
148: * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#isDSCComment()
149: */
150: public boolean isDSCComment() {
151: return true;
152: }
153:
154: /**
155: * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#getEventType()
156: */
157: public int getEventType() {
158: return DSC_COMMENT;
159: }
160: }
|