001: /*
002: * $Id: Entity.java,v 1.3 2006/11/28 18:50:04 spericas Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: /*
030: * The Apache Software License, Version 1.1
031: *
032: *
033: * Copyright (c) 1999-2002 The Apache Software Foundation.
034: * All rights reserved.
035: *
036: * Redistribution and use in source and binary forms, with or without
037: * modification, are permitted provided that the following conditions
038: * are met:
039: *
040: * 1. Redistributions of source code must retain the above copyright
041: * notice, this list of conditions and the following disclaimer.
042: *
043: * 2. Redistributions in binary form must reproduce the above copyright
044: * notice, this list of conditions and the following disclaimer in
045: * the documentation and/or other materials provided with the
046: * distribution.
047: *
048: * 3. The end-user documentation included with the redistribution,
049: * if any, must include the following acknowledgment:
050: * "This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/)."
052: * Alternately, this acknowledgment may appear in the software itself,
053: * if and wherever such third-party acknowledgments normally appear.
054: *
055: * 4. The names "Xerces" and "Apache Software Foundation" must
056: * not be used to endorse or promote products derived from this
057: * software without prior written permission. For written
058: * permission, please contact apache@apache.org.
059: *
060: * 5. Products derived from this software may not be called "Apache",
061: * nor may "Apache" appear in their name, without prior written
062: * permission of the Apache Software Foundation.
063: *
064: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
065: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
066: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
067: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
068: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
069: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
070: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
071: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
072: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
073: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
074: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
075: * SUCH DAMAGE.
076: */
077: /*
078: * Entity.java
079: *
080: * Created on December 2, 2002, 3:10 PM
081: */
082:
083: package com.sun.xml.stream;
084:
085: import com.sun.xml.stream.util.BufferAllocator;
086: import com.sun.xml.stream.util.ThreadLocalBufferAllocator;
087: import java.io.IOException;
088: import java.io.InputStream;
089: import java.io.Reader;
090:
091: import com.sun.xml.stream.xerces.xni.XMLResourceIdentifier;
092:
093: /**
094: * Entity information.
095: *
096: * @author
097: */
098: public abstract class Entity {
099:
100: //
101: // Data
102: //
103:
104: //xxx why dont we declare the type of entities, like assign integer for external/ internal etc..
105:
106: /** Entity name. */
107: public String name;
108:
109: // whether this entity's declaration was found in the internal
110: // or external subset
111: public boolean inExternalSubset;
112:
113: //
114: // Constructors
115: //
116:
117: /** Default constructor. */
118: public Entity() {
119: clear();
120: } // <init>()
121:
122: /** Constructs an entity. */
123: public Entity(String name, boolean inExternalSubset) {
124: this .name = name;
125: this .inExternalSubset = inExternalSubset;
126: } // <init>(String)
127:
128: //
129: // Public methods
130: //
131:
132: /** Returns true if this entity was declared in the external subset. */
133: public boolean isEntityDeclInExternalSubset() {
134: return inExternalSubset;
135: }
136:
137: /** Returns true if this is an external entity. */
138: public abstract boolean isExternal();
139:
140: /** Returns true if this is an unparsed entity. */
141: public abstract boolean isUnparsed();
142:
143: /** Clears the entity. */
144: public void clear() {
145: name = null;
146: inExternalSubset = false;
147: } // clear()
148:
149: /** Sets the values of the entity. */
150: public void setValues(Entity entity) {
151: name = entity.name;
152: inExternalSubset = entity.inExternalSubset;
153: } // setValues(Entity)
154:
155: /**
156: * Internal entity.
157: *
158: * @author nb131165
159: */
160: public static class InternalEntity extends Entity {
161:
162: //
163: // Data
164: //
165:
166: /** Text value of entity. */
167: public String text;
168:
169: //
170: // Constructors
171: //
172:
173: /** Default constructor. */
174: public InternalEntity() {
175: clear();
176: } // <init>()
177:
178: /** Constructs an internal entity. */
179: public InternalEntity(String name, String text,
180: boolean inExternalSubset) {
181: super (name, inExternalSubset);
182: this .text = text;
183: } // <init>(String,String)
184:
185: //
186: // Entity methods
187: //
188:
189: /** Returns true if this is an external entity. */
190: public final boolean isExternal() {
191: return false;
192: } // isExternal():boolean
193:
194: /** Returns true if this is an unparsed entity. */
195: public final boolean isUnparsed() {
196: return false;
197: } // isUnparsed():boolean
198:
199: /** Clears the entity. */
200: public void clear() {
201: super .clear();
202: text = null;
203: } // clear()
204:
205: /** Sets the values of the entity. */
206: public void setValues(Entity entity) {
207: super .setValues(entity);
208: text = null;
209: } // setValues(Entity)
210:
211: /** Sets the values of the entity. */
212: public void setValues(InternalEntity entity) {
213: super .setValues(entity);
214: text = entity.text;
215: } // setValues(InternalEntity)
216:
217: } // class InternalEntity
218:
219: /**
220: * External entity.
221: *
222: * @author nb131165
223: */
224: public static class ExternalEntity extends Entity {
225:
226: //
227: // Data
228: //
229:
230: /** container for all relevant entity location information. */
231: public XMLResourceIdentifier entityLocation;
232:
233: /** Notation name for unparsed entity. */
234: public String notation;
235:
236: //
237: // Constructors
238: //
239:
240: /** Default constructor. */
241: public ExternalEntity() {
242: clear();
243: } // <init>()
244:
245: /** Constructs an internal entity. */
246: public ExternalEntity(String name,
247: XMLResourceIdentifier entityLocation, String notation,
248: boolean inExternalSubset) {
249: super (name, inExternalSubset);
250: this .entityLocation = entityLocation;
251: this .notation = notation;
252: } // <init>(String,XMLResourceIdentifier, String)
253:
254: //
255: // Entity methods
256: //
257:
258: /** Returns true if this is an external entity. */
259: public final boolean isExternal() {
260: return true;
261: } // isExternal():boolean
262:
263: /** Returns true if this is an unparsed entity. */
264: public final boolean isUnparsed() {
265: return notation != null;
266: } // isUnparsed():boolean
267:
268: /** Clears the entity. */
269: public void clear() {
270: super .clear();
271: entityLocation = null;
272: notation = null;
273: } // clear()
274:
275: /** Sets the values of the entity. */
276: public void setValues(Entity entity) {
277: super .setValues(entity);
278: entityLocation = null;
279: notation = null;
280: } // setValues(Entity)
281:
282: /** Sets the values of the entity. */
283: public void setValues(ExternalEntity entity) {
284: super .setValues(entity);
285: entityLocation = entity.entityLocation;
286: notation = entity.notation;
287: } // setValues(ExternalEntity)
288:
289: } // class ExternalEntity
290:
291: /**
292: * Entity state.
293: *
294: * @author nb131165
295: */
296: public static class ScannedEntity extends Entity {
297:
298: /** Default buffer size (4096). */
299: public static final int DEFAULT_BUFFER_SIZE = 8192;
300: //4096;
301:
302: /**
303: * Buffer size. We get this value from a property. The default size
304: * is used if the input buffer size property is not specified.
305: * REVISIT: do we need a property for internal entity buffer size?
306: */
307: public int fBufferSize = DEFAULT_BUFFER_SIZE;
308:
309: /** Default buffer size before we've finished with the XMLDecl: */
310: public static final int DEFAULT_XMLDECL_BUFFER_SIZE = 64;
311:
312: /** Default internal entity buffer size (1024). */
313: public static final int DEFAULT_INTERNAL_BUFFER_SIZE = 1024;
314:
315: //
316: // Data
317: //
318:
319: // i/o
320:
321: /** XXX let these field remain public right now, though we have defined methods for them.
322: * Input stream. */
323: public InputStream stream;
324:
325: /** XXX let these field remain public right now, though we have defined methods for them.
326: * Reader. */
327: public Reader reader;
328:
329: // locator information
330:
331: /** entity location information */
332: public XMLResourceIdentifier entityLocation;
333:
334: // encoding
335:
336: /** Auto-detected encoding. */
337: public String encoding;
338:
339: // status
340:
341: /** True if in a literal. */
342: public boolean literal;
343:
344: // whether this is an external or internal scanned entity
345: public boolean isExternal;
346:
347: //each 'external' parsed entity may have xml/text declaration containing version information
348: public String version;
349:
350: // buffer
351:
352: /** Character buffer. */
353: public char[] ch = null;
354:
355: /** Position in character buffer at any point of time. */
356: public int position;
357:
358: /** Count of characters present in buffer. */
359: public int count;
360:
361: public int lineNumber = 1;
362: public int columnNumber = 1;
363:
364: /** This variable is used to calculate the current position in the XML stream.
365: Note that fCurrentEntity.position maintains the position relative to
366: the buffer.
367: * At any point of time absolute position in the XML stream can be calculated
368: * as fTotalCountTillLastLoad + fCurrentEntity.position
369: */
370: public int fTotalCountTillLastLoad;
371:
372: /** This variable stores the number of characters read during the load()
373: * operation. It is used to calculate fTotalCountTillLastLoad
374: */
375: public int fLastCount;
376:
377: // to allow the reader/inputStream to behave efficiently:
378: public boolean mayReadChunks;
379:
380: /** returns the name of the current encoding
381: * @return current encoding name
382: */
383: public String getEncodingName() {
384: return encoding;
385: }
386:
387: /**each 'external' parsed entity may have xml/text declaration containing version information
388: * @return String version of the enity, for an internal entity version would be null
389: */
390: public String getEntityVersion() {
391: return version;
392: }
393:
394: /** each 'external' parsed entity may have xml/text declaration containing version information
395: * @param String version of the external parsed entity
396: */
397: public void setEntityVersion(String version) {
398: this .version = version;
399: }
400:
401: /** Returns the java.io.Reader associated with this entity.Readers are used
402: * to read from the file. Readers wrap any particular InputStream that was
403: * used to open the entity.
404: * @return java.io.Reader Reader associated with this entity
405: */
406: public Reader getEntityReader() {
407: return reader;
408: }
409:
410: /** if entity was opened using the stream, return the associated inputstream
411: * with this entity
412: *@return java.io.InputStream InputStream associated with this entity
413: */
414: public InputStream getEntityInputStream() {
415: return stream;
416: }
417:
418: //
419: // Constructors
420: //
421:
422: /** Constructs a scanned entity. */
423: public ScannedEntity(String name,
424: XMLResourceIdentifier entityLocation,
425: InputStream stream, Reader reader, String encoding,
426: boolean literal, boolean mayReadChunks,
427: boolean isExternal) {
428: this .name = name;
429: this .entityLocation = entityLocation;
430: this .stream = stream;
431: this .reader = reader;
432: this .encoding = encoding;
433: this .literal = literal;
434: this .mayReadChunks = mayReadChunks;
435: this .isExternal = isExternal;
436: final int size = isExternal ? fBufferSize
437: : DEFAULT_INTERNAL_BUFFER_SIZE;
438: BufferAllocator ba = ThreadLocalBufferAllocator
439: .getBufferAllocator();
440: ch = ba.getCharBuffer(size);
441: if (ch == null) {
442: this .ch = new char[size];
443: }
444: } // <init>(StringXMLResourceIdentifier,InputStream,Reader,String,boolean, boolean)
445:
446: /**
447: * Release any resources associated with this entity.
448: */
449: public void close() throws IOException {
450: BufferAllocator ba = ThreadLocalBufferAllocator
451: .getBufferAllocator();
452: ba.returnCharBuffer(ch);
453: ch = null;
454: reader.close();
455: }
456:
457: //
458: // Entity methods
459: //
460:
461: /** Returns true if this is an external entity. */
462: public final boolean isExternal() {
463: return isExternal;
464: } // isExternal():boolean
465:
466: /** Returns true if this is an unparsed entity. */
467: public final boolean isUnparsed() {
468: return false;
469: } // isUnparsed():boolean
470:
471: //
472: // Object methods
473: //
474:
475: /** Returns a string representation of this object. */
476: public String toString() {
477:
478: StringBuffer str = new StringBuffer();
479: str.append("name=\"" + name + '"');
480: str.append(",ch=" + new String(ch));
481: str.append(",position=" + position);
482: str.append(",count=" + count);
483: return str.toString();
484:
485: } // toString():String
486:
487: } // class ScannedEntity
488:
489: } // class Entity
|