diff options
author | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-07-16 00:30:23 +0000 |
---|---|---|
committer | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-07-16 00:30:23 +0000 |
commit | c8875fb97fc03779a5bba09872227b1d08e5d52a (patch) | |
tree | a0b991cf5866ae1d616639b906ac001811d74508 /libjava/classpath/gnu | |
parent | c40c1730800ed292b6db39a83d592476fa59623c (diff) | |
download | gcc-c8875fb97fc03779a5bba09872227b1d08e5d52a.tar.gz |
Initial revision
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@102074 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/classpath/gnu')
944 files changed, 184395 insertions, 0 deletions
diff --git a/libjava/classpath/gnu/CORBA/Asynchron.java b/libjava/classpath/gnu/CORBA/Asynchron.java new file mode 100644 index 00000000000..275b5709120 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Asynchron.java @@ -0,0 +1,185 @@ +/* Asynchron.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.Request; +import org.omg.CORBA.WrongTransaction; + +import java.util.Iterator; +import java.util.LinkedList; + +/** + * Handles the asynchronous dynamic invocations. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Asynchron +{ + LinkedList sent = new LinkedList(); + + /** + * Send multiple prepared requests one way, do not caring about the answer. + * The messages, containing requests, will be marked, indicating that + * the sender is not expecting to get a reply. + * + * @param requests the prepared array of requests. + * + * @see Request#send_oneway() + */ + public void send_multiple_requests_oneway(Request[] requests) + { + for (int i = 0; i < requests.length; i++) + { + requests [ i ].send_oneway(); + } + } + + /** + * Send multiple prepared requests expecting to get a reply. All requests + * are send in parallel, each in its own separate thread. When the + * reply arrives, it is stored in the agreed fields of the corresponing + * request data structure. If this method is called repeatedly, + * the new requests are added to the set of the currently sent requests, + * but the old set is not discarded. + * + * @param requests the prepared array of requests. + * + * @see #poll_next_response() + * @see #get_next_response() + * @see Request#send_deferred() + */ + public void send_multiple_requests_deferred(Request[] requests) + { + synchronized (sent) + { + for (int i = 0; i < requests.length; i++) + { + sent.add(requests [ i ]); + + // TODO Reuse threads that are instantiated in the method below, + // one thread per call. + requests [ i ].send_deferred(); + } + } + } + + /** + * Find if any of the requests that have been previously sent with + * {@link #send_multiple_requests_deferred}, have a response yet. + * + * @return true if there is at least one response to the previously + * sent request, false otherwise. + */ + public boolean poll_next_response() + { + synchronized (sent) + { + Iterator iter = sent.iterator(); + Request r; + while (iter.hasNext()) + { + r = (Request) iter.next(); + if (r.poll_response()) + return true; + } + } + return false; + } + + /** + * Get the next instance with a response being received. If all currently + * sent responses not yet processed, this method pauses till at least one of + * them is complete. If there are no requests currently sent, the method + * pauses till some request is submitted and the response is received. + * This strategy is identical to the one accepted by Suns 1.4 ORB + * implementation. + * + * The returned response is removed from the list of the currently + * submitted responses and is never returned again. + * + * @return the previously sent request that now contains the received + * response. + * + * @throws WrongTransaction If the method was called from the transaction + * scope different than the one, used to send the request. The exception + * can be raised only if the request is implicitly associated with some + * particular transaction. + */ + public Request get_next_response() + throws WrongTransaction + { + // The hard-coded waiting times for the incremental waiter. + // TODO it is possible to write more tricky system where the + // requests notify the Asynchron when they are complete. + // Wait for 5 ms intially. + int wait = 8; + + // Double the waiting time + int INC = 2; + + // Do not increase if the waiting time is already over 500 ms. + int MAX = 500; + while (true) + { + synchronized (sent) + { + Iterator iter = sent.iterator(); + Request r; + while (iter.hasNext()) + { + r = (Request) iter.next(); + if (r.poll_response()) + { + sent.remove(r); + return r; + } + } + } + try + { + Thread.sleep(wait); + if (wait < MAX) + wait = wait * INC; + } + catch (InterruptedException ex) + { + } + } + } +} diff --git a/libjava/classpath/gnu/CORBA/BigDecimalHelper.java b/libjava/classpath/gnu/CORBA/BigDecimalHelper.java new file mode 100644 index 00000000000..f1e1dd6b42c --- /dev/null +++ b/libjava/classpath/gnu/CORBA/BigDecimalHelper.java @@ -0,0 +1,193 @@ +/* BigDecimalHelper.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; + +/** + * Reads and writes BigDecimal as CORBA <code>fixed</code>. + * The format, described in CORBA specification, requires to store + * data in hexadecimal format, two digits per byte (oceted), most + * significant digit first. The last half-byte in the representation + * stores the sign, being 0xD for negative numbers and 0xC for + * zero and positive numbers. To have the even number of half bytes, + * 0x0 is appended to the beginning, if required. The position of the + * decimal point is not stored. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class BigDecimalHelper +{ + { + } + + /** + * @todo remove from the release version. + */ + public static void main(String[] args) + { + try + { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + BigDecimal d = new BigDecimal("12234.54689"); + + write(b, d); + + byte[] a = b.toByteArray(); + + for (int i = 0; i < a.length; i++) + { + int k = a [ i ] & 0xFF; + System.out.print(Integer.toHexString(k) + " "); + } + + System.out.println("Now reading"); + + ByteArrayInputStream bin = new ByteArrayInputStream(a); + + BigDecimal r = read(bin, d.scale()); + + System.out.println(r); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + /** + * Read the CORBA fixed, autodetecting the number of bytes + * and assuming the given scale. + */ + public static BigDecimal read(java.io.InputStream in, int scale) + throws IOException + { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + + int f; + + do + { + f = in.read(); + if (f >= 0) + bout.write(f); + } + // The last byte has 0xC or 0xD in the last halfbyte. + while ((f & 0xF) <= 0x9); + + return createFixed(scale, bout.toByteArray()); + } + + /** + * Write the big decimal as CORBA <code>fixed<.code>. + * The scale will not be stored. + * + * @param out a stream to write into. + * @param x a big decimal to write. + * @param digits a number of the decimal digits in the record + * being written. For the smaller + * numbers, zeroes are added to the left. + * + * @throws IOException if the stream write method throws one. + * @throws BadKind if this BigDecimal has more digits than + * specified. + */ + public static void write(java.io.OutputStream out, BigDecimal x) + throws IOException, BadKind + { + StringBuffer v = new StringBuffer(x.unscaledValue().toString()); + + boolean negative = v.charAt(0) == '-'; + + if (negative) + v = v.deleteCharAt(0); + + if ( (v.length() & 1) == 0) + v.insert(0, '0'); + + int c; + + for (int i = 0; i < v.length() - 1; i = i + 2) + { + c = ((v.charAt(i) - '0') << 4) | (v.charAt(i + 1) - '0'); + out.write(c); + } + + c = ((v.charAt(v.length() - 1) - '0') << 4) | (negative ? 0xD : 0xC); + + out.write(c); + } + + /** + * Convert the loaded byte array, representing + * CORBA <code>fixed</code>, into an instance of + * the {@link BigDecimal} + */ + private static BigDecimal createFixed(int scale, byte[] d) + { + StringBuffer s = new StringBuffer(2 * d.length); + + int last = d.length - 1; + + if ((d [ last ] & 0xF) == 0xD) + s.append('-'); + + if (last > 0) + for (int i = 0; i < last; i++) + { + s.append((char) (((d [ i ] >> 4) & 0xF) + '0')); + s.append((char) (((d [ i ]) & 0xF) + '0')); + } + + s.append((char) (((d [ last ] >> 4) & 0xF) + '0')); + + BigInteger b = new BigInteger(s.toString()); + BigDecimal dec = new BigDecimal(b, scale); + + return dec; + } +} diff --git a/libjava/classpath/gnu/CORBA/ByteArrayComparator.java b/libjava/classpath/gnu/CORBA/ByteArrayComparator.java new file mode 100644 index 00000000000..8491154638d --- /dev/null +++ b/libjava/classpath/gnu/CORBA/ByteArrayComparator.java @@ -0,0 +1,91 @@ +/* ByteArrayComparator.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * A byte array comparator for mapping with CORBA object keys. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ByteArrayComparator + implements Comparator +{ + /** + * Compare arrays first by absolute equality, then by length + * and then (byte to byte) by content. + * + * @return 0 if arrays are equal, some comparison value otherwise. + */ + public int compare(Object an_a, Object a_b) + { + if (an_a == a_b) + return 0; + + byte[] a = null; + byte[] b = null; + try + { + a = (byte[]) an_a; + b = (byte[]) a_b; + } + catch (Exception ex) + { + throw new InternalError(an_a.getClass().getName() + "," + + a_b.getClass().getName() + ); + } + + if (a.length != b.length) + return a.length - b.length; + else + { + // The array sizes must be equal. + for (int i = 0; i < b.length; i++) + { + if (a [ i ] != b [ i ]) + return a [ i ] - b [ i ]; + } + } + + return 0; + } +} diff --git a/libjava/classpath/gnu/CORBA/CDR/BigEndianInputStream.java b/libjava/classpath/gnu/CORBA/CDR/BigEndianInputStream.java new file mode 100644 index 00000000000..bc019396adb --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/BigEndianInputStream.java @@ -0,0 +1,61 @@ +/* BigEndianInputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.DataInputStream; +import java.io.InputStream; + +/** + * As java uses Big Endian by default, this class is directly derived + * form DataInputStream. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class BigEndianInputStream + extends DataInputStream + implements abstractDataInputStream +{ + /** + * Delegates to the parent constructor. + */ + public BigEndianInputStream(InputStream in) + { + super(in); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/BigEndianOutputStream.java b/libjava/classpath/gnu/CORBA/CDR/BigEndianOutputStream.java new file mode 100644 index 00000000000..e0aa7da773e --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/BigEndianOutputStream.java @@ -0,0 +1,62 @@ +/* BigEndianOutputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.DataOutputStream; +import java.io.OutputStream; + +/** + * A stream to read the data in Big Endian format. This class is + * directly derived from DataOutputStream that uses the Big + * Endian. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class BigEndianOutputStream + extends DataOutputStream + implements abstractDataOutputStream +{ + /** + * Delegate functionality to the parent constructor. + */ + public BigEndianOutputStream(OutputStream out) + { + super(out); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/LittleEndianInputStream.java b/libjava/classpath/gnu/CORBA/CDR/LittleEndianInputStream.java new file mode 100644 index 00000000000..b71a9a4f66d --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/LittleEndianInputStream.java @@ -0,0 +1,633 @@ +/* LittleEndianInputStream.java -- + Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.DataInput; +import java.io.EOFException; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; + +/** + * This class reads data in the Little Endian format. It reuses + * code from GNU Classpath DataInputStream. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + * @author Warren Levy (warrenl@cygnus.com) + * @author Aaron M. Renn (arenn@urbanophile.com) + */ +public class LittleEndianInputStream + extends FilterInputStream + implements abstractDataInputStream +{ + // Byte buffer, used to make primitive read calls more efficient. + byte[] buf = new byte[ 8 ]; + + /** + * This constructor initializes a new <code>DataInputStream</code> + * to read from the specified subordinate stream. + * + * @param in The subordinate <code>InputStream</code> to read from + */ + public LittleEndianInputStream(InputStream in) + { + super(in); + } + + /** + * This method reads bytes from the underlying stream into the specified + * byte array buffer. It will attempt to fill the buffer completely, but + * may return a short count if there is insufficient data remaining to be + * read to fill the buffer. + * + * @param b The buffer into which bytes will be read. + * + * @return The actual number of bytes read, or -1 if end of stream reached + * before reading any bytes. + * + * @exception IOException If an error occurs. + */ + public int read(byte[] b) + throws IOException + { + return in.read(b, 0, b.length); + } + + /** + * This method reads bytes from the underlying stream into the specified + * byte array buffer. It will attempt to read <code>len</code> bytes and + * will start storing them at position <code>off</code> into the buffer. + * This method can return a short count if there is insufficient data + * remaining to be read to complete the desired read length. + * + * @param b The buffer into which bytes will be read. + * @param off The offset into the buffer to start storing bytes. + * @param len The requested number of bytes to read. + * + * @return The actual number of bytes read, or -1 if end of stream reached + * before reading any bytes. + * + * @exception IOException If an error occurs. + */ + public int read(byte[] b, int off, int len) + throws IOException + { + return in.read(b, off, len); + } + + /** + * This method reads a Java boolean value from an input stream. It does + * so by reading a single byte of data. If that byte is zero, then the + * value returned is <code>false</code>. If the byte is non-zero, then + * the value returned is <code>true</code>. + * <p> + * This method can read a <code>boolean</code> written by an object + * implementing the <code>writeBoolean()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>boolean</code> value read + * + * @exception EOFException If end of file is reached before reading + * the boolean + * @exception IOException If any other error occurs + * + * @see DataOutput#writeBoolean + */ + public boolean readBoolean() + throws IOException + { + return convertToBoolean(in.read()); + } + + /** + * This method reads a Java byte value from an input stream. The value + * is in the range of -128 to 127. + * <p> + * This method can read a <code>byte</code> written by an object + * implementing the <code>writeByte()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>byte</code> value read + * + * @exception EOFException If end of file is reached before reading the byte + * @exception IOException If any other error occurs + * + * @see DataOutput#writeByte + */ + public byte readByte() + throws IOException + { + return convertToByte(in.read()); + } + + /** + * This method reads a Java <code>char</code> value from an input stream. + * It operates by reading two bytes from the stream and converting them to + * a single 16-bit Java <code>char</code>. The two bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> and <code>byte2</code> + * represent the first and second byte read from the stream + * respectively, they will be transformed to a <code>char</code> in + * the following manner: + * <p> + * <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> + * <p> + * This method can read a <code>char</code> written by an object + * implementing the <code>writeChar()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>char</code> value read + * + * @exception EOFException If end of file is reached before reading the char + * @exception IOException If any other error occurs + * + * @see DataOutput#writeChar + */ + public char readChar() + throws IOException + { + readFully(buf, 0, 2); + return convertToChar(buf); + } + + /** + * This method reads a Java double value from an input stream. It operates + * by first reading a <code>long</code> value from the stream by calling the + * <code>readLong()</code> method in this interface, then converts + * that <code>long</code> to a <code>double</code> using the + * <code>longBitsToDouble</code> method in the class + * <code>java.lang.Double</code> + * <p> + * This method can read a <code>double</code> written by an object + * implementing the <code>writeDouble()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>double</code> value read + * + * @exception EOFException If end of file is reached before reading + * the double + * @exception IOException If any other error occurs + * + * @see DataOutput#writeDouble + * @see java.lang.Double#longBitsToDouble + */ + public double readDouble() + throws IOException + { + return Double.longBitsToDouble(readLong()); + } + + /** + * This method reads a Java float value from an input stream. It + * operates by first reading an <code>int</code> value from the + * stream by calling the <code>readInt()</code> method in this + * interface, then converts that <code>int</code> to a + * <code>float</code> using the <code>intBitsToFloat</code> method + * in the class <code>java.lang.Float</code> + * <p> + * This method can read a <code>float</code> written by an object + * implementing the <code>writeFloat()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>float</code> value read + * + * @exception EOFException If end of file is reached before reading the float + * @exception IOException If any other error occurs + * + * @see DataOutput#writeFloat + * @see java.lang.Float#intBitsToFloat + */ + public float readFloat() + throws IOException + { + return Float.intBitsToFloat(readInt()); + } + + /** + * This method reads raw bytes into the passed array until the array is + * full. Note that this method blocks until the data is available and + * throws an exception if there is not enough data left in the stream to + * fill the buffer. Note also that zero length buffers are permitted. + * In this case, the method will return immediately without reading any + * bytes from the stream. + * + * @param b The buffer into which to read the data + * + * @exception EOFException If end of file is reached before filling the + * buffer + * @exception IOException If any other error occurs + */ + public void readFully(byte[] b) + throws IOException + { + readFully(b, 0, b.length); + } + + /** + * This method reads raw bytes into the passed array <code>buf</code> + * starting + * <code>offset</code> bytes into the buffer. The number of bytes read + * will be + * exactly <code>len</code>. Note that this method blocks until the data is + * available and throws an exception if there is not enough data left in + * the stream to read <code>len</code> bytes. Note also that zero length + * buffers are permitted. In this case, the method will return immediately + * without reading any bytes from the stream. + * + * @param buf The buffer into which to read the data + * @param offset The offset into the buffer to start storing data + * @param len The number of bytes to read into the buffer + * + * @exception EOFException If end of file is reached before filling the + * buffer + * @exception IOException If any other error occurs + */ + public void readFully(byte[] buf, int offset, int len) + throws IOException + { + if (len < 0) + throw new IndexOutOfBoundsException("Negative length: " + len); + + while (len > 0) + { + // in.read will block until some data is available. + int numread = in.read(buf, offset, len); + if (numread < 0) + throw new EOFException(); + len -= numread; + offset += numread; + } + } + + /** + * This method reads a Java <code>int</code> value from an input stream + * It operates by reading four bytes from the stream and converting them to + * a single Java <code>int</code>. The bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> through <code>byte4</code> represent + * the first four bytes read from the stream, they will be + * transformed to an <code>int</code> in the following manner: + * <p> + * <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + + * ((byte3 & 0xFF)<< 8) + (byte4 & 0xFF)))</code> + * <p> + * The value returned is in the range of -2147483648 to 2147483647. + * <p> + * This method can read an <code>int</code> written by an object + * implementing the <code>writeInt()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>int</code> value read + * + * @exception EOFException If end of file is reached before reading the int + * @exception IOException If any other error occurs + * + * @see DataOutput#writeInt + */ + public int readInt() + throws IOException + { + readFully(buf, 0, 4); + return convertToInt(buf); + } + + /** + * This method reads the next line of text data from an input + * stream. It operates by reading bytes and converting those bytes + * to <code>char</code> values by treating the byte read as the low + * eight bits of the <code>char</code> and using 0 as the high eight + * bits. Because of this, it does not support the full 16-bit + * Unicode character set. + * <p> + * The reading of bytes ends when either the end of file or a line + * terminator is encountered. The bytes read are then returned as a + * <code>String</code> A line terminator is a byte sequence + * consisting of either <code>\r</code>, <code>\n</code> or + * <code>\r\n</code>. These termination charaters are discarded and + * are not returned as part of the string. + * <p> + * This method can read data that was written by an object implementing the + * <code>writeLine()</code> method in <code>DataOutput</code>. + * + * @return The line read as a <code>String</code> + * + * @exception IOException If an error occurs + * + * @see DataOutput + * + * @deprecated + */ + public String readLine() + throws IOException + { + StringBuffer strb = new StringBuffer(); + + while (true) + { + int c = in.read(); + if (c == -1) // got an EOF + return strb.length() > 0 ? strb.toString() : null; + if (c == '\r') + { + int next_c = in.read(); + if (next_c != '\n' && next_c != -1) + { + if (!(in instanceof PushbackInputStream)) + in = new PushbackInputStream(in); + ((PushbackInputStream) in).unread(next_c); + } + break; + } + if (c == '\n') + break; + strb.append((char) c); + } + + return strb.length() > 0 ? strb.toString() : ""; + } + + /** + * This method reads a Java <code>long</code> value from an input stream + * It operates by reading eight bytes from the stream and converting them to + * a single Java <code>long</code>. The bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> through <code>byte8</code> represent + * the first eight bytes read from the stream, they will be + * transformed to an <code>long</code> in the following manner: + * <p> + * <code>(long)(((byte1 & 0xFF) << 56) + ((byte2 & 0xFF) << 48) + + * ((byte3 & 0xFF) << 40) + ((byte4 & 0xFF) << 32) + + * ((byte5 & 0xFF) << 24) + ((byte6 & 0xFF) << 16) + + * ((byte7 & 0xFF) << 8) + (byte8 & 0xFF))) + * </code> + * <p> + * The value returned is in the range of -9223372036854775808 to + * 9223372036854775807. + * <p> + * This method can read an <code>long</code> written by an object + * implementing the <code>writeLong()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>long</code> value read + * + * @exception EOFException If end of file is reached before reading the long + * @exception IOException If any other error occurs + * + * @see DataOutput#writeLong + */ + public long readLong() + throws IOException + { + readFully(buf, 0, 8); + return convertToLong(buf); + } + + /** + * This method reads a signed 16-bit value into a Java in from the + * stream. It operates by reading two bytes from the stream and + * converting them to a single 16-bit Java <code>short</code>. The + * two bytes are stored most significant byte first (i.e., "big + * endian") regardless of the native host byte ordering. + * <p> + * As an example, if <code>byte1</code> and <code>byte2</code> + * represent the first and second byte read from the stream + * respectively, they will be transformed to a <code>short</code>. in + * the following manner: + * <p> + * <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF))</code> + * <p> + * The value returned is in the range of -32768 to 32767. + * <p> + * This method can read a <code>short</code> written by an object + * implementing the <code>writeShort()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>short</code> value read + * + * @exception EOFException If end of file is reached before reading the value + * @exception IOException If any other error occurs + * + * @see DataOutput#writeShort + */ + public short readShort() + throws IOException + { + readFully(buf, 0, 2); + return convertToShort(buf); + } + + /** + * This method reads 8 unsigned bits into a Java <code>int</code> + * value from the stream. The value returned is in the range of 0 to + * 255. + * <p> + * This method can read an unsigned byte written by an object + * implementing the <code>writeUnsignedByte()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The unsigned bytes value read as a Java <code>int</code>. + * + * @exception EOFException If end of file is reached before reading the value + * @exception IOException If any other error occurs + * + * @see DataOutput#writeByte + */ + public int readUnsignedByte() + throws IOException + { + return convertToUnsignedByte(in.read()); + } + + /** + * This method reads 16 unsigned bits into a Java int value from the stream. + * It operates by reading two bytes from the stream and converting them to + * a single Java <code>int</code> The two bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> and <code>byte2</code> + * represent the first and second byte read from the stream + * respectively, they will be transformed to an <code>int</code> in + * the following manner: + * <p> + * <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code> + * <p> + * The value returned is in the range of 0 to 65535. + * <p> + * This method can read an unsigned short written by an object + * implementing the <code>writeUnsignedShort()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The unsigned short value read as a Java <code>int</code> + * + * @exception EOFException If end of file is reached before reading the value + * @exception IOException If any other error occurs + * + * @see DataOutput#writeShort + */ + public int readUnsignedShort() + throws IOException + { + readFully(buf, 0, 2); + return convertToUnsignedShort(buf); + } + + /** + * This method attempts to skip and discard the specified number of bytes + * in the input stream. It may actually skip fewer bytes than requested. + * This method will not skip any bytes if passed a negative number of bytes + * to skip. + * + * @param n The requested number of bytes to skip. + * + * @return The requested number of bytes to skip. + * + * @exception IOException If an error occurs. + * @specnote The JDK docs claim that this returns the number of bytes + * actually skipped. The JCL claims that this method can throw an + * EOFException. Neither of these appear to be true in the JDK 1.3's + * implementation. This tries to implement the actual JDK behaviour. + */ + public int skipBytes(int n) + throws IOException + { + if (n <= 0) + return 0; + try + { + return (int) in.skip(n); + } + catch (EOFException x) + { + // do nothing. + } + return n; + } + + protected boolean convertToBoolean(int b) + throws EOFException + { + if (b < 0) + throw new EOFException(); + + return (b != 0); + } + + protected byte convertToByte(int i) + throws EOFException + { + if (i < 0) + throw new EOFException(); + + return (byte) i; + } + + protected int convertToUnsignedByte(int i) + throws EOFException + { + if (i < 0) + throw new EOFException(); + + return (i & 0xFF); + } + + /** + * Less significant byte first. + */ + protected char convertToChar(byte[] buf) + { + return (char) ((buf [ 1 ] << 8) | (buf [ 0 ] & 0xff)); + } + + /** + * Less significant byte first. + */ + protected short convertToShort(byte[] buf) + { + return (short) ((buf [ 1 ] << 8) | (buf [ 0 ] & 0xff)); + } + + /** + * Less significant byte first. + */ + protected int convertToUnsignedShort(byte[] buf) + { + return (((buf [ 1 ] & 0xff) << 8) | (buf [ 0 ] & 0xff)); + } + + /** + * Less significant byte first. + */ + protected int convertToInt(byte[] buf) + { + return (((buf [ 3 ] & 0xff) << 24) | ((buf [ 2 ] & 0xff) << 16) | + ((buf [ 1 ] & 0xff) << 8) | (buf [ 0 ] & 0xff)); + } + + /** + * Less significant byte first. + */ + protected long convertToLong(byte[] buf) + { + return (((long) (buf [ 7 ] & 0xff) << 56) | + ((long) (buf [ 6 ] & 0xff) << 48) | + ((long) (buf [ 5 ] & 0xff) << 40) | + ((long) (buf [ 4 ] & 0xff) << 32) | + ((long) (buf [ 3 ] & 0xff) << 24) | + ((long) (buf [ 2 ] & 0xff) << 16) | + ((long) (buf [ 1 ] & 0xff) << 8) | ((long) (buf [ 0 ] & 0xff))); + } + + /** + * This should never be called. + * + * @throws InternalError, always. + */ + public String readUTF() + { + throw new InternalError(); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/LittleEndianOutputStream.java b/libjava/classpath/gnu/CORBA/CDR/LittleEndianOutputStream.java new file mode 100644 index 00000000000..a6d56cfa6d0 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/LittleEndianOutputStream.java @@ -0,0 +1,253 @@ +/* LittleEndianOutputStream.java -- + Copyright (C) 1998, 2001, 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * This stream writes data in the Little Endian format + * (less significant byte first). This is opposite to the + * usual data presentation in java platform. + * + * This class reuses code from DataOutputStream. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + * @author Aaron M. Renn (arenn@urbanophile.com) + * @author Tom Tromey (tromey@cygnus.com) + */ +public class LittleEndianOutputStream + extends FilterOutputStream + implements abstractDataOutputStream +{ + /** + * This method initializes an instance of <code>DataOutputStream</code> to + * write its data to the specified underlying <code>OutputStream</code> + * + * @param out The subordinate <code>OutputStream</code> to which this + * object will write + */ + public LittleEndianOutputStream(OutputStream out) + { + super(out); + } + + /** + * This method flushes any unwritten bytes to the underlying stream. + * + * @exception IOException If an error occurs. + */ + public void flush() + throws IOException + { + out.flush(); + } + + /** + * This method writes the specified byte (passed as an <code>int</code>) + * to the underlying output stream. + * + * @param value The <code>byte</code> to write, passed as an <code>int</code>. + * + * @exception IOException If an error occurs. + */ + public synchronized void write(int value) + throws IOException + { + out.write(value); + } + + /** + * This method writes <code>len</code> bytes from the specified byte array + * <code>buf</code> starting at position <code>offset</code> into the + * buffer to the underlying output stream. + * + * @param buf The byte array to write from. + * @param offset The index into the byte array to start writing from. + * @param len The number of bytes to write. + * + * @exception IOException If an error occurs. + */ + public synchronized void write(byte[] buf, int offset, int len) + throws IOException + { + out.write(buf, offset, len); + } + + /** + * This method writes a Java boolean value to an output stream. If + * <code>value</code> is <code>true</code>, a byte with the value of + * 1 will be written, otherwise a byte with the value of 0 will be + * written. + * + * The value written can be read using the <code>readBoolean</code> + * method in <code>DataInput</code>. + * + * @param value The <code>boolean</code> value to write to the stream + * + * @exception IOException If an error occurs + * + * @see DataInput#readBoolean + */ + public void writeBoolean(boolean value) + throws IOException + { + write(value ? 1 : 0); + } + + /** + * This method writes a Java byte value to an output stream. The + * byte to be written will be in the lowest 8 bits of the + * <code>int</code> value passed. + * + * The value written can be read using the <code>readByte</code> or + * <code>readUnsignedByte</code> methods in <code>DataInput</code>. + * + * @param value The <code>byte</code> to write to the stream, passed as + * the low eight bits of an <code>int</code>. + * + * @exception IOException If an error occurs + * + * @see DataInput#readByte + * @see DataInput#readUnsignedByte + */ + public void writeByte(int value) + throws IOException + { + write(value & 0xff); + } + + /** + * This method writes a Java short value to an output stream. + * + * @param value The <code>short</code> value to write to the stream, + * passed as an <code>int</code>. + * + * @exception IOException If an error occurs + */ + public synchronized void writeShort(int value) + throws IOException + { + write((byte) (0xff & value)); + write((byte) (0xff & (value >> 8))); + } + + /** + * Writes char in Little Endian. + */ + public synchronized void writeChar(int value) + throws IOException + { + write((byte) (0xff & value)); + write((byte) (0xff & (value >> 8))); + } + + /** + * Writes int in Little Endian. + */ + public synchronized void writeInt(int value) + throws IOException + { + write((byte) (0xff & value)); + write((byte) (0xff & (value >> 8))); + write((byte) (0xff & (value >> 16))); + write((byte) (0xff & (value >> 24))); + } + + /** + * Writes long in Little Endian. + */ + public synchronized void writeLong(long value) + throws IOException + { + write((byte) (0xff & value)); + write((byte) (0xff & (value >> 8))); + write((byte) (0xff & (value >> 16))); + write((byte) (0xff & (value >> 24))); + write((byte) (0xff & (value >> 32))); + write((byte) (0xff & (value >> 40))); + write((byte) (0xff & (value >> 48))); + write((byte) (0xff & (value >> 56))); + } + + /** + * This method writes a Java <code>float</code> value to the stream. This + * value is written by first calling the method + * <code>Float.floatToIntBits</code> + * to retrieve an <code>int</code> representing the floating point number, + * then writing this <code>int</code> value to the stream exactly the same + * as the <code>writeInt()</code> method does. + * + * @param value The <code>float</code> value to write to the stream + * + * @exception IOException If an error occurs + * + * @see writeInt + * @see DataInput#readFloat + * @see Float#floatToIntBits + */ + public void writeFloat(float value) + throws IOException + { + writeInt(Float.floatToIntBits(value)); + } + + /** + * This method writes a Java <code>double</code> value to the stream. This + * value is written by first calling the method + * <code>Double.doubleToLongBits</code> + * to retrieve an <code>long</code> representing the floating point number, + * then writing this <code>long</code> value to the stream exactly the same + * as the <code>writeLong()</code> method does. + * + * @param value The <code>double</code> value to write to the stream + * + * @exception IOException If an error occurs + * + * @see writeLong + * @see DataInput#readDouble + * @see Double#doubleToLongBits + */ + public void writeDouble(double value) + throws IOException + { + writeLong(Double.doubleToLongBits(value)); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/Vio.java b/libjava/classpath/gnu/CORBA/CDR/Vio.java new file mode 100644 index 00000000000..8f17bd2f5a9 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/Vio.java @@ -0,0 +1,638 @@ +/* gnuValueBaseHelper.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import gnu.CORBA.ObjectCreator; + +import org.omg.CORBA.CustomMarshal; +import org.omg.CORBA.DataInputStream; +import org.omg.CORBA.DataOutputStream; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.StringSeqHelper; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; +import org.omg.CORBA.portable.ValueFactory; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.Serializable; + +/** + * A specialised class for reading and writing the value types. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public abstract class Vio +{ + /** + * If true, wrap value type data into chunks. This decrease the + * performance, but is required for the interoperability with + * Sun's CORBA implementation. Chunking may increase the security, + * as there is more control on the number of bytes being transferred. + * + * The current implementation would accept both single chunk or multiple + * chunks, but will always send a single chunk. + */ + public static boolean USE_CHUNKING = true; + + /** + * The first field in the value record. The last octet may contain + * additional flags (vf_CODEBASE, vf_ID and vf_MULTIPLE_IDS). The tag + * value is different for the indirections (vt_INDIRECTION) and + * nulls (vt_NULL). + */ + public static final int vt_VALUE_TAG = 0x7fffff00; + + /** + * The value tag flag, indicating that the codebase URL is present + * in the value tag record. + */ + public static final int vf_CODEBASE = 0x1; + + /** + * The value tag flag, indicating that a single repository id is present + * in the value tag record. + */ + public static final int vf_ID = 0x2; + + /** + * The value tag flag, indicating, that there are multiple repository + * ids present in the record. If this flag is set, the flag vf_ID must + * also be set, resulting the value of the least significant byte 0x6. + */ + public static final int vf_MULTIPLE_IDS = 0x4; + + /** + * The value tag flag, indicating the presence of chunking. Each chunk is + * preceeded by a positive int, indicating the number of bytes in the chunk. + * A sequence of chunks is terminated by a non positive int. + */ + public static final int vf_CHUNKING = 0x8; + + /** + * The indirection tag value. Such tag must be followed by the CORBA long, + * indicating the offset in the CORBA message, where the indirected + * information is present. This offset is assumed zero at the position + * where the mentioned CORBA long starts and can refer both forward + * (positive values) and backward (negative values). + */ + public static final int vt_INDIRECTION = 0xffffffff; + + /** + * This tag value means that the value object being transferred is equal + * to null. + */ + public static final int vt_NULL = 0x0; + + /** + * Read the value base from the given input stream. Determines the + * required class from the repository id. This includes operations + * that are not required when an unitialised instance or at least + * class of the value type is known. Hence it may be faster to use + * the alternative methods, read(InputStream, Class) or + * read(InputStream, Serializable). + * + * @param input a stream to read from. + * + * @return the loaded value. + * + * @throws MARSHAL if the reading has failed due any reason. + */ + public static Serializable read(InputStream input) + { + // Explicitly prevent the stream from closing as we may need + // to read the subsequent bytes as well. Stream may be auto-closed + // in its finalizer. + try + { + // We may need to jump back if the value is read via value factory. + input.mark(512); + + int value_tag = input.read_long(); + checkTag(value_tag); + + String codebase = null; + String[] ids = null; + String id = null; + + // The existing implementing object. + java.lang.Object ox = null; + + // Check for the agreed null value. + if (value_tag == vt_NULL) + return null; + else if (value_tag == vt_INDIRECTION) + + // TODO FIXME Implement support for indirections. + throw new NO_IMPLEMENT("Indirections unsupported"); + else + { + // Read the value. + if ((value_tag & vf_CODEBASE) != 0) + { + // The codebase is present. The codebase is a space + // separated list of URLs from where the implementing + // code can be downloaded. + codebase = input.read_string(); + } + + if ((value_tag & vf_MULTIPLE_IDS) != 0) + { + // Multiple supported repository ids are present. + ids = StringSeqHelper.read(input); + for (int i = 0; (i < ids.length) && (ox == null); i++) + { + ox = ObjectCreator.Idl2Object(ids [ i ]); + + if (ox == null) + { + // Try to find the value factory. + ValueFactory f = + ((org.omg.CORBA_2_3.ORB) input.orb()).lookup_value_factory(ids [ i ]); + + if (f != null) + { + // Reset, as the value factory reads from beginning. + input.reset(); + return f.read_value((org.omg.CORBA_2_3.portable.InputStream) input); + } + } + } + } + else if ((value_tag & vf_ID) != 0) + { + // Single supported repository id is present. + id = input.read_string(); + ox = ObjectCreator.Idl2Object(id); + + if (ox == null) + { + // Try to find the value factory. + ValueFactory f = + ((org.omg.CORBA_2_3.ORB) input.orb()).lookup_value_factory(id); + + if (f != null) + { + input.reset(); + return f.read_value((org.omg.CORBA_2_3.portable.InputStream) input); + } + } + } + } + + if (ox == null) + throw new MARSHAL("Unable to instantiate the value type"); + else + { + read_instance(input, ox, value_tag); + return (Serializable) ox; + } + } + catch (Exception ex) + { + throw new MARSHAL(ex + ":" + ex.getMessage()); + } + } + + /** + * Read the value base from the given input stream when + * the value base class is available. Hence there is no need + * to guess it from the repository id. + * + * @param input a stream to read from. + * @param value_class the class of the value being read. + * + * @return the loaded value. + * + * @throws MARSHAL if the reading has failed due any reason. + */ + public static Serializable read(InputStream input, Class value_class) + { + // Explicitly prevent the stream from closing as we may need + // to read the subsequent bytes as well. Stream may be auto-closed + // in its finalizer. + try + { + int value_tag = input.read_long(); + checkTag(value_tag); + + // The existing implementing object. + java.lang.Object ox = value_class.newInstance(); + + // Check for the agreed null value. + if (value_tag == vt_NULL) + return null; + else if (value_tag == vt_INDIRECTION) + + // TODO FIXME Implement support for indirections. + throw new NO_IMPLEMENT("Indirections unsupported"); + else + { + // Read the value. + if ((value_tag & vf_CODEBASE) != 0) + { + // The codebase is present, but skip it. + input.read_string(); + } + + if ((value_tag & vf_MULTIPLE_IDS) != 0) + { + // Multiple supported repository ids are present, but skip them. + StringSeqHelper.read(input); + } + else if ((value_tag & vf_ID) != 0) + { + // Single supported repository id is present, but skip it. + input.read_string(); + } + } + + read_instance(input, ox, value_tag); + return (Serializable) ox; + } + catch (Exception ex) + { + throw new MARSHAL(ex + ":" + ex.getMessage()); + } + } + + /** + * Read the value base from the given input stream when + * the unitialised instance is available. Hence there is no need + * to guess the class from the repository id and then to instantiate + * an instance. + * + * @param input a stream to read from. + * @param value_instance an instance of the value. + * + * @return the loaded value. + * + * @throws MARSHAL if the reading has failed due any reason. + */ + public static Serializable read(InputStream input, Serializable value_instance) + { + // Explicitly prevent the stream from closing as we may need + // to read the subsequent bytes as well. Stream may be auto-closed + // in its finalizer. + try + { + int value_tag = input.read_long(); + checkTag(value_tag); + + // Check for the agreed null value. + if (value_tag == vt_NULL) + return null; + else if (value_tag == vt_INDIRECTION) + + // TODO FIXME Implement support for indirections. + throw new NO_IMPLEMENT("Indirections unsupported"); + else + { + // Read the value. + if ((value_tag & vf_CODEBASE) != 0) + { + // The codebase is present, but skip it. + input.read_string(); + } + + if ((value_tag & vf_MULTIPLE_IDS) != 0) + { + // Multiple supported repository ids are present, but skip them. + StringSeqHelper.read(input); + } + else if ((value_tag & vf_ID) != 0) + { + // Single supported repository id is present, but skip it. + input.read_string(); + } + } + + read_instance(input, value_instance, value_tag); + return (Serializable) value_instance; + } + catch (Exception ex) + { + throw new MARSHAL(ex + ":" + ex.getMessage()); + } + } + + /** + * Fill in the instance fields by the data from the input stream. + * The method assumes that the value header, if any, is already + * behind. The information from the stream is stored into the + * passed ox parameter. + * + * @param input an input stream to read from. + * @param value a value type object, must be either Streamable or + * CustomMarshal. + */ + public static void read_instance(InputStream input, Object value, + int value_tag + ) + { + try + { + if ((value_tag & vf_CHUNKING) != 0) + { + ByteArrayOutputStream bout = null; + int n = -1; + + // Read all chunks. + int chunk_size = input.read_long(); + if (chunk_size <= 0) + throw new MARSHAL("Invalid first chunk size " + chunk_size); + + byte[] r = new byte[ chunk_size ]; + + while (chunk_size > 0) + { + if (r.length < chunk_size) + r = new byte[ chunk_size + 256 ]; + + n = 0; + reading: + while (n < chunk_size) + n += input.read(r, n, r.length - n); + + // Read the size of the next chunk. + chunk_size = input.read_long(); + + // If the value is non negative, there is more than one chunk. + // Accumulate chunks in the buffer. + // The last chunk (or the only chunk, if only one chunk is + // present) is not written in the buffer. It is stored in the + // array r, avoiding unnecessary buffer operations. + if (chunk_size > 0) + { + bout = new ByteArrayOutputStream(2 * chunk_size); + bout.write(r, 0, chunk_size); + } + } + + if (bout != null) + { + // More than one chunk was present. + // Add the last chunk. + bout.write(r, 0, n); + input = new cdrBufInput(bout.toByteArray()); + } + else + { + // Only one chunk was present. + input = new cdrBufInput(r); + } + } + } + catch (IOException ex) + { + MARSHAL m = new MARSHAL("Unable to read chunks"); + m.initCause(ex); + throw m; + } + + // The user-defines io operations are implemented. + if (value instanceof CustomMarshal) + { + CustomMarshal marsh = (CustomMarshal) value; + try + { + marsh.unmarshal((DataInputStream) input); + } + catch (ClassCastException ex) + { + incorrect_plug_in(ex); + } + } + else + // The IDL-generated io operations are implemented. + if (value instanceof Streamable) + { + ((Streamable) value)._read(input); + } + else + + // Stating the interfaces that the USER should use. + throw new MARSHAL("The " + value.getClass().getName() + + " must implement either StreamableValue or CustomValue." + ); + + // The negative end of state marker is expected from OMG standard. + // If the chunking is used, this marker is already extracted. + if ((value_tag & vf_CHUNKING) == 0) + { + int eor = input.read_long(); + if (eor >= 0) + throw new MARSHAL("End of state marker has an invalid value " + eor); + } + } + + /** + * Write the value base into the given stream. + * + * @param output a stream to write to. + * + * @param value a value type object, must be either Streamable or + * CustomMarshal. + * + * @throws MARSHAL if the writing failed due any reason. + */ + public static void write(OutputStream output, Serializable value) + { + // Write null if this is a null value. + if (value == null) + output.write_long(vt_NULL); + else + write(output, value, ObjectCreator.toIDL(value.getClass().getName())); + } + + /** + * Write the value base into the given stream, stating that it is an + * instance of the given class. The written record has no repository + * id and requires to supply a class or initialised instance for reading + * rather than an actual class it is. + * + * This results writing a different repository id. + * + * If the passed value implements the {@link CustomMarshal}, + * the helper uses {@link CustomMarshal#marshal} + * to write the content in a user defined way. Otherwise, + * this implementation initialises the {@link ObjectOutputStream} + * and writes through it. + * + * @param output a stream to write to. + * + * @param value a value to write. + * + * @throws MARSHAL if the writing failed due any reason. + */ + public static void write(OutputStream output, Serializable value, + Class substitute + ) + { + // Write null if this is a null value. + if (value == null) + output.write_long(vt_NULL); + + else + write(output, value, ObjectCreator.toIDL(substitute.getName())); + } + + /** + * Write value when its repository Id is explicitly given. + * + * @param output an output stream to write into. + * @param value a value to write. + * @param id a value repository id. + */ + public static void write(OutputStream output, Serializable value, String id) + { + if (value == null) + output.write_long(vt_NULL); + else + write_instance(output, value, id); + } + + /** + * Write value when its repository Id is explicitly given. + * Does not handle null. + * + * @param output an output stream to write into. + * @param value a value to write. + * @param id a value repository id. + */ + private static void write_instance(OutputStream output, Serializable value, + String id + ) + { + // This implementation always writes a single repository id. + // It never writes multiple repository ids and currently does not use + // a codebase. + int value_tag = vt_VALUE_TAG | vf_ID; + + OutputStream outObj; + cdrBufOutput out = null; + + if (USE_CHUNKING) + { + out = new cdrBufOutput(); + out.setOrb(output.orb()); + outObj = out; + value_tag |= vf_CHUNKING; + } + else + outObj = output; + + output.write_long(value_tag); + output.write_string(id); + + // User defince write method is present. + if (value instanceof CustomMarshal) + { + try + { + ((CustomMarshal) value).marshal((DataOutputStream) outObj); + } + catch (ClassCastException ex) + { + incorrect_plug_in(ex); + } + } + else if (value instanceof Streamable) + { + ((Streamable) value)._write(outObj); + } + else + + // Stating the interfaces that the USER should use. + throw new MARSHAL("The " + value.getClass().getName() + + " must implement either StreamableValue or CustomValue." + ); + + if (USE_CHUNKING) + { + output.write_long(out.buffer.size()); + try + { + out.buffer.writeTo(output); + } + catch (IOException ex) + { + MARSHAL m = new MARSHAL(); + m.initCause(ex); + throw m; + } + } + + // The end of record marker, required by OMG standard. + output.write_long(-1); + } + + /** + * This can be called if the alternative CORBA implementation + * is incorrectly plugged in. + * + * @throws NO_IMPLEMENT, always. + */ + private static void incorrect_plug_in(Throwable ex) + throws NO_IMPLEMENT + { + NO_IMPLEMENT no = new NO_IMPLEMENT("Incorrect CORBA plug-in"); + no.initCause(ex); + throw no; + } + + /** + * Check the passed value tag for correctness. + * + * @param value_tag a tag to check, must be between 0x7fffff00 and 0x7fffffff + * + * @throws MARSHAL if the tag is outside this interval. + */ + private static final void checkTag(int value_tag) + { + if ((value_tag < 0x7fffff00 || value_tag > 0x7fffffff) && + value_tag != vt_NULL && value_tag != vt_INDIRECTION + ) + throw new MARSHAL("Invalid value record, unsupported header tag: " + + value_tag + ); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/abstractDataInputStream.java b/libjava/classpath/gnu/CORBA/CDR/abstractDataInputStream.java new file mode 100644 index 00000000000..be926254dd0 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/abstractDataInputStream.java @@ -0,0 +1,392 @@ +/* abstractDataInputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.IOException; + +/** + * Some data input stream that can be either Big or + * Little Endian. + * + * This class reuses code from GNU Classpath DataInputStream. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + * @author Warren Levy (warrenl@cygnus.com) + * @author Aaron M. Renn (arenn@urbanophile.com) + */ +public interface abstractDataInputStream +{ + /** + * This method reads bytes from the underlying stream into the specified + * byte array buffer. It will attempt to fill the buffer completely, but + * may return a short count if there is insufficient data remaining to be + * read to fill the buffer. + * + * @param b The buffer into which bytes will be read. + * + * @return The actual number of bytes read, or -1 if end of stream reached + * before reading any bytes. + * + * @exception IOException If an error occurs. + */ + int read(byte[] b) + throws IOException; + + /** + * This method reads bytes from the underlying stream into the specified + * byte array buffer. It will attempt to read <code>len</code> bytes and + * will start storing them at position <code>off</code> into the buffer. + * This method can return a short count if there is insufficient data + * remaining to be read to complete the desired read length. + * + * @param b The buffer into which bytes will be read. + * @param off The offset into the buffer to start storing bytes. + * @param len The requested number of bytes to read. + * + * @return The actual number of bytes read, or -1 if end of stream reached + * before reading any bytes. + * + * @exception IOException If an error occurs. + */ + int read(byte[] b, int off, int len) + throws IOException; + + /** + * This method reads a Java boolean value from an input stream. It does + * so by reading a single byte of data. If that byte is zero, then the + * value returned is <code>false</code>. If the byte is non-zero, then + * the value returned is <code>true</code>. + * <p> + * This method can read a <code>boolean</code> written by an object + * implementing the <code>writeBoolean()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>boolean</code> value read + * + * @exception EOFException If end of file is reached before reading + * the boolean + * @exception IOException If any other error occurs + * + * @see DataOutput#writeBoolean + */ + boolean readBoolean() + throws IOException; + + /** + * This method reads a Java byte value from an input stream. The value + * is in the range of -128 to 127. + * <p> + * This method can read a <code>byte</code> written by an object + * implementing the <code>writeByte()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>byte</code> value read + * + * @exception EOFException If end of file is reached before reading the byte + * @exception IOException If any other error occurs + * + * @see DataOutput#writeByte + */ + byte readByte() + throws IOException; + + /** + * This method reads a Java <code>char</code> value from an input stream. + * It operates by reading two bytes from the stream and converting them to + * a single 16-bit Java <code>char</code>. The two bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> and <code>byte2</code> + * represent the first and second byte read from the stream + * respectively, they will be transformed to a <code>char</code> in + * the following manner: + * <p> + * <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> + * <p> + * This method can read a <code>char</code> written by an object + * implementing the <code>writeChar()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>char</code> value read + * + * @exception EOFException If end of file is reached before reading the char + * @exception IOException If any other error occurs + * + * @see DataOutput#writeChar + */ + char readChar() + throws IOException; + + /** + * This method reads a Java double value from an input stream. It operates + * by first reading a <code>long</code> value from the stream by calling the + * <code>readLong()</code> method in this interface, then converts + * that <code>long</code> to a <code>double</code> using the + * <code>longBitsToDouble</code> method in the class + * <code>java.lang.Double</code> + * <p> + * This method can read a <code>double</code> written by an object + * implementing the <code>writeDouble()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>double</code> value read + * + * @exception EOFException If end of file is reached before reading + * the double + * @exception IOException If any other error occurs + * + * @see DataOutput#writeDouble + * @see java.lang.Double#longBitsToDouble + */ + double readDouble() + throws IOException; + + /** + * This method reads a Java float value from an input stream. It + * operates by first reading an <code>int</code> value from the + * stream by calling the <code>readInt()</code> method in this + * interface, then converts that <code>int</code> to a + * <code>float</code> using the <code>intBitsToFloat</code> method + * in the class <code>java.lang.Float</code> + * <p> + * This method can read a <code>float</code> written by an object + * implementing the <code>writeFloat()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>float</code> value read + * + * @exception EOFException If end of file is reached before reading the float + * @exception IOException If any other error occurs + * + * @see DataOutput#writeFloat + * @see java.lang.Float#intBitsToFloat + */ + float readFloat() + throws IOException; + + /** + * This method reads raw bytes into the passed array until the array is + * full. Note that this method blocks until the data is available and + * throws an exception if there is not enough data left in the stream to + * fill the buffer. Note also that zero length buffers are permitted. + * In this case, the method will return immediately without reading any + * bytes from the stream. + * + * @param b The buffer into which to read the data + * + * @exception EOFException If end of file is reached before filling the + * buffer + * @exception IOException If any other error occurs + */ + void readFully(byte[] b) + throws IOException; + + /** + * This method reads a Java <code>int</code> value from an input stream + * It operates by reading four bytes from the stream and converting them to + * a single Java <code>int</code>. The bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> through <code>byte4</code> represent + * the first four bytes read from the stream, they will be + * transformed to an <code>int</code> in the following manner: + * <p> + * <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + + * ((byte3 & 0xFF)<< 8) + (byte4 & 0xFF)))</code> + * <p> + * The value returned is in the range of -2147483648 to 2147483647. + * <p> + * This method can read an <code>int</code> written by an object + * implementing the <code>writeInt()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>int</code> value read + * + * @exception EOFException If end of file is reached before reading the int + * @exception IOException If any other error occurs + * + * @see DataOutput#writeInt + */ + int readInt() + throws IOException; + + /** + * This method reads a Java <code>long</code> value from an input stream + * It operates by reading eight bytes from the stream and converting them to + * a single Java <code>long</code>. The bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> through <code>byte8</code> represent + * the first eight bytes read from the stream, they will be + * transformed to an <code>long</code> in the following manner: + * <p> + * <code>(long)(((byte1 & 0xFF) << 56) + ((byte2 & 0xFF) << 48) + + * ((byte3 & 0xFF) << 40) + ((byte4 & 0xFF) << 32) + + * ((byte5 & 0xFF) << 24) + ((byte6 & 0xFF) << 16) + + * ((byte7 & 0xFF) << 8) + (byte8 & 0xFF))) + * </code> + * <p> + * The value returned is in the range of -9223372036854775808 to + * 9223372036854775807. + * <p> + * This method can read an <code>long</code> written by an object + * implementing the <code>writeLong()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>long</code> value read + * + * @exception EOFException If end of file is reached before reading the long + * @exception IOException If any other error occurs + * + * @see DataOutput#writeLong + */ + long readLong() + throws IOException; + + /** + * This method reads a signed 16-bit value into a Java in from the + * stream. It operates by reading two bytes from the stream and + * converting them to a single 16-bit Java <code>short</code>. The + * two bytes are stored most significant byte first (i.e., "big + * endian") regardless of the native host byte ordering. + * <p> + * As an example, if <code>byte1</code> and <code>byte2</code> + * represent the first and second byte read from the stream + * respectively, they will be transformed to a <code>short</code>. in + * the following manner: + * <p> + * <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF))</code> + * <p> + * The value returned is in the range of -32768 to 32767. + * <p> + * This method can read a <code>short</code> written by an object + * implementing the <code>writeShort()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The <code>short</code> value read + * + * @exception EOFException If end of file is reached before reading the value + * @exception IOException If any other error occurs + * + * @see DataOutput#writeShort + */ + short readShort() + throws IOException; + + /** + * This method reads 8 unsigned bits into a Java <code>int</code> + * value from the stream. The value returned is in the range of 0 to + * 255. + * <p> + * This method can read an unsigned byte written by an object + * implementing the <code>writeUnsignedByte()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The unsigned bytes value read as a Java <code>int</code>. + * + * @exception EOFException If end of file is reached before reading the value + * @exception IOException If any other error occurs + * + * @see DataOutput#writeByte + */ + int readUnsignedByte() + throws IOException; + + /** + * This method reads 16 unsigned bits into a Java int value from the stream. + * It operates by reading two bytes from the stream and converting them to + * a single Java <code>int</code> The two bytes are stored most + * significant byte first (i.e., "big endian") regardless of the native + * host byte ordering. + * <p> + * As an example, if <code>byte1</code> and <code>byte2</code> + * represent the first and second byte read from the stream + * respectively, they will be transformed to an <code>int</code> in + * the following manner: + * <p> + * <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code> + * <p> + * The value returned is in the range of 0 to 65535. + * <p> + * This method can read an unsigned short written by an object + * implementing the <code>writeUnsignedShort()</code> method in the + * <code>DataOutput</code> interface. + * + * @return The unsigned short value read as a Java <code>int</code> + * + * @exception EOFException If end of file is reached before reading the value + * @exception IOException If any other error occurs + * + * @see DataOutput#writeShort + */ + int readUnsignedShort() + throws IOException; + + /** + * Read a single byte. + * + * @return a byte, extracted from the stream or -1 if + * EOF has been reached. + * @throws IOException + */ + public int read() + throws IOException; + + /** + * This method attempts to skip and discard the specified number of bytes + * in the input stream. It may actually skip fewer bytes than requested. + * This method will not skip any bytes if passed a negative number of bytes + * to skip. + * + * @param n The requested number of bytes to skip. + * + * @return The requested number of bytes to skip. + * + * @exception IOException If an error occurs. + * @specnote The JDK docs claim that this returns the number of bytes + * actually skipped. The JCL claims that this method can throw an + * EOFException. Neither of these appear to be true in the JDK 1.3's + * implementation. This tries to implement the actual JDK behaviour. + */ + int skipBytes(int n) + throws IOException; +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/abstractDataOutputStream.java b/libjava/classpath/gnu/CORBA/CDR/abstractDataOutputStream.java new file mode 100644 index 00000000000..2f9b8c419fc --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/abstractDataOutputStream.java @@ -0,0 +1,185 @@ +/* abstractDataOutputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.IOException; + +/** + * An abstract data output stream that could write data in either + * Big Endian or Little Endian format. + * + * This class reuses code from GNU Classpath DataOutputStream. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + * @author Warren Levy (warrenl@cygnus.com) + * @author Aaron M. Renn (arenn@urbanophile.com) + */ +public interface abstractDataOutputStream +{ + /** + * This method flushes any unwritten bytes to the underlying stream. + * + * @exception IOException If an error occurs. + */ + void flush() + throws IOException; + + /** + * This method writes the specified byte (passed as an <code>int</code>) + * to the underlying output stream. + * + * @param value The <code>byte</code> to write, passed as an <code>int</code>. + * + * @exception IOException If an error occurs. + */ + void write(int value) + throws IOException; + + /** + * This method writes <code>len</code> bytes from the specified byte array + * <code>buf</code> starting at position <code>offset</code> into the + * buffer to the underlying output stream. + * + * @param buf The byte array to write from. + * @param offset The index into the byte array to start writing from. + * @param len The number of bytes to write. + * + * @exception IOException If an error occurs. + */ + void write(byte[] buf, int offset, int len) + throws IOException; + + /** + * Write the complete byte array. + * @throws IOException + */ + void write(byte[] buf) + throws IOException; + + /** + * This method writes a Java boolean value to an output stream. If + * <code>value</code> is <code>true</code>, a byte with the value of + * 1 will be written, otherwise a byte with the value of 0 will be + * written. + * + * The value written can be read using the <code>readBoolean</code> + * method in <code>DataInput</code>. + * + * @param value The <code>boolean</code> value to write to the stream + * + * @exception IOException If an error occurs + */ + void writeBoolean(boolean value) + throws IOException; + + /** + * This method writes a Java byte value to an output stream. The + * byte to be written will be in the lowest 8 bits of the + * <code>int</code> value passed. + * + * The value written can be read using the <code>readByte</code> or + * <code>readUnsignedByte</code> methods in <code>DataInput</code>. + * + * @param value The <code>byte</code> to write to the stream, passed as + * the low eight bits of an <code>int</code>. + * + * @exception IOException If an error occurs + */ + void writeByte(int value) + throws IOException; + + /** + * This method writes a Java short value to an output stream. The + * char to be written will be in the lowest 16 bits of the <code>int</code> + * value passed. + * + * @exception IOException If an error occurs + */ + void writeShort(int value) + throws IOException; + + /** + * This method writes a Java char value to an output stream. The + * char to be written will be in the lowest 16 bits of the <code>int</code> + * value passed. + * + * @exception IOException If an error occurs + */ + void writeChar(int value) + throws IOException; + + /** + * This method writes a Java int value to an output stream. + * + * @param value The <code>int</code> value to write to the stream + * + * @exception IOException If an error occurs + */ + void writeInt(int value) + throws IOException; + + /** + * This method writes a Java long value to an output stream. + * + * @param value The <code>long</code> value to write to the stream + * + * @exception IOException If an error occurs + */ + void writeLong(long value) + throws IOException; + + /** + * This method writes a Java <code>float</code> value to the stream. + * @param value The <code>float</code> value to write to the stream + * + * @exception IOException If an error occurs + */ + void writeFloat(float value) + throws IOException; + + /** + * This method writes a Java <code>double</code> value to the stream. + * + * @param value The <code>double</code> value to write to the stream + * + * @exception IOException If an error occurs + */ + void writeDouble(double value) + throws IOException; +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/aligningInputStream.java b/libjava/classpath/gnu/CORBA/CDR/aligningInputStream.java new file mode 100644 index 00000000000..a719b32ee1e --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/aligningInputStream.java @@ -0,0 +1,122 @@ +/* aligningInputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.ByteArrayInputStream; + +import org.omg.CORBA.BAD_PARAM; + +/** + * The input stream with the possibility to align on the + * word (arbitrary size) boundary. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class aligningInputStream + extends ByteArrayInputStream +{ + /** + * The alignment offset. + */ + private int offset = 0; + + /** + * Create a stream, reading form the given buffer. + * + * @param a_buffer a buffer to read from. + */ + public aligningInputStream(byte[] a_buffer) + { + super(a_buffer); + } + + /** + * Create a stream, reading from the given buffer region. + * + * @param a_buffer a buffer to read from. + * @param offset the offset of the region. + * @param length thr length of the region. + */ + public aligningInputStream(byte[] a_buffer, int offset, int length) + { + super(a_buffer, offset, length); + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public void setOffset(int an_offset) + { + offset = an_offset; + } + + /** + * Skip several bytes, aligning the internal pointer on the + * selected boundary. + * + * @throws BAD_PARAM, minor code 0, the alignment is not possible, + * usually due the wrong parameter value. + */ + public void align(int alignment) + { + try + { + int d = (pos + offset) % alignment; + if (d > 0) + { + skip(alignment - d); + } + } + catch (Exception ex) + { + BAD_PARAM p = new BAD_PARAM("Unable to align at " + alignment); + p.initCause(ex); + throw p; + } + } + + /** + * Get the byte buffer, from where the data are read. + */ + public byte[] getBuffer() + { + return buf; + } +} diff --git a/libjava/classpath/gnu/CORBA/CDR/aligningOutputStream.java b/libjava/classpath/gnu/CORBA/CDR/aligningOutputStream.java new file mode 100644 index 00000000000..8a682c1fd6d --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/aligningOutputStream.java @@ -0,0 +1,121 @@ +/* aligningOutputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import org.omg.CORBA.BAD_PARAM; + +/** + * The input stream with the possibility to align on the + * word (arbitrary size) boundary. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class aligningOutputStream + extends ByteArrayOutputStream +{ + /** + * The alignment offset. + */ + private int offset = 0; + + /** + * Create a stream with the default intial buffer size. + */ + public aligningOutputStream() + { + } + + /** + * Create a stream with the given intial buffer size. + */ + public aligningOutputStream(int initial_size) + { + super(initial_size); + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public void setOffset(int an_offset) + { + offset = an_offset; + } + + /** + * Skip several bytes, aligning the internal pointer on the + * selected boundary. + * + * @throws BAD_PARAM, minor code 0, the alignment is not possible, + * usually due the wrong parameter value. + */ + public void align(int alignment) + { + try + { + int d = (count + offset) % alignment; + if (d > 0) + { + skip(alignment - d); + } + } + catch (Exception ex) + { + BAD_PARAM p = new BAD_PARAM("Unable to align at " + alignment); + p.initCause(ex); + throw p; + } + } + + /** + * Write the specified number of zero bytes. + * + * @param bytes the number of zero bytes to write. + */ + public void skip(int bytes) + { + for (int i = 0; i < bytes; i++) + { + write(0); + } + } +} diff --git a/libjava/classpath/gnu/CORBA/CDR/cdrBufInput.java b/libjava/classpath/gnu/CORBA/CDR/cdrBufInput.java new file mode 100644 index 00000000000..3cab7216c3b --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/cdrBufInput.java @@ -0,0 +1,115 @@ +/* cdrBufInput.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + + +/** + * The CDR input stream that reads data from the byte buffer. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + * + * TODO character encoding. Now the encoding can be set, but it is ignored. + * If you take this task, scan 'TODO character encoding' for + * relevant places. + */ +public class cdrBufInput + extends cdrInput +{ + /** + * The byte array input stream to read data from. + */ + public final aligningInputStream buffer; + + /** + * Creates the CDR input stream that reads from the given buffer + * array. + * + * @param a_buffer an array to read from. + */ + public cdrBufInput(byte[] a_buffer) + { + buffer = new aligningInputStream(a_buffer); + setInputStream(buffer); + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public void setOffset(int offset) + { + buffer.setOffset(offset); + } + + /** + * Skip several bytes, aligning the internal pointer on the + * selected boundary. + */ + public void align(int alignment) + { + buffer.align(alignment); + } + + /** + * Mark the current position. + * @param ahead + */ + public synchronized void mark(int ahead) + { + buffer.mark(ahead); + } + + /** + * Checks if marking is supported. + * @return + */ + public boolean markSupported() + { + return buffer.markSupported(); + } + + /** + * Resets the stream to the previously marked position. + */ + public void reset() + { + buffer.reset(); + setInputStream(buffer); + } +} diff --git a/libjava/classpath/gnu/CORBA/CDR/cdrBufOutput.java b/libjava/classpath/gnu/CORBA/CDR/cdrBufOutput.java new file mode 100644 index 00000000000..47f5f176b39 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/cdrBufOutput.java @@ -0,0 +1,115 @@ +/* cdrBufOutput.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.ByteArrayOutputStream; + +/** + * A CORBA output stream, writing data into the internal + * buffer ({@link ByteArrayOutputStream}). + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class cdrBufOutput + extends cdrOutput +{ + /** + * The byte buffer. + */ + public final aligningOutputStream buffer; + + /** + * Creates the instance with the given initial buffer size. + * @param bufSize the buffer size. + */ + public cdrBufOutput(int bufSize) + { + buffer = new aligningOutputStream(bufSize); + setOutputStream(buffer); + } + + /** + * Creates the instance with the default buffer size. + */ + public cdrBufOutput() + { + buffer = new aligningOutputStream(); + setOutputStream(buffer); + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public void setOffset(int an_offset) + { + buffer.setOffset(an_offset); + } + + /** + * Align the curretn position at the given natural boundary. + */ + public void align(int boundary) + { + buffer.align(boundary); + } + + /** + * Return the input stream that reads the previously written values. + */ + public org.omg.CORBA.portable.InputStream create_input_stream() + { + cdrBufInput in = new cdrBufInput(buffer.toByteArray()); + in.setOrb(orb); + + in.setVersion(giop); + in.setCodeSet(getCodeSet()); + + return in; + } + + /** + * Resets (clears) the buffer. + */ + public void reset() + { + buffer.reset(); + setOutputStream(buffer); + } +} diff --git a/libjava/classpath/gnu/CORBA/CDR/cdrInput.java b/libjava/classpath/gnu/CORBA/CDR/cdrInput.java new file mode 100644 index 00000000000..859f93ae5f0 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/cdrInput.java @@ -0,0 +1,1671 @@ +/* cdrInput.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import gnu.CORBA.BigDecimalHelper; +import gnu.CORBA.Functional_ORB; +import gnu.CORBA.GIOP.CharSets_OSF; +import gnu.CORBA.GIOP.cxCodeSet; +import gnu.CORBA.IOR; +import gnu.CORBA.IOR_Delegate; +import gnu.CORBA.TypeCodeHelper; +import gnu.CORBA.Unexpected; +import gnu.CORBA.Version; +import gnu.CORBA.gnuAny; +import gnu.CORBA.stubFinder; + +import org.omg.CORBA.Any; +import org.omg.CORBA.AnySeqHolder; +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.BooleanSeqHolder; +import org.omg.CORBA.CharSeqHolder; +import org.omg.CORBA.DoubleSeqHolder; +import org.omg.CORBA.FloatSeqHolder; +import org.omg.CORBA.LongLongSeqHolder; +import org.omg.CORBA.LongSeqHolder; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.ORB; +import org.omg.CORBA.OctetSeqHolder; +import org.omg.CORBA.ShortSeqHolder; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.TypeCodePackage.Bounds; +import org.omg.CORBA.ULongLongSeqHolder; +import org.omg.CORBA.ULongSeqHolder; +import org.omg.CORBA.UShortSeqHolder; +import org.omg.CORBA.WCharSeqHolder; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.ObjectImpl; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Serializable; + +import java.math.BigDecimal; + +/** + * A simple CORBA CDR (common data representation) + * input stream, reading data from the + * given {@link java.io.InputStream}. The primitive types + * are aligned on they natural boundaries by implementing the + * abstract method {@link #align(int boundary)}. + * + * The same class also implements {@link org.omg.CORBA.DataInputStream} to + * read the object content in a user defined way. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public abstract class cdrInput + extends org.omg.CORBA_2_3.portable.InputStream + implements org.omg.CORBA.DataInputStream +{ + /** + * The message, explaining that the exception has been thrown due + * unexpected end of the input stream. This usually happens the + * server and client disagree on communication or data representation + * rules. + */ + protected static final String UNEXP_EOF = "Unexpected end of stream"; + + /** + * This instance is used to convert primitive data types into the + * byte sequences. + */ + protected abstractDataInputStream b; + + /** + * The input stream, from where the data are actually + * being read. + */ + protected java.io.InputStream actual_stream; + + /** + * The associated orb, if any. + */ + protected ORB orb; + + /** + * The GIOP version. + */ + protected Version giop = new Version(1, 2); + + /** + * The code set information. + */ + protected cxCodeSet codeset = cxCodeSet.STANDARD; + + /** + * The name of the currently used narrow charset, null if + * the native narrow charset is used. + */ + private String narrow_charset = null; + + /** + * The name of the currently used wide charset, null if + * the native wide charset is used. + */ + private String wide_charset = null; + + /** + * True if the native code set is used for narrow characters. + * If the set is native, no the intermediate Reader object + * is instantiated when writing characters. + */ + private boolean narrow_native; + + /** + * True if the native code set is used for wide characters. + * If the set is native, no the intermediate Reader object + * is instantiated when writing characters. + */ + private boolean wide_native; + + /** + * If true, the stream expect + * the multi-byte data in the form "less significant byte + * first" (Little Endian). This is the opposite to the + * java standard (Big Endian). + */ + private boolean little_endian; + + /** + * Creates the stream. The stream reads Big Endian by + * default. + * + * @param readFrom a stream to read CORBA input from. + */ + public cdrInput(java.io.InputStream readFrom) + { + setInputStream(readFrom); + setCodeSet(cxCodeSet.STANDARD); + } + + /** + * Creates the stream, requiring the subsequent call + * of {@link #setInputStream(java.io.InputStream)}. + */ + public cdrInput() + { + setCodeSet(cxCodeSet.STANDARD); + } + + /** + * Set the Big Endian or Little Endian encoding. + * The stream reads Big Endian by default. + * + * @param use_little_endian if true, the stream expect + * the multi-byte data in the form "less significant byte + * first" (Little Endian). This is the opposite to the + * java standard (Big Endian). + */ + public void setBigEndian(boolean use_big_endian) + { + little_endian = !use_big_endian; + setInputStream(actual_stream); + } + + /** + * Set the input stream that receives the CORBA input. + * + * @param readFrom the stream. + */ + public void setInputStream(java.io.InputStream readFrom) + { + if (little_endian) + b = new LittleEndianInputStream(readFrom); + else + b = new BigEndianInputStream(readFrom); + + actual_stream = readFrom; + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public abstract void setOffset(int offset); + + /** + * Set the orb, associated with this stream. + * @param an_orb + */ + public void setOrb(ORB an_orb) + { + orb = an_orb; + } + + /** + * Set the GIOP version. Some data types are written differently + * for the different versions. The default version is 1.0 . + */ + public void setVersion(Version giop_version) + { + giop = giop_version; + } + + /** + * Align the curretn position at the given natural boundary. + */ + public abstract void align(int boundary); + + /** + * Reads the CORBA unsigned long (java int), returning the + * value in the sufficiently large java long. + */ + public long gnu_read_ulong() + { + try + { + long l = b.readInt(); + l &= 0xFFFFFFF; + return l; + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the unsigned short integer value and return it as java + * int, sufficiently large to hold all values. + */ + public int gnu_read_ushort() + { + try + { + align(2); + return b.readUnsignedShort(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Return the associated {@link ORB}. + * @return the associated {@link ORB} or null is no such is set. + */ + public ORB orb() + { + return orb; + } + + /** + * Read a single byte directly from the buffer. + */ + public int read() + throws java.io.IOException + { + try + { + return b.read(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + } + + /** + * Read bytes directly from the buffer. + */ + public int read(byte[] x, int ofs, int len) + throws java.io.IOException + { + try + { + return b.read(x, ofs, len); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + } + + /** + * Read bytes directly from the buffer. + */ + public int read(byte[] x) + throws java.io.IOException + { + try + { + return b.read(x); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + } + + /** + * Read the CORBA object. The object to read is represented in the + * form of the plain (not a string-encoded) IOR profile without the + * heading endian indicator. The responsible method for reading such + * data is {@link IOR.read_no_endian}. + * + * The returned object is usually casted into the given type using + * the .narrow method of its helper, despite in some cases the direct + * cast would also work. + * + * The null objects are recognised from the empty profile set. + * For such objects, null is returned. + * + * @return the loaded and constructed object. + */ + public org.omg.CORBA.Object read_Object() + { + try + { + IOR ior = new IOR(); + ior._read_no_endian(this); + + if (ior.Id == null) + return null; + + // Check maybe this is a remote reference to the local object. + // This is only possible if we access the repository of the + // connected object. + if (orb instanceof Functional_ORB) + { + Functional_ORB forb = (Functional_ORB) orb; + org.omg.CORBA.Object local = forb.find_local_object(ior); + if (local != null) + return local; + } + + // Search for the available stubs. + ObjectImpl impl = stubFinder.search(orb, ior); + try + { + if (impl._get_delegate() == null) + impl._set_delegate(new IOR_Delegate(orb, ior)); + } + catch (BAD_OPERATION ex) + { + // Some colaborants may throw this exception + // in response to the attempt to get the unset delegate. + impl._set_delegate(new IOR_Delegate(orb, ior)); + } + + return impl; + } + catch (IOException ex) + { + BAD_OPERATION bad = new BAD_OPERATION(); + bad.initCause(ex); + throw bad; + } + } + + /** + * Read the type code. The type code format is defined in the + * CORBA documenation. + */ + public TypeCode read_TypeCode() + { + try + { + return TypeCodeHelper.read(this); + } + + catch (Bounds ex) + { + throw new Unexpected(); + } + catch (BadKind ex) + { + throw new Unexpected(); + } + } + + /** + * Read the CORBA {@link Any}. This method first reads the + * type code, then delegates the functionality + * to {@link Any#read_value}. + */ + public Any read_any() + { + TypeCode ty = read_TypeCode(); + gnuAny any = new gnuAny(); + any.read_value(this, ty); + return any; + } + + /** + * Read the boolean, treating any non zero byte as true, + * zero byte as false. + */ + public boolean read_boolean() + { + try + { + return b.read() == 0 ? false : true; + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the array of boolean. + */ + public void read_boolean_array(boolean[] x, int offs, int len) + { + try + { + for (int i = offs; i < offs + len; i++) + { + x [ i ] = b.read() == 0 ? false : true; + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read a character using narrow charset encoding. Depending form + * which encoding is set, this still can be Unicode or ever wider. + */ + public char read_char() + { + try + { + if (narrow_native) + return (char) b.read(); + else + return (char) new InputStreamReader((InputStream) b, narrow_charset).read(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read a character array, using narrow charset encoding. + */ + public void read_char_array(char[] x, int offset, int length) + { + try + { + if (narrow_native) + { + for (int i = offset; i < offset + length; i++) + x [ i ] = (char) b.read(); + } + else + { + InputStreamReader reader = + new InputStreamReader((InputStream) b, narrow_charset); + reader.read(x, offset, length); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the double value, IEEE 754 format. + */ + public double read_double() + { + try + { + align(8); + return b.readDouble(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(); + } + } + + /** + * Read the array of double values, IEEE 754 format. + */ + public void read_double_array(double[] x, int offs, int len) + { + try + { + align(8); + for (int i = offs; i < offs + len; i++) + { + x [ i ] = b.readDouble(); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the encapsulated stream. + * If the encapsulated sequence appears to be in the + * Little endian format, the flag of the returned stream + * is set to read Little endian. + */ + public cdrBufInput read_encapsulation() + { + try + { + int l = read_long(); + + byte[] r = new byte[ l ]; + int n = 0; + reading: + while (n < r.length) + { + n += read(r, n, r.length - n); + } + + cdrBufInput capsule = new cdrBufInput(r); + capsule.setOrb(orb); + + int endian = capsule.read_octet(); + + if (endian != 0) + { + capsule.setBigEndian(false); + } + + return capsule; + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the CORBA fixed (the end of the <code>fixed</code> + * can be determined by its last byte). The scale is always + * assumed to be zero. + */ + public BigDecimal read_fixed() + { + try + { + return BigDecimalHelper.read(this, 0); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the float value, IEEE 754 format. + */ + public float read_float() + { + try + { + align(4); + return b.readFloat(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read an array of float values, IEEE 754 format. + */ + public void read_float_array(float[] x, int offs, int len) + { + try + { + align(4); + for (int i = offs; i < offs + len; i++) + { + x [ i ] = b.readFloat(); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the CORBA long (java int), high byte first. + */ + public int read_long() + { + try + { + align(4); + return b.readInt(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read an array of CORBA longs (java ints). + */ + public void read_long_array(int[] x, int offs, int len) + { + try + { + align(4); + for (int i = offs; i < offs + len; i++) + { + x [ i ] = b.readInt(); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the CORBA long long (java long). + */ + public long read_longlong() + { + try + { + align(8); + return b.readLong(); + } + catch (EOFException ex) + { + throw new MARSHAL(UNEXP_EOF); + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read an array of CORBA long longs (java longs). + */ + public void read_longlong_array(long[] x, int offs, int len) + { + try + { + align(8); + for (int i = offs; i < offs + len; i++) + { + x [ i ] = b.readLong(); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read a single byte. + */ + public byte read_octet() + { + try + { + return b.readByte(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the byte array. + */ + public void read_octet_array(byte[] x, int offs, int len) + { + try + { + b.read(x, offs, len); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the length of the byte array as CORBA long and then + * the array itseld. + */ + public byte[] read_sequence() + { + try + { + int l = read_long(); + byte[] b = new byte[ l ]; + if (l > 0) + { + read(b); + } + return b; + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the CORBA short integer. + */ + public short read_short() + { + try + { + align(2); + return b.readShort(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read the array of CORBA short integer values. + */ + public void read_short_array(short[] x, int offs, int len) + { + try + { + align(2); + for (int i = offs; i < offs + len; i++) + { + x [ i ] = b.readShort(); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Read a singe byte string. The method firs reads the + * byte array and then calls a constructor to create a + * string from this array. The character encoding, if + * previously set, is taken into consideration. + * + * @return a loaded string. + */ + public String read_string() + { + try + { + align(4); + + int n = b.readInt(); + byte[] s = new byte[ n ]; + b.read(s); + + // Discard the null terminator. + if (narrow_charset == null) + return new String(s, 0, n - 1); + else + return new String(s, 0, n - 1, narrow_charset); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(); + } + } + + /** + * Reads the CORBA unsigned long (java int), delegating + * functionality to {@link #read_long}. + */ + public int read_ulong() + { + return read_long(); + } + + /** + * Reads the array of CORBA unsigned long (java integer) values, + * delegating functionality to + * {@link #real_long_array}. + */ + public void read_ulong_array(int[] x, int offs, int len) + { + read_long_array(x, offs, len); + } + + /** + * Read the CORBA unsigned long long value, + * delegating functionality to {@link #read_longlong}. + * There is no way to return values over the limit of + * the java signed long in other way than returning + * the negative value. + */ + public long read_ulonglong() + { + return read_longlong(); + } + + /** + * Reads the array of CORBA long long (java long) values, + * delegating functionality to + * {@link #real_longlong_array}. + */ + public void read_ulonglong_array(long[] x, int offs, int len) + { + read_longlong_array(x, offs, len); + } + + /** + * Read the unsigned short integer value. Due strange specification, + * the returned value must be the short type as well, so the + * the best solution seems just to delegete functionality to + * read_short. + */ + public short read_ushort() + { + return read_short(); + } + + /** + * Read an array of unsigned short values, delegating the + * functionality to {@link read_short_array}. + */ + public void read_ushort_array(short[] x, int offs, int len) + { + read_short_array(x, offs, len); + } + + /** + * Reads the wide character using the encoding, specified in the + * wide_charset. + */ + public char read_wchar() + { + try + { + if (giop.until_inclusive(1, 1)) + align(2); + + if (wide_native) + return (char) b.readShort(); + else + return (char) new InputStreamReader((InputStream) b, wide_charset).read(); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + catch (IOException ex) + { + throw new Unexpected(); + } + } + + /** + * Read an array of "wide chars", each representing a two byte + * Unicode character, high byte first. + */ + public void read_wchar_array(char[] x, int offset, int length) + { + try + { + if (giop.until_inclusive(1, 1)) + align(2); + + if (wide_native) + { + for (int i = offset; i < offset + length; i++) + x [ i ] = (char) b.readShort(); + } + else + { + InputStreamReader reader = + new InputStreamReader((InputStream) b, wide_charset); + reader.read(x, offset, length); + } + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Reads the string in wide character format + * (ussually UTF-16, Unicode). Takes the currently set charset + * into consideration. + * + * If the native (UTF-16) encoding is used + * of the GIOP protocol is before 1.2, delegates functionality + * to "plain" {@link #read_wstring_UTF_16}. + */ + public String read_wstring() + { + // Native encoding or word oriented data. + if (wide_native || giop.until_inclusive(1, 1)) + return read_wstring_UTF_16(); + try + { + align(4); + + int n = b.readInt(); + byte[] s = new byte[ n ]; + b.read(s); + + return new String(s, 0, n, wide_charset); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Reads first length of the string and the all characters as an + * Unicode (UTF-16) characters. Mind that GIOP 1.1 has the extra + * null character at the end that must be discarded. + */ + public String read_wstring_UTF_16() + { + try + { + int p = 0; + int n = read_long(); + + // The null terminator that is no longer present since 1.2 . + int nt = giop.since_inclusive(1, 2) ? 0 : 1; + + // Convert bytes to shorts. + n = n / 2; + + char[] s = new char[ n ]; + + for (int i = 0; i < s.length; i++) + s [ i ] = (char) b.readShort(); + + // Check for the byte order marker here. + if (s [ 0 ] == 0xFEFF) + { + // Big endian encoding - do nothing, but move the pointer + // one position forward. + p = 1; + } + else if (s [ 0 ] == 0xFFFE) + { + // Little endian encoding, swap the bytes and move one + // position forward. + p = 1; + + for (int i = p; i < s.length; i++) + s [ i ] = swap(s [ i ]); + } + + // Discard the null terminator and, if needed, the endian marker. + return new String(s, p, n - nt - p); + } + catch (EOFException ex) + { + MARSHAL t = new MARSHAL(UNEXP_EOF); + t.initCause(ex); + throw t; + } + + catch (IOException ex) + { + throw new Unexpected(ex); + } + } + + /** + * Swap bytes in the character. + */ + public static char swap(char x) + { + int hi; + int lo; + + lo = x & 0xFF; + hi = (x >> 8) & 0xFF; + + return (char) ((lo << 8) | hi); + } + + /** + * Set the current code set context. + */ + public void setCodeSet(cxCodeSet a_codeset) + { + this.codeset = a_codeset; + narrow_charset = CharSets_OSF.getName(codeset.char_data); + wide_charset = CharSets_OSF.getName(codeset.wide_char_data); + + narrow_native = CharSets_OSF.NATIVE_CHARACTER == codeset.char_data; + wide_native = CharSets_OSF.NATIVE_WIDE_CHARACTER == codeset.wide_char_data; + } + + /** + * Get the current code set context. + */ + public cxCodeSet getCodeSet() + { + return codeset; + } + + /** + * Read the object that is an instance of the given class. The current + * implementation delegates functionality to the parameterless + * {@link readObject()}. + * + * @param klass a class of that this object the instance is. + * + * @return the returned object. + */ + public org.omg.CORBA.Object read_Object(Class klass) + { + return read_Object(); + } + + /** + * Read a value type structure from the stream. + * + * OMG specification states the writing format is outside the scope + * of GIOP definition. This implementation uses java serialization + * mechanism, calling {@link ObjectInputStream#readObject} + * + * @return an value type structure, unmarshaled from the stream + */ + public Serializable read_Value() + { + return read_value(); + } + + /** + * Read the abstract interface. An abstract interface can be either + * CORBA value type or CORBA object and is returned as an abstract + * java.lang.Object. + * + * As specified in OMG specification, this reads a single + * boolean and then delegates either to {@link #read_Object()} (for false) + * or to {@link #read_Value()} (for true). + * + * @return an abstract interface, unmarshaled from the stream + */ + public java.lang.Object read_Abstract() + { + return read_abstract_interface(); + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_char_array(CharSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_char_array(holder.value, offset, length); + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_wchar_array(WCharSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_wchar_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the char array to fit the newly + * read values. + * + * @param holder_value the existing char array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private char[] ensureArray(char[] holder_value, int offset, int length) + { + if (holder_value == null) + return new char[ offset + length ]; + else if (holder_value.length < offset + length) + { + char[] value = new char[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_ulong_array(ULongSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_ulong_array(holder.value, offset, length); + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_long_array(LongSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_ulong_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the int array to fit the newly + * read values. + * + * @param holder_value the existing int array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private int[] ensureArray(int[] holder_value, int offset, int length) + { + if (holder_value == null) + return new int[ offset + length ]; + else if (holder_value.length < offset + length) + { + int[] value = new int[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_float_array(FloatSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_float_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the float array to fit the newly + * read values. + * + * @param holder_value the existing float array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private float[] ensureArray(float[] holder_value, int offset, int length) + { + if (holder_value == null) + return new float[ offset + length ]; + else if (holder_value.length < offset + length) + { + float[] value = new float[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_double_array(DoubleSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_double_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the double array to fit the newly + * read values. + * + * @param holder_value the existing double array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private double[] ensureArray(double[] holder_value, int offset, int length) + { + if (holder_value == null) + return new double[ offset + length ]; + else if (holder_value.length < offset + length) + { + double[] value = new double[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_short_array(ShortSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_short_array(holder.value, offset, length); + } + + /** {@inheritDoc} */ + public void read_ushort_array(UShortSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_ushort_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the short array to fit the newly + * read values. + * + * @param holder_value the existing short array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private short[] ensureArray(short[] holder_value, int offset, int length) + { + if (holder_value == null) + return new short[ offset + length ]; + else if (holder_value.length < offset + length) + { + short[] value = new short[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_octet_array(OctetSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_octet_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the byte array to fit the newly + * read values. + * + * @param holder_value the existing byte array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private byte[] ensureArray(byte[] holder_value, int offset, int length) + { + if (holder_value == null) + return new byte[ offset + length ]; + else if (holder_value.length < offset + length) + { + byte[] value = new byte[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_longlong_array(LongLongSeqHolder holder, int offset, + int length + ) + { + holder.value = ensureArray(holder.value, offset, length); + read_longlong_array(holder.value, offset, length); + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_ulonglong_array(ULongLongSeqHolder holder, int offset, + int length + ) + { + holder.value = ensureArray(holder.value, offset, length); + read_ulonglong_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the array of longs to fit the newly + * read values. + * + * @param holder_value the existing array, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private long[] ensureArray(long[] holder_value, int offset, int length) + { + if (holder_value == null) + return new long[ offset + length ]; + else if (holder_value.length < offset + length) + { + long[] value = new long[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_boolean_array(BooleanSeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + read_boolean_array(holder.value, offset, length); + } + + /** + * If required, allocate or resize the array of booleans to fit the newly + * read values. + * + * @param holder_value the existing array of booleans, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private boolean[] ensureArray(boolean[] holder_value, int offset, int length) + { + if (holder_value == null) + return new boolean[ offset + length ]; + else if (holder_value.length < offset + length) + { + boolean[] value = new boolean[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * Read an array. In OMG specification is written that if the data does + * not fit into the holder value field, that array must be resized. + * The implementation follows this rule. If the holder value field + * contains null, it is newly instantiated. + */ + public void read_any_array(AnySeqHolder holder, int offset, int length) + { + holder.value = ensureArray(holder.value, offset, length); + for (int i = offset; i < offset + length; i++) + { + holder.value [ i ] = read_any(); + } + } + + /** + * If required, allocate or resize the array of Anys to fit the newly + * read values. + * + * @param holder_value the existing array of Anys, may be null. + * @param offset the required offset to read. + * @param length the length of the new sequence. + * + * @return the allocated or resized array, same array if no such operations + * are required. + */ + private Any[] ensureArray(Any[] holder_value, int offset, int length) + { + if (holder_value == null) + return new Any[ offset + length ]; + else if (holder_value.length < offset + length) + { + Any[] value = new Any[ offset + length ]; + System.arraycopy(holder_value, 0, value, 0, holder_value.length); + return value; + } + else + return holder_value; + } + + /** + * This method is required to represent the DataInputStream as a value + * type object. + * + * @return a single entity "IDL:omg.org/CORBA/DataInputStream:1.0", + * always. + */ + public String[] _truncatable_ids() + { + return new String[] { "IDL:omg.org/CORBA/DataInputStream:1.0" }; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/cdrOutput.java b/libjava/classpath/gnu/CORBA/CDR/cdrOutput.java new file mode 100644 index 00000000000..86ca3b1cb3d --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/cdrOutput.java @@ -0,0 +1,999 @@ +/* cdrOutput.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import gnu.CORBA.BigDecimalHelper; +import gnu.CORBA.GIOP.CharSets_OSF; +import gnu.CORBA.GIOP.cxCodeSet; +import gnu.CORBA.IOR; +import gnu.CORBA.Simple_delegate; +import gnu.CORBA.TypeCodeHelper; +import gnu.CORBA.Unexpected; +import gnu.CORBA.Version; +import gnu.CORBA.primitiveTypeCode; + +import org.omg.CORBA.Any; +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.Context; +import org.omg.CORBA.ContextList; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.ORB; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.UserException; +import org.omg.CORBA.portable.Delegate; +import org.omg.CORBA.portable.ObjectImpl; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Serializable; + +import java.math.BigDecimal; + +/** + * A simple CORBA CDR (common data representation) + * output stream, writing data into the + * given {@link java.io.OutputStream}. + * + * The same class also implements the {@link DataInputStream}, + * providing support for writing the value type objects + * in a user defined way. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public abstract class cdrOutput + extends org.omg.CORBA_2_3.portable.OutputStream + implements org.omg.CORBA.DataOutputStream +{ + /** + * This instance is used to convert primitive data types into the + * byte sequences. + */ + protected abstractDataOutputStream b; + + /** + * The associated orb, if any. + */ + protected ORB orb; + + /** + * The GIOP version. + */ + protected Version giop = new Version(1, 0); + + /** + * The code set information. + */ + protected cxCodeSet codeset; + + /** + * The name of the currently used narrow charset. + */ + private String narrow_charset; + + /** + * The name of the currently used wide charset, null if + * the native wide charset is used. + */ + private String wide_charset; + + /** + * True if the native code set is used for narrow characters. + * If the set is native, no the intermediate Reader object + * is instantiated when writing characters. + */ + private boolean narrow_native; + + /** + * True if the native code set is used for wide characters. + * If the set is native, no the intermediate Reader object + * is instantiated when writing characters. + */ + private boolean wide_native; + + /** + * If true, the Little Endian encoding is used to write the + * data. Otherwise, the Big Endian encoding is used. + */ + private boolean little_endian; + + /** + * The stream whre the data are actually written. + */ + private java.io.OutputStream actual_stream; + + /** + * Creates the stream. + * + * @param writeTo a stream to write CORBA output to. + */ + public cdrOutput(java.io.OutputStream writeTo) + { + setOutputStream(writeTo); + setCodeSet(cxCodeSet.STANDARD); + } + + /** + * Creates the stream, requiring the subsequent call + * of {@link #setOutputStream(java.io.OutputStream)}. + */ + public cdrOutput() + { + setCodeSet(cxCodeSet.STANDARD); + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public abstract void setOffset(int an_offset); + + /** + * Set the current code set context. + */ + public void setCodeSet(cxCodeSet a_codeset) + { + this.codeset = a_codeset; + narrow_charset = CharSets_OSF.getName(codeset.char_data); + wide_charset = CharSets_OSF.getName(codeset.wide_char_data); + + narrow_native = CharSets_OSF.NATIVE_CHARACTER == codeset.char_data; + wide_native = CharSets_OSF.NATIVE_WIDE_CHARACTER == codeset.wide_char_data; + } + + /** + * Get the current code set context. + */ + public cxCodeSet getCodeSet() + { + return codeset; + } + + /** + * Set the orb, associated with this stream. + * @param an_orb + */ + public void setOrb(ORB an_orb) + { + orb = an_orb; + } + + /** + * Set the output stream that receives the CORBA output. + * + * @param writeTo the stream. + */ + public void setOutputStream(java.io.OutputStream writeTo) + { + if (little_endian) + b = new LittleEndianOutputStream(writeTo); + else + b = new BigEndianOutputStream(writeTo); + + actual_stream = writeTo; + } + + /** + * Set the GIOP version. Some data types are written differently + * for the different versions. The default version is 1.0 . + */ + public void setVersion(Version giop_version) + { + giop = giop_version; + } + + /** + * Specify if the stream should use the Big Endian (usual for java) + * or Little Encoding. The default is Big Endian. + * + * @param use_big_endian if true, use Big Endian, if false, + * use Little Endian. + */ + public void setBigEndian(boolean use_big_endian) + { + little_endian = !use_big_endian; + setOutputStream(actual_stream); + } + + /** + * Align the curretn position at the given natural boundary. + */ + public abstract void align(int boundary); + + /** + * Create the encapsulation stream, associated with the current + * stream. The encapsulated stream must be closed. When being + * closed, the encapsulation stream writes its buffer into + * this stream using the CORBA CDR encapsulation rules. + * + * It is not allowed to write to the current stream directly + * before the encapsulation stream is closed. + * + * The encoding (Big/Little Endian) inside the encapsulated + * sequence is the same as used into the parent stream. + * + * @return the encapsulated stream. + */ + public cdrOutput createEncapsulation() + { + return new encapsulatedOutput(this, !little_endian); + } + + /** + * Return the associated {@link ORB}. + * @return the associated {@link ORB} or null is no such is set. + */ + public ORB orb() + { + return orb; + } + + /** + * Write a single byte. + * @param a byte to write (low 8 bits are written). + */ + public void write(int n) + { + try + { + b.write(n); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Write bytes directly into the underlying stream. + */ + public void write(byte[] x) + throws java.io.IOException + { + b.write(x); + } + + /** + * Write bytes directly into the underlying stream. + */ + public void write(byte[] x, int ofs, int len) + throws java.io.IOException + { + b.write(x, ofs, len); + } + + /** + * Following the specification, this is not implemented. + * Override to get the functionality. + */ + public void write_Context(Context context, ContextList contexts) + { + throw new NO_IMPLEMENT(); + } + + /** + * Read the CORBA object. The object is written + * form of the plain (not a string-encoded) IOR profile without the + * heading endian indicator. The responsible method for reading such + * data is {@link IOR.write_no_endian}. + * + * The null value is written as defined in OMG specification + * (zero length string, followed by an empty set of profiles). + */ + public void write_Object(org.omg.CORBA.Object x) + { + if (x == null) + { + IOR.write_null(this); + return; + } + else if (x instanceof ObjectImpl) + { + Delegate d = ((ObjectImpl) x)._get_delegate(); + + if (d instanceof Simple_delegate) + { + Simple_delegate ido = (Simple_delegate) d; + ido.getIor()._write_no_endian(this); + return; + } + } + + // Either this is not an ObjectImpl or it has the + // unexpected delegate. Try to convert via ORBs + // object_to_string(). + if (orb != null) + { + IOR ior = IOR.parse(orb.object_to_string(x)); + ior._write_no_endian(this); + return; + } + else + throw new BAD_OPERATION("Please set the ORB for this stream."); + } + + /** + * Write the TypeCode. This implementation delegates functionality + * to {@link cdrTypeCode}. + * + * @param x a TypeCode to write. + */ + public void write_TypeCode(TypeCode x) + { + try + { + TypeCodeHelper.write(this, x); + } + catch (UserException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes an instance of the CORBA {@link Any}. + * This method writes the typecode, followed + * by value itself. In Any contains null + * (value not set), the {@link TCKind#tk_null} + * is written. + * + * @param x the {@link Any} to write. + */ + public void write_any(Any x) + { + Streamable value = x.extract_Streamable(); + if (value != null) + { + write_TypeCode(x.type()); + value._write(this); + } + else + { + primitiveTypeCode p = new primitiveTypeCode(TCKind.tk_null); + write_TypeCode(p); + } + } + + /** + * Writes a single byte, 0 for <code>false</code>, + * 1 for <code>true</code>. + * + * @param x the value to write + */ + public void write_boolean(boolean x) + { + try + { + b.write(x ? 1 : 0); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the boolean array. + * + * @param x array + * @param ofs offset + * @param len length. + */ + public void write_boolean_array(boolean[] x, int ofs, int len) + { + try + { + for (int i = ofs; i < ofs + len; i++) + { + b.write(x [ i ] ? 1 : 0); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the lower byte of the passed parameter. + * @param x the char to write + * + * It is effective to write more characters at once. + */ + public void write_char(char x) + { + try + { + if (narrow_native) + b.write(x); + else + { + OutputStreamWriter ow = + new OutputStreamWriter((OutputStream) b, narrow_charset); + ow.write(x); + ow.flush(); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the lower bytes of the passed array members. + * + * @param chars an array + * @param offsets offset + * @param length length + */ + public void write_char_array(char[] chars, int offset, int length) + { + try + { + if (narrow_native) + { + for (int i = offset; i < offset + length; i++) + { + b.write(chars [ i ]); + } + } + else + { + OutputStreamWriter ow = + new OutputStreamWriter((OutputStream) b, narrow_charset); + ow.write(chars, offset, length); + ow.flush(); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the double value (IEEE 754 format). + */ + public void write_double(double x) + { + try + { + align(8); + b.writeDouble(x); + } + catch (Exception ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the array of double values. + */ + public void write_double_array(double[] x, int ofs, int len) + { + try + { + align(8); + for (int i = ofs; i < ofs + len; i++) + { + b.writeDouble(x [ i ]); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes CORBA fixed, storing all digits but not the scale. + * The end of the record on <code>fixed</code> can + * be determined from its last byte. + */ + public void write_fixed(BigDecimal fixed) + { + try + { + BigDecimalHelper.write(this, fixed); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + catch (BadKind ex) + { + Unexpected.error(ex); + } + } + + /** + * Write the float value (IEEE 754 format). + */ + public void write_float(float x) + { + try + { + align(4); + b.writeFloat(x); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes an array of the float values. + */ + public void write_float_array(float[] x, int ofs, int len) + { + try + { + align(4); + for (int i = ofs; i < ofs + len; i++) + { + b.writeFloat(x [ i ]); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the integer value (CORBA long, four bytes, high byte first). + * @param x the value to write. + */ + public void write_long(int x) + { + try + { + align(4); + b.writeInt(x); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the array of integer (CORBA long) values. + * + * @param x value + * @param ofs offset + * @param len length + */ + public void write_long_array(int[] x, int ofs, int len) + { + try + { + align(4); + for (int i = ofs; i < ofs + len; i++) + { + b.writeInt(x [ i ]); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the long (CORBA long long) value, 8 bytes, + * high byte first. + * + * @param x the value to write. + */ + public void write_longlong(long x) + { + try + { + align(8); + b.writeLong(x); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the array of longs (CORBA long longs) values. + * + * @param x value + * @param ofs offset + * @param len length + */ + public void write_longlong_array(long[] x, int ofs, int len) + { + try + { + align(8); + for (int i = ofs; i < ofs + len; i++) + { + b.writeLong(x [ i ]); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes this byte. + * @param x + */ + public void write_octet(byte x) + { + try + { + b.writeByte(x); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the array of bytes (CORBA octets) values. + * + * @param x value + * @param ofs offset + * @param len length + */ + public void write_octet_array(byte[] x, int ofs, int len) + { + try + { + b.write(x, ofs, len); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes first the size of array, and then the byte array using + * the {@link java.io.OutputStream#write(byte[]) }. The sequence + * being written is preceeded by the int, representing the array + * length. + */ + public void write_sequence(byte[] buf) + { + try + { + write_long(buf.length); + write(buf); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } + + /** + * Writes the contents of the provided stream. + * The sequence being written is preceeded by the int, + * representing the stream buffer length (the number of + * bytes being subsequently written). + */ + public void write_sequence(cdrBufOutput from) + { + try + { + write_long(from.buffer.size()); + from.buffer.writeTo(this); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } + + /** + * Writes the two byte integer (short), high byte first. + * + * @param x the integer to write. + */ + public void write_short(short x) + { + try + { + align(2); + b.writeShort(x); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the array of short (two byte integer) values. + * + * @param x value + * @param ofs offset + * @param len length + */ + public void write_short_array(short[] x, int ofs, int len) + { + try + { + align(2); + for (int i = ofs; i < ofs + len; i++) + { + b.writeShort(x [ i ]); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the string. This implementation first calls + * String.getBytes() and then writes the length of the returned + * array (as CORBA ulong) and the returned array itself. + * + * The encoding information, if previously set, is taken + * into consideration. + * + * @param x the string to write. + */ + public void write_string(String x) + { + try + { + byte[] ab = x.getBytes(narrow_charset); + write_long(ab.length + 1); + write(ab); + + // write null terminator. + write(0); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the CORBA unsigned long in the same way as CORBA long. + */ + public void write_ulong(int x) + { + write_long(x); + } + + /** + * Writes the array of CORBA unsigned longs in the same way as + * array of ordinary longs. + */ + public void write_ulong_array(int[] x, int ofs, int len) + { + write_long_array(x, ofs, len); + } + + /** + * Write the unsigned long long in the same way as an ordinary long long. + * + * @param x a value to write. + */ + public void write_ulonglong(long x) + { + write_longlong(x); + } + + /** + * Write the array of unsingel long longs in the same way + * an an array of the ordinary long longs. + */ + public void write_ulonglong_array(long[] x, int ofs, int len) + { + write_longlong_array(x, ofs, len); + } + + /** + * Write the unsigned short in the same way as an ordinary short. + */ + public void write_ushort(short x) + { + write_short(x); + } + + /** + * Write an array of unsigned short integersin the same way + * as an array of ordinary short integers. + */ + public void write_ushort_array(short[] x, int ofs, int len) + { + write_short_array(x, ofs, len); + } + + /** + * Writes the character as two byte short integer (Unicode value), + * high byte first. Writes in Big Endian, but never writes the + * endian indicator. + * + * The character is always written using the native UTF-16BE charset + * because its size under arbitrary encoding is not evident. + */ + public void write_wchar(char x) + { + try + { + if (giop.until_inclusive(1, 1)) + align(2); + + if (wide_native) + b.writeShort(x); + else + { + OutputStreamWriter ow = + new OutputStreamWriter((OutputStream) b, wide_charset); + ow.write(x); + ow.flush(); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Write the array of wide chars. + * + * @param chars the array of wide chars + * @param offset offset + * @param length length + * + * The char array is always written using the native UTF-16BE charset + * because the character size under arbitrary encoding is not evident. + */ + public void write_wchar_array(char[] chars, int offset, int length) + { + try + { + if (giop.until_inclusive(1, 1)) + align(2); + + if (wide_native) + { + for (int i = offset; i < offset + length; i++) + { + b.writeShort(chars [ i ]); + } + } + else + { + OutputStreamWriter ow = + new OutputStreamWriter((OutputStream) b, wide_charset); + ow.write(chars, offset, length); + ow.flush(); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Writes the length of the string in bytes (not characters) and + * then all characters as two byte unicode chars. Adds the + * Big Endian indicator, 0xFFFE, at the beginning and null wide char at + * the end. + * + * @param x the string to write. + */ + public void write_wstring(String x) + { + try + { + if (giop.since_inclusive(1, 2)) + { + byte[] bytes = x.getBytes(wide_charset); + write_sequence(bytes); + } + else + { + // Encoding with null terminator always in UTF-16. + // The wide null terminator needs extra two bytes. + write_long(2 * x.length() + 2); + + for (int i = 0; i < x.length(); i++) + { + b.writeShort(x.charAt(i)); + } + + // Write null terminator. + b.writeShort(0); + } + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** {@inheritDoc} */ + public void write_any_array(Any[] anys, int offset, int length) + { + for (int i = offset; i < offset + length; i++) + { + write_any(anys [ i ]); + } + } + + public String[] _truncatable_ids() + { + /**@todo Implement this org.omg.CORBA.portable.ValueBase abstract method*/ + throw new java.lang.UnsupportedOperationException("Method _truncatable_ids() not yet implemented."); + } + + /** {@inheritDoc} */ + public void write_Abstract(java.lang.Object value) + { + write_Abstract(value); + } + + /** {@inheritDoc} */ + public void write_Value(Serializable value) + { + write_Value(value); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/CDR/encapsulatedOutput.java b/libjava/classpath/gnu/CORBA/CDR/encapsulatedOutput.java new file mode 100644 index 00000000000..3350291bc73 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/CDR/encapsulatedOutput.java @@ -0,0 +1,146 @@ +/* EncapsulationOutput.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.CDR; + +import java.io.IOException; + +/** + * The encapsulated data, as they are defined by CORBA specification. + * This includes the extra 0 byte (Big endian) in the beginning. + * When written to the parent steam, the encapsulated data are preceeded + * by the data length in bytes. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class encapsulatedOutput + extends cdrOutput +{ + /** + * The Big Endian (most siginificant byte first flag). + */ + public static final byte BIG_ENDIAN = 0; + + /** + * The Little Endian (least siginificant byte first flag). + */ + public static final byte LITTLE_ENDIAN = 1; + + /** + * The byte buffer. + */ + public final aligningOutputStream buffer; + + /** + * The stream, where the data are being encapsulated. + */ + public final org.omg.CORBA.portable.OutputStream parent; + + /** + * Create the EncapsulationOutput with the given parent stream + * and the specified encoding. + */ + public encapsulatedOutput(org.omg.CORBA.portable.OutputStream _parent, + boolean use_big_endian) + { + super(); + buffer = new aligningOutputStream(); + setOutputStream(buffer); + parent = _parent; + write(use_big_endian?BIG_ENDIAN:LITTLE_ENDIAN); + } + + /** + * Set the alignment offset, if the index of the first byte in the + * stream is different from 0. + */ + public void setOffset(int an_offset) + { + buffer.setOffset(an_offset); + } + + /** + * Align the curretn position at the given natural boundary. + */ + public void align(int boundary) + { + buffer.align(boundary); + } + + /** + * Writes the content of the encapsulated output into the parent + * buffer. + */ + public void close() + { + try + { + parent.write_long(buffer.size()); + buffer.writeTo(parent); + } + catch (IOException ex) + { + InternalError err = new InternalError(); + err.initCause(ex); + throw err; + } + } + + /** + * Return the input stream that reads the previously written values. + */ + public org.omg.CORBA.portable.InputStream create_input_stream() + { + cdrBufInput in = new cdrBufInput(buffer.toByteArray()); + in.setOrb(orb); + + in.setVersion(giop); + in.setCodeSet(getCodeSet()); + + return in; + } + + /** + * Resets (clears) the buffer. + */ + public void reset() + { + buffer.reset(); + setOutputStream(buffer); + } +} diff --git a/libjava/classpath/gnu/CORBA/Connected_objects.java b/libjava/classpath/gnu/CORBA/Connected_objects.java new file mode 100644 index 00000000000..30d15e75c3a --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Connected_objects.java @@ -0,0 +1,231 @@ +/* Connected_objects.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +/** + * The repository of objects, that have been connected to the + * {@link FunctionalORB} by the method + * {@link ORB.connect(org.omg.CORBA.Object)}. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class Connected_objects +{ + /** + * The reference data about the connected object. + */ + public class cObject + { + /** + * Create an initialised instance. + */ + cObject(org.omg.CORBA.Object _object, int _port, byte[] _key) + { + object = _object; + port = _port; + key = _key; + } + + /** + * The object. + */ + public final org.omg.CORBA.Object object; + + /** + * The port on that the object is connected. + */ + public final int port; + + /** + * The object key. + */ + public final byte[] key; + + public boolean equals(java.lang.Object other) + { + if (other instanceof cObject) + { + cObject o = (cObject) other; + return o.object.equals(object) && o.port == port; + } + else + return false; + } + } + + /** + * The free number to give for the next instance. + * This field is incremented each time the + * new collection of the connected objects is created. + * Each collection has its own unique instance number. + */ + private static long free_object_number; + + /** + * The map of the all connected objects, maps the object key to the + * object. + */ + private Map objects = new TreeMap(new ByteArrayComparator()); + + /** + * Get the record of the stored object. + * + * @param object the stored object + * + * @return the record about the stored object, null if + * this object is not stored here. + */ + public cObject getKey(org.omg.CORBA.Object stored_object) + { + Map.Entry item; + Iterator iter = objects.entrySet().iterator(); + cObject ref; + + while (iter.hasNext()) + { + item = (Map.Entry) iter.next(); + ref = (cObject) item.getValue(); + if (stored_object.equals(ref.object)) + return ref; + } + return null; + } + + /** + * Add the new object to the repository. The object key is + * generated automatically. + * + * @param object the object to add. + * @param port, on that the ORB will be listening to the remote + * invocations. + * + * @return the newly created object record. + */ + public cObject add(org.omg.CORBA.Object object, int port) + { + return add(generateObjectKey(object), object, port); + } + + /** + * Add the new object to the repository. + * + * @param key the object key. + * @param object the object to add. + * @param port the port, on that the ORB will be listening on the + * remote invocations. + */ + public cObject add(byte[] key, org.omg.CORBA.Object object, int port) + { + cObject rec = new cObject(object, port, key); + objects.put(key, rec); + return rec; + } + + /** + * Get the stored object. + * + * @param key the key (in the byte array form). + * + * @return the matching object, null if none is matching. + */ + public cObject get(byte[] key) + { + return (cObject) objects.get(key); + } + + /** + * Get the map entry set. + * @return + */ + public Set entrySet() + { + return objects.entrySet(); + } + + /** + * Remove the given object. + * + * @param object the object to remove. + */ + public void remove(org.omg.CORBA.Object object) + { + cObject ref = getKey(object); + if (ref != null) + objects.remove(ref.key); + } + + /** + * Remove the given object, indiciating it by the key. + * + * @param object the object to remove. + */ + public void remove(byte[] key) + { + objects.remove(key); + } + + /** + * Generate the object key, unique in the currently + * running java virtual machine. + * + * The generated key includes the object class name + * and the absolute instance number. + * + * @return the generated key. + */ + protected byte[] generateObjectKey(org.omg.CORBA.Object object) + { + return (object.getClass().getName() + ":" + getFreeInstanceNumber()).getBytes(); + } + + /** + * Get next free instance number. + */ + private static synchronized long getFreeInstanceNumber() + { + long instance_number = free_object_number; + free_object_number++; + return instance_number; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/DefinitionKindHolder.java b/libjava/classpath/gnu/CORBA/DefinitionKindHolder.java new file mode 100644 index 00000000000..1ef7350ddec --- /dev/null +++ b/libjava/classpath/gnu/CORBA/DefinitionKindHolder.java @@ -0,0 +1,91 @@ +/* DefinitionKindHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.DefinitionKind; +import org.omg.CORBA.DefinitionKindHelper; + + +/** + * The definition kind holder. This class is not included in the original + * API specification, so we place it outside the org.omg namespace. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class DefinitionKindHolder + implements org.omg.CORBA.portable.Streamable +{ + /** + * The stored value. + */ + public DefinitionKind value; + + /** + * Create the initialised instance. + * @param initialValue + */ + public DefinitionKindHolder(DefinitionKind initialValue) + { + value = initialValue; + } + + /** + * Read from the CDR stream. + */ + public void _read(org.omg.CORBA.portable.InputStream in) + { + value = DefinitionKindHelper.read(in); + } + + /** + * Get the typecode. + */ + public org.omg.CORBA.TypeCode _type() + { + return DefinitionKindHelper.type(); + } + + /** + * Write into the CDR stream. + */ + public void _write(org.omg.CORBA.portable.OutputStream out) + { + DefinitionKindHelper.write(out, value); + } +} diff --git a/libjava/classpath/gnu/CORBA/DuplicateNameHolder.java b/libjava/classpath/gnu/CORBA/DuplicateNameHolder.java new file mode 100644 index 00000000000..ad004bc41b2 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/DuplicateNameHolder.java @@ -0,0 +1,106 @@ +/* DuplicateNameHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; +import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; +import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateNameHelper; + +/** +* A holder for the exception {@link DuplicateName}. + +* @author Audrius Meskauskas, Lithiania (AudriusA@Bioinformatics.org) +*/ +public class DuplicateNameHolder + implements Streamable +{ + /** + * The stored DuplicateName value. + */ + public DuplicateName value; + + /** + * Create the unitialised instance, leaving the value field + * with default <code>null</code> value. + */ + public DuplicateNameHolder() + { + } + + /** + * Create the initialised instance. + * @param initialValue the value that will be assigned to + * the <code>value</code> field. + */ + public DuplicateNameHolder(DuplicateName initialValue) + { + value = initialValue; + } + + /** + * Fill in the {@link value} by data from the CDR stream. + * + * @param input the org.omg.CORBA.portable stream to read. + */ + public void _read(InputStream input) + { + value = DuplicateNameHelper.read(input); + } + + /** + * Write the stored value into the CDR stream. + * + * @param output the org.omg.CORBA.portable stream to write. + */ + public void _write(OutputStream output) + { + DuplicateNameHelper.write(output, value); + } + + /** + * Get the typecode of the DuplicateName. + */ + public TypeCode _type() + { + return DuplicateNameHelper.type(); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/DynAn/NameValuePairHolder.java b/libjava/classpath/gnu/CORBA/DynAn/NameValuePairHolder.java new file mode 100644 index 00000000000..7afc81cd4ea --- /dev/null +++ b/libjava/classpath/gnu/CORBA/DynAn/NameValuePairHolder.java @@ -0,0 +1,95 @@ +/* NameValuePairHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.DynAn; + +import org.omg.CORBA.NameValuePair; +import org.omg.CORBA.NameValuePairHelper; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +/** + * The name-value pair holder. The {@link NameValuePair} has no standard + * holder defined, but it is needed to store the {@link NameValuePair} into + * {@link Any}. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class NameValuePairHolder + implements Streamable +{ + /** + * The stored value of the name value pair. + */ + public NameValuePair value; + + public NameValuePairHolder() + { + } + + public NameValuePairHolder(NameValuePair a_value) + { + value = a_value; + } + + /** + * Read the name value pair. + */ + public void _read(InputStream input) + { + value = NameValuePairHelper.read(input); + } + + /** + * Return the typecode of the name value pair. + */ + public TypeCode _type() + { + return NameValuePairHelper.type(); + } + + /** + * Write the name value pair. + */ + public void _write(OutputStream output) + { + NameValuePairHelper.write(output, value); + } +} diff --git a/libjava/classpath/gnu/CORBA/EmptyExceptionHolder.java b/libjava/classpath/gnu/CORBA/EmptyExceptionHolder.java new file mode 100644 index 00000000000..890ca5fe51c --- /dev/null +++ b/libjava/classpath/gnu/CORBA/EmptyExceptionHolder.java @@ -0,0 +1,132 @@ +/* EmptyStructHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.UNKNOWN; +import org.omg.CORBA.UnknownUserException; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +/** + * This holder can store any CORBA exception that has no user defined fields. + * Only the repository ID is written when the method {@link #_write} is called. + * The _read method is not supported for this holder. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class EmptyExceptionHolder + implements Streamable +{ + /** + * The wrapped exception. + */ + public Throwable value; + + /** + * The typecode of the wrapped exception. + */ + public TypeCode typecode; + + /** + * Create the exception holder, initialised to the given values. + * + * @param an_exception the wrapped exception. + * @param an_id the exception repository id. + */ + public EmptyExceptionHolder(Throwable an_exception, TypeCode a_typecode) + { + value = an_exception; + typecode = a_typecode; + } + + /** + * Reads the exception from the input stream. + * + * The value field obtains the value of either the read exception or + * the UNKNOWN if the repository ID does not match + * the exception from the reachable code. + */ + public void _read(InputStream input) + { + String id = input.read_string(); + Object ex = ObjectCreator.Idl2Object(id); + if (ex == null) + value = new UNKNOWN(id); + else + value = (Throwable) ex; + } + + /** + * Return the typecode of the stored exception. + * + * @return the value, passed as a_typecode in constructor. + */ + public TypeCode _type() + { + return typecode; + } + + /** + * Write the exception into the give output stream. Writes the + * repository id that is taken from the typecode. This method also + * works when no helper class is available. + * + * @param output a stream to write into. + * + * @throws BAD_OPERATION if the value for the holder is not set or + * the typecode cannot provide repository id. + */ + public void _write(OutputStream output) + { + try + { + output.write_string(typecode.id()); + } + catch (Exception ex) + { + BAD_OPERATION bad = new BAD_OPERATION(); + bad.initCause(ex); + throw bad; + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/ExceptionCreator.java b/libjava/classpath/gnu/CORBA/ExceptionCreator.java new file mode 100644 index 00000000000..536053cef90 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/ExceptionCreator.java @@ -0,0 +1,248 @@ +/* ExceptionCreator.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.CompletionStatus; +import org.omg.CORBA.CompletionStatusHelper; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.SystemException; +import org.omg.CORBA.UNKNOWN; +import org.omg.CORBA.UserException; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/** + * Creates the objects from the agreed IDL names. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ExceptionCreator +{ + /** + * The standard OMG prefix. + */ + public static final String OMG_PREFIX = "omg.org/"; + + /** + * The standard java prefix. + */ + public static final String JAVA_PREFIX = "org.omg."; + + /** + * Create the system exception with the given idl name. + * + * @param idl the exception IDL name, must match the syntax + * "IDL:<class/name>:1.0". + * @param minor the exception minor code. + * @param completed the exception completion status. + * + * @return the created exception. + */ + public static SystemException createSystemException(String idl, int minor, + CompletionStatus completed + ) + { + try + { + String cl = toClassName(idl); + Class exClass = Class.forName(cl); + + Constructor constructor = + exClass.getConstructor(new Class[] + { + String.class, int.class, + CompletionStatus.class + } + ); + + Object exception = + constructor.newInstance(new Object[] + { + " Remote exception " + idl + ", minor " + + minor + ", " + completed + ".", + new Integer(minor), completed + } + ); + + return (SystemException) exception; + } + catch (Exception ex) + { + ex.printStackTrace(); + return new UNKNOWN("Unsupported system exception", minor, completed); + } + } + + /** + * Read the system exception from the given stream. + * @param input the CDR stream to read from. + * @return the exception that has been stored in the stream + * (IDL name, minor code and completion status). + */ + public static SystemException readSystemException(InputStream input) + { + String idl = input.read_string(); + int minor = input.read_ulong(); + CompletionStatus status = CompletionStatusHelper.read(input); + + SystemException exception = + ExceptionCreator.createSystemException(idl, minor, status); + + return exception; + } + + /** + * Reads the user exception, having the given Id, from the + * input stream. The id is expected to be in the form like + * 'IDL:test/org/omg/CORBA/ORB/communication/ourUserException:1.0' + * + * @param idl the exception idl name. + * @param input the stream to read from. + * + * @return the loaded exception. + * @return null if the helper class cannot be found. + */ + public static UserException readUserException(String idl, InputStream input) + { + try + { + String holder = toHelperName(idl); + + System.out.println("Helper " + holder); + + Class holderClass = Class.forName(holder); + + Method read = + holderClass.getMethod("read", + new Class[] + { + org.omg.CORBA.portable.InputStream.class + } + ); + + return (UserException) read.invoke(null, new Object[] { input }); + } + catch (MARSHAL mex) + { + // This one is ok to throw + throw mex; + } + catch (Exception ex) + { + ex.printStackTrace(); + return null; + } + } + + /** + * Writes the system exception data to CDR output stream. + * + * @param output a stream to write data to. + * @param ex an exception to write. + */ + public static void writeSystemException(OutputStream output, + SystemException ex + ) + { + String exIDL = toIDL(ex.getClass().getName()); + output.write_string(exIDL); + output.write_ulong(ex.minor); + CompletionStatusHelper.write(output, ex.completed); + } + + /** + * Converts teh given IDL name to class name. + * + * @param IDL the idl name. + * + */ + protected static String toClassName(String IDL) + { + String s = IDL; + int a = s.indexOf(':') + 1; + int b = s.lastIndexOf(':'); + + s = IDL.substring(a, b); + + if (s.startsWith(OMG_PREFIX)) + s = JAVA_PREFIX + s.substring(OMG_PREFIX.length()); + + return s.replace('/', '.'); + } + + /** + * Gets the helper class name from the string like + * 'IDL:test/org/omg/CORBA/ORB/communication/ourUserException:1.0' + * + * @param IDL the idl name. + */ + protected static String toHelperName(String IDL) + { + String s = IDL; + int a = s.indexOf(':') + 1; + int b = s.lastIndexOf(':'); + + s = IDL.substring(a, b); + + if (s.startsWith(OMG_PREFIX)) + s = JAVA_PREFIX + s.substring(OMG_PREFIX.length()); + + return s.replace('/', '.') + "Helper"; + } + + /** + * Convert the class name to IDL name. + * + * @param cn the class name. + * + * @return the idl name. + */ + protected static String toIDL(String cn) + { + if (cn.startsWith(JAVA_PREFIX)) + cn = cn.substring(JAVA_PREFIX.length()); + + cn = cn.replace('.', '/'); + + return "IDL:" + OMG_PREFIX + cn + ":1.0"; + } +} diff --git a/libjava/classpath/gnu/CORBA/Functional_ORB.java b/libjava/classpath/gnu/CORBA/Functional_ORB.java new file mode 100644 index 00000000000..7395f3172e4 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Functional_ORB.java @@ -0,0 +1,1438 @@ +/* FunctionalORB.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.CDR.cdrBufOutput; +import gnu.CORBA.GIOP.ErrorMessage; +import gnu.CORBA.GIOP.MessageHeader; +import gnu.CORBA.GIOP.ReplyHeader; +import gnu.CORBA.GIOP.RequestHeader; +import gnu.CORBA.GIOP.CloseMessage; +import gnu.CORBA.NamingService.NamingServiceTransient; + +import org.omg.CORBA.BAD_INV_ORDER; +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.BAD_PARAM; +import org.omg.CORBA.CompletionStatus; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NO_RESOURCES; +import org.omg.CORBA.OBJECT_NOT_EXIST; +import org.omg.CORBA.ORBPackage.InvalidName; +import org.omg.CORBA.Request; +import org.omg.CORBA.SystemException; +import org.omg.CORBA.UNKNOWN; +import org.omg.CORBA.portable.Delegate; +import org.omg.CORBA.portable.InvokeHandler; +import org.omg.CORBA.portable.ObjectImpl; +import org.omg.CosNaming.NamingContextExt; +import org.omg.CosNaming.NamingContextExtHelper; + +import java.applet.Applet; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.UnknownHostException; + +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import java.util.Properties; +import java.util.StringTokenizer; +import java.util.TreeMap; + +/** + * The ORB implementation, capable to handle remote invocations on the + * registered object. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class Functional_ORB + extends Restricted_ORB +{ + /** + * A server, responsible for listening on requests on some + * local port. The ORB may listen on multiple ports and process + * the requests in separate threads. Normally the server takes + * one port per object being served. + */ + class portServer + extends Thread + { + /** + * The number of the currently running parallel threads. + */ + private int running_threads; + + /** + * The port on that this portServer is listening for requests. + */ + int s_port; + + /** + * The server socket of this portServer. + */ + ServerSocket service; + + /** + * True if the serving node must shutdown due + * call of the close_now(). + */ + boolean terminated; + + /** + * Create a new portServer, serving on specific port. + */ + portServer(int _port) + { + s_port = _port; + } + + /** + * Enter the serving loop (get request/process it). + * All portServer normally terminate thy threads when + * the Functional_ORB.running is set to false. + */ + public void run() + { + try + { + service = new ServerSocket(s_port); + } + catch (IOException ex) + { + BAD_OPERATION bad = + new BAD_OPERATION("Unable to open the server socket."); + bad.initCause(ex); + throw bad; + } + + while (running) + { + try + { + serve(this, service); + } + catch (SocketException ex) + { + // May be thrown when the service is closed by + // the close_now(). + if (terminated) + return; + } + catch (Exception iex) + { + // Wait 5 seconds. Do not terminate the + // service due potentially transient error. + try + { + Thread.sleep(5000); + } + catch (InterruptedException ex) + { + } + } + } + } + + /** + * Forcibly close the server socket and mark this port as free. + */ + public void close_now() + { + try + { + terminated = true; + service.close(); + } + catch (Exception ex) + { + // This may happen if the service has not been opened or + // cannot be closed. Return without action. + } + } + + /** + * If the thread is no longer in use, close the socket (if opened). + */ + protected void finalize() + { + close_now(); + } + } + + /** + * The default value where the first instance of this ORB will start + * looking for a free port. + */ + public static int DEFAULT_INITIAL_PORT = 1126; + + /** + * The property of port, on that this ORB is listening for requests from clients. + * This class supports one port per ORB only. + */ + public static final String LISTEN_ON = "gnu.classpath.CORBA.ListenOn"; + + /** + * The property, defining the IOR of the intial reference to resolve. + */ + public static final String REFERENCE = "org.omg.CORBA.ORBInitRef"; + + /** + * The property, defining the port on that the default name service is running. + */ + public static final String NS_PORT = "org.omg.CORBA.ORBInitialPort"; + + /** + * The property, defining the host on that the default name service is running. + */ + public static final String NS_HOST = "org.omg.CORBA.ORBInitialHost"; + + /** + * The string, defining the naming service initial reference. + */ + public static final String NAME_SERVICE = "NameService"; + + /** + * The if the client has once opened a socket, it should start sending + * the message header in a given time. Otherwise the server will close the + * socket. This prevents server hang when the client opens the socket, + * but does not send any message, usually due crash on the client side. + */ + public static String START_READING_MESSAGE = + "gnu.classpath.CORBA.TOUT_START_READING_MESSAGE"; + + /** + * If the client has started to send the request message, the socket time + * out changes to the specified value. + */ + public static String WHILE_READING = "gnu.classpath.CORBA.TOUT_WHILE_READING"; + + /** + * If the message body is received, the time out changes to the + * specifice value. This must be longer, as includes time, required to + * process the received task. We make it 40 minutes. + */ + public static String AFTER_RECEIVING = + "gnu.classpath.CORBA.TOUT_AFTER_RECEIVING"; + + /** + * The address of the local host. + */ + public final String LOCAL_HOST; + + /** + * The if the client has once opened a socket, it should start sending + * the message header in a given time. Otherwise the server will close the + * socket. This prevents server hang when the client opens the socket, + * but does not send any message, usually due crash on the client side. + */ + private int TOUT_START_READING_MESSAGE = 20 * 1000; + + // (Here and below, we use * to make the meaning of the constant clearler). + + /** + * If the client has started to send the request message, the socket time + * out changes to the specified value. + */ + private int TOUT_WHILE_READING = 2 * 60 * 1000; + + /** + * If the message body is received, the time out changes to the + * specifice value. This must be longer, as includes time, required to + * process the received task. We make it 40 minutes. + */ + private int TOUT_AFTER_RECEIVING = 40 * 60 * 1000; + + /** + * Some clients tend to submit multiple requests over the + * same socket. The server waits for the next request on + * the same socket for the duration, specified + * below. In additions, the request of this implementation also + * waits for the same duration before closing the socket. + * The default time is seven seconds. + */ + public static int TANDEM_REQUESTS = 7000; + + /** + * The map of the already conncted objects. + */ + protected final Connected_objects connected_objects = new Connected_objects(); + + /** + * The maximal CORBA version, supported by this ORB. The default value + * 0 means that the ORB will not check the request version while trying + * to respond. + */ + protected Version max_version; + + /** + * Setting this value to false causes the ORB to shutdown after the + * latest serving operation is complete. + */ + protected boolean running; + + /** + * The map of the initial references. + */ + private Map initial_references = new TreeMap(); + + /** + * The currently active portServers. + */ + private ArrayList portServers = new ArrayList(); + + /** + * The host, on that the name service is expected to be running. + */ + private String ns_host; + + /** + * Probably free port, under that the ORB will try listening for + * remote requests first. When the new object is connected, this + * port is used first, then it is incremented by 1, etc. If the given + * port is not available, up to 20 subsequent values are tried and then + * the parameterless server socket contructor is called. The constant is + * shared between multiple instances of this ORB. + */ + private static int Port = DEFAULT_INITIAL_PORT; + + /** + * The port, on that the name service is expected to be running. + */ + private int ns_port = 900; + + /** + * The instance, stored in this field, handles the asynchronous dynamic + * invocations. + */ + protected Asynchron asynchron = new Asynchron(); + + /** + * The list of the freed ports. The ORB reuses ports, when possible. + */ + protected LinkedList freed_ports = new LinkedList(); + + /** + * The maximal allowed number of the currently running parallel + * threads per object. For security reasons, this is made private and + * unchangeable. After exceeding this limit, the NO_RESOURCES + * is thrown back to the client. + */ + private int MAX_RUNNING_THREADS = 256; + + /** + * Create the instance of the Functional ORB. + */ + public Functional_ORB() + { + try + { + LOCAL_HOST = ns_host = InetAddress.getLocalHost().getHostAddress(); + initial_references.put("CodecFactory", new gnuCodecFactory(this)); + } + catch (UnknownHostException ex) + { + BAD_OPERATION bad = + new BAD_OPERATION("Unable to open the server socket."); + bad.initCause(ex); + throw bad; + } + } + + /** + * If the max version is assigned, the orb replies with the error + * message if the request version is above the supported 1.2 version. + * This behavior is recommended by OMG, but not all implementations + * respond that error message by re-sending the request, encoded in the older + * version. + */ + public void setMaxVersion(Version max_supported) + { + max_version = max_supported; + } + + /** + * Get the maximal supported GIOP version or null if the version is + * not checked. + */ + public Version getMaxVersion() + { + return max_version; + } + + /** + * Get the currently free port, starting from the initially set port + * and going up max 20 steps, then trying to bind into any free + * address. + * + * @return the currently available free port. + * + * @throws NO_RESOURCES if the server socked cannot be opened on the + * local host. + */ + public int getFreePort() + throws BAD_OPERATION + { + ServerSocket s; + int a_port; + + try + { + // If there are some previously freed ports, use them first. + if (!freed_ports.isEmpty()) + { + Integer free = (Integer) freed_ports.getLast(); + freed_ports.removeLast(); + s = new ServerSocket(free.intValue()); + s.close(); + return free.intValue(); + } + } + catch (Exception ex) + { + // This may be thrown if the request for the new port has arrived + // before the current service is completly shutdown. + // OK then, use a new port. + } + + for (a_port = Port; a_port < Port + 20; a_port++) + { + try + { + s = new ServerSocket(a_port); + s.close(); + Port = a_port + 1; + return a_port; + } + catch (IOException ex) + { + // Repeat the loop if this exception has been thrown. + } + } + + try + { + // Try any port. + s = new ServerSocket(); + a_port = s.getLocalPort(); + s.close(); + return a_port; + } + catch (IOException ex) + { + NO_RESOURCES bad = + new NO_RESOURCES("Unable to open the server socket."); + bad.initCause(ex); + throw bad; + } + } + + /** + * Set the port, on that the server is listening for the client requests. + * If only one object is connected to the orb, the server will be + * try listening on this port first. It the port is busy, or if more + * objects are connected, the subsequent object will receive a larger + * port values, skipping unavailable ports, if required. The change + * applies globally. + * + * @param a_Port a port, on that the server is listening for requests. + */ + public static void setPort(int a_Port) + { + Port = a_Port; + } + + /** + * Connect the given CORBA object to this ORB. After the object is + * connected, it starts receiving remote invocations via this ORB. + * + * The ORB tries to connect the object to the port, that has been + * previously set by {@link setPort(int)}. On failure, it tries + * 20 subsequent larger values and then calls the parameterless + * server socked constructor to get any free local port. + * If this fails, the {@link NO_RESOURCES} is thrown. + * + * @param object the object, must implement the {@link InvokeHandler}) + * interface. + * + * @throws BAD_PARAM if the object does not implement the + * {@link InvokeHandler}). + */ + public void connect(org.omg.CORBA.Object object) + { + int a_port = getFreePort(); + + Connected_objects.cObject ref = connected_objects.add(object, a_port); + IOR ior = createIOR(ref); + prepareObject(object, ior); + if (running) + startService(ior); + } + + /** + * Connect the given CORBA object to this ORB, explicitly specifying + * the object key. + * + * The ORB tries to connect the object to the port, that has been + * previously set by {@link setPort(int)}. On failure, it tries + * 20 subsequent larger values and then calls the parameterless + * server socked constructor to get any free local port. + * If this fails, the {@link NO_RESOURCES} is thrown. + * + * @param object the object, must implement the {@link InvokeHandler}) + * interface. + * @param key the object key, usually used to identify the object from + * remote side. + * + * @throws BAD_PARAM if the object does not implement the + * {@link InvokeHandler}). + */ + public void connect(org.omg.CORBA.Object object, byte[] key) + { + int a_port = getFreePort(); + + Connected_objects.cObject ref = connected_objects.add(key, object, a_port); + IOR ior = createIOR(ref); + prepareObject(object, ior); + if (running) + startService(ior); + } + + /** + * Start the service on the given port of this IOR. + * + * @param ior the ior (only Internet.port is used). + */ + private void startService(IOR ior) + { + portServer p = new portServer(ior.Internet.port); + portServers.add(p); + p.start(); + } + + /** + * Destroy this server, releasing the occupied resources. + */ + public void destroy() + { + super.destroy(); + + portServer p; + for (int i = 0; i < portServers.size(); i++) + { + p = (portServer) portServers.get(i); + p.close_now(); + } + } + + /** + * Disconnect the given CORBA object from this ORB. The object will be + * no longer receiving the remote invocations. In response to the + * remote invocation on this object, the ORB will send the + * exception {@link OBJECT_NOT_EXIST}. The object, however, is not + * destroyed and can receive the local invocations. + + * @param object the object to disconnect. + */ + public void disconnect(org.omg.CORBA.Object object) + { + Connected_objects.cObject rmKey = null; + + // Handle the case when it is possible to get the object key. + // Handle the case when the object is known, but not local. + if (object instanceof ObjectImpl) + { + Delegate delegate = ((ObjectImpl) object)._get_delegate(); + if (delegate instanceof Simple_delegate) + { + byte[] key = ((Simple_delegate) delegate).getIor().key; + rmKey = connected_objects.get(key); + } + } + + // Try to find and disconned the object that is not an instance of the + // object implementation. + if (rmKey == null) + rmKey = connected_objects.getKey(object); + + // Disconnect the object on any success. + if (rmKey != null) + { + // Find and stop the corresponding portServer. + portServer p; + StopService: + for (int i = 0; i < portServers.size(); i++) + { + p = (portServer) portServers.get(i); + if (p.s_port == rmKey.port) + { + p.close_now(); + freed_ports.addFirst(new Integer(rmKey.port)); + break StopService; + } + connected_objects.remove(rmKey.key); + } + } + } + + /** + * Find the local object, connected to this ORB. + * + * @param ior the ior of the potentially local object. + * + * @return the local object, represented by the given IOR, + * or null if this is not a local connected object. + */ + public org.omg.CORBA.Object find_local_object(IOR ior) + { + // Must be the same host. + if (!ior.Internet.host.equals(LOCAL_HOST)) + return null; + + // Must be the same port. + if (ior.Internet.port != Port) + return null; + + return find_connected_object(ior.key); + } + + /** + * List the initially available CORBA objects (services). + * + * @return a list of services. + * + * @see resolve_initial_references(String) + */ + public String[] list_initial_services() + { + String[] refs = new String[ initial_references.size() ]; + int p = 0; + + Iterator iter = initial_references.keySet().iterator(); + while (iter.hasNext()) + refs [ p++ ] = (String) iter.next(); + + return refs; + } + + /** + * Get the IOR reference string for the given object. + * The string embeds information about the object + * repository Id, its access key and the server internet + * address and port. With this information, the object + * can be found by another ORB, possibly located on remote + * computer. + * + * @param the CORBA object + * @return the object IOR representation. + * + * @throws BAD_PARAM if the object has not been previously + * connected to this ORB. + * @throws BAD_OPERATION in the unlikely case if the local host + * address cannot be resolved. + * + * @see string_to_object(String) + */ + public String object_to_string(org.omg.CORBA.Object forObject) + { + // Handle the case when the object is known, but not local. + if (forObject instanceof ObjectImpl) + { + Delegate delegate = ((ObjectImpl) forObject)._get_delegate(); + if (delegate instanceof Simple_delegate) + return ((Simple_delegate) delegate).getIor().toStringifiedReference(); + } + + // Handle the case when the object is local. + Connected_objects.cObject rec = connected_objects.getKey(forObject); + + if (rec == null) + throw new BAD_PARAM("The object " + forObject + + " has not been previously connected to this ORB" + ); + + IOR ior = createIOR(rec); + + return ior.toStringifiedReference(); + } + + /** + * Find and return the easily accessible CORBA object, addressed + * by name. + * + * @param name the object name. + * @return the object + * + * @throws org.omg.CORBA.ORBPackage.InvalidName if the given name + * is not associated with the known object. + */ + public org.omg.CORBA.Object resolve_initial_references(String name) + throws InvalidName + { + org.omg.CORBA.Object object = null; + try + { + object = (org.omg.CORBA.Object) initial_references.get(name); + if (object == null && name.equals(NAME_SERVICE)) + { + object = getDefaultNameService(); + if (object != null) + initial_references.put(NAME_SERVICE, object); + } + } + catch (Exception ex) + { + InvalidName err = new InvalidName(name); + err.initCause(ex); + throw err; + } + if (object != null) + return object; + else + throw new InvalidName("Not found: '" + name + "'"); + } + + /** + * Start the ORBs main working cycle + * (receive invocation - invoke on the local object - send response - + * wait for another invocation). + * + * The method only returns after calling {@link #shutdown(boolean)}. + */ + public void run() + { + running = true; + + // Instantiate the port server for each socket. + Iterator iter = connected_objects.entrySet().iterator(); + Map.Entry m; + Connected_objects.cObject obj; + + while (iter.hasNext()) + { + m = (Map.Entry) iter.next(); + obj = (Connected_objects.cObject) m.getValue(); + + portServer subserver = new portServer(obj.port); + portServers.add(subserver); + + // Reuse the current thread for the last portServer. + if (!iter.hasNext()) + { + // Discard the iterator, eliminating lock checks. + iter = null; + subserver.run(); + return; + } + else + subserver.start(); + } + } + + /** + * Shutdown the ORB server. + * + * @param wait_for_completion if true, the current thread is + * suspended until the shutdown process is complete. + */ + public void shutdown(boolean wait_for_completion) + { + super.shutdown(wait_for_completion); + running = false; + + if (!wait_for_completion) + { + for (int i = 0; i < portServers.size(); i++) + { + portServer p = (portServer) portServers.get(i); + p.close_now(); + } + } + } + + /** + * Find and return the CORBA object, addressed by the given + * IOR string representation. The object can (an usually is) + * located on a remote computer, possibly running a different + * (not necessary java) CORBA implementation. + * + * @param ior the object IOR representation string. + * + * @return the found CORBA object. + * @see object_to_string(org.omg.CORBA.Object) + */ + public org.omg.CORBA.Object string_to_object(String an_ior) + { + IOR ior = IOR.parse(an_ior); + org.omg.CORBA.Object object = find_local_object(ior); + if (object == null) + { + ObjectImpl impl = stubFinder.search(this, ior); + try + { + if (impl._get_delegate() == null) + { + impl._set_delegate(new IOR_Delegate(this, ior)); + } + } + catch (BAD_OPERATION ex) + { + // Some colaborants may throw this exception + // in response to the attempt to get the unset delegate. + impl._set_delegate(new IOR_Delegate(this, ior)); + } + + object = impl; + connected_objects.add(ior.key, impl, ior.Internet.port); + } + return object; + } + + /** + * Get the default naming service for the case when there no + * NameService entries. + */ + protected org.omg.CORBA.Object getDefaultNameService() + { + if (initial_references.containsKey(NAME_SERVICE)) + { + return (org.omg.CORBA.Object) initial_references.get(NAME_SERVICE); + } + + IOR ior = new IOR(); + ior.Id = NamingContextExtHelper.id(); + ior.Internet.host = ns_host; + ior.Internet.port = ns_port; + ior.key = NamingServiceTransient.getDefaultKey(); + + IOR_contructed_object iorc = new IOR_contructed_object(this, ior); + NamingContextExt namer = NamingContextExtHelper.narrow(iorc); + initial_references.put(NAME_SERVICE, namer); + return namer; + } + + /** + * Find and return the object, that must be previously connected + * to this ORB. Return null if no such object is available. + * + * @param key the object key. + * + * @return the connected object, null if none. + */ + protected org.omg.CORBA.Object find_connected_object(byte[] key) + { + Connected_objects.cObject ref = connected_objects.get(key); + return ref == null ? null : ref.object; + } + + /** + * Set the ORB parameters. This method is normally called from + * {@link #init(Applet, Properties)}. + * + * @param app the current applet. + * + * @param props application specific properties, passed as the second + * parameter in {@link #init(Applet, Properties)}. + * Can be <code>null</code>. + */ + protected void set_parameters(Applet app, Properties props) + { + useProperties(props); + + String[][] para = app.getParameterInfo(); + if (para != null) + { + for (int i = 0; i < para.length; i++) + { + if (para [ i ] [ 0 ].equals(LISTEN_ON)) + Port = Integer.parseInt(para [ i ] [ 1 ]); + + if (para [ i ] [ 0 ].equals(REFERENCE)) + { + StringTokenizer st = new StringTokenizer(para [ i ] [ 1 ], "="); + initial_references.put(st.nextToken(), + string_to_object(st.nextToken()) + ); + } + + if (para [ i ] [ 0 ].equals(NS_HOST)) + ns_host = para [ i ] [ 1 ]; + + if (para [ i ] [ 0 ].equals(START_READING_MESSAGE)) + TOUT_START_READING_MESSAGE = Integer.parseInt(para [ i ] [ 1 ]); + + if (para [ i ] [ 0 ].equals(WHILE_READING)) + TOUT_WHILE_READING = Integer.parseInt(para [ i ] [ 1 ]); + + if (para [ i ] [ 0 ].equals(AFTER_RECEIVING)) + TOUT_AFTER_RECEIVING = Integer.parseInt(para [ i ] [ 1 ]); + + try + { + if (para [ i ] [ 0 ].equals(NS_PORT)) + ns_port = Integer.parseInt(para [ i ] [ 1 ]); + } + catch (NumberFormatException ex) + { + BAD_PARAM bad = + new BAD_PARAM("Invalid " + NS_PORT + + "property, unable to parse '" + + props.getProperty(NS_PORT) + "'" + ); + bad.initCause(ex); + throw bad; + } + } + } + } + + /** + * Set the ORB parameters. This method is normally called from + * {@link #init(String[], Properties)}. + * + * @param para the parameters, that were passed as the parameters + * to the <code>main(String[] args)</code> method of the current standalone + * application. + * + * @param props application specific properties that were passed + * as a second parameter in {@link init(String[], Properties)}). + * Can be <code>null</code>. + */ + protected void set_parameters(String[] para, Properties props) + { + if (para.length > 1) + for (int i = 0; i < para.length - 1; i++) + { + if (para [ i ].endsWith("ListenOn")) + Port = Integer.parseInt(para [ i + 1 ]); + + if (para [ i ].endsWith("ORBInitRef")) + { + StringTokenizer st = new StringTokenizer(para [ i + 1 ], "="); + initial_references.put(st.nextToken(), + string_to_object(st.nextToken()) + ); + } + + if (para [ i ].endsWith("ORBInitialHost")) + ns_host = para [ i + 1 ]; + + try + { + if (para [ i ].endsWith("ORBInitialPort")) + ns_port = Integer.parseInt(para [ i + 1 ]); + } + catch (NumberFormatException ex) + { + throw new BAD_PARAM("Invalid " + para [ i ] + + "parameter, unable to parse '" + + props.getProperty(para [ i + 1 ]) + "'" + ); + } + } + + useProperties(props); + } + + private IOR createIOR(Connected_objects.cObject ref) + throws BAD_OPERATION + { + IOR ior = new IOR(); + ior.key = ref.key; + ior.Internet.port = ref.port; + + if (ref.object instanceof ObjectImpl) + { + ObjectImpl imp = (ObjectImpl) ref.object; + if (imp._ids().length > 0) + ior.Id = imp._ids() [ 0 ]; + } + if (ior.Id == null) + ior.Id = ref.object.getClass().getName(); + + try + { + ior.Internet.host = InetAddress.getLocalHost().getHostAddress(); + ior.Internet.port = ref.port; + } + catch (UnknownHostException ex) + { + throw new BAD_OPERATION("Cannot resolve the local host address"); + } + return ior; + } + + /** + * Prepare object for connecting it to this ORB. + * + * @param object the object being connected. + * + * @throws BAD_PARAM if the object does not implement the + * {@link InvokeHandler}). + */ + private void prepareObject(org.omg.CORBA.Object object, IOR ior) + throws BAD_PARAM + { + if (!(object instanceof InvokeHandler)) + throw new BAD_PARAM(object.getClass().getName() + + " does not implement InvokeHandler. " + ); + + // If no delegate is set, set the default delegate. + if (object instanceof ObjectImpl) + { + ObjectImpl impl = (ObjectImpl) object; + try + { + if (impl._get_delegate() == null) + { + impl._set_delegate(new Simple_delegate(this, ior)); + } + } + catch (BAD_OPERATION ex) + { + // Some colaborants may throw this exception. + impl._set_delegate(new Simple_delegate(this, ior)); + } + } + } + + /** + * Write the response message. + * + * @param net_out the stream to write response into + * @param msh_request the request message header + * @param rh_request the request header + * @param handler the invocation handler that has been used to + * invoke the operation + * @param sysEx the system exception, thrown during the invocation, + * null if none. + * + * @throws IOException + */ + private void respond_to_client(OutputStream net_out, + MessageHeader msh_request, + RequestHeader rh_request, + bufferedResponseHandler handler, + SystemException sysEx + ) + throws IOException + { + // Set the reply header properties. + ReplyHeader reply = handler.reply_header; + + if (sysEx != null) + reply.reply_status = ReplyHeader.SYSTEM_EXCEPTION; + else if (handler.isExceptionReply()) + reply.reply_status = ReplyHeader.USER_EXCEPTION; + else + reply.reply_status = ReplyHeader.NO_EXCEPTION; + + reply.request_id = rh_request.request_id; + + cdrBufOutput out = new cdrBufOutput(50 + handler.getBuffer().buffer.size()); + out.setOrb(this); + + out.setOffset(msh_request.getHeaderSize()); + + reply.write(out); + + // Write the reply data from the handler. + handler.getBuffer().buffer.writeTo(out); + + MessageHeader msh_reply = new MessageHeader(); + + msh_reply.version = msh_request.version; + msh_reply.message_type = MessageHeader.REPLY; + msh_reply.message_size = out.buffer.size(); + + // Write the reply. + msh_reply.write(net_out); + out.buffer.writeTo(net_out); + net_out.flush(); + } + + /** + * Contains a single servicing task. + * + * Normally, each task matches a single remote invocation. + * However under frequent tandem submissions the same + * task may span over several invocations. + * + * @param serverSocket the ORB server socket. + * + * @throws MARSHAL + * @throws IOException + */ + private void serve(final portServer p, ServerSocket serverSocket) + throws MARSHAL, IOException + { + final Socket service; + service = serverSocket.accept(); + + // Tell the server there are no more resources. + if (p.running_threads >= MAX_RUNNING_THREADS) + { + serveStep(service, true); + return; + } + + new Thread() + { + public void run() + { + try + { + synchronized (p) + { + p.running_threads++; + } + serveStep(service, false); + } + finally + { + synchronized (p) + { + p.running_threads--; + } + } + } + }.start(); + } + + /** + * A single servicing step, when the client socket is alrady open. + * + * Normally, each task matches a single remote invocation. + * However under frequent tandem submissions the same + * task may span over several invocations. + * + * @param service the opened client socket. + * @param no_resources if true, the "NO RESOURCES" exception + * is thrown to the client. + */ + private void serveStep(Socket service, boolean no_resources) + { + try + { + Serving: + while (true) + { + InputStream in = service.getInputStream(); + service.setSoTimeout(TOUT_START_READING_MESSAGE); + + MessageHeader msh_request = new MessageHeader(); + + try + { + msh_request.read(in); + } + catch (MARSHAL ex) + { + // This exception may be thrown due closing the connection. + return; + } + + if (max_version != null) + if (!msh_request.version.until_inclusive(max_version.major, + max_version.minor + ) + ) + { + OutputStream out = service.getOutputStream(); + new ErrorMessage(max_version).write(out); + return; + } + + byte[] r = new byte[ msh_request.message_size ]; + + int n = 0; + + service.setSoTimeout(TOUT_WHILE_READING); + + reading: + while (n < r.length) + { + n += in.read(r, n, r.length - n); + } + + service.setSoTimeout(TOUT_AFTER_RECEIVING); + + if (msh_request.message_type == MessageHeader.REQUEST) + { + RequestHeader rh_request; + + cdrBufInput cin = new cdrBufInput(r); + cin.setOrb(this); + cin.setVersion(msh_request.version); + cin.setOffset(msh_request.getHeaderSize()); + cin.setBigEndian(msh_request.isBigEndian()); + + rh_request = msh_request.create_request_header(); + + // Read header and auto set the charset. + rh_request.read(cin); + + // in 1.2 and higher, align the current position at + // 8 octet boundary. + if (msh_request.version.since_inclusive(1, 2)) + cin.align(8); + + // find the target object. + InvokeHandler target = + (InvokeHandler) find_connected_object(rh_request.object_key); + + // Prepare the reply header. This must be done in advance, + // as the size must be known for handler to set alignments + // correctly. + ReplyHeader rh_reply = msh_request.create_reply_header(); + + // TODO log errors about not existing objects and methods. + bufferedResponseHandler handler = + new bufferedResponseHandler(this, msh_request, rh_reply); + + SystemException sysEx = null; + + try + { + if (no_resources) + throw new NO_RESOURCES(); + if (target == null) + throw new OBJECT_NOT_EXIST(); + target._invoke(rh_request.operation, cin, handler); + } + catch (SystemException ex) + { + sysEx = ex; + + org.omg.CORBA.portable.OutputStream ech = + handler.createExceptionReply(); + ObjectCreator.writeSystemException(ech, ex); + } + catch (Exception except) + { + sysEx = + new UNKNOWN("Unknown", 2, CompletionStatus.COMPLETED_MAYBE); + + org.omg.CORBA.portable.OutputStream ech = + handler.createExceptionReply(); + + ObjectCreator.writeSystemException(ech, sysEx); + } + + // Write the response. + if (rh_request.isResponseExpected()) + { + OutputStream sou = service.getOutputStream(); + respond_to_client(sou, msh_request, rh_request, handler, + sysEx + ); + } + } + else if (msh_request.message_type == MessageHeader.CLOSE_CONNECTION || + msh_request.message_type == MessageHeader.MESSAGE_ERROR + ) + { + CloseMessage.close(service.getOutputStream()); + service.close(); + return; + } + else + ; + + // TODO log error: "Not a request message." + if (service != null && !service.isClosed()) + { + // Wait for the subsequent invocations on the + // same socket for the TANDEM_REQUEST duration. + service.setSoTimeout(TANDEM_REQUESTS); + } + else + return; + } + } + catch (SocketException ex) + { + // OK. + return; + } + catch (IOException ioex) + { + // Network error, probably transient. + // TODO log it. + return; + } + } + + private void useProperties(Properties props) + { + if (props != null) + { + if (props.containsKey(LISTEN_ON)) + Port = Integer.parseInt(props.getProperty(LISTEN_ON)); + + if (props.containsKey(NS_HOST)) + ns_host = props.getProperty(NS_HOST); + + try + { + if (props.containsKey(NS_PORT)) + ns_port = Integer.parseInt(props.getProperty(NS_PORT)); + + if (props.containsKey(START_READING_MESSAGE)) + TOUT_START_READING_MESSAGE = + Integer.parseInt(props.getProperty(START_READING_MESSAGE)); + + if (props.containsKey(WHILE_READING)) + TOUT_WHILE_READING = + Integer.parseInt(props.getProperty(WHILE_READING)); + + if (props.containsKey(AFTER_RECEIVING)) + TOUT_AFTER_RECEIVING = + Integer.parseInt(props.getProperty(AFTER_RECEIVING)); + } + catch (NumberFormatException ex) + { + throw new BAD_PARAM("Invalid " + NS_PORT + + "property, unable to parse '" + + props.getProperty(NS_PORT) + "'" + ); + } + + Enumeration en = props.elements(); + while (en.hasMoreElements()) + { + String item = (String) en.nextElement(); + if (item.equals(REFERENCE)) + initial_references.put(item, + string_to_object(props.getProperty(item)) + ); + } + } + } + + /** + * Get the next instance with a response being received. If all currently + * sent responses not yet processed, this method pauses till at least one of + * them is complete. If there are no requests currently sent, the method + * pauses till some request is submitted and the response is received. + * This strategy is identical to the one accepted by Suns 1.4 ORB + * implementation. + * + * The returned response is removed from the list of the currently + * submitted responses and is never returned again. + * + * @return the previously sent request that now contains the received + * response. + * + * @throws WrongTransaction If the method was called from the transaction + * scope different than the one, used to send the request. The exception + * can be raised only if the request is implicitly associated with some + * particular transaction. + */ + public Request get_next_response() + throws org.omg.CORBA.WrongTransaction + { + return asynchron.get_next_response(); + } + + /** + * Find if any of the requests that have been previously sent with + * {@link #send_multiple_requests_deferred}, have a response yet. + * + * @return true if there is at least one response to the previously + * sent request, false otherwise. + */ + public boolean poll_next_response() + { + return asynchron.poll_next_response(); + } + + /** + * Send multiple prepared requests expecting to get a reply. All requests + * are send in parallel, each in its own separate thread. When the + * reply arrives, it is stored in the agreed fields of the corresponing + * request data structure. If this method is called repeatedly, + * the new requests are added to the set of the currently sent requests, + * but the old set is not discarded. + * + * @param requests the prepared array of requests. + * + * @see #poll_next_response() + * @see #get_next_response() + * @see Request#send_deferred() + */ + public void send_multiple_requests_deferred(Request[] requests) + { + asynchron.send_multiple_requests_deferred(requests); + } + + /** + * Send multiple prepared requests one way, do not caring about the answer. + * The messages, containing requests, will be marked, indicating that + * the sender is not expecting to get a reply. + * + * @param requests the prepared array of requests. + * + * @see Request#send_oneway() + */ + public void send_multiple_requests_oneway(Request[] requests) + { + asynchron.send_multiple_requests_oneway(requests); + } + + /** + * Set the flag, forcing all server threads to terminate. + */ + protected void finalize() + throws java.lang.Throwable + { + running = false; + super.finalize(); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/GIOP/CancelHeader.java b/libjava/classpath/gnu/CORBA/GIOP/CancelHeader.java new file mode 100644 index 00000000000..9f4de0d87d9 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/CancelHeader.java @@ -0,0 +1,70 @@ +/* CancelHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; + +/** + * The message header for cancelling the request. + * + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public abstract class CancelHeader +{ + /** + * The Id of request being cancelled. + */ + public int request_id; + + /** + * Write the header. + * + * @param out a stream to write to. + */ + public abstract void read(InputStream input); + + /** + * Write the header. + * + * @param out a stream to write to. + */ + public abstract void write(OutputStream output); +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/CharSets_OSF.java b/libjava/classpath/gnu/CORBA/GIOP/CharSets_OSF.java new file mode 100644 index 00000000000..f3f35dbaa21 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/CharSets_OSF.java @@ -0,0 +1,235 @@ +/* CharSets_OSF.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import java.nio.charset.Charset; + +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Set; + +/** + * This class contains the codes, used to identify character sets + * in CORBA. These codes are defined in Open Software Foundation (OSF) + * code set registry + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class CharSets_OSF +{ + public static final int ASCII = 0x00010020; + public static final int ISO8859_1 = 0x00010001; + public static final int ISO8859_2 = 0x00010002; + public static final int ISO8859_3 = 0x00010003; + public static final int ISO8859_4 = 0x00010004; + public static final int ISO8859_5 = 0x00010005; + public static final int ISO8859_6 = 0x00010006; + public static final int ISO8859_7 = 0x00010007; + public static final int ISO8859_8 = 0x00010008; + public static final int ISO8859_9 = 0x00010009; + public static final int ISO8859_15_FDIS = 0x0001000F; + public static final int UTF8 = 0x05010001; + public static final int UTF16 = 0x00010109; + public static final int UCS2 = 0x00010100; + public static final int Cp1047 = 0x10020417; + public static final int Cp1250 = 0x100204E2; + public static final int Cp1251 = 0x100204E3; + public static final int Cp1252 = 0x100204E4; + public static final int Cp1253 = 0x100204E5; + public static final int Cp1254 = 0x100204E6; + public static final int Cp1255 = 0x100204E7; + public static final int Cp1256 = 0x100204E8; + public static final int Cp1257 = 0x100204E9; + public static final int Cp1363 = 0x10020553; + public static final int Cp1363C = 0x10020553; + public static final int Cp1381 = 0x10020565; + public static final int Cp1383 = 0x10020567; + public static final int Cp1386 = 0x1002056A; + public static final int Cp33722 = 0x100283BA; + public static final int Cp33722C = 0x100283BA; + public static final int Cp930 = 0x100203A2; + public static final int Cp943 = 0x100203AF; + public static final int Cp943C = 0x100203AF; + public static final int Cp949 = 0x100203B5; + public static final int Cp949C = 0x100203B5; + public static final int Cp950 = 0x100203B6; + public static final int Cp964 = 0x100203C4; + public static final int Cp970 = 0x100203CA; + public static final int EUC_JP = 0x00030010; + public static final int EUC_KR = 0x0004000A; + public static final int EUC_TW = 0x00050010; + + /** + * The native character set for the narrow character. + */ + public static final int NATIVE_CHARACTER = ISO8859_1; + + /** + * The native character set for the wide character. + */ + public static final int NATIVE_WIDE_CHARACTER = UTF16; + + /** + * Table to convert from the code to string name. + */ + private static Hashtable code_to_string; + + /** + * Table to convert from the string name to code. + */ + private static Hashtable string_to_code; + + /** + * Get the charset code from its name. + * + * @return the charset code of 0 if not defined. + */ + public static int getCode(String name) + { + if (string_to_code == null) + makeMap(); + + Integer code = (Integer) string_to_code.get(name); + return code == null ? 0 : code.intValue(); + } + + /** + * Get the charset name from its code. + * + * @return the code set name or nullfor the unknown code set. + */ + public static String getName(int code) + { + if (code_to_string == null) + makeMap(); + return (String) code_to_string.get(new Integer(code)); + } + + /** + * Get the list of supported char sets for that the CORBA codes are + * also known. + */ + public static int[] getSupportedCharSets() + { + Set supported_sets = Charset.availableCharsets().keySet(); + int[] supported = new int[ supported_sets.size() ]; + Iterator iter = supported_sets.iterator(); + + int i = 0; + int code; + while (iter.hasNext()) + { + code = getCode(iter.next().toString()); + if (code > 0) + supported [ i++ ] = code; + } + + // Truncate the unused part. + int[] f = new int[ i ]; + System.arraycopy(supported, 0, f, 0, f.length); + + return f; + } + + /** + * Create a convertion map. + */ + private static void makeMap() + { + code_to_string = new Hashtable(); + string_to_code = new Hashtable(); + + // Put standard char sets. + put(ASCII, "US-ASCII"); + put(ISO8859_1, "ISO-8859-1"); + put(UTF16, "UTF-16"); + + // Put other known char sets. + put(ISO8859_2, "ISO-8859-2"); + put(ISO8859_3, "ISO-8859-3"); + put(ISO8859_4, "ISO-8859-4"); + put(ISO8859_5, "ISO-8859-5"); + put(ISO8859_6, "ISO-8859-6"); + put(ISO8859_7, "ISO-8859-7"); + put(ISO8859_8, "ISO-8859-8"); + put(ISO8859_9, "ISO-8859-9"); + + put(UTF8, "UTF-8"); + put(UCS2, "UCS-2"); + + put(ISO8859_15_FDIS, "ISO8859-15-FDIS"); + + put(Cp1047, "Cp1047"); + put(Cp1250, "Cp1250"); + put(Cp1251, "Cp1251"); + put(Cp1252, "Cp1252"); + put(Cp1253, "Cp1253"); + put(Cp1254, "Cp1254"); + put(Cp1255, "Cp1255"); + put(Cp1256, "Cp1256"); + put(Cp1257, "Cp1257"); + put(Cp1363, "Cp1363"); + put(Cp1363C, "Cp1363C"); + put(Cp1381, "Cp1381"); + put(Cp1383, "Cp1383"); + put(Cp1386, "Cp1386"); + put(Cp33722, "Cp33722"); + put(Cp33722C, "Cp33722C"); + put(Cp930, "Cp930"); + put(Cp943, "Cp943"); + put(Cp943C, "Cp943C"); + put(Cp949, "Cp949"); + put(Cp949C, "Cp949C"); + put(Cp950, "Cp950"); + put(Cp964, "Cp964"); + put(Cp970, "Cp970"); + + put(EUC_JP, "EUC-JP"); + put(EUC_KR, "EUC-KR"); + put(EUC_TW, "EUC-TW"); + } + + private static void put(int code, String name) + { + Integer ic = new Integer(code); + + code_to_string.put(ic, name); + string_to_code.put(name, ic); + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/CloseMessage.java b/libjava/classpath/gnu/CORBA/GIOP/CloseMessage.java new file mode 100644 index 00000000000..d884329c440 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/CloseMessage.java @@ -0,0 +1,102 @@ +/* CloseMessage.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.IOR; + +import org.omg.CORBA.MARSHAL; + +import java.io.IOException; +import java.io.OutputStream; + +import java.net.Socket; + +/** + * The explicit command to close the connection. + * + * + * The close message consists from the message header only and + * is the same for GIOP 1.0, 1.1, 1.2 and 1.3. The CloseMessage + * uses the default value from the {@link MessageHeader}. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class CloseMessage + extends MessageHeader +{ + /** + * The singleton close message is typically enough, despite new + * instances may be instantiated if the specific version field + * value is mandatory. + */ + private static final CloseMessage Singleton = new CloseMessage(); + + /** + * Create a new error message, setting the message field + * to the {@link MESSAGE_CLOSE} and the version number to + * the given major and minor values. + */ + public CloseMessage() + { + message_type = CLOSE_CONNECTION; + } + + /** + * Send the close message to the given output stream. The method, + * however, does not close the socket itself, this must be done + * explicitly in the calling code. + * + * @param socketStream a stream, where the close message is + * written. + */ + public static void close(OutputStream socketStream) + { + try + { + Singleton.write(socketStream); + socketStream.flush(); + } + catch (IOException ex) + { + MARSHAL m = new MARSHAL("Unable to flush the stream"); + m.initCause(ex); + throw m; + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/GIOP/ErrorMessage.java b/libjava/classpath/gnu/CORBA/GIOP/ErrorMessage.java new file mode 100644 index 00000000000..8d3b3539be5 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/ErrorMessage.java @@ -0,0 +1,97 @@ +/* ErrorMessage.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.IOR; + +import java.io.IOException; +import java.io.OutputStream; + +import java.net.Socket; + +import org.omg.CORBA.MARSHAL; + +/** + * The error message is sent in response to the message, encoded + * in the unsupported version of the format or otherwise invalid. + * + * The error message consists from the message header only and + * is the same for GIOP 1.0, 1.1, 1.2 and 1.3. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ErrorMessage + extends MessageHeader +{ + /** + * Create a new error message, setting the message field + * to the {@link MESSAGE_ERROR} and the version number to + * the given major and minor values. + */ + public ErrorMessage(gnu.CORBA.Version msg_version) + { + version = msg_version; + message_type = MESSAGE_ERROR; + } + + /** + * Send the error message to the given IOR address. + * + * @param to the IOR address (host and port, other fields + * are not used). + */ + public void send(IOR ior) + { + try + { + Socket socket = new Socket(ior.Internet.host, ior.Internet.port); + + OutputStream socketOutput = socket.getOutputStream(); + write(socketOutput); + socketOutput.close(); + socket.close(); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/MessageHeader.java b/libjava/classpath/gnu/CORBA/GIOP/MessageHeader.java new file mode 100644 index 00000000000..61c46e11ea4 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/MessageHeader.java @@ -0,0 +1,350 @@ +/* MessageHeader.java -- GIOP 1.0 message header. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.CDR.BigEndianOutputStream; +import gnu.CORBA.CDR.LittleEndianInputStream; +import gnu.CORBA.CDR.LittleEndianOutputStream; +import gnu.CORBA.CDR.abstractDataOutputStream; +import gnu.CORBA.Version; + +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.portable.IDLEntity; + +import java.io.DataInputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.util.Arrays; +import gnu.CORBA.CDR.BigEndianInputStream; +import gnu.CORBA.CDR.abstractDataInputStream; +import java.io.InputStream; + +/** + * The GIOP message header. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class MessageHeader + implements IDLEntity +{ + /** + * Request message. + */ + public static final byte REQUEST = 0; + + /** + * Reply message + */ + public static final byte REPLY = 1; + + /** + * Cancel request message. + */ + public static final byte CANCEL_REQUEST = 2; + + /** + * Locate request message, used to check the server ability to + * process requests for the object reference. + * This message is also used to get the + * address where the object reference should be sent. + */ + public static final byte LOCATE_REQUEST = 3; + + /** + * Locate reply message, sent in response to the + * {@link #LocateRequest} message. + */ + public static final byte LOCATE_REPLY = 4; + + /** + * Instruction to close the connection. + */ + public static final byte CLOSE_CONNECTION = 5; + + /** + * Error report. + */ + public static final byte MESSAGE_ERROR = 6; + + /** + * The fragment messge, following the previous message that + * has more fragments flag set. Added in GIOP 1.1 + */ + public static final byte FRAGMENT = 7; + + /** + * This must always be "GIOP". + */ + public static final byte[] MAGIC = new byte[] { 'G', 'I', 'O', 'P' }; + + /** + * The message type names. + */ + protected static String[] types = + new String[] + { + "Request", "Reply", "Cancel", "Locate request", "Locate reply", + "Close connection", "Error", "Fragment" + }; + + /** + * The GIOP version. Initialised to 1.0 . + */ + public Version version; + + /** + * The flags field, introduced since GIOP 1.1. + */ + public byte flags = 0; + + /** + * The message type. + */ + public byte message_type = REQUEST; + + /** + * The message size, excluding the message header. + */ + public int message_size = 0; + + /** + * Create an empty message header, corresponding version 1.0. + */ + public MessageHeader() + { + version = new Version(1, 0); + } + + /** + * Create an empty message header, corresponding the given version. + * + * @param major the major message header version. + * @param minor the minot message header version. + */ + public MessageHeader(int major, int minor) + { + version = new Version(major, minor); + } + + /** + * Checks if the message is encoded in the Big Endian, most significant + * byte first. + */ + public boolean isBigEndian() + { + return (flags & 0x1) == 0; + } + + /** + * Set the encoding to use. + * + * @param use_big_endian if true (default), the Big Endian + * encoding is used. If false, the Little Endian encoding is used. + */ + public void setBigEndian(boolean use_big_endian) + { + if (use_big_endian) + flags = (byte) (flags & ~1); + else + flags = (byte) (flags | 1); + } + + /** + * Get the size of the message header itself. So far, it is always 12 bytes. + */ + public int getHeaderSize() + { + return 12; + } + + /** + * Get the message type as string. + * + * @param type the message type as int (the field {@link message_type}). + * + * @return the message type as string. + */ + public String getTypeString(int type) + { + try + { + return types [ type ]; + } + catch (ArrayIndexOutOfBoundsException ex) + { + return "unknown type (" + type + ")"; + } + } + + /** + * Creates reply header, matching the message header version number. + * + * @return one of {@link gnu.CORBA.GIOP.v1_0.ReplyHeader}, + * {@link gnu.CORBA.GIOP.v1_2.ReplyHeader}, etc - depending on + * the version number in this header. + */ + public ReplyHeader create_reply_header() + { + if (version.since_inclusive(1, 2)) + return new gnu.CORBA.GIOP.v1_2.ReplyHeader(); + else + return new gnu.CORBA.GIOP.v1_0.ReplyHeader(); + } + + /** + * Creates request header, matching the message header version number. + * + * @return one of {@link gnu.CORBA.GIOP.v1_0.RequestHeader}, + * {@link gnu.CORBA.GIOP.v1_2.RequestHeader}, etc - depending on + * the version number in this header. + */ + public RequestHeader create_request_header() + { + if (version.since_inclusive(1, 2)) + return new gnu.CORBA.GIOP.v1_2.RequestHeader(); + else + return new gnu.CORBA.GIOP.v1_0.RequestHeader(); + } + + /** + * Create the cancel header, matching the message header version number. + */ + public CancelHeader create_cancel_header() + { + return new gnu.CORBA.GIOP.v1_0.CancelHeader(); + } + + /** + * Create the error message. + */ + public ErrorMessage create_error_message() + { + return new ErrorMessage(version); + } + + /** + * Read the header from the stream. + * + * @param istream a stream to read from. + * + * @throws MARSHAL if this is not a GIOP 1.0 header. + */ + public void read(java.io.InputStream istream) + throws MARSHAL + { + try + { + byte[] xMagic = new byte[ MAGIC.length ]; + istream.read(xMagic); + if (!Arrays.equals(xMagic, MAGIC)) + throw new MARSHAL("Not a GIOP message"); + + version = Version.read_version(istream); + + abstractDataInputStream din; + + flags = (byte) istream.read(); + + // This checks the bit in the byte we have just received. + if (isBigEndian()) + din = new BigEndianInputStream(istream); + else + din = new LittleEndianInputStream(istream); + + message_type = (byte) din.read(); + + message_size = din.readInt(); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } + + /** + * Get the short string summary of the message. + * + * @return a short message summary. + */ + public String toString() + { + return "GIOP " + version + ", " + (isBigEndian() ? "Big" : "Little") + + " endian, " + getTypeString(message_type) + ", " + message_size + + " bytes. "; + } + + /** + * Write the header to stream. + * + * @param out a stream to write into. + */ + public void write(java.io.OutputStream out) + { + try + { + abstractDataOutputStream dout; + + if (isBigEndian()) + dout = new BigEndianOutputStream(out); + else + dout = new LittleEndianOutputStream(out); + + // Write magic sequence. + dout.write(MAGIC); + + // Write version number. + version.write((OutputStream) dout); + + dout.write(flags); + + dout.write(message_type); + + dout.writeInt(message_size); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/GIOP/ReplyHeader.java b/libjava/classpath/gnu/CORBA/GIOP/ReplyHeader.java new file mode 100644 index 00000000000..1e0e154f80d --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/ReplyHeader.java @@ -0,0 +1,156 @@ +/* ReplyHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; + + +/** + * The header of the standard reply. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public abstract class ReplyHeader +{ + /** + * Reply status, if no exception occured. + */ + public static final int NO_EXCEPTION = 0; + + /** + * Reply status, user exception. + */ + public static final int USER_EXCEPTION = 1; + + /** + * Reply status, system exception. + */ + public static final int SYSTEM_EXCEPTION = 2; + + /** + * Reply status, if the client ORB must re - send + * the request to another destination. The body + * contains IOR. + */ + public static final int LOCATION_FORWARD = 3; + + /** + * Reply status, indicating that the target has permanently changed the + * address to the supplied IOR. + */ + public static final int LOCATION_FORWARD_PERM = 4; + + /** + * Reply status, indicating, that the ORB requires to resend the object + * address in the required addressing mode, contained as the reply body. + */ + public static final int NEEDS_ADDRESSING_MODE = 5; + + /** + * Empty array, indicating that no service context is available. + */ + protected static final ServiceContext[] NO_CONTEXT = new ServiceContext[ 0 ]; + + /** + * The ORB service data. + */ + public ServiceContext[] service_context = NO_CONTEXT; + + /** + * The status of this reply, holds one of the reply status constants. + */ + public int reply_status; + + /** + * The Id of request into response of which this reply has been sent. + */ + public int request_id; + + /** + * Return the message status as a string. + */ + public String getStatusString() + { + switch (reply_status) + { + case NO_EXCEPTION : + return "ok"; + + case USER_EXCEPTION : + return "user exception"; + + case SYSTEM_EXCEPTION : + return "system exception"; + + case LOCATION_FORWARD : + return "moved"; + + default : + return null; + } + } + + /** + * Reads the header from the stream. + * + * @param in a stream to read from. + */ + public abstract void read(cdrInput in); + + /** + * Returns a short string representation. + * + * @return a string representation. + */ + public String toString() + { + String status = getStatusString(); + if (status == null) + status = "status " + reply_status; + return request_id + ", " + status; + } + + /** + * Writes the header to the stream. + * + * @param out a stream to write into. + */ + public abstract void write(cdrOutput out); +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/RequestHeader.java b/libjava/classpath/gnu/CORBA/GIOP/RequestHeader.java new file mode 100644 index 00000000000..f2de4e2715b --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/RequestHeader.java @@ -0,0 +1,161 @@ +/* RequestHeader.java -- The GIOP 1.0 request message. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; + + +import org.omg.CORBA.portable.IDLEntity; + +/** + * The GIOP request message. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public abstract class RequestHeader + implements IDLEntity +{ + /** + * The currently free request id. This field is incremented + * each time the new request header is constructed. To facilitate + * error detection, the first free id is equal to 0x01234567 + * (19088743). + */ + private static int freeId = 0x01234567; + + /** + * The operation being invoked (IDL scope name). + */ + public String operation; + + /** + * Identifies the object that is the target of the invocation. + */ + public byte[] object_key; + + /** + * A value identifying the requesting principal. + * Initialised into a single zero byte. + * + * @deprecated by CORBA 2.2. + */ + public byte[] requesting_principal; + + /** + * Contains the ORB service data being passed. Initialised as the + * zero size array by default. + */ + public ServiceContext[] service_context = new ServiceContext[ 0 ]; + + /** + * This is used to associate the reply message with the + * previous request message. Initialised each time by the + * different value, increasing form 1 to Integer.MAX_VALUE. + */ + public int request_id = getNextId(); + + /** + * If true, the response from the server is expected. + */ + protected boolean response_expected = true; + + /** + * Get next free request id. The value of the free request + * id starts from 0x02345678, it is incremented each time this + * function is called and is reset to 1 after reaching + * Integer.MAX_VALUE. + * + * @return the next free request id. + */ + public static synchronized int getNextId() + { + int f = freeId; + if (freeId == Integer.MAX_VALUE) + freeId = 1; + else + freeId++; + + return f; + } + + /** + * Set if the sender expects any response to this message. + */ + public abstract void setResponseExpected(boolean expected); + + /** + * Return true if response is expected. + */ + public abstract boolean isResponseExpected(); + + /** + * Converts an byte array into hexadecimal string values. + * Used in various toString() methods. + */ + public String bytes(byte[] array) + { + StringBuffer b = new StringBuffer(); + for (int i = 0; i < array.length; i++) + { + b.append(Integer.toHexString(array [ i ] & 0xFF)); + b.append(" "); + } + return b.toString(); + } + + /** + * Reads the header from the stream. + * + * @param in a stream to read from. + */ + public abstract void read(cdrInput in); + + /** + * Return a string representation. + */ + public abstract String toString(); + + /** + * Writes the header to the stream. + * + * @param out a stream to write into. + */ + public abstract void write(cdrOutput out); +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/ServiceContext.java b/libjava/classpath/gnu/CORBA/GIOP/ServiceContext.java new file mode 100644 index 00000000000..7e44bdcea77 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/ServiceContext.java @@ -0,0 +1,131 @@ +/* ServiceContext.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; + + +import org.omg.CORBA.portable.IDLEntity; + +/** + * Contains the ORB service data being passed. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ServiceContext + implements IDLEntity +{ + /** + * The context data. + */ + public byte[] context_data; + + /** + * The context id. + */ + public int context_id; + + /** + * Read the context values from the stream. + * + * @param istream a stream to read from. + */ + public static ServiceContext read(cdrInput istream) + { + int id = istream.read_ulong(); + + switch (id) + { + case cxCodeSet.ID : + + cxCodeSet codeset = new cxCodeSet(); + codeset.readContext(istream); + return codeset; + + default : + + ServiceContext ctx = new ServiceContext(); + ctx.context_id = id; + ctx.context_data = istream.read_sequence(); + return ctx; + } + } + + /** + * Read a sequence of contexts from the input stream. + */ + public static ServiceContext[] readSequence(cdrInput istream) + { + int size = istream.read_long(); + ServiceContext[] value = new gnu.CORBA.GIOP.ServiceContext[ size ]; + for (int i = 0; i < value.length; i++) + value [ i ] = read(istream); + return value; + } + + /** + * Write the context values into the stream. + * + * @param ostream a stream to write the data to. + */ + public void write(cdrOutput ostream) + { + ostream.write_ulong(context_id); + ostream.write_sequence(context_data); + } + + /** + * Write the sequence of contexts into the input stream. + */ + public static void writeSequence(cdrOutput ostream, ServiceContext[] value) + { + ostream.write_long(value.length); + for (int i = 0; i < value.length; i++) + value [ i ].write(ostream); + } + + /** + * Return a string representation. + */ + public String toString() + { + return "ctx "+context_id+", size "+context_data.length; + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/cxCodeSet.java b/libjava/classpath/gnu/CORBA/GIOP/cxCodeSet.java new file mode 100644 index 00000000000..7f42c07f236 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/cxCodeSet.java @@ -0,0 +1,224 @@ +/* CodeSet_sctx.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; +import gnu.CORBA.IOR; +import gnu.CORBA.IOR.CodeSets_profile; + +import java.io.IOException; + +/** + * The code set service context. This context must be included in all + * messages that use wide characters. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class cxCodeSet + extends ServiceContext +{ + /** + * The context code sets id. + */ + public static final int ID = 1; + + /** + * The standard component to include in the messages. + */ + public static final cxCodeSet STANDARD = new cxCodeSet(); + + /** + * The encoding, used to transfer the narrow (1 byte) character data. + * The default value is taken from {@link CharSets_OSF#NATIVE_CHARACTER}. + */ + public int char_data = CharSets_OSF.NATIVE_CHARACTER; + + /** + * The encoding, used to transfer the wide character data. + * The default value is taken from + * {@link CharSets_OSF#NATIVE_WIDE_CHARACTER}. + */ + public int wide_char_data = CharSets_OSF.NATIVE_WIDE_CHARACTER; + + /** + * Find and return the code set service context in the give + * contexts array. Returns {@link #STANDARD} if no code set + * context is present. + * + * @param contexts the array of contexts, can be null. + */ + public static cxCodeSet find(ServiceContext[] contexts) + { + if (contexts != null) + for (int i = 0; i < contexts.length; i++) + { + if (contexts [ i ] instanceof cxCodeSet) + return (cxCodeSet) contexts [ i ]; + } + return STANDARD; + } + + /** + * Select the suitable encoding that is defined in the provided profile. + * + * TODO character encoding. Now the encoding can be set, but it is ignored. + * If you take this task, scan 'TODO character encoding' for + * relevant places. + */ + public static cxCodeSet negotiate(IOR.CodeSets_profile profile) + { + if (profile.negotiated != null) + return profile.negotiated; + + cxCodeSet use = new cxCodeSet(); + + use.char_data = + negotiate(profile.narrow, STANDARD.char_data, CharSets_OSF.ISO8859_1); + + use.wide_char_data = + negotiate(profile.wide, STANDARD.wide_char_data, CharSets_OSF.UTF16); + + profile.negotiated = use; + + return use; + } + + /** + * Read the context from the given stream. Does not read the + * code sets id. + */ + public void readContext(cdrInput input) + { + cdrInput encap = input.read_encapsulation(); + + char_data = encap.read_ulong(); + wide_char_data = encap.read_ulong(); + } + + /** + * Return a string representation. + */ + public String toString() + { + return " Encoding: narrow " + name(char_data) + ", wide " + + name(wide_char_data) + ". "; + } + + /** + * Write the context to the given stream, including the code + * sets id. + */ + public void write(cdrOutput output) + { + output.write_ulong(ID); + + cdrOutput enout = output.createEncapsulation(); + + enout.write_long(char_data); + enout.write_ulong(wide_char_data); + + try + { + enout.close(); + } + catch (IOException ex) + { + InternalError t = new InternalError(); + t.initCause(ex); + throw t; + } + } + + /** + * Negotiate about the character encoding. Prefer our native encoding, + * if no, prefer IORs native encoding, if no, find any encoding, + * supported by both sides, if no, return the specified final decission. + * + * @param profile the component profile in IOR. + * @param our_native our native encoding + * @param final_decission the encoding that must be returned if no + * compromise is found. + * + * @return the resulted encoding. + */ + protected static int negotiate(IOR.CodeSets_profile.CodeSet_component profile, + int our_native, int final_decission + ) + { + // If our and IORs native sets match, use the native set. + if (profile.native_set == our_native) + return our_native; + + // If the native sets do not match, but the IOR says it + // supports our native set, use our native set. + if (profile.conversion != null) + for (int i = 0; i < profile.conversion.length; i++) + { + if (our_native == profile.conversion [ i ]) + return our_native; + } + + // At this point, we suggest to use the IORs native set. + int[] allSupported = CharSets_OSF.getSupportedCharSets(); + + for (int s = 0; s < allSupported.length; s++) + if (allSupported [ s ] == profile.native_set) + return profile.native_set; + + // Any compromise left? + if (profile.conversion != null) + for (int s = 0; s < allSupported.length; s++) + for (int i = 0; i < profile.conversion.length; i++) + if (allSupported [ s ] == profile.conversion [ i ]) + return allSupported [ s ]; + + // Return the CORBA default char encoding. + return final_decission; + } + + /** + * Conveniency method, used in toString() + */ + private String name(int set) + { + return "0x" + Integer.toHexString(set) + " (" + CharSets_OSF.getName(set) + + ") "; + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/v1_0/CancelHeader.java b/libjava/classpath/gnu/CORBA/GIOP/v1_0/CancelHeader.java new file mode 100644 index 00000000000..6e3650c4ba9 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/v1_0/CancelHeader.java @@ -0,0 +1,72 @@ +/* CancelHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP.v1_0; + +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; + +/** + * The message header for cancelling the request. + * + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class CancelHeader + extends gnu.CORBA.GIOP.CancelHeader +{ + /** + * Write the header. + * + * @param out a stream to write to. + */ + public void read(InputStream input) + { + request_id = input.read_ulong(); + } + + /** + * Write the header. + * + * @param out a stream to write to. + */ + public void write(OutputStream output) + { + output.write_ulong(request_id); + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/v1_0/ReplyHeader.java b/libjava/classpath/gnu/CORBA/GIOP/v1_0/ReplyHeader.java new file mode 100644 index 00000000000..27181ca6347 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/v1_0/ReplyHeader.java @@ -0,0 +1,139 @@ +/* ReplyHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP.v1_0; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; +import gnu.CORBA.GIOP.ServiceContext; +import gnu.CORBA.GIOP.cxCodeSet; + +/** + * The header of the standard reply. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ReplyHeader + extends gnu.CORBA.GIOP.ReplyHeader +{ + /** + * Return the message status as a string. + */ + public String getStatusString() + { + switch (reply_status) + { + case NO_EXCEPTION : + return "ok"; + + case USER_EXCEPTION : + return "user exception"; + + case SYSTEM_EXCEPTION : + return "system exception"; + + case LOCATION_FORWARD : + return "moved"; + + default : + return null; + } + } + + /** + * Get the string representation of all included contexts. + */ + public String contexts() + { + StringBuffer b = new StringBuffer(); + for (int i = 0; i < service_context.length; i++) + { + b.append(service_context [ i ].toString()); + b.append(' '); + } + return b.toString(); + } + + /** + * Reads the header from the stream. + * + * Sets the code set of this stream to + * the code set, specfied in the header. + * + * @param in a stream to read from. + */ + + public void read(cdrInput in) + { + service_context = ServiceContext.readSequence(in); + request_id = in.read_ulong(); + reply_status = in.read_ulong(); + + in.setCodeSet(cxCodeSet.find(service_context)); + } + + /** + * Returns a short string representation. + * + * @return a string representation. + */ + public String toString() + { + String status = getStatusString(); + if (status == null) + status = "status " + reply_status; + return request_id + ", " + status + " " + contexts(); + } + + /** + * Writes the header to the stream. + * + * Sets the code set of this stream to + * the code set, specfied in the header. + * + * @param out a stream to write into. + */ + public void write(cdrOutput out) + { + ServiceContext.writeSequence(out, service_context); + out.write_ulong(request_id); + out.write_ulong(reply_status); + + out.setCodeSet(cxCodeSet.find(service_context)); + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/v1_0/RequestHeader.java b/libjava/classpath/gnu/CORBA/GIOP/v1_0/RequestHeader.java new file mode 100644 index 00000000000..ffa45c37bc0 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/v1_0/RequestHeader.java @@ -0,0 +1,157 @@ +/* RequestHeader.java -- The GIOP 1.0 request message. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP.v1_0; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; + +import org.omg.CORBA.portable.IDLEntity; +import gnu.CORBA.GIOP.ServiceContext; +import gnu.CORBA.GIOP.cxCodeSet; + +/** + * The GIOP 1.0 request message. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class RequestHeader + extends gnu.CORBA.GIOP.RequestHeader + implements IDLEntity +{ + /** + * Creates an empty request header, setting requesting principal + * to byte[] { 'P' }. + */ + public RequestHeader() + { + requesting_principal = new byte[] { 'P' }; + } + + /** + * Set if the sender expects any response to this message. + */ + public void setResponseExpected(boolean expected) + { + response_expected = expected; + } + + /** + * Return true if response is expected. + */ + public boolean isResponseExpected() + { + return response_expected; + } + + public String bytes(byte[] array) + { + StringBuffer b = new StringBuffer(); + for (int i = 0; i < array.length; i++) + { + b.append(Integer.toHexString(array [ i ] & 0xFF)); + b.append(" "); + } + return b.toString(); + } + + /** + * Get the string representation of all included contexts. + */ + public String contexts() + { + StringBuffer b = new StringBuffer(); + for (int i = 0; i < service_context.length; i++) + { + b.append(service_context [ i ].toString()); + b.append(' '); + } + return b.toString(); + } + + /** + * Reads the header from the stream. + * + * Sets the code set of this stream to + * the code set, specfied in the header. + * + * @param in a stream to read from. + */ + public void read(cdrInput in) + { + service_context = ServiceContext.readSequence(in); + request_id = in.read_ulong(); + response_expected = in.read_boolean(); + object_key = in.read_sequence(); + operation = in.read_string(); + requesting_principal = in.read_sequence(); + + in.setCodeSet(cxCodeSet.find(service_context)); + } + + /** + * Return a string representation. + */ + public String toString() + { + return "Request " + request_id + ", call '" + operation + "' on " + + bytes(object_key) + ", " + + (response_expected ? "wait response" : "one way") + ", from " + + bytes(requesting_principal) + contexts(); + } + + /** + * Writes the header to the stream. + * + * Sets the code set of this stream to + * the code set, specfied in the header. + * + * @param out a stream to write into. + */ + public void write(cdrOutput out) + { + ServiceContext.writeSequence(out, service_context); + out.write_ulong(request_id); + out.write_boolean(response_expected); + out.write_sequence(object_key); + out.write_string(operation); + out.write_sequence(requesting_principal); + + out.setCodeSet(cxCodeSet.find(service_context)); + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/v1_2/ReplyHeader.java b/libjava/classpath/gnu/CORBA/GIOP/v1_2/ReplyHeader.java new file mode 100644 index 00000000000..c3f51a37b91 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/v1_2/ReplyHeader.java @@ -0,0 +1,118 @@ +/* ReplyHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP.v1_2; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; +import gnu.CORBA.GIOP.ServiceContext; +import gnu.CORBA.GIOP.cxCodeSet; + +/** + * GIOP 1.2 reply header. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ReplyHeader + extends gnu.CORBA.GIOP.v1_0.ReplyHeader +{ + /** + * Adds the standard encoding context. + */ + public ReplyHeader() + { + service_context = new ServiceContext[] { cxCodeSet.STANDARD }; + } + + /** + * Return the message status as a string. + */ + public String getStatusString() + { + String s = super.getStatusString(); + if (s != null) + return s; + switch (reply_status) + { + case LOCATION_FORWARD_PERM : + return "moved permanently"; + + case NEEDS_ADDRESSING_MODE : + return "the alternative addressing mode required"; + + default : + return null; + } + } + + /** + * Reads the header from the stream. + * The fields go in different order than in the previous GIOP versions. + * + * Sets the code set of this stream to + * the code set, specfied in the header. + * + * @param in a stream to read from. + */ + public void read(cdrInput in) + { + request_id = in.read_ulong(); + reply_status = in.read_ulong(); + service_context = gnu.CORBA.GIOP.ServiceContext.readSequence(in); + + in.setCodeSet(cxCodeSet.find(service_context)); + } + + /** + * Writes the header to the stream. + * The fields go in different order than in the previous GIOP versions. + * + * Sets the code set of this stream to + * the code set, specfied in the header. + * + * @param out a stream to write into. + */ + public void write(cdrOutput out) + { + out.write_ulong(request_id); + out.write_ulong(reply_status); + gnu.CORBA.GIOP.ServiceContext.writeSequence(out, service_context); + + out.setCodeSet(cxCodeSet.find(service_context)); + } +} diff --git a/libjava/classpath/gnu/CORBA/GIOP/v1_2/RequestHeader.java b/libjava/classpath/gnu/CORBA/GIOP/v1_2/RequestHeader.java new file mode 100644 index 00000000000..d294d006723 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/GIOP/v1_2/RequestHeader.java @@ -0,0 +1,213 @@ +/* RequestHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.GIOP.v1_2; + +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; +import gnu.CORBA.GIOP.ServiceContext; +import gnu.CORBA.GIOP.cxCodeSet; + +import java.io.IOException; + +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NO_IMPLEMENT; + +/** + * The GIOP 1.2 request header. The GIOP 1.1 request header + * is the same as GIOP 1.0 request header, if taking the + * alignment into consideration. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class RequestHeader + extends gnu.CORBA.GIOP.v1_0.RequestHeader +{ + /** + * Indicates that the object is addressed by the object key. + */ + public static final short KeyAddr = 0; + + /** + * Indicates that the object is addressed by the IOP tagged profile. + */ + public static final short ProfileAddr = 1; + + /** + * Indicates that the objec is addressed by IOR addressing info. + */ + public static final short ReferenceAddr = 2; + + /** + * The response flags of the header. By default, the flags are initialised + * by value 0x3 (response expected). + */ + public byte response_flags = 3; + + /** + * The used addressing method. + */ + public short AddressingDisposition; + + /** + * Adds the standard encoding context. + */ + public RequestHeader() + { + service_context = new ServiceContext[] { cxCodeSet.STANDARD }; + } + + /** + * Set if the sender expects any response to this message. + * Clears or sets the 2 lower bits of flags + * (0 - not expected, 0x3 - expected). + */ + public void setResponseExpected(boolean expected) + { + response_expected = expected; + + if (expected) + response_flags = (byte) (response_flags | 0x3); + else + response_flags = (byte) (response_flags & (~0x3)); + } + + /** + * Return true if response is expected. + * + * @return true if the two lowest bits of the flags are set or + * the response expected is explicitly set to true. + */ + public boolean isResponseExpected() + { + return response_expected || ((response_flags & 0x3) == 0x3); + } + + /** + * Read the header from the given stream. + * + * @param in a stream to read from. + */ + public void read(cdrInput in) + { + try + { + request_id = in.read_ulong(); + response_flags = (byte) in.read(); + + // Skip 3 reserved octets: + in.skip(3); + + // Read target address. + AddressingDisposition = in.read_ushort(); + + switch (AddressingDisposition) + { + case KeyAddr : + object_key = in.read_sequence(); + break; + + // TODO FIXME add other addressing methods. + case ProfileAddr : + throw new NO_IMPLEMENT("Object addressing by IOP tagged profile"); + + case ReferenceAddr : + throw new NO_IMPLEMENT("Object addressing by by IOR addressing info"); + + default : + throw new MARSHAL("Unknow addressing method in request, " + + AddressingDisposition + ); + } + + operation = in.read_string(); + service_context = gnu.CORBA.GIOP.ServiceContext.readSequence(in); + + // No requesting principal in this new format. + in.setCodeSet(cxCodeSet.find(service_context)); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } + + /** + * Return a string representation. + */ + public String toString() + { + return "Request " + request_id + ", call '" + operation + "' on " + + bytes(object_key) + ", " + + (response_expected ? "wait response" : "one way") + + " addressed by " + " method " + AddressingDisposition + "." + + contexts(); + } + + /** + * Write the header to the given stream. + * + * @param out a stream to write into. + */ + public void write(cdrOutput out) + { + out.write_ulong(request_id); + + out.write(response_flags); + + // Skip 3 reserved octets: + out.write(0); + out.write(0); + out.write(0); + + // Write addressing disposition from IOR. + // TODO FIXME add other addressing methods. + out.write_ushort(KeyAddr); + + out.write_sequence(object_key); + + out.write_string(operation); + + ServiceContext.writeSequence(out, service_context); + + // No requesting principal in this new format. + out.setCodeSet(cxCodeSet.find(service_context)); + } +} diff --git a/libjava/classpath/gnu/CORBA/IOR.java b/libjava/classpath/gnu/CORBA/IOR.java new file mode 100644 index 00000000000..cedbce46175 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/IOR.java @@ -0,0 +1,563 @@ +/* IOR.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.CDR.cdrBufOutput; +import gnu.CORBA.CDR.cdrInput; +import gnu.CORBA.CDR.cdrOutput; +import gnu.CORBA.GIOP.CharSets_OSF; +import gnu.CORBA.GIOP.cxCodeSet; + +import org.omg.CORBA.BAD_PARAM; +import org.omg.CORBA.CompletionStatus; +import org.omg.CORBA.ULongSeqHelper; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * The implementaton of the Interoperable Object Reference (IOR). + * IOR can be compared with the Internet address for a web page, + * it provides means to locate the CORBA service on the web. + * IOR contains the host address, port number, the object identifier + * (key) inside the server, the communication protocol version, + * supported charsets and so on. + * + * Ths class provides method for encoding and + * decoding the IOR information from/to the stringified references, + * usually returned by {@link org.omg.CORBA.ORB#String object_to_string()}. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + * + * @see org.mog.CORBA.Object.object_to_string(Object forObject) + * @see string_to_object(String IOR) + */ +public class IOR +{ + /** + * The code sets profile. + */ + public static class CodeSets_profile + { + /** + * The code set component. + */ + public static class CodeSet_component + { + /** + * The conversion code sets. + */ + public int[] conversion; + + /** + * The native code set. + */ + public int native_set; + + /** + * Read from the CDR stream. + */ + public void read(org.omg.CORBA.portable.InputStream in) + { + native_set = in.read_ulong(); + conversion = ULongSeqHelper.read(in); + } + + /** + * Get a string representation. + */ + public String toString() + { + StringBuffer b = new StringBuffer(); + b.append("native " + name(native_set)); + if (conversion != null && conversion.length > 0) + { + b.append(" conversion "); + for (int i = 0; i < conversion.length; i++) + { + b.append(name(conversion [ i ])); + b.append(' '); + } + } + b.append(' '); + return b.toString(); + } + + /** + * Write into CDR stream. + */ + public void write(org.omg.CORBA.portable.OutputStream out) + { + out.write_long(native_set); + ULongSeqHelper.write(out, conversion); + } + + private String name(int set) + { + return "0x" + Integer.toHexString(set) + " (" + + CharSets_OSF.getName(set) + ") "; + } + } + + /** + * The agreed tag for the Codesets profile. + */ + public static final int TAG_CODE_SETS = 1; + + /** + * Information about narrow character encoding (TCS-C). + */ + public CodeSet_component narrow = new CodeSet_component(); + + /** + * About wide character encoding (TCS-W). + */ + public CodeSet_component wide = new CodeSet_component(); + + /** + * The negotiated coding result for this IOR. Saves time, requred for + * negotiation computations. + */ + public cxCodeSet negotiated; + + /** + * Read the code set profile information from the given input stream. + * + * @param profile a stream to read from. + */ + public void read(cdrInput profile) + { + cdrBufInput encapsulation = profile.read_encapsulation(); + narrow.read(encapsulation); + wide.read(encapsulation); + } + + /** + * Returns a string representation. + */ + public String toString() + { + return "Narrow char: " + narrow + ", Wide char: " + wide; + } + + /** + * Write the code set profile information into the given input stream. + * + * @param profile a stream to write into. + */ + public void write(cdrOutput profile) + { + cdrOutput encapsulation = profile.createEncapsulation(); + narrow.write(encapsulation); + wide.write(encapsulation); + try + { + encapsulation.close(); + } + catch (IOException ex) + { + throw new InternalError(); + } + } + } + + /** + * The internet profile. + */ + public static class Internet_profile + { + /** + * The agreed tag for the Internet profile. + */ + public static final int TAG_INTERNET_IOP = 0; + + /** + * The host. + */ + public String host; + + /** + * The IIOP version (initialised to 1.2 by default). + */ + public Version version = new Version(1, 2); + + /** + * The port. + */ + public int port; + + /** + * Return the human readable representation. + */ + public String toString() + { + StringBuffer b = new StringBuffer(); + b.append(host); + b.append(":"); + b.append(port); + b.append(" (v"); + b.append(version); + b.append(")"); + return b.toString(); + } + } + + /** + * The standard minor code, indicating that the string to object + * converstio has failed due non specific reasons. + */ + public static final int FAILED = 10; + + /** + * The code sets profile of this IOR. + */ + public CodeSets_profile CodeSets = new CodeSets_profile(); + + /** + * The internet profile of this IOR. + */ + public Internet_profile Internet = new Internet_profile(); + + /** + * The object repository Id. + */ + public String Id; + + /** + * The additional tagged components, encapsulated in + * the byte arrays. They are only supported by the + * later versions, than currently implemented. + */ + public byte[][] extra; + + /** + * The object key. + */ + public byte[] key; + + /** + * True if the profile was encoded using the Big Endian or + * the encoding is not known. + * + * false if it was encoded using the Little Endian. + */ + public boolean Big_Endian = true; + + /** + * Create an empty instance, initialising the code sets to default + * values. + */ + public IOR() + { + int[] supported = CharSets_OSF.getSupportedCharSets(); + + CodeSets.narrow.native_set = CharSets_OSF.NATIVE_CHARACTER; + CodeSets.narrow.conversion = supported; + + CodeSets.wide.native_set = CharSets_OSF.NATIVE_WIDE_CHARACTER; + CodeSets.wide.conversion = supported; + } + + /** + * Parse the provided stringifed reference. + * + * @param stringified_reference, in the form of + * IOR:nnnnnn..... + * + * @return the parsed IOR + * + * @throws BAD_PARAM, minor code 10, if the IOR cannot be parsed. + * + * TODO corballoc and other alternative formats. + */ + public static IOR parse(String stringified_reference) + throws BAD_PARAM + { + try + { + if (!stringified_reference.startsWith("IOR:")) + throw new BAD_PARAM("The string refernce must start with IOR:", + FAILED, CompletionStatus.COMPLETED_NO + ); + + IOR r = new IOR(); + + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + String x = stringified_reference; + x = x.substring(x.indexOf(":") + 1); + + char cx; + + for (int i = 0; i < x.length(); i = i + 2) + { + cx = (char) Integer.parseInt(x.substring(i, i + 2), 16); + buf.write(cx); + } + + cdrBufInput cdr = new cdrBufInput(buf.toByteArray()); + + r._read(cdr); + return r; + } + catch (Exception ex) + { + ex.printStackTrace(); + throw new BAD_PARAM(ex + " while parsing " + stringified_reference, + FAILED, CompletionStatus.COMPLETED_NO + ); + } + } + + /** + * Read the IOR from the provided input stream. + * + * @param c a stream to read from. + * @throws IOException if the stream throws it. + */ + public void _read(cdrInput c) + throws IOException, BAD_PARAM + { + int endian; + + endian = c.read_long(); + if (endian != 0) + { + Big_Endian = false; + c.setBigEndian(false); + } + _read_no_endian(c); + } + + /** + * Read the IOR from the provided input stream, not reading + * the endian data at the beginning of the stream. The IOR is + * thansferred in this form in + * {@link write_Object(org.omg.CORBA.Object)}. + * + * If the stream contains a null value, the Id and Internet fields become + * equal to null. Otherwise Id contains some string (possibly + * empty). + * + * Id is checked for null in cdrInput that then returns + * null instead of object. + * + * @param c a stream to read from. + * @throws IOException if the stream throws it. + */ + public void _read_no_endian(cdrInput c) + throws IOException, BAD_PARAM + { + Id = c.read_string(); + + int n_profiles = c.read_long(); + + if (n_profiles == 0) + { + Id = null; + Internet = null; + return; + } + + for (int i = 0; i < n_profiles; i++) + { + int tag = c.read_long(); + cdrBufInput profile = c.read_encapsulation(); + + if (tag == Internet_profile.TAG_INTERNET_IOP) + { + Internet = new Internet_profile(); + Internet.version = Version.read_version(profile); + Internet.host = profile.read_string(); + Internet.port = profile.gnu_read_ushort(); + + int lk = profile.read_long(); + key = new byte[ lk ]; + profile.read(key); + + // Read tagged components. + int n_components = 0; + + try + { + if (Internet.version.since_inclusive(1, 1)) + n_components = profile.read_long(); + + for (int t = 0; t < n_components; t++) + { + int ctag = profile.read_long(); + + if (ctag == CodeSets_profile.TAG_CODE_SETS) + { + CodeSets.read(profile); + } + } + } + catch (Unexpected ex) + { + ex.printStackTrace(); + } + } + } + } + + /** + * Write this IOR record to the provided CDR stream. + * This procedure writes the zero (Big Endian) marker first. + */ + public void _write(cdrOutput out) + { + // Always use Big Endian. + out.write(0); + _write_no_endian(out); + } + + /** + * Write a null value to the CDR output stream. + * + * The null value is written as defined in OMG specification + * (zero length string, followed by an empty set of profiles). + */ + public static void write_null(cdrOutput out) + { + // Empty Id string. + out.write_string(""); + + // Empty set of profiles. + out.write_long(0); + } + + /** + * Write this IOR record to the provided CDR stream. The procedure + * writed data in Big Endian, but does NOT add any endian marker + * to the beginning. + */ + public void _write_no_endian(cdrOutput out) + { + try + { + // Write repository id. + out.write_string(Id); + + // Always one profile. + out.write_long(1); + + // It is the Internet profile. + out.write_long(Internet_profile.TAG_INTERNET_IOP); + + // Need to write the Internet profile into the separate + // stream as we must know the size in advance. + cdrOutput b = out.createEncapsulation(); + + Internet.version.write(b); + b.write_string(Internet.host); + + b.write_ushort((short) (Internet.port & 0xFFFF)); + + // Write the object key. + b.write_long(key.length); + b.write(key); + + // One tagged component. + b.write_long(1); + + b.write_long(CodeSets_profile.TAG_CODE_SETS); + CodeSets.write(b); + + b.close(); + } + catch (IOException ex) + { + Unexpected.error(ex); + } + } + + /** + * Returns a human readable string representation of this IOR object. + */ + public String toString() + { + StringBuffer b = new StringBuffer(); + b.append(Id); + b.append(" at "); + b.append(Internet); + + if (!Big_Endian) + b.append(" (Little endian) "); + + b.append(" Key "); + + for (int i = 0; i < key.length; i++) + { + b.append(Integer.toHexString(key [ i ] & 0xFF)); + } + + b.append(" "); + b.append(CodeSets); + + return b.toString(); + } + + /** + * Returs a stringified reference. + * + * @return a newly constructed stringified reference. + */ + public String toStringifiedReference() + { + cdrBufOutput out = new cdrBufOutput(); + + _write(out); + + StringBuffer b = new StringBuffer("IOR:"); + + byte[] binary = out.buffer.toByteArray(); + String s; + + for (int i = 0; i < binary.length; i++) + { + s = Integer.toHexString(binary [ i ] & 0xFF); + if (s.length() == 1) + b.append('0'); + b.append(s); + } + + return b.toString(); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/IOR_Delegate.java b/libjava/classpath/gnu/CORBA/IOR_Delegate.java new file mode 100644 index 00000000000..b06f6300d95 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/IOR_Delegate.java @@ -0,0 +1,311 @@ +/* gnuDelegate.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.GIOP.ReplyHeader; + +import org.omg.CORBA.Context; +import org.omg.CORBA.ContextList; +import org.omg.CORBA.ExceptionList; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NVList; +import org.omg.CORBA.NamedValue; +import org.omg.CORBA.ORB; +import org.omg.CORBA.Request; +import org.omg.CORBA.portable.ApplicationException; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.RemarshalException; + +import java.io.IOException; + +import java.net.Socket; + +/** + * The Classpath implementation of the {@link Delegate} functionality in the + * case, when the object was constructed from an IOR object. The IOR can be + * constructed from the stringified object reference. + * + * There is an different instance of this delegate for each CORBA object. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class IOR_Delegate + extends Simple_delegate +{ + /** + * Contructs an instance of object using the given IOR. + */ + public IOR_Delegate(ORB an_orb, IOR an_ior) + { + super(an_orb, an_ior); + } + + /** + * Creates the request to invoke the method on this object. + * + * @param target the object, for that the operation must be invoked. + * @param context context (null allowed) + * @param operation the method name + * @param parameters the method parameters + * @param returns the return value holder + * @param exceptions the exceptions that can be thrown by the method + * @param ctx_list the context list (null allowed) + * + * @return the created request. + */ + public Request create_request(org.omg.CORBA.Object target, Context context, + String operation, NVList parameters, + NamedValue returns + ) + { + gnuRequest request = new gnuRequest(); + + request.setIor(getIor()); + request.set_target(target); + + request.setOperation(operation); + request.set_args(parameters); + request.m_context = context; + request.set_result(returns); + request.setORB(orb); + + return request; + } + + /** + * Creates the request to invoke the method on this object. + * + * @param target the object, for that the operation must be invoked. + * @param context context (null allowed) + * @param operation the method name + * @param parameters the method parameters + * @param returns the return value holder + * + * @return the created request. + */ + public Request create_request(org.omg.CORBA.Object target, Context context, + String operation, NVList parameters, + NamedValue returns, ExceptionList exceptions, + ContextList ctx_list + ) + { + gnuRequest request = new gnuRequest(); + + request.setIor(ior); + request.set_target(target); + + request.setOperation(operation); + request.set_args(parameters); + request.m_context = context; + request.set_result(returns); + request.set_exceptions(exceptions); + request.set_context_list(ctx_list); + request.setORB(orb); + + return request; + } + + /** + * Invoke operation on the given object, writing parameters to the given + * output stream. + * + * @param target the target object. + * @param output the output stream, previously returned by + * {@link #request(org.omg.CORBA.Object, String, boolean)}. + * + * @return the input stream, to read the response from or null for a + * one-way request. + * + * @throws SystemException if the SystemException has been thrown on the + * remote side (the exact type and the minor code matches the data of + * the remote exception that has been thrown). + * + * @throws org.omg.CORBA.portable.ApplicationException as specified. + * @throws org.omg.CORBA.portable.RemarshalException as specified. + */ + public InputStream invoke(org.omg.CORBA.Object target, OutputStream output) + throws ApplicationException, RemarshalException + { + streamRequest request = (streamRequest) output; + if (request.response_expected) + { + binaryReply response = request.request.submit(); + + // Read reply header. + ReplyHeader rh = response.header.create_reply_header(); + cdrBufInput input = response.getStream(); + input.setOrb(orb); + rh.read(input); + + boolean moved_permanently = false; + + switch (rh.reply_status) + { + case ReplyHeader.NO_EXCEPTION : + if (response.header.version.since_inclusive(1, 2)) + input.align(8); + return input; + + case ReplyHeader.SYSTEM_EXCEPTION : + if (response.header.version.since_inclusive(1, 2)) + input.align(8); + throw ObjectCreator.readSystemException(input); + + case ReplyHeader.USER_EXCEPTION : + if (response.header.version.since_inclusive(1, 2)) + input.align(8); + input.mark(2000); + + String uxId = input.read_string(); + input.reset(); + + throw new ApplicationException(uxId, input); + + case ReplyHeader.LOCATION_FORWARD_PERM : + moved_permanently = true; + + case ReplyHeader.LOCATION_FORWARD : + if (response.header.version.since_inclusive(1, 2)) + input.align(8); + + IOR forwarded = new IOR(); + try + { + forwarded._read_no_endian(input); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL("Cant read forwarding info"); + t.initCause(ex); + throw t; + } + + request.request.setIor(forwarded); + + // If the object has moved permanently, its IOR is replaced. + if (moved_permanently) + setIor(forwarded); + + return invoke(target, request); + + default : + throw new MARSHAL("Unknow reply status: " + rh.reply_status); + } + } + else + { + request.request.send_oneway(); + return null; + } + } + + /** + * Create a request to invoke the method of this CORBA object. + * + * @param target the CORBA object, to that this operation must be applied. + * @param operation the name of the method to invoke. + * + * @return the request. + */ + public Request request(org.omg.CORBA.Object target, String operation) + { + gnuRequest request = new gnuRequest(); + + request.setIor(ior); + request.set_target(target); + + request.setOperation(operation); + request.setORB(orb); + + return request; + } + + /** + * Create a request to invoke the method of this CORBA object. + * + * @param target the CORBA object, to that this operation must be applied. + * @param operation the name of the method to invoke. + * @param response_expected specifies if this is one way message or the + * response to the message is expected. + * + * @return the stream where the method arguments should be written. + */ + public OutputStream request(org.omg.CORBA.Object target, String operation, + boolean response_expected + ) + { + gnuRequest request = new gnuRequest(); + + request.setIor(ior); + request.set_target(target); + request.setOperation(operation); + + request.getParameterStream().response_expected = response_expected; + request.setORB(orb); + + return request.getParameterStream(); + } + + /** + * If there is an opened cache socket to access this object, close + * that socket. + * + * @param target The target is not used, this delegate requires a + * single instance per object. + */ + public void release(org.omg.CORBA.Object target) + { + String key = ior.Internet.host + ":" + ior.Internet.port; + + Socket socket = SocketRepository.get_socket(key); + try + { + if (socket != null) + { + socket.close(); + } + } + catch (IOException ex) + { + // do nothing, then. + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/IOR_contructed_object.java b/libjava/classpath/gnu/CORBA/IOR_contructed_object.java new file mode 100644 index 00000000000..2fab70799db --- /dev/null +++ b/libjava/classpath/gnu/CORBA/IOR_contructed_object.java @@ -0,0 +1,109 @@ +/* IOR_contructed_object.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.ORB; +import org.omg.CORBA.portable.ObjectImpl; + +/** + * Implements an object, constructed from an IOR reference. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class IOR_contructed_object + extends ObjectImpl +{ + /** + * The IOR, from which the object was constructed. + */ + protected final IOR ior; + + /** + * The object id, as defined in IOR. + */ + protected final String[] id; + + /** + * Create the object from the given IOR. + * + * @param an_ior the IOR. + */ + public IOR_contructed_object(ORB orb, IOR an_ior) + { + ior = an_ior; + _set_delegate(new IOR_Delegate(orb, ior)); + id = new String[] { ior.Id }; + } + + /** + * Create the object from the given string IOR representation. + * + * @param an_ior the IOR in the string form. + */ + public IOR_contructed_object(Functional_ORB orb, String an_ior) + { + ior = IOR.parse(an_ior); + _set_delegate(new IOR_Delegate(orb, ior)); + id = new String[] { ior.Id }; + } + + public String[] _ids() + { + return id; + } + + /** + * Get a string reference for this object. + * + * @return the class name:IOR profile + */ + public String toString() + { + return getClass().getName() + ":IOR:" + ior; + } + + /** + * Calls realease on the delegate. + */ + protected void finalize() + throws java.lang.Throwable + { + _get_delegate().release(this); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/NamingService/Binding_iterator_impl.java b/libjava/classpath/gnu/CORBA/NamingService/Binding_iterator_impl.java new file mode 100644 index 00000000000..79d787083ca --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/Binding_iterator_impl.java @@ -0,0 +1,139 @@ +/* Binding_iterator.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import org.omg.CosNaming.Binding; +import org.omg.CosNaming.BindingHolder; +import org.omg.CosNaming.BindingListHolder; +import org.omg.CosNaming.BindingType; +import org.omg.CosNaming.NameComponent; +import org.omg.CosNaming._BindingIteratorImplBase; + +/** + * The implementation of the {@link BindingIterator}. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Binding_iterator_impl + extends _BindingIteratorImplBase +{ + /** + * The value, returned by the {@link #next_one} when there + * are no bindings available. + */ + private static final Binding no_more_bindings = + new Binding(new NameComponent[ 0 ], BindingType.nobject); + + /** + * The collection of the available bindings. + */ + private final Binding[] bindings; + + /** + * The position of the internal iterator pointer. + */ + private int p; + + public Binding_iterator_impl(Binding[] a_bindings) + { + bindings = a_bindings; + } + + /** + * Disconnect the iterator from its ORB. The iterator will + * no longer be accessible and will be a subject of the + * garbage collection. + */ + public void destroy() + { + _orb().disconnect(this); + } + + /** + * Return the desired amount of bindings. + * + * @param amount the maximal number of bindings to return. + * @param a_list a holder to store the returned bindings. + * + * @return false if there are no more bindings available, + * true otherwise. + */ + public boolean next_n(int amount, BindingListHolder a_list) + { + if (p < bindings.length) + { + int n = bindings.length - p; + if (n > amount) + n = amount; + + a_list.value = new Binding[ n ]; + for (int i = 0; i < n; i++) + a_list.value [ i ] = bindings [ p++ ]; + + return true; + } + else + { + a_list.value = new Binding[ 0 ]; + return false; + } + } + + /** + * Return the next binding. + * + * @param a_binding a holder, where the next binding will be stored. + * + * @return false if there are no more bindings available, true + * otherwise. + */ + public boolean next_one(BindingHolder a_binding) + { + if (p < bindings.length) + { + a_binding.value = (Binding) bindings [ p++ ]; + return true; + } + else + { + a_binding.value = no_more_bindings; + return false; + } + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/Ext.java b/libjava/classpath/gnu/CORBA/NamingService/Ext.java new file mode 100644 index 00000000000..fb7406c4618 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/Ext.java @@ -0,0 +1,230 @@ +/* TransientContextExt.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.Object; +import org.omg.CORBA.portable.Delegate; +import org.omg.CORBA.portable.ObjectImpl; +import org.omg.CosNaming.BindingIteratorHolder; +import org.omg.CosNaming.BindingListHolder; +import org.omg.CosNaming.NameComponent; +import org.omg.CosNaming.NamingContext; +import org.omg.CosNaming.NamingContextPackage.AlreadyBound; +import org.omg.CosNaming.NamingContextPackage.CannotProceed; +import org.omg.CosNaming.NamingContextPackage.InvalidName; +import org.omg.CosNaming.NamingContextPackage.NotEmpty; +import org.omg.CosNaming.NamingContextPackage.NotFound; +import org.omg.CosNaming._NamingContextExtImplBase; + +/** + * This naming context that adds the the string based extensions, + * defined by {@link NamingContextExt}. The basic functionality + * is handled by the enclosed instance of the {@link NamingContext}. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Ext + extends _NamingContextExtImplBase +{ + /** + * The older version of the naming context, where all relevant calls + * are forwarded. + */ + private final NamingContext classic; + + /** + * The converter class converts between string and array form of the + * name. + */ + private snConverter converter = new snConverter(); + + /** + * Create the extensions for the given instance of the context. + * + * @param previous_version the previous version of the naming context. + */ + public Ext(NamingContext previous_version) + { + classic = previous_version; + } + + /** + * Sets a delegate to this context and, if appropriated, also + * sets the same delegate to the enclosing 'classic' context. + * + * @param a_delegate a delegate to set. + */ + public void _set_delegate(Delegate a_delegate) + { + super._set_delegate(a_delegate); + if (classic instanceof ObjectImpl) + ((ObjectImpl) classic)._set_delegate(a_delegate); + } + + /** {@inheritDoc} */ + public void bind(NameComponent[] a_name, Object an_object) + throws NotFound, CannotProceed, InvalidName, AlreadyBound + { + classic.bind(a_name, an_object); + } + + /** {@inheritDoc} */ + public void bind_context(NameComponent[] a_name, NamingContext context) + throws NotFound, CannotProceed, InvalidName, AlreadyBound + { + classic.bind_context(a_name, context); + } + + /** {@inheritDoc} */ + public NamingContext bind_new_context(NameComponent[] a_name) + throws NotFound, AlreadyBound, CannotProceed, + InvalidName + { + return classic.bind_new_context(a_name); + } + + /** {@inheritDoc} */ + public void destroy() + throws NotEmpty + { + classic.destroy(); + } + + /** {@inheritDoc} */ + public void list(int amount, BindingListHolder a_list, + BindingIteratorHolder an_iter + ) + { + classic.list(amount, a_list, an_iter); + } + + /** {@inheritDoc} */ + public NamingContext new_context() + { + return classic.new_context(); + } + + /** {@inheritDoc} */ + public void rebind(NameComponent[] a_name, Object an_object) + throws NotFound, CannotProceed, InvalidName + { + classic.rebind(a_name, an_object); + } + + /** {@inheritDoc} */ + public void rebind_context(NameComponent[] a_name, NamingContext context) + throws NotFound, CannotProceed, InvalidName + { + classic.rebind_context(a_name, context); + } + + /** {@inheritDoc} */ + public Object resolve(NameComponent[] a_name) + throws NotFound, CannotProceed, InvalidName + { + return classic.resolve(a_name); + } + + /** + * Resolves the name, represented in the form of the string. The name + * is first parsed into an array representation, then the call + * is forwarded to the {@link resolve(NameComponent[])}. + * + * @param a_name_string a name to resolve. + * + * @return the resolved object. + * + * @throws NotFound if the name cannot be resolved. + * @throws InvalidName if the name is invalid. + * @throws CannotProceed on unexpected circumstances. + */ + public Object resolve_str(String a_name_string) + throws NotFound, CannotProceed, InvalidName + { + return resolve(to_name(a_name_string)); + } + + /** + * Convert the name string representation into array representation. + * + * @param a_name_string a string to convert. + * @return a converted array of the name components + * + * @throws InvalidName on parsing error. + */ + public NameComponent[] to_name(String a_name_string) + throws InvalidName + { + return converter.toName(a_name_string); + } + + /** + * Convert a name component array representation into string representation. + * + * @param a_name a name to convert. + * + * @return a string form. + * + * @throws InvalidName if the passed name is invalid. + */ + public String to_string(NameComponent[] a_name) + throws InvalidName + { + return converter.toString(a_name); + } + + /** + * This method is not yet implemented. + * FIXME TODO implement it. + */ + public String to_url(String an_address, String a_name_string) + throws org.omg.CosNaming.NamingContextExtPackage.InvalidAddress, + InvalidName + { + throw new NO_IMPLEMENT("Method to_url() not yet implemented."); + } + + /** {@inheritDoc} */ + public void unbind(NameComponent[] a_name) + throws NotFound, CannotProceed, InvalidName + { + classic.unbind(a_name); + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/NameValidator.java b/libjava/classpath/gnu/CORBA/NamingService/NameValidator.java new file mode 100644 index 00000000000..d7d5a14bb4b --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/NameValidator.java @@ -0,0 +1,79 @@ +/* NameValidator.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + + +import org.omg.CosNaming.NameComponent; +import org.omg.CosNaming.NamingContextPackage.InvalidName; + +/** + * Checks the given name for validity. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class NameValidator +{ + /** + * Check the given name. This method must be package level, as it is + * not defined in the API. + * + * @param name the name to check. + * + * @throws InvalidName if the given name is not valid. + */ + public static void check(NameComponent[] name) + throws InvalidName + { + if (name == null) + throw new InvalidName("name=null"); + + if (name.length == 0) + throw new InvalidName("name.length=0"); + + for (int i = 0; i < name.length; i++) + { + if (name [ i ] == null) + throw new InvalidName("name[" + i + "]=null"); + if (name [ i ].id == null) + throw new InvalidName("name[" + i + "].id=null"); + if (name [ i ].kind == null) + throw new InvalidName("name[" + i + "].kind=null"); + } + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/NamingMap.java b/libjava/classpath/gnu/CORBA/NamingService/NamingMap.java new file mode 100644 index 00000000000..a69b0617269 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/NamingMap.java @@ -0,0 +1,187 @@ +/* NamingMap.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import org.omg.CosNaming.NameComponent; +import org.omg.CosNaming.NamingContextPackage.AlreadyBound; +import org.omg.CosNaming.NamingContextPackage.InvalidName; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +/** + * The Naming Map maps the single names components into associated objects or + * naming contexts. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class NamingMap +{ + /** + * The actual map. + */ + private final TreeMap map; + + /** + * Creates an instance of the naming map, intialising the comparator + * to the {@link cmpNameComparator}. + */ + public NamingMap() + { + map = new TreeMap(cmpNameComponent.singleton); + } + + /** + * Put the given CORBA object, specifying the given name as a key. + * If the entry with the given name already exists, or if the given + * object is already mapped under another name, the + * {@link AlreadyBound} exception will be thrown. + * + * @param name the name + * @param object the object + */ + public void bind(NameComponent name, org.omg.CORBA.Object object) + throws AlreadyBound, InvalidName + { + if (containsKey(name)) + { + Object x = get(name); + + // Do not throw an exception if the same object is named by + // the same name. + if (x.equals(object)) + throw new AlreadyBound("The name is in use for another object"); + } + else + { + if (containsValue(object)) + throw new AlreadyBound("Tha object has another name"); + } + } + + /** + * Checks if this map contains the definition of the given name. + * + * @param key the name to check. + */ + public boolean containsKey(NameComponent key) + { + return map.containsKey(key); + } + + /** + * Checks if this map contains the definition of the given object. + * + * @param object the object to check. + */ + public boolean containsValue(org.omg.CORBA.Object object) + { + return map.containsValue(object); + } + + /** + * Returns the map entry set. + * + * @return the map entry set, containing the instances of the + * Map.Entry. + */ + public Set entries() + { + return map.entrySet(); + } + + /** + * Get the CORBA object, associated with the given name. + * + * @param name the name. + * + * @return the associated object, null if none. + */ + public org.omg.CORBA.Object get(NameComponent name) + { + return (org.omg.CORBA.Object) map.get(name); + } + + /** + * Put the given CORBA object, specifying the given name as a key. + * Remove all pre - existing mappings for the given name and object. + * + * @param name the name. + * @param object + */ + public void rebind(NameComponent name, org.omg.CORBA.Object object) + throws InvalidName + { + // Remove the existing mapping for the given name, if present. + remove(name); + + Iterator iter = entries().iterator(); + Map.Entry item; + + // Remove the existing mapping for the given object, if present. + while (iter.hasNext()) + { + item = (Map.Entry) iter.next(); + if (item.getValue().equals(object)) + iter.remove(); + } + + map.put(name, object); + } + + /** + * Removes the given name, if present. + * + * @param name a name to remove. + */ + public void remove(NameComponent name) + { + map.remove(name); + } + + /** + * Get the size of the map. + */ + public int size() + { + return map.size(); + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/NamingServiceTransient.java b/libjava/classpath/gnu/CORBA/NamingService/NamingServiceTransient.java new file mode 100644 index 00000000000..fda46adbb00 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/NamingServiceTransient.java @@ -0,0 +1,160 @@ +/* Server.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import gnu.CORBA.Functional_ORB; + +import org.omg.CosNaming.NamingContextExt; + +import java.io.FileOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; + +/** + * The server for the gnu classpath naming service. This is an executable + * class that must be started to launch the GNU Classpath CORBA + * transient naming service. + * + * GNU Classpath currently works with this naming service and is also + * interoperable with the Sun Microsystems naming services from + * releases 1.3 and 1.4, both transient <i>tnameserv</i> and persistent + * <i>orbd</i>. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class NamingServiceTransient +{ + /** + * The default port (900), on that the naming service starts if no + * -ORBInitialPort is specified in the command line. + */ + public static final int PORT = 900; + + /** + * Get the object key for the naming service. The default + * key is the string "NameService" in ASCII. + * + * @return the byte array. + */ + public static byte[] getDefaultKey() + { + try + { // NameService + return "NameService".getBytes("UTF-8"); + } + catch (UnsupportedEncodingException ex) + { + throw new InternalError("UTF-8 unsupported"); + } + } + + /** + * Start the naming service on the current host at the given port. + * The parameter -org.omg.CORBA.ORBInitialPort NNN or + * -ORBInitialPort NNN, if present, specifies the port, on that + * the service must be started. If this key is not specified, + * the service starts at the port 900. + * + * The parameter -ior FILE_NAME, if present, forces to store the ior string + * of this naming service to the specified file. + * + * @param args the parameter string. + */ + public static void main(String[] args) + { + int port = PORT; + String iorf = null; + try + { + // Create and initialize the ORB + final Functional_ORB orb = new Functional_ORB(); + + if (args.length > 1) + for (int i = 0; i < args.length - 1; i++) + { + if (args [ i ].endsWith("ORBInitialPort")) + port = Integer.parseInt(args [ i + 1 ]); + + if (args [ i ].equals("-ior")) + iorf = args [ i + 1 ]; + } + + Functional_ORB.setPort(port); + + // Create the servant and register it with the ORB + NamingContextExt namer = new Ext(new TransientContext()); + orb.connect(namer, getDefaultKey()); + + // Storing the IOR reference. + String ior = orb.object_to_string(namer); + if (iorf != null) + { + FileOutputStream f = new FileOutputStream(iorf); + PrintStream p = new PrintStream(f); + p.print(ior); + p.close(); + } + + System.out.println("GNU Classpath, transient naming service. " + + "Copyright (C) 2005 Free Software Foundation\n" + + "This tool comes with ABSOLUTELY NO WARRANTY. " + + "This is free software, and you are\nwelcome to " + + "redistribute it under conditions, defined in " + + "GNU Classpath license.\n\n" + ior + ); + + new Thread() + { + public void run() + { + // Wait for invocations from clients. + orb.run(); + } + }.start(); + } + catch (Exception e) + { + System.err.println("ERROR: " + e); + e.printStackTrace(System.out); + } + + // Restore the default value for allocating ports for the subsequent objects. + Functional_ORB.setPort(Functional_ORB.DEFAULT_INITIAL_PORT); + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/TransientContext.java b/libjava/classpath/gnu/CORBA/NamingService/TransientContext.java new file mode 100644 index 00000000000..4b7c1938fde --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/TransientContext.java @@ -0,0 +1,416 @@ +/* nContext.java -- implementation of NamingContext + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import org.omg.CORBA.Object; +import org.omg.CosNaming.Binding; +import org.omg.CosNaming.BindingIteratorHolder; +import org.omg.CosNaming.BindingListHolder; +import org.omg.CosNaming.BindingType; +import org.omg.CosNaming.NameComponent; +import org.omg.CosNaming.NamingContext; +import org.omg.CosNaming.NamingContextOperations; +import org.omg.CosNaming.NamingContextPackage.AlreadyBound; +import org.omg.CosNaming.NamingContextPackage.CannotProceed; +import org.omg.CosNaming.NamingContextPackage.InvalidName; +import org.omg.CosNaming.NamingContextPackage.NotEmpty; +import org.omg.CosNaming.NamingContextPackage.NotFound; +import org.omg.CosNaming.NamingContextPackage.NotFoundReason; +import org.omg.CosNaming._NamingContextImplBase; + +import java.util.Iterator; +import java.util.Map; + +/** + * This class implements the transient naming service, defined by + * {@link NamingContex}. The 'transient' means that the service does + * not store its state into the persistent memory. If the service is + * restarted, the named objects must be re-registered again. + * + * TODO Write the persistent naming service. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class TransientContext + extends _NamingContextImplBase + implements NamingContext, NamingContextOperations +{ + /** + * The already named contexts. + */ + protected final NamingMap named_contexts = new NamingMap(); + + /** + * The already named objects. + */ + protected final NamingMap named_objects = new NamingMap(); + + /** + * Gives the object a name, valid in this context. + * + * @param a_name the name, being given to the object. + * @param an_object the object, being named. + * + * @throws AlreadyBound if the object is already named in this context. + * @throws InvalidName if the name has zero length or otherwise invalid. + */ + public void bind(NameComponent[] a_name, Object an_object) + throws NotFound, CannotProceed, InvalidName, AlreadyBound + { + if (a_name.length == 1) + named_objects.bind(a_name [ 0 ], an_object); + else + { + NamingContext context = + (NamingContext) named_contexts.get(a_name [ 0 ]); + context.bind(getSuffix(a_name), an_object); + } + } + + /** + * Gives a child context name, valid in this context. + * + * @param a_name the name, being given to the child context. + * @param a_context the child context being named. + * + * @throws AlreadyBound if the child context is already named in + * the current context. + */ + public void bind_context(NameComponent[] a_name, NamingContext a_context) + throws NotFound, CannotProceed, InvalidName, AlreadyBound + { + if (a_name.length == 1) + named_contexts.bind(a_name [ 0 ], a_context); + else + { + NamingContext context = + (NamingContext) named_contexts.get(a_name [ 0 ]); + context.bind_context(getSuffix(a_name), a_context); + } + } + + /** + * Create a new context and give it a given name (bound it) + * in the current context. + * + * The context being created is returned by calling + * {@link #new_context()}. + * + * @param a_name the name being given to the new context. + * + * @return the newly created context. + * + * @throws AlreadyBound if the name is already in use. + * @throws InvalidName if the name has zero length or otherwise invalid. + */ + public NamingContext bind_new_context(NameComponent[] a_name) + throws NotFound, AlreadyBound, CannotProceed, + InvalidName + { + if (named_contexts.containsKey(a_name [ 0 ]) || + named_objects.containsKey(a_name [ 0 ]) + ) + throw new AlreadyBound(); + + NamingContext child = new_context(); + bind_context(a_name, child); + return child; + } + + /** + * Destroy this context (must be empty). + * @throws NotEmpty if the context being destroyed is not empty. + */ + public void destroy() + throws NotEmpty + { + if (named_contexts.size() > 0 || named_objects.size() > 0) + throw new NotEmpty(); + } + + /** + * Iterate over all bindings, defined in this namind context. + * + * @param amount the maximal number of context to return in the + * holder a_list. The remaining bindings are accessible via iterator + * an_iter. If the parameter amount is zero, all bindings are accessed only + * via this iterator. + * + * This implementation list contexts first, then objects. + * + * @param a_list the holder, where the returned bindigs are stored. + * @param an_iter the iterator that can be used to access the remaining + * bindings. + */ + public void list(int amount, BindingListHolder a_list, + BindingIteratorHolder an_iter + ) + { + int nb = named_contexts.size() + named_objects.size(); + int nl = nb; + if (nl > amount) + nl = amount; + + a_list.value = new Binding[ nl ]; + + Iterator contexts = named_contexts.entries().iterator(); + Iterator objects = named_objects.entries().iterator(); + + // Create a binding list. + for (int i = 0; i < nl; i++) + { + if (contexts.hasNext()) + a_list.value [ i ] = mkBinding(contexts.next(), BindingType.ncontext); + else if (objects.hasNext()) + a_list.value [ i ] = mkBinding(objects.next(), BindingType.nobject); + else + throw new InternalError(); + } + + // Create an iterator. + Binding[] remainder = new Binding[ nb - nl ]; + int p = 0; + + while (contexts.hasNext()) + remainder [ p++ ] = mkBinding(contexts.next(), BindingType.ncontext); + + while (objects.hasNext()) + remainder [ p++ ] = mkBinding(objects.next(), BindingType.nobject); + + Binding_iterator_impl bit = new Binding_iterator_impl(remainder); + _orb().connect(bit); + an_iter.value = bit; + } + + /** + * Creates a new naming context, not bound to any name. + */ + public NamingContext new_context() + { + Ext context = new Ext(new TransientContext()); + + // Connect the context to the current ORB: + _orb().connect(context); + return context; + } + + /** + * Names or renames the object. + * + * @param a_name the new name, being given to the object + * in the scope of the current context. If the object is already + * named in this context, it is renamed. + * + * @param an_object the object, being named. + * + * @throws InvalidName if the name has zero length or otherwise invalid. + */ + public void rebind(NameComponent[] a_name, Object an_object) + throws NotFound, CannotProceed, InvalidName + { + if (a_name.length == 1) + named_objects.rebind(a_name [ 0 ], an_object); + else + { + NamingContext context = + (NamingContext) named_contexts.get(a_name [ 0 ]); + context.rebind(getSuffix(a_name), an_object); + } + } + + /** + * Names or renames the child context. + * If the child context is already named in + * the current context, it is renamed. The the name being given is in + * use, the old meaning of the name is discarded. + * + * @param a_name the name, being given to the child context in the scope + * of the current context. + * + * @param a_context the child context being named. + * + * @throws InvalidName if the name has zero length or otherwise invalid. + */ + public void rebind_context(NameComponent[] a_name, NamingContext a_context) + throws NotFound, CannotProceed, InvalidName + { + if (a_name.length == 1) + named_contexts.rebind(a_name [ 0 ], a_context); + else + { + NamingContext context = + (NamingContext) named_contexts.get(a_name [ 0 ]); + context.rebind_context(getSuffix(a_name), a_context); + } + } + + /** + * Get the object, bound to the specified name in this + * context. The given object must match the bound + * name. + * + * This implementation resolves the names as defined in specification + * of the CORBA naming service. This means, if the beginning of the + * name can be resolved to some naming context, the request is + * forwarded to this context, passing the unresolved name part as a + * parameter. In this way, it is possible to have a hierarchy of the + * naming services. The central services resolve the the beginning + * of the name. The local services resolve the remaining nodes of the + * name that may be relevant to some local details. It can be three or + * more ranks of the naming services. + * + * @param a_name the object name. + * + * @return the object, matching this name. The client + * usually casts or narrows (using the helper) the returned value + * to the more specific type. + * + * @throws NotFound if the name cannot be resolved. + * @throws InvalidName if the name has zero length or otherwise invalid. + */ + public Object resolve(NameComponent[] a_name) + throws NotFound, CannotProceed, InvalidName + { + NameValidator.check(a_name); + + if (a_name.length > 1) + return resolveSubContext(a_name); + else + { + // A single node name. + org.omg.CORBA.Object object; + + object = named_objects.get(a_name [ 0 ]); + if (object != null) + return object; + + object = named_contexts.get(a_name [ 0 ]); + if (object != null) + return object; + } + + throw new NotFound(NotFoundReason.missing_node, a_name); + } + + /** + * Removes the name from the binding context. + * + * @param a_name a name to remove. + * + * @throws InvalidName if the name has zero length or otherwise invalid. + */ + public void unbind(NameComponent[] a_name) + throws NotFound, CannotProceed, InvalidName + { + NameValidator.check(a_name); + + // Single node name - handle it. + if (a_name.length == 1) + { + if (named_objects.containsKey(a_name [ 0 ])) + named_objects.remove(a_name [ 0 ]); + else if (named_contexts.containsKey(a_name [ 0 ])) + named_contexts.remove(a_name [ 0 ]); + else + throw new NotFound(NotFoundReason.missing_node, a_name); + } + else + { + // Handle the first node and forward the command. + NamingContext subcontext = + (NamingContext) named_contexts.get(a_name [ 0 ]); + + if (subcontext == null) + throw new NotFound(NotFoundReason.missing_node, a_name); + + subcontext.unbind(getSuffix(a_name)); + } + } + + /** + * Get the name suffix, discarding the first member. + */ + private NameComponent[] getSuffix(NameComponent[] a_name) + { + NameComponent[] suffix = new NameComponent[ a_name.length - 1 ]; + System.arraycopy(a_name, 1, suffix, 0, suffix.length); + return suffix; + } + + /** + * Create a binding. + * + * @param entry the entry, defining the bound object. + * @param type the binding type. + * @return the created binding. + */ + private Binding mkBinding(java.lang.Object an_entry, BindingType type) + { + Map.Entry entry = (Map.Entry) an_entry; + Binding b = new Binding(); + + // The name component has always only one node (the current context) + b.binding_name = new NameComponent[] { (NameComponent) entry.getKey() }; + b.binding_type = type; + return b; + } + + /** + * Find the context, bound to the first name of the given + * name, and pass the remainder (without the first node) + * of the name for that context to resolve. + * + * @param name the name to resolve. + * + * @return the resolved context + */ + private Object resolveSubContext(NameComponent[] a_name) + throws NotFound, CannotProceed, InvalidName + { + // A multiple node name. + // This context resolves the first node only. + NamingContext context = (NamingContext) named_contexts.get(a_name [ 0 ]); + if (context == null) + throw new NotFound(NotFoundReason.missing_node, a_name); + + NameComponent[] suffix = getSuffix(a_name); + + return context.resolve(suffix); + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/cmpNameComponent.java b/libjava/classpath/gnu/CORBA/NamingService/cmpNameComponent.java new file mode 100644 index 00000000000..1e06fb8ee35 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/cmpNameComponent.java @@ -0,0 +1,98 @@ +/* cmpNameComponent.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import org.omg.CORBA.BAD_PARAM; +import org.omg.CosNaming.NameComponent; + +import java.util.Comparator; + +/** + * This class implements the name component comparator, needed to + * sort and compare the name components in maps and sorted sets. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public final class cmpNameComponent + implements Comparator +{ + /** + * The singleton instance of the name comparator. + */ + public static final cmpNameComponent singleton = new cmpNameComponent(); + + /** + * It is enough to have a singleton. + */ + private cmpNameComponent() + { + } + + /** + * Compare the two names components. + * + * @param nc_a the first name component. + * @param nc_b the second name component. + * + * @return 0 if the name components are equal, non zero value + * as result of comparison otherwise. + * + * @throws BAD_PARAM if one of the components is empty or + * has {@link NameComponent#id} or {@link NameComponent#kind} + * field intialised to null. + */ + public final int compare(Object nc_a, Object nc_b) + { + NameComponent a = (NameComponent) nc_a; + NameComponent b = (NameComponent) nc_b; + + int cn = a.id.compareTo(b.id); + if (cn != 0) + return cn; + return a.kind.compareTo(b.kind); + } + + /** + * All instances of this class are equal. + */ + public boolean equals(Object x) + { + return x instanceof cmpNameComponent; + } +} diff --git a/libjava/classpath/gnu/CORBA/NamingService/snConverter.java b/libjava/classpath/gnu/CORBA/NamingService/snConverter.java new file mode 100644 index 00000000000..a9b9219d76a --- /dev/null +++ b/libjava/classpath/gnu/CORBA/NamingService/snConverter.java @@ -0,0 +1,328 @@ +/* snConverter.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.NamingService; + +import org.omg.CORBA.IntHolder; +import org.omg.CosNaming.NameComponent; +import org.omg.CosNaming.NamingContextPackage.InvalidName; + +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * This class converts between string and array representations of the + * multi component object names. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class snConverter +{ + /** + * A string, indicating the escape character. + */ + public static final String ESCAPE = "\\"; + + /** + * Convert the string name representation into the array name + * representation. See {@link #toString(NameComponent)} for the + * description of this format. + * + * @param name the string form of the name. + * + * @return the array form of the name. + * + * @throws InvalidName if the name cannot be parsed. + */ + public NameComponent[] toName(String a_name) + throws InvalidName + { + ArrayList components = new ArrayList(); + StringTokenizer st = new StringTokenizer(a_name, "./\\", true); + + String id; + String kind; + String next; + + // Create the buffer array, reserving the last element for null. + String[] n = new String[ st.countTokens() + 1 ]; + + int pp = 0; + while (st.hasMoreTokens()) + n [ pp++ ] = st.nextToken(); + + IntHolder p = new IntHolder(); + + NameComponent node = readNode(p, n); + + while (node != null) + { + components.add(node); + node = readNode(p, n); + } + + NameComponent[] name = new NameComponent[ components.size() ]; + for (int i = 0; i < name.length; i++) + { + name [ i ] = (NameComponent) components.get(i); + } + + NameValidator.check(name); + + return name; + } + + /** + * Converts the name into its string representation, as defined in + * the specification CORBA naming service. + * + * A string representation for the name consists of the name components, + * separated by a slash '/' character (for example, 'a/b/c'). If the + * {@link NameComponent#kind} field is not empty, it is given after + * period ('.'), for example 'a.b/c.d/.' . + * The period alone represents node where part where both + * {@link NameComponent#kind} and {@link NameComponent#id} are empty strings. + * + * If slash or dot are part of the name, they are escaped by backslash ('\'). + * If the backslash itself is part of the name, it is doubled. + * + * @param a_name a name to convert. + * @return a string representation. + */ + public String toString(NameComponent[] a_name) + throws InvalidName + { + NameValidator.check(a_name); + + StringBuffer b = new StringBuffer(); + + NameComponent n; + + for (int ni = 0; ni < a_name.length; ni++) + { + n = a_name [ ni ]; + appEscaping(b, n.id); + if (n.kind.length() > 0) + { + b.append('.'); + appEscaping(b, n.kind); + } + + if (ni < a_name.length - 1) + b.append('/'); + } + return b.toString(); + } + + /** + * Append the contents of the string to this + * string buffer, inserting the escape sequences, where required. + * + * @param b a buffer to append the contents to. + * @param s a string to append. + */ + private void appEscaping(StringBuffer b, String s) + { + char c; + for (int i = 0; i < s.length(); i++) + { + c = s.charAt(i); + switch (c) + { + case '.' : + case '/' : + case '\\' : + b.append('\\'); + b.append(c); + break; + + default : + b.append(c); + break; + } + } + } + + /** + * Assert the end of the current name component. + */ + private void assertEndOfNode(IntHolder p, String[] t) + throws InvalidName + { + if (t [ p.value ] != null) + if (!t [ p.value ].equals("/")) + throw new InvalidName("End of node expected at token " + p.value); + } + + /** + * Read the named component node. After reading the current positon + * advances to the beginning of the next node in an array. + * + * @param p the current position being wrapped inside the passed + * IntHolder. + * + * @param t the text buffer. + * + * @return the created node. + */ + private NameComponent readNode(IntHolder p, String[] t) + throws InvalidName + { + // End of stream has been reached. + if (t [ p.value ] == null) + return null; + + NameComponent n = new NameComponent(); + + if (t [ p.value ].equals(".")) + { + // The 'id' is missing, but the 'kind' may follow. + n.id = ""; + p.value++; + n.kind = readPart(p, t); + assertEndOfNode(p, t); + if (t [ p.value ] != null) + p.value++; + } + else if (t [ p.value ].equals("/")) + { + // This is not allowed here and may happen only + // on two subsequent slashes. + throw new InvalidName("Unexpected '/' token " + p.value); + } + else + { + n.id = readPart(p, t); + + // If some chars follow the id. + if (t [ p.value ] != null) + { + // Dot means that the kind part follows + if (t [ p.value ].equals(".")) + { + p.value++; + n.kind = readPart(p, t); + assertEndOfNode(p, t); + if (t [ p.value ] != null) + p.value++; + } + + // The next name component follows - advance to + // the beginning of the next name component. + else if (t [ p.value ].equals("/")) + { + n.kind = ""; + p.value++; + } + else + throw new InvalidName("Unexpected '" + t [ p.value ] + + "' at token " + p.value + ); + } + else + + // Id, and then end of sequence. + n.kind = ""; + } + + return n; + } + + /** + * Read the name part (id or kind). + * + * @param p the current position. After reading, advances + * to the beginning of the next name fragment. + * + * @param t the string buffer. + * + * @return the name part with resolved escape sequences. + */ + private String readPart(IntHolder p, String[] t) + { + StringBuffer part = new StringBuffer(); + + while (t [ p.value ] != null && !t [ p.value ].equals(".") && + !t [ p.value ].equals("/") + ) + { + if (t [ p.value ].equals(ESCAPE)) + { + p.value++; + part.append(t [ p.value ]); + } + else + part.append(t [ p.value ]); + + p.value++; + } + + return part.toString(); + } + + public static void main(String[] args) + { + NameComponent a = new NameComponent("a", "ak"); + NameComponent b = new NameComponent("b/z", "b.k"); + NameComponent c = new NameComponent("c", ""); + + snConverter sn = new snConverter(); + + try + { + String s = sn.toString(new NameComponent[] { a, b, c }); + System.out.println(s); + + //NameComponent[] k = toName("a.k/b.k2/c/d/."); + //NameComponent[] k = toName("a.bc/.b/c.x"); + + NameComponent[] k = sn.toName(s); + System.out.println("ToString"); + + for (int i = 0; i < k.length; i++) + { + System.out.println(k [ i ].id + ":" + k [ i ].kind); + } + } + catch (InvalidName ex) + { + ex.printStackTrace(); + } + } + +} diff --git a/libjava/classpath/gnu/CORBA/ObjectCreator.java b/libjava/classpath/gnu/CORBA/ObjectCreator.java new file mode 100644 index 00000000000..b99c46ba63f --- /dev/null +++ b/libjava/classpath/gnu/CORBA/ObjectCreator.java @@ -0,0 +1,348 @@ +/* ExceptionCreator.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.CompletionStatus; +import org.omg.CORBA.CompletionStatusHelper; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.SystemException; +import org.omg.CORBA.UNKNOWN; +import org.omg.CORBA.UserException; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/** + * Creates java objects from the agreed IDL names for the simple + * case when the CORBA object is directly mapped into the locally + * defined java class. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ObjectCreator +{ + /** + * The standard OMG prefix. + */ + public static final String OMG_PREFIX = "omg.org/"; + + /** + * The standard java prefix. + */ + public static final String JAVA_PREFIX = "org.omg."; + + /** + * The prefix for classes that are placed instide the + * gnu.CORBA namespace. + */ + public static final String CLASSPATH_PREFIX = "gnu.CORBA."; + + /** + * Try to instantiate an object with the given IDL name. + * The object must be mapped to the local java class. + * The omg.org domain must be mapped into the object in either + * org/omg or gnu/CORBA namespace. + * + * @param IDL name + * @return instantiated object instance or null if no such + * available. + */ + public static java.lang.Object createObject(String idl, String suffix) + { + try + { + return Class.forName(toClassName(JAVA_PREFIX, idl) + suffix) + .newInstance(); + } + catch (Exception ex) + { + try + { + return Class.forName(toClassName(CLASSPATH_PREFIX, idl) + suffix) + .newInstance(); + } + catch (Exception exex) + { + return null; + } + } + } + + /** + * Create the system exception with the given idl name. + * + * @param idl the exception IDL name, must match the syntax + * "IDL:<class/name>:1.0". + * @param minor the exception minor code. + * @param completed the exception completion status. + * + * @return the created exception. + */ + public static SystemException createSystemException(String idl, int minor, + CompletionStatus completed + ) + { + try + { + String cl = toClassName(JAVA_PREFIX, idl); + Class exClass = Class.forName(cl); + + Constructor constructor = + exClass.getConstructor(new Class[] + { + String.class, int.class, + CompletionStatus.class + } + ); + + Object exception = + constructor.newInstance(new Object[] + { + " Remote exception " + idl + ", minor " + + minor + ", " + completed + ".", + new Integer(minor), completed + } + ); + + return (SystemException) exception; + } + catch (Exception ex) + { + ex.printStackTrace(); + return new UNKNOWN("Unsupported system exception", minor, completed); + } + } + + /** + * Read the system exception from the given stream. + * @param input the CDR stream to read from. + * @return the exception that has been stored in the stream + * (IDL name, minor code and completion status). + */ + public static SystemException readSystemException(InputStream input) + { + String idl = input.read_string(); + int minor = input.read_ulong(); + CompletionStatus status = CompletionStatusHelper.read(input); + + SystemException exception = + ObjectCreator.createSystemException(idl, minor, status); + + return exception; + } + + /** + * Reads the user exception, having the given Id, from the + * input stream. The id is expected to be in the form like + * 'IDL:test/org/omg/CORBA/ORB/communication/ourUserException:1.0' + * + * @param idl the exception idl name. + * @param input the stream to read from. + * + * @return the loaded exception. + * @return null if the helper class cannot be found. + */ + public static UserException readUserException(String idl, InputStream input) + { + try + { + String helper = toHelperName(idl); + Class helperClass = Class.forName(helper); + + Method read = + helperClass.getMethod("read", + new Class[] + { + org.omg.CORBA.portable.InputStream.class + } + ); + + return (UserException) read.invoke(null, new Object[] { input }); + } + catch (MARSHAL mex) + { + // This one is ok to throw + throw mex; + } + catch (Exception ex) + { + ex.printStackTrace(); + return null; + } + } + + /** + * Gets the helper class name from the string like + * 'IDL:test/org/omg/CORBA/ORB/communication/ourUserException:1.0' + * + * @param IDL the idl name. + */ + public static String toHelperName(String IDL) + { + String s = IDL; + int a = s.indexOf(':') + 1; + int b = s.lastIndexOf(':'); + + s = IDL.substring(a, b); + + if (s.startsWith(OMG_PREFIX)) + s = JAVA_PREFIX + s.substring(OMG_PREFIX.length()); + + return s.replace('/', '.') + "Helper"; + } + + /** + * Writes the system exception data to CDR output stream. + * + * @param output a stream to write data to. + * @param ex an exception to write. + */ + public static void writeSystemException(OutputStream output, + SystemException ex + ) + { + String exIDL = toIDL(ex.getClass().getName()); + output.write_string(exIDL); + output.write_ulong(ex.minor); + CompletionStatusHelper.write(output, ex.completed); + } + + /** + * Converts the given IDL name to class name. + * + * @param IDL the idl name. + * + */ + protected static String toClassName(String prefix, String IDL) + { + String s = IDL; + int a = s.indexOf(':') + 1; + int b = s.lastIndexOf(':'); + + s = IDL.substring(a, b); + + if (s.startsWith(OMG_PREFIX)) + s = prefix + s.substring(OMG_PREFIX.length()); + + return s.replace('/', '.'); + } + + /** + * Converts the given IDL name to class name and tries to load the + * matching class. The OMG prefix (omg.org) is replaced by + * the java prefix org.omg. No other prefixes are added. + * + * @param IDL the idl name. + * + * TODO Cache the returned classes, avoiding these string manipulations + * each time the conversion is required. + * + * @return the matching class or null if no such is available. + */ + public static Class Idl2class(String IDL) + { + String s = IDL; + int a = s.indexOf(':') + 1; + int b = s.lastIndexOf(':'); + + s = IDL.substring(a, b); + + if (s.startsWith(OMG_PREFIX)) + s = JAVA_PREFIX + s.substring(OMG_PREFIX.length()); + + String cn = s.replace('/', '.'); + + try + { + return Class.forName(cn); + } + catch (ClassNotFoundException ex) + { + return null; + } + } + + /** + * Converts the given IDL name to class name, tries to load the + * matching class and create an object instance with parameterless + * constructor. The OMG prefix (omg.org) is replaced by + * the java prefix org.omg. No other prefixes are added. + * + * @param IDL the idl name. + * + * @return instantiated object instance or null if such attempt was not + * successful. + */ + public static java.lang.Object Idl2Object(String IDL) + { + Class cx = Idl2class(IDL); + + try + { + if (cx != null) + return cx.newInstance(); + else + return null; + } + catch (Exception ex) + { + return null; + } + } + + /** + * Convert the class name to IDL name. + * + * @param cn the class name. + * + * @return the idl name. + */ + public static String toIDL(String cn) + { + if (cn.startsWith(JAVA_PREFIX)) + cn = OMG_PREFIX + cn.substring(JAVA_PREFIX.length()).replace('.', '/'); + else if (cn.startsWith(CLASSPATH_PREFIX)) + cn = + OMG_PREFIX + cn.substring(CLASSPATH_PREFIX.length()).replace('.', '/'); + + return "IDL:" + cn + ":1.0"; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/OctetHolder.java b/libjava/classpath/gnu/CORBA/OctetHolder.java new file mode 100644 index 00000000000..02fd9a432ec --- /dev/null +++ b/libjava/classpath/gnu/CORBA/OctetHolder.java @@ -0,0 +1,129 @@ +/* OctetHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.primitiveTypeCode; + +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; + +/** + * A holder for CORBA <code>octet</code> that is mapped into + * java <code>long</code>. + * + * The holders have several application areas. The end user usually + * sees them implementing CORBA methods where the primitive type + * is passed by reference. While CORBA (or, for example, C) supports + * this, the java does not and a wrapper class is required. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public final class OctetHolder + implements Streamable +{ + /** + * The default type code for this holder. + */ + private static final TypeCode t_octet = + new primitiveTypeCode(TCKind.tk_octet); + + /** + * The <code>long</code> (CORBA <code>octet</code>) value, + * held by this OctetHolder. + */ + public byte value; + + /** + * Constructs an instance of OctetHolder, + * initializing {@link #value} to <code>0 </code>. + */ + public OctetHolder() + { + } + + /** + * Constructs an instance of OctetHolder, + * initializing {@link #value} to the given <code>octed</code> (byte). + * + * @param initial_value a value that will be assigned to the + * {@link #value} field. + */ + public OctetHolder(byte initial_value) + { + value = initial_value; + } + + /** + * Fill in the {@link value } field by reading the required data + * from the given stream. For <code>octet</code>, the functionality + * is delegated to + * {@link org.omg.CORBA.portable.InputStream#read_octet}. + * + * @param input the input stream to read from. + */ + public void _read(InputStream input) + { + value = input.read_octet(); + } + + /** + * Returns the TypeCode, corresponding the CORBA type that is stored + * using this holder. + */ + public TypeCode _type() + { + return t_octet; + } + + /** + * Write the {@link value } field to the given stream. + * For <code>octet</code>, the functionality + * is delegated to + * {@link org.omg.CORBA.portable.OutputStream#write_octet(long) }. + * + * @param output the output stream to write into. + */ + public void _write(OutputStream output) + { + output.write_octet(value); + } +} diff --git a/libjava/classpath/gnu/CORBA/Poa/InvalidPolicyHolder.java b/libjava/classpath/gnu/CORBA/Poa/InvalidPolicyHolder.java new file mode 100644 index 00000000000..154762acf00 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Poa/InvalidPolicyHolder.java @@ -0,0 +1,106 @@ +/* InvalidPolicyHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA.Poa; + +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; +import org.omg.PortableServer.POAPackage.InvalidPolicy; +import org.omg.PortableServer.POAPackage.InvalidPolicyHelper; + +/** +* A holder for the exception {@link InvalidPolicy}. + +* @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) +*/ +public class InvalidPolicyHolder + implements Streamable +{ + /** + * The stored InvalidPolicy value. + */ + public InvalidPolicy value; + + /** + * Create the unitialised instance, leaving the value field + * with default <code>null</code> value. + */ + public InvalidPolicyHolder() + { + } + + /** + * Create the initialised instance. + * @param initialValue the value that will be assigned to + * the <code>value</code> field. + */ + public InvalidPolicyHolder(InvalidPolicy initialValue) + { + value = initialValue; + } + + /** + * Fill in the {@link value} by data from the CDR stream. + * + * @param input the org.omg.CORBA.portable stream to read. + */ + public void _read(InputStream input) + { + value = InvalidPolicyHelper.read(input); + } + + /** + * Write the stored value into the CDR stream. + * + * @param output the org.omg.CORBA.portable stream to write. + */ + public void _write(OutputStream output) + { + InvalidPolicyHelper.write(output, value); + } + + /** + * Get the typecode of the InvalidPolicy. + */ + public TypeCode _type() + { + return InvalidPolicyHelper.type(); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/Restricted_ORB.java b/libjava/classpath/gnu/CORBA/Restricted_ORB.java new file mode 100644 index 00000000000..801154e2064 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Restricted_ORB.java @@ -0,0 +1,463 @@ +/* RestrictedORB.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufOutput; + +import org.omg.CORBA.Any; +import org.omg.CORBA.BAD_PARAM; +import org.omg.CORBA.ContextList; +import org.omg.CORBA.Environment; +import org.omg.CORBA.ExceptionList; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.NVList; +import org.omg.CORBA.NamedValue; +import org.omg.CORBA.ORB; +import org.omg.CORBA.ORBPackage.InvalidName; +import org.omg.CORBA.Request; +import org.omg.CORBA.StructMember; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.UnionMember; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.ValueFactory; + +import java.applet.Applet; + +import java.util.Hashtable; +import java.util.Properties; + +/** + * This class implements so-called Singleton ORB, a highly restricted version + * that cannot communicate over network. This ORB is provided + * for the potentially malicious applets with heavy security restrictions. + * It, however, supports some basic features that might be needed even + * when the network access is not granted. + * + * This ORB can only create typecodes, + * {@link Any}, {@link ContextList}, {@link NVList} and + * {@link org.omg.CORBA.portable.OutputStream} that writes to an + * internal buffer. + * + * All other methods throw the {@link NO_IMPLEMENT} exception. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class Restricted_ORB + extends org.omg.CORBA_2_3.ORB +{ + /** + * The singleton instance of this ORB. + */ + public static final ORB Singleton = new Restricted_ORB(); + + /** + * The value factories. + */ + protected Hashtable factories = new Hashtable(); + + /** + * Create a new instance of the RestrictedORB. This is used + * in derived classes only. + */ + protected Restricted_ORB() + { + } + + /** {@inheritDoc} */ + public TypeCode create_alias_tc(String id, String name, TypeCode typecode) + { + return new aliasTypeCode(typecode, id, name); + } + + /** {@inheritDoc} */ + public Any create_any() + { + gnuAny any = new gnuAny(); + any.setOrb(this); + return any; + } + + /** {@inheritDoc} */ + public TypeCode create_array_tc(int length, TypeCode element_type) + { + primitiveArrayTypeCode p = + new primitiveArrayTypeCode(TCKind.tk_array, element_type); + p.setLength(length); + return p; + } + + /** {@inheritDoc} */ + public ContextList create_context_list() + { + return new gnuContextList(); + } + + /** {@inheritDoc} */ + public TypeCode create_enum_tc(String id, String name, String[] values) + { + recordTypeCode r = new recordTypeCode(TCKind.tk_enum); + for (int i = 0; i < values.length; i++) + { + r.field().name = values [ i ]; + } + + r.setId(id); + r.setName(name); + + return r; + } + + /** {@inheritDoc} */ + public Environment create_environment() + { + return new gnuEnvironment(); + } + + /** {@inheritDoc} */ + public ExceptionList create_exception_list() + { + return new gnuExceptionList(); + } + + /** {@inheritDoc} */ + public TypeCode create_exception_tc(String id, String name, + StructMember[] members + ) + { + recordTypeCode r = new recordTypeCode(TCKind.tk_except); + r.setId(id); + r.setName(name); + + for (int i = 0; i < members.length; i++) + { + r.add(members [ i ]); + } + + return r; + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public TypeCode create_interface_tc(String id, String name) + { + no(); + return null; + } + + /** {@inheritDoc} */ + public NVList create_list(int count) + { + return new gnuNVList(count); + } + + /** {@inheritDoc} */ + public NamedValue create_named_value(String s, Any any, int flags) + { + return new gnuNamedValue(); + } + + /** {@inheritDoc} */ + public OutputStream create_output_stream() + { + cdrBufOutput stream = new cdrBufOutput(); + stream.setOrb(this); + return stream; + } + + /** {@inheritDoc} */ + public TypeCode create_sequence_tc(int bound, TypeCode element_type) + { + primitiveArrayTypeCode p = + new primitiveArrayTypeCode(TCKind.tk_sequence, element_type); + p.setLength(bound); + return p; + } + + /** {@inheritDoc} */ + public TypeCode create_string_tc(int bound) + { + stringTypeCode p = new stringTypeCode(TCKind.tk_string); + p.setLength(bound); + return p; + } + + /** {@inheritDoc} */ + public TypeCode create_struct_tc(String id, String name, + StructMember[] members + ) + { + recordTypeCode r = new recordTypeCode(TCKind.tk_struct); + r.setId(id); + r.setName(name); + + for (int i = 0; i < members.length; i++) + { + r.add(members [ i ]); + } + + return r; + } + + /** {@inheritDoc} */ + public TypeCode create_union_tc(String id, String name, TypeCode type, + UnionMember[] members + ) + { + recordTypeCode r = new recordTypeCode(TCKind.tk_union); + r.setId(id); + r.setName(name); + + for (int i = 0; i < members.length; i++) + { + r.add(members [ i ]); + } + + return r; + } + + /** {@inheritDoc} */ + public TypeCode create_wstring_tc(int bound) + { + stringTypeCode p = new stringTypeCode(TCKind.tk_wstring); + p.setLength(bound); + return p; + } + + /** {@inheritDoc} */ + public TypeCode get_primitive_tc(TCKind tcKind) + { + try + { + return typeNamer.getPrimitveTC(tcKind); + } + catch (BadKind ex) + { + throw new BAD_PARAM("This is not a primitive type code: " + + tcKind.value() + ); + } + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public String[] list_initial_services() + { + no(); + throw new InternalError(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public String object_to_string(org.omg.CORBA.Object forObject) + { + no(); + throw new InternalError(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws InvalidName never in this class, but it is thrown + * in the derived classes. + * + * @throws NO_IMPLEMENT, always. + */ + public org.omg.CORBA.Object resolve_initial_references(String name) + throws InvalidName + { + no(); + throw new InternalError(); + } + + /** + * Shutdown the ORB server. + * + * For RestrictedORB, returns witout action. + */ + public void run() + { + } + + /** + * Shutdown the ORB server. + * + * For RestrictedORB, returns witout action. + */ + public void shutdown(boolean wait_for_completion) + { + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public org.omg.CORBA.Object string_to_object(String IOR) + { + no(); + throw new InternalError(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + protected void set_parameters(Applet app, Properties props) + { + no(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + protected void set_parameters(String[] args, Properties props) + { + no(); + } + + /** + * Throws an exception, stating that the given method is not supported + * by the Restricted ORB. + */ + private final void no() + { + // Apart the programming errors, this can only happen if the + // malicious code is trying to do that it is not allowed. + throw new NO_IMPLEMENT("Use init(args, props) for the functional version."); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public Request get_next_response() + throws org.omg.CORBA.WrongTransaction + { + no(); + throw new InternalError(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public boolean poll_next_response() + { + no(); + throw new InternalError(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public void send_multiple_requests_deferred(Request[] requests) + { + no(); + } + + /** + * This method is not allowed for a RestrictedORB. + * + * @throws NO_IMPLEMENT, always. + */ + public void send_multiple_requests_oneway(Request[] requests) + { + no(); + } + + /** + * Register the value factory under the given repository id. + */ + public ValueFactory register_value_factory(String repository_id, + ValueFactory factory + ) + { + factories.put(repository_id, factory); + return factory; + } + + /** + * Unregister the value factroy. + */ + public void unregister_value_factory(String id) + { + factories.remove(id); + } + + /** + * Look for the value factory for the value, having the given repository id. + * The implementation checks for the registered value factories first. + * If none found, it tries to load and instantiate the class, mathing the + * given naming convention. If this faild, null is returned. + * + * @param repository_id a repository id. + * + * @return a found value factory, null if none. + */ + public ValueFactory lookup_value_factory(String repository_id) + { + ValueFactory f = (ValueFactory) factories.get(repository_id); + if (f != null) + return f; + + f = (ValueFactory) ObjectCreator.createObject(repository_id, "DefaultFactory"); + if (f != null) + factories.put(repository_id, f); + + return f; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/ServiceDetailHolder.java b/libjava/classpath/gnu/CORBA/ServiceDetailHolder.java new file mode 100644 index 00000000000..e8786374aa1 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/ServiceDetailHolder.java @@ -0,0 +1,91 @@ +/* ServiceDetailHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.ServiceDetail; +import org.omg.CORBA.ServiceDetailHelper; + + +/** + * The service detail holder. This class is not included in the original + * API specification, so we place it outside the org.omg namespace. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class ServiceDetailHolder + implements org.omg.CORBA.portable.Streamable +{ + /** + * The stored value. + */ + public ServiceDetail value; + + /** + * Create the initialised instance. + * @param initialValue + */ + public ServiceDetailHolder(ServiceDetail initialValue) + { + value = initialValue; + } + + /** + * Read from the CDR stream. + */ + public void _read(org.omg.CORBA.portable.InputStream in) + { + value = ServiceDetailHelper.read(in); + } + + /** + * Get the typecode. + */ + public org.omg.CORBA.TypeCode _type() + { + return ServiceDetailHelper.type(); + } + + /** + * Write into the CDR stream. + */ + public void _write(org.omg.CORBA.portable.OutputStream out) + { + ServiceDetailHelper.write(out, value); + } +} diff --git a/libjava/classpath/gnu/CORBA/ServiceRequestAdapter.java b/libjava/classpath/gnu/CORBA/ServiceRequestAdapter.java new file mode 100644 index 00000000000..0b698818215 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/ServiceRequestAdapter.java @@ -0,0 +1,159 @@ +/* ServiceRequestConverter.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufOutput; + +import org.omg.CORBA.ARG_IN; +import org.omg.CORBA.ARG_INOUT; +import org.omg.CORBA.ARG_OUT; +import org.omg.CORBA.Any; +import org.omg.CORBA.Bounds; +import org.omg.CORBA.ServerRequest; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.InvokeHandler; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.ResponseHandler; +import org.omg.CORBA.portable.Streamable; + +/** + * This class exists to handle obsolete invocation style using + * ServerRequest. + * + * @deprecated The method {@link ObjectImpl#_invoke} is much faster. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class ServiceRequestAdapter + implements ResponseHandler +{ + /** + * A buffer for writing the response. + */ + cdrBufOutput reply = new cdrBufOutput(); + + /** + * If set to true, an exception has been thrown during the invocation. + */ + boolean isException; + + public OutputStream createExceptionReply() + { + isException = true; + return reply; + } + + public OutputStream createReply() + { + isException = false; + return reply; + } + + /** + * The old style invocation using the currently deprecated server + * request class. + * + * @param request a server request, containg the invocation information. + * @param target the invocation target + * @param result the result holder with the set suitable streamable to read + * the result or null for void. + */ + public static void invoke(ServerRequest request, InvokeHandler target, + Streamable result + ) + { + try + { + int IN = ARG_IN.value; + int OUT = ARG_OUT.value; + + // Write all arguments to the buffer output stream. + cdrBufOutput buffer = new cdrBufOutput(); + gnuNVList args = new gnuNVList(); + request.arguments(args); + + for (int i = 0; i < args.count(); i++) + { + if ((args.item(i).flags() & IN) != 0) + { + args.item(i).value().write_value(buffer); + } + } + + ServiceRequestAdapter h = new ServiceRequestAdapter(); + + target._invoke(request.operation(), buffer.create_input_stream(), h); + + InputStream in = h.reply.create_input_stream(); + + if (h.isException) + { + // Write the exception information + gnuAny exc = new gnuAny(); + universalHolder uku = new universalHolder(h.reply); + exc.insert_Streamable(uku); + request.set_exception(exc); + } + else + { + if (result != null) + { + result._read(in); + gnuAny r = new gnuAny(); + r.insert_Streamable(result); + request.set_result(r); + }; + + // Unpack the arguments + for (int i = 0; i < args.count(); i++) + { + if ((args.item(i).flags() & OUT) != 0) + { + Any a = args.item(i).value(); + a.read_value(in, a.type()); + } + } + } + } + catch (Bounds ex) + { + throw new InternalError(); + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/SetOverrideTypeHolder.java b/libjava/classpath/gnu/CORBA/SetOverrideTypeHolder.java new file mode 100644 index 00000000000..12b4b9f0cc8 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/SetOverrideTypeHolder.java @@ -0,0 +1,90 @@ +/* SetOverrideTypeHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.SetOverrideType; +import org.omg.CORBA.SetOverrideTypeHelper; + +/** + * The holder for SetOverrideType. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class SetOverrideTypeHolder + implements org.omg.CORBA.portable.Streamable +{ + /** + * The stored SetOverrideType value. + */ + public SetOverrideType value; + + /** + * Create the initialised instance. + * + * @param initialValue the initial value. + */ + public SetOverrideTypeHolder(SetOverrideType initialValue) + { + value = initialValue; + } + + /** + * Fill in the {@link value} by data from the CDR stream. + */ + public void _read(org.omg.CORBA.portable.InputStream in) + { + value = SetOverrideTypeHelper.read(in); + } + + /** + * Get the typecode of the SetOverrideType. + */ + public org.omg.CORBA.TypeCode _type() + { + return SetOverrideTypeHelper.type(); + } + + /** + * Write the stored value into the CDR stream. + */ + public void _write(org.omg.CORBA.portable.OutputStream out) + { + SetOverrideTypeHelper.write(out, value); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/Simple_delegate.java b/libjava/classpath/gnu/CORBA/Simple_delegate.java new file mode 100644 index 00000000000..d0b2ad29e0b --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Simple_delegate.java @@ -0,0 +1,249 @@ +/* Local_delegate.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.Context; +import org.omg.CORBA.ContextList; +import org.omg.CORBA.ExceptionList; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.NVList; +import org.omg.CORBA.NamedValue; +import org.omg.CORBA.ORB; +import org.omg.CORBA.Request; +import org.omg.CORBA.portable.Delegate; +import org.omg.CORBA.portable.ObjectImpl; + +/** + * The delegate, implementing the basic functionality only. This delegate + * is set in {@link ORG.connect(org.omg.CORBA.Object)} if ORB + * determines that the object is an instance of the + * {@link org.omg.CORBA.portable.ObjectImpl} and no other delegate is set. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class Simple_delegate + extends Delegate +{ + /** + * The orb. + */ + protected final ORB orb; + + /** + * The ior. + */ + protected IOR ior; + + public Simple_delegate(ORB an_orb, IOR an_ior) + { + orb = an_orb; + ior = an_ior; + } + + /** + * Set the IOR of this object. The IOR must be newly set if + * the server reports that the object has permanently moved to a new + * location. + * + * @param an_ior the new IOR. + */ + public void setIor(IOR an_ior) + { + this.ior = an_ior; + } + + /** + * Get the IOR of this object. + */ + public IOR getIor() + { + return ior; + } + + /** + * Not implemented. + * + * @throws NO_IMPLEMENT, always. + */ + public Request create_request(org.omg.CORBA.Object target, Context context, + String operation, NVList parameters, + NamedValue returns + ) + { + throw new NO_IMPLEMENT(); + } + + /** + * Not implemented. + * + * @throws NO_IMPLEMENT, always. + */ + public Request create_request(org.omg.CORBA.Object target, Context context, + String operation, NVList parameters, + NamedValue returns, ExceptionList exceptions, + ContextList ctx_list + ) + { + throw new NO_IMPLEMENT(); + } + + /** + * Not implemented. + * + * @throws NO_IMPLEMENT, always. + */ + public org.omg.CORBA.Object duplicate(org.omg.CORBA.Object target) + { + throw new NO_IMPLEMENT(); + } + + /** + * Performs direct comparison ('=='). + */ + public boolean equals(org.omg.CORBA.Object self, org.omg.CORBA.Object other) + { + return self == other; + } + + /** + * Not implemented. + * + * @throws NO_IMPLEMENT, always. + */ + public org.omg.CORBA.Object get_interface_def(org.omg.CORBA.Object target) + { + throw new NO_IMPLEMENT(); + } + + /** + * Return the hashcode (0 <= hashcode < maximum). + */ + public int hash(org.omg.CORBA.Object target, int maximum) + { + return target == null ? 0 : target.hashCode() % maximum; + } + + /** + * Delegates functionality to java.lang.Object.hashCode(); + */ + public int hashCode(org.omg.CORBA.Object target) + { + return target == null ? 0 : target.hashCode(); + } + + /** + * Check if this object can be referenced by the given repository id. + * + * @param target the CORBA object, must be an instance of + * {@link org.omg.CORBA.portable.ObjectImpl}. + * + * @param repositoryIdentifer the repository id. + * + * @return true if the passed parameter is a repository id of this + * CORBA object. + */ + public boolean is_a(org.omg.CORBA.Object target, String repositoryIdentifer) + { + if (!(target instanceof ObjectImpl)) + throw new NO_IMPLEMENT("Supported only for org.omg.CORBA.portable.ObjectImpl"); + + ObjectImpl imp = (ObjectImpl) target; + String[] ids = imp._ids(); + + for (int i = 0; i < ids.length; i++) + { + if (ids [ i ].equals(repositoryIdentifer)) + return true; + } + return false; + } + + /** + * Only returns true if the objects are equal ('=='). + */ + public boolean is_equivalent(org.omg.CORBA.Object target, + org.omg.CORBA.Object other + ) + { + return target == other; + } + + /** + * Returns true by default. + */ + public boolean is_local(org.omg.CORBA.Object self) + { + return true; + } + + /** + * Returns true if the target is null. + */ + public boolean non_existent(org.omg.CORBA.Object target) + { + return target == null; + } + + /** + * Returns the ORB, passed in constructor, + * regardless of the argument. This class requires a single instance + * per each object. + */ + public ORB orb(org.omg.CORBA.Object target) + { + return orb; + } + + /** + * Returns without action. + */ + public void release(org.omg.CORBA.Object target) + { + } + + /** + * This should never be called this type delegate. + * + * @throws InternalError, always. + */ + public Request request(org.omg.CORBA.Object target, String operation) + { + throw new InternalError(); + } +} diff --git a/libjava/classpath/gnu/CORBA/SocketRepository.java b/libjava/classpath/gnu/CORBA/SocketRepository.java new file mode 100644 index 00000000000..70bceadf5f7 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/SocketRepository.java @@ -0,0 +1,93 @@ +/* SocketRepository.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.net.Socket; + +import java.util.HashMap; + +/** + * This class caches the opened sockets that are reused during the + * frequent calls. Otherwise, some CORBA applications may spend + * up to 90 % of the working time just for closing and opening the sockets. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class SocketRepository +{ + /** + * The socket map. + */ + private static HashMap sockets = new HashMap(); + + /** + * Put a socket. + * + * @param key as socket key. + * + * @param s a socket. + */ + public static void put_socket(Object key, Socket s) + { + sockets.put(key, s); + } + + /** + * Get a socket. + * + * @param key a socket key. + * + * @return an opened socket for reuse, null if no such + * available or it is closed. + */ + public static Socket get_socket(Object key) + { + Socket s = (Socket) sockets.get(key); + if (s != null && s.isClosed()) + { + sockets.remove(key); + return null; + } + else + { + sockets.remove(key); + return s; + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/TypeCodeHelper.java b/libjava/classpath/gnu/CORBA/TypeCodeHelper.java new file mode 100644 index 00000000000..c742275420e --- /dev/null +++ b/libjava/classpath/gnu/CORBA/TypeCodeHelper.java @@ -0,0 +1,297 @@ +/* TypeCodeHelper.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.TypeCodePackage.Bounds; + +/** + * Reads and writes the TypeCodes usind common data representation. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class TypeCodeHelper +{ + /** + * Read the CORBA {@link TypeCode}. First, the TypeCode kind + * is read as four byte long. Then, if needed, the additional + * parameters are loaded following CORBA specification. + * + * @param in a stream to read from. + */ + public static TypeCode read(org.omg.CORBA.portable.InputStream in) + throws BadKind, Bounds + { + TCKind kind = TCKind.from_int(in.read_long()); + TypeCode rt; + generalTypeCode g; + recordTypeCode r; + recordTypeCode.Field f; + stringTypeCode s; + int n; + + switch (kind.value()) + { + case TCKind._tk_sequence : + case TCKind._tk_array : + + primitiveArrayTypeCode p = new primitiveArrayTypeCode(kind); + p.setLength(in.read_long()); + rt = p; + break; + + case TCKind._tk_string : + case TCKind._tk_wstring : + s = new stringTypeCode(kind); + s.setLength(in.read_long()); + rt = s; + break; + + case TCKind._tk_fixed : + + fixedTypeCode fx = new fixedTypeCode(); + fx.setDigits(in.read_short()); + fx.setScale(in.read_short()); + rt = fx; + break; + + case TCKind._tk_objref : + case TCKind._tk_native : + case TCKind._tk_abstract_interface : + g = new generalTypeCode(kind); + g.setId(in.read_string()); + g.setName(in.read_string()); + rt = g; + break; + + case TCKind._tk_alias : + case TCKind._tk_value_box : + g = new generalTypeCode(kind); + g.setId(in.read_string()); + g.setName(in.read_string()); + g.setContentType(in.read_TypeCode()); + rt = g; + break; + + case TCKind._tk_struct : + case TCKind._tk_except : + r = new recordTypeCode(kind); + r.setId(in.read_string()); + r.setName(in.read_string()); + + n = in.read_long(); + + for (int i = 0; i < n; i++) + { + f = r.field(); + f.name = in.read_string(); + f.type = in.read_TypeCode(); + } + rt = r; + break; + + case TCKind._tk_enum : + r = new recordTypeCode(kind); + r.setId(in.read_string()); + r.setName(in.read_string()); + + n = in.read_long(); + + for (int i = 0; i < n; i++) + { + f = r.field(); + f.name = in.read_string(); + } + rt = r; + break; + + case TCKind._tk_union : + r = new recordTypeCode(kind); + r.setId(in.read_string()); + r.setName(in.read_string()); + r.setDiscriminator_type(in.read_TypeCode()); + r.setDefaultIndex(in.read_long()); + + n = in.read_long(); + + for (int i = 0; i < n; i++) + { + f = r.field(); + f.label = in.read_any(); + f.name = in.read_string(); + f.type = in.read_TypeCode(); + } + rt = r; + + break; + + case TCKind._tk_value : + r = new recordTypeCode(kind); + r.setId(in.read_string()); + r.setName(in.read_string()); + r.setTypeModifier(in.read_short()); + r.setConcreteBase_type(in.read_TypeCode()); + + n = in.read_long(); + + for (int i = 0; i < n; i++) + { + f = r.field(); + f.name = in.read_string(); + f.type = in.read_TypeCode(); + f.visibility = in.read_short(); + } + rt = r; + break; + + default : + rt = new primitiveTypeCode(kind); + } + return rt; + } + + /** + * Write the CORBA {@link TypeCode}. First, the TypeCode kind + * is written as four byte long. Then, if needed, the additional + * parameters are stored following CORBA specification. + * + * @param out a stream to write into. + * @param x a {@link TypeCode} to write. + */ + public static void write(org.omg.CORBA.portable.OutputStream out, TypeCode x) + throws BadKind, Bounds + { + out.write_long(x.kind().value()); + + switch (x.kind().value()) + { + case TCKind._tk_string : + case TCKind._tk_wstring : + out.write_long(x.length()); + break; + + case TCKind._tk_sequence : + case TCKind._tk_array : + write(out, x.content_type()); + out.write_long(x.length()); + break; + + case TCKind._tk_fixed : + out.write_short(x.fixed_digits()); + out.write_short(x.fixed_scale()); + break; + + case TCKind._tk_objref : + case TCKind._tk_native : + case TCKind._tk_abstract_interface : + out.write_string(x.id()); + out.write_string(x.name()); + break; + + case TCKind._tk_alias : + case TCKind._tk_value_box : + out.write_string(x.id()); + out.write_string(x.name()); + write(out, x.content_type()); + break; + + case TCKind._tk_struct : + case TCKind._tk_except : + out.write_string(x.id()); + out.write_string(x.name()); + + out.write_long(x.member_count()); + + for (int i = 0; i < x.member_count(); i++) + { + out.write_string(x.member_name(i)); + write(out, x.member_type(i)); + } + break; + + case TCKind._tk_enum : + out.write_string(x.id()); + out.write_string(x.name()); + + out.write_long(x.member_count()); + + for (int i = 0; i < x.member_count(); i++) + { + out.write_string(x.member_name(i)); + } + break; + + case TCKind._tk_union : + out.write_string(x.id()); + out.write_string(x.name()); + + write(out, x.discriminator_type()); + out.write_long(x.default_index()); + + out.write_long(x.member_count()); + + for (int i = 0; i < x.member_count(); i++) + { + out.write_any(x.member_label(i)); + out.write_string(x.member_name(i)); + write(out, x.member_type(i)); + } + break; + + case TCKind._tk_value : + out.write_string(x.id()); + out.write_string(x.name()); + out.write_short(x.type_modifier()); + write(out, x.concrete_base_type()); + + out.write_long(x.member_count()); + + for (int i = 0; i < x.member_count(); i++) + { + out.write_string(x.member_name(i)); + write(out, x.member_type(i)); + out.write_short(x.member_visibility(i)); + } + break; + + default :} + } +} diff --git a/libjava/classpath/gnu/CORBA/Unexpected.java b/libjava/classpath/gnu/CORBA/Unexpected.java new file mode 100644 index 00000000000..89fb7e7b9dc --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Unexpected.java @@ -0,0 +1,128 @@ +/* DNW.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + + +/** + * Contains the static method to throw an error in the case + * when the execution should never get into the current point. + * + * The error message contains the text, suggesting to check + * the user code first and then report a bug. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class Unexpected + extends InternalError +{ + /** + * Use serialVersionUID for interoperability. + */ + private static final long serialVersionUID = 1; + + /** + * The default message for the CORBA assertion error. + */ + public static final String SHARED_MESSAGE = + "CORBA assertion error. Please check your code. " + + "If you think it is Classpath problem, please report " + + "this bug providing as much information as possible."; + + /** + * Create an instance with explaining message and enclosing + * exception. + */ + public Unexpected(String msg, Exception why) + { + super(msg + ". " + SHARED_MESSAGE); + if (why != null) + initCause(why); + } + + /** + * Create an instance with enclosing exception. + */ + public Unexpected(Exception why) + { + super(SHARED_MESSAGE); + if (why != null) + initCause(why); + } + + /** + * Create an instance. + */ + public Unexpected() + { + super(SHARED_MESSAGE); + } + + /** + * Throws an error with the custom explaining message and + * the appended share message. + * + * @param msg the error message + * @param why the enclosing exception. + */ + public static void error(String msg, Exception why) + { + throw new Unexpected(msg, why); + } + + /** + * Throws an error with the shared explaining message. + * + * @param why the enclosing exception. + * @throws Error, always. + */ + public static void error(Exception why) + { + throw new Unexpected(why); + } + + /** + * Throws an error with the shared explaining message. + * + * @throws Error, always. + */ + public static void error() + { + throw new Unexpected(); + } +} diff --git a/libjava/classpath/gnu/CORBA/Version.java b/libjava/classpath/gnu/CORBA/Version.java new file mode 100644 index 00000000000..84f40bf4f39 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/Version.java @@ -0,0 +1,206 @@ +/* Version.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.io.IOException; +import java.io.Serializable; + +import org.omg.CORBA.MARSHAL; + +/** + * A version number, represented by the major version number + * and the minor version number. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class Version + implements Serializable +{ + /** + * Use serialVersionUID for interoperability. + */ + private static final long serialVersionUID = 1; + + /** + * Major number (0..256, so the byte cannot be used). + */ + public final int major; + + /** + * Minor number. + */ + public final int minor; + + /** + * Create the version with the given version numbers. + * + * @param major major number (0..255) + * @param minor minor number (0..255) + */ + public Version(int _major, int _minor) + { + major = (byte) _major; + minor = (byte) _minor; + } + + /** + * Returns true if the versions are equal. + * @param other the other version to compare. + * + * @return true if the versions are equal + */ + public boolean equals(java.lang.Object other) + { + if (other == this) + { + return true; + } + if (!(other instanceof Version)) + { + return false; + } + + Version that = (Version) other; + return same(that); + } + + /** + * Read from the input stream, major number first. + * @param in a stream to read from. + */ + public static Version read_version(java.io.InputStream in) + { + try + { + int major = in.read() & 0xFF; + int minor = in.read() & 0xFF; + return new Version(major, minor); + } + catch (IOException ex) + { + throw new MARSHAL("IOException while reading message header"); + } + } + + /** + * Returns true if the versions are the same. + * + * @param that the other version to compare. + * + * @return true if the versions are the same. + */ + public boolean same(Version that) + { + return major == that.major && minor == that.minor; + } + + /** + * Returns true if the given version is higher than + * or equals to the version, supplied as parameter + * in the form of two integers. + * + * @param a_major major number of the version to compare. + * @param a_minor minor number of the version to compare. + * + * @return true if this version is higher than or equals to + * the version v. + */ + public boolean since_inclusive(int a_major, int a_minor) + { + if (major > a_major) + return true; + else if (major < a_major) + return false; + else + + // Major numbers are equal. + return minor >= a_minor; + } + + /** + * Return the string representation, in the form + * major.minor. + */ + public String toString() + { + return major + "." + minor; + } + + /** + * Returs true if the given version is lower or equal to the + * version, specified by the provided minor and major version + * number. This means, the version, specified by these two numbers, + * should be supported by teh current version. + * + * @param a_major a major version number. + * @param a_minor a minor version number. + * + * @return true if the current version should be supported by the + * version, specified by the two passed numbers. + */ + public boolean until_inclusive(int a_major, int a_minor) + { + if (major < a_major) + return true; + else if (major > a_major) + return false; + else + + // Major numbers are equal. + return minor <= a_minor; + } + + /** + * Write into the output stream, major number first. + * + * @param out a stream to write into. + */ + public void write(java.io.OutputStream out) + { + try + { + out.write(major & 0xFF); + out.write(minor & 0xFF); + } + catch (IOException ex) + { + throw new MARSHAL("IOException while writing message header"); + } + } +} diff --git a/libjava/classpath/gnu/CORBA/WCharHolder.java b/libjava/classpath/gnu/CORBA/WCharHolder.java new file mode 100644 index 00000000000..23f0ad100f9 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/WCharHolder.java @@ -0,0 +1,126 @@ +/* WCharHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +/** + * A holder for CORBA <code>char</code> that is mapped into + * java <code>char</code>. + * + * The holders have several application areas. The end user usually + * sees them implementing CORBA methods where the primitive type + * is passed by reference. While CORBA (or, for example, C) supports + * this, the java does not and a wrapper class is required. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public final class WCharHolder + implements Streamable +{ + /** + * The default type code for this holder. + */ + private static final TypeCode t_char = new primitiveTypeCode(TCKind.tk_wchar); + + /** + * The <code>char</code> (CORBA <code>wchar</code>) value, + * held by this WCharHolder. + */ + public char value; + + /** + * Constructs an instance of WCharHolder, + * initializing {@link #value} to <code>0 </code>. + */ + public WCharHolder() + { + } + + /** + * Constructs an instance of WCharHolder, + * initializing {@link #value} to the given <code>char</code>. + * + * @param initial_value a value that will be assigned to the + * {@link #value} field. + */ + public WCharHolder(char initial_value) + { + value = initial_value; + } + + /** + * Fill in the {@link value } field by reading the required data + * from the given stream. For <code>char</code>, the functionality + * is delegated to + * {@link org.omg.CORBA.portable.InputStream#read_wchar}. + * + * @param input the input stream to read from. + */ + public void _read(InputStream input) + { + value = input.read_wchar(); + } + + /** + * Returns the TypeCode, corresponding the CORBA type that is stored + * using this holder. + */ + public TypeCode _type() + { + return t_char; + } + + /** + * Write the {@link value } field to the given stream. + * For <code>char</code>, the functionality + * is delegated to + * {@link org.omg.CORBA.portable.OutputStream#write_wchar(char) }. + * + * @param output the output stream to write into. + */ + public void _write(OutputStream output) + { + output.write_wchar(value); + } +} diff --git a/libjava/classpath/gnu/CORBA/WStringHolder.java b/libjava/classpath/gnu/CORBA/WStringHolder.java new file mode 100644 index 00000000000..c9e8e33234d --- /dev/null +++ b/libjava/classpath/gnu/CORBA/WStringHolder.java @@ -0,0 +1,129 @@ +/* WStringHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +/** + * A holder for CORBA <code>wstring</code> that is mapped into + * java <code>String</code>. This holder writes and reads differently + * from the StringHolder. + * + * The holders have several application areas. The end user usually + * sees them implementing CORBA methods where the primitive type + * is passed by reference. While CORBA (or, for example, C) supports + * this, the java does not and a wrapper class is required. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class WStringHolder + implements Streamable +{ + /** + * The default type code for this holder. + */ + private static final stringTypeCode t_string = + new stringTypeCode(TCKind.tk_wstring); + + /** + * The <code>String</code> (CORBA <code>string</code>) value, + * held by this WStringHolder. + */ + public String value; + + /** + * Constructs an instance of WStringHolder, + * initializing {@link #value} to <code>null</code>. + */ + public WStringHolder() + { + } + + /** + * Constructs an instance of WStringHolder, + * initializing {@link #value} to the given <code>String</code>. + * + * @param initial_value a value that will be assigned to the + * {@link #value} field. + */ + public WStringHolder(String initial_value) + { + value = initial_value; + } + + /** + * Fill in the {@link #value } field by reading the required data + * from the given stream. For <code>string</code>, the functionality + * is delegated to + * {@link org.omg.CORBA.portable.InputStream#read_wstring}. + * + * @param input the input stream to read from. + */ + public void _read(InputStream input) + { + value = input.read_wstring(); + } + + /** + * Returns the TypeCode, corresponding the CORBA type that is stored + * using this holder. The {@link TypeCode#length()} method of the + * returned typecode always returns 0. + */ + public TypeCode _type() + { + return t_string; + } + + /** + * Write the {@link #value } field to the given stream. + * For <code>string</code>, the functionality + * is delegated to + * {@link org.omg.CORBA.portable.OutputStream#write_wstring(String) }. + * + * @param output the output stream to write into. + */ + public void _write(OutputStream output) + { + output.write_wstring(value); + } +} diff --git a/libjava/classpath/gnu/CORBA/_PolicyImplBase.java b/libjava/classpath/gnu/CORBA/_PolicyImplBase.java new file mode 100644 index 00000000000..d9ff9d648ad --- /dev/null +++ b/libjava/classpath/gnu/CORBA/_PolicyImplBase.java @@ -0,0 +1,231 @@ +/* _PolicyImplBase.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.CompletionStatus; +import org.omg.CORBA.Policy; +import org.omg.CORBA.PolicyHelper; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.InvokeHandler; +import org.omg.CORBA.portable.ObjectImpl; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.ResponseHandler; + +/** + * The server side implementation base for the {@link Policy}. + * + * @specnote The java 1.4 API does not define the server side policy + * implementation base, but it defines the policy client side stub. + * As these two classes always work together, and even no separate testing is + * possible, the required implementation base is provided in gnu.CORBA + * namespace. Sun will probably include they base in the future java APIs. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public abstract class _PolicyImplBase + extends ObjectImpl + implements Policy, InvokeHandler +{ + /** + * Use serialVersionUID for interoperability. + */ + private static final long serialVersionUID = 1; + + /** + * The policy repository ids. + */ + private final String[] ids; + + /** + * The type of this policy. + */ + private final int type; + + /** + * The value of this policy. The value object is never the same + * for different policies. + */ + private final java.lang.Object value; + + /** + * The policy integer code, written in request to write + * the policy value. + */ + private final int policyCode; + + /** + * Create the new policy of the given type, having the given value. + * For security reasons, the method is kept package private. + * + * @param p_type the type of this policy. + * @param p_value the value of this policy. + * @param p_code the integer code of this policy. + * @param p_idl the policy IDL type string. The {@link #_ids()} + * will return array, first line being this string and another + * being PolicyHelper.id(). + */ + public _PolicyImplBase(int p_type, java.lang.Object p_value, int p_code, + String p_idl + ) + { + type = p_type; + value = p_value; + policyCode = p_code; + ids = new String[] { p_idl, PolicyHelper.id() }; + } + + /** + * Get the integer code of the type of this policy. + */ + public final int policy_type() + { + return type; + } + + /** + * Return the list of repository ids. + */ + public final String[] _ids() + { + return ids; + } + + /** + * Call the required method. + */ + public final OutputStream _invoke(String method, InputStream input, + ResponseHandler rh + ) + { + OutputStream output = null; + + if (method.equals("destroy")) + { + // The "destroy" has been invoked. + destroy(); + output = rh.createReply(); + } + else if (method.equals("copy")) + { + // The "copy" has been invoked. + org.omg.CORBA.Object returns = copy(); + output = rh.createReply(); + output.write_Object(this); + } + else if (method.equals("policy_type")) + { + // The "policy_type" has been invoked. + int returns = policy_type(); + output = rh.createReply(); + output.write_long(returns); + } + else if (method.equals("value")) + { + // The "value" can be invoked on the children types + // and must return an integer, representing the policy value + // (CORBA enumeration). + output = rh.createReply(); + output.write_long(policyCode); + } + else + throw new BAD_OPERATION(method, 0, CompletionStatus.COMPLETED_MAYBE); + + return output; + } + + /** + * Get the value of this policy + */ + public final java.lang.Object getValue() + { + return value; + } + + /** + * Get the integer code of this policy value. + */ + public final int getCode() + { + return policyCode; + } + + /** + * Returns without action. It is a work of garbage collector + * to remove the unused objects. + */ + public final void destroy() + { + } + + /** + * Returns the string representation of the given policy. + */ + public final String toString() + { + return value.toString(); + } + + /** + * Create a copy of this policy. The object is not mutable, so + * <code>this</code> can be returned. + * + * @return <code>this</code> + */ + public Policy copy() + { + return this; + } + + /** + * Use the value to get a hash code. + */ + public int hashCode() + { + return getValue().hashCode(); + } + + /** + * Check the values for equality. + */ + public boolean equals(Object x) + { + return x == null ? false : getValue().equals(x); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/aliasTypeCode.java b/libjava/classpath/gnu/CORBA/aliasTypeCode.java new file mode 100644 index 00000000000..88466310142 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/aliasTypeCode.java @@ -0,0 +1,142 @@ +/* aliasTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; + +/** + * The type code that is an alias of another type code. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class aliasTypeCode + extends primitiveTypeCode +{ + /** + * The typecode repository id. + */ + protected final String id; + + /** + * The typecode name. + */ + protected final String name; + + /** + * The type code for that this typecode is an alias. + */ + protected final TypeCode aliasFor; + + /** + * Create the typecode, specifying for that typecode it is an + * alias and the id and name of the newly created typecode. + * + * @param an_aliasFor the typecode, for that this typecode is an + * alias. + * + * @param an_id the repository id fo the newly created typecode. + * + * @param a_name the name of the newly created typecode. + */ + public aliasTypeCode(TypeCode an_aliasFor, String an_id, String a_name) + { + super(TCKind.tk_alias); + aliasFor = an_aliasFor; + id = an_id; + name = a_name; + } + + /** + * Get the typecode, for that this typecode is an alias. + */ + public TypeCode content_type() + { + return aliasFor; + } + + /** + * The objects are assumed to be equal if they repository + * ids are both equal or both unavailable and the + * kind values are equal. + * + * @param other the other typecode to compare. + */ + public boolean equal(TypeCode other) + { + if (super.equal(other)) + return true; + try + { + return id.equals(other.id()); + } + catch (BadKind ex) + { + return false; + } + } + + /** + * Return true if the given typecode is equal for + * either this typecode of the alias typecode. + * + * @param other the typecode to compare. + */ + public boolean equivalent(TypeCode other) + { + return other.equal(this) || other.equal(aliasFor); + } + + /** + * Get the repository id of this typecode. + */ + public String id() + { + return id; + } + + /** + * Get the name of this typecode. + */ + public String name() + { + return name; + } +} diff --git a/libjava/classpath/gnu/CORBA/binaryReply.java b/libjava/classpath/gnu/CORBA/binaryReply.java new file mode 100644 index 00000000000..71afa377679 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/binaryReply.java @@ -0,0 +1,95 @@ +/* binaryReply.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.GIOP.MessageHeader; + +import org.omg.CORBA.ORB; + +/** + * The remote object reply in the binary form, holding + * the message header and the following binary data. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +class binaryReply +{ + /** + * The message header. + */ + final MessageHeader header; + + /** + * The associated orb. + */ + final ORB orb; + + /** + * The message data. + */ + final byte[] data; + + /** + * Create the binary reply. + * + * @param an_header the message header + * @param a_data the message data. + */ + binaryReply(ORB an_orb, MessageHeader an_header, byte[] a_data) + { + orb = an_orb; + header = an_header; + data = a_data; + } + + /** + * Get the CDR input stream with the correctly set alignment. + * + * @return the CDR stream to read the message data. + */ + cdrBufInput getStream() + { + cdrBufInput in = new cdrBufInput(data); + in.setOffset(header.getHeaderSize()); + in.setVersion(header.version); + in.setOrb(orb); + in.setBigEndian(header.isBigEndian()); + return in; + } +} diff --git a/libjava/classpath/gnu/CORBA/bufferedResponseHandler.java b/libjava/classpath/gnu/CORBA/bufferedResponseHandler.java new file mode 100644 index 00000000000..e7f00baebe2 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/bufferedResponseHandler.java @@ -0,0 +1,187 @@ +/* bufferedResponseHandler.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufOutput; +import gnu.CORBA.GIOP.MessageHeader; +import gnu.CORBA.GIOP.ReplyHeader; +import gnu.CORBA.GIOP.cxCodeSet; + +import org.omg.CORBA.ORB; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.ResponseHandler; + +/** + * Provides the CDR output streams for writing the response to the given + * buffer. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +class bufferedResponseHandler + implements ResponseHandler +{ + /** + * The message header. + * This field is used to compute the size and alignments. + * It is, however, never directly written to the buffer stream. + */ + final MessageHeader message_header; + + /** + * The associated orb. + */ + final ORB orb; + + /** + * The reply header. This field is used to compute the size and alignments. + * It is, however, never directly written to the buffer stream. + */ + final ReplyHeader reply_header; + + /** + * True if the stream was obtained by invoking {@link #createExceptionReply()}, + * false otherwise. + */ + private boolean exceptionReply; + + /** + * The buffer to write into. + */ + private cdrBufOutput buffer; + + /** + * Create a new buffered response handler that uses the given message + * headers. The headers are used to compute sizes and check the versions. + * They are not written into a stream inside this class. + * + * @param m_header a message header. + * @param r_header a reply header. + */ + bufferedResponseHandler(ORB an_orb, MessageHeader m_header, + ReplyHeader r_header + ) + { + message_header = m_header; + reply_header = r_header; + orb = an_orb; + prepareStream(); + } + + /** + * Get an output stream for providing details about the exception. + * Before returning the stream, the handler automatically writes + * the message header and the reply about exception header, + * but not the message header. + * + * @return the stream to write exception details into. + */ + public OutputStream createExceptionReply() + { + exceptionReply = true; + prepareStream(); + return buffer; + } + + /** + * Get an output stream for writing a regular reply (not an exception). + * + * Before returning the stream, the handler automatically writes + * the regular reply header, but not the message header. + * + * @return the output stream for writing a regular reply. + */ + public OutputStream createReply() + { + exceptionReply = false; + prepareStream(); + reply_header.reply_status = ReplyHeader.NO_EXCEPTION; + return buffer; + } + + /** + * Get the buffer, normally containing the written reply. + * The reply includes the reply header (or the exception header) + * but does not include the message header. + * + * The stream buffer can also be empty if no data have been written + * into streams, returned by {@link #createReply()} or + * {@link #createExceptionReply()}. + * + * @return the CDR output stream, containing the written output. + */ + cdrBufOutput getBuffer() + { + return buffer; + } + + /** + * True if the stream was obtained by invoking + * {@link #createExceptionReply()}, false otherwise + * (usually no-exception reply). + */ + boolean isExceptionReply() + { + return exceptionReply; + } + + /** + * Compute the header offset, set the correct version number and codeset. + */ + private void prepareStream() + { + buffer = new cdrBufOutput(); + buffer.setOrb(orb); + buffer.setOffset(message_header.getHeaderSize()); + + // Get the position after the reply header would be written. + reply_header.write(buffer); + + int new_offset = message_header.getHeaderSize() + buffer.buffer.size(); + + buffer.buffer.reset(); + buffer.setOffset(new_offset); + + if (message_header.version.since_inclusive(1, 2)) + buffer.align(8); + + buffer.setVersion(message_header.version); + + buffer.setCodeSet(cxCodeSet.find(reply_header.service_context)); + } +} diff --git a/libjava/classpath/gnu/CORBA/cdrEncapsCodec.java b/libjava/classpath/gnu/CORBA/cdrEncapsCodec.java new file mode 100644 index 00000000000..699c6f76a9c --- /dev/null +++ b/libjava/classpath/gnu/CORBA/cdrEncapsCodec.java @@ -0,0 +1,356 @@ +/* cdrEncapsCodec.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.CDR.cdrBufOutput; +import gnu.CORBA.CDR.cdrOutput; + +import org.omg.CORBA.Any; +import org.omg.CORBA.LocalObject; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.ORB; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.UserException; +import org.omg.IOP.Codec; +import org.omg.IOP.CodecPackage.FormatMismatch; +import org.omg.IOP.CodecPackage.InvalidTypeForEncoding; +import org.omg.IOP.CodecPackage.TypeMismatch; + +/** + * The local {@link Codec} implementation for ENCODING_CDR_ENCAPS + * encoding. This is a local implementation; the remote side should + * have its own Codec of this kind. + * + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class cdrEncapsCodec + extends LocalObject + implements Codec +{ + /** + * The default version of encoding, used in parameterless constructor. + */ + private static final Version DEFAULT_VERSION = new Version(1, 2); + + /** + * If set to true, no wide string or wide character is allowed (GIOP 1.0). + */ + private final boolean noWide; + + /** + * The version of this encoding. + */ + private final Version version; + + /** + * The associated ORB. + */ + protected final ORB orb; + + /** + * If true, this Codec writes the record length (as int) in the beginning + * of the record. This indicator is part of the formal OMG standard, but it is + * missing in Sun's implementation. Both Suns's and this Codec detects + * the indicator, if present, but can also decode data where this information + * is missing. If the length indicator is missing, the first four bytes in + * Suns encoding are equal to 0 (Big Endian marker). + */ + private boolean lengthIndicator = true; + + /** + * Create an instance of this Codec, encoding following the given version. + */ + public cdrEncapsCodec(ORB _orb, Version _version) + { + orb = _orb; + version = _version; + noWide = version.until_inclusive(1, 0); + } + + /** + * Return the array of repository ids for this object. + * + * @return { "IDL:gnu/CORBA/cdrEnapsCodec:1.0" }, always. + */ + public String[] _ids() + { + return new String[] { "IDL:gnu/CORBA/cdrEnapsCodec:1.0" }; + } + + /** + * Decode the contents of the byte array into Any. + * The byte array may have the optional four byte length indicator + * in the beginning. If these four bytes are zero, it is assumed, + * that no length indicator is present. + */ + public Any decode(byte[] them) + throws FormatMismatch + { + cdrBufInput input = createInput(them); + cdrBufInput encapsulation = createEncapsulation(them, input); + + TypeCode type = encapsulation.read_TypeCode(); + + try + { + checkTypePossibility("", type); + } + catch (InvalidTypeForEncoding ex) + { + throw new FormatMismatch(ex.getMessage()); + } + + return readAny(type, encapsulation); + } + + private cdrBufInput createEncapsulation(byte[] them, cdrBufInput input) + { + cdrBufInput encapsulation; + + if ((them [ 0 ] | them [ 1 ] | them [ 2 ] | them [ 3 ]) == 0) + { + // Skip that appears to be the always present Big Endian marker. + encapsulation = input; + input.read_short(); + } + else + encapsulation = input.read_encapsulation(); + return encapsulation; + } + + /** {@inheritDoc} */ + public byte[] encode(Any that) + throws InvalidTypeForEncoding + { + checkTypePossibility("", that.type()); + + cdrBufOutput output = createOutput(that); + + // cdrBufOutput has internal support for this encoding. + cdrOutput encapsulation = output.createEncapsulation(); + + try + { + TypeCodeHelper.write(encapsulation, that.type()); + that.write_value(encapsulation); + + encapsulation.close(); + output.close(); + } + catch (Exception ex) + { + MARSHAL m = new MARSHAL(); + m.initCause(ex); + throw m; + } + return output.buffer.toByteArray(); + } + + /** + * Decode the value, stored in the byte array, into Any, assuming, + * that the byte array holds the data structure, defined by the + * given typecode. + * + * The byte array may have the optional four byte length indicator + * in the beginning. If these four bytes are zero, it is assumed, + * that no length indicator is present. + */ + public Any decode_value(byte[] them, TypeCode type) + throws FormatMismatch, TypeMismatch + { + try + { + checkTypePossibility("", type); + } + catch (InvalidTypeForEncoding ex) + { + throw new TypeMismatch(ex.getMessage()); + } + + cdrBufInput input = createInput(them); + cdrBufInput encapsulation = createEncapsulation(them, input); + return readAny(type, encapsulation); + } + + /** + * Read an Any from the given stream. + * + * @param type a type of the Any to read. + * @param input the encapsulation stream. + */ + private Any readAny(TypeCode type, cdrBufInput encapsulation) + throws MARSHAL + { + gnuAny a = new gnuAny(); + a.setOrb(orb); + + // cdrBufInput has internal support for this encoding. + a.read_value(encapsulation, type); + return a; + } + + /** {@inheritDoc} */ + public byte[] encode_value(Any that) + throws InvalidTypeForEncoding + { + checkTypePossibility("", that.type()); + + cdrBufOutput output = createOutput(that); + + cdrOutput encapsulation = output.createEncapsulation(); + + try + { + that.write_value(encapsulation); + + encapsulation.close(); + output.close(); + } + catch (Exception ex) + { + MARSHAL m = new MARSHAL(); + m.initCause(ex); + throw m; + } + return output.buffer.toByteArray(); + } + + /** + * Create the CDR output stream for writing the given Any. + * The cdrBufOutput has internal support for encapsulation encodings. + * + * @param that the Any that will be written. + * + * @return the stream. + * + * @throws InvalidTypeForEncoding if that Any cannot be written under the + * given version. + */ + private cdrBufOutput createOutput(Any that) + throws InvalidTypeForEncoding + { + cdrBufOutput output = new cdrBufOutput(); + output.setOrb(orb); + output.setVersion(version); + return output; + } + + /** + * Checks if the given type can be encoded. Currently only checks for wide + * strings and wide chars for GIOP 1.0. + * + * @param t a typecode to chek. + * + * @throws InvalidTypeForEncoding if the typecode is not valid for the given + * version. + */ + private void checkTypePossibility(String name, TypeCode t) + throws InvalidTypeForEncoding + { + if (noWide) + { + try + { + int kind = t.kind().value(); + + if (kind == TCKind._tk_wchar || kind == TCKind._tk_wstring) + throw new InvalidTypeForEncoding(name + " wide char in " + + version + ); + else if (kind == TCKind._tk_alias || kind == TCKind._tk_array || + kind == TCKind._tk_sequence + ) + checkTypePossibility("Array member", t.content_type()); + + else if (kind == TCKind._tk_struct || kind == TCKind._tk_union) + { + for (int i = 0; i < t.member_count(); i++) + { + checkTypePossibility(t.member_name(i), t.member_type(i)); + } + } + } + catch (UserException ex) + { + InternalError ierr = new InternalError(); + ierr.initCause(ex); + throw ierr; + } + } + } + + /** + * Create the CDR input stream for reading the given byte array. + * + * @param them a byte array to read. + * + * @return the stream. + */ + private cdrBufInput createInput(byte[] them) + { + cdrBufInput input = new cdrBufInput(them); + input.setOrb(orb); + input.setVersion(version); + return input; + } + + /** + * Check if the Codec writes the length indicator. + */ + public boolean hasLengthIndicator() + { + return lengthIndicator; + } + + /** + * Sets if the Codec must write the record length in the beginning of the + * array. Encodings both with and without that indicator are understood + * both by Suns and this codec, but the OMG specification seems requiring + * it. The default behavior is to use the length indicator. + * + * @param use_lengthIndicator + */ + public void setUseLengthIndicator(boolean use_lengthIndicator) + { + lengthIndicator = use_lengthIndicator; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/corbaArrayList.java b/libjava/classpath/gnu/CORBA/corbaArrayList.java new file mode 100644 index 00000000000..1690f0583cb --- /dev/null +++ b/libjava/classpath/gnu/CORBA/corbaArrayList.java @@ -0,0 +1,115 @@ +/* corbaArrayList.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.io.Serializable; + +import java.util.ArrayList; + +import org.omg.CORBA.Bounds; + +/** + * This class is used to store array lists. Differently from + * the java.util lists, + * it throws {@link org.omg.CORBA.Bounds} rather than + * {@link IndexOutOfBoundsException}. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class corbaArrayList + extends ArrayList + implements Serializable +{ + /** + * Use serialVersionUID for interoperability. + */ + private static final long serialVersionUID = 1; + + /** + * Creates the list with the given initial size. + */ + public corbaArrayList(int initial_size) + { + super(initial_size); + } + + /** + * Creates the list with the default size. + */ + public corbaArrayList() + { + } + + /** + * Remove the item at the given index. + * @param at the index + * @throws org.omg.CORBA.Bounds if the index is out of bounds. + */ + public void drop(int at) + throws Bounds + { + try + { + super.remove(at); + } + catch (IndexOutOfBoundsException ex) + { + throw new Bounds("[" + at + "], valid [0.." + size() + "]"); + } + } + + /** + * Get the item at the given index. + * @param at the index + * @return the item at the index + * @throws org.omg.CORBA.Bounds if the index is out of bounds. + */ + public Object item(int at) + throws Bounds + { + try + { + return super.get(at); + } + catch (IndexOutOfBoundsException ex) + { + throw new Bounds("[" + at + "], valid [0.." + size() + "]"); + } + } +} diff --git a/libjava/classpath/gnu/CORBA/fixedTypeCode.java b/libjava/classpath/gnu/CORBA/fixedTypeCode.java new file mode 100644 index 00000000000..ec88c22be34 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/fixedTypeCode.java @@ -0,0 +1,149 @@ +/* fixedTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.math.BigDecimal; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; + +/** + * A typecode for CORBA <code>fixed</code> + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class fixedTypeCode + extends primitiveTypeCode +{ + /** + * The number of the used digits. + */ + private short digits; + + /** + * The number of the digits after the decimal point. + */ + private short scale; + + /** + * Creates the instance of the fixed type code. + */ + public fixedTypeCode() + { + super(TCKind.tk_fixed); + } + + /** + * Creates the instance of the fixed type code, + * setting the digits and scale by example. + */ + public fixedTypeCode(BigDecimal example) + { + super(TCKind.tk_fixed); + if (example != null) + { + setScale(example.scale()); + setDigits(countDigits(example)); + } + } + + /** + * Set the number of digits. + */ + public void setDigits(int a_digits) + { + this.digits = (short) a_digits; + } + + /** + * Set the number of digits after the decimal point. + */ + public void setScale(int a_scale) + { + this.scale = (short) a_scale; + } + + /** + * Get the number of digits in thid BigDecimal + * + * @param x a BigDecimal to check. + */ + public static int countDigits(BigDecimal number) + { + return number.unscaledValue().abs().toString().length(); + } + + public boolean equals(Object other) + { + if (other == this) + { + return true; + } + if (!(other instanceof TypeCode)) + { + return false; + } + try + { + TypeCode that = (TypeCode) other; + return kind() == that.kind() && digits == that.fixed_digits() && + scale == that.fixed_scale(); + } + catch (BadKind ex) + { + return false; + } + } + + /** + * Get the number of digits. + */ + public short fixed_digits() + { + return digits; + } + + /** + * Get the number of digits after the decimal point. + */ + public short fixed_scale() + { + return scale; + } +} diff --git a/libjava/classpath/gnu/CORBA/generalTypeCode.java b/libjava/classpath/gnu/CORBA/generalTypeCode.java new file mode 100644 index 00000000000..3b7914878c3 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/generalTypeCode.java @@ -0,0 +1,243 @@ +/* generalTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufOutput; + +import java.util.Arrays; +import java.util.BitSet; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; + +/** + * A typecode for types, requiring to provide various additional + * properties but still not requiring to store the + * members of the structure. The property can be retrieved + * by the corresponding method if it has been previously assigned. + * Otherwise, a {@link BadKind} is thrown. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class generalTypeCode + extends primitiveTypeCode +{ + /** + * Indicates that the field value has not been previously set. + */ + protected static int UNSET = Integer.MIN_VALUE; + + /** + * The kinds for that the length() must return 0 even if it + * has not been previously set. + */ + private static final BitSet lengthAllowed = new BitSet(); + + static + { + lengthAllowed.set(TCKind._tk_array); + lengthAllowed.set(TCKind._tk_sequence); + lengthAllowed.set(TCKind._tk_string); + lengthAllowed.set(TCKind._tk_wstring); + } + + private String id; + private String name; + private TypeCode concrete_base_type; + private TypeCode content_type; + private int len; + private int type_modifier = UNSET; + + /** + * Create a new instance, setting kind to the given kind. + * @param kind + */ + public generalTypeCode(TCKind kind) + { + super(kind); + if (!lengthAllowed.get(kind.value())) + len = UNSET; + } + + /** + * Set this property. + */ + public void setConcreteBase_type(TypeCode concrete_base_type) + { + this.concrete_base_type = concrete_base_type; + } + + /** + * Set the component content type. + */ + public void setContentType(TypeCode a_content_type) + { + this.content_type = a_content_type; + } + + /** + * Set this property. + */ + public void setId(String id) + { + this.id = id; + } + + /** + * Set the length property. + * @param l + */ + public void setLength(int l) + { + len = l; + } + + /** + * Set this property. + */ + public void setName(String name) + { + this.name = name; + } + + /** + * Set the type modifier. + */ + public void setTypeModifier(int a_type_modifier) + { + this.type_modifier = a_type_modifier; + } + + /** {@inheritDoc} */ + public TypeCode concrete_base_type() + throws BadKind + { + if (concrete_base_type != null) + return concrete_base_type; + throw new BadKind("concrete_base_type"); + } + + /** + * Returns the content type that must be explicitly set + * for this class. + * + * @throws BadKind if the content type has not been set. + */ + public TypeCode content_type() + throws BadKind + { + if (content_type != null) + return content_type; + throw new BadKind("content_type"); + } + + /** + * Returns true if both typecodes, if written into CDR + * stream, would result the same stream content. + */ + public boolean equal(TypeCode other) + { + if (this == other) + return true; + if (kind() != other.kind()) + return false; + + cdrBufOutput a = new cdrBufOutput(16); + cdrBufOutput b = new cdrBufOutput(16); + + a.write_TypeCode(this); + b.write_TypeCode(other); + + return Arrays.equals(a.buffer.toByteArray(), b.buffer.toByteArray()); + } + + /** + * Delegates functionality to {@link #equal}. + */ + public boolean equivalent(TypeCode other) + { + return equal(other); + } + + /** {@inheritDoc} */ + public String id() + throws BadKind + { + if (id != null) + return id; + throw new BadKind("id"); + } + + /** + * Get the length. For sequences, arrays, strings and wstrings + * this method returns 0 rather than throwing a BadKind even + * if {@link setLength(int)} has not been previously called. + * + * @return the length of string, array or sequence. + * + * @throws BadKind if the method cannot be invoked for the + * given kind of typecode. + */ + public int length() + throws BadKind + { + if (len != UNSET) + return len; + throw new BadKind("length"); + } + + /** {@inheritDoc} */ + public String name() + throws BadKind + { + if (name != null) + return name; + throw new BadKind("name"); + } + + /** {@inheritDoc} */ + public short type_modifier() + throws BadKind + { + if (type_modifier != UNSET) + return (short) type_modifier; + throw new BadKind("type_modifier"); + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuAny.java b/libjava/classpath/gnu/CORBA/gnuAny.java new file mode 100644 index 00000000000..a48c50d61ba --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuAny.java @@ -0,0 +1,830 @@ +/* gnuAny.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.CDR.cdrBufOutput; + +import org.omg.CORBA.Any; +import org.omg.CORBA.AnyHolder; +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.BooleanHolder; +import org.omg.CORBA.CharHolder; +import org.omg.CORBA.DoubleHolder; +import org.omg.CORBA.FixedHolder; +import org.omg.CORBA.FloatHolder; +import org.omg.CORBA.IntHolder; +import org.omg.CORBA.LongHolder; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.ORB; +import org.omg.CORBA.ObjectHolder; +import org.omg.CORBA.Principal; +import org.omg.CORBA.PrincipalHolder; +import org.omg.CORBA.ShortHolder; +import org.omg.CORBA.StringHolder; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodeHolder; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.ValueBaseHolder; +import org.omg.CORBA.portable.Streamable; + +import java.io.IOException; +import java.io.Serializable; + +import java.lang.reflect.Field; + +import java.math.BigDecimal; + +import java.util.Arrays; + +/** + * The implementation of {@link Any}. + * + * For performance reasonse, the inserted values are not cloned. + * If the value object allows modifications (like {@link Streamable}), + * these subsequent alterations are reflected by the instance of + * this gnuAny, and the gnuAny alterations are reflected by the + * returned value. If it is required to have the uncoupled value, + * it must be requested from the copy of the current instance. + * The {@link gnuAny} can be simply cloned by the provided + * {@link Clone()} method. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuAny + extends Any +{ + /** + * The value, returned by {@link #type()} if the value has been + * not intialized. + */ + protected static final TypeCode nullType = + new primitiveTypeCode(TCKind.tk_null); + + /** + * The Streamable, representing the value, held by this gnuAny. + */ + protected Streamable has; + + /** + * The complete typecode of the Streamable, if explicitly set. + */ + protected TypeCode typecode; + + /** + * The typecode kind of the Streamable, if explicitly set. + */ + protected int xKind = -1; + + /** + * The associated ORB. + */ + private ORB orb; + + /** + * Set the associated orb. + */ + public void setOrb(ORB an_orb) + { + orb = an_orb; + } + + /** + * Creates a deep copy of this gnuAny, writing to and subsequently + * reading from from the byte buffer. + * + * @return the uncoupled gnuAny with all fields set to identical + * values. + */ + public gnuAny Clone() + { + cdrBufOutput out = new cdrBufOutput(); + out.setOrb(orb); + out.write_any(this); + + cdrBufInput in = new cdrBufInput(out.buffer.toByteArray()); + in.setOrb(orb); + return (gnuAny) in.read_any(); + } + + /** + * Create the buffered CDR input stream, containing the + * value, stored inside of this {@link Any}. + */ + public org.omg.CORBA.portable.InputStream create_input_stream() + { + if (has instanceof universalHolder) + { + universalHolder u = (universalHolder) has; + return u.getInputStream(); + } + else + { + cdrBufOutput out = new cdrBufOutput(); + out.setOrb(orb); + write_value(out); + + cdrBufInput in = new cdrBufInput(out.buffer.toByteArray()); + in.setOrb(orb); + return in; + } + } + + /** + * Create the buffered CDR output stream (empty). + */ + public org.omg.CORBA.portable.OutputStream create_output_stream() + { + cdrBufOutput stream = new cdrBufOutput(); + stream.setOrb(orb); + return stream; + } + + /** + * Compare two Any's for equality. + * @param other the other Any to compare. + */ + public boolean equal(Any other) + { + if (other == this) + return true; + if (type().kind() != other.type().kind()) + return false; + + if (has != null && other instanceof gnuAny) + if (has.equals(((gnuAny) other).has)) + return true; + + cdrBufOutput a = new cdrBufOutput(); + a.setOrb(orb); + write_value(a); + + cdrBufOutput b = new cdrBufOutput(); + b.setOrb(orb); + other.write_value(b); + + byte[] ba = a.buffer.toByteArray(); + byte[] bb = b.buffer.toByteArray(); + + return Arrays.equals(ba, bb); + } + + /** + * Delegates functionality to {@link #equal(Any)}. + */ + public boolean equals(java.lang.Object other) + { + if (other == this) + return true; + if (!(other instanceof Any)) + return false; + + return equal((Any) other); + } + + /** + * Extract the previously stored object. + */ + public org.omg.CORBA.Object extract_Object() + { + try + { + return ((ObjectHolder) has).value; + } + catch (ClassCastException ex) + { + throw new BAD_OPERATION(); + } + } + + /** + * Extract the previously inserted CORBA <code>Principal</code>/ + * @return the previously inserted value. + * + * @throws org.omg.CORBA.BAD_OPERATION if the holder contains something + * else than Principal. + * + * @deprecated by CORBA 2.2. + */ + public Principal extract_Principal() + { + check(TCKind._tk_Principal); + return ((PrincipalHolder) has).value; + } + + /** + * Return the value, encapsulated in a suitable holder. + * This implementation returns the direct reference, + * so the alterations on the returned streamable are + * directly reflected to the content of this {@link Any}. + */ + public Streamable extract_Streamable() + { + return has; + } + + public TypeCode extract_TypeCode() + throws BAD_OPERATION + { + check(TCKind._tk_TypeCode); + return ((TypeCodeHolder) has).value; + } + + /** + * Extract the stored value type. + * + * @return the previously stored value type. + * + * @throws BAD_OPERATION if the Any contains something different. + * + * @see org.omg.CORBA.portable.ValueBase + */ + public Serializable extract_Value() + throws BAD_OPERATION + { + try + { + if (has instanceof ValueBaseHolder) + return ((ValueBaseHolder) has).value; + else + { + // Normally, ValueBase holder must be an instance of the + // ValueBaseHolder. However some IDL compilers probably + // have a bug, do not deriving this way. The the only + // way to access the wrapped value is via reflection. + Field f = has.getClass().getField("value"); + return (Serializable) f.get(has); + } + } + catch (Exception ex) + { + return new BAD_OPERATION("Value type expected"); + } + } + + /** {@inheritDoc} */ + public Any extract_any() + throws BAD_OPERATION + { + check(TCKind._tk_any); + return ((AnyHolder) has).value; + } + + /** {@inheritDoc} */ + public boolean extract_boolean() + throws BAD_OPERATION + { + check(TCKind._tk_boolean); + return ((BooleanHolder) has).value; + } + + /** {@inheritDoc} */ + public char extract_char() + throws BAD_OPERATION + { + check(TCKind._tk_char); + return ((CharHolder) has).value; + } + + /** {@inheritDoc} */ + public double extract_double() + throws BAD_OPERATION + { + check(TCKind._tk_double); + return ((DoubleHolder) has).value; + } + + /** + * Extract the previously inserted CORBA <code>fixed</code>/ + * @return the previously inserted value. + * + * @throws org.omg.CORBA.BAD_OPERATION if the holder contains something + * else than BigDecimal. + */ + public BigDecimal extract_fixed() + throws org.omg.CORBA.BAD_OPERATION + { + check(TCKind._tk_fixed); + return ((FixedHolder) has).value; + } + + /** {@inheritDoc} */ + public float extract_float() + throws BAD_OPERATION + { + check(TCKind._tk_float); + return ((FloatHolder) has).value; + } + + /** {@inheritDoc} */ + public int extract_long() + throws BAD_OPERATION + { + // CORBA long = java int. + check(TCKind._tk_long); + return ((IntHolder) has).value; + } + + /** {@inheritDoc} */ + public long extract_longlong() + throws BAD_OPERATION + { + check(TCKind._tk_longlong); + return ((LongHolder) has).value; + } + + /** {@inheritDoc} */ + public byte extract_octet() + throws BAD_OPERATION + { + // ShortHolder holds also octets. + check(TCKind._tk_octet); + return (byte) ((OctetHolder) has).value; + } + + /** {@inheritDoc} */ + public short extract_short() + throws BAD_OPERATION + { + check(TCKind._tk_short); + return ((ShortHolder) has).value; + } + + /** {@inheritDoc} */ + public String extract_string() + throws BAD_OPERATION + { + check(TCKind._tk_string); + return ((StringHolder) has).value; + } + + /** {@inheritDoc} */ + public int extract_ulong() + throws BAD_OPERATION + { + // IntHolder also holds ulongs. + check(TCKind._tk_ulong); + return ((IntHolder) has).value; + } + + /** {@inheritDoc} */ + public long extract_ulonglong() + throws BAD_OPERATION + { + // LongHolder also holds ulonglong + check(TCKind._tk_ulonglong); + return ((LongHolder) has).value; + } + + /** {@inheritDoc} */ + public short extract_ushort() + throws BAD_OPERATION + { + // ShortHolder also holds ushorts. + check(TCKind._tk_ushort); + return ((ShortHolder) has).value; + } + + /** {@inheritDoc} */ + public char extract_wchar() + throws BAD_OPERATION + { + check(TCKind._tk_wchar); + return ((WCharHolder) has).value; + } + + /** {@inheritDoc} */ + public String extract_wstring() + throws BAD_OPERATION + { + // StringHolder also holds wstrings. + check(TCKind._tk_wstring); + return ((WStringHolder) has).value; + } + + /** + * Inserts the CORBA object and sets the typecode to the given type. + */ + public void insert_Object(org.omg.CORBA.Object x, TypeCode typecode) + { + has = new ObjectHolder(x); + type(typecode); + } + + /** + * Inserts the CORBA object. + */ + public void insert_Object(org.omg.CORBA.Object x) + { + has = new ObjectHolder(x); + } + + /** + * Insert the CORBA Principal. + * This implementation uses direct assignment, so the later + * alterations of that BigDecimal are reflected on the + * content of this {@link Any}. + * + * @deprecated by CORBA 2.2. + */ + public void insert_Principal(Principal x) + { + resetTypes(); + if (has instanceof PrincipalHolder) + ((PrincipalHolder) has).value = x; + else + has = new PrincipalHolder(x); + } + + /** + * Sets the value to the value, encapsulated in this holder. + * This implementation uses direct assignment, so the later + * alterations of that streamable are reflected on the + * content of this {@link Any}. + */ + public void insert_Streamable(Streamable x) + { + resetTypes(); + has = x; + } + + /** + * Insert the typecode into this Any + * @param typecode the typecode to insert. + */ + public void insert_TypeCode(TypeCode typecode) + { + resetTypes(); + if (has instanceof TypeCodeHolder) + ((TypeCodeHolder) has).value = typecode; + else + has = new TypeCodeHolder(typecode); + } + + /** {@inheritDoc} */ + public void insert_Value(Serializable x, TypeCode typecode) + { + type(typecode); + insert_Value(x); + } + + /** {@inheritDoc} */ + public void insert_Value(Serializable x) + { + resetTypes(); + if (has instanceof ValueBaseHolder) + ((ValueBaseHolder) has).value = x; + else + has = new ValueBaseHolder(x); + } + + /** + * Insert another {@link Any} into this {@link Any}. + * This implementation uses direct assignment, so the later + * alterations of that {@link Any} are reflected on the + * content of this {@link Any}. + */ + public void insert_any(Any an_any) + { + resetTypes(); + if (has instanceof AnyHolder) + ((AnyHolder) has).value = an_any; + else + has = new AnyHolder(an_any); + } + + /** {@inheritDoc} */ + public void insert_boolean(boolean x) + { + resetTypes(); + if (has instanceof BooleanHolder) + ((BooleanHolder) has).value = x; + else + has = new BooleanHolder(x); + } + + /** {@inheritDoc} */ + public void insert_char(char x) + { + resetTypes(); + if (has instanceof CharHolder) + ((CharHolder) has).value = x; + else + has = new CharHolder(x); + } + + /** {@inheritDoc} */ + public void insert_double(double x) + { + resetTypes(); + if (has instanceof DoubleHolder) + ((DoubleHolder) has).value = x; + else + has = new DoubleHolder(x); + } + + /** + * Inserts the CORBA <code>fixed</code>, setting the typecode + * explicitly. + * This implementation uses direct assignment, so the later + * alterations of that BigDecimal are reflected on the + * content of this {@link Any}. + */ + public void insert_fixed(BigDecimal x, TypeCode x_typecode) + { + resetTypes(); + insert_fixed(x); + typecode = x_typecode; + } + + /** + * Inserts the CORBA <code>fixed</code>, setting the typecode + * by example of the currently passed value. + * This implementation uses direct assignment, so the later + * alterations of that BigDecimal are reflected on the + * content of this {@link Any}, including the typecode. + */ + public void insert_fixed(BigDecimal x) + { + resetTypes(); + if (has instanceof FixedHolder) + ((FixedHolder) has).value = x; + else + has = new FixedHolder(x); + } + + /** {@inheritDoc} */ + public void insert_float(float x) + { + resetTypes(); + if (has instanceof FloatHolder) + ((FloatHolder) has).value = x; + else + has = new FloatHolder(x); + } + + /** {@inheritDoc} */ + public void insert_long(int x) + { + resetTypes(); + if (has instanceof IntHolder) + ((IntHolder) has).value = x; + else + has = new IntHolder(x); + } + + /** {@inheritDoc} */ + public void insert_longlong(long x) + { + resetTypes(); + if (has instanceof LongHolder) + ((LongHolder) has).value = x; + else + has = new LongHolder(x); + } + + /** {@inheritDoc} */ + public void insert_octet(byte x) + { + resetTypes(); + if (has instanceof OctetHolder) + ((OctetHolder) has).value = x; + else + has = new OctetHolder(x); + } + + /** {@inheritDoc} */ + public void insert_short(short x) + { + resetTypes(); + if (has instanceof ShortHolder) + ((ShortHolder) has).value = x; + else + has = new ShortHolder(x); + } + + /** {@inheritDoc} */ + public void insert_string(String x) + { + resetTypes(); + if (has instanceof StringHolder) + ((StringHolder) has).value = x; + else + has = new StringHolder(x); + + typecode = new stringTypeCode(TCKind.tk_string); + } + + /** {@inheritDoc} */ + public void insert_ulong(int x) + { + resetTypes(); + if (has instanceof IntHolder) + ((IntHolder) has).value = x; + else + has = new IntHolder(x); + xKind = TCKind._tk_ulong; + } + + /** {@inheritDoc} */ + public void insert_ulonglong(long x) + { + resetTypes(); + if (has instanceof LongHolder) + ((LongHolder) has).value = x; + else + has = new LongHolder(x); + xKind = TCKind._tk_ulonglong; + } + + /** {@inheritDoc} */ + public void insert_ushort(short x) + { + resetTypes(); + if (has instanceof ShortHolder) + ((ShortHolder) has).value = x; + else + has = new ShortHolder(x); + xKind = TCKind._tk_ushort; + } + + /** {@inheritDoc} */ + public void insert_wchar(char x) + { + resetTypes(); + if (has instanceof WCharHolder) + ((WCharHolder) has).value = x; + else + has = new WCharHolder(x); + } + + /** {@inheritDoc} */ + public void insert_wstring(String x) + { + resetTypes(); + if (has instanceof WStringHolder) + ((WStringHolder) has).value = x; + else + has = new WStringHolder(x); + } + + /** + * Return the associated orb. + */ + public ORB orb() + { + return orb; + } + + /** + * Read the value of the given type from the given stream. + * + * @param input a stream to read from. + * @param a_type a typecode of the value to read. + */ + public void read_value(org.omg.CORBA.portable.InputStream input, + TypeCode a_type + ) + throws MARSHAL + { + try + { + int kind = a_type.kind().value(); + + // Fixed needs special handling. + if (kind == TCKind._tk_fixed) + { + BigDecimal dec = BigDecimalHelper.read(input, a_type.fixed_scale()); + has = new FixedHolder(dec); + } + else + { + has = holderFactory.createHolder(a_type); + if (has == null) + { + // Use the Universal Holder that reads till the end of stream. + // This works with the extract/insert pair of the typical + // Helper. + cdrBufOutput buffer = new cdrBufOutput(); + buffer.setOrb(orb); + has = new universalHolder(buffer); + } + } + type(a_type); + has._read(input); + } + catch (BadKind ex) + { + throw new MARSHAL("Bad kind: " + ex.getMessage()); + } + catch (IOException ex) + { + throw new MARSHAL("IO exception: " + ex.getMessage()); + } + } + + /** {@inheritDoc} */ + public TypeCode type() + { + if (typecode != null) + return typecode; + else if (xKind >= 0) + { + typecode = new primitiveTypeCode(TCKind.from_int(xKind)); + return typecode; + } + else + return has != null ? has._type() : nullType; + } + + /** + * Explicitly set the typecode of the value to the given type. + * + * @param valueTypeCode the typecode of the value. + */ + public void type(TypeCode valueTypeCode) + { + xKind = valueTypeCode.kind().value(); + typecode = valueTypeCode; + } + + /** {@inheritDoc} */ + public void write_value(org.omg.CORBA.portable.OutputStream output) + { + if (has != null) + has._write(output); + } + + /** + * Check if the current value if the value of the given kind. + * @param kind a kind to check. + * @throws BAD_OPERATION if the value is not set of is different kind. + */ + protected void check(int kind) + throws BAD_OPERATION + { + if (has == null) + throw new BAD_OPERATION("value not set"); + + if (xKind >= 0) + { + if (xKind != kind) + throw new BAD_OPERATION("Extracting " + typeNamer.nameIt(kind) + + " when stored " + typeNamer.nameIt(xKind) + ); + } + else + { + if (type().kind().value() != kind) + throw new BAD_OPERATION("Extracting " + typeNamer.nameIt(kind) + + " stored " + typeNamer.nameIt(type()) + ); + } + } + + /** + * Clear the additional type information before reusing this instance. + */ + private final void resetTypes() + { + typecode = null; + xKind = -1; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/gnuCodecFactory.java b/libjava/classpath/gnu/CORBA/gnuCodecFactory.java new file mode 100644 index 00000000000..8b71baf149c --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuCodecFactory.java @@ -0,0 +1,95 @@ +/* gnuCodecFactory.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.*; +import org.omg.CORBA.LocalObject; +import org.omg.IOP.*; +import org.omg.IOP.Codec; +import org.omg.IOP.CodecFactory; +import org.omg.IOP.CodecFactoryPackage.UnknownEncoding; +import org.omg.IOP.Encoding; + +/** + * A simple implementation of the Codec factory, able to return the + * standard Codec's. Only ENCODING_CDR_ENCAPS encoding is supported. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class gnuCodecFactory + extends LocalObject + implements CodecFactory +{ + /** + * The associated ORB. + */ + private final ORB orb; + + /** + * Create a new instance of the this factory, associated with the given ORB. + */ + public gnuCodecFactory(ORB an_orb) + { + orb = an_orb; + } + + /** + * Creates the Codec for the given encoding. + * + * @param for_encoding the encoding for that the Codec must be created. + * + * @return the suitable Codec. + * + * @throws UnknownEncoding if the encoding is not a ENCODING_CDR_ENCAPS. + */ + public Codec create_codec(Encoding for_encoding) + throws UnknownEncoding + { + if (for_encoding.format != ENCODING_CDR_ENCAPS.value) + throw new UnknownEncoding("Only ENCODING_CDR_ENCAPS is " + + "supported by this factory." + ); + + return new cdrEncapsCodec(orb, + new Version(for_encoding.major_version, + for_encoding.minor_version + ) + ); + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/gnuContext.java b/libjava/classpath/gnu/CORBA/gnuContext.java new file mode 100644 index 00000000000..baa9fc80482 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuContext.java @@ -0,0 +1,202 @@ +/* gnuContext.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.omg.CORBA.Any; +import org.omg.CORBA.Bounds; +import org.omg.CORBA.CTX_RESTRICT_SCOPE; +import org.omg.CORBA.Context; +import org.omg.CORBA.NVList; + +/** + * The working implementation of the {@link org.omg.CORBA.Context}. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuContext + extends Context +{ + /** + * The parent context. + */ + Context parent; + + /** + * The collection to store the context properties. + */ + Map properties = new Hashtable(); + + /** + * The name of this context. + */ + String name; + + /** + * Creates the new context with the given name and parent. + * + * @param a_name a name of the new context. + * @param a_parent a parent, used to resolve the missing references. + */ + public gnuContext(String a_name, Context a_parent) + { + name = a_name; + parent = a_parent; + } + + /** {@inheritDoc} */ + public String context_name() + { + return name; + } + + /** {@inheritDoc} */ + public Context create_child(String child) + { + return new gnuContext(child, this); + } + + /** {@inheritDoc} */ + public void delete_values(String property) + { + boolean starts = false; + if (property.endsWith("*")) + { + starts = true; + property = property.substring(0, property.length() - 1); + } + + Set keys = properties.keySet(); + + Iterator iter = keys.iterator(); + while (iter.hasNext()) + { + String key = (String) iter.next(); + if ((starts && key.startsWith(property)) || + (!starts && key.equals(property)) + ) + iter.remove(); + } + } + + /** {@inheritDoc} */ + public NVList get_values(String start_scope, int flags, String pattern) + { + if (start_scope != null) + { + Context c = this; + while (c != null && !c.context_name().equals(start_scope)) + c = c.parent(); + if (c == null) + return new gnuNVList(); + } + + try + { + gnuNVList rt = new gnuNVList(); + + boolean starts = false; + if (pattern.endsWith("*")) + { + starts = true; + pattern = pattern.substring(0, pattern.length() - 1); + } + + Set keys = properties.keySet(); + + Iterator iter = keys.iterator(); + while (iter.hasNext()) + { + String key = (String) iter.next(); + if ((starts && key.startsWith(pattern)) || + (!starts && key.equals(pattern)) + ) + { + rt.add_value(key, (Any) properties.get(key), 0); + } + } + + if ((flags & CTX_RESTRICT_SCOPE.value) == 0 && parent != null) + { + NVList par = parent.get_values(start_scope, flags, pattern); + for (int i = 0; i < par.count(); i++) + { + rt.list.add(par.item(i)); + } + } + + return rt; + } + catch (Bounds ex) + { + throw new Error("Report this bug."); + } + } + + /** {@inheritDoc} */ + public Context parent() + { + return parent; + } + + /** {@inheritDoc} */ + public void set_one_value(String name, Any value) + { + properties.put(name, value); + } + + /** {@inheritDoc} */ + public void set_values(NVList values) + { + try + { + for (int i = 0; i < values.count(); i++) + { + properties.put(values.item(i).name(), values.item(i).value()); + } + } + catch (Bounds ex) + { + throw new Error("Please report this bug."); + } + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuContextList.java b/libjava/classpath/gnu/CORBA/gnuContextList.java new file mode 100644 index 00000000000..2a26437debe --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuContextList.java @@ -0,0 +1,83 @@ +/* gnuContextList.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.util.ArrayList; + +import org.omg.CORBA.Bounds; +import org.omg.CORBA.ContextList; + +/** + * The working implementation of the {@link org.omg.CORBA.ContextList}. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuContextList + extends ContextList +{ + /** + * The collection, holding the actual list of strings. + */ + corbaArrayList strings = new corbaArrayList(); + + /** {@inheritDoc} */ + public void add(String name) + { + strings.add(name); + } + + /** {@inheritDoc} */ + public int count() + { + return strings.size(); + } + + /** {@inheritDoc} */ + public String item(int at) + throws Bounds + { + return (String) strings.item(at); + } + + /** {@inheritDoc} */ + public void remove(int at) + throws Bounds + { + strings.drop(at); + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuEnvironment.java b/libjava/classpath/gnu/CORBA/gnuEnvironment.java new file mode 100644 index 00000000000..ba02e3b194a --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuEnvironment.java @@ -0,0 +1,72 @@ +/* gnuEnvironment.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.Environment; + +/** + * The implementation of the exception container ("Environment"). + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuEnvironment + extends Environment +{ + /** + * The stored exception. + */ + protected Exception exception; + + /** {@inheritDoc} */ + public void clear() + { + exception = null; + } + + /** {@inheritDoc} */ + public void exception(Exception except) + { + exception = except; + } + + /** {@inheritDoc} */ + public Exception exception() + { + return exception; + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuExceptionList.java b/libjava/classpath/gnu/CORBA/gnuExceptionList.java new file mode 100644 index 00000000000..b684ec928cf --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuExceptionList.java @@ -0,0 +1,84 @@ +/* gnuExceptionList.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import java.util.ArrayList; + +import org.omg.CORBA.Bounds; +import org.omg.CORBA.ExceptionList; +import org.omg.CORBA.TypeCode; + +/** + * The implementation of the list of type codes for exceptions. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org). + */ +public class gnuExceptionList + extends ExceptionList +{ + /** + * A list to store the objects. + */ + protected corbaArrayList list = new corbaArrayList(); + + /** {@inheritDoc} */ + public void add(TypeCode an_exception) + { + list.add(an_exception); + } + + /** {@inheritDoc} */ + public int count() + { + return list.size(); + } + + /** {@inheritDoc} */ + public TypeCode item(int at) + throws Bounds + { + return (TypeCode) list.item(at); + } + + /** {@inheritDoc} */ + public void remove(int at) + throws Bounds + { + list.drop(at); + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuNVList.java b/libjava/classpath/gnu/CORBA/gnuNVList.java new file mode 100644 index 00000000000..e436c332caa --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuNVList.java @@ -0,0 +1,127 @@ +/* gnuNVList.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.Any; +import org.omg.CORBA.Bounds; +import org.omg.CORBA.NVList; +import org.omg.CORBA.NamedValue; + +/** + * The implementation of {@link NVList}. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuNVList + extends NVList +{ + /** + * The list of the named values. + */ + protected corbaArrayList list; + + /** + * Creates the list with the default initial size. + */ + public gnuNVList() + { + list = new corbaArrayList(); + } + + /** + * Creates the list with the given initial size. + */ + public gnuNVList(int initial_size) + { + list = new corbaArrayList(initial_size); + } + + /** {@inheritDoc} */ + public NamedValue add(int a_flags) + { + return add_value(null, new gnuAny(), a_flags); + } + + /** {@inheritDoc} */ + public NamedValue add_item(String a_name, int a_flags) + { + return add_value(a_name, new gnuAny(), a_flags); + } + + /** {@inheritDoc} */ + public NamedValue add_value(String a_name, Any a_value, int a_flags) + { + gnuNamedValue n = new gnuNamedValue(); + n.setName(a_name); + n.setValue(a_value); + n.setFlags(a_flags); + list.add(n); + return n; + } + + /** + * Add the given named value to the list directly. + * + * @param value the named vaue to add. + */ + public void add(NamedValue value) + { + list.add(value); + } + + + /** {@inheritDoc} */ + public int count() + { + return list.size(); + } + + /** {@inheritDoc} */ + public NamedValue item(int at) + throws Bounds + { + return (NamedValue) list.item(at); + } + + /** {@inheritDoc} */ + public void remove(int at) + throws Bounds + { + list.drop(at); + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuNamedValue.java b/libjava/classpath/gnu/CORBA/gnuNamedValue.java new file mode 100644 index 00000000000..6e3c271c9a3 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuNamedValue.java @@ -0,0 +1,112 @@ +/* gnuNamedValue.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.Any; +import org.omg.CORBA.NamedValue; + +/** + * The implementation of the named value. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuNamedValue + extends NamedValue +{ + /** + * The named value value. + */ + private Any m_value = new gnuAny(); + + /** + * The named value name. + */ + private String m_name; + + /** + * The named value flags. + */ + private int m_flags; + + /** + * Set the flags, the normally expected values are + * {@link org.omg.CORBA.ARG_IN#value}, + * {@link org.omg.CORBA.ARG_OUT#value} and + * {@link org.omg.CORBA.ARG_INOUT#value}. + */ + public void setFlags(int flags) + { + m_flags = flags; + } + + /** + * Set the name of the value. + * @param name the name of this value + */ + public void setName(String name) + { + m_name = name; + } + + /** + * Set the value of the value. + * @param value the value of this object. + */ + public void setValue(Any value) + { + m_value = value; + } + + /** {@inheritDoc} */ + public int flags() + { + return m_flags; + } + + /** {@inheritDoc} */ + public String name() + { + return m_name; + } + + /** {@inheritDoc} */ + public Any value() + { + return m_value; + } +} diff --git a/libjava/classpath/gnu/CORBA/gnuRequest.java b/libjava/classpath/gnu/CORBA/gnuRequest.java new file mode 100644 index 00000000000..a47410e0bc5 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/gnuRequest.java @@ -0,0 +1,1008 @@ +/* gnuRequest.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.CDR.cdrBufOutput; +import gnu.CORBA.GIOP.CloseMessage; +import gnu.CORBA.GIOP.MessageHeader; +import gnu.CORBA.GIOP.ReplyHeader; +import gnu.CORBA.GIOP.RequestHeader; +import gnu.CORBA.GIOP.cxCodeSet; + +import org.omg.CORBA.ARG_IN; +import org.omg.CORBA.ARG_INOUT; +import org.omg.CORBA.ARG_OUT; +import org.omg.CORBA.Any; +import org.omg.CORBA.BAD_INV_ORDER; +import org.omg.CORBA.Bounds; +import org.omg.CORBA.Context; +import org.omg.CORBA.ContextList; +import org.omg.CORBA.Environment; +import org.omg.CORBA.ExceptionList; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NO_RESOURCES; +import org.omg.CORBA.NVList; +import org.omg.CORBA.NamedValue; +import org.omg.CORBA.ORB; +import org.omg.CORBA.Request; +import org.omg.CORBA.SystemException; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.UnknownUserException; +import org.omg.CORBA.UserException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import java.net.BindException; +import java.net.Socket; + +/** + * The implementation of the CORBA request. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class gnuRequest + extends Request + implements Cloneable +{ + /** + * The maximal supported GIOP version. + */ + public static Version MAX_SUPPORTED = new Version(1, 2); + + /** + * The initial pause that the Request makes when + * the required port is not available. + */ + public static int PAUSE_INITIAL = 50; + + /** + * The number of repretetive attempts to get a required + * port, if it is not immediately available. + */ + public static int PAUSE_STEPS = 12; + + /** + * The maximal pausing interval between two repetetive attempts. + * The interval doubles after each unsuccessful attempt, but + * will not exceed this value. + */ + public static int PAUSE_MAX = 1000; + + /** + * The empty byte array. + */ + private static final binaryReply EMPTY = + new binaryReply(null, new MessageHeader(), new byte[ 0 ]); + + /** + * The context holder for methods ctx(Context) and ctx(). + */ + protected Context m_context; + + /** + * The context list for method contexts(). + */ + protected ContextList m_context_list; + + /** + * The request environment for holding the exception + * the has possibly been thrown by the method being invoked. + */ + protected Environment m_environment = new gnuEnvironment(); + + /** + * The list of all exceptions that can be thrown by the + * method being invoked. + */ + protected ExceptionList m_exceptions = new gnuExceptionList(); + + /** + * The result, returned by the invoked method (function). + */ + protected NamedValue m_result = new gnuNamedValue(); + + /** + * The invocation target. + */ + protected org.omg.CORBA.Object m_target; + + /** + * The name of the method being invoked. + */ + protected String m_operation; + + /** + * The flag, indicating that the request has been sent + * and the result is already received. + */ + protected boolean complete; + + /** + * The flag, indicating that the response to this request must be + * ignored (used with {@link #send_oneway()}). + */ + protected boolean oneWay; + + /** + * The flag, indicating that the request has been sent + * and no result is yet received. + */ + protected boolean running; + + /** + * The request arguments. + */ + protected gnuNVList m_args = new gnuNVList(); + + /** + * The request arguments in the case when they are directly written into + * the parameter buffer. + */ + protected streamRequest m_parameter_buffer; + + /** + * The IOR of the target. + */ + private IOR ior; + + /** + * The ORB of the target. + */ + private ORB orb; + + /** + * The encoding, used to send the message. + * + * The default encoding is inherited from the set IOR + * (that string reference can be encoded in either Big or + * Little endian). If the IOR encoding is not known + * (for example, by obtaining the reference from the naming + * service), the Big Endian is used. + */ + private boolean Big_endian = true; + + /** + * Set the IOR data, sufficient to find the invocation target. + * This also sets default endian encoding for invocations. + * + * @see IOR.parse(String) + */ + public void setIor(IOR an_ior) + { + ior = an_ior; + setBigEndian(ior.Big_Endian); + } + + /** + * Get the IOR data, sufficient to find the invocation target. + * + * @return the IOR data. + */ + public IOR getIor() + { + return ior; + } + + /** + * Set the ORB, related to the invocation target. + */ + public void setORB(ORB an_orb) + { + orb = an_orb; + } + + /** + * Set the encoding that will be used to send the message. + * The default encoding is inherited from the set IOR + * (that string reference can be encoded in either Big or + * Little endian). If the IOR encoding is not known + * (for example, by obtaining the reference from the naming + * service), the Big Endian is used. + * + * @param use_big_endian true to use the Big Endian, false + * to use the Little Endian encoding. + */ + public void setBigEndian(boolean use_big_endian) + { + Big_endian = use_big_endian; + } + + /** + * The the method name to invoke. + * + * @param operation the method name. + */ + public void setOperation(String operation) + { + m_operation = operation; + } + + /** + * Get the parameter stream, where the invocation arguments should + * be written if they are written into the stream directly. + */ + public streamRequest getParameterStream() + { + m_parameter_buffer = new streamRequest(); + m_parameter_buffer.request = this; + m_parameter_buffer.setVersion(ior.Internet.version); + m_parameter_buffer.setCodeSet(cxCodeSet.negotiate(ior.CodeSets)); + m_parameter_buffer.setOrb(orb); + m_parameter_buffer.setBigEndian(Big_endian); + return m_parameter_buffer; + } + + /** + * Creates a shallow copy of this request. + */ + public gnuRequest Clone() + { + try + { + return (gnuRequest) clone(); + } + catch (CloneNotSupportedException ex) + { + throw new Unexpected(ex); + } + } + + /** {@inheritDoc} */ + public Any add_in_arg() + { + gnuNamedValue v = new gnuNamedValue(); + v.setFlags(ARG_IN.value); + m_args.add(v); + return v.value(); + } + + /** {@inheritDoc} */ + public Any add_inout_arg() + { + gnuNamedValue v = new gnuNamedValue(); + v.setFlags(ARG_INOUT.value); + m_args.add(v); + return v.value(); + } + + /** {@inheritDoc} */ + public Any add_named_in_arg(String name) + { + gnuNamedValue v = new gnuNamedValue(); + v.setFlags(ARG_IN.value); + v.setName(name); + m_args.add(v); + return v.value(); + } + + /** {@inheritDoc} */ + public Any add_named_inout_arg(String name) + { + gnuNamedValue v = new gnuNamedValue(); + v.setFlags(ARG_INOUT.value); + v.setName(name); + m_args.add(v); + return v.value(); + } + + /** {@inheritDoc} */ + public Any add_named_out_arg(String name) + { + gnuNamedValue v = new gnuNamedValue(); + v.setFlags(ARG_OUT.value); + v.setName(name); + m_args.add(v); + return v.value(); + } + + /** {@inheritDoc} */ + public Any add_out_arg() + { + gnuNamedValue v = new gnuNamedValue(); + v.setFlags(ARG_OUT.value); + m_args.add(v); + return v.value(); + } + + /** {@inheritDoc} */ + public NVList arguments() + { + return m_args; + } + + /** {@inheritDoc} */ + public ContextList contexts() + { + return m_context_list; + } + + /** {@inheritDoc} */ + public Context ctx() + { + return m_context; + } + + /** {@inheritDoc} */ + public void ctx(Context a_context) + { + m_context = a_context; + } + + /** {@inheritDoc} */ + public Environment env() + { + return m_environment; + } + + /** {@inheritDoc} */ + public ExceptionList exceptions() + { + return m_exceptions; + } + + /** {@inheritDoc} */ + public void get_response() + throws org.omg.CORBA.WrongTransaction + { + /** + * The response is ready after it is received. + * FIXME implement context checks and any other functionality, + * if required. + */ + } + + /** + * Submit the request, suspending the current thread until the + * answer is received. + * + * This implementation requires to set the IOR property + * ({@link #setIOR(IOR)} before calling this method. + * + * @throws BAD_INV_ORDER, minor code 0, if the IOR has not been + * previously set. + * + * @throws SystemException if this exception has been thrown on + * remote side. The exact exception type and the minor code are + * the same as they have been for the exception, thrown on remoted + * side. + */ + public synchronized void invoke() + throws BAD_INV_ORDER + { + waitWhileBusy(); + complete = false; + running = true; + + if (ior == null) + throw new BAD_INV_ORDER("Set IOR property first"); + + try + { + p_invoke(); + } + finally + { + running = false; + complete = true; + } + } + + /** {@inheritDoc} */ + public String operation() + { + return m_operation; + } + + /** + * Get the orb, related to the invocation target. + */ + public ORB orb() + { + return orb; + } + + /** {@inheritDoc} */ + public boolean poll_response() + { + return complete && !running; + } + + /** {@inheritDoc} */ + public NamedValue result() + { + return m_result; + } + + /** {@inheritDoc} + * + */ + public Any return_value() + { + return m_result.value(); + } + + /** {@inheritDoc} */ + public synchronized void send_deferred() + { + waitWhileBusy(); + new Thread() + { + public void run() + { + invoke(); + } + }.start(); + } + + /** + * Send a request and forget about it, not waiting for a response. + * This can be done also for methods that normally are expected + * to return some values. + * + * TODO It is generally recommended to reuse the threads. Reuse? + */ + public void send_oneway() + { + final gnuRequest cloned = Clone(); + cloned.oneWay = true; + + new Thread() + { + public void run() + { + cloned.invoke(); + } + }.start(); + } + + /** + * Set the argument list. + * This field is initialised as empty non null instance by default, + * so the method is only used in cases when the direct replacement + * is desired. + * + * @param a_args the argument list. + */ + public void set_args(NVList a_args) + { + if (a_args instanceof gnuNVList) + m_args = (gnuNVList) a_args; + else + { + try + { + // In case if this is another implementation of the NVList. + m_args.list.clear(); + for (int i = 0; i < a_args.count(); i++) + { + m_args.add(a_args.item(i)); + } + } + catch (Bounds ex) + { + Unexpected.error(ex); + } + } + } + + /** + * Set the context list that is later returned by the + * method {@link #contexts()}. + * + * @param a_context_list a new context list. + */ + public void set_context_list(ContextList a_context_list) + { + m_context_list = a_context_list; + } + + /** + * Set the exception container. + * This field is initialised as empty non null instance by default, + * so the method is only used in cases when the direct replacement + * is desired. + * + * @param a_environment the new exception container. + */ + public void set_environment(Environment a_environment) + { + m_environment = a_environment; + } + + /** + * Set the list of exceptions. + * This field is initialised as empty non null instance by default, + * so the method is only used in cases when the direct replacement + * is desired. + * + * @param a_exceptions a list of exceptions. + */ + public void set_exceptions(ExceptionList a_exceptions) + { + m_exceptions = a_exceptions; + } + + /** + * Set the operation name. + * + * @param a_operation the operation name. + */ + public void set_operation(String a_operation) + { + m_operation = a_operation; + } + + /** + * Set the named value, returned as result. + * This field is initialised as empty non null instance by default, + * so the method is only used in cases when the direct replacement + * is desired. + * + * @param a_result the result keeper. + */ + public void set_result(NamedValue a_result) + { + m_result = a_result; + } + + /** + * Set the type of the named value, returned as a result. + * Instantiates a new instance of the result value. + */ + public void set_return_type(TypeCode returns) + { + if (m_result == null || !returns.equal(m_result.value().type())) + { + m_result = new gnuNamedValue(); + m_result.value().type(returns); + } + } + + /** + * Set the invocation target. + * + * @param a_target the CORBA object for that the method will be invoked. + */ + public void set_target(org.omg.CORBA.Object a_target) + { + m_target = a_target; + } + + /** + * Do the actual invocation. + * This implementation requires to set the IOR property + * ({@link #setIOR(IOR)} before calling this method. + * + * @throws BAD_INV_ORDER, minor code 0, if the IOR has not been + * previously set or if the direct argument addition is mixed with + * the direct argument writing into the output stream. + * + * @return the server response in binary form. + */ + public synchronized binaryReply submit() + { + gnu.CORBA.GIOP.MessageHeader header = new gnu.CORBA.GIOP.MessageHeader(); + + header.setBigEndian(Big_endian); + + // The byte order will be Big Endian by default. + header.message_type = gnu.CORBA.GIOP.MessageHeader.REQUEST; + header.version = useVersion(ior.Internet.version); + + RequestHeader rh = header.create_request_header(); + + rh.object_key = ior.key; + rh.operation = m_operation; + + // Prepare the submission. + cdrBufOutput request_part = new cdrBufOutput(); + + request_part.setOffset(header.getHeaderSize()); + request_part.setVersion(header.version); + request_part.setCodeSet(cxCodeSet.negotiate(ior.CodeSets)); + request_part.setOrb(orb); + request_part.setBigEndian(header.isBigEndian()); + + // This also sets the stream encoding to the encoding, specified + // in the header. + rh.write(request_part); + + if (m_args != null && m_args.count() > 0) + { + write_parameters(header, request_part); + + if (m_parameter_buffer != null) + throw new BAD_INV_ORDER("Please either add parameters or " + + "write them into stream, but not both " + + "at once." + ); + } + + if (m_parameter_buffer != null) + { + write_parameter_buffer(header, request_part); + } + + // Now the message size is available. + header.message_size = request_part.buffer.size(); + + Socket socket = null; + + java.lang.Object key = ior.Internet.host + ":" + ior.Internet.port; + + synchronized (SocketRepository.class) + { + socket = SocketRepository.get_socket(key); + } + + try + { + long pause = PAUSE_INITIAL; + + if (socket == null) + { + // The BindException may be thrown under very heavy parallel + // load. For some time, just wait, exceptiong the socket to free. + Open: + for (int i = 0; i < PAUSE_STEPS; i++) + { + try + { + socket = new Socket(ior.Internet.host, ior.Internet.port); + break Open; + } + catch (BindException ex) + { + try + { + // Expecting to free a socket via finaliser. + System.gc(); + Thread.sleep(pause); + pause = pause * 2; + if (pause > PAUSE_MAX) + pause = PAUSE_MAX; + } + catch (InterruptedException iex) + { + } + } + } + } + + if (socket == null) + throw new NO_RESOURCES(ior.Internet.host + ":" + ior.Internet.port + + " in use" + ); + socket.setKeepAlive(true); + + OutputStream socketOutput = socket.getOutputStream(); + + // Write the message header. + header.write(socketOutput); + + // Write the request header and parameters (if present). + request_part.buffer.writeTo(socketOutput); + + socketOutput.flush(); + if (!socket.isClosed()) + { + MessageHeader response_header = new MessageHeader(); + InputStream socketInput = socket.getInputStream(); + response_header.read(socketInput); + + byte[] r = new byte[ response_header.message_size ]; + int n = 0; + reading: + while (n < r.length) + { + n += socketInput.read(r, n, r.length - n); + } + return new binaryReply(orb, response_header, r); + } + else + return EMPTY; + } + catch (IOException io_ex) + { + MARSHAL m = + new MARSHAL("Unable to open a socket at " + ior.Internet.host + ":" + + ior.Internet.port + ); + m.initCause(io_ex); + throw m; + } + finally + { + try + { + if (socket != null && !socket.isClosed()) + { + socket.setSoTimeout(Functional_ORB.TANDEM_REQUESTS); + SocketRepository.put_socket(key, socket); + } + } + catch (IOException scx) + { + InternalError ierr = new InternalError(); + ierr.initCause(scx); + throw ierr; + } + } + } + + /** {@inheritDoc} */ + public org.omg.CORBA.Object target() + { + return m_target; + } + + /** + * Get the used version. Normally, it is better to respond using the + * same version as it is specified in IOR, but not above the maximal + * supported version. + */ + public Version useVersion(Version desired) + { + if (desired.until_inclusive(MAX_SUPPORTED.major, MAX_SUPPORTED.minor)) + return desired; + else + return MAX_SUPPORTED; + } + + /** + * Wait while the response to request, submitted using + * {@link #send_deferred()} or {@link #invoke()} (from other thread) + * is returned. + * + * FIXME It is possible to rewrite this using + * Object.wait() and Object.notify(), but be sure to prepare the test + * as well. + */ + public synchronized void waitWhileBusy() + { + // Waiting constants. + long wait = 10; + long increment = 2; + long max = 5000; + + while (running) + { + try + { + Thread.sleep(wait); + if (wait < max) + wait = wait * increment; + } + catch (InterruptedException ex) + { + } + } + } + + /** + * Do actual invocation. This method recursively calls itself if + * the redirection is detected. + */ + private void p_invoke() + throws SystemException + { + binaryReply response = submit(); + + ReplyHeader rh = response.header.create_reply_header(); + cdrBufInput input = response.getStream(); + input.setOrb(orb); + + rh.read(input); + + // The stream must be aligned sinve v1.2, but only once. + boolean align = response.header.version.since_inclusive(1, 2); + + boolean moved_permanently = false; + + switch (rh.reply_status) + { + case ReplyHeader.NO_EXCEPTION : + + NamedValue arg; + + // Read return value, if set. + if (m_result != null) + { + if (align) + { + input.align(8); + align = false; + } + m_result.value().read_value(input, m_result.value().type()); + } + + // Read returned parameters, if set. + if (m_args != null) + for (int i = 0; i < m_args.count(); i++) + { + try + { + arg = m_args.item(i); + + // Both ARG_INOUT and ARG_OUT have this binary flag set. + if ((arg.flags() & ARG_OUT.value) != 0) + { + if (align) + { + input.align(8); + align = false; + } + + arg.value().read_value(input, arg.value().type()); + } + } + catch (Bounds ex) + { + Unexpected.error(ex); + } + } + + break; + + case ReplyHeader.SYSTEM_EXCEPTION : + if (align) + { + input.align(8); + align = false; + } + + SystemException exception = ObjectCreator.readSystemException(input); + + m_environment.exception(exception); + + throw exception; + + case ReplyHeader.USER_EXCEPTION : + if (align) + { + input.align(8); + align = false; + } + + // Prepare an Any that will hold the exception. + gnuAny exc = new gnuAny(); + + exc.insert_Streamable(new streamReadyHolder(input)); + + UnknownUserException unuex = new UnknownUserException(exc); + m_environment.exception(unuex); + + break; + + case ReplyHeader.LOCATION_FORWARD_PERM : + case ReplyHeader.LOCATION_FORWARD : + if (response.header.version.since_inclusive(1, 2)) + input.align(8); + + IOR forwarded = new IOR(); + try + { + forwarded._read_no_endian(input); + } + catch (IOException ex) + { + throw new MARSHAL(ex + " while reading the forwarding info"); + } + + setIor(forwarded); + + // Repeat with the forwarded information. + p_invoke(); + return; + + default : + throw new MARSHAL("Unknow reply status: " + rh.reply_status); + } + } + + /** + * Write the operation parameters. + * + * @param header the message header + * @param request_part the stream to write parameters into + * + * @throws MARSHAL if the attempt to write the parameters has failde. + */ + private void write_parameter_buffer(MessageHeader header, + cdrBufOutput request_part + ) + throws MARSHAL + { + try + { + if (header.version.since_inclusive(1, 2)) + { + request_part.align(8); + } + m_parameter_buffer.buffer.writeTo(request_part); + } + catch (IOException ex) + { + throw new MARSHAL("Unable to write method arguments to CDR output."); + } + } + + /** + * Write the operation parameters. + * + * @param header the message header + * @param request_part the stream to write parameters into + * + * @throws MARSHAL if the attempt to write the parameters has failde. + */ + private void write_parameters(MessageHeader header, cdrBufOutput request_part) + throws MARSHAL + { + // Align after 1.2, but only once. + boolean align = header.version.since_inclusive(1, 2); + NamedValue para; + + try + { + // Write parameters now. + for (int i = 0; i < m_args.count(); i++) + { + para = m_args.item(i); + + //This bit is set both for ARG_IN and ARG_INOUT + if ((para.flags() & ARG_IN.value) != 0) + { + if (align) + { + request_part.align(8); + align = false; + } + para.value().write_value(request_part); + } + } + } + catch (Bounds ex) + { + throw new MARSHAL("Unable to write method arguments to CDR output."); + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/holderFactory.java b/libjava/classpath/gnu/CORBA/holderFactory.java new file mode 100644 index 00000000000..177797a9c0c --- /dev/null +++ b/libjava/classpath/gnu/CORBA/holderFactory.java @@ -0,0 +1,183 @@ +/* holderFactory.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.AnyHolder; +import org.omg.CORBA.AnySeqHolder; +import org.omg.CORBA.BooleanHolder; +import org.omg.CORBA.BooleanSeqHolder; +import org.omg.CORBA.CharHolder; +import org.omg.CORBA.CharSeqHolder; +import org.omg.CORBA.DoubleHolder; +import org.omg.CORBA.DoubleSeqHolder; +import org.omg.CORBA.FixedHolder; +import org.omg.CORBA.FloatHolder; +import org.omg.CORBA.FloatSeqHolder; +import org.omg.CORBA.IntHolder; +import org.omg.CORBA.LongHolder; +import org.omg.CORBA.LongLongSeqHolder; +import org.omg.CORBA.LongSeqHolder; +import org.omg.CORBA.OctetSeqHolder; +import org.omg.CORBA.PrincipalHolder; +import org.omg.CORBA.ShortHolder; +import org.omg.CORBA.ShortSeqHolder; +import org.omg.CORBA.StringHolder; +import org.omg.CORBA.StringSeqHolder; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodeHolder; +import org.omg.CORBA.ULongLongSeqHolder; +import org.omg.CORBA.ULongSeqHolder; +import org.omg.CORBA.UShortSeqHolder; +import org.omg.CORBA.WCharSeqHolder; +import org.omg.CORBA.WStringSeqHolder; +import org.omg.CORBA.portable.Streamable; + +/** + * Creates the suitable holder for storing the value of the given + * type. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class holderFactory +{ + /** + * The array, sufficiently large to use any {@link TCKind}._tk* constant + * as an index. + */ + private static final Class[] holders; + private static final Class[] seqHolders; + + static + { + holders = new Class[ 32 ]; + holders [ TCKind._tk_Principal ] = PrincipalHolder.class; + holders [ TCKind._tk_TypeCode ] = TypeCodeHolder.class; + holders [ TCKind._tk_any ] = AnyHolder.class; + holders [ TCKind._tk_boolean ] = BooleanHolder.class; + holders [ TCKind._tk_char ] = CharHolder.class; + holders [ TCKind._tk_double ] = DoubleHolder.class; + holders [ TCKind._tk_float ] = FloatHolder.class; + holders [ TCKind._tk_fixed ] = FixedHolder.class; + holders [ TCKind._tk_long ] = IntHolder.class; + holders [ TCKind._tk_longdouble ] = DoubleHolder.class; + holders [ TCKind._tk_longlong ] = LongHolder.class; + holders [ TCKind._tk_octet ] = OctetHolder.class; + holders [ TCKind._tk_short ] = ShortHolder.class; + holders [ TCKind._tk_string ] = StringHolder.class; + holders [ TCKind._tk_ulong ] = IntHolder.class; + holders [ TCKind._tk_ulonglong ] = LongHolder.class; + holders [ TCKind._tk_ushort ] = ShortHolder.class; + holders [ TCKind._tk_wchar ] = WCharHolder.class; + holders [ TCKind._tk_wstring ] = WStringHolder.class; + + seqHolders = new Class[ 32 ]; + + seqHolders [ TCKind._tk_ulonglong ] = ULongLongSeqHolder.class; + seqHolders [ TCKind._tk_short ] = ShortSeqHolder.class; + seqHolders [ TCKind._tk_octet ] = OctetSeqHolder.class; + seqHolders [ TCKind._tk_any ] = AnySeqHolder.class; + seqHolders [ TCKind._tk_long ] = LongSeqHolder.class; + seqHolders [ TCKind._tk_longlong ] = LongLongSeqHolder.class; + seqHolders [ TCKind._tk_float ] = FloatSeqHolder.class; + seqHolders [ TCKind._tk_double ] = DoubleSeqHolder.class; + seqHolders [ TCKind._tk_char ] = CharSeqHolder.class; + seqHolders [ TCKind._tk_boolean ] = BooleanSeqHolder.class; + seqHolders [ TCKind._tk_wchar ] = WCharSeqHolder.class; + seqHolders [ TCKind._tk_ushort ] = UShortSeqHolder.class; + seqHolders [ TCKind._tk_ulong ] = ULongSeqHolder.class; + seqHolders [ TCKind._tk_string ] = StringSeqHolder.class; + seqHolders [ TCKind._tk_wstring ] = WStringSeqHolder.class; + } + + /** + * Create a holder for storing the value of the given built-in type. + * This function returns the defined holders for the built-in primitive + * types and they sequences. + * + * @param t the typecode + * + * @return an instance of the corresponding built-in holder of null + * if no such is defined for this type. The holder is created with a + * parameterless constructor. + */ + public static Streamable createHolder(TypeCode t) + { + try + { + int kind = t.kind().value(); + int componentKind; + + Streamable holder = null; + Streamable component; + + if (kind < holders.length && holders [ kind ] != null) + holder = (Streamable) holders [ kind ].newInstance(); + + if (holder != null) + return holder; + + switch (kind) + { + case TCKind._tk_sequence : + componentKind = t.content_type().kind().value(); + if (componentKind < seqHolders.length) + return (Streamable) seqHolders [ componentKind ].newInstance(); + break; + + default : + break; + } + } + catch (Exception ex) + { + throw new Unexpected(ex); + } + + try + { + Object ox = ObjectCreator.createObject(t.id(), "Holder"); + return (Streamable) ox; + } + catch (Exception ex) + { + return null; + } + } +} diff --git a/libjava/classpath/gnu/CORBA/primitiveArrayTypeCode.java b/libjava/classpath/gnu/CORBA/primitiveArrayTypeCode.java new file mode 100644 index 00000000000..7e2beebc949 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/primitiveArrayTypeCode.java @@ -0,0 +1,265 @@ +/* primitiveArrayTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; + +/** + * A TypeCode for arrays. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class primitiveArrayTypeCode + extends primitiveTypeCode +{ + /** + * The array components. + */ + TypeCode of; + + /** + * The length of the array, must be updated when setting + * a new value. + */ + private int length; + + /** + * Create a primitive array type code, defining the sequence + * {@link TCKind.tk_sequence)} with + * the given member type. + * + * @param array_of the sequence member type. + */ + public primitiveArrayTypeCode(TCKind array_of) + { + super(TCKind.tk_sequence); + of = new primitiveTypeCode(array_of); + } + + /** + * Create a primitive array type code, defining the array, sequence + * or other type with the given member type. + * + * @param this_type the type of this type (normally either + * sequence of array). + * @param array_of the sequence member type. + */ + public primitiveArrayTypeCode(TCKind this_type, TypeCode array_of) + { + super(this_type); + of = array_of; + } + + /** + * Return the array component type. + * @return the array component type + * @throws org.omg.CORBA.TypeCodePackage.BadKind + */ + public TypeCode content_type() + throws org.omg.CORBA.TypeCodePackage.BadKind + { + return of; + } + + /** + * Return true if the other TypeCode defines the array, having elements + * of the same type. The sizes of arrays are not taken into + * consideration. + * + * @param other the other TypeCode + * @return true if <code>other</code> is an array with the same + * component type. + */ + public boolean equal(TypeCode other) + { + try + { + return kind() == other.kind() && + content_type() == other.content_type(); + } + catch (BadKind ex) + { + // Should not be thrown. + return false; + } + } + + /** + * Returns the agreed Id in the form of + * <code>IDL:omg.org/CORBA/ {type name} Seq:1.0</code>. + * + * @return the Id of this TypeCode. + * + * @throws org.omg.CORBA.TypeCodePackage.BadKind if the content type + * is not one of the constants, defined in {@link TCKind}. + * This package class should not be used as TypeCode for the arrays, + * holding the user defined components. + */ + public String id() + throws org.omg.CORBA.TypeCodePackage.BadKind + { + switch (content_type().kind().value()) + { + case TCKind._tk_null : + return "IDL:omg.org/CORBA/NullSeq:1.0"; + + case TCKind._tk_void : + return "IDL:omg.org/CORBA/VoidSeq:1.0"; + + case TCKind._tk_short : + return "IDL:omg.org/CORBA/ShortSeq:1.0"; + + case TCKind._tk_long : + return "IDL:omg.org/CORBA/LongSeq:1.0"; + + case TCKind._tk_ushort : + return "IDL:omg.org/CORBA/UShortSeq:1.0"; + + case TCKind._tk_ulong : + return "IDL:omg.org/CORBA/ULongSeq:1.0"; + + case TCKind._tk_float : + return "IDL:omg.org/CORBA/FloatSeq:1.0"; + + case TCKind._tk_double : + return "IDL:omg.org/CORBA/DoubleSeq:1.0"; + + case TCKind._tk_boolean : + return "IDL:omg.org/CORBA/BooleanSeq:1.0"; + + case TCKind._tk_char : + return "IDL:omg.org/CORBA/CharSeq:1.0"; + + case TCKind._tk_octet : + return "IDL:omg.org/CORBA/OctetSeq:1.0"; + + case TCKind._tk_any : + return "IDL:omg.org/CORBA/AnySeq:1.0"; + + case TCKind._tk_TypeCode : + return "IDL:omg.org/CORBA/TypeCodeSeq:1.0"; + + case TCKind._tk_Principal : + return "IDL:omg.org/CORBA/PrincipalSeq:1.0"; + + case TCKind._tk_objref : + return "IDL:omg.org/CORBA/ObjrefSeq:1.0"; + + case TCKind._tk_struct : + return "IDL:omg.org/CORBA/StructSeq:1.0"; + + case TCKind._tk_union : + return "IDL:omg.org/CORBA/UnionSeq:1.0"; + + case TCKind._tk_enum : + return "IDL:omg.org/CORBA/EnumSeq:1.0"; + + case TCKind._tk_string : + return "IDL:omg.org/CORBA/StringSeq:1.0"; + + case TCKind._tk_sequence : + return "IDL:omg.org/CORBA/SequenceSeq:1.0"; + + case TCKind._tk_array : + return "IDL:omg.org/CORBA/ArraySeq:1.0"; + + case TCKind._tk_alias : + return "IDL:omg.org/CORBA/AliasSeq:1.0"; + + case TCKind._tk_except : + return "IDL:omg.org/CORBA/ExceptSeq:1.0"; + + case TCKind._tk_longlong : + return "IDL:omg.org/CORBA/LongLongSeq:1.0"; + + case TCKind._tk_ulonglong : + return "IDL:omg.org/CORBA/ULongLongSeq:1.0"; + + case TCKind._tk_longdouble : + return "IDL:omg.org/CORBA/LongDoubleSeq:1.0"; + + case TCKind._tk_wchar : + return "IDL:omg.org/CORBA/WCharSeq:1.0"; + + case TCKind._tk_wstring : + return "IDL:omg.org/CORBA/WStringSeq:1.0"; + + case TCKind._tk_fixed : + return "IDL:omg.org/CORBA/FixedSeq:1.0"; + + case TCKind._tk_value : + return "IDL:omg.org/CORBA/ValueSeq:1.0"; + + case TCKind._tk_value_box : + return "IDL:omg.org/CORBA/Value_boxSeq:1.0"; + + case TCKind._tk_native : + return "IDL:omg.org/CORBA/NativeSeq:1.0"; + + case TCKind._tk_abstract_interface : + return "IDL:omg.org/CORBA/Abstract_interfaceSeq:1.0"; + + default : + throw new BadKind(); + } + } + + /** + * Return the array length. + * @return the length of the array. + * @throws org.omg.CORBA.TypeCodePackage.BadKind + */ + public int length() + throws org.omg.CORBA.TypeCodePackage.BadKind + { + return length; + } + + /** + * Sets the array length to the supplied value. + * + * @param l the new length. + */ + public void setLength(int l) + { + this.length = l; + } + +} diff --git a/libjava/classpath/gnu/CORBA/primitiveTypeCode.java b/libjava/classpath/gnu/CORBA/primitiveTypeCode.java new file mode 100644 index 00000000000..1fa5cd09fe0 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/primitiveTypeCode.java @@ -0,0 +1,195 @@ +/* primitiveTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package gnu.CORBA; + +import java.io.Serializable; + +import org.omg.CORBA.Any; +import org.omg.CORBA.IDLEntity; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.TypeCodePackage.Bounds; + +/** + * An information about a primitive CORBA data type + * (boolean, char, wchar, octet and also signed or unsigned short, long, + * long long, float and double). + * This class only implements the methods {@link #kind() } + * and {@link equal() } that are valid for + * all TypeCode kinds. Other methods are implemented in derived + * subclasses. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class primitiveTypeCode + extends TypeCode + implements IDLEntity, Serializable +{ + /** + * The kind of this TypeCode. + */ + protected final TCKind kind; + + public primitiveTypeCode(TCKind a_kind) + { + kind = a_kind; + } + + public TypeCode concrete_base_type() + throws BadKind + { + throw new BadKind(); + } + + public TypeCode content_type() + throws BadKind + { + throw new BadKind(); + } + + public int default_index() + throws BadKind + { + throw new BadKind(); + } + + public TypeCode discriminator_type() + throws BadKind + { + throw new BadKind(); + } + + /** + * Test two types for equality. The default implementation + * returs true of the types of the same kind. + * @param other the other type to compere with + * @return true if the types are interchangeable. + */ + public boolean equal(TypeCode other) + { + return kind() == other.kind(); + } + + public boolean equivalent(TypeCode parm1) + { + throw new NO_IMPLEMENT(); + } + + public short fixed_digits() + throws BadKind + { + throw new BadKind("fixed_digits"); + } + + public short fixed_scale() + throws BadKind + { + throw new BadKind("fixed_scale"); + } + + public TypeCode get_compact_typecode() + { + throw new NO_IMPLEMENT(); + } + + public String id() + throws BadKind + { + throw new BadKind("id"); + } + + /** + * Return the kind of this type code object. + * @return one of the <code>TCKind.t_..</code> fields. + */ + public TCKind kind() + { + return kind; + } + + public int length() + throws BadKind + { + throw new BadKind("length"); + } + + public int member_count() + throws BadKind + { + throw new BadKind("member_count"); + } + + public Any member_label(int index) + throws BadKind, Bounds + { + throw new BadKind("member_label"); + } + + public String member_name(int index) + throws BadKind, Bounds + { + throw new BadKind("member_name"); + } + + public TypeCode member_type(int index) + throws BadKind, Bounds + { + throw new BadKind("member_type"); + } + + public short member_visibility(int index) + throws BadKind, Bounds + { + throw new BadKind("member_visibility"); + } + + public String name() + throws BadKind + { + throw new BadKind("name"); + } + + public short type_modifier() + throws BadKind + { + throw new BadKind("type_modifier"); + } +} diff --git a/libjava/classpath/gnu/CORBA/recordTypeCode.java b/libjava/classpath/gnu/CORBA/recordTypeCode.java new file mode 100644 index 00000000000..8f2ecde7d1a --- /dev/null +++ b/libjava/classpath/gnu/CORBA/recordTypeCode.java @@ -0,0 +1,245 @@ +/* recordTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.Any; +import org.omg.CORBA.StructMember; +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; +import org.omg.CORBA.TypeCodePackage.Bounds; +import org.omg.CORBA.UnionMember; +import org.omg.CORBA.ValueMember; + +/** + * The type code that also has the member property getters + * supported. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class recordTypeCode + extends generalTypeCode +{ + /** + * The individual field of the record. + */ + public static class Field + { + /** + * The record label. + */ + public Any label; + + /** + * The record name. + */ + public String name; + + /** + * The record type. + */ + public TypeCode type; + + /** + * The record visibility. + */ + public int visibility = UNSET; + } + + /** + * The members of this data structure. + */ + protected corbaArrayList members = new corbaArrayList(); + private TypeCode discriminator_type; + private int default_index = UNSET; + + /** + * Creates the type code of the given kind. + */ + public recordTypeCode(TCKind kind) + { + super(kind); + } + + /** + * Set the default index. + */ + public void setDefaultIndex(int a_default_index) + { + this.default_index = a_default_index; + } + + /** + * Set the discriminator type. + */ + public void setDiscriminator_type(TypeCode a_discriminator_type) + { + this.discriminator_type = a_discriminator_type; + } + + public Field getField(int p) + { + return (Field) members.get(p); + } + + public void add(Field field) + { + members.add(field); + } + + /** + * Adds a new field, taking values from the passed + * {@link StructMember}. + */ + public void add(StructMember m) + { + Field f = field(); + f.name = m.name; + f.type = m.type; + } + + /** + * Adds a new field, taking values from the passed + * {@link ValueMember}. + */ + public void add(ValueMember m) + { + Field f = field(); + f.name = m.name; + f.type = m.type; + f.visibility = m.access; + + } + + /** + * Adds a new field, taking values from the passed + * {@link UnionMember}. + */ + public void add(UnionMember m) + { + Field f = field(); + f.name = m.name; + f.type = m.type; + f.label = m.label; + } + + public int default_index() + throws BadKind + { + if (default_index != UNSET) + return default_index; + throw new BadKind(); + } + + public TypeCode discriminator_type() + throws BadKind + { + if (discriminator_type != null) + return discriminator_type; + throw new BadKind(); + } + + /** + * Creates, adds and returns new field. + */ + public Field field() + { + Field f = new Field(); + members.add(f); + return f; + } + + /** {@inheritDoc} */ + public int member_count() + { + return members.size(); + } + + /** {@inheritDoc} */ + public Any member_label(int index) + throws BadKind, Bounds + { + Field f = getField(index); + if (f.label != null) + { + return f.label; + } + else + throw new BadKind(); + } + + /** {@inheritDoc} */ + public String member_name(int index) + throws BadKind + { + Field f = getField(index); + if (f.name != null) + { + return f.name; + } + else + throw new BadKind(); + } + + /** {@inheritDoc} */ + public TypeCode member_type(int index) + throws BadKind, Bounds + { + Field f = getField(index); + if (f.type != null) + { + return f.type; + } + else + throw new BadKind(); + } + + /** {@inheritDoc} */ + public short member_visibility(int index) + throws BadKind, Bounds + { + Field f = getField(index); + if (f.visibility != UNSET) + { + return (short) f.visibility; + } + else + throw new BadKind(); + } +} diff --git a/libjava/classpath/gnu/CORBA/recursiveTypeCode.java b/libjava/classpath/gnu/CORBA/recursiveTypeCode.java new file mode 100644 index 00000000000..6bc672e6e49 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/recursiveTypeCode.java @@ -0,0 +1,78 @@ +/* recursiveTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; + +/** + * The typecode, serving as a placeholder in defining + * typecodes, containing recursion. + */ +public class recursiveTypeCode + extends primitiveTypeCode +{ + /** + * The id of the type for that this type serves as a + * placeholder. + */ + private final String the_id; + + /** + * Create a typecode that serves as a placeholder for + * the typecode with the given id. + * + * @param id the Id of the type for that this type serves as a + * placeholder. + */ + public recursiveTypeCode(String an_id) + { + super(TCKind.tk_null); + the_id = an_id; + } + + /** + * Get the id of the type for that this type serves as a + * placeholder. + */ + public String id() + throws org.omg.CORBA.TypeCodePackage.BadKind + { + return the_id; + } +} diff --git a/libjava/classpath/gnu/CORBA/streamReadyHolder.java b/libjava/classpath/gnu/CORBA/streamReadyHolder.java new file mode 100644 index 00000000000..a777bd55597 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/streamReadyHolder.java @@ -0,0 +1,120 @@ +/* streamReadyHolder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.CORBA; + +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.NO_IMPLEMENT; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +import java.io.IOException; + +/** + * A holder that stores the input stream, from that the holder data + * can be read. There is no way to write the data into this holder. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class streamReadyHolder + implements Streamable +{ + /** + * The stream, holding the data for this holder. + */ + protected final InputStream stream; + + /** + * Create a holder that will read from the given stream. + * + * @param a_stream a stream. + */ + public streamReadyHolder(InputStream a_stream) + { + stream = a_stream; + } + + /** + * This method is not in use, should never be called. + */ + public TypeCode _type() + { + throw new NO_IMPLEMENT(); + } + + /** + * Writes the data from the stored stream into the provided + * output stream till the end of the input stream is reached. + * + * @throws MARSHAL if the IOException is thrown during operation. + */ + public void _write(OutputStream output) + { + try + { + int d = stream.read(); + + while (d >= 0) + { + output.write(d); + d = stream.read(); + } + } + catch (IOException ex) + { + throw new MARSHAL(ex + ":" + ex.getMessage()); + } + } + + /** + * This method is not in use, should never be called. + */ + public void _read(InputStream input) + { + throw new NO_IMPLEMENT(); + } + + /** + * Get the input stream that has been passed in constructor. + */ + InputStream getInputStream() + { + return stream; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/CORBA/streamRequest.java b/libjava/classpath/gnu/CORBA/streamRequest.java new file mode 100644 index 00000000000..a0f7eb09928 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/streamRequest.java @@ -0,0 +1,60 @@ +/* gnuStreamRequest.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufOutput; + +/** + * A stream, additionally holding the gnu request. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class streamRequest + extends cdrBufOutput +{ + /** + * The enclosed request. + */ + public gnuRequest request; + + /** + * True if the response is expected. + */ + public boolean response_expected = true; +} diff --git a/libjava/classpath/gnu/CORBA/stringTypeCode.java b/libjava/classpath/gnu/CORBA/stringTypeCode.java new file mode 100644 index 00000000000..27aa119f30e --- /dev/null +++ b/libjava/classpath/gnu/CORBA/stringTypeCode.java @@ -0,0 +1,83 @@ +/* stringTypeCode.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; + +/** + * The typecode for string and wide string. + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class stringTypeCode + extends primitiveTypeCode +{ + private int len = 0; + + /** + * Create a new instance of the string type code. + * + * @param a_kind a kind of this typecode, normally + * either tk_string or tk_wstring. + */ + public stringTypeCode(TCKind a_kind) + { + super(a_kind); + } + + /** + * Set the length property. + * + * @param a_length a length. + */ + public void setLength(int a_length) + { + len = a_length; + } + + /** + * Get the length of the string. This method returns 0 + * (unbounded) if the property has not been set. + * + * @return the length (bound) of the string. + */ + public int length() + { + return len; + } +} diff --git a/libjava/classpath/gnu/CORBA/stubFinder.java b/libjava/classpath/gnu/CORBA/stubFinder.java new file mode 100644 index 00000000000..77efd004717 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/stubFinder.java @@ -0,0 +1,110 @@ +/* stubFinder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.ORB; +import org.omg.CORBA.portable.ObjectImpl; + +/** + * Finds a stub class like "_HelloStub" that can be instantiated + * from IOR reference. The returned object can be casted to the + * used type like "Hello" without using the helper .narrow method, + * and the object is not unnsecessarily re - instantiated if + * the .narrow method is used anyway. If no stub, matching the naming + * conventions, is available, the returned stub replacement can still be used + * to get the valid request, add parameter and invoke the method by name. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class stubFinder +{ + /** + * Search for the possibly available default stub. + * + * @param orb the current ORB. It is not required to find the binding + * classes, but is needed to instantiate the default implementation + * if no binding classes are found. + * + * @param ior the IOR, possibly containing the information about the + * correct implementation class. + */ + public static ObjectImpl search(ORB orb, IOR ior) + { + try + { + int a = ior.Id.indexOf(':'); + int b = ior.Id.lastIndexOf(':'); + + String s = ior.Id.substring(a + 1, b).replace('/', '.'); + + String path; + + b = s.lastIndexOf('.'); + if (b > 0) + path = s.substring(0, b + 1); + else + path = ""; + + String stub = "_" + s.substring(b + 1) + "Stub"; + + Class stubClass = Class.forName(path + stub); + + return (ObjectImpl) stubClass.newInstance(); + } + catch (Exception failed) + { + // Various exceptions can be thrown if the Id cannot be parsed, + // the class is missing, cannot be instantiated or is not an + // instance of the ObjectImpl. + return createDefaultStub(orb, ior); + } + } + + /** + * Return the default stub for the case when the client binding classes + * are not locally available. The returned stub can still be used + * to get the valid request, add parameter and invoke the method by name. + * + * @return the default implementation. + */ + protected static ObjectImpl createDefaultStub(ORB orb, IOR ior) + { + return new IOR_contructed_object(orb, ior); + } +} diff --git a/libjava/classpath/gnu/CORBA/typeNamer.java b/libjava/classpath/gnu/CORBA/typeNamer.java new file mode 100644 index 00000000000..8b701015e23 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/typeNamer.java @@ -0,0 +1,171 @@ +/* primitiveTypes.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import org.omg.CORBA.TCKind; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.TypeCodePackage.BadKind; + +/** + * A conveniency method for naming the built-in types. + * This is used in error reporting that is part of the user interface. + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class typeNamer +{ + /** + * Names of the primitve types. + */ + protected static final String[] tk = + new String[] + { + "null", "void", "short", "long", "ushort", "ulong", "float", "double", + "boolean", "char", "octet", "any", "TypeCode", "Principal", "objref", + "struct", "union", "enum", "string", "sequence", "array", "alias", + "exception", "longlong", "ulonglong", "longdouble", "wchar", "wstring", + "fixed", "value", "value_box", "native", "abstract_interface" + }; + + /** + * Primitve TypeCodes. + */ + protected static final TypeCode[] primitveCodes = + new TypeCode[] + { + new primitiveTypeCode(TCKind.tk_null), + new primitiveTypeCode(TCKind.tk_void), + new primitiveTypeCode(TCKind.tk_short), + new primitiveTypeCode(TCKind.tk_long), + new primitiveTypeCode(TCKind.tk_ushort), + new primitiveTypeCode(TCKind.tk_ulong), + new primitiveTypeCode(TCKind.tk_float), + new primitiveTypeCode(TCKind.tk_double), + new primitiveTypeCode(TCKind.tk_boolean), + new primitiveTypeCode(TCKind.tk_char), + new primitiveTypeCode(TCKind.tk_octet), + new primitiveTypeCode(TCKind.tk_any), + new primitiveTypeCode(TCKind.tk_TypeCode), + new primitiveTypeCode(TCKind.tk_Principal), + new primitiveTypeCode(TCKind.tk_objref), + new primitiveTypeCode(TCKind.tk_struct), + new primitiveTypeCode(TCKind.tk_union), + new primitiveTypeCode(TCKind.tk_enum), + new primitiveTypeCode(TCKind.tk_string), + new primitiveTypeCode(TCKind.tk_sequence), + new primitiveTypeCode(TCKind.tk_array), + new primitiveTypeCode(TCKind.tk_alias), + new primitiveTypeCode(TCKind.tk_except), + new primitiveTypeCode(TCKind.tk_longlong), + new primitiveTypeCode(TCKind.tk_ulonglong), + new primitiveTypeCode(TCKind.tk_longdouble), + new primitiveTypeCode(TCKind.tk_wchar), + new primitiveTypeCode(TCKind.tk_wstring), + new primitiveTypeCode(TCKind.tk_fixed), + new primitiveTypeCode(TCKind.tk_value), + new primitiveTypeCode(TCKind.tk_value_box), + new primitiveTypeCode(TCKind.tk_native), + new primitiveTypeCode(TCKind.tk_abstract_interface) + }; + + /** + * Get the primitive type code. + * + * @return the primitve type code, corresponding the passed value. + * + * @throws BadKind if this is not a primitive type code. + */ + public static TypeCode getPrimitveTC(TCKind tc) + throws BadKind + { + try + { + return primitveCodes [ tc.value() ]; + } + catch (ArrayIndexOutOfBoundsException ex) + { + throw new BadKind(tc.value() + " is not a primitve type."); + } + } + + /** + * Get the string name of the passed primitive type. + * + * @param kind the kind of the primitive type the must be defined + * in {@link omg.org.CORBA.TCKind}. + * + * @return the short string name, used in error reporting, etc. + */ + public static String nameIt(int kind) + { + try + { + return tk [ kind ]; + } + catch (ArrayIndexOutOfBoundsException ex) + { + return "type of kind '" + kind + "'"; + } + } + + /** + * Get the string name of the passed primitive type. + * + * @param kind the kind of the primitive type the must be defined + * in {@link omg.org.CORBA.TCKind}. + * + * @return the short string name, used in error reporting, etc. + */ + public static String nameIt(TypeCode type) + { + try + { + if (type.kind().value() == TCKind._tk_array) + return "array of " + nameIt(type.content_type()); + else if (type.kind().value() == TCKind._tk_sequence) + return "sequence of " + nameIt(type.content_type()); + else + return nameIt(type.kind().value()); + } + catch (Exception ex) + { + return "type of kind '" + type.kind().value() + "'"; + } + } +} diff --git a/libjava/classpath/gnu/CORBA/universalHolder.java b/libjava/classpath/gnu/CORBA/universalHolder.java new file mode 100644 index 00000000000..6d8b7747290 --- /dev/null +++ b/libjava/classpath/gnu/CORBA/universalHolder.java @@ -0,0 +1,157 @@ +/* universalStreamable.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.CORBA; + +import gnu.CORBA.CDR.cdrBufInput; +import gnu.CORBA.CDR.cdrBufOutput; + +import org.omg.CORBA.BAD_OPERATION; +import org.omg.CORBA.MARSHAL; +import org.omg.CORBA.TypeCode; +import org.omg.CORBA.portable.InputStream; +import org.omg.CORBA.portable.OutputStream; +import org.omg.CORBA.portable.Streamable; + +import java.io.IOException; + +/** + * This class holds the abstract binary data array of the Streamable + * being stored. It is used to insert and extract into {@link Any} objects + * that have no holder, but have the helper classes. + * Encoding/decoding then must be performed by the helper. This class is + * defined as package private because it is not idiot proof and + * must be used with care. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +class universalHolder + implements Streamable +{ + /** + * The binary data, stored inside this holder. + */ + private cdrBufOutput value = new cdrBufOutput(); + + /** + * Create the universal holder that uses the given buffer to store the data. + */ + universalHolder(cdrBufOutput buffer) + { + value = buffer; + } + + /** + * Reads into the internal buffer till the end of stream is + * reached. No alignment operations are performed. This operation + * normally reads from the stream, where the single value, + * stored using {@link #_write}, is written. + * + * @throws MARSHALL, if the IOException is thrown during the + * stream operation. + */ + public void _read(InputStream input) + { + try + { + if (input instanceof cdrBufInput) + { + cdrBufInput b = (cdrBufInput) input; + value.write(b.buffer.getBuffer()); + } + else + { + int c; + + do + { + c = input.read(); + if (c >= 0) + value.write(c); + } + while (c >= 0); + } + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } + + /** + * The type is not known and cannot be returned. + * + * @throws BAD_OPERATION, always. + */ + public TypeCode _type() + { + throw new BAD_OPERATION(); + } + + /** + * Writes the binary data being stored into the given output + * stream. This operation supposes that the current stream + * position is 0 or satisfies the required alignments anyway. + * + * @throws MARSHAL if the IOExeption is thrown when writing the + * byte array. + */ + public void _write(OutputStream output) + { + try + { + value.buffer.writeTo(output); + } + catch (IOException ex) + { + MARSHAL t = new MARSHAL(); + t.initCause(ex); + throw t; + } + } + + /** + * Get the input stream that reads the fields of the stored value. + */ + InputStream getInputStream() + { + return value.create_input_stream(); + } +} diff --git a/libjava/classpath/gnu/classpath/.cvsignore b/libjava/classpath/gnu/classpath/.cvsignore new file mode 100644 index 00000000000..11f6639ebbd --- /dev/null +++ b/libjava/classpath/gnu/classpath/.cvsignore @@ -0,0 +1 @@ +Configuration.java diff --git a/libjava/classpath/gnu/classpath/Configuration.java.in b/libjava/classpath/gnu/classpath/Configuration.java.in new file mode 100644 index 00000000000..9da4a820c83 --- /dev/null +++ b/libjava/classpath/gnu/classpath/Configuration.java.in @@ -0,0 +1,131 @@ +/* Configuration.java -- + Copyright (C) 1998, 2001, 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath; + +/** + * This file defines compile-time constants that can be accessed by + * java code. It is pre-processed by configure. + */ +public interface Configuration +{ + /** + * The value of CLASSPATH_HOME is the location that the classpath + * libraries and support files where installed in. It is set according to + * the argument for --prefix given to configure and used to set the + * System property gnu.classpath.home. + */ + String CLASSPATH_HOME = "@prefix@"; + + /** + * The release version number of GNU Classpath. + * It is set according to the value of 'version' in the configure[.in] file + * and used to set the System property gnu.classpath.version. + */ + String CLASSPATH_VERSION = "@VERSION@"; + + /** + * The value of DEBUG is substituted according to whether the + * "--enable-debug" argument was passed to configure. Code + * which is made conditional based on the value of this flag - typically + * code that generates debugging output - will be removed by the optimizer + * in a non-debug build. + */ + boolean DEBUG = @LIBDEBUG@; + + /** + * The value of LOAD_LIBRARY is substituted according to whether the + * "--enable-load-library" or "--disable-load-library" argument was passed + * to configure. By default, configure should define this is as true. + * If set to false, loadLibrary() calls to load native function + * implementations, typically found in static initializers of classes + * which contain native functions, will be omitted. This is useful for + * runtimes which pre-link their native function implementations and do + * not require additional shared libraries to be loaded. + */ + boolean INIT_LOAD_LIBRARY = @INIT_LOAD_LIBRARY@; + + /** + * Set to true if the VM provides a native method to implement + * Proxy.getProxyClass completely, including argument verification. + * If this is true, HAVE_NATIVE_GET_PROXY_DATA and + * HAVE_NATIVE_GENERATE_PROXY_CLASS should be false. + * @see java.lang.reflect.Proxy + */ + boolean HAVE_NATIVE_GET_PROXY_CLASS = false; + + /** + * Set to true if the VM provides a native method to implement + * the first part of Proxy.getProxyClass: generation of the array + * of methods to convert, and verification of the arguments. + * If this is true, HAVE_NATIVE_GET_PROXY_CLASS should be false. + * @see java.lang.reflect.Proxy + */ + boolean HAVE_NATIVE_GET_PROXY_DATA = false; + + /** + * Set to true if the VM provides a native method to implement + * the second part of Proxy.getProxyClass: conversion of an array of + * methods into an actual proxy class. + * If this is true, HAVE_NATIVE_GET_PROXY_CLASS should be false. + * @see java.lang.reflect.Proxy + */ + boolean HAVE_NATIVE_GENERATE_PROXY_CLASS = false; + + /** + * Name of default AWT peer library. + */ + String default_awt_peer_toolkit = "@default_toolkit@"; + + /** + * Whether to automatically run the init* methods in java.lang.System + * (the default) at class initialization time or whether to have the VM + * explicitly invoke them. + * + * The default is false, meaning the VM does not explicitly run the + * initializers. + * + */ + boolean JAVA_LANG_SYSTEM_EXPLICIT_INITIALIZATION = + @JAVA_LANG_SYSTEM_EXPLICIT_INITIALIZATION@; + + /** + * Set to true if Cairo was found and enabled during configure, + * false otherwise. + */ + boolean GTK_CAIRO_ENABLED = @GTK_CAIRO_ENABLED@; +} diff --git a/libjava/classpath/gnu/classpath/RawData.java b/libjava/classpath/gnu/classpath/RawData.java new file mode 100644 index 00000000000..3ce97482c02 --- /dev/null +++ b/libjava/classpath/gnu/classpath/RawData.java @@ -0,0 +1,47 @@ +/* RawData.java -- Pointer to VM specific data + Copyright (C) 1999, 2000, 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +/* This file is originally part of libgcj. */ + +package gnu.classpath; + +/** A type used to indicate special data used by native code that should not + be marked by the garbage collector. */ + +public abstract class RawData +{ +} diff --git a/libjava/classpath/gnu/classpath/RawData32.java b/libjava/classpath/gnu/classpath/RawData32.java new file mode 100644 index 00000000000..c77163152f2 --- /dev/null +++ b/libjava/classpath/gnu/classpath/RawData32.java @@ -0,0 +1,52 @@ +/* RawData32.java -- 32 bit Pointer + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath; + +/** + * A type used to indicate special data used by native code that should not + * be marked by the garbage collector. + */ +public final class RawData32 extends RawData +{ + final int data; + + public RawData32(int data) + { + this.data = data; + } +} diff --git a/libjava/classpath/gnu/classpath/RawData64.java b/libjava/classpath/gnu/classpath/RawData64.java new file mode 100644 index 00000000000..e3b6a93e28e --- /dev/null +++ b/libjava/classpath/gnu/classpath/RawData64.java @@ -0,0 +1,52 @@ +/* RawData64.java -- 64 bit Pointer + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath; + +/** + * A type used to indicate special data used by native code that should not + * be marked by the garbage collector. + */ +public final class RawData64 extends RawData +{ + final long data; + + public RawData64(long data) + { + this.data = data; + } +} diff --git a/libjava/classpath/gnu/classpath/ServiceFactory.java b/libjava/classpath/gnu/classpath/ServiceFactory.java new file mode 100644 index 00000000000..711a9042cbf --- /dev/null +++ b/libjava/classpath/gnu/classpath/ServiceFactory.java @@ -0,0 +1,573 @@ +/* ServiceFactory.java -- Factory for plug-in services. + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + + +/** + * A factory for plug-ins that conform to a service provider + * interface. This is a general mechanism that gets used by a number + * of packages in the Java API. For instance, {@link + * java.nio.charset.spi.CharsetProvider} allows to write custom + * encoders and decoders for character sets, {@link + * javax.imageio.spi.ImageReaderSpi} allows to support custom image + * formats, and {@link javax.print.PrintService} makes it possible to + * write custom printer drivers. + * + * <p>The plug-ins are concrete implementations of the service + * provider interface, which is defined as an interface or an abstract + * class. The implementation classes must be public and have a public + * constructor that takes no arguments. + * + * <p>Plug-ins are usually deployed in JAR files. A JAR that provides + * an implementation of a service must declare this in a resource file + * whose name is the fully qualified service name and whose location + * is the directory <code>META-INF/services</code>. This UTF-8 encoded + * text file lists, on separate lines, the fully qualified names of + * the concrete implementations. Thus, one JAR file can provide an + * arbitrary number of implementations for an arbitrary count of + * service provider interfaces. + * + * <p><b>Example</b> + * + * <p>For example, a JAR might provide two implementations of the + * service provider interface <code>org.foo.ThinkService</code>, + * namely <code>com.acme.QuickThinker</code> and + * <code>com.acme.DeepThinker</code>. The code for <code>QuickThinker</code> + * woud look as follows: + * + * <pre> + * package com.acme; + * + * /** + * * Provices a super-quick, but not very deep implementation of ThinkService. + * */ + * public class QuickThinker + * implements org.foo.ThinkService + * { + * /** + * * Constructs a new QuickThinker. The service factory (which is + * * part of the Java environment) calls this no-argument constructor + * * when it looks up the available implementations of ThinkService. + * * + * * <p>Note that an application might query all available + * * ThinkService providers, but use just one of them. Therefore, + * * constructing an instance should be very inexpensive. For example, + * * large data structures should only be allocated when the service + * * actually gets used. + * */ + * public QuickThinker() + * { + * } + * + * /** + * * Returns the speed of this ThinkService in thoughts per second. + * * Applications can choose among the available service providers + * * based on this value. + * */ + * public double getSpeed() + * { + * return 314159.2654; + * } + * + * /** + * * Produces a thought. While the returned thoughts are not very + * * deep, they are generated in very short time. + * */ + * public Thought think() + * { + * return null; + * } + * } + * </pre> + * + * <p>The code for <code>com.acme.DeepThinker</code> is left as an + * exercise to the reader. + * + * <p>Acme’s <code>ThinkService</code> plug-in gets deployed as + * a JAR file. Besides the bytecode and resources for + * <code>QuickThinker</code> and <code>DeepThinker</code>, it also + * contains the text file + * <code>META-INF/services/org.foo.ThinkService</code>: + * + * <pre> + * # Available implementations of org.foo.ThinkService + * com.acme.QuickThinker + * com.acme.DeepThinker + * </pre> + * + * <p><b>Thread Safety</b> + * + * <p>It is safe to use <code>ServiceFactory</code> from multiple + * concurrent threads without external synchronization. + * + * <p><b>Note for User Applications</b> + * + * <p>User applications that want to load plug-ins should not directly + * use <code>gnu.classpath.ServiceFactory</code>, because this class + * is only available in Java environments that are based on GNU + * Classpath. Instead, it is recommended that user applications call + * {@link + * javax.imageio.spi.ServiceRegistry#lookupProviders(Class)}. This API + * is actually independent of image I/O, and it is available on every + * environment. + * + * @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a> + */ +public final class ServiceFactory +{ + /** + * A logger that gets informed when a service gets loaded, or + * when there is a problem with loading a service. + * + * <p>Because {@link java.util.logging.Logger#getLogger(String)} + * is thread-safe, we do not need to worry about synchronization + * here. + */ + private static final Logger LOGGER = Logger.getLogger("gnu.classpath"); + + + /** + * Declared private in order to prevent constructing instances of + * this utility class. + */ + private ServiceFactory() + { + } + + + /** + * Finds service providers that are implementing the specified + * Service Provider Interface. + * + * <p><b>On-demand loading:</b> Loading and initializing service + * providers is delayed as much as possible. The rationale is that + * typical clients will iterate through the set of installed service + * providers until one is found that matches some criteria (like + * supported formats, or quality of service). In such scenarios, it + * might make sense to install only the frequently needed service + * providers on the local machine. More exotic providers can be put + * onto a server; the server will only be contacted when no suitable + * service could be found locally. + * + * <p><b>Security considerations:</b> Any loaded service providers + * are loaded through the specified ClassLoader, or the system + * ClassLoader if <code>classLoader</code> is + * <code>null</code>. When <code>lookupProviders</code> is called, + * the current {@link AccessControlContext} gets recorded. This + * captured security context will determine the permissions when + * services get loaded via the <code>next()</code> method of the + * returned <code>Iterator</code>. + * + * @param spi the service provider interface which must be + * implemented by any loaded service providers. + * + * @param loader the class loader that will be used to load the + * service providers, or <code>null</code> for the system class + * loader. For using the context class loader, see {@link + * #lookupProviders(Class)}. + * + * @return an iterator over instances of <code>spi</code>. + * + * @throws IllegalArgumentException if <code>spi</code> is + * <code>null</code>. + */ + public static Iterator lookupProviders(Class spi, + ClassLoader loader) + { + String resourceName; + Enumeration urls; + + if (spi == null) + throw new IllegalArgumentException(); + + if (loader == null) + loader = ClassLoader.getSystemClassLoader(); + + resourceName = "META-INF/services/" + spi.getName(); + try + { + urls = loader.getResources(resourceName); + } + catch (IOException ioex) + { + /* If an I/O error occurs here, we cannot provide any service + * providers. In this case, we simply return an iterator that + * does not return anything (no providers installed). + */ + log(Level.WARNING, "cannot access {0}", resourceName, ioex); + return Collections.EMPTY_LIST.iterator(); + } + + return new ServiceIterator(spi, urls, loader, + AccessController.getContext()); + } + + + /** + * Finds service providers that are implementing the specified + * Service Provider Interface, using the context class loader + * for loading providers. + * + * @param spi the service provider interface which must be + * implemented by any loaded service providers. + * + * @return an iterator over instances of <code>spi</code>. + * + * @throws IllegalArgumentException if <code>spi</code> is + * <code>null</code>. + * + * @see #lookupProviders(Class, ClassLoader) + */ + public static Iterator lookupProviders(Class spi) + { + ClassLoader ctxLoader; + + ctxLoader = Thread.currentThread().getContextClassLoader(); + return lookupProviders(spi, ctxLoader); + } + + + /** + * An iterator over service providers that are listed in service + * provider configuration files, which get passed as an Enumeration + * of URLs. This is a helper class for {@link + * ServiceFactory#lookupProviders}. + * + * @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a> + */ + private static final class ServiceIterator + implements Iterator + { + /** + * The service provider interface (usually an interface, sometimes + * an abstract class) which the services must implement. + */ + private final Class spi; + + + /** + * An Enumeration<URL> over the URLs that contain a resource + * <code>META-INF/services/<org.foo.SomeService></code>, + * as returned by {@link ClassLoader#getResources(String)}. + */ + private final Enumeration urls; + + + /** + * The class loader used for loading service providers. + */ + private final ClassLoader loader; + + + /** + * The security context used when loading and initializing service + * providers. We want to load and initialize all plug-in service + * providers under the same security context, namely the one that + * was active when {@link #lookupProviders} has been called. + */ + private final AccessControlContext securityContext; + + + /** + * A reader for the current file listing class names of service + * implementors, or <code>null</code> when the last reader has + * been fetched. + */ + private BufferedReader reader; + + + /** + * The URL currently being processed. This is only used for + * emitting error messages. + */ + private URL currentURL; + + + /** + * The service provider that will be returned by the next call to + * {@link #next()}, or <code>null</code> if the iterator has + * already returned all service providers. + */ + private Object nextProvider; + + + /** + * Constructs an Iterator that loads and initializes services on + * demand. + * + * @param spi the service provider interface which the services + * must implement. Usually, this is a Java interface type, but it + * might also be an abstract class or even a concrete superclass. + * + * @param urls an Enumeration<URL> over the URLs that contain a + * resource + * <code>META-INF/services/<org.foo.SomeService></code>, as + * determined by {@link ClassLoader#getResources(String)}. + * + * @param loader the ClassLoader that gets used for loading + * service providers. + * + * @param securityContext the security context to use when loading + * and initializing service providers. + */ + ServiceIterator(Class spi, Enumeration urls, ClassLoader loader, + AccessControlContext securityContext) + { + this.spi = spi; + this.urls = urls; + this.loader = loader; + this.securityContext = securityContext; + this.nextProvider = loadNextServiceProvider(); + } + + + /** + * @throws NoSuchElementException if {@link #hasNext} returns + * <code>false</code>. + */ + public Object next() + { + Object result; + + if (!hasNext()) + throw new NoSuchElementException(); + + result = nextProvider; + nextProvider = loadNextServiceProvider(); + return result; + } + + + public boolean hasNext() + { + return nextProvider != null; + } + + + public void remove() + { + throw new UnsupportedOperationException(); + } + + + private Object loadNextServiceProvider() + { + String line; + + if (reader == null) + advanceReader(); + + for (;;) + { + /* If we have reached the last provider list, we cannot + * retrieve any further lines. + */ + if (reader == null) + return null; + + try + { + line = reader.readLine(); + } + catch (IOException readProblem) + { + log(Level.WARNING, "IOException upon reading {0}", currentURL, + readProblem); + line = null; + } + + /* When we are at the end of one list of services, + * switch over to the next one. + */ + if (line == null) + { + advanceReader(); + continue; + } + + + // Skip whitespace at the beginning and end of each line. + line = line.trim(); + + // Skip empty lines. + if (line.length() == 0) + continue; + + // Skip comment lines. + if (line.charAt(0) == '#') + continue; + + try + { + log(Level.FINE, + "Loading service provider \"{0}\", specified" + + " by \"META-INF/services/{1}\" in {2}.", + new Object[] { line, spi.getName(), currentURL }, + null); + + /* Load the class in the security context that was + * active when calling lookupProviders. + */ + return AccessController.doPrivileged( + new ServiceProviderLoadingAction(spi, line, loader), + securityContext); + } + catch (Exception ex) + { + String msg = "Cannot load service provider class \"{0}\"," + + " specified by \"META-INF/services/{1}\" in {2}"; + if (ex instanceof PrivilegedActionException + && ex.getCause() instanceof ClassCastException) + msg = "Service provider class \"{0}\" is not an instance" + + " of \"{1}\". Specified" + + " by \"META-INF/services/{1}\" in {2}."; + + log(Level.WARNING, msg, + new Object[] { line, spi.getName(), currentURL }, + ex); + continue; + } + } + } + + + private void advanceReader() + { + do + { + if (reader != null) + { + try + { + reader.close(); + log(Level.FINE, "closed {0}", currentURL, null); + } + catch (Exception ex) + { + log(Level.WARNING, "cannot close {0}", currentURL, ex); + } + reader = null; + currentURL = null; + } + + if (!urls.hasMoreElements()) + return; + + currentURL = (URL) urls.nextElement(); + try + { + reader = new BufferedReader(new InputStreamReader( + currentURL.openStream(), "UTF-8")); + log(Level.FINE, "opened {0}", currentURL, null); + } + catch (Exception ex) + { + log(Level.WARNING, "cannot open {0}", currentURL, ex); + } + } + while (reader == null); + } + } + + + // Package-private to avoid a trampoline. + /** + * Passes a log message to the <code>java.util.logging</code> + * framework. This call returns very quickly if no log message will + * be produced, so there is not much overhead in the standard case. + * + * @param the severity of the message, for instance {@link + * Level#WARNING}. + * + * @param msg the log message, for instance <code>“Could not + * load {0}.”</code> + * + * @param param the parameter(s) for the log message, or + * <code>null</code> if <code>msg</code> does not specify any + * parameters. If <code>param</code> is not an array, an array with + * <code>param</code> as its single element gets passed to the + * logging framework. + * + * @param t a Throwable that is associated with the log record, or + * <code>null</code> if the log message is not associated with a + * Throwable. + */ + static void log(Level level, String msg, Object param, Throwable t) + { + LogRecord rec; + + // Return quickly if no log message will be produced. + if (!LOGGER.isLoggable(level)) + return; + + rec = new LogRecord(level, msg); + if (param != null && param.getClass().isArray()) + rec.setParameters((Object[]) param); + else + rec.setParameters(new Object[] { param }); + + rec.setThrown(t); + + // While java.util.logging can sometimes infer the class and + // method of the caller, this automatic inference is not reliable + // on highly optimizing VMs. Also, log messages make more sense to + // developers when they display a public method in a public class; + // otherwise, they might feel tempted to figure out the internals + // of ServiceFactory in order to understand the problem. + rec.setSourceClassName(ServiceFactory.class.getName()); + rec.setSourceMethodName("lookupProviders"); + + LOGGER.log(rec); + } +} diff --git a/libjava/classpath/gnu/classpath/ServiceProviderLoadingAction.java b/libjava/classpath/gnu/classpath/ServiceProviderLoadingAction.java new file mode 100644 index 00000000000..b5e59cb4b6f --- /dev/null +++ b/libjava/classpath/gnu/classpath/ServiceProviderLoadingAction.java @@ -0,0 +1,149 @@ +/* ServiceProviderLoadingAction.java -- Action for loading plug-in services. + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath; + +import java.security.PrivilegedExceptionAction; + +/** + * A privileged action for creating a new instance of a service + * provider. + * + * <p>Class loading and instantiation is encapsulated in a + * <code>PriviledgedAction</code> in order to restrict the loaded + * service providers to the {@link java.security.AccessControlContext} + * that was active when {@link + * gnu.classpath.ServiceFactory#lookupProviders} was called, even + * though the actual loading is delayed to the time when the provider + * is actually needed. + * + * @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a> + */ +final class ServiceProviderLoadingAction + implements PrivilegedExceptionAction +{ + /** + * The interface to which the loaded service provider implementation + * must conform. Usually, this is a Java interface type, but it + * might also be an abstract class or even a concrete class. + */ + private final Class spi; + + + /** + * The fully qualified name of the class that gets loaded when + * this action is executed. + */ + private final String providerName; + + + /** + * The ClassLoader that gets used for loading the service provider + * class. + */ + private final ClassLoader loader; + + + /** + * Constructs a privileged action for loading a service provider. + * + * @param spi the interface to which the loaded service provider + * implementation must conform. Usually, this is a Java interface + * type, but it might also be an abstract class or even a concrete + * superclass. + * + * @param providerName the fully qualified name of the class that + * gets loaded when this action is executed. + * + * @param loader the ClassLoader that gets used for loading the + * service provider class. + * + * @throws IllegalArgumentException if <code>spi</code>, + * <code>providerName</code> or <code>loader</code> is + * <code>null</code>. + */ + ServiceProviderLoadingAction(Class spi, String providerName, + ClassLoader loader) + { + if (spi == null || providerName == null || loader == null) + throw new IllegalArgumentException(); + + this.spi = spi; + this.providerName = providerName; + this.loader = loader; + } + + + /** + * Loads an implementation class for a service provider, and creates + * a new instance of the loaded class by invoking its public + * no-argument constructor. + * + * @return a new instance of the class whose name was passed as + * <code>providerName</code> to the constructor. + * + * @throws ClassCastException if the service provider does not + * implement the <code>spi</code> interface that was passed to the + * constructor. + * + * @throws IllegalAccessException if the service provider class or + * its no-argument constructor are not <code>public</code>. + * + * @throws InstantiationException if the service provider class is + * <code>abstract</code>, an interface, a primitive type, an array + * class, or void; or if service provider class does not have a + * no-argument constructor; or if there some other problem with + * creating a new instance of the service provider. + */ + public Object run() + throws Exception + { + Class loadedClass; + Object serviceProvider; + + loadedClass = loader.loadClass(providerName); + serviceProvider = loadedClass.newInstance(); + + // Ensure that the loaded provider is actually implementing + // the service provider interface. + if (!spi.isInstance(serviceProvider)) + throw new ClassCastException(spi.getName()); + + return serviceProvider; + } +} diff --git a/libjava/classpath/gnu/classpath/SystemProperties.java b/libjava/classpath/gnu/classpath/SystemProperties.java new file mode 100644 index 00000000000..600e1a6c159 --- /dev/null +++ b/libjava/classpath/gnu/classpath/SystemProperties.java @@ -0,0 +1,158 @@ +/* SystemProperties.java -- Manage the System properties. + Copyright (C) 2004, 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath; + +import java.util.Properties; + +/** + * The class manages the System properties. This class is only available to + * privileged code (i.e. code loaded by the bootstrap class loader) and + * therefore doesn't do any security checks. + * This class is separated out from java.lang.System to simplify bootstrap + * dependencies and to allow trusted code a simple and efficient mechanism + * to access the system properties. + */ +public class SystemProperties +{ + /** + * Stores the current system properties. This can be modified by + * {@link #setProperties(Properties)}, but will never be null, because + * setProperties(null) sucks in the default properties. + */ + private static Properties properties; + + /** + * The default properties. Once the default is stabilized, + * it should not be modified; + * instead it is cloned when calling <code>setProperties(null)</code>. + */ + private static final Properties defaultProperties = new Properties(); + + static + { + VMSystemProperties.preInit(defaultProperties); + + defaultProperties.put("gnu.classpath.home", Configuration.CLASSPATH_HOME); + defaultProperties.put("gnu.classpath.version", + Configuration.CLASSPATH_VERSION); + + // Set base URL if not already set. + if (defaultProperties.get("gnu.classpath.home.url") == null) + defaultProperties.put("gnu.classpath.home.url", + "file://" + + Configuration.CLASSPATH_HOME + + "/lib"); + + // Set short name if not already set. + if (defaultProperties.get("gnu.classpath.vm.shortname") == null) + { + String value = defaultProperties.getProperty("java.vm.name"); + int index = value.lastIndexOf(' '); + if (index != -1) + value = value.substring(index + 1); + defaultProperties.put("gnu.classpath.vm.shortname", value); + } + + // Network properties + if (defaultProperties.get("http.agent") == null) + { + String userAgent = ("gnu-classpath/" + + defaultProperties.getProperty("gnu.classpath.version") + + " (" + + defaultProperties.getProperty("gnu.classpath.vm.shortname") + + "/" + + defaultProperties.getProperty("java.vm.version") + + ")"); + defaultProperties.put("http.agent", userAgent); + } + + // 8859_1 is a safe default encoding to use when not explicitly set + if (defaultProperties.get("file.encoding") == null) + defaultProperties.put("file.encoding", "8859_1"); + + // Default to the Swing FocusManager so that the old-style Swing API + // for FocusManager can be supported without hardcoding it in AWT. + if (defaultProperties.get("gnu.java.awt.FocusManager") == null) + defaultProperties.put("gnu.java.awt.FocusManager", + "gnu.java.awt.FocusManager"); + + // XXX FIXME - Temp hack for old systems that set the wrong property + if (defaultProperties.get("java.io.tmpdir") == null) + defaultProperties.put("java.io.tmpdir", + defaultProperties.get("java.tmpdir")); + + VMSystemProperties.postInit(defaultProperties); + + // Note that we use clone here and not new. Some programs assume + // that the system properties do not have a parent. + properties = (Properties) defaultProperties.clone(); + } + + public static String getProperty(String name) + { + return properties.getProperty(name); + } + + public static String getProperty(String name, String defaultValue) + { + return properties.getProperty(name, defaultValue); + } + + public static String setProperty(String name, String value) + { + return (String) properties.setProperty(name, value); + } + + public static Properties getProperties() + { + return properties; + } + + public static void setProperties(Properties properties) + { + if (properties == null) + { + // Note that we use clone here and not new. Some programs + // assume that the system properties do not have a parent. + properties = (Properties)defaultProperties.clone(); + } + + SystemProperties.properties = properties; + } +} diff --git a/libjava/classpath/gnu/classpath/debug/Component.java b/libjava/classpath/gnu/classpath/debug/Component.java new file mode 100644 index 00000000000..3dfc8927bb7 --- /dev/null +++ b/libjava/classpath/gnu/classpath/debug/Component.java @@ -0,0 +1,159 @@ +/* Component.java -- a component log level. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under terms +of your choice, provided that you also meet, for each linked independent +module, the terms and conditions of the license of that module. An +independent module is a module which is not derived from or based on +this library. If you modify this library, you may extend this exception +to your version of the library, but you are not obligated to do so. If +you do not wish to do so, delete this exception statement from your +version. */ + + +package gnu.classpath.debug; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.logging.Level; + +public final class Component extends Level +{ + + /* + * HOW TO ADD NEW COMPONENTS: + * + * If you want to add a new, simple component, that you will use in + * logging statements, simply create a new class variable that + * instantiates this class, and choose an appropriate string name + * and a integer constant not used by any other component level. + * + * For example, if my component had to do with 'frobbing', I would + * add this entry below: + * + * private static final Component FROBBING = new Component ("FROBBING", 7); + * + * Then, I would update the component 'EVERYTHING' to have and end + * index ONE GREATER THAN the index of the new component. + * + * ADDING NEW COMPONENT CLASSES: + * + * A "component class" is a run of more than one component, which can + * be enabled all at once. EVERYTHING and SSL are examples of component + * classes. To add a new class, create a new component with a start index + * equal to the index of the first member component, and with an end + * index equal to the index of the last member component plus one. + */ + + /** + * Signifies that everything should be logged. This should be used to + * enable or disable levels only; logging code should not use it. + */ + public static final Component EVERYTHING = new Component ("*", 0, 7); + + /** + * Signifies that all SSL related messages should be logged. This should + * be used to enable or disable levels only; logging code should not use + * it. + */ + public static final Component SSL = new Component ("SSL", 0, 5); + + /** + * Traces the progression of an SSL handshake. + */ + public static final Component SSL_HANDSHAKE = new Component ("SSL HANDSHAKE", 0); + + /** + * Traces the application messages during SSL communications. + */ + public static final Component SSL_APPLICATION = new Component ("SSL APPLICATION", 1); + + /** + * Trace details about the SSL key exchange. + */ + public static final Component SSL_KEY_EXCHANGE = new Component ("SSL KEY EXCHANGE", 2); + + /* Indices 3 and 4 reserved for future use by SSL components. */ + + /** + * Trace the operation of cryptographic primitives. + */ + public static final Component CRYPTO = new Component ("CRYPTO", 5); + + /** + * Trace the parsing of X.509 certificates and related objects. + */ + public static final Component X509 = new Component ("X.509", 6); + + private final int startIndex; + private final int endIndex; + + private Component (final String name, final int bitIndex) + { + this (name, bitIndex, bitIndex + 1); + } + + private Component (final String name, final int startIndex, final int endIndex) + { + super (name, Level.FINE.intValue ()); + this.startIndex = startIndex; + this.endIndex = endIndex; + } + + /** + * Return the component for the given name. + * + * @param name The name of the component to get. + * @return The named component, or null if there is no such component. + */ + public static Component forName (final String name) + { + try + { + Field f = Component.class.getField (name.toUpperCase ()); + if (!Modifier.isStatic (f.getModifiers ()) + || Component.class.isAssignableFrom (f.getClass ())) + return null; + return (Component) f.get (null); + } + catch (Throwable _) + { + return null; + } + } + + public int startIndex () + { + return startIndex; + } + + public int endIndex () + { + return endIndex; + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/classpath/debug/PreciseFilter.java b/libjava/classpath/gnu/classpath/debug/PreciseFilter.java new file mode 100644 index 00000000000..7b88b2c8c92 --- /dev/null +++ b/libjava/classpath/gnu/classpath/debug/PreciseFilter.java @@ -0,0 +1,105 @@ +/* PreciseFilter.java -- filter log messages by precise level. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under terms +of your choice, provided that you also meet, for each linked independent +module, the terms and conditions of the license of that module. An +independent module is a module which is not derived from or based on +this library. If you modify this library, you may extend this exception +to your version of the library, but you are not obligated to do so. If +you do not wish to do so, delete this exception statement from your +version. */ + + +package gnu.classpath.debug; + +import java.util.BitSet; +import java.util.logging.Filter; +import java.util.logging.LogRecord; + +public final class PreciseFilter implements Filter +{ + + /** + * The singleton filter instance. + */ + public static final PreciseFilter GLOBAL = new PreciseFilter (); + + private final BitSet enabled; + + private PreciseFilter () + { + enabled = new BitSet (); + } + + /** + * Disable logging of a component. + * + * @param component The component to disable logging for. + * @throws NullPointerException If component is null. + */ + public void disable (final Component component) + { + enabled.clear (component.startIndex (), component.endIndex ()); + } + + /** + * Enable logging of a component. + * + * @param component The component to enable logging for. + * @throws NullPointerException If component is null. + */ + public void enable (final Component component) + { + enabled.set (component.startIndex (), component.endIndex ()); + } + + /** + * Tell if a component is enabled for logging. + * + * @param component The component to test. + * @return True iff the specified component is enabled for logging. + * @throws NullPointerException If component is null. + */ + public boolean isEnabled (final Component component) + { + return (enabled.get (component.startIndex ())); + } + + public boolean isLoggable (final LogRecord record) + { + try + { + return isEnabled ((Component) record.getLevel ()); + } + catch (ClassCastException cce) + { + return true; + } + } +}
\ No newline at end of file diff --git a/libjava/classpath/gnu/classpath/debug/SystemLogger.java b/libjava/classpath/gnu/classpath/debug/SystemLogger.java new file mode 100644 index 00000000000..94aa93f69da --- /dev/null +++ b/libjava/classpath/gnu/classpath/debug/SystemLogger.java @@ -0,0 +1,71 @@ +/* SystemLogger.java -- Classpath's system debugging logger. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under terms +of your choice, provided that you also meet, for each linked independent +module, the terms and conditions of the license of that module. An +independent module is a module which is not derived from or based on +this library. If you modify this library, you may extend this exception +to your version of the library, but you are not obligated to do so. If +you do not wish to do so, delete this exception statement from your +version. */ + + +package gnu.classpath.debug; + +import gnu.classpath.SystemProperties; +import java.util.StringTokenizer; +import java.util.logging.Logger; + +public final class SystemLogger +{ + public static final Logger SYSTEM = Logger.getLogger ("gnu.classpath"); + + static + { + SYSTEM.setFilter (PreciseFilter.GLOBAL); + + String defaults = SystemProperties.getProperty ("gnu.classpath.debug.components"); + + if (defaults != null) + { + StringTokenizer tok = new StringTokenizer (defaults, ","); + while (tok.hasMoreTokens ()) + { + Component c = Component.forName (tok.nextToken ()); + if (c != null) + PreciseFilter.GLOBAL.enable (c); + SYSTEM.log (java.util.logging.Level.INFO, "enabled: {0}", c); + } + } + + java.util.logging.Handler[] h = SYSTEM.getHandlers (); + for (int i = 0; i < h.length; i++) + System.out.println (h[i]); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidClassException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidClassException.java new file mode 100644 index 00000000000..cd276be8c7a --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidClassException.java @@ -0,0 +1,63 @@ +/* InvalidClassException.java -- invalid/unknown class reference id exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown by the JDWP back-end when an invalid reference + * type id is used by the debugger. + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidClassException + extends JdwpException +{ + public InvalidClassException (long id) + { + super (JdwpConstants.Error.INVALID_CLASS, + "invalid class id (" + id + ")"); + } + + public InvalidClassException (Throwable t) + { + super (JdwpConstants.Error.INVALID_CLASS, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidCountException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidCountException.java new file mode 100644 index 00000000000..d5f40c96b1f --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidCountException.java @@ -0,0 +1,61 @@ +/* InvalidCountException -- an invalid count exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when a count filter is given an invalid count. + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidCountException + extends JdwpException +{ + public InvalidCountException (int id) + { + super (JdwpConstants.Error.INVALID_COUNT, "invalid count (" + id + ")"); + } + + public InvalidCountException (Throwable t) + { + super (JdwpConstants.Error.INVALID_COUNT, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidEventTypeException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidEventTypeException.java new file mode 100644 index 00000000000..e0402283ff9 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidEventTypeException.java @@ -0,0 +1,63 @@ +/* InvalidEventTypeException.java -- an invalid event kind exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when the debugger asks for an event request + * for a non-existant event + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidEventTypeException + extends JdwpException +{ + public InvalidEventTypeException (byte kind) + { + super (JdwpConstants.Error.INVALID_EVENT_TYPE, + "invalid event type (" + kind + ")"); + } + + public InvalidEventTypeException (Throwable t) + { + super (JdwpConstants.Error.INVALID_EVENT_TYPE, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidObjectException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidObjectException.java new file mode 100644 index 00000000000..49e8ac1e56d --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidObjectException.java @@ -0,0 +1,63 @@ +/* InvalidObjectException.java -- an invalid object id exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when an invalid object id is used by + * the debugger + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidObjectException + extends JdwpException +{ + public InvalidObjectException (long id) + { + super (JdwpConstants.Error.INVALID_OBJECT, + "invalid object id (" + id + ")"); + } + + public InvalidObjectException (Throwable t) + { + super (JdwpConstants.Error.INVALID_OBJECT, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidStringException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidStringException.java new file mode 100644 index 00000000000..c84a960f163 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidStringException.java @@ -0,0 +1,68 @@ +/* InvalidStringException.java -- an invalid string exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when the debugger uses an invalid String. + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidStringException + extends JdwpException +{ + public InvalidStringException (String str) + { + super (JdwpConstants.Error.INVALID_STRING, + "invalid string (" + str + ")"); + } + + public InvalidStringException (long id) + { + super (JdwpConstants.Error.INVALID_STRING, + "invalid string id (" + id + ")"); + } + + public InvalidStringException (Throwable t) + { + super (JdwpConstants.Error.INVALID_STRING, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidThreadException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidThreadException.java new file mode 100644 index 00000000000..985b1a79109 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidThreadException.java @@ -0,0 +1,63 @@ +/* InvalidThreadException.java -- an invalid thread exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when an invalid thread is used + * by the debugger + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidThreadException + extends JdwpException +{ + public InvalidThreadException (long id) + { + super (JdwpConstants.Error.INVALID_THREAD, + "invalid thread id (" + id + ")"); + } + + public InvalidThreadException (Throwable t) + { + super (JdwpConstants.Error.INVALID_THREAD, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/InvalidThreadGroupException.java b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidThreadGroupException.java new file mode 100644 index 00000000000..04762189210 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/InvalidThreadGroupException.java @@ -0,0 +1,63 @@ +/* InvalidThreadGroupException.java -- an invalid thread group exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when an invalid thread group is used + * by the debugger + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class InvalidThreadGroupException + extends JdwpException +{ + public InvalidThreadGroupException (long id) + { + super (JdwpConstants.Error.INVALID_THREAD_GROUP, + "invalid thread id (" + id + ")"); + } + + public InvalidThreadGroupException (Throwable t) + { + super (JdwpConstants.Error.INVALID_THREAD, t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/JdwpException.java b/libjava/classpath/gnu/classpath/jdwp/exception/JdwpException.java new file mode 100644 index 00000000000..5c96cc56b67 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/JdwpException.java @@ -0,0 +1,86 @@ +/* JdwpException.java -- an exception base class for all JDWP exceptions + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +/** + * A base class exception for all JDWP back-end exceptions + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class JdwpException + extends Exception +{ + // The integer error code defined by JDWP + private short _errorCode; + + /** + * Constructs a new <code>JdwpException</code> with the + * given error code and given cause + * + * @param code the JDWP error code + * @param t the cause of the exception + */ + public JdwpException (short code, Throwable t) + { + super (t); + _errorCode = code; + } + + /** + * Constructs a new <code>JdwpException</code> with the + * given error code and string error message + * + * @param code the JDWP error code + * @param str an error message + */ + public JdwpException (short code, String str) + { + super (str); + _errorCode = code; + } + + /** + * Returns the JDWP error code represented by this exception + */ + public short getErrorCode () + { + return _errorCode; + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/JdwpInternalErrorException.java b/libjava/classpath/gnu/classpath/jdwp/exception/JdwpInternalErrorException.java new file mode 100644 index 00000000000..c022dc53742 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/JdwpInternalErrorException.java @@ -0,0 +1,57 @@ +/* JdwpInternalErrorException.java -- an internal error exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown by the JDWP back-end when an unusual runtime + * error occurs internally + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class JdwpInternalErrorException + extends JdwpException +{ + public JdwpInternalErrorException (Throwable cause) + { + super (JdwpConstants.Error.INTERNAL, cause); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/NotImplementedException.java b/libjava/classpath/gnu/classpath/jdwp/exception/NotImplementedException.java new file mode 100644 index 00000000000..fba627ea274 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/NotImplementedException.java @@ -0,0 +1,58 @@ +/* NotImplementedException.java -- an exception for unimplemented JDWP + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown by virtual machines when functionality + * or features are not implemented + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class NotImplementedException + extends JdwpException +{ + public NotImplementedException (String feature) + { + super (JdwpConstants.Error.NOT_IMPLEMENTED, + feature + " is not yet implemented"); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/exception/VmDeadException.java b/libjava/classpath/gnu/classpath/jdwp/exception/VmDeadException.java new file mode 100644 index 00000000000..f3c4a152bfb --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/exception/VmDeadException.java @@ -0,0 +1,55 @@ +/* VmDeadException.java -- dead virtual machine exception + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath.jdwp.exception; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * An exception thrown when the virtual machine is dead + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class VmDeadException + extends JdwpException +{ + public VmDeadException () + { + super (JdwpConstants.Error.VM_DEAD, "Virtual machine is dead"); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ArrayId.java b/libjava/classpath/gnu/classpath/jdwp/id/ArrayId.java new file mode 100644 index 00000000000..cd428a172b3 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ArrayId.java @@ -0,0 +1,62 @@ +/* ArrayId.java -- array object IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A class which represents a JDWP array id + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ArrayId + extends ObjectId +{ + // Arrays are handled a little differently than other IDs + //public static final Class typeClass = UNDEFINED + + /** + * Constructs a new <code>ArrayId</code> + */ + public ArrayId () + { + super (JdwpConstants.Tag.ARRAY); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ArrayReferenceTypeId.java b/libjava/classpath/gnu/classpath/jdwp/id/ArrayReferenceTypeId.java new file mode 100644 index 00000000000..14a73dc5b24 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ArrayReferenceTypeId.java @@ -0,0 +1,59 @@ +/* ArrayReferenceTypeId.java -- array reference type ids + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A reference type ID representing java arrays + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ArrayReferenceTypeId + extends ReferenceTypeId +{ + /** + * Constructs a new <code>ArrayReferenceTypeId</code> + */ + public ArrayReferenceTypeId () + { + super (JdwpConstants.TypeTag.ARRAY); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ClassLoaderId.java b/libjava/classpath/gnu/classpath/jdwp/id/ClassLoaderId.java new file mode 100644 index 00000000000..133872566fd --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ClassLoaderId.java @@ -0,0 +1,64 @@ +/* ClassLoaderId.java -- class loader IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A class which represents a JDWP thread id + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ClassLoaderId + extends ObjectId +{ + /** + * The object class that this id represents + */ + public static final Class typeClass = ClassLoader.class; + + /** + * Constructs a new <code>ClassLoaderId</code> + */ + public ClassLoaderId () + { + super (JdwpConstants.Tag.CLASS_LOADER); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ClassObjectId.java b/libjava/classpath/gnu/classpath/jdwp/id/ClassObjectId.java new file mode 100644 index 00000000000..e5559ce10f3 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ClassObjectId.java @@ -0,0 +1,64 @@ +/* ClassObjectId.java -- class object IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A class which represents a JDWP class object id + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ClassObjectId + extends ObjectId +{ + /** + * The object class that this id represents + */ + public static final Class typeClass = Class.class; + + /** + * Constructs a new <code>ClassObjectId</code> + */ + public ClassObjectId () + { + super (JdwpConstants.Tag.CLASS_OBJECT); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ClassReferenceTypeId.java b/libjava/classpath/gnu/classpath/jdwp/id/ClassReferenceTypeId.java new file mode 100644 index 00000000000..6b57673f827 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ClassReferenceTypeId.java @@ -0,0 +1,59 @@ +/* ClassReferenceTypeId.java -- class reference type ids + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A reference type ID representing java classes + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ClassReferenceTypeId + extends ReferenceTypeId +{ + /** + * Constructs a new <code>ClassReferenceTypeId</code> + */ + public ClassReferenceTypeId () + { + super (JdwpConstants.TypeTag.CLASS); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/InterfaceReferenceTypeId.java b/libjava/classpath/gnu/classpath/jdwp/id/InterfaceReferenceTypeId.java new file mode 100644 index 00000000000..bdbd6b6ebff --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/InterfaceReferenceTypeId.java @@ -0,0 +1,59 @@ +/* InterfaceReferenceTypeId.java -- interface reference type ids + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A reference type ID representing java interfaces + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class InterfaceReferenceTypeId + extends ReferenceTypeId +{ + /** + * Constructs a new <code>InterfaceReferenceTypeId</code> + */ + public InterfaceReferenceTypeId () + { + super (JdwpConstants.TypeTag.INTERFACE); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/JdwpId.java b/libjava/classpath/gnu/classpath/jdwp/id/JdwpId.java new file mode 100644 index 00000000000..37f82e208c5 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/JdwpId.java @@ -0,0 +1,127 @@ +/* JdwpId.java -- base class for all object ID types + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A baseclass for all object types reported to the debugger + * + * @author Keith Seitz <keiths@redhat.com> + */ +public abstract class JdwpId +{ + /** + * ID assigned to this object + */ + protected long _id; + + /** + * Tag of ID's type (see {@link gnu.classpath.jdwp.JdwpConstants.Tag}) + * for object-like IDs or the type tag (see {@link + * gnu.classpath.JdwpConstants.TypeTag}) for reference type IDs. + */ + private byte _tag; + + /** + * Constructs an empty <code>JdwpId</code> + */ + public JdwpId (byte tag) + { + _tag = tag; + } + + /** + * Sets the id for this object reference + */ + void setId (long id) + { + _id = id; + } + + /** + * Returns the id for this object reference + */ + public long getId () + { + return _id; + } + + /** + * Compares two object ids for equality. Two object ids + * are equal if they point to the same type and contain to + * the same id number. (NOTE: This is a much stricter check + * than is necessary: all <code>JdwpId</code>s have unique + * ids.) + */ + public boolean equals (JdwpId id) + { + return ((id.getClass () == getClass ()) && (id.getId () == getId ())); + } + + /** + * Returns size of this type (used by IDSizes) + */ + public abstract int size (); + + /** + * Writes the contents of this type to the <code>DataOutputStream</code> + * @param outStream the <code>DataOutputStream</code> to use + * @throws IOException when an error occurs on the <code>OutputStream</code> + */ + public abstract void write (DataOutputStream outStream) + throws IOException; + + /** + * Writes the contents of this type to the output stream, preceded + * by a one-byte tag for tagged object IDs or type tag for + * reference type IDs. + * + * @param outStream the <code>DataOutputStream</code> to use + * @throws IOException when an error occurs on the <code>OutputStream</code> + */ + public void writeTagged (DataOutputStream outStream) + throws IOException + { + outStream.writeByte (_tag); + write (outStream); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/JdwpIdFactory.java b/libjava/classpath/gnu/classpath/jdwp/id/JdwpIdFactory.java new file mode 100644 index 00000000000..06ec3c7681f --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/JdwpIdFactory.java @@ -0,0 +1,165 @@ +/* JdwpIdFactory.java -- factory for generating type and object IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import java.util.HashMap; + +/** + * This factory generates ids for objects and types that may + * be sent to a debugger. + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class JdwpIdFactory +{ + // ID of last object / referencetype + private static Object _idLock = new Object (); + private static Object _ridLock = new Object (); + private static long _lastId = 0; + private static long _lastRid = 0; + + // A list of all ID types + private static HashMap _idList = new HashMap (); + + // Initialize the id list with known types + static + { + // ObjectId and ArrayId are special cases. See newId. + _idList.put (ClassLoaderId.typeClass, ClassLoaderId.class); + _idList.put (ClassObjectId.typeClass, ClassObjectId.class); + //_idList.put (FieldId.typeClass, FieldId.class); + //_idList.put (FrameId.typeClass, FrameId.class); + //_idList.put (MethodId.typeClass, MethodId.class); + _idList.put (StringId.typeClass, StringId.class); + _idList.put (ThreadId.typeClass, ThreadId.class); + _idList.put (ThreadGroupId.typeClass, ThreadGroupId.class); + } + + /** + * Returns a new id for the given object + * + * @param object the object for which an id is desired + * @returns a suitable object id + */ + public static JdwpId newId (Object object) + { + JdwpId id = null; + + // Special case: arrays + if (object.getClass ().isArray ()) + id = new ArrayId (); + else + { + // Loop through all classes until we hit baseclass + Class myClass; + for (myClass = object.getClass (); myClass != null; + myClass = myClass.getSuperclass ()) + { + Class clz = (Class) _idList.get (myClass); + if (clz != null) + { + try + { + id = (JdwpId) clz.newInstance (); + synchronized (_idLock) + { + id.setId (++_lastId); + } + return id; + } + catch (InstantiationException ie) + { + // This really should not happen + throw new RuntimeException ("cannot create new ID", ie); + } + catch (IllegalAccessException iae) + { + // This really should not happen + throw new RuntimeException ("illegal access of ID", iae); + } + } + } + + /* getSuperclass returned null and no matching ID type found. + So it must derive from Object. */ + id = new ObjectId (); + } + + synchronized (_idLock) + { + id.setId (++_lastId); + } + + return id; + } + + /** + * Returns a new reference type id for the given class + * + * @param clazz the <code>Class</code> for which an id is desired + * @returns a suitable reference type id or <code>null</code> + */ + public static ReferenceTypeId newReferenceTypeId (Class clazz) + { + ReferenceTypeId id = null; + try + { + if (clazz.isArray ()) + id = new ArrayReferenceTypeId (); + else if (clazz.isInterface ()) + id = new InterfaceReferenceTypeId (); + else + id = new ClassReferenceTypeId (); + synchronized (_ridLock) + { + id.setId (++_lastRid); + } + return id; + } + catch (InstantiationException ie) + { + return null; + } + catch (IllegalAccessException iae) + { + return null; + } + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ObjectId.java b/libjava/classpath/gnu/classpath/jdwp/id/ObjectId.java new file mode 100644 index 00000000000..e34a3b59ebf --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ObjectId.java @@ -0,0 +1,99 @@ +/* ObjectId.java -- object IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A class which represents a JDWP object id for an object + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ObjectId + extends JdwpId +{ + /** + * The object class that this id represents + */ + public static final Class typeClass = Object.class; + + /** + * Constructs a new <code>ObjectId</code> + */ + public ObjectId () + { + super (JdwpConstants.Tag.OBJECT); + } + + /** + * Constructs a new <code>ObjectId</code> of the + * given type. + * + * @param tag the tag of this type of object ID + */ + public ObjectId (byte tag) + { + super (tag); + } + + /** + * Returns the size of this id type + */ + public int size () + { + return 8; + } + + /** + * Writes the id to the stream + * + * @param outStream the stream to which to write + * @throws IOException when an error occurs on the <code>OutputStream</code> + */ + public void write (DataOutputStream outStream) + throws IOException + { + // All we need to do is write out our id as an 8-byte integer + outStream.writeLong (_id); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ReferenceTypeId.java b/libjava/classpath/gnu/classpath/jdwp/id/ReferenceTypeId.java new file mode 100644 index 00000000000..cdb78040a41 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ReferenceTypeId.java @@ -0,0 +1,81 @@ +/* ReferenceTypeId.java -- a base class for all reference type IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * Base class for reference type IDs. This class usurps + * <code>JdwpId</code>'s tag member for its own use (type tag). + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ReferenceTypeId + extends JdwpId +{ + /** + * Constructor used by {Array,Interface,Class}ReferenceTypeId + */ + public ReferenceTypeId (byte tag) + { + super (tag); + } + + /** + * Returns the size of this ID type + */ + public int size () + { + return 8; + } + + /** + * Outputs the reference type ID to the given output stream + * + * @param outStream the stream to which to write the data + * @throws IOException for errors writing to the stream + */ + public void write (DataOutputStream outStream) + throws IOException + { + outStream.writeLong (_id); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/StringId.java b/libjava/classpath/gnu/classpath/jdwp/id/StringId.java new file mode 100644 index 00000000000..ea1a83a56a2 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/StringId.java @@ -0,0 +1,64 @@ +/* StringId.java -- string IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A class which represents a JDWP string id + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class StringId + extends ObjectId +{ + /** + * The object class that this id represents + */ + public static final Class typeClass = String.class; + + /** + * Constructs a new <code>StringId</code> + */ + public StringId () + { + super (JdwpConstants.Tag.STRING); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ThreadGroupId.java b/libjava/classpath/gnu/classpath/jdwp/id/ThreadGroupId.java new file mode 100644 index 00000000000..aef7d5b5421 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ThreadGroupId.java @@ -0,0 +1,64 @@ +/* ThreadGroupId.java -- thread group IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A class which represents a JDWP thread group id + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ThreadGroupId + extends ObjectId +{ + /** + * The object class that this id represents + */ + public static final Class typeClass = ThreadGroup.class; + + /** + * Constructs a new <code>ThreadGroupId</code> + */ + public ThreadGroupId () + { + super (JdwpConstants.Tag.THREAD_GROUP); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/id/ThreadId.java b/libjava/classpath/gnu/classpath/jdwp/id/ThreadId.java new file mode 100644 index 00000000000..733bf55102b --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/id/ThreadId.java @@ -0,0 +1,64 @@ +/* ThreadId.java -- thread IDs + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.id; + +import gnu.classpath.jdwp.JdwpConstants; + +/** + * A class which represents a JDWP thread id + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class ThreadId + extends ObjectId +{ + /** + * The object class that this id represents + */ + public static final Class typeClass = Thread.class; + + /** + * Constructs a new <code>ThreadId</code> + */ + public ThreadId () + { + super (JdwpConstants.Tag.THREAD); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/CommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/CommandSet.java new file mode 100644 index 00000000000..17b956ceadc --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/CommandSet.java @@ -0,0 +1,68 @@ +/* CommandSet.java -- An interface defining JDWP Command Sets + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.exception.JdwpException; + +import java.io.DataOutputStream; +import java.nio.ByteBuffer; + +/** + * A class representing a JDWP Command Set. This class serves as a generic + * interface for all Command Sets types used by JDWP. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public interface CommandSet +{ + /** + * Runs the given command with the data in distr and writes the data for the + * reply packet to ostr. + * + * @param bb holds the data portion of the Command Packet + * @param os data portion of the Reply Packet will be written here + * @param command the command field of the Command Packet + * @return true if the JDWP layer should shut down in response to this packet + * @throws JdwpException command wasn't carried out successfully + */ + public boolean runCommand(ByteBuffer bb, DataOutputStream os, + byte command) + throws JdwpException; +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/FieldCommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/FieldCommandSet.java new file mode 100644 index 00000000000..f14635b9188 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/FieldCommandSet.java @@ -0,0 +1,66 @@ +/* FieldCommandSet.java -- class to implement the Field Command Set + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.exception.NotImplementedException; + +import java.io.DataOutputStream; +import java.nio.ByteBuffer; + +/** + * A class representing the Field Command Set. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class FieldCommandSet implements CommandSet +{ + /** + * There are no commands for this CommandSet at this time so we just throw a + * NotImplementedException whenever it's called. + * + * @throws JdwpException An exception will always be thrown + */ + public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command) + throws JdwpException + { + throw new NotImplementedException( + "No commands for command set Field implemented."); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/InterfaceTypeCommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/InterfaceTypeCommandSet.java new file mode 100644 index 00000000000..a32da38b324 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/InterfaceTypeCommandSet.java @@ -0,0 +1,67 @@ +/* InterfaceTypeCommandSet.java -- class to implement the InterfaceType + Command Set + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.exception.NotImplementedException; + +import java.io.DataOutputStream; +import java.nio.ByteBuffer; + +/** + * A class representing the InterfaceType Command Set. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class InterfaceTypeCommandSet implements CommandSet +{ + /** + * There are no commands for this CommandSet at this time so we just throw a + * NotImplementedException whenever it's called. + * + * @throws JdwpException An exception will always be thrown + */ + public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command) + throws JdwpException + { + throw new NotImplementedException( + "No commands for command set InterfaceType implemented."); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/ObjectReferenceCommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/ObjectReferenceCommandSet.java new file mode 100644 index 00000000000..38bb0cdbf96 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/ObjectReferenceCommandSet.java @@ -0,0 +1,249 @@ +/* ObjectReferenceCommandSet.java -- lass to implement the ObjectReference + Command Set + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.IVirtualMachine; +import gnu.classpath.jdwp.Jdwp; +import gnu.classpath.jdwp.JdwpConstants; +import gnu.classpath.jdwp.exception.InvalidFieldException; +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.exception.JdwpInternalErrorException; +import gnu.classpath.jdwp.exception.NotImplementedException; +import gnu.classpath.jdwp.id.IdManager; +import gnu.classpath.jdwp.id.ObjectId; +import gnu.classpath.jdwp.id.ReferenceTypeId; +import gnu.classpath.jdwp.util.Value; +import gnu.classpath.jdwp.util.MethodInvoker; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; + +/** + * A class representing the ObjectReference Command Set. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class ObjectReferenceCommandSet implements CommandSet +{ + // Our hook into the jvm + private final IVirtualMachine vm = Jdwp.getIVirtualMachine(); + + // Manages all the different ids that are assigned by jdwp + private final IdManager idMan = Jdwp.getIdManager(); + + public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command) + throws JdwpException + { + try + { + switch (command) + { + case JdwpConstants.CommandSet.ObjectReference.REFERENCE_TYPE: + executeReferenceType(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.GET_VALUES: + executeGetValues(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.SET_VALUES: + executeSetValues(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.MONITOR_INFO: + executeMonitorInfo(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.INVOKE_METHOD: + executeInvokeMethod(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.DISABLE_COLLECTION: + executeDisableCollection(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.ENABLE_COLLECTION: + executeEnableCollection(bb, os); + break; + case JdwpConstants.CommandSet.ObjectReference.IS_COLLECTED: + executeIsCollected(bb, os); + break; + default: + throw new NotImplementedException("Command " + command + + " not found in String Reference Command Set."); + } + } + catch (IOException ex) + { + // The DataOutputStream we're using isn't talking to a socket at all + // So if we throw an IOException we're in serious trouble + throw new JdwpInternalErrorException(ex); + } + return true; + } + + private void executeReferenceType(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + Object obj = oid.getObject(); + Class clazz = obj.getClass(); + ReferenceTypeId refId = idMan.getReferenceTypeId(clazz); + refId.writeTagged(os); + } + + private void executeGetValues(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + Object obj = oid.getObject(); + + int numFields = bb.getInt(); + + os.writeInt(numFields); // Looks pointless but this is the protocol + + for (int i = 0; i < numFields; i++) + { + Field field = (Field) idMan.readId(bb).getObject(); + Value.writeValueFromField(os, field, obj); + } + } + + private void executeSetValues(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + Object obj = oid.getObject(); + + int numFields = bb.getInt(); + + for (int i = 0; i < numFields; i++) + { + Field field = (Field) idMan.readId(bb).getObject(); + Object value = Value.getObj(bb, field); + try + { + field.set(obj, value); + } + catch (IllegalArgumentException ex) + { + // I suppose this would best qualify as an invalid field then + throw new InvalidFieldException(ex); + } + catch (IllegalAccessException ex) + { + // We should be able to access any field + throw new JdwpInternalErrorException(ex); + } + } + } + + private void executeMonitorInfo(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // This command is optional, determined by VirtualMachines CapabilitiesNew + // so we'll leave it till later to implement + throw new NotImplementedException( + "Command ExecuteMonitorInfo not implemented."); + + } + + private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + Object obj = oid.getObject(); + + ObjectId tid = idMan.readId(bb); + Thread thread = (Thread) tid.getObject(); + + ReferenceTypeId rid = idMan.readReferenceTypeId(bb); + Class clazz = rid.getType(); + + ObjectId mid = idMan.readId(bb); + Method method = (Method) mid.getObject(); + + int args = bb.getInt(); + Object[] values = new Object[args]; + + for (int i = 0; i < args; i++) + { + values[i] = Value.getObj(bb); + } + + int invokeOptions = bb.getInt(); + + if ((invokeOptions & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED) != 0) + { // We must suspend all other running threads first + vm.suspendAllThreads(); + } + boolean nonVirtual; + if ((invokeOptions & JdwpConstants.InvokeOptions.INVOKE_NONVIRTUAL) != 0) + nonVirtual = true; + else + nonVirtual = false; + MethodInvoker vmi = new MethodInvoker(vm); + + vmi.executeMethod(obj, thread, clazz, method, values, nonVirtual); + Object value = vmi.getReturnedValue(); + ObjectId exceptionId = vmi.getExceptionId(); + + Value.writeValue(os, value); + exceptionId.writeTagged(os); + } + + private void executeDisableCollection(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + oid.disableCollection(); + } + + private void executeEnableCollection(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + oid.enableCollection(); + } + + private void executeIsCollected(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = idMan.readId(bb); + boolean collected = oid.isCollected(); + os.writeBoolean(collected); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/PacketProcessor.java b/libjava/classpath/gnu/classpath/jdwp/processor/PacketProcessor.java new file mode 100644 index 00000000000..914494c249f --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/PacketProcessor.java @@ -0,0 +1,216 @@ +/* PacketProcessor.java -- a thread which processes command packets + from the debugger + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.Jdwp; +import gnu.classpath.jdwp.JdwpConstants; +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.transport.JdwpCommandPacket; +import gnu.classpath.jdwp.transport.JdwpConnection; +import gnu.classpath.jdwp.transport.JdwpPacket; +import gnu.classpath.jdwp.transport.JdwpReplyPacket; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * This class is responsible for processing packets from the + * debugger. It waits for an available packet from the connection + * ({@link gnu.classpath.jdwp.transport.JdwpConnection}) and then + * processes the packet and any reply. + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class PacketProcessor + extends Thread +{ + // The connection to the debugger + private JdwpConnection _connection; + + // Shutdown this thread? + private boolean _shutdown; + + // A Mapping of the command set (Byte) to the specific CommandSet + private CommandSet[] _sets; + + // Contents of the ReplyPackets data field + private ByteArrayOutputStream _outputBytes; + + // Output stream around _outputBytes + private DataOutputStream _os; + + /** + * Constructs a new <code>PacketProcessor</code> object + * Connection must be validated before getting here! + * + * @param con the connection + */ + public PacketProcessor (JdwpConnection con) + { + _connection = con; + _shutdown = false; + + // MAXIMUM is the value of the largest command set we may receive + _sets = new CommandSet[JdwpConstants.CommandSet.MAXIMUM + 1]; + _outputBytes = new ByteArrayOutputStream(); + _os = new DataOutputStream (_outputBytes); + + // Create all the Command Sets and add them to our array + _sets[JdwpConstants.CommandSet.VirtualMachine.CS_VALUE] = + new VirtualMachineCommandSet(); + _sets[JdwpConstants.CommandSet.ReferenceType.CS_VALUE] = + new ReferenceTypeCommandSet(); + _sets[JdwpConstants.CommandSet.ClassType.CS_VALUE] = + new ClassTypeCommandSet(); + _sets[JdwpConstants.CommandSet.ArrayType.CS_VALUE] = + new ArrayTypeCommandSet(); + _sets[JdwpConstants.CommandSet.InterfaceType.CS_VALUE] = + new InterfaceTypeCommandSet(); + _sets[JdwpConstants.CommandSet.Method.CS_VALUE] = + new MethodCommandSet(); + _sets[JdwpConstants.CommandSet.Field.CS_VALUE] = + new FieldCommandSet(); + _sets[JdwpConstants.CommandSet.ObjectReference.CS_VALUE] = + new ObjectReferenceCommandSet(); + _sets[JdwpConstants.CommandSet.StringReference.CS_VALUE] = + new StringReferenceCommandSet(); + _sets[JdwpConstants.CommandSet.ThreadReference.CS_VALUE] = + new ThreadReferenceCommandSet(); + _sets[JdwpConstants.CommandSet.ThreadGroupReference.CS_VALUE] = + new ThreadGroupReferenceCommandSet(); + _sets[JdwpConstants.CommandSet.ArrayReference.CS_VALUE] = + new ArrayReferenceCommandSet(); + _sets[JdwpConstants.CommandSet.ClassLoaderReference.CS_VALUE] = + new ClassLoaderReferenceCommandSet(); + _sets[JdwpConstants.CommandSet.EventRequest.CS_VALUE] = + new EventRequestCommandSet(); + _sets[JdwpConstants.CommandSet.StackFrame.CS_VALUE] = + new StackFrameCommandSet(); + _sets[JdwpConstants.CommandSet.ClassObjectReference.CS_VALUE] = + new ClassObjectReferenceCommandSet(); + } + + /** + * Main run routine for this thread. Will loop getting packets + * from the connection and processing them. + */ + public void run () + { + try + { + while (!_shutdown) + { + _processOnePacket (); + } + } + catch (IOException ex) + { + ex.printStackTrace(); + } + // Time to shutdown, tell Jdwp to shutdown + Jdwp.getDefault().shutdown(); + } + + /** + * Shutdown the packet processor + */ + public void shutdown () + { + _shutdown = true; + interrupt (); + } + + // Helper function which actually does all the work of waiting + // for a packet and getting it processed. + private void _processOnePacket () + throws IOException + { + JdwpPacket pkt = _connection.getPacket (); + + if (!(pkt instanceof JdwpCommandPacket)) + { + // We're not supposed to get these from the debugger! + // Drop it on the floor + return; + } + + if (pkt != null) + { + JdwpCommandPacket commandPkt = (JdwpCommandPacket) pkt; + JdwpReplyPacket reply = new JdwpReplyPacket(commandPkt); + + // Reset our output stream + _outputBytes.reset(); + + // Create a ByteBuffer around the command packet + ByteBuffer bb = ByteBuffer.wrap(commandPkt.getData()); + byte command = commandPkt.getCommand(); + byte commandSet = commandPkt.getCommandSet(); + + CommandSet set = null; + try + { + // There is no command set with value 0 + if (commandSet > 0 && commandSet < _sets.length) + { + set = _sets[commandPkt.getCommandSet()]; + } + if (set != null) + { + _shutdown = set.runCommand(bb, _os, command); + reply.setData(_outputBytes.toByteArray()); + } + else + { + // This command set wasn't in our tree + reply.setErrorCode(JdwpConstants.Error.NOT_IMPLEMENTED); + } + } + catch (JdwpException ex) + { + reply.setErrorCode(ex.getErrorCode ()); + } + _connection.sendPacket (reply); + } + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/ReferenceTypeCommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/ReferenceTypeCommandSet.java new file mode 100644 index 00000000000..34def9f2d07 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/ReferenceTypeCommandSet.java @@ -0,0 +1,321 @@ +/* ReferenceTypeCommandSet.java -- lass to implement the ReferenceType + Command Set + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.IVirtualMachine; +import gnu.classpath.jdwp.Jdwp; +import gnu.classpath.jdwp.JdwpConstants; +import gnu.classpath.jdwp.exception.InvalidFieldException; +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.exception.JdwpInternalErrorException; +import gnu.classpath.jdwp.exception.NotImplementedException; +import gnu.classpath.jdwp.id.IdManager; +import gnu.classpath.jdwp.id.ObjectId; +import gnu.classpath.jdwp.id.ReferenceTypeId; +import gnu.classpath.jdwp.util.JdwpString; +import gnu.classpath.jdwp.util.Signature; +import gnu.classpath.jdwp.util.Value; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; + +/** + * A class representing the ReferenceType Command Set. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class ReferenceTypeCommandSet implements CommandSet +{ + // Our hook into the jvm + private final IVirtualMachine vm = Jdwp.getIVirtualMachine(); + + // Manages all the different ids that are assigned by jdwp + private final IdManager idMan = Jdwp.getIdManager(); + + public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command) + throws JdwpException + { + try + { + switch (command) + { + case JdwpConstants.CommandSet.ReferenceType.SIGNATURE: + executeSignature(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.CLASS_LOADER: + executeClassLoader(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.MODIFIERS: + executeModifiers(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.FIELDS: + executeFields(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.METHODS: + executeMethods(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.GET_VALUES: + executeGetValues(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.SOURCE_FILE: + executeSourceFile(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.NESTED_TYPES: + executeNestedTypes(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.STATUS: + executeStatus(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.INTERFACES: + executeInterfaces(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.CLASS_OBJECT: + executeClassObject(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.SOURCE_DEBUG_EXTENSION: + executeSourceDebugExtension(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.SIGNATURE_WITH_GENERIC: + executeSignatureWithGeneric(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.FIELDS_WITH_GENERIC: + executeFieldWithGeneric(bb, os); + break; + case JdwpConstants.CommandSet.ReferenceType.METHODS_WITH_GENERIC: + executeMethodsWithGeneric(bb, os); + break; + default: + throw new NotImplementedException("Command " + command + + " not found in String Reference Command Set."); + } + } + catch (IOException ex) + { + // The DataOutputStream we're using isn't talking to a socket at all + // So if we throw an IOException we're in serious trouble + throw new JdwpInternalErrorException(ex); + } + return true; + } + + private void executeSignature(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + String sig = Signature.computeClassSignature(refId.getType()); + JdwpString.writeString(os, sig); + } + + private void executeClassLoader(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + + Class clazz = refId.getType(); + ClassLoader loader = clazz.getClassLoader(); + ObjectId oid = idMan.getId(loader); + oid.write(os); + } + + private void executeModifiers(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + + Class clazz = refId.getType(); + os.writeInt(clazz.getModifiers()); + } + + private void executeFields(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + + Field[] fields = clazz.getFields(); + os.writeInt(fields.length); + for (int i = 0; i < fields.length; i++) + { + Field field = fields[i]; + idMan.getId(field).write(os); + JdwpString.writeString(os, field.getName()); + JdwpString.writeString(os, Signature.computeFieldSignature(field)); + os.writeInt(field.getModifiers()); + } + } + + private void executeMethods(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + + Method[] methods = clazz.getMethods(); + os.writeInt(methods.length); + for (int i = 0; i < methods.length; i++) + { + Method method = methods[i]; + idMan.getId(method).write(os); + JdwpString.writeString(os, method.getName()); + JdwpString.writeString(os, Signature.computeMethodSignature(method)); + os.writeInt(method.getModifiers()); + } + } + + private void executeGetValues(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + + int numFields = bb.getInt(); + os.writeInt(numFields); // Looks pointless but this is the protocol + for (int i = 0; i < numFields; i++) + { + ObjectId fieldId = idMan.readId(bb); + Field field = (Field) (fieldId.getObject()); + Class fieldClazz = field.getDeclaringClass(); + + // We don't actually need the clazz to get the field but we might as + // well check that the debugger got it right + if (fieldClazz.isAssignableFrom(clazz)) + Value.writeStaticValueFromField(os, field); + else + throw new InvalidFieldException(fieldId.getId()); + } + } + + private void executeSourceFile(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + + // We'll need to go into the jvm for this unless there's an easier way + String sourceFileName = vm.getSourceFile(clazz); + JdwpString.writeString(os, sourceFileName); + // clazz.getProtectionDomain().getCodeSource().getLocation(); + } + + private void executeNestedTypes(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + Class[] declaredClazzes = clazz.getDeclaredClasses(); + os.writeInt(declaredClazzes.length); + for (int i = 0; i < declaredClazzes.length; i++) + { + Class decClazz = declaredClazzes[i]; + ReferenceTypeId clazzId = idMan.getReferenceTypeId(decClazz); + clazzId.writeTagged(os); + } + } + + private void executeStatus(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + + // I don't think there's any other way to get this + int status = vm.getStatus(clazz); + os.writeInt(status); + } + + private void executeInterfaces(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + Class[] interfaces = clazz.getInterfaces(); + os.writeInt(interfaces.length); + for (int i = 0; i < interfaces.length; i++) + { + Class interfaceClass = interfaces[i]; + ReferenceTypeId intId = idMan.getReferenceTypeId(interfaceClass); + intId.write(os); + } + } + + private void executeClassObject(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ReferenceTypeId refId = idMan.readReferenceTypeId(bb); + Class clazz = refId.getType(); + ObjectId clazzObjectId = idMan.getId(clazz); + clazzObjectId.write(os); + } + + private void executeSourceDebugExtension(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // This command is optional, determined by VirtualMachines CapabilitiesNew + // so we'll leave it till later to implement + throw new NotImplementedException( + "Command SourceDebugExtension not implemented."); + } + + private void executeSignatureWithGeneric(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // We don't have generics yet + throw new NotImplementedException( + "Command SourceDebugExtension not implemented."); + } + + private void executeFieldWithGeneric(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // We don't have generics yet + throw new NotImplementedException( + "Command SourceDebugExtension not implemented."); + } + + private void executeMethodsWithGeneric(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // We don't have generics yet + throw new NotImplementedException( + "Command SourceDebugExtension not implemented."); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/StringReferenceCommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/StringReferenceCommandSet.java new file mode 100644 index 00000000000..1f1b04cb0cc --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/StringReferenceCommandSet.java @@ -0,0 +1,98 @@ +/* StringReferenceCommandSet.java -- class to implement the StringReference + Command Set + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.Jdwp; +import gnu.classpath.jdwp.JdwpConstants; +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.exception.JdwpInternalErrorException; +import gnu.classpath.jdwp.exception.NotImplementedException; +import gnu.classpath.jdwp.id.ObjectId; +import gnu.classpath.jdwp.util.JdwpString; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * A class representing the StringReference Command Set. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class StringReferenceCommandSet implements CommandSet +{ + + public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command) + throws JdwpException + { + try + { + + // Although there's only a single command to choose from we still use + // a switch to maintain consistency with the rest of the CommandSets + switch (command) + { + case JdwpConstants.CommandSet.StringReference.VALUE: + executeValue(bb, os); + break; + default: + throw new NotImplementedException("Command " + command + + " not found in String Reference Command Set."); + } + } + catch (IOException ex) + { + // The DataOutputStream we're using isn't talking to a socket at all + // So if we throw an IOException we're in serious trouble + throw new JdwpInternalErrorException(ex); + } + return true; + } + + private void executeValue(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = Jdwp.getIdManager().readId(bb); + + String str = (String) oid.getObject(); + JdwpString.writeString(os, str); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/processor/VirtualMachineCommandSet.java b/libjava/classpath/gnu/classpath/jdwp/processor/VirtualMachineCommandSet.java new file mode 100644 index 00000000000..32b98781727 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/processor/VirtualMachineCommandSet.java @@ -0,0 +1,474 @@ +/* VirtualMachineCommandSet.java -- class to implement the VirtualMachine + Command Set + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.processor; + +import gnu.classpath.jdwp.IVirtualMachine; +import gnu.classpath.jdwp.Jdwp; +import gnu.classpath.jdwp.JdwpConstants; +import gnu.classpath.jdwp.exception.JdwpException; +import gnu.classpath.jdwp.exception.JdwpInternalErrorException; +import gnu.classpath.jdwp.exception.NotImplementedException; +import gnu.classpath.jdwp.id.IdManager; +import gnu.classpath.jdwp.id.ObjectId; +import gnu.classpath.jdwp.id.ReferenceTypeId; +import gnu.classpath.jdwp.util.JdwpString; +import gnu.classpath.jdwp.util.Signature; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Properties; + +/** + * A class representing the VirtualMachine Command Set. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class VirtualMachineCommandSet implements CommandSet +{ + // Our hook into the jvm + private final IVirtualMachine vm = Jdwp.getIVirtualMachine(); + + // Manages all the different ids that are assigned by jdwp + private final IdManager idMan = Jdwp.getIdManager(); + + // The Jdwp object + private final Jdwp jdwp = Jdwp.getDefault(); + + public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command) + throws JdwpException + { + boolean keepRunning = true; + try + { + switch (command) + { + case JdwpConstants.CommandSet.VirtualMachine.VERSION: + executeVersion(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.CLASSES_BY_SIGNATURE: + executeClassesBySignature(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.ALL_CLASSES: + executeAllClasses(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.ALL_THREADS: + executeAllThreads(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.TOP_LEVEL_THREAD_GROUPS: + executeTopLevelThreadGroups(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.IDSIZES: + executeIDsizes(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.DISPOSE: + keepRunning = false; + executeDispose(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.SUSPEND: + executeSuspend(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.RESUME: + executeResume(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.EXIT: + keepRunning = false; + executeExit(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.CREATE_STRING: + executeCreateString(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.CAPABILITIES: + executeCapabilities(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.CLASS_PATHS: + executeClassPaths(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.DISPOSE_OBJECTS: + executeDisposeObjects(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.HOLD_EVENTS: + executeHoldEvents(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.RELEASE_EVENTS: + executeReleaseEvents(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.CAPABILITIES_NEW: + executeCapabilitiesNew(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.REDEFINE_CLASSES: + executeRedefineClasses(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.SET_DEFAULT_STRATUM: + executeSetDefaultStratum(bb, os); + break; + case JdwpConstants.CommandSet.VirtualMachine.ALL_CLASSES_WITH_GENERIC: + executeAllClassesWithGeneric(bb, os); + break; + + default: + break; + } + } + catch (IOException ex) + { + // The DataOutputStream we're using isn't talking to a socket at all + // So if we throw an IOException we're in serious trouble + throw new JdwpInternalErrorException(ex); + } + return keepRunning; + } + + private void executeVersion(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + + Properties props = System.getProperties(); + + int jdwpMajor = JdwpConstants.Version.MAJOR; + int jdwpMinor = JdwpConstants.Version.MINOR; + // The description field is pretty loosely defined + String description = "JDWP version " + jdwpMajor + "." + jdwpMinor + + ", JVM version " + props.getProperty("java.vm.name") + + " " + props.getProperty("java.vm.version") + " " + + props.getProperty("java.version"); + String vmVersion = props.getProperty("java.version"); + String vmName = props.getProperty("java.vm.name"); + JdwpString.writeString(os, description); + os.write(jdwpMajor); + os.write(jdwpMinor); + JdwpString.writeString(os, vmName); + JdwpString.writeString(os, vmVersion); + } + + private void executeClassesBySignature(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + String sig = JdwpString.readString(bb); + ArrayList allMatchingClasses = new ArrayList(); + + // This will be an Iterator over all loaded Classes + Iterator iter = vm.getAllLoadedClasses(); + + while (iter.hasNext()) + { + Class clazz = (Class) iter.next(); + String clazzSig = Signature.computeClassSignature(clazz); + if (clazzSig.equals(sig)) + allMatchingClasses.add(clazz); + } + + os.writeInt(allMatchingClasses.size()); + for (int i = 0; i < allMatchingClasses.size(); i++) + { + Class clazz = (Class) allMatchingClasses.get(i); + ReferenceTypeId id = idMan.getReferenceTypeId(clazz); + id.writeTagged(os); + int status = vm.getStatus(clazz); + os.writeInt(status); + } + } + + private void executeAllClasses(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // Disable garbage collection while we're collecting the info on loaded + // classes so we some classes don't get collected between the time we get + // the count and the time we get the list + vm.disableGarbageCollection(); + + int classCount = vm.getAllLoadedClassesCount(); + os.writeInt(classCount); + + // This will be an Iterator over all loaded Classes + Iterator iter = vm.getAllLoadedClasses(); + vm.enableGarbageCollection(); + int count = 0; + + // Note it's possible classes were created since out classCount so make + // sure we don't write more classes than we told the debugger + while (iter.hasNext() && count++ < classCount) + { + Class clazz = (Class) iter.next(); + ReferenceTypeId id = idMan.getReferenceTypeId(clazz); + id.writeTagged(os); + String sig = Signature.computeClassSignature(clazz); + JdwpString.writeString(os, sig); + int status = vm.getStatus(clazz); + os.writeInt(status); + } + } + + private void executeAllThreads(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ThreadGroup jdwpGroup = Thread.currentThread().getThreadGroup(); + ThreadGroup root = getRootThreadGroup(jdwpGroup); + + int numThreads = root.activeCount(); + Thread allThreads[] = new Thread[numThreads]; + root.enumerate(allThreads, true); + + // We need to loop through for the true count since some threads may have + // been destroyed since we got + // activeCount so those spots in the array will be null. As well we must + // ignore any threads that belong to jdwp + numThreads = 0; + for (int i = 0; i < allThreads.length; i++) + { + Thread thread = allThreads[i]; + if (thread == null) + break; // No threads after this point + if (!thread.getThreadGroup().equals(jdwpGroup)) + numThreads++; + } + + os.writeInt(numThreads); + + for (int i = 0; i < allThreads.length; i++) + { + Thread thread = allThreads[i]; + if (thread == null) + break; // No threads after this point + if (!thread.getThreadGroup().equals(jdwpGroup)) + idMan.getId(thread).write(os); + } + } + + private void executeTopLevelThreadGroups(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ThreadGroup jdwpGroup = jdwp.getJdwpThreadGroup(); + ThreadGroup root = getRootThreadGroup(jdwpGroup); + + os.writeInt(1); // Just one top level group allowed? + idMan.getId(root); + } + + private void executeDispose(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // resumeAllThreads isn't sufficient as a thread may have been + // suspended multiple times, we likely need a way to keep track of how many + // times a thread has been suspended or else a stronger resume method for + // this purpose + // vm.resumeAllThreadsExcept(jdwp.getJdwpThreadGroup()); + + // Simply shutting down the jdwp layer will take care of the rest of the + // shutdown other than disabling debugging in the VM + // vm.disableDebugging(); + + // Don't implement this until we're sure how to remove all the debugging + // effects from the VM. + throw new NotImplementedException( + "Command VirtualMachine.Dispose not implemented"); + + } + + private void executeIDsizes(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + ObjectId oid = new ObjectId(); + os.writeInt(oid.size()); // fieldId + os.writeInt(oid.size()); // methodId + os.writeInt(oid.size()); // objectId + os.writeInt(new ReferenceTypeId((byte) 0x00).size()); // referenceTypeId + os.writeInt(oid.size()); // frameId + } + + private void executeSuspend(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + vm.suspendAllThreadsExcept(jdwp.getJdwpThreadGroup()); + } + + private void executeResume(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + vm.resumeAllThreadsExcept(jdwp.getJdwpThreadGroup()); + } + + private void executeExit(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + int exitCode = bb.getInt(); + jdwp.setExit(exitCode); + } + + private void executeCreateString(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + String string = JdwpString.readString(bb); + ObjectId stringId = Jdwp.getIdManager().getId(string); + + // Since this string isn't referenced anywhere we'll disable garbage + // collection on it so it's still around when the debugger gets back to it. + stringId.disableCollection(); + stringId.write(os); + } + + private void executeCapabilities(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // Store these somewhere? + os.writeBoolean(false); // canWatchFieldModification + os.writeBoolean(false); // canWatchFieldAccess + os.writeBoolean(false); // canGetBytecodes + os.writeBoolean(false); // canGetSyntheticAttribute + os.writeBoolean(false); // canGetOwnedMonitorInfo + os.writeBoolean(false); // canGetCurrentContendedMonitor + os.writeBoolean(false); // canGetMonitorInfo + } + + private void executeClassPaths(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + String baseDir = System.getProperty("user.dir"); + JdwpString.writeString(os, baseDir); + + // Find and write the classpath + String classPath = System.getProperty("java.class.path"); + String[] paths = classPath.split(":"); + + os.writeInt(paths.length); + for (int i = 0; i < paths.length; i++) + JdwpString.writeString(os, paths[i]); + + // Now the bootpath + String bootPath = System.getProperty("sun.boot.class.path"); + paths = bootPath.split(":"); + os.writeInt(paths.length); + for (int i = 0; i < paths.length; i++) + JdwpString.writeString(os, paths[i]); + } + + private void executeDisposeObjects(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // Instead of going through the list of objects they give us it's probably + // better just to find the garbage collected objects ourselves + idMan.update(); + } + + private void executeHoldEvents(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // Going to have to implement a send queue somewhere and do this without + // triggering events + // Until then just don't implement + throw new NotImplementedException( + "Command VirtualMachine.HoldEvents not implemented"); + } + + // Opposite of executeHoldEvents + private void executeReleaseEvents(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + throw new NotImplementedException( + "Command VirtualMachine.ReleaseEvents not implemented"); + } + + private void executeCapabilitiesNew(ByteBuffer bb, DataOutputStream os) + throws JdwpException, IOException + { + // Store these somewhere? + final int CAPABILITIES_NEW_SIZE = 32; + os.writeBoolean(false); // canWatchFieldModification + os.writeBoolean(false); // canWatchFieldAccess + os.writeBoolean(false); // canGetBytecodes + os.writeBoolean(false); // canGetSyntheticAttribute + os.writeBoolean(false); // canGetOwnedMonitorInfo + os.writeBoolean(false); // canGetCurrentContendedMonitor + os.writeBoolean(false); // canGetMonitorInfo + os.writeBoolean(false); // canRedefineClasses + os.writeBoolean(false); // canAddMethod + os.writeBoolean(false); // canUnrestrictedlyRedefineClasses + os.writeBoolean(false); // canPopFrames + os.writeBoolean(false); // canUseInstanceFilters + os.writeBoolean(false); // canGetSourceDebugExtension + os.writeBoolean(false); // canRequestVMDeathEvent + os.writeBoolean(false); // canSetDefaultStratum + for (int i = 15; i < CAPABILITIES_NEW_SIZE; i++) + // Future capabilities + // currently unused + os.writeBoolean(false); // Set to false + } + + private void executeRedefineClasses(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // Optional command, don't implement + throw new NotImplementedException( + "Command VirtualMachine.RedefineClasses not implemented"); + } + + private void executeSetDefaultStratum(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // Optional command, don't implement + throw new NotImplementedException( + "Command VirtualMachine.SetDefaultStratum not implemented"); + } + + private void executeAllClassesWithGeneric(ByteBuffer bb, DataOutputStream os) + throws JdwpException + { + // We don't handle generics + throw new NotImplementedException( + "Command VirtualMachine.AllClassesWithGeneric not implemented"); + } + + /** + * Find the root ThreadGroup of this ThreadGroup + */ + private ThreadGroup getRootThreadGroup(ThreadGroup group) + { + ThreadGroup parent = group.getParent(); + + while (parent != null) + { + group = parent; + parent = group.getParent(); + } + return group; // This group was the root + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/ITransport.java b/libjava/classpath/gnu/classpath/jdwp/transport/ITransport.java new file mode 100644 index 00000000000..371cf8f06af --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/ITransport.java @@ -0,0 +1,84 @@ +/* ITransport.java -- An interface defining JDWP transports + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.IllegalArgumentException; +import java.util.HashMap; + +/** + * A class representing a transport layer. This class serves as a generic + * interface for all transport types used in the JDWP back-end. + * + * @author Keith Seitz <keiths@redhat.com> + */ +public interface ITransport +{ + /** + * Configure the transport with the given properties + * + * @param properties properties of the transport configuration + * @throws TransportException on configury error + */ + public void configure (HashMap properties) + throws TransportException; + + /** + * Initialize the transport + * + * @throws TransportException on initialization error + */ + public void initialize () + throws TransportException; + + /** + * Get the input stream for the transport + */ + public InputStream getInputStream () + throws IOException; + + /** + * Get the output stream for the transport + */ + public OutputStream getOutputStream () + throws IOException; +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/JdwpCommandPacket.java b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpCommandPacket.java new file mode 100644 index 00000000000..e99159aad9b --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpCommandPacket.java @@ -0,0 +1,149 @@ +/* JdwpCommandPacket.java -- JDWP command packet + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A class representing a JDWP command packet. + * This class adds command set and command to the packet header + * information in {@link gnu.classpath.jdwp.transport.JdwpPacket} + * and adds additional command packet-specific processing. + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class JdwpCommandPacket extends JdwpPacket +{ + /** + * Command set + */ + protected byte _commandSet; + + /** + * Command + */ + protected byte _command; + + // Minimum packet size [excluding super class] + // ( commandSet (1) + command (1) ) + private static final int MINIMUM_LENGTH = 2; + + /** + * Constructs a new <code>JdwpCommandPacket</code> + */ + public JdwpCommandPacket () + { + // Don't assign an id. This constructor is called by + // JdwpPacket.fromBytes, and that will assign a packet id. + } + + /** + * Constructs a new <code>JdwpCommandPacket</code> + * with the given command set and command + * + * @param set the command set + * @param command the command + */ + public JdwpCommandPacket (byte set, byte command) + { + _id = ++_last_id; + _commandSet = set; + _command = command; + } + + /** + * Retuns the length of this packet + */ + public int getLength () + { + return MINIMUM_LENGTH + super.getLength (); + } + + /** + * Returns the command set + */ + public byte getCommandSet () + { + return _commandSet; + } + + /** + * Sets the command set + */ + public void setCommandSet (byte cs) + { + _commandSet = cs; + } + + /** + * Returns the command + */ + public byte getCommand () + { + return _command; + } + + /** + * Sets the command + */ + public void setCommand (byte cmd) + { + _command = cmd; + } + + // Reads command packet data from the given buffer, starting + // at the given offset + protected int myFromBytes (byte[] bytes, int index) + { + int i = 0; + setCommandSet (bytes[index + i++]); + setCommand (bytes[index + i++]); + return i; + } + + // Writes the command packet data into the given buffer + protected void myWrite (DataOutputStream dos) + throws IOException + { + dos.writeByte (getCommandSet ()); + dos.writeByte (getCommand ()); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/JdwpConnection.java b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpConnection.java new file mode 100644 index 00000000000..18250d3209b --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpConnection.java @@ -0,0 +1,298 @@ +/* JdwpConnection.java -- A JDWP-speaking connection + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import gnu.classpath.jdwp.Jdwp; +import gnu.classpath.jdwp.event.Event; +import gnu.classpath.jdwp.event.EventRequest; + +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + +/** + * A connection via some transport to some JDWP-speaking entity. + * This is also a thread which handles all communications to/from + * the debugger. While access to the transport layer may be accessed by + * several threads, start-up and initialization should not be allowed + * to occur more than once. + * + * <p>This class is also a thread that is responsible for pulling + * packets off the wire and sticking them in a queue for packet + * processing threads. + * + * @author Keith Seitz (keiths@redhat.com) + */ +public class JdwpConnection + extends Thread +{ + // The JDWP handshake + private static final byte[] _HANDSHAKE = {'J', 'D', 'W', 'P', '-', 'H', 'a', + 'n', 'd', 's', 'h', 'a', 'k', 'e'}; + + // Transport method + private ITransport _transport; + + // Command queue + private ArrayList _commandQueue; + + // Shutdown flag + private boolean _shutdown; + + // Input stream from transport + private DataInputStream _inStream; + + // Output stream from transprot + private DataOutputStream _outStream; + + // A buffer used to construct the packet data + private ByteArrayOutputStream _bytes; + + // A DataOutputStream for the byte buffer + private DataOutputStream _doStream; + + /** + * Creates a new <code>JdwpConnection</code> instance + * + * @param transport the transport to use for communications + */ + public JdwpConnection (ITransport transport) + { + _transport = transport; + _commandQueue = new ArrayList (); + _shutdown = false; + _bytes = new ByteArrayOutputStream (); + _doStream = new DataOutputStream (_bytes); + } + + /** + * Initializes the connection, including connecting + * to socket or shared memory endpoint + * + * @throws TransportException if initialization fails + */ + public void initialize () + throws TransportException + { + // Initialize transport (connect socket, e.g.) + _transport.initialize (); + + // Do handshake + try + { + _inStream = new DataInputStream (_transport.getInputStream ()); + _outStream = new DataOutputStream (_transport.getOutputStream ()); + _doHandshake (); + } + catch (IOException ioe) + { + throw new TransportException (ioe); + } + } + + /* Does the JDWP handshake -- this should not need synchronization + because this is called by VM startup code, i.e., no packet + processing threads have started yet. */ + private void _doHandshake () + throws IOException + { + // According to the spec, the handshake is always initiated by + // the debugger, regardless of whether the JVM is in client mode or + // server mode. + + // Wait for handshake from debugger + byte[] hshake = new byte[_HANDSHAKE.length]; + _inStream.readFully (hshake, 0, _HANDSHAKE.length); + + if (Arrays.equals (hshake, _HANDSHAKE)) + { + // Send reply handshake + _outStream.write (_HANDSHAKE, 0, _HANDSHAKE.length); + return; + } + else + { + throw new IOException ("invalid JDWP handshake (\"" + hshake + "\")"); + } + } + + /** + * Main run method for the thread. This thread loops waiting for + * packets to be read via the connection. When a packet is complete + * and ready for processing, it places the packet in a queue that can + * be accessed via <code>getPacket</code> + */ + public void run () + { + while (!_shutdown) + { + try + { + _readOnePacket (); + } + catch (IOException ioe) + { + /* IOException can occur for two reasons: + 1. Lost connection with the other side + 2. Transport was shutdown + In either case, we make sure that all of the + back-end gets shutdown. */ + Jdwp.getInstance().shutdown (); + } + catch (Throwable t) + { + System.out.println ("JdwpConnection.run: caught an exception: " + + t); + // Just keep going + } + } + } + + // Reads a single packet from the connection, adding it to the packet + // queue when a complete packet is ready. + private void _readOnePacket () + throws IOException + { + byte[] data = null; + + // Read in the packet + int length = _inStream.readInt (); + if (length < 11) + { + throw new IOException ("JDWP packet length < 11 (" + + length + ")"); + } + + data = new byte[length]; + data[0] = (byte) (length >>> 24); + data[1] = (byte) (length >>> 16); + data[2] = (byte) (length >>> 8); + data[3] = (byte) length; + _inStream.readFully (data, 4, length - 4); + + JdwpPacket packet = JdwpPacket.fromBytes (data); + if (packet != null) + { + synchronized (_commandQueue) + { + _commandQueue.add (packet); + _commandQueue.notifyAll (); + } + } + } + + /** + * Returns a packet from the queue of ready packets + * + * @returns a <code>JdwpPacket</code> ready for processing + * <code>null</code> when shutting down + */ + public JdwpPacket getPacket () + { + synchronized (_commandQueue) + { + while (_commandQueue.isEmpty ()) + { + try + { + _commandQueue.wait (); + } + catch (InterruptedException ie) + { + /* PacketProcessor is interrupted + when shutting down */ + return null; + } + } + + return (JdwpPacket) _commandQueue.remove (0); + } + } + + /** + * Send a packet to the debugger + * + * @param pkt a <code>JdwpPacket</code> to send + * @throws IOException + */ + public void sendPacket (JdwpPacket pkt) + throws IOException + { + pkt.write (_outStream); + } + + /** + * Send an event notification to the debugger + * + * @param request the debugger request that wanted this event + * @param event the event + * @throws IOException + */ + public void sendEvent (EventRequest request, Event event) + throws IOException + { + JdwpPacket pkt; + + synchronized (_bytes) + { + _bytes.reset (); + pkt = event.toPacket (_doStream, request); + pkt.setData (_bytes.toByteArray ()); + } + + sendPacket (pkt); + } + + /** + * Shutdown the connection + */ + public void shutdown () + { + if (!_shutdown) + { + _transport.shutdown (); + _shutdown = true; + interrupt (); + } + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/JdwpPacket.java b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpPacket.java new file mode 100644 index 00000000000..7fa93e2de1b --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpPacket.java @@ -0,0 +1,278 @@ +/* JdwpPacket.java -- Base class for JDWP command and reply packets + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * All command and reply packets in JDWP share + * common header type information: + * + * length (4 bytes) : size of entire packet, including length + * id (4 bytes) : unique packet id + * flags (1 byte) : flag byte + * [command packet stuff | reply packet stuff] + * data (variable) : unique command-/reply-specific data + * + * This class deal with everything except the command- and reply-specific + * data, which get handled in {@link + * gnu.classpath.jdwp.transport.JdwpCommandPacket} and {@link + * gnu.classpath.jdwp.transprot.JdwpReplyPacket}. + * + * @author Keith Seitz <keiths@redhat.com> + */ +public abstract class JdwpPacket +{ + // Last id of packet constructed + protected static int _last_id = 0; + + // JDWP reply packet flag + protected static final int JDWP_FLAG_REPLY = 0x80; + + /** + * Minimum packet size excluding sub-class data + * ( length (4) + id (4) + flags (1) ) + */ + protected static final int MINIMUM_SIZE = 9; + + /** + * Id of command/reply + */ + protected int _id; + + /** + * Packet flags + */ + protected byte _flags; + + /** + * Packet-specific data + */ + protected byte[] _data; + + /** + * Constructor + */ + public JdwpPacket () + { + // By default, DON'T assign an id. This way when a packet + // is constructed from fromBytes, _last_id won't increment (i.e., + // it won't leave holes in the outgoing packet ids). + } + + /** + * Constructs a <code>JdwpPacket</code> with the id + * from the given packet. + * + * @param pkt a packet whose id will be used in this new packet + */ + public JdwpPacket (JdwpPacket pkt) + { + _id = pkt.getId (); + } + + /** + * Returns the packet id + */ + public int getId () + { + return _id; + } + + /** + * Sets the packet id + */ + public void setId (int id) + { + _id = id; + } + + /** + * Returns the packet flags + */ + public byte getFlags () + { + return _flags; + } + + /** + * Sets the packet flags + */ + public void setFlags (byte flags) + { + _flags = flags; + } + + /** + * Gets the command/reply-specific data in this packet + */ + public byte[] getData () + { + return _data; + } + + /** + * Sets the command/reply-specific data in this packet + */ + public void setData (byte[] data) + { + _data = data; + } + + /** + * Returns the length of this entire packet + */ + public int getLength () + { + return MINIMUM_SIZE + (_data == null ? 0 : _data.length); + } + + /** + * Allow subclasses to initialize from data + * + * @param bytes packet data from the wire + * @param index index into <code>bytes</code> to start processing + * @return number of bytes in <code>bytes</code> processed + */ + protected abstract int myFromBytes (byte[] bytes, int index); + + /** + * Convert the given bytes into a <code>JdwpPacket</code>. Uses the + * abstract method <code>myFromBytes</code> to allow subclasses to + * process data. + * + * If the given data does not represent a valid JDWP packet, it returns + * <code>null</code>. + * + * @param bytes packet data from the wire + * @param index index into <code>bytes</code> to start processing + * @return number of bytes in <code>bytes</code> processed + */ + public static JdwpPacket fromBytes (byte[] bytes) + { + int i = 0; + int length = ((bytes[i++] & 0xff) << 24 | (bytes[i++] & 0xff) << 16 + | (bytes[i++] & 0xff) << 8 | (bytes[i++] & 0xff)); + int id = 0; + byte flags = 0; + + if (bytes.length == length) + { + id = ((bytes[i++] & 0xff) << 24 | (bytes[i++] & 0xff) << 16 + | (bytes[i++] & 0xff) << 8 | (bytes[i++] & 0xff)); + flags = bytes[i++]; + + Class clazz = null; + if (flags == 0) + clazz = JdwpCommandPacket.class; + else if ((flags & JDWP_FLAG_REPLY) != 0) + clazz = JdwpReplyPacket.class; + else + { + // Malformed packet. Discard it. + return null; + } + + JdwpPacket pkt = null; + try + { + pkt = (JdwpPacket) clazz.newInstance (); + } + catch (InstantiationException ie) + { + // Discard packet + return null; + } + catch (IllegalAccessException iae) + { + // Discard packet + return null; + } + + pkt.setId (id); + pkt.setFlags (flags); + + i += pkt.myFromBytes (bytes, i); + byte[] data = new byte[length - i]; + System.arraycopy (bytes, i, data, 0, data.length); + pkt.setData (data); + + return pkt; + } + + return null; + } + + /** + * Put subclass information onto the stream + * + * @param dos the output stream to which to write + */ + protected abstract void myWrite (DataOutputStream dos) + throws IOException; + + /** + * Writes the packet to the output stream + * + * @param dos the output stream to which to write the packet + */ + public void write (DataOutputStream dos) + throws IOException + { + // length + int length = getLength (); + dos.writeInt (length); + + // ID + dos.writeInt (getId ()); + + // flag + dos.writeByte (getFlags ()); + + // packet-specific stuff + myWrite (dos); + + // data (if any) + byte[] data = getData (); + if (data != null && data.length > 0) + dos.write (data, 0, data.length); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/JdwpReplyPacket.java b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpReplyPacket.java new file mode 100644 index 00000000000..de32ecf995b --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/JdwpReplyPacket.java @@ -0,0 +1,137 @@ +/* JdwpReplyPacket.java -- JDWP reply packet + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * A class represents a JDWP reply packet. + * This class adds an error code to the packet header information + * in {@link gnu.classpath.transport.JdwpPacket} and adds additional + * reply packet-specific processing. + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class JdwpReplyPacket extends JdwpPacket +{ + /** + * Error code + */ + protected short _errorCode; + + // Minimum packet size [excluding super class] ( errorCode (2) ) + private static final int MINIMUM_LENGTH = 2; + + /** + * Constructs a <code>JdwpReplyPacket</code>. + */ + public JdwpReplyPacket () + { + // Don't assign a packet id. This is called by JdwpPacket.fromBytes + // which assigns a packet id. (Not that a VM would do that...) + } + + /** + * Constructs a <code>JdwpReplyPacket</code> with the + * id from the given packet and error code + * + * @param pkt the packet whose id this packet will use + * @param errorCode the error code + */ + public JdwpReplyPacket (JdwpPacket pkt, short errorCode) + { + this(pkt); + _errorCode = errorCode; + } + + /** + * Constructs a <code>JdwpReplyPacket</code> with the + * id from the given packet and an empty error code + * + * @param pkt the packet whose id this packet will use + */ + public JdwpReplyPacket (JdwpPacket pkt) + { + super (pkt); + _flags = (byte) JDWP_FLAG_REPLY; + } + + /** + * Returns the length of this packet + */ + public int getLength () + { + return MINIMUM_LENGTH + super.getLength (); + } + + /** + * Returns the error code + */ + public short getErrorCode () + { + return _errorCode; + } + + /** + * Sets the error code + */ + public void setErrorCode (short ec) + { + _errorCode = ec; + } + + // Reads command packet data from the given buffer, starting + // at the given offset + protected int myFromBytes (byte[] bytes, int index) + { + int i = 0; + setErrorCode ((short) ((bytes[index + i++] & 0xff) << 8 + | (bytes[index + i++] & 0xff))); + return i; + } + + // Writes the command packet data into the given buffer + protected void myWrite (DataOutputStream dos) + throws IOException + { + dos.writeShort (getErrorCode ()); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/SocketTransport.java b/libjava/classpath/gnu/classpath/jdwp/transport/SocketTransport.java new file mode 100644 index 00000000000..0ad7357e183 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/SocketTransport.java @@ -0,0 +1,171 @@ +/* SocketTransport.java -- a socket transport + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import gnu.classpath.jdwp.transport.ITransport; +import gnu.classpath.jdwp.transport.TransportException; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.HashMap; + +import javax.net.ServerSocketFactory; +import javax.net.SocketFactory; + +/** + * A socket-based transport. This transport uses + * configury string that looks like "name=dt_socket, + * address=localhost:1234,server=y". + * + * @author Keith Seitz <keiths@redhat.com> + */ +class SocketTransport + implements ITransport +{ + /** + * Name of this transport + */ + public static final String NAME = "dt_socket"; + + // Configure properties + private static final String _PROPERTY_ADDRESS = "address"; + private static final String _PROPERTY_SERVER = "server"; + + // Port number + private int _port; + + // Host name + private String _host; + + // Are we acting as a server? + private boolean _server = false; + + // Socket + private Socket _socket; + + /** + * Setup the connection configuration from the given properties + * + * @param properties the properties of the JDWP session + * @throws TransportException for any configury errors + */ + public void configure (HashMap properties) + throws TransportException + { + // Get address [form: "hostname:port"] + String p = (String) properties.get (_PROPERTY_ADDRESS); + if (p != null) + { + String[] s = p.split (":"); + if (s.length == 2) + { + _host = s[0]; + _port = Integer.parseInt (s[1]); + } + } + + // Get server [form: "y" or "n"] + p = (String) properties.get (_PROPERTY_SERVER); + if (p != null) + { + if (p.toLowerCase().equals ("y")) + _server = true; + } + } + + /** + * Initialize this socket connection. This includes + * connecting to the host (or listening for it). + * + * @throws TransportException if a transport-related error occurs + */ + public void initialize () + throws TransportException + { + try + { + if (_server) + { + // Get a server socket + ServerSocketFactory ssf = ServerSocketFactory.getDefault (); + ServerSocket ss = ssf.createServerSocket (_port, 1); + _socket = ss.accept (); + } + else + { + // Get a client socket (the factory will connect it) + SocketFactory sf = SocketFactory.getDefault (); + _socket = sf.createSocket (_host, _port); + } + } + catch (IOException ioe) + { + // This will grab UnknownHostException, too. + throw new TransportException (ioe); + } + } + + /** + * Returns an <code>InputStream</code> for the transport + * + * @throws IOException if an I/O error occurs creating the stream + * or the socket is not connected + */ + public InputStream getInputStream () + throws IOException + { + return _socket.getInputStream (); + } + + /** + * Returns an <code>OutputStream</code> for the transport + * + * @throws IOException if an I/O error occurs creating the stream + * or the socket is not connected + */ + public OutputStream getOutputStream () + throws IOException + { + return _socket.getOutputStream (); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/TransportException.java b/libjava/classpath/gnu/classpath/jdwp/transport/TransportException.java new file mode 100644 index 00000000000..057422a879e --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/TransportException.java @@ -0,0 +1,75 @@ +/* TransportException.java -- Exception for transport configury errors + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +/** + * A transport configury or initialization exception thrown by + * JDWP transports. This class is a generic exception class + * that the different transport layers use to report transport-specific + * exceptions that may occur (which could be very different from + * one transport to the next.). + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class TransportException + extends Exception +{ + /** + * Constructs a <code>TransportException</code> with the + * given message + * + * @param message a message describing the exception + */ + public TransportException (String message) + { + super (message); + } + + /** + * Constructs a <code>TransportException</code> with the + * given <code>Throwable</code> root cause + * + * @param t the cause of the exception + */ + public TransportException (Throwable t) + { + super (t); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/transport/TransportFactory.java b/libjava/classpath/gnu/classpath/jdwp/transport/TransportFactory.java new file mode 100644 index 00000000000..480fb362939 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/transport/TransportFactory.java @@ -0,0 +1,115 @@ +/* TransportFactory.java -- Factory for transports + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.transport; + +import java.util.HashMap; + +/** + * A factory class that constructs transports for use by + * the JDWP back-end. + * + * @author Keith Seitz <keiths@redhat.com> + */ +public class TransportFactory +{ + // Keyword in configspec that specifies transport method + private static final String _TRANSPORT_PROPERTY = "transport"; + + // A single transport method mapping + private static class TransportMethod + { + String name; + Class clazz; + public TransportMethod (String name, Class clazz) + { + this.name = name; + this.clazz = clazz; + } + } + + // List of all supported transport methods + private static TransportMethod[] _transportMethods = new TransportMethod[] + { + new TransportMethod (SocketTransport.NAME, SocketTransport.class) + //new TransportMethod (ShmemTransport.NAME, ShmemTransport.class) + }; + + /** + * Get a transport configured as specified in the properties + * + * @param properties a <code>HashMap</code> specifying the JDWP + * configuration properties + * @returns the created and configured transport + * @throws TransportException for invalid configurations + */ + public static ITransport newInstance (HashMap properties) + throws TransportException + { + String name = null; + if (properties != null) + name = (String) properties.get (_TRANSPORT_PROPERTY); + if (name == null) + throw new TransportException ("no transport specified"); + + for (int i = 0; i < _transportMethods.length; ++i) + { + if (_transportMethods[i].name.equals (name)) + { + try + { + ITransport t; + t = (ITransport) _transportMethods[i].clazz.newInstance (); + t.configure (properties); + return t; + } + catch (TransportException te) + { + throw te; + } + catch (Exception e) + { + throw new TransportException (e); + } + } + } + + throw new TransportException ("transport \"" + name + "\" not found"); + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/util/JdwpString.java b/libjava/classpath/gnu/classpath/jdwp/util/JdwpString.java new file mode 100644 index 00000000000..e94700fe1ed --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/util/JdwpString.java @@ -0,0 +1,95 @@ +/* JdwpString.java -- utility class to read and write jdwp strings + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.classpath.jdwp.util; + +import gnu.classpath.jdwp.exception.JdwpInternalErrorException; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; + +/** + * A class to compute the JDWP representation of Strings. + * + * @author Aaron Luchko <aluchko@redhat.com> + */ +public class JdwpString +{ + + /** + * Write this String to the outStream as a string understood by jdwp. + * + * @param os write the String here + * @param string the String to write + * @throws IOException + */ + public static void writeString(DataOutputStream os, String string) + throws IOException + { + // Get the bytes of the string as a string in UTF-8 + byte[] strBytes = string.getBytes("UTF-8"); + os.writeInt(strBytes.length); + os.write(strBytes); + } + + /** + * Read a jdwp style string from the ByteBuffer. + * + * @param bb contains the string + * @return the String that was read + * @throws JdwpInternalErrorException bb didn't contain a value UTF-8 string + */ + public static String readString(ByteBuffer bb) + throws JdwpInternalErrorException + { + int length = bb.getInt(); + byte[] strBytes = new byte[length]; + bb.get(strBytes); + try + { + return new String(strBytes, "UTF-8"); + } + catch (UnsupportedEncodingException ex) + { + // Any string from the VM should be in UTF-8 so an encoding error + // shouldn't be possible + throw new JdwpInternalErrorException(ex); + } + } +} diff --git a/libjava/classpath/gnu/classpath/jdwp/util/Signature.java b/libjava/classpath/gnu/classpath/jdwp/util/Signature.java new file mode 100644 index 00000000000..36445c31841 --- /dev/null +++ b/libjava/classpath/gnu/classpath/jdwp/util/Signature.java @@ -0,0 +1,149 @@ +/* Signature.java -- utility class to compute class and method signatures + Copyright (C) 2005 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.classpath.jdwp.util; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * A class to compute class and method signatures. + * + * @author Tom Tromey (tromey@redhat.com) + * @author Keith Seitz (keiths@redhat.com) + */ +public class Signature +{ + /** + * Computes the class signature, i.e., java.lang.String.class + * returns "Ljava/lang/String;". + * + * @param theClass the class for which to compute the signature + * @return the class's type signature + */ + public static String computeClassSignature (Class theClass) + { + StringBuffer sb = new StringBuffer (); + _addToSignature (sb, theClass); + return sb.toString (); + } + + /** + * Computes the field signature which is just the class signature of the + * field's type, ie a Field of type java.lang.String this will return + * "Ljava/lang/String;". + * + * @param field the field for which to compute the signature + * @return the field's type signature + */ + public static String computeFieldSignature (Field field) + { + return computeClassSignature (field.getType()); + } + + /** + * Computes the method signature, i.e., java.lang.String.split (String, int) + * returns "(Ljava/lang/String;I)[Ljava/lang/String;" + * + * @param method the method for which to compute the signature + * @return the method's type signature + */ + public static String computeMethodSignature (Method method) + { + return _computeSignature (method.getReturnType (), + method.getParameterTypes ()); + } + + private static String _computeSignature (Class returnType, + Class[] paramTypes) + { + StringBuffer sb = new StringBuffer ("("); + if (paramTypes != null) + { + for (int i = 0; i < paramTypes.length; ++i) + _addToSignature (sb, paramTypes[i]); + } + sb.append (")"); + _addToSignature (sb, returnType); + return sb.toString(); + } + + private static void _addToSignature (StringBuffer sb, Class k) + { + // For some reason there's no easy way to get the signature of a + // class. + if (k.isPrimitive ()) + { + if (k == void.class) + sb.append('V'); + else if (k == boolean.class) + sb.append('Z'); + else if (k == byte.class) + sb.append('B'); + else if (k == char.class) + sb.append('C'); + else if (k == short.class) + sb.append('S'); + else if (k == int.class) + sb.append('I'); + else if (k == float.class) + sb.append('F'); + else if (k == double.class) + sb.append('D'); + else if (k == long.class) + sb.append('J'); + return; + } + + String name = k.getName (); + int len = name.length (); + sb.ensureCapacity (len); + if (! k.isArray ()) + sb.append('L'); + for (int i = 0; i < len; ++i) + { + char c = name.charAt (i); + if (c == '.') + c = '/'; + sb.append (c); + } + if (! k.isArray ()) + sb.append(';'); + } +} diff --git a/libjava/classpath/gnu/java/awt/AWTUtilities.java b/libjava/classpath/gnu/java/awt/AWTUtilities.java new file mode 100644 index 00000000000..a02d1c462b0 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/AWTUtilities.java @@ -0,0 +1,321 @@ +/* AWTUtilities.java -- Common utility methods for AWT and Swing. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt; + +import java.awt.Component; +import java.awt.Container; +import java.util.AbstractSequentialList; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.WeakHashMap; + +/** + * This class provides utility methods that are commonly used in AWT + * (and Swing). + */ +public class AWTUtilities +{ + + /** + * This List implementation wraps the Component[] returned by + * {@link Container#getComponents()} and iterates over the visible Components + * in that array. This class is used in {@link #getVisibleChildren}. + */ + static class VisibleComponentList extends AbstractSequentialList + { + /** + * The ListIterator for this List. + */ + class VisibleComponentIterator implements ListIterator + { + /** The current index in the Component[]. */ + int index; + + /** The index in the List of visible Components. */ + int listIndex; + + /** + * Creates a new VisibleComponentIterator that starts at the specified + * <code>listIndex</code>. The array of Components is searched from + * the beginning to find the matching array index. + * + * @param listIndex the index from where to begin iterating + */ + VisibleComponentIterator(int listIndex) + { + this.listIndex = listIndex; + int visibleComponentsFound = 0; + for (index = 0; visibleComponentsFound != listIndex; index++) + { + if (components[index].isVisible()) + visibleComponentsFound++; + } + } + + /** + * Returns <code>true</code> if there are more visible components in the + * array, <code>false</code> otherwise. + * + * @return <code>true</code> if there are more visible components in the + * array, <code>false</code> otherwise + */ + public boolean hasNext() + { + boolean hasNext = false; + for (int i = index; i < components.length; i++) + { + if (components[i].isVisible()) + { + hasNext = true; + break; + } + } + return hasNext; + } + + /** + * Returns the next visible <code>Component</code> in the List. + * + * @return the next visible <code>Component</code> in the List + * + * @throws if there is no next element + */ + public Object next() + { + Object o = null; + for (; index < components.length; index++) + { + if (components[index].isVisible()) + { + o = components[index]; + break; + } + } + if (o != null) + { + index++; + listIndex++; + return o; + } + else + throw new NoSuchElementException(); + } + + /** + * Returns <code>true</code> if there are more visible components in the + * array in the reverse direction, <code>false</code> otherwise. + * + * @return <code>true</code> if there are more visible components in the + * array in the reverse direction, <code>false</code> otherwise + */ + public boolean hasPrevious() + { + boolean hasPrevious = false; + for (int i = index - 1; i >= 0; i--) + { + if (components[i].isVisible()) + { + hasPrevious = true; + break; + } + } + return hasPrevious; + } + + /** + * Returns the previous visible <code>Component</code> in the List. + * + * @return the previous visible <code>Component</code> in the List + * + * @throws NoSuchElementException if there is no previous element + */ + public Object previous() + { + Object o = null; + for (index--; index >= 0; index--) + { + if (components[index].isVisible()) + { + o = components[index]; + break; + } + } + if (o != null) + { + listIndex--; + return o; + } + else + throw new NoSuchElementException(); + } + + /** + * Returns the index of the next element in the List. + * + * @return the index of the next element in the List + */ + public int nextIndex() + { + return listIndex + 1; + } + + /** + * Returns the index of the previous element in the List. + * + * @return the index of the previous element in the List + */ + public int previousIndex() + { + return listIndex - 1; + } + + /** + * This operation is not supported because the List is immutable. + * + * @throws UnsupportedOperationException because the List is immutable + */ + public void remove() + { + throw new UnsupportedOperationException + ("VisibleComponentList is immutable"); + } + + /** + * This operation is not supported because the List is immutable. + * + * @param o not used here + * + * @throws UnsupportedOperationException because the List is immutable + */ + public void set(Object o) + { + throw new UnsupportedOperationException + ("VisibleComponentList is immutable"); + } + + /** + * This operation is not supported because the List is immutable. + * + * @param o not used here + * + * @throws UnsupportedOperationException because the List is immutable + */ + public void add(Object o) + { + throw new UnsupportedOperationException + ("VisibleComponentList is immutable"); + } + } + + /** + * The components over which we iterate. Only the visible components + * are returned by this List. + */ + Component[] components; + + /** + * Creates a new instance of VisibleComponentList that wraps the specified + * <code>Component[]</code>. + * + * @param c the <code>Component[]</code> to be wrapped. + */ + VisibleComponentList(Component[] c) + { + components = c; + } + + /** + * Returns a {@link ListIterator} for iterating over this List. + * + * @return a {@link ListIterator} for iterating over this List + */ + public ListIterator listIterator(int index) + { + return new VisibleComponentIterator(index); + } + + /** + * Returns the number of visible components in the wrapped Component[]. + * + * @return the number of visible components + */ + public int size() + { + int visibleComponents = 0; + for (int i = 0; i < components.length; i++) + if (components[i].isVisible()) + visibleComponents++; + return visibleComponents; + } + } + + /** + * The cache for our List instances. We try to hold one instance of + * VisibleComponentList for each Component[] that is requested. Note + * that we use a WeakHashMap for caching, so that the cache itself + * does not keep the array or the List from beeing garbage collected + * if no other objects hold references to it. + */ + static WeakHashMap visibleChildrenCache = new WeakHashMap(); + + /** + * Returns the visible children of a {@link Container}. This method is + * commonly needed in LayoutManagers, because they only have to layout + * the visible children of a Container. + * + * @param c the Container from which to extract the visible children + * + * @return the visible children of <code>c</code> + */ + public static List getVisibleChildren(Container c) + { + Component[] children = c.getComponents(); + Object o = visibleChildrenCache.get(children); + VisibleComponentList visibleChildren = null; + if (o == null) + { + visibleChildren = new VisibleComponentList(children); + visibleChildrenCache.put(children, visibleChildren); + } + else + visibleChildren = (VisibleComponentList) o; + + return visibleChildren; + } +} diff --git a/libjava/classpath/gnu/java/awt/BitMaskExtent.java b/libjava/classpath/gnu/java/awt/BitMaskExtent.java new file mode 100644 index 00000000000..28134991099 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/BitMaskExtent.java @@ -0,0 +1,79 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt; + +/** + * Simple transparent utility class that can be used to perform bit + * mask extent calculations. + */ +public final class BitMaskExtent +{ + /** The number of the least significant bit of the bit mask extent. */ + public byte leastSignificantBit; + + /** The number of bits in the bit mask extent. */ + public byte bitWidth; + + /** + * Set the bit mask. This will calculate and set the leastSignificantBit + * and bitWidth fields. + * + * @see #leastSignificantBit + * @see #bitWidth + */ + public void setMask(long mask) + { + leastSignificantBit = 0; + bitWidth = 0; + if (mask == 0) return; + long shiftMask = mask; + for (; (shiftMask&1) == 0; shiftMask >>>=1) leastSignificantBit++; + for (; (shiftMask&1) != 0; shiftMask >>>=1) bitWidth++; + + if (shiftMask != 0) + throw new IllegalArgumentException("mask must be continuous"); + } + + /** + * Calculate the bit mask based on the values of the + * leastSignificantBit and bitWidth fields. + */ + public long toMask() + { + return ((1<<bitWidth)-1) << leastSignificantBit; + } +} diff --git a/libjava/classpath/gnu/java/awt/BitwiseXORComposite.java b/libjava/classpath/gnu/java/awt/BitwiseXORComposite.java new file mode 100644 index 00000000000..b568e1108c1 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/BitwiseXORComposite.java @@ -0,0 +1,295 @@ +/* BitwiseXORComposite.java -- Composite for emulating old-style XOR. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt; + +import java.awt.Color; +import java.awt.Composite; +import java.awt.CompositeContext; +import java.awt.Rectangle; +import java.awt.RenderingHints; +import java.awt.image.ColorModel; +import java.awt.image.DataBuffer; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; + + +/** + * A composite for emulating traditional bitwise XOR of pixel values. + * Please note that this composite does <i>not</i> implement the Porter-Duff + * XOR operator, but an exclusive or of overlapping subpixel regions. + * + * <p><img src="doc-files/BitwiseXORComposite-1.png" width="545" + * height="138" alt="A screen shot of BitwiseXORComposite in action" + * /> + * + * <p>The above screen shot shows the result of applying six different + * BitwiseXORComposites. They were constructed with the colors colors + * white, blue, black, orange, green, and brown, respectively. Each + * composite was used to paint a fully white rectangle on top of the + * blue bar in the background. + * + * <p>The purpose of this composite is to support the {@link + * Graphics#setXORMode(Color)} method in composite-aware graphics + * implementations. Applications typically would use + * <code>setXORMode</code> for drawing “highlights” such + * as text selections or cursors by inverting colors temporarily and + * then inverting them back. + * + * <p>A concrete <code>Graphics</code> implementation may contain + * the following code: + * + * <p><pre> public void setXORMode(Color xorColor) + * { + * setComposite(new gnu.java.awt.BitwiseXORComposite(xorColor)); + * } + * + * public void setPaintMode() + * { + * setComposite(java.awt.AlphaComposite.SrcOver); + * }</pre> + * + * @author Graydon Hoare (graydon@redhat.com) + * @author Sascha Brawer (brawer@dandelis.ch) + */ +public class BitwiseXORComposite + implements Composite +{ + /** + * The color whose RGB value is xor-ed with the values of each + * pixel. + */ + protected Color xorColor; + + + /** + * Constructs a new composite for xor-ing the pixel value. + * + * @param xorColor the color whose pixel value will be bitwise + * xor-ed with the source and destination pixels. + */ + public BitwiseXORComposite(Color xorColor) + { + this.xorColor = xorColor; + } + + + /** + * Creates a context object for performing the compositing + * operation. Several contexts may co-exist for one composite; each + * context may simultaneously be called from concurrent threads. + * + * @param srcColorModel the color model of the source. + * @param dstColorModel the color model of the destination. + * @param hints hints for choosing between rendering alternatives. + */ + public CompositeContext createContext(ColorModel srcColorModel, + ColorModel dstColorModel, + RenderingHints hints) + { + if (IntContext.isSupported(srcColorModel, dstColorModel, hints)) + return new IntContext(srcColorModel, xorColor); + + return new GeneralContext(srcColorModel, dstColorModel, xorColor); + } + + + /** + * A fallback CompositeContext that performs bitwise XOR of pixel + * values with the pixel value of the specified <code>xorColor</code>. + * + * <p>Applying this CompositeContext on a 1024x1024 BufferedImage of + * <code>TYPE_INT_RGB</code> took 611 ms on a lightly loaded 2.4 GHz + * Intel Pentium 4 CPU running Sun J2SE 1.4.1_01 on GNU/Linux + * 2.4.20. The timing is the average of ten runs on the same + * BufferedImage. Since the measurements were taken with {@link + * System#currentTimeMillis()}, they are rather inaccurate. + * + * @author Graydon Hoare (graydon@redhat.com) + */ + private static class GeneralContext + implements CompositeContext + { + ColorModel srcColorModel; + ColorModel dstColorModel; + Color xorColor; + + public GeneralContext(ColorModel srcColorModel, + ColorModel dstColorModel, + Color xorColor) + { + this.srcColorModel = srcColorModel; + this.dstColorModel = dstColorModel; + this.xorColor = xorColor; + } + + + public void compose(Raster src, Raster dstIn, WritableRaster dstOut) + { + Rectangle srcRect = src.getBounds(); + Rectangle dstInRect = dstIn.getBounds(); + Rectangle dstOutRect = dstOut.getBounds(); + + int xp = xorColor.getRGB(); + int w = Math.min(Math.min(srcRect.width, dstOutRect.width), + dstInRect.width); + int h = Math.min(Math.min(srcRect.height, dstOutRect.height), + dstInRect.height); + + Object srcPix = null, dstPix = null, rpPix = null; + + // Re-using the rpPix object saved 1-2% of execution time in + // the 1024x1024 pixel benchmark. + + for (int y = 0; y < h; y++) + { + for (int x = 0; x < w; x++) + { + srcPix = src.getDataElements(x + srcRect.x, y + srcRect.y, srcPix); + dstPix = dstIn.getDataElements(x + dstInRect.x, y + dstInRect.y, + dstPix); + int sp = srcColorModel.getRGB(srcPix); + int dp = dstColorModel.getRGB(dstPix); + int rp = sp ^ xp ^ dp; + dstOut.setDataElements(x + dstOutRect.x, y + dstOutRect.y, + dstColorModel.getDataElements(rp, rpPix)); + } + } + } + + + /** + * Disposes any cached resources. The default implementation does + * nothing because no resources are cached. + */ + public void dispose() + { + } + } + + + /** + * An optimized CompositeContext that performs bitwise XOR of + * <code>int</code> pixel values with the pixel value of a specified + * <code>xorColor</code>. This CompositeContext working only for + * rasters whose transfer format is {@link DataBuffer#TYPE_INT}. + * + * <p>Applying this CompositeContext on a 1024x1024 BufferedImage of + * <code>TYPE_INT_RGB</code> took 69 ms on a lightly loaded 2.4 GHz + * Intel Pentium 4 CPU running Sun J2SE 1.4.1_01 on GNU/Linux + * 2.4.20. The timing is the average of ten runs on the same + * BufferedImage. Since the measurements were taken with {@link + * System#currentTimeMillis()}, they are rather inaccurate. + * + * @author Sascha Brawer (brawer@dandelis.ch) + */ + private static class IntContext + extends GeneralContext + { + public IntContext(ColorModel colorModel, Color xorColor) + { + super(colorModel, colorModel, xorColor); + } + + + public void compose(Raster src, Raster dstIn, + WritableRaster dstOut) + { + int aX, bX, dstX, aY, bY, dstY, width, height; + int xorPixel; + int[] srcLine, dstLine; + + aX = src.getMinX(); + aY = src.getMinY(); + bX = dstIn.getMinX(); + bY = dstIn.getMinY(); + dstX = dstOut.getMinX(); + dstY = dstOut.getMinY(); + width = Math.min(Math.min(src.getWidth(), dstIn.getWidth()), + dstOut.getWidth()); + height = Math.min(Math.min(src.getHeight(), dstIn.getHeight()), + dstOut.getHeight()); + if ((width < 1) || (height < 1)) + return; + + srcLine = new int[width]; + dstLine = new int[width]; + + /* We need an int[] array with at least one element here; + * srcLine is as good as any other. + */ + srcColorModel.getDataElements(this.xorColor.getRGB(), srcLine); + xorPixel = srcLine[0]; + + for (int y = 0; y < height; y++) + { + src.getDataElements(aX, y + aY, width, 1, srcLine); + dstIn.getDataElements(bX, y + bY, width, 1, dstLine); + + for (int x = 0; x < width; x++) + dstLine[x] ^= srcLine[x] ^ xorPixel; + + dstOut.setDataElements(dstX, y + dstY, width, 1, dstLine); + } + } + + + /** + * Determines whether an instance of this CompositeContext would + * be able to process the specified color models. + */ + public static boolean isSupported(ColorModel srcColorModel, + ColorModel dstColorModel, + RenderingHints hints) + { + // FIXME: It would be good if someone could review these checks. + // They probably need to be more restrictive. + + int transferType; + + transferType = srcColorModel.getTransferType(); + if (transferType != dstColorModel.getTransferType()) + return false; + + if (transferType != DataBuffer.TYPE_INT) + return false; + + return true; + } + } +} diff --git a/libjava/classpath/gnu/java/awt/Buffers.java b/libjava/classpath/gnu/java/awt/Buffers.java new file mode 100644 index 00000000000..c6ccf919145 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/Buffers.java @@ -0,0 +1,243 @@ +/* Buffers.java -- + Copyright (C) 2000, 2002, 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt; + +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.DataBufferDouble; +import java.awt.image.DataBufferFloat; +import java.awt.image.DataBufferInt; +import java.awt.image.DataBufferShort; +import java.awt.image.DataBufferUShort; + +/** + * Utility class for creating and accessing data buffers of arbitrary + * data types. + */ +public final class Buffers +{ + /** + * Create a data buffer of a particular type. + * + * @param dataType the desired data type of the buffer. + * @param data an array containing data, or null + * @param size the size of the data buffer bank + */ + public static DataBuffer createBuffer(int dataType, Object data, + int size) + { + if (data == null) return createBuffer(dataType, size, 1); + + return createBufferFromData(dataType, data, size); + } + + + /** + * Create a data buffer of a particular type. + * + * @param dataType the desired data type of the buffer. + * @param size the size of the data buffer bank + */ + public static DataBuffer createBuffer(int dataType, int size) { + return createBuffer(dataType, size, 1); + } + + /** + * Create a data buffer of a particular type. + * + * @param dataType the desired data type of the buffer. + * @param size the size of the data buffer bank + * @param numBanks the number of banks the buffer should have + */ + public static DataBuffer createBuffer(int dataType, int size, int numBanks) + { + switch (dataType) + { + case DataBuffer.TYPE_BYTE: + return new DataBufferByte(size, numBanks); + case DataBuffer.TYPE_SHORT: + return new DataBufferShort(size, numBanks); + case DataBuffer.TYPE_USHORT: + return new DataBufferUShort(size, numBanks); + case DataBuffer.TYPE_INT: + return new DataBufferInt(size, numBanks); + case DataBuffer.TYPE_FLOAT: + return new DataBufferFloat(size, numBanks); + case DataBuffer.TYPE_DOUBLE: + return new DataBufferDouble(size, numBanks); + default: + throw new UnsupportedOperationException(); + } + } + + /** + * Create a data buffer of a particular type. + * + * @param dataType the desired data type of the buffer + * @param data an array containing the data + * @param size the size of the data buffer bank + */ + public static DataBuffer createBufferFromData(int dataType, Object data, + int size) + { + switch (dataType) + { + case DataBuffer.TYPE_BYTE: + return new DataBufferByte((byte[]) data, size); + case DataBuffer.TYPE_SHORT: + return new DataBufferShort((short[]) data, size); + case DataBuffer.TYPE_USHORT: + return new DataBufferUShort((short[]) data, size); + case DataBuffer.TYPE_INT: + return new DataBufferInt((int[]) data, size); + case DataBuffer.TYPE_FLOAT: + return new DataBufferFloat((float[]) data, size); + case DataBuffer.TYPE_DOUBLE: + return new DataBufferDouble((double[]) data, size); + default: + throw new UnsupportedOperationException(); + } + } + + /** + * Return the data array of a data buffer, regardless of the data + * type. + * + * @return an array of primitive values. The actual array type + * depends on the data type of the buffer. + */ + public static Object getData(DataBuffer buffer) + { + if (buffer instanceof DataBufferByte) + return ((DataBufferByte) buffer).getData(); + + if (buffer instanceof DataBufferShort) + return ((DataBufferShort) buffer).getData(); + + if (buffer instanceof DataBufferUShort) + return ((DataBufferUShort) buffer).getData(); + + if (buffer instanceof DataBufferInt) + return ((DataBufferInt) buffer).getData(); + + if (buffer instanceof DataBufferFloat) + return ((DataBufferFloat) buffer).getData(); + + if (buffer instanceof DataBufferDouble) + return ((DataBufferDouble) buffer).getData(); + + throw new ClassCastException("Unknown data buffer type"); + } + + + /** + * Copy data from array contained in data buffer, much like + * System.arraycopy. Create a suitable destination array if the + * given destination array is null. + */ + public static Object getData(DataBuffer src, int srcOffset, + Object dest, int destOffset, + int length) + { + Object from; + if (src instanceof DataBufferByte) + { + from = ((DataBufferByte) src).getData(); + if (dest == null) dest = new byte[length+destOffset]; + } + else if (src instanceof DataBufferShort) + { + from = ((DataBufferShort) src).getData(); + if (dest == null) dest = new short[length+destOffset]; + } + else if (src instanceof DataBufferUShort) + { + from = ((DataBufferUShort) src).getData(); + if (dest == null) dest = new short[length+destOffset]; + } + else if (src instanceof DataBufferInt) + { + from = ((DataBufferInt) src).getData(); + if (dest == null) dest = new int[length+destOffset]; + } + else if (src instanceof DataBufferFloat) + { + from = ((DataBufferFloat) src).getData(); + if (dest == null) dest = new float[length+destOffset]; + } + else if (src instanceof DataBufferDouble) + { + from = ((DataBufferDouble) src).getData(); + if (dest == null) dest = new double[length+destOffset]; + } + else + { + throw new ClassCastException("Unknown data buffer type"); + } + + System.arraycopy(from, srcOffset, dest, destOffset, length); + return dest; + } + + /** + * @param bits the width of a data element measured in bits + * + * @return the smallest data type that can store data elements of + * the given number of bits, without any truncation. + */ + public static int smallestAppropriateTransferType(int bits) + { + if (bits <= 8) + { + return DataBuffer.TYPE_BYTE; + } + else if (bits <= 16) + { + return DataBuffer.TYPE_USHORT; + } + else if (bits <= 32) + { + return DataBuffer.TYPE_INT; + } + else + { + return DataBuffer.TYPE_UNDEFINED; + } + } +} diff --git a/libjava/classpath/gnu/java/awt/ClasspathToolkit.java b/libjava/classpath/gnu/java/awt/ClasspathToolkit.java new file mode 100644 index 00000000000..5fb444f9866 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/ClasspathToolkit.java @@ -0,0 +1,379 @@ +/* ClasspathToolkit.java -- Abstract superclass for Classpath toolkits. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt; + +import gnu.java.awt.EmbeddedWindow; +import gnu.java.awt.peer.ClasspathFontPeer; +import gnu.java.awt.peer.EmbeddedWindowPeer; +import gnu.java.awt.peer.ClasspathTextLayoutPeer; + +import java.awt.AWTException; +import java.awt.Dimension; +import java.awt.DisplayMode; +import java.awt.EventQueue; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Image; +import java.awt.Toolkit; +import java.awt.font.FontRenderContext; +import java.awt.image.ColorModel; +import java.awt.image.ImageProducer; +import java.awt.peer.RobotPeer; +import java.io.File; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.text.AttributedString; +import java.util.HashMap; +import java.util.Map; + +import javax.imageio.spi.IIORegistry; + +/** + * An abstract superclass for Classpath toolkits. + * + * <p>There exist some parts of AWT and Java2D that are specific to + * the underlying platform, but for which the {@link Toolkit} class + * does not provide suitable abstractions. Examples include some + * methods of {@link Font} or {@link GraphicsEnvironment}. Those + * methods use ClasspathToolkit as a central place for obtaining + * platform-specific functionality. + * + * <p>In addition, ClasspathToolkit implements some abstract methods + * of {@link java.awt.Toolkit} that are not really platform-specific, + * such as the maintenance of a cache of loaded images. + * + * <p><b>Thread Safety:</b> The methods of this class may safely be + * called without external synchronization. This also hold for any + * inherited {@link Toolkit} methods. Subclasses are responsible for + * the necessary synchronization. + * + * @author Sascha Brawer (brawer@dandelis.ch) + */ +public abstract class ClasspathToolkit + extends Toolkit +{ + /** + * A map from URLs to previously loaded images, used by {@link + * #getImage(java.net.URL)}. For images that were loaded via a path + * to an image file, the map contains a key with a file URL. + */ + private HashMap imageCache; + + + /** + * Returns a shared instance of the local, platform-specific + * graphics environment. + * + * <p>This method is specific to GNU Classpath. It gets called by + * the Classpath implementation of {@link + * GraphicsEnvironment.getLocalGraphcisEnvironment()}. + */ + public abstract GraphicsEnvironment getLocalGraphicsEnvironment(); + + + /** + * Determines the current size of the default, primary screen. + * + * @throws HeadlessException if the local graphics environment is + * headless, which means that no screen is attached and no user + * interaction is allowed. + */ + public Dimension getScreenSize() + { + DisplayMode mode; + + // getDefaultScreenDevice throws HeadlessException if the + // local graphics environment is headless. + mode = GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice().getDisplayMode(); + + return new Dimension(mode.getWidth(), mode.getHeight()); + } + + + /** + * Determines the current color model of the default, primary + * screen. + * + * @see GraphicsEnvironment#getDefaultScreenDevice() + * @see java.awt.GraphicsDevice#getDefaultConfiguration() + * @see java.awt.GraphicsConfiguration#getColorModel() + * + * @throws HeadlessException if the local graphics environment is + * headless, which means that no screen is attached and no user + * interaction is allowed. + */ + public ColorModel getColorModel() + { + // getDefaultScreenDevice throws HeadlessException if the + // local graphics environment is headless. + return GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice().getDefaultConfiguration() + .getColorModel(); + } + + /** + * Retrieves the metrics for rendering a font on the screen. + * + * @param font the font whose metrics are requested. + */ + public FontMetrics getFontMetrics(Font font) + { + return ((ClasspathFontPeer) font.getPeer ()).getFontMetrics (font); + } + + + /** + * Acquires an appropriate {@link ClasspathFontPeer}, for use in + * classpath's implementation of {@link java.awt.Font}. + * + * @param name The logical name of the font. This may be either a face + * name or a logical font name, or may even be null. A default + * implementation of name decoding is provided in + * {@link ClasspathFontPeer}, but may be overridden in other toolkits. + * + * @param attrs Any extra {@link java.awt.font.TextAttribute} attributes + * this font peer should have, such as size, weight, family name, or + * transformation. + */ + public abstract ClasspathFontPeer getClasspathFontPeer (String name, Map attrs); + + public abstract ClasspathTextLayoutPeer + getClasspathTextLayoutPeer (AttributedString str, FontRenderContext frc); + + + /** + * Creates a {@link Font}, in a platform-specific manner. + * + * The default implementation simply constructs a {@link Font}, but some + * toolkits may wish to override this, to return {@link Font} subclasses which + * implement {@link java.awt.font.OpenType} or + * {@link java.awt.font.MultipleMaster}. + */ + public Font getFont (String name, Map attrs) + { + return new Font (name, attrs); + } + + + /** + * Creates a font, reading the glyph definitions from a stream. + * + * <p>This method provides the platform-specific implementation for + * the static factory method {@link Font#createFont(int, + * java.io.InputStream)}. + * + * @param format the format of the font data, such as {@link + * Font#TRUETYPE_FONT}. An implementation may ignore this argument + * if it is able to automatically recognize the font format from the + * provided data. + * + * @param stream an input stream from where the font data is read + * in. The stream will be advanced to the position after the font + * data, but not closed. + * + * @throws IllegalArgumentException if <code>format</code> is + * not supported. + * + * @throws FontFormatException if <code>stream</code> does not + * contain data in the expected format, or if required tables are + * missing from a font. + * + * @throws IOException if a problem occurs while reading in the + * contents of <code>stream</code>. + */ + public abstract Font createFont(int format, InputStream stream); + + + /** + * Returns an image from the specified file, which must be in a + * recognized format. The set of recognized image formats may vary + * from toolkit to toolkit. + * + * <p>This method maintains a cache for images. If an image has been + * loaded from the same path before, the cached copy will be + * returned. The implementation may hold cached copies for an + * indefinite time, which can consume substantial resources with + * large images. Users are therefore advised to use {@link + * #createImage(java.lang.String)} instead. + * + * <p>The default implementation creates a file URL for the + * specified path and invokes {@link #getImage(URL)}. + * + * @param path A path to the image file. + * + * @return IllegalArgumentException if <code>path</code> does not + * designate a valid path. + */ + public Image getImage(String path) + { + try + { + return getImage(new File(path).toURL()); + } + catch (MalformedURLException muex) + { + throw (IllegalArgumentException) new IllegalArgumentException(path) + .initCause(muex); + } + } + + + /** + * Loads an image from the specified URL. The image data must be in + * a recognized format. The set of recognized image formats may vary + * from toolkit to toolkit. + * + * <p>This method maintains a cache for images. If an image has been + * loaded from the same URL before, the cached copy will be + * returned. The implementation may hold cached copies for an + * indefinite time, which can consume substantial resources with + * large images. Users are therefore advised to use {@link + * #createImage(java.net.URL)} instead. + * + * @param url the URL from where the image is read. + */ + public Image getImage(URL url) + { + Image result; + + synchronized (this) + { + // Many applications never call getImage. Therefore, we lazily + // create the image cache when it is actually needed. + if (imageCache == null) + imageCache = new HashMap(); + else + { + result = (Image) imageCache.get(url); + if (result != null) + return result; + } + + // The createImage(URL) method, which is specified by + // java.awt.Toolkit, is not implemented by this abstract class + // because it is platform-dependent. Once Classpath has support + // for the javax.imageio package, it might be worth considering + // that toolkits provide native stream readers. Then, the class + // ClasspathToolkit could provide a general implementation that + // delegates the image format parsing to javax.imageio. + result = createImage(url); + + // It is not clear whether it would be a good idea to use weak + // references here. The advantage would be reduced memory + // consumption, since loaded images would not be kept + // forever. But on VMs that frequently perform garbage + // collection (which includes VMs with a parallel or incremental + // collector), the image might frequently need to be re-loaded, + // possibly over a slow network connection. + imageCache.put(url, result); + + return result; + } + } + + + /** + * Returns an image from the specified file, which must be in a + * recognized format. The set of recognized image formats may vary + * from toolkit to toolkit. + * + * <p>A new image is created every time this method gets called, + * even if the same path has been passed before. + * + * <p>The default implementation creates a file URL for the + * specified path and invokes {@link #createImage(URL)}. + * + * @param path A path to the file to be read in. + */ + public Image createImage(String path) + { + try + { + // The abstract method createImage(URL) is defined by + // java.awt.Toolkit, but intentionally not implemented by + // ClasspathToolkit because it is platform specific. + return createImage(new File(path).toURL()); + } + catch (MalformedURLException muex) + { + throw (IllegalArgumentException) new IllegalArgumentException(path) + .initCause(muex); + } + } + + /** + * Creates an ImageProducer from the specified URL. The image is assumed + * to be in a recognised format. If the toolkit does not implement the + * image format or the image format is not recognised, null is returned. + * This default implementation is overriden by the Toolkit implementations. + * + * @param url URL to read image data from. + */ + public ImageProducer createImageProducer(URL url) + { + return null; + } + + public abstract RobotPeer createRobot (GraphicsDevice screen) + throws AWTException; + + /** + * Creates an embedded window peer, and associates it with an + * EmbeddedWindow object. + * + * @param w The embedded window with which to associate a peer. + */ + public abstract EmbeddedWindowPeer createEmbeddedWindow (EmbeddedWindow w); + + /** + * Used to register ImageIO SPIs provided by the toolkit. + */ + + public void registerImageIOSpis(IIORegistry reg) + { + } + + public abstract boolean nativeQueueEmpty(); + public abstract void wakeNativeQueue(); + public abstract void iterateNativeQueue(EventQueue locked, boolean block); +} diff --git a/libjava/classpath/gnu/java/awt/ComponentDataBlitOp.java b/libjava/classpath/gnu/java/awt/ComponentDataBlitOp.java new file mode 100644 index 00000000000..545427ea767 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/ComponentDataBlitOp.java @@ -0,0 +1,156 @@ +/* Copyright (C) 2000, 2002, 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt; + +import java.awt.RenderingHints; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.image.ComponentSampleModel; +import java.awt.image.DataBuffer; +import java.awt.image.Raster; +import java.awt.image.RasterOp; +import java.awt.image.WritableRaster; + +/** + * This raster copy operation assumes that both source and destination + * sample models are tightly pixel packed and contain the same number + * of bands. + * + * @throws java.lang.ClassCastException if the sample models of the + * rasters are not of type ComponentSampleModel. + * + * @author Rolf W. Rasmussen (rolfwr@ii.uib.no) + */ +public class ComponentDataBlitOp implements RasterOp +{ + public static final ComponentDataBlitOp INSTANCE = new ComponentDataBlitOp(); + + public WritableRaster filter(Raster src, WritableRaster dest) + { + if (dest == null) + dest = createCompatibleDestRaster(src); + + DataBuffer srcDB = src.getDataBuffer(); + DataBuffer destDB = dest.getDataBuffer(); + + ComponentSampleModel srcSM = (ComponentSampleModel) src.getSampleModel(); + ComponentSampleModel destSM = (ComponentSampleModel) dest.getSampleModel(); + + + // Calculate offset to data in the underlying arrays: + + int srcScanlineStride = srcSM.getScanlineStride(); + int destScanlineStride = destSM.getScanlineStride(); + int srcX = src.getMinX() - src.getSampleModelTranslateX(); + int srcY = src.getMinY() - src.getSampleModelTranslateY(); + int destX = dest.getMinX() - dest.getSampleModelTranslateX(); + int destY = dest.getMinY() - dest.getSampleModelTranslateY(); + + int numBands = srcSM.getNumBands(); + + /* We can't use getOffset(x, y) from the sample model since we + don't want the band offset added in. */ + + int srcOffset = + numBands*srcX + srcScanlineStride*srcY + // from sample model + srcDB.getOffset(); // from data buffer + + int destOffset = + numBands*destX + destScanlineStride*destY + // from sample model + destDB.getOffset(); // from data buffer + + // Determine how much, and how many times to blit. + + int rowSize = src.getWidth()*numBands; + int h = src.getHeight(); + + if ((rowSize == srcScanlineStride) && + (rowSize == destScanlineStride)) + { + // collapse scan line blits to one large blit. + rowSize *= h; + h = 1; + } + + + // Do blitting + + Object srcArray = Buffers.getData(srcDB); + Object destArray = Buffers.getData(destDB); + + for (int yd = 0; yd<h; yd++) + { + System.arraycopy(srcArray, srcOffset, + destArray, destOffset, + rowSize); + srcOffset += srcScanlineStride; + destOffset += destScanlineStride; + } + + + return dest; + } + + public Rectangle2D getBounds2D(Raster src) + { + return src.getBounds(); + } + + public WritableRaster createCompatibleDestRaster(Raster src) { + + /* FIXME: Maybe we should explicitly create a raster with a + tightly pixel packed sample model, rather than assuming + that the createCompatibleWritableRaster() method in Raster + will create one. */ + + return src.createCompatibleWritableRaster(); + } + + public Point2D getPoint2D(Point2D srcPoint, Point2D destPoint) + { + if (destPoint == null) + return (Point2D) srcPoint.clone(); + + destPoint.setLocation(srcPoint); + return destPoint; + } + + public RenderingHints getRenderingHints() + { + throw new UnsupportedOperationException("not implemented"); + } +} diff --git a/libjava/classpath/gnu/java/awt/EmbeddedWindow.java b/libjava/classpath/gnu/java/awt/EmbeddedWindow.java new file mode 100644 index 00000000000..a85ce7454c1 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/EmbeddedWindow.java @@ -0,0 +1,139 @@ +/* EmbeddedWindow.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt; + +import gnu.java.awt.peer.EmbeddedWindowPeer; +import gnu.java.security.action.SetAccessibleAction; + +import java.awt.Component; +import java.awt.Frame; +import java.awt.Toolkit; +import java.lang.reflect.Field; +import java.security.AccessController; + +/** + * Represents an AWT window that can be embedded into another + * application. + * + * @author Michael Koch (konqueror@gmx.de) + */ +public class EmbeddedWindow extends Frame +{ + private long handle; + + /** + * Creates a window to be embedded into another application. The + * window will only be embedded after its setHandle method has been + * called. + */ + public EmbeddedWindow () + { + super(); + this.handle = 0; + } + + /** + * Creates a window to be embedded into another application. + * + * @param handle the native handle to the screen area where the AWT + * window should be embedded + */ + public EmbeddedWindow (long handle) + { + super(); + this.handle = handle; + } + + /** + * Creates the native peer for this embedded window. + */ + public void addNotify() + { + // Assume we're using ClasspathToolkit + ClasspathToolkit tk = (ClasspathToolkit) getToolkit(); + + // Circumvent the package-privateness of the AWT internal + // java.awt.Component.peer member variable. + try + { + Field peerField = Component.class.getDeclaredField("peer"); + AccessController.doPrivileged(new SetAccessibleAction(peerField)); + peerField.set(this, tk.createEmbeddedWindow (this)); + } + catch (IllegalAccessException e) + { + // This should never happen. + } + catch (NoSuchFieldException e) + { + // This should never happen. + } + + super.addNotify(); + } + + /** + * If the native peer for this embedded window has been created, + * then setHandle will embed the window. If not, setHandle tells + * us where to embed ourselves when our peer is created. + * + * @param handle the native handle to the screen area where the AWT + * window should be embedded + */ + public void setHandle(long handle) + { + if (this.handle != 0) + throw new RuntimeException ("EmbeddedWindow is already embedded"); + + this.handle = handle; + if (getPeer() != null) + ((EmbeddedWindowPeer) getPeer()).embed (this.handle); + } + + /** + * Gets the native handle of the screen area where the window will + * be embedded. + * + * @return The native handle that was passed to the constructor. + */ + public long getHandle() + { + return handle; + } +} diff --git a/libjava/classpath/gnu/java/awt/EventModifier.java b/libjava/classpath/gnu/java/awt/EventModifier.java new file mode 100644 index 00000000000..565fcbc3297 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/EventModifier.java @@ -0,0 +1,107 @@ +/* EventModifier.java -- tool for converting modifier bits to 1.4 syle + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt; + +import java.awt.event.InputEvent; + +public class EventModifier +{ + /** The mask for old events. */ + public static final int OLD_MASK = 0x3f; + + /** The mask for new events. */ + public static final int NEW_MASK = 0x3fc0; + + /** + * Non-instantiable. + */ + private EventModifier() + { + throw new InternalError(); + } + + /** + * Converts the old style modifiers (0x3f) to the new style (0xffffffc0). + * + * @param mod the modifiers to convert + * @return the adjusted modifiers + */ + public static int extend(int mod) + { + // Favor what we hope will be the common case. + if ((mod & OLD_MASK) == 0) + return mod; + if ((mod & InputEvent.SHIFT_MASK) != 0) + mod |= InputEvent.SHIFT_DOWN_MASK; + if ((mod & InputEvent.CTRL_MASK) != 0) + mod |= InputEvent.CTRL_DOWN_MASK; + if ((mod & InputEvent.META_MASK) != 0) + mod |= InputEvent.META_DOWN_MASK; + if ((mod & InputEvent.ALT_MASK) != 0) + mod |= InputEvent.ALT_DOWN_MASK; + if ((mod & InputEvent.BUTTON1_MASK) != 0) + mod |= InputEvent.BUTTON1_DOWN_MASK; + if ((mod & InputEvent.ALT_GRAPH_MASK) != 0) + mod |= InputEvent.ALT_GRAPH_DOWN_MASK; + return mod & ~OLD_MASK; + } + + /** + * Converts the new style modifiers (0xffffffc0) to the old style (0x3f). + * + * @param mod the modifiers to convert + * @return the adjusted modifiers + */ + public static int revert(int mod) + { + if ((mod & InputEvent.SHIFT_DOWN_MASK) != 0) + mod |= InputEvent.SHIFT_MASK; + if ((mod & InputEvent.CTRL_DOWN_MASK) != 0) + mod |= InputEvent.CTRL_MASK; + if ((mod & InputEvent.META_DOWN_MASK) != 0) + mod |= InputEvent.META_MASK; + if ((mod & InputEvent.ALT_DOWN_MASK) != 0) + mod |= InputEvent.ALT_MASK; + if ((mod & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) + mod |= InputEvent.ALT_GRAPH_MASK; + if ((mod & InputEvent.BUTTON1_DOWN_MASK) != 0) + mod |= InputEvent.BUTTON1_MASK; + return mod & OLD_MASK; + } +} // class EventModifier diff --git a/libjava/classpath/gnu/java/awt/FocusManager.java b/libjava/classpath/gnu/java/awt/FocusManager.java new file mode 100644 index 00000000000..49b40bfc184 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/FocusManager.java @@ -0,0 +1,52 @@ +/* FocusManager.java -- Provide Swing FocusManager API compatibility + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt; + +/** + * This is a subclass of the otherwise abstract class + * {@link javax.swing.FocusManager}. Its sole purpose is to make the Swing + * FocusManager usable as a FocusManager in AWT, so that we can provide both + * the new (1.4) KeyboardFocusManager API and still support the older + * Swing FocusManager. + * + * @author Roman Kennke + */ +public class FocusManager + extends javax.swing.FocusManager +{ +} diff --git a/libjava/classpath/gnu/java/awt/GradientPaintContext.java b/libjava/classpath/gnu/java/awt/GradientPaintContext.java new file mode 100644 index 00000000000..5e1d62dd972 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/GradientPaintContext.java @@ -0,0 +1,164 @@ +/* GradientPaintContext.java -- + Copyright (C) 2005, Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt; + +import java.awt.geom.Point2D; +import java.awt.image.ColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.PaintContext; +import java.awt.Color; + +/** + * A {@link PaintContext} used by the {@link GradientPaint} class. + */ +public class GradientPaintContext implements PaintContext +{ + + // This implementation follows the technique described in + // "Java(tm) 2D Graphics" by Jonathan Knudsen (O'Reilly 1999). + + /** The x-coordinate of the anchor point for color 1. */ + private final float x1; + + /** The y-coordinate of the anchor point for color 1. */ + private final float y1; + + /** Color 1. */ + private final Color c1; + + /** The x-coordinate of the anchor point for color 2. */ + private final float x2; + + /** The y-coordinate of the anchor point for color 2. */ + private final float y2; + + /** Color 2. */ + private final Color c2; + + /** A flag indicating whether the gradient is cyclic or acyclic. */ + private final boolean cyclic; + + /** The length of the gradient line - computed from the two anchor points. */ + private final double length; + + /** + * Creates a new instance. + * + * @param x1 the x-coordinate for the anchor point for color 1. + * @param y1 the y-coordinate for the anchor point for color 1. + * @param c1 color 1. + * @param x2 the x-coordinate for the anchor point for color 2. + * @param y2 the y-coordinate for the anchor point for color 2. + * @param c2 color 2. + * @param cyclic a flag that determines whether the gradient is cyclic + * or acyclic. + */ + public GradientPaintContext(float x1, float y1, Color c1, + float x2, float y2, Color c2, boolean cyclic) + { + this.x1 = x1; + this.y1 = y1; + this.c1 = c1; + this.x2 = x2; + this.y2 = y2; + this.c2 = c2; + this.cyclic = cyclic; + length = Point2D.distance(x1, y1, x2, y2); + } + + /** + * Return the color model of this context. It may be different from the + * hint specified during createContext, as not all contexts can generate + * color patterns in an arbitrary model. + * + * @return the context color model + */ + public ColorModel getColorModel() + { + return ColorModel.getRGBdefault(); + } + + /** + * Return a raster containing the colors for the graphics operation. + * + * @param x the x-coordinate, in device space + * @param y the y-coordinate, in device space + * @param w the width, in device space + * @param h the height, in device space + * @return a raster for the given area and color + */ + public Raster getRaster(int x, int y, int w, int h) { + ColorModel cm = getColorModel(); + WritableRaster raster = cm.createCompatibleWritableRaster(w, h); + int[] data = new int[w * h * 4]; + double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + for (int r = 0; r < h; r++) { + for (int c = 0; c < w; c++) { + double u = 0.0; + if (pd2 != 0) + u = (((x + c) - x1) * (x2 - x1) + ((y + r) - y1) * (y2 - y1)) + / Math.sqrt(pd2); + double ratio = u / length; + if (cyclic) + ratio = Math.abs(ratio - Math.floor((ratio + 1.0) / 2.0) * 2.0); + else + ratio = Math.max(0.0, Math.min(1.0, ratio)); + int base = (r * w + c) * 4; + data[base] = (int) (c1.getRed() + ratio * (c2.getRed() - c1.getRed())); + data[base + 1] + = (int) (c1.getGreen() + ratio * (c2.getGreen() - c1.getGreen())); + data[base + 2] + = (int) (c1.getBlue() + ratio * (c2.getBlue() - c1.getBlue())); + data[base + 3] + = (int) (c1.getAlpha() + ratio * (c2.getAlpha() - c1.getAlpha())); + } + } + raster.setPixels(0, 0, w, h, data); + return raster; + } + + /** + * Release the resources allocated for the paint (none in this + * implementation). + */ + public void dispose() { + // nothing to do + } + +} diff --git a/libjava/classpath/gnu/java/awt/color/CieXyzConverter.java b/libjava/classpath/gnu/java/awt/color/CieXyzConverter.java new file mode 100644 index 00000000000..1742805854d --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/CieXyzConverter.java @@ -0,0 +1,73 @@ +/* CieXyzConverter.java -- CieXyz conversion class + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * CieXyzConverter - converts to/from a D50-relative CIE XYZ color space. + * + * The sRGB<->CIE XYZ conversions in SrgbConverter are used. + * + * @author Sven de Marothy + */ +public class CieXyzConverter implements ColorSpaceConverter +{ + public float[] toCIEXYZ(float[] in) + { + float[] out = new float[3]; + System.arraycopy(in, 0, out, 0, 3); + return out; + } + + public float[] fromCIEXYZ(float[] in) + { + float[] out = new float[3]; + System.arraycopy(in, 0, out, 0, 3); + return out; + } + + public float[] toRGB(float[] in) + { + return SrgbConverter.XYZtoRGB(in); + } + + public float[] fromRGB(float[] in) + { + return SrgbConverter.RGBtoXYZ(in); + } +} diff --git a/libjava/classpath/gnu/java/awt/color/ClutProfileConverter.java b/libjava/classpath/gnu/java/awt/color/ClutProfileConverter.java new file mode 100644 index 00000000000..bff97cc9b87 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/ClutProfileConverter.java @@ -0,0 +1,152 @@ +/* ClutProfileConverter.java -- Conversion routines for CLUT-Based profiles + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + +import java.awt.color.ICC_Profile; + + +/** + * ClutProfileConverter - conversions through a CLUT-based profile + * + * @author Sven de Marothy + */ +public class ClutProfileConverter implements ColorSpaceConverter +{ + private ColorLookUpTable toPCS; + private ColorLookUpTable fromPCS; + private int nChannels; + + public ClutProfileConverter(ICC_Profile profile) + { + nChannels = profile.getNumComponents(); + + // Sun does not specifiy which rendering intent should be used, + // neither does the ICC v2 spec really. + // Try intent 0 + try + { + toPCS = new ColorLookUpTable(profile, ICC_Profile.icSigAToB0Tag); + } + catch (Exception e) + { + toPCS = null; + } + + try + { + fromPCS = new ColorLookUpTable(profile, ICC_Profile.icSigBToA0Tag); + } + catch (Exception e) + { + fromPCS = null; + } + + if (toPCS != null || fromPCS != null) + return; + + // If no intent 0 clut is available, look for a intent 1 clut. + try + { + toPCS = new ColorLookUpTable(profile, ICC_Profile.icSigAToB1Tag); + } + catch (Exception e) + { + toPCS = null; + } + + try + { + fromPCS = new ColorLookUpTable(profile, ICC_Profile.icSigBToA1Tag); + } + catch (Exception e) + { + fromPCS = null; + } + + if (toPCS != null || fromPCS != null) + return; + + // Last shot.. intent 2 CLUT. + try + { + toPCS = new ColorLookUpTable(profile, ICC_Profile.icSigAToB2Tag); + } + catch (Exception e) + { + toPCS = null; + } + + try + { + fromPCS = new ColorLookUpTable(profile, ICC_Profile.icSigBToA2Tag); + } + catch (Exception e) + { + fromPCS = null; + } + + if (toPCS == null && fromPCS == null) + throw new IllegalArgumentException("No CLUTs in profile!"); + } + + public float[] toCIEXYZ(float[] in) + { + if (toPCS != null) + return toPCS.lookup(in); + else + return new float[3]; + } + + public float[] toRGB(float[] in) + { + return SrgbConverter.XYZtoRGB(toCIEXYZ(in)); + } + + public float[] fromCIEXYZ(float[] in) + { + if (fromPCS != null) + return fromPCS.lookup(in); + else + return new float[nChannels]; + } + + public float[] fromRGB(float[] in) + { + return fromCIEXYZ(SrgbConverter.RGBtoXYZ(in)); + } +} diff --git a/libjava/classpath/gnu/java/awt/color/ColorLookUpTable.java b/libjava/classpath/gnu/java/awt/color/ColorLookUpTable.java new file mode 100644 index 00000000000..6b6418bc284 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/ColorLookUpTable.java @@ -0,0 +1,429 @@ +/* ColorLookUpTable.java -- ICC v2 CLUT + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + +import java.awt.color.ColorSpace; +import java.awt.color.ICC_Profile; +import java.nio.ByteBuffer; + + +/** + * ColorLookUpTable handles color lookups through a color lookup table, + * as defined in the ICC specification. + * Both 'mft2' and 'mft1' (8 and 16-bit) type CLUTs are handled. + * + * This will have to be updated later for ICC 4.0.0 + * + * @author Sven de Marothy + */ +public class ColorLookUpTable +{ + /** + * CIE 1931 D50 white point (in Lab coordinates) + */ + private static float[] D50 = { 0.96422f, 1.00f, 0.82521f }; + + /** + * Number of input/output channels + */ + int nIn; + + /** + * Number of input/output channels + */ + int nOut; + int nInTableEntries; // Number of input table entries + int nOutTableEntries; // Number of output table entries + int gridpoints; // Number of gridpoints + int nClut; // This is nOut*(gridpoints**nIn) + double[][] inTable; // 1D input table ([channel][table]) + short[][] outTable; // 1D input table ([channel][table]) + double[] clut; // The color lookup table + float[][] inMatrix; // input matrix (XYZ only) + boolean useMatrix; // Whether to use the matrix or not. + int[] multiplier; + int[] offsets; // Hypercube offsets + boolean inputLab; // Set if the CLUT input CS is Lab + boolean outputLab; // Set if the CLUT output CS is Lab + + /** + * Constructor + * Requires a profile file to get the CLUT from and the tag of the + * CLUT to create. (icSigXToYZTag where X,Y = [A | B], Z = [0,1,2]) + */ + public ColorLookUpTable(ICC_Profile profile, int tag) + { + useMatrix = false; + + switch (tag) + { + case ICC_Profile.icSigAToB0Tag: + case ICC_Profile.icSigAToB1Tag: + case ICC_Profile.icSigAToB2Tag: + if (profile.getColorSpaceType() == ColorSpace.TYPE_XYZ) + useMatrix = true; + inputLab = false; + outputLab = (profile.getPCSType() == ColorSpace.TYPE_Lab); + break; + case ICC_Profile.icSigBToA0Tag: + case ICC_Profile.icSigBToA1Tag: + case ICC_Profile.icSigBToA2Tag: + if (profile.getPCSType() == ColorSpace.TYPE_XYZ) + useMatrix = true; + inputLab = (profile.getPCSType() == ColorSpace.TYPE_Lab); + outputLab = false; + break; + default: + throw new IllegalArgumentException("Not a clut-type tag."); + } + + byte[] data = profile.getData(tag); + if (data == null) + throw new IllegalArgumentException("Unsuitable profile, does not contain a CLUT."); + + // check 'mft' + if (data[0] != 0x6d || data[1] != 0x66 || data[2] != 0x74) + throw new IllegalArgumentException("Unsuitable profile, invalid CLUT data."); + + if (data[3] == 0x32) + readClut16(data); + else if (data[3] == 0x31) + readClut8(data); + else + throw new IllegalArgumentException("Unknown/invalid CLUT type."); + } + + /** + * Loads a 16-bit CLUT into our data structures + */ + private void readClut16(byte[] data) + { + ByteBuffer buf = ByteBuffer.wrap(data); + + nIn = data[8] & (0xFF); + nOut = data[9] & (0xFF); + nInTableEntries = buf.getShort(48); + nOutTableEntries = buf.getShort(50); + gridpoints = data[10] & (0xFF); + + inMatrix = new float[3][3]; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + inMatrix[i][j] = ((float) (buf.getInt(12 + (i * 3 + j) * 4))) / 65536.0f; + + inTable = new double[nIn][nInTableEntries]; + for (int channel = 0; channel < nIn; channel++) + for (int i = 0; i < nInTableEntries; i++) + inTable[channel][i] = (double) ((int) buf.getShort(52 + + (channel * nInTableEntries + + i) * 2) + & (0xFFFF)) / 65536.0; + + nClut = nOut; + multiplier = new int[nIn]; + multiplier[nIn - 1] = nOut; + for (int i = 0; i < nIn; i++) + { + nClut *= gridpoints; + if (i > 0) + multiplier[nIn - i - 1] = multiplier[nIn - i] * gridpoints; + } + + int clutOffset = 52 + nIn * nInTableEntries * 2; + clut = new double[nClut]; + for (int i = 0; i < nClut; i++) + clut[i] = (double) ((int) buf.getShort(clutOffset + i * 2) & (0xFFFF)) / 65536.0; + + outTable = new short[nOut][nOutTableEntries]; + for (int channel = 0; channel < nOut; channel++) + for (int i = 0; i < nOutTableEntries; i++) + outTable[channel][i] = buf.getShort(clutOffset + + (nClut + + channel * nOutTableEntries + i) * 2); + + // calculate the hypercube corner offsets + offsets = new int[(1 << nIn)]; + offsets[0] = 0; + for (int j = 0; j < nIn; j++) + { + int factor = 1 << j; + for (int i = 0; i < factor; i++) + offsets[factor + i] = offsets[i] + multiplier[j]; + } + } + + /** + * Loads a 8-bit CLUT into our data structures. + */ + private void readClut8(byte[] data) + { + ByteBuffer buf = ByteBuffer.wrap(data); + + nIn = (data[8] & (0xFF)); + nOut = (data[9] & (0xFF)); + nInTableEntries = 256; // always 256 + nOutTableEntries = 256; // always 256 + gridpoints = (data[10] & (0xFF)); + + inMatrix = new float[3][3]; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + inMatrix[i][j] = ((float) (buf.getInt(12 + (i * 3 + j) * 4))) / 65536.0f; + + inTable = new double[nIn][nInTableEntries]; + for (int channel = 0; channel < nIn; channel++) + for (int i = 0; i < nInTableEntries; i++) + inTable[channel][i] = (double) ((int) buf.get(48 + + (channel * nInTableEntries + + i)) & (0xFF)) / 255.0; + + nClut = nOut; + multiplier = new int[nIn]; + multiplier[nIn - 1] = nOut; + for (int i = 0; i < nIn; i++) + { + nClut *= gridpoints; + if (i > 0) + multiplier[nIn - i - 1] = multiplier[nIn - i] * gridpoints; + } + + int clutOffset = 48 + nIn * nInTableEntries; + clut = new double[nClut]; + for (int i = 0; i < nClut; i++) + clut[i] = (double) ((int) buf.get(clutOffset + i) & (0xFF)) / 255.0; + + outTable = new short[nOut][nOutTableEntries]; + for (int channel = 0; channel < nOut; channel++) + for (int i = 0; i < nOutTableEntries; i++) + outTable[channel][i] = (short) (buf.get(clutOffset + nClut + + channel * nOutTableEntries + + i) * 257); + + // calculate the hypercube corner offsets + offsets = new int[(1 << nIn)]; + offsets[0] = 0; + for (int j = 0; j < nIn; j++) + { + int factor = 1 << j; + for (int i = 0; i < factor; i++) + offsets[factor + i] = offsets[i] + multiplier[j]; + } + } + + /** + * Performs a lookup through the Color LookUp Table. + * If the CLUT tag type is AtoB the conversion will be from the device + * color space to the PCS, BtoA type goes in the opposite direction. + * + * For convenience, the PCS values for input or output will always be + * CIE XYZ (D50), if the actual PCS is Lab, the values will be converted. + * + * N-dimensional linear interpolation is used. + */ + float[] lookup(float[] in) + { + float[] in2 = new float[in.length]; + if (useMatrix) + { + for (int i = 0; i < 3; i++) + in2[i] = in[0] * inMatrix[i][0] + in[1] * inMatrix[i][1] + + in[2] * inMatrix[i][2]; + } + else if (inputLab) + in2 = XYZtoLab(in); + else + System.arraycopy(in, 0, in2, 0, in.length); + + // input table + for (int i = 0; i < nIn; i++) + { + int index = (int) Math.floor(in2[i] * (double) (nInTableEntries - 1)); // floor in + + // clip values. + if (index >= nInTableEntries - 1) + in2[i] = (float) inTable[i][nInTableEntries - 1]; + else if (index < 0) + in2[i] = (float) inTable[i][0]; + else + { + // linear interpolation + double alpha = in2[i] * ((double) nInTableEntries - 1.0) - index; + in2[i] = (float) (inTable[i][index] * (1 - alpha) + + inTable[i][index + 1] * alpha); + } + } + + // CLUT lookup + double[] output2 = new double[nOut]; + double[] weights = new double[(1 << nIn)]; + double[] clutalpha = new double[nIn]; // interpolation values + int offset = 0; // = gp + for (int i = 0; i < nIn; i++) + { + int index = (int) Math.floor(in2[i] * ((double) gridpoints - 1.0)); + double alpha = in2[i] * ((double) gridpoints - 1.0) - (double) index; + + // clip values. + if (index >= gridpoints - 1) + { + index = gridpoints - 1; + alpha = 1.0; + } + else if (index < 0) + index = 0; + clutalpha[i] = alpha; + offset += index * multiplier[i]; + } + + // Calculate interpolation weights + weights[0] = 1.0; + for (int j = 0; j < nIn; j++) + { + int factor = 1 << j; + for (int i = 0; i < factor; i++) + { + weights[factor + i] = weights[i] * clutalpha[j]; + weights[i] *= (1.0 - clutalpha[j]); + } + } + + for (int i = 0; i < nOut; i++) + output2[i] = weights[0] * clut[offset + i]; + + for (int i = 1; i < (1 << nIn); i++) + { + int offset2 = offset + offsets[i]; + for (int f = 0; f < nOut; f++) + output2[f] += weights[i] * clut[offset2 + f]; + } + + // output table + float[] output = new float[nOut]; + for (int i = 0; i < nOut; i++) + { + int index = (int) Math.floor(output2[i] * ((double) nOutTableEntries + - 1.0)); + + // clip values. + if (index >= nOutTableEntries - 1) + output[i] = outTable[i][nOutTableEntries - 1]; + else if (index < 0) + output[i] = outTable[i][0]; + else + { + // linear interpolation + double a = output2[i] * ((double) nOutTableEntries - 1.0) + - (double) index; + output[i] = (float) ((double) ((int) outTable[i][index] & (0xFFFF)) * (1 + - a) + + (double) ((int) outTable[i][index + 1] & (0xFFFF)) * a) / 65536f; + } + } + + if (outputLab) + return LabtoXYZ(output); + return output; + } + + /** + * Converts CIE Lab coordinates to (D50) XYZ ones. + */ + private float[] LabtoXYZ(float[] in) + { + // Convert from byte-packed format to a + // more convenient one (actual Lab values) + // (See ICC spec for details) + // factor is 100 * 65536 / 65280 + in[0] = (float) (100.392156862745 * in[0]); + in[1] = (in[1] * 256.0f) - 128.0f; + in[2] = (in[2] * 256.0f) - 128.0f; + + float[] out = new float[3]; + + out[1] = (in[0] + 16.0f) / 116.0f; + out[0] = in[1] / 500.0f + out[1]; + out[2] = out[1] - in[2] / 200.0f; + + for (int i = 0; i < 3; i++) + { + double exp = out[i] * out[i] * out[i]; + if (exp <= 0.008856) + out[i] = (out[i] - 16.0f / 116.0f) / 7.787f; + else + out[i] = (float) exp; + out[i] = D50[i] * out[i]; + } + return out; + } + + /** + * Converts CIE XYZ coordinates to Lab ones. + */ + private float[] XYZtoLab(float[] in) + { + float[] temp = new float[3]; + + for (int i = 0; i < 3; i++) + { + temp[i] = in[i] / D50[i]; + + if (temp[i] <= 0.008856f) + temp[i] = (7.7870689f * temp[i]) + (16f / 116.0f); + else + temp[i] = (float) Math.exp((1.0 / 3.0) * Math.log(temp[i])); + } + + float[] out = new float[3]; + out[0] = (116.0f * temp[1]) - 16f; + out[1] = 500.0f * (temp[0] - temp[1]); + out[2] = 200.0f * (temp[1] - temp[2]); + + // Normalize to packed format + out[0] = (float) (out[0] / 100.392156862745); + out[1] = (out[1] + 128f) / 256f; + out[2] = (out[2] + 128f) / 256f; + for (int i = 0; i < 3; i++) + { + if (out[i] < 0f) + out[i] = 0f; + if (out[i] > 1f) + out[i] = 1f; + } + return out; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/ColorSpaceConverter.java b/libjava/classpath/gnu/java/awt/color/ColorSpaceConverter.java new file mode 100644 index 00000000000..63ba08a4ff5 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/ColorSpaceConverter.java @@ -0,0 +1,69 @@ +/* ColorSpaceConverter.java -- an interface for colorspace conversion + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * ColorSpaceConverter - used by java.awt.color.ICC_ColorSpace + * + * Color space conversion can occur in several ways: + * + * -Directly (for the built in spaces sRGB, linear RGB, gray, CIE XYZ and PYCC + * -ICC_ProfileRGB works through TRC curves and a matrix + * -ICC_ProfileGray works through a single TRC + * -Everything else is done through Color lookup tables. + * + * The different conversion methods are implemented through + * an interface. The built-in colorspaces are implemented directly + * with the relevant conversion equations. + * + * In this way, we hopefully will always use the fastest and most + * accurate method available. + * + * @author Sven de Marothy + */ +public interface ColorSpaceConverter +{ + float[] toCIEXYZ(float[] in); + + float[] fromCIEXYZ(float[] in); + + float[] toRGB(float[] in); + + float[] fromRGB(float[] in); +} diff --git a/libjava/classpath/gnu/java/awt/color/GrayProfileConverter.java b/libjava/classpath/gnu/java/awt/color/GrayProfileConverter.java new file mode 100644 index 00000000000..3c725138a66 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/GrayProfileConverter.java @@ -0,0 +1,137 @@ +/* GrayProfileConverter.java -- Gray profile conversion class + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.color; + +import java.awt.color.ICC_Profile; +import java.awt.color.ICC_ProfileGray; +import java.awt.color.ProfileDataException; + +/** + * GrayProfileConverter - converts Grayscale profiles (ICC_ProfileGray) + * + * This type of profile contains a single tone reproduction curve (TRC). + * Conversion consists of simple TRC lookup. + * + * This implementation is very lazy and does everything applying the TRC and + * utilizing the built-in linear grayscale color space. + * + * @author Sven de Marothy + */ +public class GrayProfileConverter implements ColorSpaceConverter +{ + private GrayScaleConverter gc; + private ToneReproductionCurve trc; + private ColorLookUpTable toPCS; + private ColorLookUpTable fromPCS; + + /** + * Constructs the converter described by an ICC_ProfileGray object + */ + public GrayProfileConverter(ICC_ProfileGray profile) + { + try + { + trc = new ToneReproductionCurve(profile.getGamma()); + } + catch (ProfileDataException e) + { + trc = new ToneReproductionCurve(profile.getTRC()); + } + + // linear grayscale converter + gc = new GrayScaleConverter(); + + // If a CLUT is available, it should be used, and the TRCs ignored. + // Note: A valid profile may only have CLUTs in one direction, and + // TRC:s without useful info, making reverse-transforms impossible. + // In this case the TRC will be used for the reverse-transform with + // unpredictable results. This is in line with the Java specification, + try + { + toPCS = new ColorLookUpTable(profile, ICC_Profile.icSigAToB0Tag); + } + catch (Exception e) + { + toPCS = null; + } + + try + { + fromPCS = new ColorLookUpTable(profile, ICC_Profile.icSigBToA0Tag); + } + catch (Exception e) + { + fromPCS = null; + } + } + + public float[] toCIEXYZ(float[] in) + { + if (toPCS != null) + return toPCS.lookup(in); + float[] gray = new float[1]; + gray[0] = trc.lookup(in[0]); + return gc.toCIEXYZ(gray); + } + + public float[] toRGB(float[] in) + { + float[] gray = new float[1]; + gray[0] = trc.lookup(in[0]); + return gc.toRGB(gray); + } + + public float[] fromRGB(float[] in) + { + // get linear grayscale value + float[] gray = gc.fromRGB(in); + gray[0] = trc.reverseLookup(gray[0]); + return gray; + } + + public float[] fromCIEXYZ(float[] in) + { + if (fromPCS != null) + return fromPCS.lookup(in); + + float[] gray = gc.fromCIEXYZ(in); + gray[0] = trc.reverseLookup(gray[0]); + return gray; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/GrayScaleConverter.java b/libjava/classpath/gnu/java/awt/color/GrayScaleConverter.java new file mode 100644 index 00000000000..7a27ddba828 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/GrayScaleConverter.java @@ -0,0 +1,110 @@ +/* GrayScaleConverter.java -- Linear grayscale conversion class + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * Linear Grayscale converter + * + * @author Sven de Marothy + */ +public class GrayScaleConverter implements ColorSpaceConverter +{ + // intensity factors (ITU Rec. BT.709) + double[] coeff = { 0.2125f, 0.7154f, 0.0721f }; + + /** + * CIE 1931 D50 white point (in Lab coordinates) + */ + private static float[] D50 = { 0.96422f, 1.00f, 0.82521f }; + + public float[] toCIEXYZ(float[] in) + { + float g = in[0]; + if (g < 0) + g = 1 + g; + float[] out = { g * D50[0], g * D50[1], g * D50[2] }; // White spot + return out; + } + + public float[] toRGB(float[] in) + { + float[] out = new float[3]; + if (in[0] <= 0.00304f) + out[0] = in[0] * 12.92f; + else + out[0] = 1.055f * ((float) Math.exp((1 / 2.4) * Math.log(in[0]))) + - 0.055f; + out[1] = out[2] = out[0]; + return out; + } + + public float[] fromCIEXYZ(float[] in) + { + float[] temp = new float[3]; + temp[0] = 3.1338f * in[0] - 1.6171f * in[1] - 0.4907f * in[2]; + temp[1] = -0.9785f * in[0] + 1.9160f * in[1] + 0.0334f * in[2]; + temp[2] = 0.0720f * in[0] - 0.2290f * in[1] + 1.4056f * in[2]; + float[] out = new float[1]; + for (int i = 0; i < 3; i++) + out[0] = (float) (temp[i] * coeff[i]); + return out; + } + + public float[] fromRGB(float[] in) + { + float[] out = new float[1]; + + // Convert non-linear RGB coordinates to linear ones, + // numbers from the w3 spec. + out[0] = 0; + for (int i = 0; i < 3; i++) + { + float n = in[i]; + if (n < 0) + n = 0f; + if (n > 1) + n = 1f; + if (n <= 0.03928f) + out[0] += (float) (coeff[i] * n / 12.92); + else + out[0] += (float) (coeff[i] * Math.exp(2.4 * Math.log((n + 0.055) / 1.055))); + } + return out; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/LinearRGBConverter.java b/libjava/classpath/gnu/java/awt/color/LinearRGBConverter.java new file mode 100644 index 00000000000..13a4e2c04f4 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/LinearRGBConverter.java @@ -0,0 +1,152 @@ +/* LinearRGBConverter.java -- conversion to a linear RGB color space + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * LinearRGBConverter - conversion routines for a linear sRGB colorspace + * sRGB is a standard for RGB colorspaces, adopted by the w3c. + * + * The specification is available at: + * http://www.w3.org/Graphics/Color/sRGB.html + * + * @author Sven de Marothy + */ +public class LinearRGBConverter implements ColorSpaceConverter +{ + /** + * linear RGB --> sRGB + * Use the inverse gamma curve + */ + public float[] toRGB(float[] in) + { + float[] out = new float[3]; + for (int i = 0; i < 3; i++) + { + float n = in[i]; + if (n < 0) + n = 0f; + if (n > 1) + n = 1f; + if (n <= 0.00304f) + out[i] = in[0] * 12.92f; + else + out[i] = 1.055f * ((float) Math.exp((1 / 2.4) * Math.log(n))) + - 0.055f; + } + return out; + } + + /** + * sRGB --> linear RGB + * Use the gamma curve (gamma=2.4 in sRGB) + */ + public float[] fromRGB(float[] in) + { + float[] out = new float[3]; + + // Convert non-linear RGB coordinates to linear ones, + // numbers from the w3 spec. + for (int i = 0; i < 3; i++) + { + float n = in[i]; + if (n < 0) + n = 0f; + if (n > 1) + n = 1f; + if (n <= 0.03928f) + out[i] = (float) (n / 12.92); + else + out[i] = (float) (Math.exp(2.4 * Math.log((n + 0.055) / 1.055))); + } + return out; + } + + /** + * Linear RGB --> CIE XYZ (D50 relative) + * This is a simple matrix transform, the matrix (relative D65) + * is given in the sRGB spec. This has been combined with a + * linear Bradford transform for the D65-->D50 mapping, resulting + * in a single matrix which does the whole thing. + * + */ + public float[] fromCIEXYZ(float[] in) + { + /* + * Note: The numbers which were used to calculate this only had four + * digits of accuracy. So don't be fooled by the number of digits here. + * If someone has more accurate source, feel free to update this. + */ + float[] out = new float[3]; + out[0] = (float) (3.13383065124221 * in[0] - 1.61711949411313 * in[1] + - 0.49071914111101 * in[2]); + out[1] = (float) (-0.97847026691142 * in[0] + 1.91597856031996 * in[1] + + 0.03340430640699 * in[2]); + out[2] = (float) (0.07203679486279 * in[0] - 0.22903073553113 * in[1] + + 1.40557835776234 * in[2]); + if (out[0] < 0) + out[0] = 0f; + if (out[1] < 0) + out[1] = 0f; + if (out[2] < 0) + out[2] = 0f; + if (out[0] > 1.0f) + out[0] = 1.0f; + if (out[1] > 1.0f) + out[1] = 1.0f; + if (out[2] > 1.0f) + out[2] = 1.0f; + return out; + } + + /** + * Linear RGB --> CIE XYZ (D50 relative) + * Uses the inverse of the above matrix. + */ + public float[] toCIEXYZ(float[] in) + { + float[] out = new float[3]; + out[0] = (float) (0.43606375022190 * in[0] + 0.38514960146481 * in[1] + + 0.14308641888799 * in[2]); + out[1] = (float) (0.22245089403542 * in[0] + 0.71692584775182 * in[1] + + 0.06062451125578 * in[2]); + out[2] = (float) (0.01389851860679 * in[0] + 0.09707969011198 * in[1] + + 0.71399604572506 * in[2]); + return out; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/ProfileHeader.java b/libjava/classpath/gnu/java/awt/color/ProfileHeader.java new file mode 100644 index 00000000000..4fdef560a42 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/ProfileHeader.java @@ -0,0 +1,398 @@ +/* ProfileHeader.java -- Encapsules ICC Profile header data + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + +import java.awt.color.ColorSpace; +import java.awt.color.ICC_Profile; +import java.nio.ByteBuffer; + + +/** + * Header, abstracts and validates the header data. + * + * @author Sven de Marothy + */ +public class ProfileHeader +{ + /** + * Magic identifier (ASCII 'acsp') + */ + private static final int icMagicNumber = 0x61637370; + + /** + * Mapping from ICC Profile signatures to ColorSpace types + */ + private static final int[] csTypeMap = + { + ICC_Profile.icSigXYZData, + ColorSpace.TYPE_XYZ, + ICC_Profile.icSigLabData, + ColorSpace.TYPE_Lab, + ICC_Profile.icSigLuvData, + ColorSpace.TYPE_Luv, + ICC_Profile.icSigYCbCrData, + ColorSpace.TYPE_YCbCr, + ICC_Profile.icSigYxyData, + ColorSpace.TYPE_Yxy, + ICC_Profile.icSigRgbData, + ColorSpace.TYPE_RGB, + ICC_Profile.icSigGrayData, + ColorSpace.TYPE_GRAY, + ICC_Profile.icSigHsvData, + ColorSpace.TYPE_HSV, + ICC_Profile.icSigHlsData, + ColorSpace.TYPE_HLS, + ICC_Profile.icSigCmykData, + ColorSpace.TYPE_CMYK, + ICC_Profile.icSigCmyData, + ColorSpace.TYPE_CMY, + ICC_Profile.icSigSpace2CLR, + ColorSpace.TYPE_2CLR, + ICC_Profile.icSigSpace3CLR, + ColorSpace.TYPE_3CLR, + ICC_Profile.icSigSpace4CLR, + ColorSpace.TYPE_4CLR, + ICC_Profile.icSigSpace5CLR, + ColorSpace.TYPE_5CLR, + ICC_Profile.icSigSpace6CLR, + ColorSpace.TYPE_6CLR, + ICC_Profile.icSigSpace7CLR, + ColorSpace.TYPE_7CLR, + ICC_Profile.icSigSpace8CLR, + ColorSpace.TYPE_8CLR, + ICC_Profile.icSigSpace9CLR, + ColorSpace.TYPE_9CLR, + ICC_Profile.icSigSpaceACLR, + ColorSpace.TYPE_ACLR, + ICC_Profile.icSigSpaceBCLR, + ColorSpace.TYPE_BCLR, + ICC_Profile.icSigSpaceCCLR, + ColorSpace.TYPE_CCLR, + ICC_Profile.icSigSpaceDCLR, + ColorSpace.TYPE_DCLR, + ICC_Profile.icSigSpaceECLR, + ColorSpace.TYPE_ECLR, + ICC_Profile.icSigSpaceFCLR, + ColorSpace.TYPE_FCLR + }; + + /** + * Size of an ICC header (128 bytes) + */ + public static final int HEADERSIZE = 128; + + /** + * Mapping of ICC class signatures to profile class constants + */ + private static final int[] classMap = + { + ICC_Profile.icSigInputClass, + ICC_Profile.CLASS_INPUT, + ICC_Profile.icSigDisplayClass, + ICC_Profile.CLASS_DISPLAY, + ICC_Profile.icSigOutputClass, + ICC_Profile.CLASS_OUTPUT, + ICC_Profile.icSigLinkClass, + ICC_Profile.CLASS_DEVICELINK, + ICC_Profile.icSigColorSpaceClass, + ICC_Profile.CLASS_COLORSPACECONVERSION, + ICC_Profile.icSigAbstractClass, + ICC_Profile.CLASS_ABSTRACT, + ICC_Profile.icSigNamedColorClass, + ICC_Profile.CLASS_NAMEDCOLOR + }; + private int size; + private int cmmId; + + // Major/Minor version, The ICC-1998 spec is major v2 + private int majorVersion; + + // Major/Minor version, The ICC-1998 spec is major v2 + private int minorVersion; + private int profileClass; // profile device class + private int colorSpace; // data color space type + private int profileColorSpace; // profile connection space (PCS) type + private byte[] timestamp; // original creation timestamp + private int platform; // platform signature + private int flags; // flags + private int magic; // magic number. + private int manufacturerSig; // manufacturer sig + private int modelSig; // model sig + private byte[] attributes; // Attributes + private int intent; // rendering intent + private byte[] illuminant; // illuminant info (Coordinates of D50 in the PCS) + private int creatorSig; // Creator sig (same type as manufacturer) + + /** + * Creates a 'default' header for use with our predefined profiles. + * Note the device and profile color spaces are not set. + */ + public ProfileHeader() + { + creatorSig = 0; + intent = 0; + modelSig = manufacturerSig = (int) 0x6E6f6E65; // 'none' + magic = icMagicNumber; + cmmId = 0; + platform = 0; // no preferred platform + timestamp = new byte[8]; + majorVersion = 2; + minorVersion = 0x10; + flags = 0; + + // D50 in XYZ format (encoded) + illuminant = new byte[] + { + (byte) 0x00, (byte) 0x00, (byte) 0xf6, (byte) 0xd6, + (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0xd3, (byte) 0x2d + }; + attributes = new byte[8]; + profileClass = ICC_Profile.CLASS_DISPLAY; + } + + /** + * Creates a header from profile data. Only the header portion (128 bytes) + * is read, so the array passed need not be the full profile. + */ + public ProfileHeader(byte[] data) + { + ByteBuffer buf = ByteBuffer.wrap(data); + + // Get size (the sign bit shouldn't matter. + // A valid profile can never be +2Gb) + size = buf.getInt(ICC_Profile.icHdrSize); + + // CMM ID + cmmId = buf.getInt(ICC_Profile.icHdrCmmId); + + // Version number + majorVersion = (int) (data[ICC_Profile.icHdrVersion]); + minorVersion = (int) (data[ICC_Profile.icHdrVersion + 1]); + + // Profile/Device class + int classSig = buf.getInt(ICC_Profile.icHdrDeviceClass); + profileClass = -1; + for (int i = 0; i < classMap.length; i += 2) + if (classMap[i] == classSig) + { + profileClass = classMap[i + 1]; + break; + } + + // get the data color space + int csSig = buf.getInt(ICC_Profile.icHdrColorSpace); + colorSpace = -1; + for (int i = 0; i < csTypeMap.length; i += 2) + if (csTypeMap[i] == csSig) + { + colorSpace = csTypeMap[i + 1]; + break; + } + + // get the profile color space (PCS), must be xyz or lab except + // for device-link-class profiles + int pcsSig = buf.getInt(ICC_Profile.icHdrPcs); + profileColorSpace = -1; + if (profileClass != ICC_Profile.CLASS_DEVICELINK) + { + if (pcsSig == ICC_Profile.icSigXYZData) + profileColorSpace = ColorSpace.TYPE_XYZ; + if (pcsSig == ICC_Profile.icSigLabData) + profileColorSpace = ColorSpace.TYPE_Lab; + } + else + { + for (int i = 0; i < csTypeMap.length; i += 2) + if (csTypeMap[i] == pcsSig) + { + profileColorSpace = csTypeMap[i + 1]; + break; + } + } + + // creation timestamp + timestamp = new byte[8]; + System.arraycopy(data, ICC_Profile.icHdrDate, timestamp, 0, 8); + + // magic number + magic = buf.getInt(ICC_Profile.icHdrMagic); + + // platform info + platform = buf.getInt(ICC_Profile.icHdrPlatform); + // get flags + flags = buf.getInt(ICC_Profile.icHdrFlags); + // get manufacturer sign + manufacturerSig = buf.getInt(ICC_Profile.icHdrManufacturer); + // get header model + modelSig = buf.getInt(ICC_Profile.icHdrModel); + // attributes + attributes = new byte[8]; + System.arraycopy(data, ICC_Profile.icHdrAttributes, attributes, 0, 8); + // rendering intent + intent = buf.getInt(ICC_Profile.icHdrRenderingIntent); + // illuminant info + illuminant = new byte[12]; + System.arraycopy(data, ICC_Profile.icHdrIlluminant, illuminant, 0, 12); + // Creator signature + creatorSig = buf.getInt(ICC_Profile.icHdrCreator); + // The rest of the header (Total size: 128 bytes) is unused.. + } + + /** + * Verify that the header is valid + * @param size equals the file size if it is to be verified, -1 otherwise + * @throws IllegalArgumentException if the header is found to be invalid. + */ + public void verifyHeader(int size) throws IllegalArgumentException + { + // verify size + if (size != -1 && this.size != size) + throw new IllegalArgumentException("Invalid profile length:" + size); + + // Check version number + if (majorVersion != 2) + throw new IllegalArgumentException("Wrong major version number:" + + majorVersion); + + // Profile/Device class + if (profileClass == -1) + throw new IllegalArgumentException("Invalid profile/device class"); + + // get the data color space + if (colorSpace == -1) + throw new IllegalArgumentException("Invalid colorspace"); + + // profile color space + if (profileColorSpace == -1) + throw new IllegalArgumentException("Invalid PCS."); + + // check magic number + if (magic != icMagicNumber) + throw new IllegalArgumentException("Invalid magic number!"); + } + + /** + * Creates a header, setting the header file size at the same time. + * @param size the profile file size. + */ + public byte[] getData(int size) + { + byte[] data = new byte[HEADERSIZE]; + ByteBuffer buf = ByteBuffer.wrap(data); + buf.putInt(ICC_Profile.icHdrSize, size); + buf.putInt(ICC_Profile.icHdrCmmId, cmmId); + buf.putShort(ICC_Profile.icHdrVersion, + (short) (majorVersion << 8 | minorVersion)); + for (int i = 1; i < classMap.length; i += 2) + if (profileClass == classMap[i]) + buf.putInt(ICC_Profile.icHdrDeviceClass, classMap[i - 1]); + for (int i = 1; i < csTypeMap.length; i += 2) + if (csTypeMap[i] == colorSpace) + buf.putInt(ICC_Profile.icHdrColorSpace, csTypeMap[i - 1]); + for (int i = 1; i < csTypeMap.length; i += 2) + if (csTypeMap[i] == profileColorSpace) + buf.putInt(ICC_Profile.icHdrPcs, csTypeMap[i - 1]); + + System.arraycopy(timestamp, 0, data, ICC_Profile.icHdrDate, + timestamp.length); + buf.putInt(ICC_Profile.icHdrMagic, icMagicNumber); + buf.putInt(ICC_Profile.icHdrPlatform, platform); + buf.putInt(ICC_Profile.icHdrFlags, flags); + buf.putInt(ICC_Profile.icHdrManufacturer, manufacturerSig); + buf.putInt(ICC_Profile.icHdrModel, modelSig); + System.arraycopy(attributes, 0, data, ICC_Profile.icHdrAttributes, + attributes.length); + buf.putInt(ICC_Profile.icHdrRenderingIntent, intent); + System.arraycopy(illuminant, 0, data, ICC_Profile.icHdrIlluminant, + illuminant.length); + buf.putInt(ICC_Profile.icHdrCreator, creatorSig); + return buf.array(); + } + + public int getSize() + { + return size; + } + + public void setSize(int s) + { + size = s; + } + + public int getMajorVersion() + { + return majorVersion; + } + + public int getMinorVersion() + { + return minorVersion; + } + + public int getProfileClass() + { + return profileClass; + } + + public void setProfileClass(int pc) + { + profileClass = pc; + } + + public int getColorSpace() + { + return colorSpace; + } + + public int getProfileColorSpace() + { + return profileColorSpace; + } + + public void setColorSpace(int cs) + { + colorSpace = cs; + } + + public void setProfileColorSpace(int pcs) + { + profileColorSpace = pcs; + } + +} diff --git a/libjava/classpath/gnu/java/awt/color/PyccConverter.java b/libjava/classpath/gnu/java/awt/color/PyccConverter.java new file mode 100644 index 00000000000..cd50d8776cd --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/PyccConverter.java @@ -0,0 +1,72 @@ +/* PyccConverter.java -- PhotoYCC conversion class + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * PyccConverter - conversion routines for the PhotoYCC colorspace + * + * Also known as PhotoCD YCC, it is an expansion of the conventional + * YCC color space to also include colors with over 100% white. + * + * XXX FIXME: Not yet implemented, implementation pending. + * + * @author Sven de Marothy + */ +public class PyccConverter implements ColorSpaceConverter +{ + public float[] toRGB(float[] in) + { + return null; + } + + public float[] fromRGB(float[] in) + { + return null; + } + + public float[] toCIEXYZ(float[] in) + { + return null; + } + + public float[] fromCIEXYZ(float[] in) + { + return null; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/RgbProfileConverter.java b/libjava/classpath/gnu/java/awt/color/RgbProfileConverter.java new file mode 100644 index 00000000000..0cbd28f6cca --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/RgbProfileConverter.java @@ -0,0 +1,244 @@ +/* RgbProfileConverter.java -- RGB Profile conversion class + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.color; + +import java.awt.color.ICC_Profile; +import java.awt.color.ICC_ProfileRGB; +import java.awt.color.ProfileDataException; + +/** + * RgbProfileConverter - converts RGB profiles (ICC_ProfileRGB) + * + * This type of profile contains a matrix and three + * tone reproduction curves (TRCs). + * + * Device RGB --> CIE XYZ is done through first multiplying with + * a matrix, then each component is looked-up against it's TRC. + * + * The opposite transform is done using the inverse of the matrix, + * and TRC:s. + * + * @author Sven de Marothy + */ +public class RgbProfileConverter implements ColorSpaceConverter +{ + private float[][] matrix; + private float[][] inv_matrix; + private ToneReproductionCurve rTRC; + private ToneReproductionCurve gTRC; + private ToneReproductionCurve bTRC; + private ColorLookUpTable toPCS; + private ColorLookUpTable fromPCS; + + /** + * CIE 1931 D50 white point (in Lab coordinates) + */ + private static float[] D50 = { 0.96422f, 1.00f, 0.82521f }; + + /** + * Constructs an RgbProfileConverter from a given ICC_ProfileRGB + */ + public RgbProfileConverter(ICC_ProfileRGB profile) + { + toPCS = fromPCS = null; + matrix = profile.getMatrix(); + + // get TRCs + try + { + rTRC = new ToneReproductionCurve(profile.getGamma(ICC_ProfileRGB.REDCOMPONENT)); + } + catch (ProfileDataException e) + { + rTRC = new ToneReproductionCurve(profile.getTRC(ICC_ProfileRGB.REDCOMPONENT)); + } + try + { + gTRC = new ToneReproductionCurve(profile.getGamma(ICC_ProfileRGB.GREENCOMPONENT)); + } + catch (ProfileDataException e) + { + gTRC = new ToneReproductionCurve(profile.getTRC(ICC_ProfileRGB.GREENCOMPONENT)); + } + try + { + bTRC = new ToneReproductionCurve(profile.getGamma(ICC_ProfileRGB.BLUECOMPONENT)); + } + catch (ProfileDataException e) + { + bTRC = new ToneReproductionCurve(profile.getTRC(ICC_ProfileRGB.BLUECOMPONENT)); + } + + // If a CLUT is available, it should be used, and the TRCs ignored. + // Note: A valid profile may only have CLUTs in one direction, and + // TRC:s without useful info, making reverse-transforms impossible. + // In this case the TRC will be used for the reverse-transform with + // unpredictable results. This is in line with the Java specification, + try + { + toPCS = new ColorLookUpTable(profile, ICC_Profile.icSigAToB0Tag); + } + catch (Exception e) + { + toPCS = null; + } + + try + { + fromPCS = new ColorLookUpTable(profile, ICC_Profile.icSigBToA0Tag); + } + catch (Exception e) + { + fromPCS = null; + } + + // Calculate the inverse matrix if no reverse CLUT is available + if(fromPCS == null) + inv_matrix = invertMatrix(matrix); + else + { + // otherwise just set it to an identity matrix + inv_matrix = new float[3][3]; + inv_matrix[0][0] = inv_matrix[1][1] = inv_matrix[2][2] = 1.0f; + } + } + + public float[] toCIEXYZ(float[] in) + { + // CLUT takes precedence + if (toPCS != null) + return toPCS.lookup(in); + + float[] temp = new float[3]; + float[] out = new float[3]; + + // device space --> linear gamma + temp[0] = rTRC.lookup(in[0]); + temp[1] = gTRC.lookup(in[1]); + temp[2] = bTRC.lookup(in[2]); + + // matrix multiplication + out[0] = matrix[0][0] * temp[0] + matrix[0][1] * temp[1] + + matrix[0][2] * temp[2]; + out[1] = matrix[1][0] * temp[0] + matrix[1][1] * temp[1] + + matrix[1][2] * temp[2]; + out[2] = matrix[2][0] * temp[0] + matrix[2][1] * temp[1] + + matrix[2][2] * temp[2]; + + return out; + } + + public float[] toRGB(float[] in) + { + return SrgbConverter.XYZtoRGB(toCIEXYZ(in)); + } + + public float[] fromCIEXYZ(float[] in) + { + if (fromPCS != null) + return fromPCS.lookup(in); + + float[] temp = new float[3]; + float[] out = new float[3]; + + // matrix multiplication + temp[0] = inv_matrix[0][0] * in[0] + inv_matrix[0][1] * in[1] + + inv_matrix[0][2] * in[2]; + temp[1] = inv_matrix[1][0] * in[0] + inv_matrix[1][1] * in[1] + + inv_matrix[1][2] * in[2]; + temp[2] = inv_matrix[2][0] * in[0] + inv_matrix[2][1] * in[1] + + inv_matrix[2][2] * in[2]; + + // device space --> linear gamma + out[0] = rTRC.reverseLookup(temp[0]); + out[1] = gTRC.reverseLookup(temp[1]); + out[2] = bTRC.reverseLookup(temp[2]); + + // FIXME: Sun appears to clip the return values to [0,1] + // I don't believe that is a Good Thing, + // (some colorspaces may allow values outside that range.) + // So we return the actual values here. + return out; + } + + public float[] fromRGB(float[] in) + { + return fromCIEXYZ(SrgbConverter.RGBtoXYZ(in)); + } + + /** + * Inverts a 3x3 matrix, returns the inverse, + * throws an IllegalArgumentException if the matrix is not + * invertible (this shouldn't happen for a valid profile) + */ + private float[][] invertMatrix(float[][] matrix) + { + float[][] out = new float[3][3]; + double determinant = matrix[0][0] * (matrix[1][1] * matrix[2][2] + - matrix[2][1] * matrix[1][2]) + - matrix[0][1] * (matrix[1][0] * matrix[2][2] + - matrix[2][0] * matrix[1][2]) + + matrix[0][2] * (matrix[1][0] * matrix[2][1] + - matrix[2][0] * matrix[1][1]); + + if (determinant == 0.0) + throw new IllegalArgumentException("Can't invert conversion matrix."); + float invdet = (float) (1.0 / determinant); + + out[0][0] = invdet * (matrix[1][1] * matrix[2][2] + - matrix[1][2] * matrix[2][1]); + out[0][1] = invdet * (matrix[0][2] * matrix[2][1] + - matrix[0][1] * matrix[2][2]); + out[0][2] = invdet * (matrix[0][1] * matrix[1][2] + - matrix[0][2] * matrix[1][1]); + out[1][0] = invdet * (matrix[1][2] * matrix[2][0] + - matrix[1][0] * matrix[2][2]); + out[1][1] = invdet * (matrix[0][0] * matrix[2][2] + - matrix[0][2] * matrix[2][0]); + out[1][2] = invdet * (matrix[0][2] * matrix[1][0] + - matrix[0][0] * matrix[1][2]); + out[2][0] = invdet * (matrix[1][0] * matrix[2][1] + - matrix[1][1] * matrix[2][0]); + out[2][1] = invdet * (matrix[0][1] * matrix[2][0] + - matrix[0][0] * matrix[2][1]); + out[2][2] = invdet * (matrix[0][0] * matrix[1][1] + - matrix[0][1] * matrix[1][0]); + return out; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/SrgbConverter.java b/libjava/classpath/gnu/java/awt/color/SrgbConverter.java new file mode 100644 index 00000000000..a30a0db906c --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/SrgbConverter.java @@ -0,0 +1,152 @@ +/* SrgbConverter.java -- sRGB conversion class + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * SrgbConverter - conversion routines for the sRGB colorspace + * sRGB is a standard for RGB colorspaces, adopted by the w3c. + * + * The specification is available at: + * http://www.w3.org/Graphics/Color/sRGB.html + * + * @author Sven de Marothy + */ +/** + * + * Note the matrix numbers used here are NOT identical to those in the + * w3 spec, as those numbers are CIE XYZ relative a D65 white point. + * The CIE XYZ we use is relative a D50 white point, so therefore a + * linear Bradford transform matrix for D65->D50 mapping has been applied. + * (The ICC documents describe this transform) + * + * Linearized Bradford transform: + * 0.8951 0.2664 -0.1614 + * -0.7502 1.7135 0.0367 + * 0.0389 -0.0685 1.0296 + * + * Inverse: + * 0.9870 -0.1471 0.1600 + * 0.4323 0.5184 0.0493 + * -0.00853 0.0400 0.9685 + */ +public class SrgbConverter implements ColorSpaceConverter +{ + public float[] fromCIEXYZ(float[] in) + { + return XYZtoRGB(in); + } + + public float[] toCIEXYZ(float[] in) + { + return RGBtoXYZ(in); + } + + public float[] toRGB(float[] in) + { + float[] out = new float[3]; + System.arraycopy(in, 0, out, 0, 3); + return out; + } + + public float[] fromRGB(float[] in) + { + float[] out = new float[3]; + System.arraycopy(in, 0, out, 0, 3); + return out; + } + + /** + * CIE XYZ (D50 relative) --> sRGB + * + * Static as it's used by other ColorSpaceConverters to + * convert to sRGB if the color space is defined in XYZ. + */ + public static float[] XYZtoRGB(float[] in) + { + float[] temp = new float[3]; + temp[0] = 3.1338f * in[0] - 1.6171f * in[1] - 0.4907f * in[2]; + temp[1] = -0.9785f * in[0] + 1.9160f * in[1] + 0.0334f * in[2]; + temp[2] = 0.0720f * in[0] - 0.2290f * in[1] + 1.4056f * in[2]; + + float[] out = new float[3]; + for (int i = 0; i < 3; i++) + { + if (temp[i] < 0) + temp[i] = 0.0f; + if (temp[i] > 1) + temp[i] = 1.0f; + if (temp[i] <= 0.00304f) + out[i] = temp[i] * 12.92f; + else + out[i] = 1.055f * ((float) Math.exp((1 / 2.4) * Math.log(temp[i]))) + - 0.055f; + } + return out; + } + + /** + * sRGB --> CIE XYZ (D50 relative) + * + * Static as it's used by other ColorSpaceConverters to + * convert to XYZ if the color space is defined in RGB. + */ + public static float[] RGBtoXYZ(float[] in) + { + float[] temp = new float[3]; + float[] out = new float[3]; + for (int i = 0; i < 3; i++) + if (in[i] <= 0.03928f) + temp[i] = in[i] / 12.92f; + else + temp[i] = (float) Math.exp(2.4 * Math.log((in[i] + 0.055) / 1.055)); + + /* + * Note: The numbers which were used to calculate this only had four + * digits of accuracy. So don't be fooled by the number of digits here. + * If someone has more accurate source, feel free to update this. + */ + out[0] = (float) (0.436063750222 * temp[0] + 0.385149601465 * temp[1] + + 0.143086418888 * temp[2]); + out[1] = (float) (0.222450894035 * temp[0] + 0.71692584775 * temp[1] + + 0.060624511256 * temp[2]); + out[2] = (float) (0.0138985186 * temp[0] + 0.097079690112 * temp[1] + + 0.713996045725 * temp[2]); + return out; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/TagEntry.java b/libjava/classpath/gnu/java/awt/color/TagEntry.java new file mode 100644 index 00000000000..a9786468384 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/TagEntry.java @@ -0,0 +1,121 @@ +/* TagEntry.java -- A utility class used for storing the tags in ICC_Profile + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * TagEntry - stores a profile tag. + * These are conveniently stored in a hashtable with the tag signature + * as a key. A legal profile can only have one tag with a given sig, + * so we can conveniently ignore collisions. + * + * @author Sven de Marothy + */ +public class TagEntry +{ + // tag table entry size + public static final int entrySize = 12; + private int signature; + private int size; + private int offset; + private byte[] data; + + public TagEntry(int sig, int offset, int size, byte[] data) + { + this.signature = sig; + this.offset = offset; + this.size = size; + this.data = new byte[size]; + System.arraycopy(data, offset, this.data, 0, size); + } + + public TagEntry(int sig, byte[] data) + { + this.signature = sig; + this.size = data.length; + this.data = new byte[size]; + System.arraycopy(data, offset, this.data, 0, size); + } + + public byte[] getData() + { + byte[] d = new byte[size]; + System.arraycopy(this.data, 0, d, 0, size); + return d; + } + + public String hashKey() + { + return tagHashKey(signature); + } + + public String toString() + { + String s = ""; + s = s + (char) ((byte) ((signature >> 24) & 0xFF)); + s = s + (char) ((byte) ((signature >> 16) & 0xFF)); + s = s + (char) ((byte) ((signature >> 8) & 0xFF)); + s = s + (char) ((byte) (signature & 0xFF)); + return s; + } + + public int getSignature() + { + return signature; + } + + public int getSize() + { + return size; + } + + public int getOffset() + { + return offset; + } + + public void setOffset(int offset) + { + this.offset = offset; + } + + public static String tagHashKey(int sig) + { + return "" + sig; + } +} diff --git a/libjava/classpath/gnu/java/awt/color/ToneReproductionCurve.java b/libjava/classpath/gnu/java/awt/color/ToneReproductionCurve.java new file mode 100644 index 00000000000..a1bccbd4068 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/ToneReproductionCurve.java @@ -0,0 +1,177 @@ +/* ToneReproductionCurve.java -- Representation of an ICC 'curv' type TRC + Copyright (C) 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.color; + + +/** + * ToneReproductionCurve - TRCs are used to describe RGB + * and Grayscale profiles. The TRC is essentially the gamma + * function of the color space. + * + * For example, Apple RGB has a gamma of 1.8, most monitors are ~2.2, + * sRGB is 2.4 with a small linear part near 0. + * Linear spaces are of course 1.0. + * (The exact function is implemented in SrgbConverter) + * + * The ICC specification allows the TRC to be described as a single + * Gamma value, where the function is thus out = in**gamma. + * Alternatively, the gamma function may be represented by a lookup table + * of values, in which case linear interpolation is used. + * + * @author Sven de Marothy + */ +public class ToneReproductionCurve +{ + private float[] trc; + private float gamma; + private float[] reverseTrc; + + /** + * Constructs a TRC from a gamma values + */ + public ToneReproductionCurve(float gamma) + { + trc = null; + reverseTrc = null; + this.gamma = gamma; + } + + /** + * Constructs a TRC from a set of float values + */ + public ToneReproductionCurve(float[] trcValues) + { + trc = new float[trcValues.length]; + System.arraycopy(trcValues, 0, trc, 0, trcValues.length); + setupReverseTrc(); + } + + /** + * Constructs a TRC from a set of short values normalized to + * the 0-65535 range (as in the ICC profile file). + * (Note the values are treated as unsigned) + */ + public ToneReproductionCurve(short[] trcValues) + { + trc = new float[trcValues.length]; + for (int i = 0; i < trcValues.length; i++) + trc[i] = (float) ((int) trcValues[i] & (0xFFFF)) / 65535.0f; + setupReverseTrc(); + } + + /** + * Performs a TRC lookup + */ + public float lookup(float in) + { + float out; + + if (trc == null) + { + if (in == 0f) + return 0.0f; + return (float) Math.exp(gamma * Math.log(in)); + } + else + { + double alpha = in * (trc.length - 1); + int index = (int) Math.floor(alpha); + alpha = alpha - (double) index; + if (index >= trc.length - 1) + return trc[trc.length - 1]; + if (index <= 0) + return trc[0]; + out = (float) (trc[index] * (1.0 - alpha) + trc[index + 1] * alpha); + } + return out; + } + + /** + * Performs an reverse lookup + */ + public float reverseLookup(float in) + { + float out; + + if (trc == null) + { + if (in == 0f) + return 0.0f; + return (float) Math.exp((1.0 / gamma) * Math.log(in)); + } + else + { + double alpha = in * (reverseTrc.length - 1); + int index = (int) Math.floor(alpha); + alpha = alpha - (double) index; + if (index >= reverseTrc.length - 1) + return reverseTrc[reverseTrc.length - 1]; + if (index <= 0) + return reverseTrc[0]; + out = (float) (reverseTrc[index] * (1.0 - alpha) + + reverseTrc[index + 1] * alpha); + } + return out; + } + + /** + * Calculates a reverse-lookup table. + * We use a whopping 10,000 entries.. This is should be more than any + * real-life TRC table (typically around 256-1024) so we won't be losing + * any precision. + * + * This will of course generate completely invalid results if the curve + * is not monotonic and invertable. But what's the alternative? + */ + public void setupReverseTrc() + { + reverseTrc = new float[10000]; + int j = 0; + for (int i = 0; i < 10000; i++) + { + float n = ((float) i) / 10000f; + while (trc[j + 1] < n && j < trc.length - 2) + j++; + + if (j == trc.length - 2) + reverseTrc[i] = trc[trc.length - 1]; + else + reverseTrc[i] = (j + (n - trc[j]) / (trc[j + 1] - trc[j])) / ((float) trc.length); + } + } +} diff --git a/libjava/classpath/gnu/java/awt/color/package.html b/libjava/classpath/gnu/java/awt/color/package.html new file mode 100644 index 00000000000..c4705cce5dd --- /dev/null +++ b/libjava/classpath/gnu/java/awt/color/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.awt.color package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.awt.color</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/awt/doc-files/BitwiseXORComposite-1.png b/libjava/classpath/gnu/java/awt/doc-files/BitwiseXORComposite-1.png Binary files differnew file mode 100644 index 00000000000..588c910dd75 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/doc-files/BitwiseXORComposite-1.png diff --git a/libjava/classpath/gnu/java/awt/image/ImageDecoder.java b/libjava/classpath/gnu/java/awt/image/ImageDecoder.java new file mode 100644 index 00000000000..141c8541794 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/image/ImageDecoder.java @@ -0,0 +1,156 @@ +/* ImageDecoder.java -- + Copyright (C) 1999, 2000, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.image; + +import java.awt.image.ImageConsumer; +import java.awt.image.ImageProducer; +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Vector; + +public abstract class ImageDecoder implements ImageProducer +{ + Vector consumers = new Vector (); + String filename; + URL url; + byte[] data; + int offset; + int length; + InputStream input; + + static + { + // FIXME: there was some broken code here that looked like + // it wanted to rely on this property. I don't have any idea + // what it was intended to do. + // String endian = System.getProperties ().getProperty ("gnu.cpu.endian"); + } + + public ImageDecoder (String filename) + { + this.filename = filename; + } + + public ImageDecoder (URL url) + { + this.url = url; + } + + public ImageDecoder (InputStream is) + { + this.input = is; + } + + public ImageDecoder (byte[] imagedata, int imageoffset, int imagelength) + { + data = imagedata; + offset = imageoffset; + length = imagelength; + } + + public void addConsumer (ImageConsumer ic) + { + consumers.addElement (ic); + } + + public boolean isConsumer (ImageConsumer ic) + { + return consumers.contains (ic); + } + + public void removeConsumer (ImageConsumer ic) + { + consumers.removeElement (ic); + } + + public void startProduction (ImageConsumer ic) + { + if (!isConsumer(ic)) + addConsumer(ic); + + Vector list = (Vector) consumers.clone (); + try + { + // Create the input stream here rather than in the + // ImageDecoder constructors so that exceptions cause + // imageComplete to be called with an appropriate error + // status. + if (input == null) + { + try + { + if (url != null) + input = url.openStream(); + else + { + if (filename != null) + input = new FileInputStream (filename); + else + input = new ByteArrayInputStream (data, offset, length); + } + produce (list, input); + } + finally + { + input = null; + } + } + else + { + produce (list, input); + } + } + catch (Exception e) + { + for (int i = 0; i < list.size (); i++) + { + ImageConsumer ic2 = (ImageConsumer) list.elementAt (i); + ic2.imageComplete (ImageConsumer.IMAGEERROR); + } + } + } + + public void requestTopDownLeftRightResend (ImageConsumer ic) + { + } + + public abstract void produce (Vector v, InputStream is) throws IOException; +} diff --git a/libjava/classpath/gnu/java/awt/image/XBMDecoder.java b/libjava/classpath/gnu/java/awt/image/XBMDecoder.java new file mode 100644 index 00000000000..0793d8ee749 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/image/XBMDecoder.java @@ -0,0 +1,155 @@ +/* XBMDecoder.java -- Decodes X-bitmaps + Copyright (C) 1999, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.image; + +import java.awt.image.ColorModel; +import java.awt.image.ImageConsumer; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; +import java.util.StringTokenizer; +import java.util.Vector; + +public class XBMDecoder extends ImageDecoder +{ + BufferedReader reader; + static final ColorModel cm = ColorModel.getRGBdefault (); + static final int black = 0xff000000; + static final int transparent = 0x00000000; + static final int masktable[] = { 0x01, 0x02, 0x04, 0x08, + 0x10, 0x20, 0x40, 0x80 }; + + public XBMDecoder (String filename) + { + super (filename); + } + + public XBMDecoder (URL url) + { + super (url); + } + + public void produce (Vector v, InputStream is) throws IOException + { + reader = new BufferedReader (new InputStreamReader (is)); + int width = -1, height = -1; + + for (int i = 0; i < 2; i++) + { + String line = reader.readLine (); + StringTokenizer st = new StringTokenizer (line); + + st.nextToken (); // #define + st.nextToken (); // name_[width|height] + if (i == 0) + width = Integer.parseInt (st.nextToken (), 10); + else + height = Integer.parseInt (st.nextToken (), 10); + } + + for (int i = 0; i < v.size (); i++) + { + ImageConsumer ic = (ImageConsumer) v.elementAt (i); + + ic.setDimensions (width, height); + ic.setColorModel (cm); + ic.setHints (ImageConsumer.COMPLETESCANLINES + | ImageConsumer.SINGLEFRAME + | ImageConsumer.SINGLEPASS + | ImageConsumer.TOPDOWNLEFTRIGHT); + } + + /* skip to the byte array */ + while (reader.read () != '{') { } + + /* loop through each scanline */ + for (int line = 0; line < height; line++) + { + int scanline[] = getScanline (reader, width); + + for (int i = 0; i < v.size (); i++) + { + ImageConsumer ic = (ImageConsumer) v.elementAt (i); + ic.setPixels (0, 0 + line, width, 1, cm, scanline, 0, width); + } + } + + /* tell each ImageConsumer that we're finished */ + for (int i = 0; i < v.size (); i++) + { + ImageConsumer ic = (ImageConsumer) v.elementAt (i); + ic.imageComplete (ImageConsumer.STATICIMAGEDONE); + } + } + + public static int[] getScanline (Reader in, int len) throws IOException + { + char byteStr[] = new char[2]; + int scanline[] = new int[len]; + int x = 0; + + while (x < len) + { + int ch = in.read (); + if (ch == '0') + { + in.read (); // 'x' + + byteStr[0] = (char) in.read (); + byteStr[1] = (char) in.read (); + + int byteVal = Integer.parseInt (new String (byteStr), 16); + + for (int i = 0; i < 8; i++, x++) + { + if (x == len) // condition occurs if bitmap is padded + return scanline; + + scanline[x] = ((byteVal & masktable[i]) != 0) ? + black : transparent; + } + } + } + + return scanline; + } +} diff --git a/libjava/classpath/gnu/java/awt/image/package.html b/libjava/classpath/gnu/java/awt/image/package.html new file mode 100644 index 00000000000..8823367eab1 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/image/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.awt.image package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.awt.image</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/awt/package.html b/libjava/classpath/gnu/java/awt/package.html new file mode 100644 index 00000000000..166168510ef --- /dev/null +++ b/libjava/classpath/gnu/java/awt/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.awt package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.awt</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/awt/peer/ClasspathFontPeer.java b/libjava/classpath/gnu/java/awt/peer/ClasspathFontPeer.java new file mode 100644 index 00000000000..78ab3a9de21 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/ClasspathFontPeer.java @@ -0,0 +1,846 @@ +/* ClasspathFontPeer.java -- Font peer used by GNU Classpath. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer; + +import gnu.java.awt.ClasspathToolkit; + +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Toolkit; +import java.awt.font.FontRenderContext; +import java.awt.font.GlyphVector; +import java.awt.font.LineMetrics; +import java.awt.font.TextAttribute; +import java.awt.font.TransformAttribute; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; +import java.awt.peer.FontPeer; +import java.text.AttributedCharacterIterator; +import java.text.CharacterIterator; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +/** + * A peer for fonts that are used inside Classpath. The purpose of + * this interface is to abstract from platform-specific font handling + * in the Classpath implementation of java.awt.Font and related + * classes. + * + * <p><b>State kept by the peer:</b> a peer is generated for each Font + * object in the default implementation. If you wish to share peers between + * fonts, you will need to subclass both ClasspathFontPeer and + * {@link ClasspathToolKit}.</p> + * + * <p><b>Thread Safety:</b> Methods of this interface may be called + * from arbitrary threads at any time. Implementations of the + * <code>ClasspathFontPeer</code> interface are required to perform + * the necessary synchronization.</p> + * + * @see java.awt.Font#getPeer + * @see java.awt.Toolkit#getFontPeer + * + * @author Sascha Brawer (brawer@dandelis.ch) + * @author Graydon Hoare (graydon@redhat.com) + */ +public abstract class ClasspathFontPeer + implements FontPeer +{ + + /*************************************************************************/ + + /* + * Instance Variables + */ + + /** + * The 3 names of this font. all fonts have 3 names, some of which + * may be equal: + * + * logical -- name the font was constructed from + * family -- a designer or brand name (Helvetica) + * face -- specific instance of a design (Helvetica Regular) + * + * @see isLogicalFontName + */ + + protected String logicalName; + protected String familyName; + protected String faceName; + + /** + * The font style, which is a combination (by OR-ing) of the font style + * constants PLAIN, BOLD and ITALIC, in this class. + */ + protected int style; + + /** + * The font point size. A point is 1/72 of an inch. + */ + protected float size; + + /** + * The affine transformation the font is currently subject to. + */ + protected AffineTransform transform; + + protected static ClasspathToolkit tk() + { + return (ClasspathToolkit)(Toolkit.getDefaultToolkit ()); + } + + /* + * Confusingly, a Logical Font is a concept unrelated to + * a Font's Logical Name. + * + * A Logical Font is one of 6 built-in, abstract font types + * which must be supported by any java environment: SansSerif, + * Serif, Monospaced, Dialog, and DialogInput. + * + * A Font's Logical Name is the name the font was constructed + * from. This might be the name of a Logical Font, or it might + * be the name of a Font Face. + */ + + protected static boolean isLogicalFontName(String name) + { + String uname = name.toUpperCase (); + return (uname.equals ("SANSSERIF") || + uname.equals ("SERIF") || + uname.equals ("MONOSPACED") || + uname.equals ("DIALOG") || + uname.equals ("DIALOGINPUT")); + } + + protected static String logicalFontNameToFaceName (String name) + { + String uname = name.toUpperCase (); + if (uname.equals("SANSSERIF")) + return "Helvetica"; + else if (uname.equals ("SERIF")) + return "Times"; + else if (uname.equals ("MONOSPACED")) + return "Courier"; + else if (uname.equals ("DIALOG")) + return "Helvetica"; + else if (uname.equals ("DIALOGINPUT")) + return "Helvetica"; + else + return "Helvetica"; + } + + protected static String faceNameToFamilyName (String name) + { + return name; + } + + public static void copyStyleToAttrs (int style, Map attrs) + { + if ((style & Font.BOLD) == Font.BOLD) + attrs.put (TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); + else + attrs.put (TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR); + + if ((style & Font.ITALIC) == Font.ITALIC) + attrs.put (TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); + else + attrs.put (TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR); + } + + protected static void copyFamilyToAttrs (String fam, Map attrs) + { + if (fam != null) + attrs.put (TextAttribute.FAMILY, fam); + } + + public static void copySizeToAttrs (float size, Map attrs) + { + attrs.put (TextAttribute.SIZE, new Float (size)); + } + + protected static void copyTransformToAttrs (AffineTransform trans, Map attrs) + { + if (trans != null) + attrs.put(TextAttribute.TRANSFORM, new TransformAttribute (trans)); + } + + + protected void setStandardAttributes (String name, String family, int style, + float size, AffineTransform trans) + { + this.logicalName = name; + + if (isLogicalFontName (name)) + this.faceName = logicalFontNameToFaceName (name); + else + this.faceName = name; + + if (family != null) + this.familyName = family; + else + this.familyName = faceNameToFamilyName (faceName); + + this.style = style; + this.size = size; + this.transform = trans; + } + + + protected void setStandardAttributes (String name, Map attribs) + { + String family = this.familyName; + AffineTransform trans = this.transform; + float size = this.size; + int style = this.style; + + if (attribs.containsKey (TextAttribute.FAMILY)) + family = (String) attribs.get (TextAttribute.FAMILY); + + if (name == null) + name = "SansSerif"; + + if (attribs.containsKey (TextAttribute.WEIGHT)) + { + Float weight = (Float) attribs.get (TextAttribute.WEIGHT); + if (weight.floatValue () >= TextAttribute.WEIGHT_BOLD.floatValue ()) + style += Font.BOLD; + } + + if (attribs.containsKey (TextAttribute.POSTURE)) + { + Float posture = (Float) attribs.get (TextAttribute.POSTURE); + if (posture.floatValue () >= TextAttribute.POSTURE_OBLIQUE.floatValue ()) + style += Font.ITALIC; + } + + if (attribs.containsKey (TextAttribute.SIZE)) + { + Float sz = (Float) attribs.get (TextAttribute.SIZE); + size = sz.floatValue (); + + // Pango doesn't accept 0 as a font size. + if (size < 1) + size = 1; + } + else + size = 12; + + if (attribs.containsKey (TextAttribute.TRANSFORM)) + { + TransformAttribute ta = (TransformAttribute) + attribs.get(TextAttribute.TRANSFORM); + trans = ta.getTransform (); + } + + setStandardAttributes (name, family, style, size, trans); + } + + protected void getStandardAttributes (Map attrs) + { + copyFamilyToAttrs (this.familyName, attrs); + copySizeToAttrs (this.size, attrs); + copyStyleToAttrs (this.style, attrs); + copyTransformToAttrs (this.transform, attrs); + } + + + /* Begin public API */ + + public ClasspathFontPeer (String name, Map attrs) + { + setStandardAttributes (name, attrs); + } + + public ClasspathFontPeer (String name, int style, int size) + { + setStandardAttributes (name, (String)null, style, + (float)size, (AffineTransform)null); + } + + /** + * Implementation of {@link Font#getName} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public String getName (Font font) + { + return logicalName; + } + + /** + * Implementation of {@link Font#getFamily()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public String getFamily (Font font) + { + return familyName; + } + + /** + * Implementation of {@link Font#getFamily(Locale)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public String getFamily (Font font, Locale lc) + { + return familyName; + } + + /** + * Implementation of {@link Font#getFontName()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public String getFontName (Font font) + { + return faceName; + } + + /** + * Implementation of {@link Font#getFontName(Locale)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public String getFontName (Font font, Locale lc) + { + return faceName; + } + + /** + * Implementation of {@link Font#getSize} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public float getSize (Font font) + { + return size; + } + + /** + * Implementation of {@link Font#isPlain} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public boolean isPlain (Font font) + { + return style == Font.PLAIN; + } + + /** + * Implementation of {@link Font#isBold} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public boolean isBold (Font font) + { + return ((style & Font.BOLD) == Font.BOLD); + } + + /** + * Implementation of {@link Font#isItalic} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public boolean isItalic (Font font) + { + return ((style & Font.ITALIC) == Font.ITALIC); + } + + /** + * Implementation of {@link Font#deriveFont(int, float)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Font deriveFont (Font font, int style, float size) + { + Map attrs = new HashMap (); + getStandardAttributes (attrs); + copyStyleToAttrs (style, attrs); + copySizeToAttrs (size, attrs); + return tk().getFont (logicalName, attrs); + } + + /** + * Implementation of {@link Font#deriveFont(float)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Font deriveFont (Font font, float size) + { + Map attrs = new HashMap (); + getStandardAttributes (attrs); + copySizeToAttrs (size, attrs); + return tk().getFont (logicalName, attrs); + } + + /** + * Implementation of {@link Font#deriveFont(int)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Font deriveFont (Font font, int style) + { + Map attrs = new HashMap (); + getStandardAttributes (attrs); + copyStyleToAttrs (style, attrs); + return tk().getFont (logicalName, attrs); + } + + /** + * Implementation of {@link Font#deriveFont(int, AffineTransform)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Font deriveFont (Font font, int style, AffineTransform t) + { + Map attrs = new HashMap (); + getStandardAttributes (attrs); + copyStyleToAttrs (style, attrs); + copyTransformToAttrs (t, attrs); + return tk().getFont (logicalName, attrs); + } + + /** + * Implementation of {@link Font#deriveFont(AffineTransform)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Font deriveFont (Font font, AffineTransform t) + { + Map attrs = new HashMap (); + getStandardAttributes (attrs); + copyTransformToAttrs (t, attrs); + return tk().getFont (logicalName, attrs); + } + + /** + * Implementation of {@link Font#deriveFont(Map)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Font deriveFont (Font font, Map attrs) + { + return tk().getFont (logicalName, attrs); + } + + /** + * Implementation of {@link Font#getAttributes()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public Map getAttributes (Font font) + { + HashMap h = new HashMap (); + getStandardAttributes (h); + return h; + } + + /** + * Implementation of {@link Font#getAvailableAttributes()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public AttributedCharacterIterator.Attribute[] getAvailableAttributes(Font font) + { + AttributedCharacterIterator.Attribute a[] = + new AttributedCharacterIterator.Attribute[5]; + a[0] = TextAttribute.FAMILY; + a[1] = TextAttribute.SIZE; + a[2] = TextAttribute.POSTURE; + a[3] = TextAttribute.WEIGHT; + a[4] = TextAttribute.TRANSFORM; + return a; + } + + /** + * Implementation of {@link Font#getTransform()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public AffineTransform getTransform (Font font) + { + if (transform == null) + transform = new AffineTransform (); + return transform; + } + + /** + * Implementation of {@link Font#isTransformed()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public boolean isTransformed (Font font) + { + return ! transform.isIdentity (); + } + + /** + * Implementation of {@link Font#getItalicAngle()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public float getItalicAngle (Font font) + { + if ((style & Font.ITALIC) == Font.ITALIC) + return TextAttribute.POSTURE_OBLIQUE.floatValue (); + else + return TextAttribute.POSTURE_REGULAR.floatValue (); + } + + + /** + * Implementation of {@link Font#getStyle()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public int getStyle (Font font) + { + return style; + } + + + + + /* Remaining methods are abstract */ + + /** + * Implementation of {@link Font#canDisplay(char)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract boolean canDisplay (Font font, char c); + + /** + * Implementation of {@link Font#canDisplay(String)}, + * {@link Font#canDisplay(char [], int, int)}, and + * {@link Font#canDisplay(CharacterIterator, int, int)}. + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract int canDisplayUpTo (Font font, CharacterIterator i, int start, int limit); + + + /** + * Returns the name of this font face inside the family, for example + * <i>“Light”</i>. + * + * <p>This method is currently not used by {@link Font}. However, + * this name would be needed by any serious desktop publishing + * application. + * + * @param font the font whose sub-family name is requested. + * + * @param locale the locale for which to localize the name. If + * <code>locale</code> is <code>null</code>, the returned name is + * localized to the user’s default locale. + * + * @return the name of the face inside its family, or + * <code>null</code> if the font does not provide a sub-family name. + */ + + public abstract String getSubFamilyName (Font font, Locale locale); + + + /** + * Implementation of {@link Font#getPSName()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract String getPostScriptName (Font font); + + + /** + * Implementation of {@link Font#getNumGlyphs()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract int getNumGlyphs (Font font); + + + /** + * Implementation of {@link Font#getMissingGlyphCode()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract int getMissingGlyphCode (Font font); + + + /** + * Implementation of {@link Font#getBaselineFor(char)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract byte getBaselineFor (Font font, char c); + + + /** + * Returns a name for the specified glyph. This is useful for + * generating PostScript or PDF files that embed some glyphs of a + * font. If the implementation follows glyph naming conventions + * specified by Adobe, search engines can extract the original text + * from the generated PostScript and PDF files. + * + * <p>This method is currently not used by GNU Classpath. However, + * it would be very useful for someone wishing to write a good + * PostScript or PDF stream provider for the + * <code>javax.print</code> package. + * + * <p><b>Names are not unique:</b> Under some rare circumstances, + * the same name can be returned for different glyphs. It is + * therefore recommended that printer drivers check whether the same + * name has already been returned for antoher glyph, and make the + * name unique by adding the string ".alt" followed by the glyph + * index.</p> + * + * <p>This situation would occur for an OpenType or TrueType font + * that has a <code>post</code> table of format 3 and provides a + * mapping from glyph IDs to Unicode sequences through a + * <code>Zapf</code> table. If the same sequence of Unicode + * codepoints leads to different glyphs (depending on contextual + * position, for example, or on typographic sophistication level), + * the same name would get synthesized for those glyphs. To avoid + * this, the font peer would have to go through the names of all + * glyphs, which would make this operation very inefficient with + * large fonts. + * + * @param font the font containing the glyph whose name is + * requested. + * + * @param glyphIndex the glyph whose name the caller wants to + * retrieve. + * + * @return the glyph name, or <code>null</code> if a font does not + * provide glyph names. + */ + + public abstract String getGlyphName (Font font, int glyphIndex); + + + /** + * Implementation of {@link + * Font#createGlyphVector(FontRenderContext, String)}, {@link + * Font#createGlyphVector(FontRenderContext, char[])}, and {@link + * Font#createGlyphVector(FontRenderContext, CharacterIterator)}. + * + * @param font the font object that the created GlyphVector will return + * when it gets asked for its font. This argument is needed because the + * public API of {@link GlyphVector} works with {@link java.awt.Font}, + * not with font peers. + */ + + public abstract GlyphVector createGlyphVector (Font font, + FontRenderContext frc, + CharacterIterator ci); + + + /** + * Implementation of {@link Font#createGlyphVector(FontRenderContext, + * int[])}. + * + * @param font the font object that the created GlyphVector will return + * when it gets asked for its font. This argument is needed because the + * public API of {@link GlyphVector} works with {@link java.awt.Font}, + * not with font peers. + */ + + public abstract GlyphVector createGlyphVector (Font font, + FontRenderContext ctx, + int[] glyphCodes); + + + /** + * Implementation of {@link Font#layoutGlyphVector(FontRenderContext, + * char[], int, int, int)}. + * + * @param font the font object that the created GlyphVector will return + * when it gets asked for its font. This argument is needed because the + * public API of {@link GlyphVector} works with {@link java.awt.Font}, + * not with font peers. + */ + + public abstract GlyphVector layoutGlyphVector (Font font, + FontRenderContext frc, + char[] chars, int start, + int limit, int flags); + + + /** + * Implementation of {@link Font#getFontMetrics()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract FontMetrics getFontMetrics (Font font); + + + /** + * Implementation of {@link Font#hasUniformLineMetrics()} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract boolean hasUniformLineMetrics (Font font); + + + /** + * Implementation of {@link Font#getLineMetrics(CharacterIterator, int, + * int, FontRenderContext)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract LineMetrics getLineMetrics (Font font, + CharacterIterator ci, + int begin, int limit, + FontRenderContext rc); + + /** + * Implementation of {@link Font#getMaxCharBounds(FontRenderContext)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract Rectangle2D getMaxCharBounds (Font font, + FontRenderContext rc); + + /** + * Implementation of {@link Font#getStringBounds(CharacterIterator, int, + * int, FontRenderContext)} + * + * @param font the font this peer is being called from. This may be + * useful if you are sharing peers between Font objects. Otherwise it may + * be ignored. + */ + + public abstract Rectangle2D getStringBounds (Font font, + CharacterIterator ci, + int begin, int limit, + FontRenderContext frc); + +} diff --git a/libjava/classpath/gnu/java/awt/peer/ClasspathTextLayoutPeer.java b/libjava/classpath/gnu/java/awt/peer/ClasspathTextLayoutPeer.java new file mode 100644 index 00000000000..70df2ef74ef --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/ClasspathTextLayoutPeer.java @@ -0,0 +1,104 @@ +/* ClasspathTextLayoutPeer.java + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer; + +import java.awt.Graphics2D; +import java.awt.Shape; +import java.awt.font.TextHitInfo; +import java.awt.font.TextLayout; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; + +/** + * @author Graydon Hoare + */ + +public interface ClasspathTextLayoutPeer +{ + TextHitInfo getStrongCaret (TextHitInfo hit1, + TextHitInfo hit2); + + void draw (Graphics2D g2, float x, float y); + + byte getBaseline (); + + boolean isLeftToRight (); + boolean isVertical (); + + float getAdvance (); + float getAscent (); + float getDescent (); + float getLeading (); + + int getCharacterCount (); + byte getCharacterLevel (int index); + + float[] getBaselineOffsets (); + Shape getBlackBoxBounds (int firstEndpoint, int secondEndpoint); + Rectangle2D getBounds (); + + float[] getCaretInfo (TextHitInfo hit, Rectangle2D bounds); + Shape getCaretShape (TextHitInfo hit, Rectangle2D bounds); + Shape[] getCaretShapes (int offset, Rectangle2D bounds, + TextLayout.CaretPolicy policy); + + Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint, + Rectangle2D bounds); + int[] getLogicalRangesForVisualSelection (TextHitInfo firstEndpoint, + TextHitInfo secondEndpoint); + + TextHitInfo getNextLeftHit (int offset, TextLayout.CaretPolicy policy); + TextHitInfo getNextRightHit (int offset, TextLayout.CaretPolicy policy); + TextHitInfo hitTestChar (float x, float y, Rectangle2D bounds); + TextHitInfo getVisualOtherHit (TextHitInfo hit); + + float getVisibleAdvance (); + Shape getOutline (AffineTransform tx); + Shape getVisualHighlightShape (TextHitInfo firstEndpoint, + TextHitInfo secondEndpoint, + Rectangle2D bounds); + + TextLayout getJustifiedLayout (float justificationWidth); + void handleJustify (float justificationWidth); + + Object clone (); + int hashCode (); + boolean equals (ClasspathTextLayoutPeer tl); + String toString (); +} diff --git a/libjava/classpath/gnu/java/awt/peer/EmbeddedWindowPeer.java b/libjava/classpath/gnu/java/awt/peer/EmbeddedWindowPeer.java new file mode 100644 index 00000000000..4c64a1d2d18 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/EmbeddedWindowPeer.java @@ -0,0 +1,47 @@ +/* EmbeddedWindowPeer.java -- Interface for window peers that may be + embedded into other applications + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer; + +import java.awt.peer.FramePeer; + +public interface EmbeddedWindowPeer extends FramePeer +{ + void embed (long handle); +} diff --git a/libjava/classpath/gnu/java/awt/peer/GLightweightPeer.java b/libjava/classpath/gnu/java/awt/peer/GLightweightPeer.java new file mode 100644 index 00000000000..3bcaebcea62 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/GLightweightPeer.java @@ -0,0 +1,298 @@ +/* GLightweightPeer.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer; + +import java.awt.AWTEvent; +import java.awt.AWTException; +import java.awt.BufferCapabilities; +import java.awt.Color; +import java.awt.Component; +import java.awt.Cursor; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.GraphicsConfiguration; +import java.awt.Image; +import java.awt.Insets; +import java.awt.Point; +import java.awt.Toolkit; +import java.awt.event.PaintEvent; +import java.awt.image.ColorModel; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; +import java.awt.image.VolatileImage; +import java.awt.peer.ContainerPeer; +import java.awt.peer.LightweightPeer; + +/* + * Another possible implementation strategy for lightweight peers is + * to make GLightweightPeer a placeholder class that implements + * LightweightPeer. Then the Component and Container classes could + * identify a peer as lightweight and handle it specially. The + * current approach is probably more clear but less efficient. + */ + +/** + * A stub class that implements the ComponentPeer and ContainerPeer + * interfaces using callbacks into the Component and Container + * classes. GLightweightPeer allows the Component and Container + * classes to treat lightweight and heavyweight peers in the same way. + * + * Lightweight components are painted directly onto their parent + * containers through an Image object provided by the toolkit. + */ +public class GLightweightPeer + implements LightweightPeer, ContainerPeer +{ + private Component comp; + + private Insets containerInsets; + + public GLightweightPeer(Component comp) + { + this.comp = comp; + } + + // -------- java.awt.peer.ContainerPeer implementation: + + public Insets insets() + { + return getInsets (); + } + + public Insets getInsets() + { + if (containerInsets == null) + containerInsets = new Insets (0,0,0,0); + return containerInsets; + } + + public void beginValidate() + { + } + + public void endValidate() + { + } + + public void beginLayout() + { + } + + public void endLayout() + { + } + + public boolean isPaintPending() + { + return false; + } + + // -------- java.awt.peer.ComponentPeer implementation: + + public int checkImage(Image img, int width, int height, ImageObserver o) + { + return comp.getToolkit().checkImage(img, width, height, o); + } + + public Image createImage(ImageProducer prod) + { + return comp.getToolkit().createImage(prod); + } + + /* This method is not called. */ + public Image createImage(int width, int height) + { + return null; + } + + public void disable() {} + + public void dispose() {} + + public void enable() {} + + public GraphicsConfiguration getGraphicsConfiguration() + { + return null; + } + + public FontMetrics getFontMetrics(Font f) + { + return comp.getToolkit().getFontMetrics(f); + } + + /* Returning null here tells the Component object that called us to + * use its parent's Graphics. */ + public Graphics getGraphics() + { + return null; + } + + public Point getLocationOnScreen() + { + Point parentLocation = comp.getParent().getLocationOnScreen(); + return new Point (parentLocation.x + comp.getX(), + parentLocation.y + comp.getY()); + } + + public Dimension getMinimumSize() + { + return new Dimension(comp.getWidth(), comp.getHeight()); + } + + /* A lightweight component's preferred size is equivalent to its + * Component width and height values. */ + public Dimension getPreferredSize() + { + return new Dimension(comp.getWidth(), comp.getHeight()); + } + + /* Returning null here tells the Component object that called us to + * use its parent's Toolkit. */ + public Toolkit getToolkit() + { + return null; + } + + public void handleEvent(AWTEvent e) {} + + public void hide() {} + + public boolean isFocusable() + { + return false; + } + + public boolean isFocusTraversable() + { + return false; + } + + public Dimension minimumSize() + { + return getMinimumSize(); + } + + public Dimension preferredSize() + { + return getPreferredSize(); + } + + public void paint(Graphics graphics) {} + + public boolean prepareImage(Image img, int width, int height, + ImageObserver o) + { + return comp.getToolkit().prepareImage(img, width, height, o); + } + + public void print(Graphics graphics) {} + + public void repaint(long tm, int x, int y, int width, int height) {} + + public void requestFocus() {} + + public boolean requestFocus(Component source, boolean bool1, boolean bool2, long x) + { + return false; + } + + public void reshape(int x, int y, int width, int height) {} + + public void setBackground(Color color) {} + + public void setBounds(int x, int y, int width, int height) {} + + public void setCursor(Cursor cursor) {} + + public void setEnabled(boolean enabled) {} + + public void setEventMask(long eventMask) {} + + public void setFont(Font font) {} + + public void setForeground(Color color) {} + + public void setVisible(boolean visible) {} + + public void show() {} + + public ColorModel getColorModel () + { + return comp.getColorModel (); + } + + public boolean isObscured() + { + return false; + } + + public boolean canDetermineObscurity() + { + return false; + } + + public void coalescePaintEvent(PaintEvent e) { } + + public void updateCursorImmediately() { } + + public VolatileImage createVolatileImage(int width, int height) + { + return null; + } + + public boolean handlesWheelScrolling() + { + return false; + } + + public void createBuffers(int x, BufferCapabilities capabilities) + throws AWTException { } + + public Image getBackBuffer() + { + return null; + } + + public void flip(BufferCapabilities.FlipContents contents) { } + + public void destroyBuffers() { } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GThreadMutex.java b/libjava/classpath/gnu/java/awt/peer/gtk/GThreadMutex.java new file mode 100644 index 00000000000..e73df9e5509 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GThreadMutex.java @@ -0,0 +1,109 @@ +/* GThreadMutex.java -- Implements a mutex object for glib's gthread + abstraction, for use with GNU Classpath's --portable-native-sync option. + This is used in gthread-jni.c + + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.peer.gtk; + +/** Implements a mutex object for glib's gthread + abstraction, for use with GNU Classpath's --portable-native-sync option. + This is used in gthread-jni.c. + + We use this object to implement the POSIX semantics for Mutexes. They are + needed are needed for the function vector that is passed to glib's + g_thread subpackage's initialization function. + + The GThreadMutex object itself serves as the Real Lock; if code has + entered the monitor for this GThreadMutex object (in Java language, if + it's synchronized on this object) then it holds the lock that this object + represents. + + @author Steven Augart + May, 2004 + + +*/ + +class GThreadMutex +{ + /** Might "lock" be locked? Is anyone waiting + to get that lock? How long is the queue? + + If zero, nobody holds a lock on this GThreadMutex object, and nobody is + trying to get one. Before someone attempts to acquire a lock on this + object, they must increment potentialLockers. After they release their + lock on this object, they must decrement potentialLockers. + + Access to this field is guarded by synchronizing on the object + <code>lockForPotentialLockers</code>. + + After construction, we only access this field via JNI. + */ + volatile int potentialLockers; + + /** An object to synchronize to if you want to examine or modify the + <code>potentialLockers</code> field. Only hold this lock for brief + moments, just long enough to check or set the value of + <code>lockForPotentialLockers</code>. + + We use this representation so that g_thread_mutex_trylock() will work + with the POSIX semantics. This is the only case in which you ever hold a + lock on <code>lockForPotentialLockers</code> while trying to get another + lock -- if you are the mutex_trylock() implementation, and you have just + checked that <code>potentialLockers</code> has the value zero. In that + case, mutex_trylock() holds the lock on lockForPotentialLockers so that + another thread calling mutex_trylock() or mutex_lock() won't increment + potentialLockers after we've checked it and before we've gained the lock + on the POSIX mutex. Of course, in that case the operation of gaining + the POSIX lock itself will succeed immediately, and once it has + succeeded, trylock releases lockForPotentialLockers right away, + incremented to 1 (one). + + After construction, we only access this field via JNI. + */ + Object lockForPotentialLockers; + + GThreadMutex() + { + potentialLockers = 0; + lockForPotentialLockers = new Object(); + } +} +// Local Variables: +// c-file-style: "gnu" +// End: diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GThreadNativeMethodRunner.java b/libjava/classpath/gnu/java/awt/peer/gtk/GThreadNativeMethodRunner.java new file mode 100644 index 00000000000..9a1b8e3a30a --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GThreadNativeMethodRunner.java @@ -0,0 +1,303 @@ +/* GThreadNativeMethodRunner.java -- Implements pthread_create(), under + glib's gthread abstraction, for use with GNU Classpath's + --portable-native-sync option. + This is used by gthread-jni.c + + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.peer.gtk; + +import java.lang.ref.WeakReference; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** Implements pthread_create(), under glib's gthread abstraction, for use + with GNU Classpath's --portable-native-sync option. This is used in + gthread-jni.c + + Also implements a registry for threads, mapping Thread objects to small + integers. The registry uses weak references for threads that aren't + joinable, so that they will be garbage collected. + + There are a number of possible alternative implementations. + + + The rest of this comment consists of an answer to a question that was + raised on the commit-classpath mailing list: + + Mark Wielaard wrote: + + > Can't we assume that jobject and gpointer are both (void *) so we don't + > need the int <-> Thread (global jobject ref) mapping? + > Maybe there are platforms where jobject and gpointer aren't the same, + > but I guess that is pretty unlikely. + + + I agree with you on the pointer size issues. A gpointer is a void *, so + it's certainly guaranteed to be at least as large as any other + pointer. And a jobject is implicitly an opaque pointer (in Jikes RVM, we + use small integers, but we coerce them into the representation of a + pointer). + + The int <==> Thread mapping addresses a different issue. I realize that I + did not document this properly (two and a half lines in thread_create), + and the point is subtle (at least to me; took me a while to figure out). + + The int => Thread mapping always returns jobjects that are local + references, not global ones. This is because Thread objects need to be + able to go away and be garbage collected after the thread they refer to + has died. + + If we keep a global object reference to a thread, then when do we delete + that global object reference? We have an answer in the case of GThread + objects that were explicitly created with the joinable attribute. It is + safe for us to maintain a global reference to any joinable thread, since + the joinable thread must linger (even if only in a zombie state) + until it's explicitly joined via a g_thread_join() call. The global ref + could be cleaned up at that point too. + + However, in the case of GThreads that were created non-joinable by + g_thread_create(), and in the case of Java threads that were created + within pure Java code (not via g_thread_create()), we don't want them to + linger forever, and there is no way to tell when the last reference + to such threads needs to expire. In the case of this application -- AWT + with GTK peers -- it would probably be safe anyway, since there are not + very many threads we create, but I was going for correctness even in the + case of long-running programs that might set up and tear down AWT + interfaces many times. + + So, I duplicated the POSIX thread-ID semantics. The thread ID of a + non-joinable thread remains valid as long as that thread is still alive. + Once that thread dies, the old thread ID may be reused at any moment. And + that's why the array indexed by thread ID numbers is an array of weak + references. + + That's also why the int => Thread jobject mapping function always returns + local references, since global references would lock the Thread in memory + forever. + + I would dearly love there to be a cleaner solution. I dislike the + repeated dips from C code into Java that are necessary to look up thread + ID numbers. If anyone can think of one, I'm all ears. +*/ + +class GThreadNativeMethodRunner + extends Thread +{ + /** The C function pointer that was passed to g_thread_create(). + Specifically, this the numeric address of an object of + C type "void *(*funcPtr)(void *funcArg)". + */ + private final long funcPtr; + + /** The argument for the function "funcPtr(funcArg)". */ + private final long funcArg; + + GThreadNativeMethodRunner(long funcPtr, long funcArg, boolean joinable) + { + this.funcPtr = funcPtr; + this.funcArg = funcArg; + + if (joinable) + registerSelfJoinable(); + } + + public void run() + { + nativeRun(funcPtr, funcArg); + } + + private native void nativeRun(long funcPtr, long funcArg); + + /** THREADS is an array of threads, indexed by thread ID codes. Not sure + whether this is the "best" approach but it does make it O(1) to look up a + thread by its ID. + + Zero is a valid thread ID code. Any negative number is invalid. + + Possible future fixes (TODO?) + + - The THREADS array will only grow. probably not a problem. + But we could keep count when nulling entries and shrink when we have + lots of nulls at the end. Probably not worth it. --mjw + + - Could make this a set of Object; see the comment on "joinable" below. + + The initial size of 17 is just a starting point. Any number will do, + including zero. + */ + private static WeakReference[] threads = new WeakReference[17]; + + /** Used by threadToThreadID, below. Returns the registration number of + the newly-registered thread. + */ + private static synchronized int registerThread(Thread t) + { + int i; + + for (i = 0; i < threads.length; ++i) + { + WeakReference ref = threads[i]; + if (ref == null) + break; // found an empty spot. + } + + if (i == threads.length) + { + /* expand the array */ + WeakReference[] bigger = new WeakReference[threads.length * 2]; + System.arraycopy(threads, 0, bigger, 0, threads.length); + threads = bigger; + } + + threads[i] = new WeakReference(t); + + return i; + } + + /** Look up the Thread ID # for a Thread. Assign a Thread ID # if none + exists. This is a general routine for handling all threads, including + the VM's main thread, if appropriate. + + + Runs in O(n/2) time. + + We can't just issue a threadID upon thread creation. If we were to do + that, not all threads would have a threadID, because not all threads + are launched by GThreadNativeMethodRunner. + */ + static synchronized int threadToThreadID(Thread t) + { + for (int i = 0; i < threads.length; ++i ) + { + if (threads[i] == null) + continue; + Thread referent = (Thread) threads[i].get(); + if (referent == null) + { + threads[i] = null; // Purge the dead WeakReference. + continue; + } + if (referent.equals(t)) + return i; + } // for() + + /* No match found. */ + return registerThread(t); + } + + /** @param threadID Must be a non-negative integer. + + Used to return null if the thread number was out of range or if + the thread was unregistered. Now we throw an exception. + + Possible Alternative Interface: We could go back to returning null in + some sort of check-free mode, so code that calls this function must + be prepared to get null. + */ + static Thread threadIDToThread(int threadID) + throws IllegalArgumentException + { + if (threadID < 0) + throw new IllegalArgumentException("Received a negative threadID, " + + threadID); + if (threadID >= threads.length) + throw new IllegalArgumentException("Received a threadID (" + threadID + + ") higher than was" + + " ever issued"); + + /* Note: if the user is using a stale reference, things will just + break. We might end up getting a different thread than the one + expected. + + TODO: Add an error-checking mode where the user's problems with threads + are announced. For instance, if the user asks for the thread + associated with a threadID that was never issued, we could print a + warning or even abort. + + TODO: Consider optionally disabling all of the error-checking we + already have; it probably slows down the implementation. We could + just return NULL. This is just the reverse of the above TODO item. + */ + + WeakReference threadRef = threads[threadID]; + + if (threadRef == null) + throw new IllegalArgumentException("Asked to look up a stale or unissued" + + "threadID (" + threadID + ")" ); + + + Thread referent = (Thread) threadRef.get(); + if (referent == null) + throw new IllegalArgumentException ("Asked to look up a stale threadID (" + + threadID + ")"); + return referent; + } + + /** Joinable threads need a hard reference, so that they won't go away when + they die. That is because their thread IDs need to stay valid until the + thread is joined via thread_join(threadID). Joinable threads have to be + explicitly joined before they are allowed to go away completely. + + Possible Alternative Implementation: Eliminate the Joinable set. When + calling getThreadIDFromThread() you know whether or not the thread + is joinable. So just store the Thread itself in the threads array? + Make that array an Object array and check with instanceof. This + looks cleaner and more robust to me and it saves a native -> Java + call. But instanceof might be expensive. --mjw + */ + private static final Set joinable = + Collections.synchronizedSet(new HashSet()); + + /** Only called from the constructor. */ + private void registerSelfJoinable() + { + joinable.add(this); + } + + /** This method is only called from JNI, and only after we have succeeded in + a thread_join() operation. */ + static void deRegisterJoinable(Thread thread) + { + joinable.remove(thread); + } +} + +// Local Variables: +// c-file-style: "gnu" +// End: diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkFontMetrics.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkFontMetrics.java new file mode 100644 index 00000000000..7a439e83a9d --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkFontMetrics.java @@ -0,0 +1,134 @@ +/* GdkFontMetrics.java + Copyright (C) 1999, 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.java.awt.ClasspathToolkit; + +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Toolkit; + +public class GdkFontMetrics extends FontMetrics +{ + + private int[] font_metrics; + GdkFontPeer peer; + + static final int FONT_METRICS_ASCENT = 0; + static final int FONT_METRICS_MAX_ASCENT = 1; + static final int FONT_METRICS_DESCENT = 2; + static final int FONT_METRICS_MAX_DESCENT = 3; + static final int FONT_METRICS_MAX_ADVANCE = 4; + + static final int TEXT_METRICS_X_BEARING = 0; + static final int TEXT_METRICS_Y_BEARING = 1; + static final int TEXT_METRICS_WIDTH = 2; + static final int TEXT_METRICS_HEIGHT = 3; + static final int TEXT_METRICS_X_ADVANCE = 4; + static final int TEXT_METRICS_Y_ADVANCE = 5; + + + public GdkFontMetrics (Font font) + { + super (font.getPeer() instanceof GdkFontPeer + ? font + : ((ClasspathToolkit)(Toolkit.getDefaultToolkit ())) + .getFont (font.getName(), font.getAttributes ())); + + peer = (GdkFontPeer) this.font.getPeer(); + + font_metrics = new int[5]; + double [] hires = new double[5]; + peer.getFontMetrics (hires); + for (int i = 0; i < 5; ++i) + font_metrics[i] = (int) hires[i]; + } + + public int stringWidth (String str) + { + double [] hires = new double[6]; + peer.getTextMetrics(str, hires); + return (int) hires [TEXT_METRICS_WIDTH]; + } + + public int charWidth (char ch) + { + return stringWidth (new String (new char[] { ch })); + } + + public int charsWidth (char data[], int off, int len) + { + return stringWidth (new String (data, off, len)); + } + + /* + Sun's Motif implementation always returns 0 or 1 here (???), but + going by the X11 man pages, it seems as though we should return + font.ascent + font.descent. + */ + public int getLeading () + { + return 1; + } + + public int getAscent () + { + return font_metrics[FONT_METRICS_ASCENT]; + } + + public int getMaxAscent () + { + return font_metrics[FONT_METRICS_MAX_ASCENT]; + } + + public int getDescent () + { + return font_metrics[FONT_METRICS_DESCENT]; + } + + public int getMaxDescent () + { + return font_metrics[FONT_METRICS_MAX_DESCENT]; + } + + public int getMaxAdvance () + { + return font_metrics[FONT_METRICS_MAX_ADVANCE]; + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkFontPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkFontPeer.java new file mode 100644 index 00000000000..c6d42b37276 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkFontPeer.java @@ -0,0 +1,307 @@ +/* GdkFontPeer.java -- Implements FontPeer with GTK+ + Copyright (C) 1999, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.classpath.Configuration; +import gnu.java.awt.peer.ClasspathFontPeer; + +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.font.FontRenderContext; +import java.awt.font.GlyphVector; +import java.awt.font.LineMetrics; +import java.awt.geom.Rectangle2D; +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; +import java.util.Locale; +import java.util.Map; +import java.util.ResourceBundle; + +public class GdkFontPeer extends ClasspathFontPeer +{ + static native void initStaticState(); + private final int native_state = GtkGenericPeer.getUniqueInteger (); + private static ResourceBundle bundle; + + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("gtkpeer"); + } + + initStaticState (); + + try + { + bundle = ResourceBundle.getBundle ("gnu.java.awt.peer.gtk.font"); + } + catch (Throwable ignored) + { + bundle = null; + } + } + + private native void initState (); + private native void dispose (); + private native void setFont (String family, int style, int size, boolean useGraphics2D); + + native void getFontMetrics(double [] metrics); + native void getTextMetrics(String str, double [] metrics); + + protected void finalize () + { + if (GtkToolkit.useGraphics2D ()) + GdkGraphics2D.releasePeerGraphicsResource(this); + dispose (); + } + + /* + * Helpers for the 3-way overloading that this class seems to suffer + * from. Remove them if you feel like they're a performance bottleneck, + * for the time being I prefer my code not be written and debugged in + * triplicate. + */ + + private String buildString(CharacterIterator iter) + { + StringBuffer sb = new StringBuffer(); + for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) + sb.append(c); + return sb.toString(); + } + + private String buildString(CharacterIterator iter, int begin, int limit) + { + StringBuffer sb = new StringBuffer(); + int i = 0; + for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next(), i++) + { + if (begin <= i) + sb.append(c); + if (limit <= i) + break; + } + return sb.toString(); + } + + private String buildString(char[] chars, int begin, int limit) + { + return new String(chars, begin, limit - begin); + } + + /* Public API */ + + public GdkFontPeer (String name, int style) + { + // All fonts get a default size of 12 if size is not specified. + this(name, style, 12); + } + + public GdkFontPeer (String name, int style, int size) + { + super(name, style, size); + initState (); + setFont (this.familyName, this.style, (int)this.size, + GtkToolkit.useGraphics2D()); + } + + public GdkFontPeer (String name, Map attributes) + { + super(name, attributes); + initState (); + setFont (this.familyName, this.style, (int)this.size, + GtkToolkit.useGraphics2D()); + } + + public String getSubFamilyName(Font font, Locale locale) + { + return null; + } + + public String getPostScriptName(Font font) + { + return null; + } + + public boolean canDisplay (Font font, char c) + { + // FIXME: inquire with pango + return true; + } + + public int canDisplayUpTo (Font font, CharacterIterator i, int start, int limit) + { + // FIXME: inquire with pango + return -1; + } + + private native GdkGlyphVector getGlyphVector(String txt, + Font f, + FontRenderContext ctx); + + public GlyphVector createGlyphVector (Font font, + FontRenderContext ctx, + CharacterIterator i) + { + return getGlyphVector(buildString (i), font, ctx); + } + + public GlyphVector createGlyphVector (Font font, + FontRenderContext ctx, + int[] glyphCodes) + { + return null; + // return new GdkGlyphVector (font, this, ctx, glyphCodes); + } + + public byte getBaselineFor (Font font, char c) + { + throw new UnsupportedOperationException (); + } + + protected class GdkFontLineMetrics extends LineMetrics + { + FontMetrics fm; + int nchars; + + public GdkFontLineMetrics (FontMetrics m, int n) + { + fm = m; + nchars = n; + } + + public float getAscent() + { + return (float) fm.getAscent (); + } + + public int getBaselineIndex() + { + return Font.ROMAN_BASELINE; + } + + public float[] getBaselineOffsets() + { + return new float[3]; + } + + public float getDescent() + { + return (float) fm.getDescent (); + } + + public float getHeight() + { + return (float) fm.getHeight (); + } + + public float getLeading() { return 0.f; } + public int getNumChars() { return nchars; } + public float getStrikethroughOffset() { return 0.f; } + public float getStrikethroughThickness() { return 0.f; } + public float getUnderlineOffset() { return 0.f; } + public float getUnderlineThickness() { return 0.f; } + + } + + public LineMetrics getLineMetrics (Font font, CharacterIterator ci, + int begin, int limit, FontRenderContext rc) + { + return new GdkFontLineMetrics (getFontMetrics (font), limit - begin); + } + + public Rectangle2D getMaxCharBounds (Font font, FontRenderContext rc) + { + throw new UnsupportedOperationException (); + } + + public int getMissingGlyphCode (Font font) + { + throw new UnsupportedOperationException (); + } + + public String getGlyphName (Font font, int glyphIndex) + { + throw new UnsupportedOperationException (); + } + + public int getNumGlyphs (Font font) + { + throw new UnsupportedOperationException (); + } + + public Rectangle2D getStringBounds (Font font, CharacterIterator ci, + int begin, int limit, FontRenderContext frc) + { + GdkGlyphVector gv = getGlyphVector(buildString (ci, begin, limit), font, frc); + return gv.getVisualBounds(); + } + + public boolean hasUniformLineMetrics (Font font) + { + return true; + } + + public GlyphVector layoutGlyphVector (Font font, FontRenderContext frc, + char[] chars, int start, int limit, + int flags) + { + int nchars = (limit - start) + 1; + char[] nc = new char[nchars]; + + for (int i = 0; i < nchars; ++i) + nc[i] = chars[start + i]; + + return createGlyphVector (font, frc, + new StringCharacterIterator (new String (nc))); + } + + public LineMetrics getLineMetrics (Font font, String str, + FontRenderContext frc) + { + return new GdkFontLineMetrics (getFontMetrics (font), str.length ()); + } + + public FontMetrics getFontMetrics (Font font) + { + return new GdkFontMetrics (font); + } + +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGlyphVector.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGlyphVector.java new file mode 100644 index 00000000000..f0ddea43a12 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGlyphVector.java @@ -0,0 +1,359 @@ +/* GdkGlyphVector.java -- Glyph vector object + Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Font; +import java.awt.Rectangle; +import java.awt.Shape; +import java.awt.font.FontRenderContext; +import java.awt.font.GlyphJustificationInfo; +import java.awt.font.GlyphMetrics; +import java.awt.font.GlyphVector; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; + +public class GdkGlyphVector extends GlyphVector +{ + + /* We use a simple representation for glyph vectors here. Glyph i + * consumes 8 doubles: + * + * logical x: extents[ 10*i ] + * logical y: extents[ 10*i + 1 ] + * logical width: extents[ 10*i + 2 ] + * logical height: extents[ 10*i + 3 ] + * + * visual x: extents[ 10*i + 4 ] + * visual y: extents[ 10*i + 5 ] + * visual width: extents[ 10*i + 6 ] + * visual height: extents[ 10*i + 7 ] + * + * origin pos x: extents[ 10*i + 8 ] + * origin pos y: extents[ 10*i + 9 ] + * + * as well as one int, code[i], representing the glyph code in the font. + */ + + double [] extents; + int [] codes; + + Font font; + FontRenderContext fontRenderContext; + + Rectangle2D allLogical; + Rectangle2D allVisual; + + public GdkGlyphVector(double[] extents, int[] codes, Font font, FontRenderContext frc) + { + this.extents = extents; + this.codes = codes; + this.font = font; + this.fontRenderContext = frc; + + allLogical = new Rectangle2D.Double(); + allVisual = new Rectangle2D.Double(); + + for (int i = 0; i < codes.length; ++i) + { + allLogical.add (new Rectangle2D.Double(extents[10*i ] + extents[10*i + 8], + extents[10*i + 1] + extents[10*i + 9], + extents[10*i + 2], + extents[10*i + 3])); + + allVisual.add (new Rectangle2D.Double(extents[10*i + 4] + extents[10*i + 8], + extents[10*i + 5] + extents[10*i + 9], + extents[10*i + 6], + extents[10*i + 7])); + } + } + + /* + geometric notes: + + the FRC contains a mapping from points -> pixels. + + typographics points are typically 1/72 of an inch. + + pixel displays are often around 72 dpi. + + so the FRC can get away with using an identity transform on a screen, + often. behavior is documented by sun to fall back to an identity + transform if the internal transformation is null. + + coordinates coming up from pango are expressed as floats -- in device + space, so basically pixels-with-fractional-bits -- derived from their + storage format in pango (1024ths of pixels). + + it is not clear from the javadocs whether the results of methods like + getGlyphPositions ought to return coordinates in device space, or + "point" space, or what. for now I'm returning them in device space. + + */ + + public double[] getExtents() + { + return extents; + } + + public int[] getCodes() + { + return codes; + } + + public Font getFont () + { + return font; + } + + public FontRenderContext getFontRenderContext () + { + return fontRenderContext; + } + + public int getGlyphCharIndex (int glyphIndex) + { + // FIXME: currently pango does not provide glyph-by-glyph + // reverse mapping information, so we assume a broken 1:1 + // glyph model here. This is plainly wrong. + return glyphIndex; + } + + public int[] getGlyphCharIndices (int beginGlyphIndex, + int numEntries, + int[] codeReturn) + { + int ix[] = codeReturn; + if (ix == null) + ix = new int[numEntries]; + + for (int i = 0; i < numEntries; i++) + ix[i] = getGlyphCharIndex (beginGlyphIndex + i); + return ix; + } + + public int getGlyphCode (int glyphIndex) + { + return codes[glyphIndex]; + } + + public int[] getGlyphCodes (int beginGlyphIndex, int numEntries, + int[] codeReturn) + { + if (codeReturn == null) + codeReturn = new int[numEntries]; + + System.arraycopy(codes, beginGlyphIndex, codeReturn, 0, numEntries); + return codeReturn; + } + + public Shape getGlyphLogicalBounds (int i) + { + return new Rectangle2D.Double (extents[8*i], extents[8*i + 1], + extents[8*i + 2], extents[8*i + 3]); + } + + public GlyphMetrics getGlyphMetrics (int i) + { + // FIXME: pango does not yield vertical layout information at the + // moment. + + boolean is_horizontal = true; + double advanceX = extents[8*i + 2]; // "logical width" == advanceX + double advanceY = 0; + + return new GlyphMetrics (is_horizontal, + (float) advanceX, (float) advanceY, + (Rectangle2D) getGlyphVisualBounds(i), + GlyphMetrics.STANDARD); + } + + public Shape getGlyphOutline (int glyphIndex) + { + throw new UnsupportedOperationException (); + } + + public Shape getGlyphOutline (int glyphIndex, float x, float y) + { + throw new UnsupportedOperationException (); + } + + public Rectangle getGlyphPixelBounds (int i, + FontRenderContext renderFRC, + float x, float y) + { + return new Rectangle((int) x, (int) y, + (int) extents[8*i + 6], (int) extents[8*i + 7]); + } + + public Point2D getGlyphPosition (int i) + { + return new Point2D.Double (extents[10*i + 8], + extents[10*i + 9]); + } + + public float[] getGlyphPositions (int beginGlyphIndex, + int numEntries, + float[] positionReturn) + { + float fx[] = positionReturn; + if (fx == null) + fx = new float[numEntries * 2]; + + for (int i = 0; i < numEntries; ++i) + { + fx[2*i ] = (float) extents[10*i + 8]; + fx[2*i + 1] = (float) extents[10*i + 9]; + } + return fx; + } + + public AffineTransform getGlyphTransform (int glyphIndex) + { + // Glyphs don't have independent transforms in these simple glyph + // vectors; docs specify null is an ok return here. + return null; + } + + public Shape getGlyphVisualBounds (int i) + { + return new Rectangle2D.Double(extents[8*i + 4], extents[8*i + 5], + extents[8*i + 6], extents[8*i + 7]); + } + + public int getLayoutFlags () + { + return 0; + } + + public Rectangle2D getLogicalBounds () + { + return allLogical; + } + + public int getNumGlyphs () + { + return codes.length; + } + + public Shape getOutline () + { + throw new UnsupportedOperationException (); + } + + public Rectangle getPixelBounds (FontRenderContext renderFRC, + float x, float y) + { + return new Rectangle((int)x, + (int)y, + (int) allVisual.getWidth(), + (int) allVisual.getHeight()); + } + + public Rectangle2D getVisualBounds () + { + return allVisual; + } + + public void performDefaultLayout () + { + } + + public void setGlyphPosition (int i, Point2D newPos) + { + extents[8*i ] = newPos.getX(); + extents[8*i + 1] = newPos.getY(); + + extents[8*i + 4] = newPos.getX(); + extents[8*i + 5] = newPos.getY(); + } + + public void setGlyphTransform (int glyphIndex, + AffineTransform newTX) + { + // not yet.. maybe not ever? + throw new UnsupportedOperationException (); + } + + public boolean equals(GlyphVector gv) + { + if (gv == null) + return false; + + if (! (gv instanceof GdkGlyphVector)) + return false; + + GdkGlyphVector ggv = (GdkGlyphVector) gv; + + if ((ggv.codes.length != this.codes.length) + || (ggv.extents.length != this.extents.length)) + return false; + + if ((ggv.font == null && this.font != null) + || (ggv.font != null && this.font == null) + || (!ggv.font.equals(this.font))) + return false; + + if ((ggv.fontRenderContext == null && this.fontRenderContext != null) + || (ggv.fontRenderContext != null && this.fontRenderContext == null) + || (!ggv.fontRenderContext.equals(this.fontRenderContext))) + return false; + + for (int i = 0; i < ggv.codes.length; ++i) + if (ggv.codes[i] != this.codes[i]) + return false; + + for (int i = 0; i < ggv.extents.length; ++i) + if (ggv.extents[i] != this.extents[i]) + return false; + + return true; + } + + public GlyphJustificationInfo getGlyphJustificationInfo(int idx) + { + throw new UnsupportedOperationException (); + } + + public Shape getOutline(float x, float y) + { + throw new UnsupportedOperationException (); + } + +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java new file mode 100644 index 00000000000..a125be7b95e --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java @@ -0,0 +1,388 @@ +/* GdkGraphics.java + Copyright (C) 1998, 1999, 2002, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Rectangle; +import java.awt.Shape; +import java.awt.SystemColor; +import java.awt.image.ImageObserver; +import java.text.AttributedCharacterIterator; + +public class GdkGraphics extends Graphics +{ + private final int native_state = GtkGenericPeer.getUniqueInteger(); + + Color color, xorColor; + GtkComponentPeer component; + Font font; + Rectangle clip; + GtkImage image; + + int xOffset = 0; + int yOffset = 0; + + static final int GDK_COPY = 0, GDK_XOR = 2; + + native void initState (GtkComponentPeer component); + native void initState (int width, int height); + native void initFromImage (GtkImage image); + native void copyState (GdkGraphics g); + + GdkGraphics (GdkGraphics g) + { + color = g.color; + xorColor = g.xorColor; + font = g.font; + clip = new Rectangle (g.clip); + component = g.component; + + copyState (g); + } + + GdkGraphics (int width, int height) + { + initState (width, height); + color = Color.black; + clip = new Rectangle (0, 0, width, height); + font = new Font ("Dialog", Font.PLAIN, 12); + } + + GdkGraphics (GtkImage image) + { + this.image = image; + initFromImage (image); + color = Color.black; + clip = new Rectangle (0, 0, + image.getWidth(null), image.getHeight(null)); + font = new Font ("Dialog", Font.PLAIN, 12); + } + + GdkGraphics (GtkComponentPeer component) + { + this.component = component; + font = component.awtComponent.getFont (); + color = Color.black; + + if (component.isRealized ()) + initComponentGraphics (); + else + connectSignals (component); + } + + void initComponentGraphics () + { + initState (component); + color = component.awtComponent.getForeground (); + Dimension d = component.awtComponent.getSize (); + clip = new Rectangle (0, 0, d.width, d.height); + } + + native void connectSignals (GtkComponentPeer component); + + public native void clearRect(int x, int y, int width, int height); + + public void clipRect (int x, int y, int width, int height) + { + if (component != null && ! component.isRealized ()) + return; + + clip = clip.intersection (new Rectangle (x, y, width, height)); + setClipRectangle (clip.x, clip.y, clip.width, clip.height); + } + + public native void copyArea(int x, int y, int width, int height, + int dx, int dy); + + public Graphics create () + { + return new GdkGraphics (this); + } + + public native void dispose(); + + public boolean drawImage (Image img, int x, int y, + Color bgcolor, ImageObserver observer) + { + return drawImage(img, x, y, img.getWidth(null), img.getHeight(null), + bgcolor, observer); + } + + public boolean drawImage (Image img, int x, int y, ImageObserver observer) + { + return drawImage (img, x, y, null, observer); + } + + public boolean drawImage (Image img, int x, int y, int width, int height, + Color bgcolor, ImageObserver observer) + { + if (img instanceof GtkImage) + return ((GtkImage)img).drawImage (this, x, y, width, height, + bgcolor, observer); + else + return (new GtkImage(img.getSource())).drawImage (this, x, y, + width, height, + bgcolor, observer); + } + + public boolean drawImage (Image img, int x, int y, int width, int height, + ImageObserver observer) + { + return drawImage (img, x, y, width, height, null, observer); + } + + public boolean drawImage (Image img, int dx1, int dy1, int dx2, int dy2, + int sx1, int sy1, int sx2, int sy2, + Color bgcolor, ImageObserver observer) + { + if (img instanceof GtkImage) + return ((GtkImage)img).drawImage(this, dx1, dy1, dx2, dy2, + sx1, sy1, sx2, sy2, bgcolor, observer); + else + return (new GtkImage(img.getSource())).drawImage(this, dx1, dy1, + dx2, dy2, + sx1, sy1, sx2, sy2, + bgcolor, observer); + } + + public boolean drawImage (Image img, int dx1, int dy1, int dx2, int dy2, + int sx1, int sy1, int sx2, int sy2, + ImageObserver observer) + { + return drawImage (img, dx1, dy1, dx2, dy2, + sx1, sy1, sx2, sy2, + null, observer); + } + + public native void drawLine(int x1, int y1, int x2, int y2); + + public native void drawArc(int x, int y, int width, int height, + int startAngle, int arcAngle); + public native void fillArc(int x, int y, int width, int height, + int startAngle, int arcAngle); + public native void drawOval(int x, int y, int width, int height); + public native void fillOval(int x, int y, int width, int height); + + public native void drawPolygon(int[] xPoints, int[] yPoints, int nPoints); + public native void fillPolygon(int[] xPoints, int[] yPoints, int nPoints); + + public native void drawPolyline(int[] xPoints, int[] yPoints, int nPoints); + + public native void drawRect(int x, int y, int width, int height); + public native void fillRect(int x, int y, int width, int height); + + GdkFontPeer getFontPeer() + { + return (GdkFontPeer) getFont().getPeer(); + } + + native void drawString (GdkFontPeer f, String str, int x, int y); + public void drawString (String str, int x, int y) + { + drawString(getFontPeer(), str, x, y); + } + + + public void drawString (AttributedCharacterIterator ci, int x, int y) + { + throw new Error ("not implemented"); + } + + public void drawRoundRect(int x, int y, int width, int height, + int arcWidth, int arcHeight) + { + if (arcWidth > width) + arcWidth = width; + if (arcHeight > height) + arcHeight = height; + + int xx = x + width - arcWidth; + int yy = y + height - arcHeight; + + drawArc (x, y, arcWidth, arcHeight, 90, 90); + drawArc (xx, y, arcWidth, arcHeight, 0, 90); + drawArc (xx, yy, arcWidth, arcHeight, 270, 90); + drawArc (x, yy, arcWidth, arcHeight, 180, 90); + + int y1 = y + arcHeight / 2; + int y2 = y + height - arcHeight / 2; + drawLine (x, y1, x, y2); + drawLine (x + width, y1, x + width, y2); + + int x1 = x + arcWidth / 2; + int x2 = x + width - arcWidth / 2; + drawLine (x1, y, x2, y); + drawLine (x1, y + height, x2, y + height); + } + + public void fillRoundRect (int x, int y, int width, int height, + int arcWidth, int arcHeight) + { + if (arcWidth > width) + arcWidth = width; + if (arcHeight > height) + arcHeight = height; + + int xx = x + width - arcWidth; + int yy = y + height - arcHeight; + + fillArc (x, y, arcWidth, arcHeight, 90, 90); + fillArc (xx, y, arcWidth, arcHeight, 0, 90); + fillArc (xx, yy, arcWidth, arcHeight, 270, 90); + fillArc (x, yy, arcWidth, arcHeight, 180, 90); + + fillRect (x, y + arcHeight / 2, width, height - arcHeight + 1); + fillRect (x + arcWidth / 2, y, width - arcWidth + 1, height); + } + + public Shape getClip () + { + return getClipBounds (); + } + + public Rectangle getClipBounds () + { + if (clip == null) + return null; + else + return clip.getBounds(); + } + + public Color getColor () + { + return color; + } + + public Font getFont () + { + return font; + } + + public FontMetrics getFontMetrics (Font font) + { + return new GdkFontMetrics (font); + } + + native void setClipRectangle (int x, int y, int width, int height); + + public void setClip (int x, int y, int width, int height) + { + if ((component != null && ! component.isRealized ()) + || clip == null) + return; + + clip.x = x; + clip.y = y; + clip.width = width; + clip.height = height; + + setClipRectangle (x, y, width, height); + } + + public void setClip (Rectangle clip) + { + setClip (clip.x, clip.y, clip.width, clip.height); + } + + public void setClip (Shape clip) + { + if (clip != null) + setClip(clip.getBounds()); + } + + private native void setFGColor(int red, int green, int blue); + + public void setColor (Color c) + { + if (c == null) + color = Color.BLACK; + else + color = c; + + if (xorColor == null) /* paint mode */ + setFGColor (color.getRed (), color.getGreen (), color.getBlue ()); + else /* xor mode */ + setFGColor (color.getRed () ^ xorColor.getRed (), + color.getGreen () ^ xorColor.getGreen (), + color.getBlue () ^ xorColor.getBlue ()); + } + + public void setFont (Font font) + { + this.font = font; + } + + native void setFunction (int gdk_func); + + public void setPaintMode () + { + xorColor = null; + + setFunction (GDK_COPY); + setFGColor (color.getRed (), color.getGreen (), color.getBlue ()); + } + + public void setXORMode (Color c) + { + xorColor = c; + + setFunction (GDK_XOR); + setFGColor (color.getRed () ^ xorColor.getRed (), + color.getGreen () ^ xorColor.getGreen (), + color.getBlue () ^ xorColor.getBlue ()); + } + + public native void translateNative(int x, int y); + + public void translate (int x, int y) + { + if (component != null && ! component.isRealized ()) + return; + + clip.x -= x; + clip.y -= y; + + translateNative (x, y); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics2D.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics2D.java new file mode 100644 index 00000000000..b8203179d29 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics2D.java @@ -0,0 +1,1453 @@ +/* GdkGraphics2D.java -- + Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.classpath.Configuration; +import gnu.java.awt.ClasspathToolkit; + +import java.awt.AlphaComposite; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Composite; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.GradientPaint; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; +import java.awt.Image; +import java.awt.Paint; +import java.awt.Rectangle; +import java.awt.RenderingHints; +import java.awt.Shape; +import java.awt.Stroke; +import java.awt.TexturePaint; +import java.awt.Toolkit; +import java.awt.font.FontRenderContext; +import java.awt.font.GlyphVector; +import java.awt.geom.AffineTransform; +import java.awt.geom.Arc2D; +import java.awt.geom.GeneralPath; +import java.awt.geom.NoninvertibleTransformException; +import java.awt.geom.PathIterator; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.image.AffineTransformOp; +import java.awt.image.BufferedImage; +import java.awt.image.BufferedImageOp; +import java.awt.image.ColorModel; +import java.awt.image.CropImageFilter; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferInt; +import java.awt.image.DirectColorModel; +import java.awt.image.FilteredImageSource; +import java.awt.image.ImageObserver; +import java.awt.image.ImagingOpException; +import java.awt.image.MultiPixelPackedSampleModel; +import java.awt.image.Raster; +import java.awt.image.RenderedImage; +import java.awt.image.SampleModel; +import java.awt.image.WritableRaster; +import java.awt.image.renderable.RenderContext; +import java.awt.image.renderable.RenderableImage; +import java.text.AttributedCharacterIterator; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + +public class GdkGraphics2D extends Graphics2D +{ + ////////////////////////////////////// + ////// State Management Methods ////// + ////////////////////////////////////// + + static + { + if (! Configuration.GTK_CAIRO_ENABLED) + throw new Error("Grahics2D not implemented. " + + "Cairo was not found or disabled at configure time"); + + if (Configuration.INIT_LOAD_LIBRARY) + System.loadLibrary("gtkpeer"); + + if (GtkToolkit.useGraphics2D()) + initStaticState(); + } + + static native void initStaticState(); + + private final int native_state = GtkGenericPeer.getUniqueInteger(); + + // These are package-private to avoid accessor methods. + Paint paint; + Stroke stroke; + Color fg; + Color bg; + Shape clip; + AffineTransform transform; + private GtkComponentPeer component; + // This is package-private to avoid an accessor method. + Font font; + private RenderingHints hints; + private BufferedImage bimage; + private boolean pixelConversionRequired; + private int[] pixelBuffer; + // This is package-private to avoid an accessor method. + Composite comp; + private Stack stateStack; + + private native void initState(GtkComponentPeer component); + private native void initState(int width, int height); + private native void initState(int[] pixes, int width, int height); + private native void copyState(GdkGraphics2D g); + public native void dispose(); + private native void cairoSurfaceSetFilter(int filter); + native void connectSignals(GtkComponentPeer component); + + public void finalize() + { + dispose(); + } + + public Graphics create() + { + return new GdkGraphics2D(this); + } + + public Graphics create(int x, int y, int width, int height) + { + return new GdkGraphics2D(width, height); + } + + GdkGraphics2D(GdkGraphics2D g) + { + paint = g.paint; + stroke = g.stroke; + setRenderingHints(g.hints); + + if (g.fg.getAlpha() != -1) + fg = new Color(g.fg.getRed(), g.fg.getGreen(), g.fg.getBlue(), + g.fg.getAlpha()); + else + fg = new Color(g.fg.getRGB()); + + if (g.bg.getAlpha() != -1) + bg = new Color(g.bg.getRed(), g.bg.getGreen(), g.bg.getBlue(), + g.bg.getAlpha()); + else + bg = new Color(g.bg.getRGB()); + + if (g.clip == null) + clip = null; + else + clip = new Rectangle(g.getClipBounds()); + + if (g.transform == null) + transform = new AffineTransform(); + else + transform = new AffineTransform(g.transform); + + font = g.font; + component = g.component; + copyState(g); + + setColor(fg); + setBackground(bg); + setPaint(paint); + setStroke(stroke); + setTransform(transform); + setClip(clip); + stateStack = new Stack(); + } + + GdkGraphics2D(int width, int height) + { + initState(width, height); + + setColor(Color.black); + setBackground(new Color(0, 0, 0, 0)); + setPaint(getColor()); + setFont(new Font("SansSerif", Font.PLAIN, 12)); + setTransform(new AffineTransform()); + setStroke(new BasicStroke()); + setRenderingHints(getDefaultHints()); + + stateStack = new Stack(); + } + + GdkGraphics2D(GtkComponentPeer component) + { + this.component = component; + + if (component.isRealized()) + initComponentGraphics2D(); + else + connectSignals(component); + } + + void initComponentGraphics2D() + { + initState(component); + + setColor(component.awtComponent.getForeground()); + setBackground(component.awtComponent.getBackground()); + setPaint(getColor()); + setTransform(new AffineTransform()); + setStroke(new BasicStroke()); + setRenderingHints(getDefaultHints()); + setFont(new Font("SansSerif", Font.PLAIN, 12)); + + stateStack = new Stack(); + } + + GdkGraphics2D(BufferedImage bimage) + { + this.bimage = bimage; + this.pixelBuffer = findSimpleIntegerArray(bimage.getColorModel(), + bimage.getRaster()); + if (this.pixelBuffer == null) + { + this.pixelBuffer = new int[bimage.getRaster().getWidth() * bimage.getRaster() + .getHeight()]; + this.pixelConversionRequired = true; + } + else + { + this.pixelConversionRequired = false; + } + + initState(this.pixelBuffer, bimage.getWidth(), bimage.getHeight()); + + setColor(Color.black); + setBackground(new Color(0, 0, 0, 0)); + setPaint(getColor()); + setFont(new Font("SansSerif", Font.PLAIN, 12)); + setTransform(new AffineTransform()); + setStroke(new BasicStroke()); + setRenderingHints(getDefaultHints()); + + stateStack = new Stack(); + + // draw current buffered image to the pixmap associated + // with it, if the image is not equal to our paint buffer. + if (pixelConversionRequired) + drawImage(bimage, new AffineTransform(1, 0, 0, 1, 0, 0), bg, null); + } + + //////////////////////////////////// + ////// Native Drawing Methods ////// + //////////////////////////////////// + + // GDK drawing methods + private native void gdkDrawDrawable(GdkGraphics2D other, int x, int y); + + // drawing utility methods + private native void drawPixels(int[] pixels, int w, int h, int stride, + double[] i2u); + private native void setTexturePixels(int[] pixels, int w, int h, int stride); + private native void setGradient(double x1, double y1, double x2, double y2, + int r1, int g1, int b1, int a1, int r2, + int g2, int b2, int a2, boolean cyclic); + + // simple passthroughs to cairo + private native void cairoSave(); + private native void cairoRestore(); + private native void cairoSetMatrix(double[] m); + private native void cairoSetOperator(int cairoOperator); + private native void cairoSetRGBAColor(double red, double green, + double blue, double alpha); + private native void cairoSetFillRule(int cairoFillRule); + private native void cairoSetLineWidth(double width); + private native void cairoSetLineCap(int cairoLineCap); + private native void cairoSetLineJoin(int cairoLineJoin); + private native void cairoSetDash(double[] dashes, int ndash, double offset); + + private native void cairoSetMiterLimit(double limit); + private native void cairoNewPath(); + private native void cairoMoveTo(double x, double y); + private native void cairoLineTo(double x, double y); + private native void cairoCurveTo(double x1, double y1, double x2, double y2, + double x3, double y3); + private native void cairoRelMoveTo(double dx, double dy); + private native void cairoRelLineTo(double dx, double dy); + private native void cairoRelCurveTo(double dx1, double dy1, double dx2, + double dy2, double dx3, double dy3); + private native void cairoRectangle(double x, double y, double width, + double height); + private native void cairoClosePath(); + private native void cairoStroke(); + private native void cairoFill(); + private native void cairoClip(); + + ///////////////////////////////////////////// + ////// General Drawing Support Methods ////// + ///////////////////////////////////////////// + + private class DrawState + { + private Paint paint; + private Stroke stroke; + private Color fg; + private Color bg; + private Shape clip; + private AffineTransform transform; + private Font font; + private Composite comp; + + DrawState(GdkGraphics2D g) + { + this.paint = g.paint; + this.stroke = g.stroke; + this.fg = g.fg; + this.bg = g.bg; + this.clip = g.clip; + if (g.transform != null) + this.transform = (AffineTransform) g.transform.clone(); + this.font = g.font; + this.comp = g.comp; + } + + public void restore(GdkGraphics2D g) + { + g.paint = this.paint; + g.stroke = this.stroke; + g.fg = this.fg; + g.bg = this.bg; + g.clip = this.clip; + g.transform = this.transform; + g.font = this.font; + g.comp = this.comp; + } + } + + private void stateSave() + { + stateStack.push(new DrawState(this)); + cairoSave(); + } + + private void stateRestore() + { + ((DrawState) (stateStack.pop())).restore(this); + cairoRestore(); + } + + // Some operations (drawing rather than filling) require that their + // coords be shifted to land on 0.5-pixel boundaries, in order to land on + // "middle of pixel" coordinates and light up complete pixels. + private boolean shiftDrawCalls = false; + + private double shifted(double coord, boolean doShift) + { + if (doShift) + return Math.floor(coord) + 0.5; + else + return coord; + } + + private void walkPath(PathIterator p, boolean doShift) + { + double x = 0; + double y = 0; + double[] coords = new double[6]; + + cairoSetFillRule(p.getWindingRule()); + for (; ! p.isDone(); p.next()) + { + int seg = p.currentSegment(coords); + switch (seg) + { + case PathIterator.SEG_MOVETO: + x = shifted(coords[0], doShift); + y = shifted(coords[1], doShift); + cairoMoveTo(x, y); + break; + case PathIterator.SEG_LINETO: + x = shifted(coords[0], doShift); + y = shifted(coords[1], doShift); + cairoLineTo(x, y); + break; + case PathIterator.SEG_QUADTO: + // splitting a quadratic bezier into a cubic: + // see: http://pfaedit.sourceforge.net/bezier.html + double x1 = x + (2.0 / 3.0) * (shifted(coords[0], doShift) - x); + double y1 = y + (2.0 / 3.0) * (shifted(coords[1], doShift) - y); + + double x2 = x1 + (1.0 / 3.0) * (shifted(coords[2], doShift) - x); + double y2 = y1 + (1.0 / 3.0) * (shifted(coords[3], doShift) - y); + + x = shifted(coords[2], doShift); + y = shifted(coords[3], doShift); + cairoCurveTo(x1, y1, x2, y2, x, y); + break; + case PathIterator.SEG_CUBICTO: + x = shifted(coords[4], doShift); + y = shifted(coords[5], doShift); + cairoCurveTo(shifted(coords[0], doShift), + shifted(coords[1], doShift), + shifted(coords[2], doShift), + shifted(coords[3], doShift), x, y); + break; + case PathIterator.SEG_CLOSE: + cairoClosePath(); + break; + } + } + } + + private Map getDefaultHints() + { + HashMap defaultHints = new HashMap(); + + defaultHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT); + + defaultHints.put(RenderingHints.KEY_STROKE_CONTROL, + RenderingHints.VALUE_STROKE_DEFAULT); + + defaultHints.put(RenderingHints.KEY_FRACTIONALMETRICS, + RenderingHints.VALUE_FRACTIONALMETRICS_OFF); + + defaultHints.put(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + + defaultHints.put(RenderingHints.KEY_RENDERING, + RenderingHints.VALUE_RENDER_DEFAULT); + + return defaultHints; + } + + public static int[] findSimpleIntegerArray (ColorModel cm, Raster raster) + { + if (cm == null || raster == null) + return null; + + if (! cm.getColorSpace().isCS_sRGB()) + return null; + + if (! (cm instanceof DirectColorModel)) + return null; + + DirectColorModel dcm = (DirectColorModel) cm; + + if (dcm.getRedMask() != 0x00FF0000 || dcm.getGreenMask() != 0x0000FF00 + || dcm.getBlueMask() != 0x000000FF) + return null; + + if (! (raster instanceof WritableRaster)) + return null; + + if (raster.getSampleModel().getDataType() != DataBuffer.TYPE_INT) + return null; + + if (! (raster.getDataBuffer() instanceof DataBufferInt)) + return null; + + DataBufferInt db = (DataBufferInt) raster.getDataBuffer(); + + if (db.getNumBanks() != 1) + return null; + + // Finally, we have determined that this is a single bank, [A]RGB-int + // buffer in sRGB space. It's worth checking all this, because it means + // that cairo can paint directly into the data buffer, which is very + // fast compared to all the normal copying and converting. + + return db.getData(); + } + + private void updateBufferedImage() + { + if (bimage != null && pixelConversionRequired) + { + int height = bimage.getHeight(); + int width = bimage.getWidth(); + int index = 0; + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) + bimage.setRGB(x, y, pixelBuffer[index++]); + } + } + + private boolean drawImage(Image img, AffineTransform xform, + Color bgcolor, ImageObserver obs) + { + if (img == null) + return false; + + // FIXME: I'll fix this, /Sven +// if (img instanceof GtkOffScreenImage +// && img.getGraphics() instanceof GdkGraphics2D +// && (xform == null || xform.getType() == AffineTransform.TYPE_IDENTITY +// || xform.getType() == AffineTransform.TYPE_TRANSLATION)) +// { +// // we are being asked to flush a double buffer from Gdk +// GdkGraphics2D g2 = (GdkGraphics2D) img.getGraphics(); +// gdkDrawDrawable(g2, (int) xform.getTranslateX(), +// (int) xform.getTranslateY()); + +// updateBufferedImage(); + +// return true; +// } +// else + { + // In this case, xform is an AffineTransform that transforms bounding + // box of the specified image from image space to user space. However + // when we pass this transform to cairo, cairo will use this transform + // to map "user coordinates" to "pixel" coordinates, which is the + // other way around. Therefore to get the "user -> pixel" transform + // that cairo wants from "image -> user" transform that we currently + // have, we will need to invert the transformation matrix. + AffineTransform invertedXform = new AffineTransform(); + + try + { + invertedXform = xform.createInverse(); + if (img instanceof BufferedImage) + { + // draw an image which has actually been loaded + // into memory fully + BufferedImage b = (BufferedImage) img; + return drawRaster(b.getColorModel(), b.getTile(0, 0), + invertedXform, bgcolor); + } + else + return this.drawImage(GdkPixbufDecoder.createBufferedImage(img + .getSource()), + xform, bgcolor, obs); + } + catch (NoninvertibleTransformException e) + { + throw new ImagingOpException("Unable to invert transform " + + xform.toString()); + } + } + } + + ////////////////////////////////////////////////// + ////// Implementation of Graphics2D Methods ////// + ////////////////////////////////////////////////// + + public void draw(Shape s) + { + if (stroke != null && ! (stroke instanceof BasicStroke)) + { + fill(stroke.createStrokedShape(s)); + return; + } + + cairoNewPath(); + + if (s instanceof Rectangle2D) + { + Rectangle2D r = (Rectangle2D) s; + cairoRectangle(shifted(r.getX(), shiftDrawCalls), + shifted(r.getY(), shiftDrawCalls), r.getWidth(), + r.getHeight()); + } + else + walkPath(s.getPathIterator(null), shiftDrawCalls); + cairoStroke(); + + updateBufferedImage(); + } + + public void fill(Shape s) + { + cairoNewPath(); + if (s instanceof Rectangle2D) + { + Rectangle2D r = (Rectangle2D) s; + cairoRectangle(r.getX(), r.getY(), r.getWidth(), r.getHeight()); + } + else + walkPath(s.getPathIterator(null), false); + + cairoFill(); + + updateBufferedImage(); + } + + public void clip(Shape s) + { + // update it + if (clip == null || s == null) + clip = s; + else if (s instanceof Rectangle2D && clip instanceof Rectangle2D) + { + Rectangle2D r = (Rectangle2D) s; + Rectangle2D curr = (Rectangle2D) clip; + clip = curr.createIntersection(r); + } + else + throw new UnsupportedOperationException(); + + // draw it + if (clip != null) + { + cairoNewPath(); + if (clip instanceof Rectangle2D) + { + Rectangle2D r = (Rectangle2D) clip; + cairoRectangle(r.getX(), r.getY(), r.getWidth(), r.getHeight()); + } + else + walkPath(clip.getPathIterator(null), false); + + // cairoClosePath (); + cairoClip(); + } + } + + public Paint getPaint() + { + return paint; + } + + public AffineTransform getTransform() + { + return (AffineTransform) transform.clone(); + } + + public void setPaint(Paint p) + { + if (paint == null) + return; + + paint = p; + if (paint instanceof Color) + { + setColor((Color) paint); + } + else if (paint instanceof TexturePaint) + { + TexturePaint tp = (TexturePaint) paint; + BufferedImage img = tp.getImage(); + + // map the image to the anchor rectangle + int width = (int) tp.getAnchorRect().getWidth(); + int height = (int) tp.getAnchorRect().getHeight(); + + double scaleX = width / (double) img.getWidth(); + double scaleY = width / (double) img.getHeight(); + + AffineTransform at = new AffineTransform(scaleX, 0, 0, scaleY, 0, 0); + AffineTransformOp op = new AffineTransformOp(at, getRenderingHints()); + BufferedImage texture = op.filter(img, null); + int[] pixels = texture.getRGB(0, 0, width, height, null, 0, width); + setTexturePixels(pixels, width, height, width); + } + else if (paint instanceof GradientPaint) + { + GradientPaint gp = (GradientPaint) paint; + Point2D p1 = gp.getPoint1(); + Point2D p2 = gp.getPoint2(); + Color c1 = gp.getColor1(); + Color c2 = gp.getColor2(); + setGradient(p1.getX(), p1.getY(), p2.getX(), p2.getY(), c1.getRed(), + c1.getGreen(), c1.getBlue(), c1.getAlpha(), c2.getRed(), + c2.getGreen(), c2.getBlue(), c2.getAlpha(), gp.isCyclic()); + } + else + throw new java.lang.UnsupportedOperationException(); + } + + public void setTransform(AffineTransform tx) + { + transform = tx; + if (transform != null) + { + double[] m = new double[6]; + transform.getMatrix(m); + cairoSetMatrix(m); + } + } + + public void transform(AffineTransform tx) + { + if (transform == null) + transform = new AffineTransform(tx); + else + transform.concatenate(tx); + setTransform(transform); + if (clip != null) + { + // FIXME: this should actuall try to transform the shape + // rather than degrade to bounds. + Rectangle2D r = clip.getBounds2D(); + double[] coords = new double[] + { + r.getX(), r.getY(), r.getX() + r.getWidth(), + r.getY() + r.getHeight() + }; + try + { + tx.createInverse().transform(coords, 0, coords, 0, 2); + r.setRect(coords[0], coords[1], coords[2] - coords[0], + coords[3] - coords[1]); + clip = r; + } + catch (java.awt.geom.NoninvertibleTransformException e) + { + } + } + } + + public void rotate(double theta) + { + transform(AffineTransform.getRotateInstance(theta)); + } + + public void rotate(double theta, double x, double y) + { + transform(AffineTransform.getRotateInstance(theta, x, y)); + } + + public void scale(double sx, double sy) + { + transform(AffineTransform.getScaleInstance(sx, sy)); + } + + public void translate(double tx, double ty) + { + transform(AffineTransform.getTranslateInstance(tx, ty)); + } + + public void translate(int x, int y) + { + translate((double) x, (double) y); + } + + public void shear(double shearX, double shearY) + { + transform(AffineTransform.getShearInstance(shearX, shearY)); + } + + public Stroke getStroke() + { + return stroke; + } + + public void setStroke(Stroke st) + { + stroke = st; + if (stroke instanceof BasicStroke) + { + BasicStroke bs = (BasicStroke) stroke; + cairoSetLineCap(bs.getEndCap()); + cairoSetLineWidth(bs.getLineWidth()); + cairoSetLineJoin(bs.getLineJoin()); + cairoSetMiterLimit(bs.getMiterLimit()); + float[] dashes = bs.getDashArray(); + if (dashes != null) + { + double[] double_dashes = new double[dashes.length]; + for (int i = 0; i < dashes.length; i++) + double_dashes[i] = dashes[i]; + cairoSetDash(double_dashes, double_dashes.length, + (double) bs.getDashPhase()); + } + } + } + + //////////////////////////////////////////////// + ////// Implementation of Graphics Methods ////// + //////////////////////////////////////////////// + + public void setPaintMode() + { + setComposite(java.awt.AlphaComposite.SrcOver); + } + + public void setXORMode(Color c) + { + setComposite(new gnu.java.awt.BitwiseXORComposite(c)); + } + + public void setColor(Color c) + { + if (c == null) + c = Color.BLACK; + + fg = c; + paint = c; + cairoSetRGBAColor(fg.getRed() / 255.0, fg.getGreen() / 255.0, + fg.getBlue() / 255.0, fg.getAlpha() / 255.0); + } + + public Color getColor() + { + return fg; + } + + public void clipRect(int x, int y, int width, int height) + { + clip(new Rectangle(x, y, width, height)); + } + + public Shape getClip() + { + return clip.getBounds2D(); //getClipInDevSpace(); + } + + public Rectangle getClipBounds() + { + if (clip == null) + return null; + else + return clip.getBounds(); + } + + protected Rectangle2D getClipInDevSpace() + { + Rectangle2D uclip = clip.getBounds2D(); + if (transform == null) + return uclip; + else + { + Point2D pos = transform.transform(new Point2D.Double(uclip.getX(), + uclip.getY()), + (Point2D) null); + Point2D extent = transform.deltaTransform(new Point2D.Double(uclip + .getWidth(), + uclip + .getHeight()), + (Point2D) null); + return new Rectangle2D.Double(pos.getX(), pos.getY(), extent.getX(), + extent.getY()); + } + } + + public void setClip(int x, int y, int width, int height) + { + setClip(new Rectangle2D.Double((double) x, (double) y, (double) width, + (double) height)); + } + + public void setClip(Shape s) + { + clip = s; + if (s != null) + { + cairoNewPath(); + if (s instanceof Rectangle2D) + { + Rectangle2D r = (Rectangle2D) s; + cairoRectangle(r.getX(), r.getY(), r.getWidth(), r.getHeight()); + } + else + walkPath(s.getPathIterator(null), false); + + // cairoClosePath (); + cairoClip(); + } + } + + private static BasicStroke draw3DRectStroke = new BasicStroke(); + + public void draw3DRect(int x, int y, int width, int height, boolean raised) + { + Stroke tmp = stroke; + setStroke(draw3DRectStroke); + super.draw3DRect(x, y, width, height, raised); + setStroke(tmp); + updateBufferedImage(); + } + + public void fill3DRect(int x, int y, int width, int height, boolean raised) + { + Stroke tmp = stroke; + setStroke(draw3DRectStroke); + super.fill3DRect(x, y, width, height, raised); + setStroke(tmp); + updateBufferedImage(); + } + + public void drawRect(int x, int y, int width, int height) + { + draw(new Rectangle(x, y, width, height)); + } + + public void fillRect(int x, int y, int width, int height) + { + cairoNewPath(); + cairoRectangle(x, y, width, height); + cairoFill(); + } + + public void clearRect(int x, int y, int width, int height) + { + cairoSetRGBAColor(bg.getRed() / 255.0, bg.getGreen() / 255.0, + bg.getBlue() / 255.0, 1.0); + cairoNewPath(); + cairoRectangle(x, y, width, height); + cairoFill(); + setColor(fg); + + updateBufferedImage(); + } + + public void setBackground(Color c) + { + bg = c; + } + + public Color getBackground() + { + return bg; + } + + private void doPolygon(int[] xPoints, int[] yPoints, int nPoints, + boolean close, boolean fill) + { + if (nPoints < 1) + return; + GeneralPath gp = new GeneralPath(PathIterator.WIND_EVEN_ODD); + gp.moveTo((float) xPoints[0], (float) yPoints[0]); + for (int i = 1; i < nPoints; i++) + gp.lineTo((float) xPoints[i], (float) yPoints[i]); + + if (close) + gp.closePath(); + + Shape sh = gp; + if (fill == false && stroke != null && ! (stroke instanceof BasicStroke)) + { + sh = stroke.createStrokedShape(gp); + fill = true; + } + + if (fill) + fill(sh); + else + draw(sh); + } + + public void drawLine(int x1, int y1, int x2, int y2) + { + int[] xp = new int[2]; + int[] yp = new int[2]; + + xp[0] = x1; + xp[1] = x2; + yp[0] = y1; + yp[1] = y2; + + doPolygon(xp, yp, 2, false, false); + } + + public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) + { + doPolygon(xPoints, yPoints, nPoints, true, true); + } + + public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) + { + doPolygon(xPoints, yPoints, nPoints, true, false); + } + + public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) + { + doPolygon(xPoints, yPoints, nPoints, false, false); + } + + private boolean drawRaster(ColorModel cm, Raster r, + AffineTransform imageToUser, Color bgcolor) + { + if (r == null) + return false; + + SampleModel sm = r.getSampleModel(); + DataBuffer db = r.getDataBuffer(); + + if (db == null || sm == null) + return false; + + if (cm == null) + cm = ColorModel.getRGBdefault(); + + double[] i2u = new double[6]; + if (imageToUser != null) + imageToUser.getMatrix(i2u); + else + { + i2u[0] = 1; + i2u[1] = 0; + i2u[2] = 0; + i2u[3] = 1; + i2u[4] = 0; + i2u[5] = 0; + } + + int[] pixels = findSimpleIntegerArray(cm, r); + + if (pixels == null) + { + // FIXME: I don't think this code will work correctly with a non-RGB + // MultiPixelPackedSampleModel. Although this entire method should + // probably be rewritten to better utilize Cairo's different supported + // data formats. + if (sm instanceof MultiPixelPackedSampleModel) + { + pixels = r.getPixels(0, 0, r.getWidth(), r.getHeight(), pixels); + for (int i = 0; i < pixels.length; i++) + pixels[i] = cm.getRGB(pixels[i]); + } + else + { + pixels = new int[r.getWidth() * r.getHeight()]; + for (int i = 0; i < pixels.length; i++) + pixels[i] = cm.getRGB(db.getElem(i)); + } + } + + // Change all transparent pixels in the image to the specified bgcolor, + // or (if there's no alpha) fill in an alpha channel so that it paints + // correctly. + if (cm.hasAlpha()) + { + if (bgcolor != null && cm.hasAlpha()) + for (int i = 0; i < pixels.length; i++) + { + if (cm.getAlpha(pixels[i]) == 0) + pixels[i] = bgcolor.getRGB(); + } + } + else + for (int i = 0; i < pixels.length; i++) + pixels[i] |= 0xFF000000; + + drawPixels(pixels, r.getWidth(), r.getHeight(), r.getWidth(), i2u); + + updateBufferedImage(); + + return true; + } + + public void drawRenderedImage(RenderedImage image, AffineTransform xform) + { + drawRaster(image.getColorModel(), image.getData(), xform, bg); + } + + public void drawRenderableImage(RenderableImage image, AffineTransform xform) + { + drawRenderedImage(image.createRendering(new RenderContext(xform)), xform); + } + + public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) + { + return drawImage(img, xform, bg, obs); + } + + public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y) + { + Image filtered = op.filter(image, null); + drawImage(filtered, new AffineTransform(1f, 0f, 0f, 1f, x, y), bg, null); + } + + public boolean drawImage(Image img, int x, int y, ImageObserver observer) + { + return drawImage(img, new AffineTransform(1f, 0f, 0f, 1f, x, y), bg, + observer); + } + + /////////////////////////////////////////////// + ////// Unimplemented Stubs and Overloads ////// + /////////////////////////////////////////////// + + public boolean hit(Rectangle rect, Shape text, boolean onStroke) + { + throw new java.lang.UnsupportedOperationException(); + } + + public GraphicsConfiguration getDeviceConfiguration() + { + throw new java.lang.UnsupportedOperationException(); + } + + public void setComposite(Composite comp) + { + this.comp = comp; + + if (comp instanceof AlphaComposite) + { + AlphaComposite a = (AlphaComposite) comp; + cairoSetOperator(a.getRule()); + Color c = getColor(); + setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), + (int) (a.getAlpha() * ((float) c.getAlpha())))); + } + else + throw new java.lang.UnsupportedOperationException(); + } + + public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) + { + hints.put(hintKey, hintValue); + + if (hintKey.equals(RenderingHints.KEY_INTERPOLATION) + || hintKey.equals(RenderingHints.KEY_ALPHA_INTERPOLATION)) + { + if (hintValue.equals(RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)) + cairoSurfaceSetFilter(0); + + else if (hintValue.equals(RenderingHints.VALUE_INTERPOLATION_BILINEAR)) + cairoSurfaceSetFilter(1); + + else if (hintValue.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED)) + cairoSurfaceSetFilter(2); + + else if (hintValue.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY)) + cairoSurfaceSetFilter(3); + + else if (hintValue.equals(RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT)) + cairoSurfaceSetFilter(4); + } + + shiftDrawCalls = hints.containsValue(RenderingHints.VALUE_STROKE_NORMALIZE) + || hints.containsValue(RenderingHints.VALUE_STROKE_DEFAULT); + } + + public Object getRenderingHint(RenderingHints.Key hintKey) + { + return hints.get(hintKey); + } + + public void setRenderingHints(Map hints) + { + this.hints = new RenderingHints(getDefaultHints()); + this.hints.add(new RenderingHints(hints)); + + if (hints.containsKey(RenderingHints.KEY_INTERPOLATION)) + { + if (hints.containsValue(RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)) + cairoSurfaceSetFilter(0); + + else if (hints.containsValue(RenderingHints.VALUE_INTERPOLATION_BILINEAR)) + cairoSurfaceSetFilter(1); + } + + if (hints.containsKey(RenderingHints.KEY_ALPHA_INTERPOLATION)) + { + if (hints.containsValue(RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED)) + cairoSurfaceSetFilter(2); + + else if (hints.containsValue(RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY)) + cairoSurfaceSetFilter(3); + + else if (hints.containsValue(RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT)) + cairoSurfaceSetFilter(4); + } + + shiftDrawCalls = hints.containsValue(RenderingHints.VALUE_STROKE_NORMALIZE) + || hints.containsValue(RenderingHints.VALUE_STROKE_DEFAULT); + } + + public void addRenderingHints(Map hints) + { + this.hints.add(new RenderingHints(hints)); + } + + public RenderingHints getRenderingHints() + { + return hints; + } + + public Composite getComposite() + { + if (comp == null) + return AlphaComposite.SrcOver; + else + return comp; + } + + public FontRenderContext getFontRenderContext() + { + return new FontRenderContext(transform, true, true); + } + + public void copyArea(int x, int y, int width, int height, int dx, int dy) + { + throw new java.lang.UnsupportedOperationException(); + } + + public void drawArc(int x, int y, int width, int height, int startAngle, + int arcAngle) + { + draw(new Arc2D.Double((double) x, (double) y, (double) width, + (double) height, (double) startAngle, + (double) arcAngle, Arc2D.OPEN)); + } + + public boolean drawImage(Image img, int x, int y, Color bgcolor, + ImageObserver observer) + { + return drawImage(img, x, y, img.getWidth(observer), + img.getHeight(observer), bgcolor, observer); + } + + public boolean drawImage(Image img, int x, int y, int width, int height, + Color bgcolor, ImageObserver observer) + { + double scaleX = width / (double) img.getWidth(observer); + double scaleY = height / (double) img.getHeight(observer); + + return drawImage(img, new AffineTransform(scaleX, 0f, 0f, scaleY, x, y), + bgcolor, observer); + } + + public boolean drawImage(Image img, int x, int y, int width, int height, + ImageObserver observer) + { + return drawImage(img, x, y, width, height, bg, observer); + } + + public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, + int sx1, int sy1, int sx2, int sy2, Color bgcolor, + ImageObserver observer) + { + if (img == null) + return false; + + Image subImage; + + int sourceWidth = sx2 - sx1; + int sourceHeight = sy2 - sy1; + + int destWidth = dx2 - dx1; + int destHeight = dy2 - dy1; + + double scaleX = destWidth / (double) sourceWidth; + double scaleY = destHeight / (double) sourceHeight; + + // Get the subimage of the source enclosed in the + // rectangle specified by sx1, sy1, sx2, sy2 + + if (img instanceof BufferedImage) + { + BufferedImage b = (BufferedImage) img; + subImage = b.getSubimage(sx1, sy1, sx2, sy2); + } + else + { + // FIXME: This code currently doesn't work. Null Pointer + // exception is thrown in this case. This happens + // because img.getSource() always returns null, since source gets + // never initialized when it is created with the help of + // createImage(int width, int height). + CropImageFilter filter = new CropImageFilter(sx1, sx2, sx2, sy2); + FilteredImageSource src = new FilteredImageSource(img.getSource(), + filter); + + subImage = Toolkit.getDefaultToolkit().createImage(src); + } + + return drawImage(subImage, + new AffineTransform(scaleX, 0, 0, scaleY, dx1, dy1), + bgcolor, observer); + } + + public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, + int sx1, int sy1, int sx2, int sy2, + ImageObserver observer) + { + return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bg, observer); + } + + public void drawOval(int x, int y, int width, int height) + { + drawArc(x, y, width, height, 0, 360); + } + + public void drawRoundRect(int x, int y, int width, int height, int arcWidth, + int arcHeight) + { + if (arcWidth > width) + arcWidth = width; + if (arcHeight > height) + arcHeight = height; + + int xx = x + width - arcWidth; + int yy = y + height - arcHeight; + + drawArc(x, y, arcWidth, arcHeight, 90, 90); + drawArc(xx, y, arcWidth, arcHeight, 0, 90); + drawArc(xx, yy, arcWidth, arcHeight, 270, 90); + drawArc(x, yy, arcWidth, arcHeight, 180, 90); + + int y1 = y + arcHeight / 2; + int y2 = y + height - arcHeight / 2; + drawLine(x, y1, x, y2); + drawLine(x + width, y1, x + width, y2); + + int x1 = x + arcWidth / 2; + int x2 = x + width - arcWidth / 2; + drawLine(x1, y, x2, y); + drawLine(x1, y + height, x2, y + height); + } + + // these are the most accelerated painting paths + native void cairoDrawGlyphVector(GdkFontPeer font, + float x, float y, int n, + int[] codes, float[] positions); + + native void cairoDrawGdkTextLayout(GdkTextLayout gl, + float x, float y); + + GdkFontPeer getFontPeer() + { + return (GdkFontPeer) getFont().getPeer(); + } + + public void drawGdkTextLayout(GdkTextLayout gl, float x, float y) + { + cairoDrawGdkTextLayout (gl, x, y); + updateBufferedImage (); + } + + public void drawString(String str, float x, float y) + { + drawGlyphVector(getFont().createGlyphVector(null, str), x, y); + updateBufferedImage (); + } + + public void drawString(String str, int x, int y) + { + drawString (str, (float) x, (float) y); + } + + public void drawString(AttributedCharacterIterator ci, int x, int y) + { + drawString (ci, (float) x, (float) y); + } + + public void drawGlyphVector(GlyphVector gv, float x, float y) + { + int n = gv.getNumGlyphs (); + int[] codes = gv.getGlyphCodes (0, n, null); + float[] positions = gv.getGlyphPositions (0, n, null); + + setFont (gv.getFont ()); + cairoDrawGlyphVector (getFontPeer(), x, y, n, codes, positions); + updateBufferedImage (); + } + + public void drawString(AttributedCharacterIterator ci, float x, float y) + { + GlyphVector gv = getFont().createGlyphVector(getFontRenderContext(), ci); + drawGlyphVector(gv, x, y); + } + + public void fillArc(int x, int y, int width, int height, int startAngle, + int arcAngle) + { + fill(new Arc2D.Double((double) x, (double) y, (double) width, + (double) height, (double) startAngle, + (double) arcAngle, Arc2D.OPEN)); + } + + public void fillOval(int x, int y, int width, int height) + { + fillArc(x, y, width, height, 0, 360); + } + + public void fillRoundRect(int x, int y, int width, int height, int arcWidth, + int arcHeight) + { + if (arcWidth > width) + arcWidth = width; + if (arcHeight > height) + arcHeight = height; + + int xx = x + width - arcWidth; + int yy = y + height - arcHeight; + + fillArc(x, y, arcWidth, arcHeight, 90, 90); + fillArc(xx, y, arcWidth, arcHeight, 0, 90); + fillArc(xx, yy, arcWidth, arcHeight, 270, 90); + fillArc(x, yy, arcWidth, arcHeight, 180, 90); + + fillRect(x, y + arcHeight / 2, width, height - arcHeight + 1); + fillRect(x + arcWidth / 2, y, width - arcWidth + 1, height); + } + + public Font getFont() + { + if (font == null) + return new Font("SansSerif", Font.PLAIN, 12); + return font; + } + + // Until such time as pango is happy to talk directly to cairo, we + // actually need to redirect some calls from the GtkFontPeer and + // GtkFontMetrics into the drawing kit and ask cairo ourselves. + + static native void releasePeerGraphicsResource(GdkFontPeer f); + + public FontMetrics getFontMetrics() + { + return getFontMetrics(getFont()); + } + + public FontMetrics getFontMetrics(Font f) + { + // the reason we go via the toolkit here is to try to get + // a cached object. the toolkit keeps such a cache. + return Toolkit.getDefaultToolkit().getFontMetrics(f); + } + + public void setFont(Font f) + { + if (f.getPeer() instanceof GdkFontPeer) + font = f; + else + font = + ((ClasspathToolkit)(Toolkit.getDefaultToolkit())) + .getFont(f.getName(), f.getAttributes()); + } + + public String toString() + { + return (getClass().getName() + + "[font=" + getFont().toString() + + ",color=" + fg.toString() + + "]"); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphicsConfiguration.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphicsConfiguration.java new file mode 100644 index 00000000000..bfad87acac0 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphicsConfiguration.java @@ -0,0 +1,138 @@ +/* GdkGraphicsConfiguration.java -- describes characteristics of graphics + Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.peer.gtk; + +import java.awt.BufferCapabilities; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.ImageCapabilities; +import java.awt.Rectangle; + +import java.awt.geom.AffineTransform; + +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.VolatileImage; + +public class GdkGraphicsConfiguration + extends GraphicsConfiguration +{ + GdkScreenGraphicsDevice gdkScreenGraphicsDevice; + ColorModel cm; + Rectangle bounds; + + public GtkToolkit getToolkit() + { + return gdkScreenGraphicsDevice.getToolkit(); + } + + public GdkGraphicsConfiguration(GdkScreenGraphicsDevice dev) + { + this.gdkScreenGraphicsDevice = dev; + cm = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).getColorModel(); + bounds = getToolkit().getBounds(); + } + + public GraphicsDevice getDevice() + { + return gdkScreenGraphicsDevice; + } + + public BufferedImage createCompatibleImage(int w, int h) + { + return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); + } + + public BufferedImage createCompatibleImage(int w, int h, + int transparency) + { + return createCompatibleImage(w, h); + } + + public VolatileImage createCompatibleVolatileImage(int w, int h) + { + return new GtkVolatileImage(w, h); + } + + public VolatileImage createCompatibleVolatileImage(int w, int h, + ImageCapabilities caps) + throws java.awt.AWTException + { + return new GtkVolatileImage(w, h, caps); + } + + public ColorModel getColorModel() + { + return cm; + } + + public ColorModel getColorModel(int transparency) + { + return getColorModel(); + } + + public AffineTransform getDefaultTransform() + { + // FIXME: extract the GDK DPI information here. + return new AffineTransform(); + } + + public AffineTransform getNormalizingTransform() + { + // FIXME: extract the GDK DPI information here. + return new AffineTransform(); + } + + public Rectangle getBounds() + { + return bounds; + } + + public BufferCapabilities getBufferCapabilities() + { + return new BufferCapabilities(getImageCapabilities(), + getImageCapabilities(), + BufferCapabilities.FlipContents.UNDEFINED); + } + + public ImageCapabilities getImageCapabilities() + { + return new ImageCapabilities(false); + } + +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java new file mode 100644 index 00000000000..4f9d1c27af6 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java @@ -0,0 +1,107 @@ +/* GdkGraphicsEnvironment.java -- information about the graphics environment + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.HeadlessException; +import java.awt.image.BufferedImage; +import java.util.Locale; + +public class GdkGraphicsEnvironment extends GraphicsEnvironment +{ + GtkToolkit gtkToolkit; + + public GtkToolkit getToolkit() + { + return gtkToolkit; + } + + public GdkGraphicsEnvironment (GtkToolkit tk) + { + super(); + gtkToolkit = tk; + } + + public GraphicsDevice[] getScreenDevices () + { + // FIXME: Support multiple screens, since GDK can. + return new GraphicsDevice[] { new GdkScreenGraphicsDevice (this) }; + } + + public GraphicsDevice getDefaultScreenDevice () + { + if (GraphicsEnvironment.isHeadless ()) + throw new HeadlessException (); + + return new GdkScreenGraphicsDevice (this); + } + + public Graphics2D createGraphics (BufferedImage image) + { + return new GdkGraphics2D (image); + } + + private native int nativeGetNumFontFamilies(); + private native void nativeGetFontFamilies(String[] family_names); + + public Font[] getAllFonts () + { + throw new java.lang.UnsupportedOperationException (); + } + + public String[] getAvailableFontFamilyNames () + { + String[] family_names; + int array_size; + + array_size = nativeGetNumFontFamilies(); + family_names = new String[array_size]; + + nativeGetFontFamilies(family_names); + return family_names; + } + + public String[] getAvailableFontFamilyNames (Locale l) + { + throw new java.lang.UnsupportedOperationException (); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java new file mode 100644 index 00000000000..57d5a36da3a --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java @@ -0,0 +1,681 @@ +/* GdkPixbufDecoder.java -- Image data decoding object + Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.classpath.Configuration; + +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.ImageConsumer; +import java.awt.image.ImageProducer; +import java.awt.image.Raster; +import java.awt.image.RenderedImage; +import java.io.DataOutput; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Locale; +import java.util.Vector; + +import javax.imageio.IIOImage; +import javax.imageio.ImageReadParam; +import javax.imageio.ImageReader; +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.spi.IIORegistry; +import javax.imageio.spi.ImageReaderSpi; +import javax.imageio.spi.ImageWriterSpi; +import javax.imageio.stream.ImageInputStream; +import javax.imageio.stream.ImageOutputStream; + +public class GdkPixbufDecoder extends gnu.java.awt.image.ImageDecoder +{ + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("gtkpeer"); + } + initStaticState (); + } + + static native void initStaticState(); + private final int native_state = GtkGenericPeer.getUniqueInteger (); + + // initState() has been called, but pumpDone() has not yet been called. + private boolean needsClose = false; + + // the current set of ImageConsumers for this decoder + Vector curr; + + // interface to GdkPixbuf + native void initState (); + native void pumpBytes (byte[] bytes, int len) throws IOException; + native void pumpDone () throws IOException; + native void finish (boolean needsClose); + static native void streamImage(int[] bytes, String format, int width, int height, boolean hasAlpha, DataOutput sink); + + // gdk-pixbuf provids data in RGBA format + static final ColorModel cm = new DirectColorModel (32, 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff); + public GdkPixbufDecoder (InputStream in) + { + super (in); + } + + public GdkPixbufDecoder (String filename) + { + super (filename); + } + + public GdkPixbufDecoder (URL url) + { + super (url); + } + + public GdkPixbufDecoder (byte[] imagedata, int imageoffset, int imagelength) + { + super (imagedata, imageoffset, imagelength); + } + + // called back by native side + void areaPrepared (int width, int height) + { + + if (curr == null) + return; + + for (int i = 0; i < curr.size (); i++) + { + ImageConsumer ic = (ImageConsumer) curr.elementAt (i); + ic.setDimensions (width, height); + ic.setColorModel (cm); + ic.setHints (ImageConsumer.RANDOMPIXELORDER); + } + } + + // called back by native side + void areaUpdated (int x, int y, int width, int height, + int pixels[], int scansize) + { + if (curr == null) + return; + + for (int i = 0; i < curr.size (); i++) + { + ImageConsumer ic = (ImageConsumer) curr.elementAt (i); + ic.setPixels (x, y, width, height, cm, pixels, 0, scansize); + } + } + + // called from an async image loader of one sort or another, this method + // repeatedly reads bytes from the input stream and passes them through a + // GdkPixbufLoader using the native method pumpBytes. pumpBytes in turn + // decodes the image data and calls back areaPrepared and areaUpdated on + // this object, feeding back decoded pixel blocks, which we pass to each + // of the ImageConsumers in the provided Vector. + + public void produce (Vector v, InputStream is) throws IOException + { + curr = v; + + byte bytes[] = new byte[4096]; + int len = 0; + initState(); + needsClose = true; + while ((len = is.read (bytes)) != -1) + pumpBytes (bytes, len); + pumpDone(); + needsClose = false; + + for (int i = 0; i < curr.size (); i++) + { + ImageConsumer ic = (ImageConsumer) curr.elementAt (i); + ic.imageComplete (ImageConsumer.STATICIMAGEDONE); + } + + curr = null; + } + + public void finalize() + { + finish(needsClose); + } + + + public static class ImageFormatSpec + { + public String name; + public boolean writable = false; + public ArrayList mimeTypes = new ArrayList(); + public ArrayList extensions = new ArrayList(); + + public ImageFormatSpec(String name, boolean writable) + { + this.name = name; + this.writable = writable; + } + + public synchronized void addMimeType(String m) + { + mimeTypes.add(m); + } + + public synchronized void addExtension(String e) + { + extensions.add(e); + } + } + + static ArrayList imageFormatSpecs; + + public static ImageFormatSpec registerFormat(String name, boolean writable) + { + ImageFormatSpec ifs = new ImageFormatSpec(name, writable); + synchronized(GdkPixbufDecoder.class) + { + if (imageFormatSpecs == null) + imageFormatSpecs = new ArrayList(); + imageFormatSpecs.add(ifs); + } + return ifs; + } + + static String[] getFormatNames(boolean writable) + { + ArrayList names = new ArrayList(); + synchronized (imageFormatSpecs) + { + Iterator i = imageFormatSpecs.iterator(); + while (i.hasNext()) + { + ImageFormatSpec ifs = (ImageFormatSpec) i.next(); + if (writable && !ifs.writable) + continue; + names.add(ifs.name); + + /* + * In order to make the filtering code work, we need to register + * this type under every "format name" likely to be used as a synonym. + * This generally means "all the extensions people might use". + */ + + Iterator j = ifs.extensions.iterator(); + while (j.hasNext()) + names.add((String) j.next()); + } + } + Object[] objs = names.toArray(); + String[] strings = new String[objs.length]; + for (int i = 0; i < objs.length; ++i) + strings[i] = (String) objs[i]; + return strings; + } + + static String[] getFormatExtensions(boolean writable) + { + ArrayList extensions = new ArrayList(); + synchronized (imageFormatSpecs) + { + Iterator i = imageFormatSpecs.iterator(); + while (i.hasNext()) + { + ImageFormatSpec ifs = (ImageFormatSpec) i.next(); + if (writable && !ifs.writable) + continue; + Iterator j = ifs.extensions.iterator(); + while (j.hasNext()) + extensions.add((String) j.next()); + } + } + Object[] objs = extensions.toArray(); + String[] strings = new String[objs.length]; + for (int i = 0; i < objs.length; ++i) + strings[i] = (String) objs[i]; + return strings; + } + + static String[] getFormatMimeTypes(boolean writable) + { + ArrayList mimeTypes = new ArrayList(); + synchronized (imageFormatSpecs) + { + Iterator i = imageFormatSpecs.iterator(); + while (i.hasNext()) + { + ImageFormatSpec ifs = (ImageFormatSpec) i.next(); + if (writable && !ifs.writable) + continue; + Iterator j = ifs.mimeTypes.iterator(); + while (j.hasNext()) + mimeTypes.add((String) j.next()); + } + } + Object[] objs = mimeTypes.toArray(); + String[] strings = new String[objs.length]; + for (int i = 0; i < objs.length; ++i) + strings[i] = (String) objs[i]; + return strings; + } + + + static String findFormatName(Object ext, boolean needWritable) + { + if (ext == null) + throw new IllegalArgumentException("extension is null"); + + if (!(ext instanceof String)) + throw new IllegalArgumentException("extension is not a string"); + + String str = (String) ext; + + Iterator i = imageFormatSpecs.iterator(); + while (i.hasNext()) + { + ImageFormatSpec ifs = (ImageFormatSpec) i.next(); + + if (needWritable && !ifs.writable) + continue; + + if (ifs.name.equals(str)) + return str; + + Iterator j = ifs.extensions.iterator(); + while (j.hasNext()) + { + String extension = (String)j.next(); + if (extension.equals(str)) + return ifs.name; + } + + j = ifs.mimeTypes.iterator(); + while (j.hasNext()) + { + String mimeType = (String)j.next(); + if (mimeType.equals(str)) + return ifs.name; + } + } + throw new IllegalArgumentException("unknown extension '" + str + "'"); + } + + private static GdkPixbufReaderSpi readerSpi; + private static GdkPixbufWriterSpi writerSpi; + + public static synchronized GdkPixbufReaderSpi getReaderSpi() + { + if (readerSpi == null) + readerSpi = new GdkPixbufReaderSpi(); + return readerSpi; + } + + public static synchronized GdkPixbufWriterSpi getWriterSpi() + { + if (writerSpi == null) + writerSpi = new GdkPixbufWriterSpi(); + return writerSpi; + } + + public static void registerSpis(IIORegistry reg) + { + reg.registerServiceProvider(getReaderSpi(), ImageReaderSpi.class); + reg.registerServiceProvider(getWriterSpi(), ImageWriterSpi.class); + } + + public static class GdkPixbufWriterSpi extends ImageWriterSpi + { + public GdkPixbufWriterSpi() + { + super("GdkPixbuf", "2.x", + GdkPixbufDecoder.getFormatNames(true), + GdkPixbufDecoder.getFormatExtensions(true), + GdkPixbufDecoder.getFormatMimeTypes(true), + "gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufWriter", + new Class[] { ImageOutputStream.class }, + new String[] { "gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufReaderSpi" }, + false, null, null, null, null, + false, null, null, null, null); + } + + public boolean canEncodeImage(ImageTypeSpecifier ts) + { + return true; + } + + public ImageWriter createWriterInstance(Object ext) + { + return new GdkPixbufWriter(this, ext); + } + + public String getDescription(java.util.Locale loc) + { + return "GdkPixbuf Writer SPI"; + } + + } + + public static class GdkPixbufReaderSpi extends ImageReaderSpi + { + public GdkPixbufReaderSpi() + { + super("GdkPixbuf", "2.x", + GdkPixbufDecoder.getFormatNames(false), + GdkPixbufDecoder.getFormatExtensions(false), + GdkPixbufDecoder.getFormatMimeTypes(false), + "gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufReader", + new Class[] { ImageInputStream.class }, + new String[] { "gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufWriterSpi" }, + false, null, null, null, null, + false, null, null, null, null); + } + + public boolean canDecodeInput(Object obj) + { + return true; + } + + public ImageReader createReaderInstance(Object ext) + { + return new GdkPixbufReader(this, ext); + } + + public String getDescription(Locale loc) + { + return "GdkPixbuf Reader SPI"; + } + } + + private static class GdkPixbufWriter + extends ImageWriter + { + String ext; + public GdkPixbufWriter(GdkPixbufWriterSpi ownerSpi, Object ext) + { + super(ownerSpi); + this.ext = findFormatName(ext, true); + } + + public IIOMetadata convertImageMetadata (IIOMetadata inData, + ImageTypeSpecifier imageType, + ImageWriteParam param) + { + return null; + } + + public IIOMetadata convertStreamMetadata (IIOMetadata inData, + ImageWriteParam param) + { + return null; + } + + public IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType, + ImageWriteParam param) + { + return null; + } + + public IIOMetadata getDefaultStreamMetadata (ImageWriteParam param) + { + return null; + } + + public void write (IIOMetadata streamMetadata, IIOImage i, ImageWriteParam param) + throws IOException + { + RenderedImage image = i.getRenderedImage(); + Raster ras = image.getData(); + int width = ras.getWidth(); + int height = ras.getHeight(); + ColorModel model = image.getColorModel(); + int[] pixels = GdkGraphics2D.findSimpleIntegerArray (image.getColorModel(), ras); + + if (pixels == null) + { + BufferedImage img = new BufferedImage(width, height, + (model != null && model.hasAlpha() ? + BufferedImage.TYPE_INT_ARGB + : BufferedImage.TYPE_INT_RGB)); + int[] pix = new int[4]; + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) + img.setRGB(x, y, model.getRGB(ras.getPixel(x, y, pix))); + pixels = GdkGraphics2D.findSimpleIntegerArray (img.getColorModel(), + img.getRaster()); + model = img.getColorModel(); + } + + processImageStarted(1); + streamImage(pixels, this.ext, width, height, model.hasAlpha(), + (DataOutput) this.getOutput()); + processImageComplete(); + } + } + + private static class GdkPixbufReader + extends ImageReader + implements ImageConsumer + { + // ImageConsumer parts + GdkPixbufDecoder dec; + BufferedImage bufferedImage; + ColorModel defaultModel; + int width; + int height; + String ext; + + public GdkPixbufReader(GdkPixbufReaderSpi ownerSpi, Object ext) + { + super(ownerSpi); + this.ext = findFormatName(ext, false); + } + + public GdkPixbufReader(GdkPixbufReaderSpi ownerSpi, Object ext, GdkPixbufDecoder d) + { + this(ownerSpi, ext); + dec = d; + } + + public void setDimensions(int w, int h) + { + processImageStarted(1); + width = w; + height = h; + } + + public void setProperties(Hashtable props) {} + + public void setColorModel(ColorModel model) + { + defaultModel = model; + } + + public void setHints(int flags) {} + + public void setPixels(int x, int y, int w, int h, + ColorModel model, byte[] pixels, + int offset, int scansize) + { + } + + public void setPixels(int x, int y, int w, int h, + ColorModel model, int[] pixels, + int offset, int scansize) + { + if (model == null) + model = defaultModel; + + if (bufferedImage == null) + { + bufferedImage = new BufferedImage (width, height, (model != null && model.hasAlpha() ? + BufferedImage.TYPE_INT_ARGB + : BufferedImage.TYPE_INT_RGB)); + } + + int pixels2[]; + if (model != null) + { + pixels2 = new int[pixels.length]; + for (int yy = 0; yy < h; yy++) + for (int xx = 0; xx < w; xx++) + { + int i = yy * scansize + xx; + pixels2[i] = model.getRGB (pixels[i]); + } + } + else + pixels2 = pixels; + + bufferedImage.setRGB (x, y, w, h, pixels2, offset, scansize); + processImageProgress(y / (height == 0 ? 1 : height)); + } + + public void imageComplete(int status) + { + processImageComplete(); + } + + public BufferedImage getBufferedImage() + { + if (bufferedImage == null && dec != null) + dec.startProduction (this); + return bufferedImage; + } + + // ImageReader parts + + public int getNumImages(boolean allowSearch) + throws IOException + { + return 1; + } + + public IIOMetadata getImageMetadata(int i) + { + return null; + } + + public IIOMetadata getStreamMetadata() + throws IOException + { + return null; + } + + public Iterator getImageTypes(int imageIndex) + throws IOException + { + BufferedImage img = getBufferedImage(); + Vector vec = new Vector(); + vec.add(new ImageTypeSpecifier(img)); + return vec.iterator(); + } + + public int getHeight(int imageIndex) + throws IOException + { + return getBufferedImage().getHeight(); + } + + public int getWidth(int imageIndex) + throws IOException + { + return getBufferedImage().getWidth(); + } + + public void setInput(Object input, + boolean seekForwardOnly, + boolean ignoreMetadata) + { + super.setInput(input, seekForwardOnly, ignoreMetadata); + dec = new GdkPixbufDecoder((InputStream) getInput()); + } + + public BufferedImage read(int imageIndex, ImageReadParam param) + throws IOException + { + return getBufferedImage (); + } + } + + // remaining helper class and static method is a convenience for the Gtk + // peers, for loading a BufferedImage in off a disk file without going + // through the whole imageio system. + + public static BufferedImage createBufferedImage (String filename) + { + GdkPixbufReader r = new GdkPixbufReader (getReaderSpi(), + "png", // reader auto-detects, doesn't matter + new GdkPixbufDecoder (filename)); + return r.getBufferedImage (); + } + + public static BufferedImage createBufferedImage (URL u) + { + GdkPixbufReader r = new GdkPixbufReader (getReaderSpi(), + "png", // reader auto-detects, doesn't matter + new GdkPixbufDecoder (u)); + return r.getBufferedImage (); + } + + public static BufferedImage createBufferedImage (byte[] imagedata, int imageoffset, + int imagelength) + { + GdkPixbufReader r = new GdkPixbufReader (getReaderSpi(), + "png", // reader auto-detects, doesn't matter + new GdkPixbufDecoder (imagedata, + imageoffset, + imagelength)); + return r.getBufferedImage (); + } + + public static BufferedImage createBufferedImage (ImageProducer producer) + { + GdkPixbufReader r = new GdkPixbufReader (getReaderSpi(), "png" /* ignored */, null); + producer.startProduction(r); + return r.getBufferedImage (); + } + +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkRobotPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkRobotPeer.java new file mode 100644 index 00000000000..6d0218d057a --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkRobotPeer.java @@ -0,0 +1,94 @@ +/* GdkRobot.java -- an XTest implementation of RobotPeer + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTException; +import java.awt.GraphicsDevice; +import java.awt.Rectangle; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.peer.RobotPeer; + +/** + * Implements the RobotPeer interface using the XTest extension. + * + * @author Thomas Fitzsimmons + */ +public class GdkRobotPeer implements RobotPeer +{ + // gdk-pixbuf provides data in RGBA format + static final ColorModel cm = new DirectColorModel (32, 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff); + + public GdkRobotPeer (GraphicsDevice screen) throws AWTException + { + // FIXME: make use of screen parameter when GraphicsDevice is + // implemented. + if (!initXTest ()) + throw new AWTException ("XTest extension not supported"); + } + + native boolean initXTest (); + + // RobotPeer methods + public native void mouseMove (int x, int y); + public native void mousePress (int buttons); + public native void mouseRelease (int buttons); + public native void mouseWheel (int wheelAmt); + public native void keyPress (int keycode); + public native void keyRelease (int keycode); + native int[] nativeGetRGBPixels (int x, int y, int width, int height); + + public int getRGBPixel (int x, int y) + { + return cm.getRGB (nativeGetRGBPixels (x, y, 1, 1)[0]); + } + + public int[] getRGBPixels (Rectangle r) + { + int[] gdk_pixels = nativeGetRGBPixels (r.x, r.y, r.width, r.height); + int[] pixels = new int[r.width * r.height]; + + for (int i = 0; i < r.width * r.height; i++) + pixels[i] = cm.getRGB (gdk_pixels[i]); + + return pixels; + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice.java new file mode 100644 index 00000000000..2bf9d23db94 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice.java @@ -0,0 +1,115 @@ +/* GdkScreenGraphicsDevice.java -- information about a screen device + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Dimension; +import java.awt.DisplayMode; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; + +public class GdkScreenGraphicsDevice extends GraphicsDevice +{ + GdkGraphicsEnvironment env; + + public GtkToolkit getToolkit() + { + return env.getToolkit(); + } + + public GdkScreenGraphicsDevice (GdkGraphicsEnvironment e) + { + super (); + env = e; + } + + public int getType () + { + return GraphicsDevice.TYPE_RASTER_SCREEN; + } + + public String getIDstring () + { + // FIXME: query X for this string + return "default GDK device ID string"; + } + + public GraphicsConfiguration[] getConfigurations () + { + // FIXME: query X for the list of possible configurations + return new GraphicsConfiguration [] { new GdkGraphicsConfiguration(this) }; + } + + public GraphicsConfiguration getDefaultConfiguration () + { + + // FIXME: query X for default configuration + return new GdkGraphicsConfiguration(this); + } + + + /** + * Returns the current display mode of this device, or null if unknown. + * + * @return the current display mode + * @see #setDisplayMode(DisplayMode) + * @see #getDisplayModes() + * @since 1.4 + */ + public DisplayMode getDisplayMode() + { + // determine display mode + Dimension dim = getToolkit().getScreenSize(); + DisplayMode mode = new DisplayMode(dim.width, dim.height, 0, + DisplayMode.REFRESH_RATE_UNKNOWN); + return mode; + } + + /** + * This device does not yet support fullscreen exclusive mode, so this + * returns <code>false</code>. + * + * @return <code>false</code> + * @since 1.4 + */ + public boolean isFullScreenSupported() + { + return false; + } + +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkTextLayout.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkTextLayout.java new file mode 100644 index 00000000000..ff51745f26c --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkTextLayout.java @@ -0,0 +1,434 @@ +/* GdkTextLayout.java + Copyright (C) 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.classpath.Configuration; +import gnu.java.awt.peer.ClasspathTextLayoutPeer; + +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Shape; +import java.awt.font.FontRenderContext; +import java.awt.font.GlyphMetrics; +import java.awt.font.GlyphVector; +import java.awt.font.TextAttribute; +import java.awt.font.TextHitInfo; +import java.awt.font.TextLayout; +import java.awt.geom.AffineTransform; +import java.awt.geom.GeneralPath; +import java.awt.geom.Rectangle2D; +import java.text.AttributedCharacterIterator; +import java.text.AttributedString; +import java.text.CharacterIterator; + +/** + * This is an implementation of the text layout peer interface which + * delegates all the hard work to pango. + * + * @author Graydon Hoare + */ + +public class GdkTextLayout + implements ClasspathTextLayoutPeer +{ + // native side, plumbing, etc. + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("gtkpeer"); + } + initStaticState (); + } + private native void setText(String str); + private native void getExtents(double[] inkExtents, + double[] logExtents); + private native void indexToPos(int idx, double[] pos); + private native void initState (); + private native void dispose (); + static native void initStaticState(); + private final int native_state = GtkGenericPeer.getUniqueInteger (); + protected void finalize () + { + dispose (); + } + + // we hold on to these to make sure we can render when presented + // with non-GdkGraphics2D paint targets + private AttributedString attributedString; + private FontRenderContext fontRenderContext; + + public GdkTextLayout(AttributedString str, FontRenderContext frc) + { + initState(); + attributedString = str; + fontRenderContext = frc; + } + + protected class CharacterIteratorProxy + implements CharacterIterator + { + public CharacterIterator target; + public int begin; + public int limit; + public int index; + + public CharacterIteratorProxy (CharacterIterator ci) + { + target = ci; + } + + public int getBeginIndex () + { + return begin; + } + + public int getEndIndex () + { + return limit; + } + + public int getIndex () + { + return index; + } + + public char setIndex (int idx) + throws IllegalArgumentException + { + if (idx < begin || idx >= limit) + throw new IllegalArgumentException (); + char ch = target.setIndex (idx); + index = idx; + return ch; + } + + public char first () + { + int save = target.getIndex (); + char ch = target.setIndex (begin); + target.setIndex (save); + return ch; + } + + public char last () + { + if (begin == limit) + return this.first (); + + int save = target.getIndex (); + char ch = target.setIndex (limit - 1); + target.setIndex (save); + return ch; + } + + public char current () + { + return target.current(); + } + + public char next () + { + if (index >= limit - 1) + return CharacterIterator.DONE; + else + { + index++; + return target.next(); + } + } + + public char previous () + { + if (index <= begin) + return CharacterIterator.DONE; + else + { + index--; + return target.previous (); + } + } + + public Object clone () + { + CharacterIteratorProxy cip = new CharacterIteratorProxy (this.target); + cip.begin = this.begin; + cip.limit = this.limit; + cip.index = this.index; + return cip; + } + + } + + + // public side + + public void draw (Graphics2D g2, float x, float y) + { + if (g2 instanceof GdkGraphics2D) + { + // we share pango structures directly with GdkGraphics2D + // when legal + GdkGraphics2D gg2 = (GdkGraphics2D) g2; + gg2.drawGdkTextLayout(this, x, y); + } + else + { + // falling back to a rather tedious layout algorithm when + // not legal + AttributedCharacterIterator ci = attributedString.getIterator (); + CharacterIteratorProxy proxy = new CharacterIteratorProxy (ci); + Font defFont = g2.getFont (); + + /* Note: this implementation currently only interprets FONT text + * attributes. There is a reasonable argument to be made for some + * attributes being interpreted out here, where we have control of the + * Graphics2D and can construct or derive new fonts, and some + * attributes being interpreted by the GlyphVector itself. So far, for + * all attributes except FONT we do neither. + */ + + for (char c = ci.first (); + c != CharacterIterator.DONE; + c = ci.next ()) + { + proxy.begin = ci.getIndex (); + proxy.limit = ci.getRunLimit(TextAttribute.FONT); + if (proxy.limit <= proxy.begin) + continue; + + proxy.index = proxy.begin; + + Object fnt = ci.getAttribute(TextAttribute.FONT); + GlyphVector gv; + if (fnt instanceof Font) + gv = ((Font)fnt).createGlyphVector (fontRenderContext, proxy); + else + gv = defFont.createGlyphVector (fontRenderContext, proxy); + + g2.drawGlyphVector (gv, x, y); + + int n = gv.getNumGlyphs (); + for (int i = 0; i < n; ++i) + { + GlyphMetrics gm = gv.getGlyphMetrics (i); + if (gm.getAdvanceX() == gm.getAdvance ()) + x += gm.getAdvanceX (); + else + y += gm.getAdvanceY (); + } + } + } + } + + public TextHitInfo getStrongCaret (TextHitInfo hit1, + TextHitInfo hit2) + { + throw new Error("not implemented"); + } + + public byte getBaseline () + { + throw new Error("not implemented"); + } + + public boolean isLeftToRight () + { + throw new Error("not implemented"); + } + + public boolean isVertical () + { + throw new Error("not implemented"); + } + + public float getAdvance () + { + throw new Error("not implemented"); + } + + public float getAscent () + { + throw new Error("not implemented"); + } + + public float getDescent () + { + throw new Error("not implemented"); + } + + public float getLeading () + { + throw new Error("not implemented"); + } + + public int getCharacterCount () + { + throw new Error("not implemented"); + } + + public byte getCharacterLevel (int index) + { + throw new Error("not implemented"); + } + + public float[] getBaselineOffsets () + { + throw new Error("not implemented"); + } + + public Shape getBlackBoxBounds (int firstEndpoint, int secondEndpoint) + { + throw new Error("not implemented"); + } + + public Rectangle2D getBounds () + { + double[] inkExtents = new double[4]; + double[] logExtents = new double[4]; + getExtents(inkExtents, logExtents); + return new Rectangle2D.Double(logExtents[0], logExtents[1], + logExtents[2], logExtents[3]); + } + + public float[] getCaretInfo (TextHitInfo hit, Rectangle2D bounds) + { + throw new Error("not implemented"); + } + + public Shape getCaretShape (TextHitInfo hit, Rectangle2D bounds) + { + throw new Error("not implemented"); + } + + public Shape[] getCaretShapes (int offset, Rectangle2D bounds, + TextLayout.CaretPolicy policy) + { + throw new Error("not implemented"); + } + + public Shape getLogicalHighlightShape (int firstEndpoint, int secondEndpoint, + Rectangle2D bounds) + { + AffineTransform at = new AffineTransform(); + GeneralPath gp = new GeneralPath(); + double [] rect = new double[4]; + Rectangle2D tmp = new Rectangle2D.Double(); + for (int i = firstEndpoint; i <= secondEndpoint; ++i) + { + indexToPos(i, rect); + tmp.setRect(rect[0], rect[1], rect[2], rect[3]); + Rectangle2D.intersect(tmp, bounds, tmp); + gp.append(tmp.getPathIterator(at), false); + } + return gp; + } + + public int[] getLogicalRangesForVisualSelection (TextHitInfo firstEndpoint, + TextHitInfo secondEndpoint) + { + throw new Error("not implemented"); + } + + public TextHitInfo getNextLeftHit (int offset, TextLayout.CaretPolicy policy) + { + throw new Error("not implemented"); + } + public TextHitInfo getNextRightHit (int offset, TextLayout.CaretPolicy policy) + { + throw new Error("not implemented"); + } + public TextHitInfo hitTestChar (float x, float y, Rectangle2D bounds) + { + throw new Error("not implemented"); + } + public TextHitInfo getVisualOtherHit (TextHitInfo hit) + { + throw new Error("not implemented"); + } + + public float getVisibleAdvance () + { + throw new Error("not implemented"); + } + + public Shape getOutline (AffineTransform tx) + { + throw new Error("not implemented"); + } + + public Shape getVisualHighlightShape (TextHitInfo firstEndpoint, + TextHitInfo secondEndpoint, + Rectangle2D bounds) + { + throw new Error("not implemented"); + } + + + public TextLayout getJustifiedLayout (float justificationWidth) + { + throw new Error("not implemented"); + } + + public void handleJustify (float justificationWidth) + { + throw new Error("not implemented"); + } + + public Object clone () + { + throw new Error("not implemented"); + } + + public int hashCode () + { + throw new Error("not implemented"); + } + + public boolean equals (ClasspathTextLayoutPeer tl) + { + throw new Error("not implemented"); + } + + public String toString () + { + throw new Error("not implemented"); + } + +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkButtonPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkButtonPeer.java new file mode 100644 index 00000000000..ab5df9f463f --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkButtonPeer.java @@ -0,0 +1,107 @@ +/* GtkButtonPeer.java -- Implements ButtonPeer with GTK + Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTEvent; +import java.awt.Button; +import java.awt.Component; +import java.awt.Point; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.peer.ButtonPeer; + +public class GtkButtonPeer extends GtkComponentPeer + implements ButtonPeer +{ + native void create (String label); + + public native void connectSignals (); + + native void gtkWidgetModifyFont (String name, int style, int size); + native void gtkSetLabel (String label); + native void gtkWidgetSetForeground (int red, int green, int blue); + native void gtkWidgetSetBackground (int red, int green, int blue); + native void gtkActivate (); + native void gtkWidgetRequestFocus (); + native void setNativeBounds (int x, int y, int width, int height); + + public GtkButtonPeer (Button b) + { + super (b); + } + + void create () + { + create (((Button) awtComponent).getLabel ()); + } + + public void setLabel (String label) + { + gtkSetLabel(label); + } + + public void handleEvent (AWTEvent e) + { + if (e.getID () == MouseEvent.MOUSE_RELEASED && isEnabled ()) + { + MouseEvent me = (MouseEvent) e; + Point p = me.getPoint(); + p.translate(((Component) me.getSource()).getX(), + ((Component) me.getSource()).getY()); + if (!me.isConsumed () + && (me.getModifiersEx () & MouseEvent.BUTTON1_DOWN_MASK) != 0 + && awtComponent.getBounds().contains(p)) + postActionEvent (((Button) awtComponent).getActionCommand (), + me.getModifiersEx ()); + } + + if (e.getID () == KeyEvent.KEY_PRESSED) + { + KeyEvent ke = (KeyEvent) e; + if (!ke.isConsumed () && ke.getKeyCode () == KeyEvent.VK_SPACE) + { + postActionEvent (((Button) awtComponent).getActionCommand (), + ke.getModifiersEx ()); + gtkActivate (); + } + } + + super.handleEvent (e); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkCanvasPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCanvasPeer.java new file mode 100644 index 00000000000..dc21761209e --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCanvasPeer.java @@ -0,0 +1,100 @@ +/* GtkCanvasPeer.java + Copyright (C) 1998, 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTEvent; +import java.awt.Canvas; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.event.PaintEvent; +import java.awt.peer.CanvasPeer; + +public class GtkCanvasPeer extends GtkComponentPeer implements CanvasPeer +{ + native void create (); + + public GtkCanvasPeer (Canvas c) + { + super (c); + } + + public Graphics getGraphics () + { + if (GtkToolkit.useGraphics2D ()) + return new GdkGraphics2D (this); + else + return new GdkGraphics (this); + } + + public void handleEvent (AWTEvent event) + { + int id = event.getID(); + + switch (id) + { + case PaintEvent.PAINT: + case PaintEvent.UPDATE: + { + try + { + Graphics g = getGraphics (); + g.setClip (((PaintEvent)event).getUpdateRect()); + + if (id == PaintEvent.PAINT) + awtComponent.paint (g); + else + awtComponent.update (g); + + g.dispose (); + } + catch (InternalError e) + { + System.err.println (e); + } + } + break; + } + } + + /* Preferred size for a drawing widget is always what the user requested */ + public Dimension getPreferredSize () + { + return awtComponent.getSize (); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java new file mode 100644 index 00000000000..46b0733d363 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java @@ -0,0 +1,86 @@ +/* GtkCheckboxGroupPeer.java - Wrap a CheckboxGroup + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.CheckboxGroup; +import java.util.WeakHashMap; + +// Note that there is no peer interface for a CheckboxGroup. We +// introduce our own in order to make it easier to keep a piece of +// native state for each one. +public class GtkCheckboxGroupPeer extends GtkGenericPeer +{ + // This maps from a CheckboxGroup to the native peer. + private static WeakHashMap map = new WeakHashMap (); + + // Find the native peer corresponding to a CheckboxGroup. + public static synchronized GtkCheckboxGroupPeer + getCheckboxGroupPeer (CheckboxGroup group) + { + if (group == null) + return null; + GtkCheckboxGroupPeer nat = (GtkCheckboxGroupPeer) map.get (group); + if (nat == null) + { + nat = new GtkCheckboxGroupPeer (); + map.put (group, nat); + } + return nat; + } + + private GtkCheckboxGroupPeer () + { + // We don't need any special state here. Note that we can't store + // a reference to the java-side CheckboxGroup. That would mean + // they could never be collected. + super (null); + } + + // Dispose of our native resources. + public native void dispose (); + + // Remove a given checkbox from this group. + public native void remove (GtkCheckboxPeer box); + + // When collected, clean up the native state. + protected void finalize () + { + dispose (); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxMenuItemPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxMenuItemPeer.java new file mode 100644 index 00000000000..01a6e3102d9 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxMenuItemPeer.java @@ -0,0 +1,69 @@ +/* GtkCheckboxMenuItemPeer.java -- Implements CheckboxMenuItemPeer with GTK+ + Copyright (C) 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.CheckboxMenuItem; +import java.awt.ItemSelectable; +import java.awt.event.ItemEvent; +import java.awt.peer.CheckboxMenuItemPeer; + +public class GtkCheckboxMenuItemPeer extends GtkMenuItemPeer + implements CheckboxMenuItemPeer +{ + native void create (String label); + + public GtkCheckboxMenuItemPeer (CheckboxMenuItem menu) + { + super (menu); + setState (menu.getState ()); + } + + public native void setState(boolean t); + + protected void postMenuActionEvent () + { + CheckboxMenuItem item = (CheckboxMenuItem)awtWidget; + q().postEvent (new ItemEvent ((ItemSelectable)awtWidget, + ItemEvent.ITEM_STATE_CHANGED, + item.getActionCommand(), + item.getState() ? ItemEvent.DESELECTED : ItemEvent.SELECTED)); + + super.postMenuActionEvent(); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java new file mode 100644 index 00000000000..851757245aa --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java @@ -0,0 +1,128 @@ +/* GtkCheckboxPeer.java -- Implements CheckboxPeer with GTK + Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Checkbox; +import java.awt.CheckboxGroup; +import java.awt.peer.CheckboxPeer; + +public class GtkCheckboxPeer extends GtkComponentPeer + implements CheckboxPeer +{ + // Group from last time it was set. + public GtkCheckboxGroupPeer old_group; + // The current state of the GTK checkbox. + private boolean currentState; + + public native void create (GtkCheckboxGroupPeer group); + public native void nativeSetCheckboxGroup (GtkCheckboxGroupPeer group); + public native void connectSignals (); + native void gtkWidgetModifyFont (String name, int style, int size); + native void gtkButtonSetLabel (String label); + native void gtkToggleButtonSetActive (boolean is_active); + + public GtkCheckboxPeer (Checkbox c) + { + super (c); + } + + // FIXME: we must be able to switch between a checkbutton and a + // radiobutton dynamically. + public void create () + { + Checkbox checkbox = (Checkbox) awtComponent; + CheckboxGroup g = checkbox.getCheckboxGroup (); + old_group = GtkCheckboxGroupPeer.getCheckboxGroupPeer (g); + create (old_group); + gtkToggleButtonSetActive (checkbox.getState ()); + gtkButtonSetLabel (checkbox.getLabel ()); + } + + public void setState (boolean state) + { + if (currentState != state) + gtkToggleButtonSetActive (state); + } + + public void setLabel (String label) + { + gtkButtonSetLabel (label); + } + + public void setCheckboxGroup (CheckboxGroup group) + { + GtkCheckboxGroupPeer gp + = GtkCheckboxGroupPeer.getCheckboxGroupPeer (group); + if (gp != old_group) + { + if (old_group != null) + old_group.remove (this); + nativeSetCheckboxGroup (gp); + old_group = gp; + } + } + + // Override the superclass postItemEvent so that the peer doesn't + // need information that we have. + public void postItemEvent (Object item, int stateChange) + { + Checkbox currentCheckBox = ((Checkbox)awtComponent); + // A firing of the event is only desired if the state has changed due to a + // button press. The currentCheckBox's state must be different from the + // one that the stateChange is changing to. + // stateChange = 1 if it goes from false -> true + // stateChange = 2 if it goes from true -> false + if (( !currentCheckBox.getState() && stateChange == 1) + || (currentCheckBox.getState() && stateChange == 2)) + { + super.postItemEvent (awtComponent, stateChange); + currentState = !currentCheckBox.getState(); + currentCheckBox.setState(currentState); + } + } + + public void dispose () + { + // Notify the group so that the native state can be cleaned up + // appropriately. + if (old_group != null) + old_group.remove (this); + super.dispose (); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkChoicePeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkChoicePeer.java new file mode 100644 index 00000000000..5028ea779c7 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkChoicePeer.java @@ -0,0 +1,130 @@ +/* GtkChoicePeer.java -- Implements ChoicePeer with GTK + Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Choice; +import java.awt.event.ItemEvent; +import java.awt.peer.ChoicePeer; + +public class GtkChoicePeer extends GtkComponentPeer + implements ChoicePeer +{ + public GtkChoicePeer (Choice c) + { + super (c); + + int count = c.getItemCount (); + if (count > 0) + { + String items[] = new String[count]; + for (int i = 0; i < count; i++) + items[i] = c.getItem (i); + + append (items); + } + + int selected = c.getSelectedIndex(); + if (selected >= 0) + select(selected); + } + + native void create (); + + native void append (String items[]); + native int nativeGetSelected (); + native void nativeAdd (String item, int index); + native void nativeRemove (int index); + native void nativeRemoveAll (); + + native void connectSignals (); + + public native void select (int position); + + public void add (String item, int index) + { + int before = nativeGetSelected(); + + nativeAdd (item, index); + + /* Generate an ItemEvent if we added the first one or + if we inserted at or before the currently selected item. */ + if ((before < 0) || (before >= index)) + { + // Must set our state before notifying listeners + ((Choice) awtComponent).select (((Choice) awtComponent).getItem (0)); + postItemEvent (((Choice) awtComponent).getItem (0), ItemEvent.SELECTED); + } + } + + public void remove (int index) + { + int before = nativeGetSelected(); + int after; + + nativeRemove (index); + after = nativeGetSelected(); + + /* Generate an ItemEvent if we are removing the currently selected item + and there are at least one item left. */ + if ((before == index) && (after >= 0)) + { + // Must set our state before notifying listeners + ((Choice) awtComponent).select (((Choice) awtComponent).getItem (0)); + postItemEvent (((Choice) awtComponent).getItem (0), ItemEvent.SELECTED); + } + } + + public void removeAll () + { + nativeRemoveAll(); + } + + public void addItem (String item, int position) + { + add (item, position); + } + + protected void postChoiceItemEvent (String label, int stateChange) + { + // Must set our state before notifying listeners + if (stateChange == ItemEvent.SELECTED) + ((Choice) awtComponent).select (label); + postItemEvent (label, stateChange); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkClipboard.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkClipboard.java new file mode 100644 index 00000000000..c719cddb575 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkClipboard.java @@ -0,0 +1,170 @@ +/* GtkClipboard.java + Copyright (C) 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.ClipboardOwner; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class GtkClipboard extends Clipboard +{ + /* the number of milliseconds that we'll wait around for the + owner of the GDK_SELECTION_PRIMARY selection to convert + the requested data */ + static final int SELECTION_RECEIVED_TIMEOUT = 5000; + + /* We currently only support transferring of text between applications */ + static String selection; + static Object selectionLock = new Object (); + + static boolean hasSelection = false; + + protected GtkClipboard() + { + super("System Clipboard"); + initNativeState(); + } + + public Transferable getContents(Object requestor) + { + synchronized (this) + { + if (hasSelection) + return contents; + } + + /* Java doesn't own the selection, so we need to ask X11 */ + // XXX: Does this hold with Swing too ? + synchronized (selectionLock) + { + requestStringConversion(); + + try + { + selectionLock.wait(SELECTION_RECEIVED_TIMEOUT); + } + catch (InterruptedException e) + { + return null; + } + + return selection == null ? null : new StringSelection(selection); + } + } + + void stringSelectionReceived(String newSelection) + { + synchronized (selectionLock) + { + selection = newSelection; + selectionLock.notify(); + } + } + + /* convert Java clipboard data into a String suitable for sending + to another application */ + synchronized String stringSelectionHandler() throws IOException + { + String selection = null; + + try + { + if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) + selection = (String)contents.getTransferData(DataFlavor.stringFlavor); + else if (contents.isDataFlavorSupported(DataFlavor.plainTextFlavor)) + { + StringBuffer sbuf = new StringBuffer(); + InputStreamReader reader; + char readBuf[] = new char[512]; + int numChars; + + reader = new InputStreamReader + ((InputStream) + contents.getTransferData(DataFlavor.plainTextFlavor), "UNICODE"); + + while (true) + { + numChars = reader.read(readBuf); + if (numChars == -1) + break; + sbuf.append(readBuf, 0, numChars); + } + + selection = new String(sbuf); + } + } + catch (Exception e) + { + } + + return selection; + } + + public synchronized void setContents(Transferable contents, + ClipboardOwner owner) + { + selectionGet(); + + this.contents = contents; + this.owner = owner; + + hasSelection = true; + } + + synchronized void selectionClear() + { + hasSelection = false; + + if (owner != null) + { + owner.lostOwnership(this, contents); + owner = null; + contents = null; + } + } + + native void initNativeState(); + static native void requestStringConversion(); + static native void selectionGet(); +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkComponentPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkComponentPeer.java new file mode 100644 index 00000000000..1578a9cfc3c --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkComponentPeer.java @@ -0,0 +1,651 @@ +/* GtkComponentPeer.java -- Implements ComponentPeer with GTK + Copyright (C) 1998, 1999, 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTEvent; +import java.awt.AWTException; +import java.awt.BufferCapabilities; +import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import java.awt.Cursor; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; +import java.awt.Image; +import java.awt.Insets; +import java.awt.ItemSelectable; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.Toolkit; +import java.awt.Window; +import java.awt.event.FocusEvent; +import java.awt.event.ItemEvent; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.event.PaintEvent; +import java.awt.event.TextEvent; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; +import java.awt.image.VolatileImage; +import java.awt.peer.ComponentPeer; + +public class GtkComponentPeer extends GtkGenericPeer + implements ComponentPeer +{ + VolatileImage backBuffer; + BufferCapabilities caps; + + Component awtComponent; + + Insets insets; + + boolean isInRepaint; + + /* this isEnabled differs from Component.isEnabled, in that it + knows if a parent is disabled. In that case Component.isEnabled + may return true, but our isEnabled will always return false */ + native boolean isEnabled (); + static native boolean modalHasGrab(); + + native int[] gtkWidgetGetForeground (); + native int[] gtkWidgetGetBackground (); + native void gtkWidgetGetDimensions (int[] dim); + native void gtkWidgetGetPreferredDimensions (int[] dim); + native void gtkWidgetGetLocationOnScreen (int[] point); + native void gtkWidgetSetCursor (int type); + native void gtkWidgetSetBackground (int red, int green, int blue); + native void gtkWidgetSetForeground (int red, int green, int blue); + native void gtkWidgetSetSensitive (boolean sensitive); + native void gtkWidgetSetParent (ComponentPeer parent); + native void gtkWidgetRequestFocus (); + native void gtkWidgetDispatchKeyEvent (int id, long when, int mods, + int keyCode, int keyLocation); + + native boolean isRealized (); + + void realize () + { + // Default implementation does nothing + } + + native void setNativeEventMask (); + + void create () + { + throw new RuntimeException (); + } + + native void connectSignals (); + + protected GtkComponentPeer (Component awtComponent) + { + super (awtComponent); + this.awtComponent = awtComponent; + insets = new Insets (0, 0, 0, 0); + + create (); + + connectSignals (); + + if (awtComponent.getForeground () != null) + setForeground (awtComponent.getForeground ()); + if (awtComponent.getBackground () != null) + setBackground (awtComponent.getBackground ()); + if (awtComponent.getFont() != null) + setFont(awtComponent.getFont()); + + Component parent = awtComponent.getParent (); + + // Only set our parent on the GTK side if our parent on the AWT + // side is not showing. Otherwise the gtk peer will be shown + // before we've had a chance to position and size it properly. + if (awtComponent instanceof Window + || (parent != null && ! parent.isShowing ())) + setParentAndBounds (); + + setNativeEventMask (); + + realize (); + } + + void setParentAndBounds () + { + setParent (); + + setComponentBounds (); + + setVisibleAndEnabled (); + } + + void setParent () + { + ComponentPeer p; + Component component = awtComponent; + do + { + component = component.getParent (); + p = component.getPeer (); + } + while (p instanceof java.awt.peer.LightweightPeer); + + if (p != null) + gtkWidgetSetParent (p); + } + + void beginNativeRepaint () + { + isInRepaint = true; + } + + void endNativeRepaint () + { + isInRepaint = false; + } + + /* + * Set the bounds of this peer's AWT Component based on dimensions + * returned by the native windowing system. Most Components impose + * their dimensions on the peers which is what the default + * implementation does. However some peers, like GtkFileDialogPeer, + * need to pass their size back to the AWT Component. + */ + void setComponentBounds () + { + Rectangle bounds = awtComponent.getBounds (); + + if (bounds.x == 0 && bounds.y == 0 + && bounds.width == 0 && bounds.height == 0) + return; + + setBounds (bounds.x, bounds.y, bounds.width, bounds.height); + } + + void setVisibleAndEnabled () + { + setVisible (awtComponent.isVisible ()); + setEnabled (awtComponent.isEnabled ()); + } + + public int checkImage (Image image, int width, int height, + ImageObserver observer) + { + return getToolkit().checkImage(image, width, height, observer); + } + + public Image createImage (ImageProducer producer) + { + return new GtkImage (producer); + } + + public Image createImage (int width, int height) + { + Image image; + if (GtkToolkit.useGraphics2D ()) + image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); + else + image = new GtkImage (width, height); + + Graphics g = image.getGraphics(); + g.setColor(getBackground()); + g.fillRect(0, 0, width, height); + return image; + } + + public void disable () + { + setEnabled (false); + } + + public void enable () + { + setEnabled (true); + } + + public ColorModel getColorModel () + { + return ColorModel.getRGBdefault (); + } + + public FontMetrics getFontMetrics (Font font) + { + return getToolkit().getFontMetrics(font); + } + + public Graphics getGraphics () + { + if (GtkToolkit.useGraphics2D ()) + return new GdkGraphics2D (this); + else + return new GdkGraphics (this); + } + + public Point getLocationOnScreen () + { + int point[] = new int[2]; + gtkWidgetGetLocationOnScreen (point); + return new Point (point[0], point[1]); + } + + public Dimension getMinimumSize () + { + return minimumSize (); + } + + public Dimension getPreferredSize () + { + return preferredSize (); + } + + public Toolkit getToolkit () + { + return Toolkit.getDefaultToolkit(); + } + + public void handleEvent (AWTEvent event) + { + int id = event.getID(); + KeyEvent ke = null; + + switch (id) + { + case PaintEvent.PAINT: + case PaintEvent.UPDATE: + { + try + { + Graphics g = getGraphics (); + + // Some peers like GtkFileDialogPeer are repainted by Gtk itself + if (g == null) + break; + + g.setClip (((PaintEvent) event).getUpdateRect()); + + if (id == PaintEvent.PAINT) + awtComponent.paint (g); + else + awtComponent.update (g); + + g.dispose (); + } + catch (InternalError e) + { + System.err.println (e); + } + } + break; + case KeyEvent.KEY_PRESSED: + ke = (KeyEvent) event; + gtkWidgetDispatchKeyEvent (ke.getID (), ke.getWhen (), ke.getModifiersEx (), + ke.getKeyCode (), ke.getKeyLocation ()); + break; + case KeyEvent.KEY_RELEASED: + ke = (KeyEvent) event; + gtkWidgetDispatchKeyEvent (ke.getID (), ke.getWhen (), ke.getModifiersEx (), + ke.getKeyCode (), ke.getKeyLocation ()); + break; + } + } + + public boolean isFocusTraversable () + { + return true; + } + + public Dimension minimumSize () + { + int dim[] = new int[2]; + + gtkWidgetGetPreferredDimensions (dim); + + return new Dimension (dim[0], dim[1]); + } + + public void paint (Graphics g) + { + } + + public Dimension preferredSize () + { + int dim[] = new int[2]; + + gtkWidgetGetPreferredDimensions (dim); + + return new Dimension (dim[0], dim[1]); + } + + public boolean prepareImage (Image image, int width, int height, + ImageObserver observer) + { + return getToolkit().prepareImage(image, width, height, observer); + } + + public void print (Graphics g) + { + throw new RuntimeException (); + } + + public void repaint (long tm, int x, int y, int width, int height) + { + if (x == 0 && y == 0 && width == 0 && height == 0) + return; + + q().postEvent (new PaintEvent (awtComponent, PaintEvent.UPDATE, + new Rectangle (x, y, width, height))); + } + + public void requestFocus () + { + gtkWidgetRequestFocus(); + postFocusEvent(FocusEvent.FOCUS_GAINED, false); + } + + public void reshape (int x, int y, int width, int height) + { + setBounds (x, y, width, height); + } + + public void setBackground (Color c) + { + gtkWidgetSetBackground (c.getRed(), c.getGreen(), c.getBlue()); + } + + native void setNativeBounds (int x, int y, int width, int height); + + public void setBounds (int x, int y, int width, int height) + { + Component parent = awtComponent.getParent (); + + // Heavyweight components that are children of one or more + // lightweight containers have to be handled specially. Because + // calls to GLightweightPeer.setBounds do nothing, GTK has no + // knowledge of the lightweight containers' positions. So we have + // to add the offsets manually when placing a heavyweight + // component within a lightweight container. The lightweight + // container may itself be in a lightweight container and so on, + // so we need to continue adding offsets until we reach a + // container whose position GTK knows -- that is, the first + // non-lightweight. + boolean lightweightChild = false; + Insets i; + while (parent.isLightweight ()) + { + lightweightChild = true; + + i = ((Container) parent).getInsets (); + + x += parent.getX () + i.left; + y += parent.getY () + i.top; + + parent = parent.getParent (); + } + + // We only need to convert from Java to GTK coordinates if we're + // placing a heavyweight component in a Window. + if (parent instanceof Window && !lightweightChild) + { + Insets insets = ((Window) parent).getInsets (); + GtkWindowPeer peer = (GtkWindowPeer) parent.getPeer (); + int menuBarHeight = 0; + if (peer instanceof GtkFramePeer) + menuBarHeight = ((GtkFramePeer) peer).getMenuBarHeight (); + + // Convert from Java coordinates to GTK coordinates. + setNativeBounds (x - insets.left, y - insets.top + menuBarHeight, + width, height); + } + else + setNativeBounds (x, y, width, height); + } + + void setCursor () + { + setCursor (awtComponent.getCursor ()); + } + + public void setCursor (Cursor cursor) + { + gtkWidgetSetCursor (cursor.getType ()); + } + + public void setEnabled (boolean b) + { + gtkWidgetSetSensitive (b); + } + + public void setFont (Font f) + { + // FIXME: This should really affect the widget tree below me. + // Currently this is only handled if the call is made directly on + // a text widget, which implements setFont() itself. + gtkWidgetModifyFont(f.getName(), f.getStyle(), f.getSize()); + } + + public void setForeground (Color c) + { + gtkWidgetSetForeground (c.getRed(), c.getGreen(), c.getBlue()); + } + + public Color getForeground () + { + int rgb[] = gtkWidgetGetForeground (); + return new Color (rgb[0], rgb[1], rgb[2]); + } + + public Color getBackground () + { + int rgb[] = gtkWidgetGetBackground (); + return new Color (rgb[0], rgb[1], rgb[2]); + } + + public void setVisible (boolean b) + { + if (b) + show (); + else + hide (); + } + + public native void hide (); + public native void show (); + + protected void postMouseEvent(int id, long when, int mods, int x, int y, + int clickCount, boolean popupTrigger) + { + q().postEvent(new MouseEvent(awtComponent, id, when, mods, x, y, + clickCount, popupTrigger)); + } + + protected void postExposeEvent (int x, int y, int width, int height) + { + if (!isInRepaint) + q().postEvent (new PaintEvent (awtComponent, PaintEvent.PAINT, + new Rectangle (x, y, width, height))); + } + + protected void postKeyEvent (int id, long when, int mods, + int keyCode, char keyChar, int keyLocation) + { + KeyEvent keyEvent = new KeyEvent (awtComponent, id, when, mods, + keyCode, keyChar, keyLocation); + + // Also post a KEY_TYPED event if keyEvent is a key press that + // doesn't represent an action or modifier key. + if (keyEvent.getID () == KeyEvent.KEY_PRESSED + && (!keyEvent.isActionKey () + && keyCode != KeyEvent.VK_SHIFT + && keyCode != KeyEvent.VK_CONTROL + && keyCode != KeyEvent.VK_ALT)) + { + synchronized (q) + { + q().postEvent (keyEvent); + q().postEvent (new KeyEvent (awtComponent, KeyEvent.KEY_TYPED, when, mods, + KeyEvent.VK_UNDEFINED, keyChar, keyLocation)); + } + } + else + q().postEvent (keyEvent); + } + + protected void postFocusEvent (int id, boolean temporary) + { + q().postEvent (new FocusEvent (awtComponent, id, temporary)); + } + + protected void postItemEvent (Object item, int stateChange) + { + q().postEvent (new ItemEvent ((ItemSelectable)awtComponent, + ItemEvent.ITEM_STATE_CHANGED, + item, stateChange)); + } + + protected void postTextEvent () + { + q().postEvent (new TextEvent (awtComponent, TextEvent.TEXT_VALUE_CHANGED)); + } + + public GraphicsConfiguration getGraphicsConfiguration () + { + // FIXME: just a stub for now. + return null; + } + + public void setEventMask (long mask) + { + // FIXME: just a stub for now. + } + + public boolean isFocusable () + { + return false; + } + + public boolean requestFocus (Component source, boolean b1, + boolean b2, long x) + { + return false; + } + + public boolean isObscured () + { + return false; + } + + public boolean canDetermineObscurity () + { + return false; + } + + public void coalescePaintEvent (PaintEvent e) + { + + } + + public void updateCursorImmediately () + { + + } + + public boolean handlesWheelScrolling () + { + return false; + } + + // Convenience method to create a new volatile image on the screen + // on which this component is displayed. + public VolatileImage createVolatileImage (int width, int height) + { + return new GtkVolatileImage (width, height); + } + + // Creates buffers used in a buffering strategy. + public void createBuffers (int numBuffers, BufferCapabilities caps) + throws AWTException + { + // numBuffers == 2 implies double-buffering, meaning one back + // buffer and one front buffer. + if (numBuffers == 2) + backBuffer = new GtkVolatileImage(awtComponent.getWidth(), + awtComponent.getHeight(), + caps.getBackBufferCapabilities()); + else + throw new AWTException("GtkComponentPeer.createBuffers:" + + " multi-buffering not supported"); + this.caps = caps; + } + + // Return the back buffer. + public Image getBackBuffer () + { + return backBuffer; + } + + // FIXME: flip should be implemented as a fast native operation + public void flip (BufferCapabilities.FlipContents contents) + { + getGraphics().drawImage(backBuffer, + awtComponent.getWidth(), + awtComponent.getHeight(), + null); + + // create new back buffer and clear it to the background color. + if (contents == BufferCapabilities.FlipContents.BACKGROUND) + { + backBuffer = createVolatileImage(awtComponent.getWidth(), + awtComponent.getHeight()); + backBuffer.getGraphics().clearRect(0, 0, + awtComponent.getWidth(), + awtComponent.getHeight()); + } + // FIXME: support BufferCapabilities.FlipContents.PRIOR + } + + // Release the resources allocated to back buffers. + public void destroyBuffers () + { + backBuffer.flush(); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkContainerPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkContainerPeer.java new file mode 100644 index 00000000000..c2865f70f61 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkContainerPeer.java @@ -0,0 +1,156 @@ +/* GtkContainerPeer.java -- Implements ContainerPeer with GTK + Copyright (C) 1998, 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Insets; +import java.awt.Window; +import java.awt.peer.ComponentPeer; +import java.awt.peer.ContainerPeer; + +public class GtkContainerPeer extends GtkComponentPeer + implements ContainerPeer +{ + Container c; + boolean isValidating; + + public GtkContainerPeer(Container c) + { + super (c); + this.c = c; + } + + public void beginValidate () + { + isValidating = true; + } + + public void endValidate () + { + Component parent = awtComponent.getParent (); + + // Only set our parent on the GTK side if our parent on the AWT + // side is not showing. Otherwise the gtk peer will be shown + // before we've had a chance to position and size it properly. + if (parent != null && parent.isShowing ()) + { + Component[] components = ((Container) awtComponent).getComponents (); + int ncomponents = components.length; + + for (int i = 0; i < ncomponents; i++) + { + ComponentPeer peer = components[i].getPeer (); + + // Skip lightweight peers. + if (peer instanceof GtkComponentPeer) + ((GtkComponentPeer) peer).setParentAndBounds (); + } + + // GTK windows don't have parents. + if (!(awtComponent instanceof Window)) + setParentAndBounds (); + } + + isValidating = false; + } + + public Insets getInsets() + { + return insets; + } + + public Insets insets() + { + return getInsets (); + } + + public void setBounds (int x, int y, int width, int height) + { + super.setBounds (x, y, width, height); + } + + public void setFont(Font f) + { + super.setFont(f); + Component[] components = ((Container) awtComponent).getComponents(); + for (int i = 0; i < components.length; i++) + { + if (components[i].isLightweight ()) + components[i].setFont (f); + else + { + GtkComponentPeer peer = (GtkComponentPeer) components[i].getPeer(); + if (peer != null && ! peer.awtComponent.isFontSet()) + peer.setFont(f); + } + } + } + + public Graphics getGraphics () + { + return super.getGraphics(); + } + + public void beginLayout () { } + public void endLayout () { } + public boolean isPaintPending () { return false; } + + public void setBackground (Color c) + { + super.setBackground(c); + + Object components[] = ((Container) awtComponent).getComponents(); + for (int i = 0; i < components.length; i++) + { + Component comp = (Component) components[i]; + + // If the child's background has not been explicitly set yet, + // it should inherit this container's background. This makes the + // child component appear as if it has a transparent background. + // Note that we do not alter the background property of the child, + // but only repaint the child with the parent's background color. + if (!comp.isBackgroundSet() && comp.getPeer() != null) + comp.getPeer().setBackground(c); + } + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkDialogPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkDialogPeer.java new file mode 100644 index 00000000000..c2cbc37dce9 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkDialogPeer.java @@ -0,0 +1,94 @@ +/* GtkDialogPeer.java -- Implements DialogPeer with GTK + Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Dialog; +import java.awt.Graphics; +import java.awt.Rectangle; +import java.awt.event.PaintEvent; +import java.awt.peer.DialogPeer; + +public class GtkDialogPeer extends GtkWindowPeer + implements DialogPeer +{ + public GtkDialogPeer (Dialog dialog) + { + super (dialog); + } + + public Graphics getGraphics () + { + Graphics g; + if (GtkToolkit.useGraphics2D ()) + g = new GdkGraphics2D (this); + else + g = new GdkGraphics (this); + g.translate (-insets.left, -insets.top); + return g; + } + + protected void postMouseEvent(int id, long when, int mods, int x, int y, + int clickCount, boolean popupTrigger) + { + super.postMouseEvent (id, when, mods, + x + insets.left, y + insets.top, + clickCount, popupTrigger); + } + + protected void postExposeEvent (int x, int y, int width, int height) + { + if (!isInRepaint) + q().postEvent (new PaintEvent (awtComponent, PaintEvent.PAINT, + new Rectangle (x + insets.left, + y + insets.top, + width, height))); + } + + void create () + { + // Create a decorated dialog window. + create (GDK_WINDOW_TYPE_HINT_DIALOG, true); + + Dialog dialog = (Dialog) awtComponent; + + gtkWindowSetModal (dialog.isModal ()); + setTitle (dialog.getTitle ()); + setResizable (dialog.isResizable ()); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkEmbeddedWindowPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkEmbeddedWindowPeer.java new file mode 100644 index 00000000000..0533d2759c2 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkEmbeddedWindowPeer.java @@ -0,0 +1,73 @@ +/* GtkEmbeddedWindowPeer.java -- Implements EmbeddedWindowPeer using a + GtkPlug + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.java.awt.EmbeddedWindow; +import gnu.java.awt.peer.EmbeddedWindowPeer; + +public class GtkEmbeddedWindowPeer extends GtkFramePeer + implements EmbeddedWindowPeer +{ + native void create (long socket_id); + + void create () + { + create (((EmbeddedWindow) awtComponent).getHandle ()); + } + + native void construct (long socket_id); + + // FIXME: embed doesn't work right now, though I believe it should. + // This means that you can't call setVisible (true) on an + // EmbeddedWindow before calling setHandle with a valid handle. The + // problem is that somewhere after the call to + // GtkEmbeddedWindow.create and before the call to + // GtkEmbeddedWindow.construct, the GtkPlug peer is being realized. + // GtkSocket silently fails to embed an already-realized GtkPlug. + public void embed (long handle) + { + construct (handle); + } + + public GtkEmbeddedWindowPeer (EmbeddedWindow w) + { + super (w); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkFileDialogPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkFileDialogPeer.java new file mode 100644 index 00000000000..bd1f0775a92 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkFileDialogPeer.java @@ -0,0 +1,219 @@ +/* GtkFileDialogPeer.java -- Implements FileDialogPeer with GTK + Copyright (C) 1998, 1999, 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Dialog; +import java.awt.FileDialog; +import java.awt.Graphics; +import java.awt.Window; +import java.awt.peer.FileDialogPeer; +import java.io.File; +import java.io.FilenameFilter; + +public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer +{ + static final String FS = System.getProperty("file.separator"); + + private String currentFile = null; + private String currentDirectory = null; + private FilenameFilter filter; + + native void create (GtkContainerPeer parent); + native void connectSignals (); + native void nativeSetFile (String file); + public native String nativeGetDirectory(); + public native void nativeSetDirectory(String directory); + native void nativeSetFilenameFilter (FilenameFilter filter); + + public void create() + { + create((GtkContainerPeer) awtComponent.getParent().getPeer()); + + FileDialog fd = (FileDialog) awtComponent; + + setDirectory(fd.getDirectory()); + setFile(fd.getFile()); + + FilenameFilter filter = fd.getFilenameFilter(); + if (filter != null) + setFilenameFilter(filter); + } + + public GtkFileDialogPeer (FileDialog fd) + { + super (fd); + } + + void setComponentBounds () + { + if (awtComponent.getHeight () == 0 + && awtComponent.getWidth () == 0) + { + int[] dims = new int[2]; + gtkWidgetGetPreferredDimensions (dims); + ((GtkFileDialogPeer) this).setBoundsCallback ((Window) awtComponent, + awtComponent.getX (), + awtComponent.getY (), + dims[0], dims[1]); + } + super.setComponentBounds (); + } + + public void setFile (String fileName) + { + /* If nothing changed do nothing. This usually happens because + the only way we have to set the file name in FileDialog is by + calling its SetFile which will call us back. */ + if ((fileName == null && currentFile == null) + || (fileName != null && fileName.equals (currentFile))) + return; + + if (fileName == null || fileName.equals ("")) + { + currentFile = ""; + nativeSetFile (""); + return; + } + + // GtkFileChooser requires absolute filenames. If the given filename + // is not absolute, let's construct it based on current directory. + currentFile = fileName; + if (fileName.indexOf(FS) == 0) + { + nativeSetFile (fileName); + } + else + { + nativeSetFile (nativeGetDirectory() + FS + fileName); + } + } + + public void setDirectory (String directory) + { + /* If nothing changed so nothing. This usually happens because + the only way we have to set the directory in FileDialog is by + calling its setDirectory which will call us back. */ + if ((directory == null && currentDirectory == null) + || (directory != null && directory.equals (currentDirectory))) + return; + + if (directory == null || directory.equals ("")) + { + currentDirectory = FS; + nativeSetFile (FS); + return; + } + + currentDirectory = directory; + nativeSetDirectory (directory); + } + + public void setFilenameFilter (FilenameFilter filter) + { + this.filter = filter; + nativeSetFilenameFilter(filter); + } + + /* This method interacts with the native callback function of the + same name. The native function will extract the filename from the + GtkFileFilterInfo object and send it to this method, which will + in turn call the filter's accept() method and give back the return + value. */ + boolean filenameFilterCallback (String fullname) { + String filename = fullname.substring(fullname.lastIndexOf(FS) + 1); + String dirname = fullname.substring(0, fullname.lastIndexOf(FS)); + File dir = new File(dirname); + return filter.accept(dir, filename); + } + + public Graphics getGraphics () + { + // GtkFileDialog will repaint by itself + return null; + } + + void gtkHideFileDialog () + { + ((Dialog) awtComponent).hide(); + } + + void gtkDisposeFileDialog () + { + ((Dialog) awtComponent).dispose(); + } + + /* Callback to set the file and directory values when the user is finished + * with the dialog. + */ + void gtkSetFilename (String fileName) + { + FileDialog fd = (FileDialog) awtWidget; + if (fileName == null) + { + currentFile = null; + fd.setFile(null); + return; + } + + int sepIndex = fileName.lastIndexOf (FS); + if (sepIndex < 0) + { + /* This should never happen on Unix (all paths start with '/') */ + currentFile = fileName; + } + else + { + if (fileName.length() > (sepIndex + 1)) + { + String fn = fileName.substring (sepIndex + 1); + currentFile = fn; + } + else + { + currentFile = null; + } + + String dn = fileName.substring (0, sepIndex + 1); + currentDirectory = dn; + fd.setDirectory(dn); + } + + fd.setFile (currentFile); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkFontPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkFontPeer.java new file mode 100644 index 00000000000..80ad1580362 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkFontPeer.java @@ -0,0 +1,225 @@ +/* GtkFontPeer.java -- Implements FontPeer with GTK+ + Copyright (C) 1999, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.java.awt.peer.ClasspathFontPeer; + +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.font.FontRenderContext; +import java.awt.font.GlyphVector; +import java.awt.font.LineMetrics; +import java.awt.geom.Rectangle2D; +import java.text.CharacterIterator; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +public class GtkFontPeer extends ClasspathFontPeer +{ + private static ResourceBundle bundle; + + static + { + try + { + bundle = ResourceBundle.getBundle ("gnu.java.awt.peer.gtk.font"); + } + catch (Throwable ignored) + { + bundle = null; + } + } + + private final String Xname; + + public GtkFontPeer (String name, int style) + { + // All fonts get a default size of 12 if size is not specified. + this(name, style, 12); + } + + public GtkFontPeer (String name, int style, int size) + { + super(name, style, size); + + String Xname = null; + if (bundle != null) + { + try + { + Xname = bundle.getString (name.toLowerCase () + "." + style); + } + catch (MissingResourceException mre) + { + // ignored + } + } + + if (Xname == null) + { + String weight; + String slant; + String spacing; + + if (style == Font.ITALIC || (style == (Font.BOLD+Font.ITALIC))) + slant = "i"; + else + slant = "r"; + if (style == Font.BOLD || (style == (Font.BOLD+Font.ITALIC))) + weight = "bold"; + else + weight = "medium"; + if (name.equals("Serif") || name.equals("SansSerif") + || name.equals("Helvetica") || name.equals("Times")) + spacing = "p"; + else + spacing = "c"; + + Xname = "-*-*-" + weight + "-" + slant + "-normal-*-*-" + size + "-*-*-" + spacing + "-*-*-*"; + } + + this.Xname = Xname; + } + + public String getXLFD () + { + return Xname; + } + + + /* remaining methods are for static compatibility with the newer + ClasspathFontPeer superclass; none of these methods ever existed or + worked on the older FontPeer interface, but we need to pretend to + support them anyways. */ + + public boolean canDisplay (Font font, char c) + { + throw new UnsupportedOperationException(); + } + + public int canDisplayUpTo (Font font, CharacterIterator i, int start, int limit) + { + throw new UnsupportedOperationException(); + } + + public String getSubFamilyName (Font font, Locale locale) + { + throw new UnsupportedOperationException(); + } + + public String getPostScriptName (Font font) + { + throw new UnsupportedOperationException(); + } + + public int getNumGlyphs (Font font) + { + throw new UnsupportedOperationException(); + } + + public int getMissingGlyphCode (Font font) + { + throw new UnsupportedOperationException(); + } + + public byte getBaselineFor (Font font, char c) + { + throw new UnsupportedOperationException(); + } + + public String getGlyphName (Font font, int glyphIndex) + { + throw new UnsupportedOperationException(); + } + + public GlyphVector createGlyphVector (Font font, + FontRenderContext frc, + CharacterIterator ci) + { + throw new UnsupportedOperationException(); + } + + public GlyphVector createGlyphVector (Font font, + FontRenderContext ctx, + int[] glyphCodes) + { + throw new UnsupportedOperationException(); + } + + public GlyphVector layoutGlyphVector (Font font, + FontRenderContext frc, + char[] chars, int start, + int limit, int flags) + { + throw new UnsupportedOperationException(); + } + + public FontMetrics getFontMetrics (Font font) + { + throw new UnsupportedOperationException(); + } + + public boolean hasUniformLineMetrics (Font font) + { + throw new UnsupportedOperationException(); + } + + public LineMetrics getLineMetrics (Font font, + CharacterIterator ci, + int begin, int limit, + FontRenderContext rc) + { + throw new UnsupportedOperationException(); + } + + public Rectangle2D getMaxCharBounds (Font font, + FontRenderContext rc) + { + throw new UnsupportedOperationException(); + } + + public Rectangle2D getStringBounds (Font font, + CharacterIterator ci, + int begin, int limit, + FontRenderContext frc) + { + throw new UnsupportedOperationException(); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkFramePeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkFramePeer.java new file mode 100644 index 00000000000..b242d66adcb --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkFramePeer.java @@ -0,0 +1,256 @@ +/* GtkFramePeer.java -- Implements FramePeer with GTK + Copyright (C) 1999, 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.MenuBar; +import java.awt.Rectangle; +import java.awt.Window; +import java.awt.event.PaintEvent; +import java.awt.image.ColorModel; +import java.awt.peer.FramePeer; +import java.awt.peer.MenuBarPeer; + +public class GtkFramePeer extends GtkWindowPeer + implements FramePeer +{ + private int menuBarHeight; + private MenuBarPeer menuBar; + native int getMenuBarHeight (MenuBarPeer bar); + native void setMenuBarWidth (MenuBarPeer bar, int width); + native void setMenuBarPeer (MenuBarPeer bar); + native void removeMenuBarPeer (); + native void gtkFixedSetVisible (boolean visible); + + int getMenuBarHeight () + { + return menuBar == null ? 0 : getMenuBarHeight (menuBar); + } + + public void setMenuBar (MenuBar bar) + { + if (bar == null && menuBar != null) + { + // We're removing the menubar. + gtkFixedSetVisible (false); + menuBar = null; + removeMenuBarPeer (); + insets.top -= menuBarHeight; + menuBarHeight = 0; + awtComponent.validate (); + gtkFixedSetVisible (true); + } + else if (bar != null && menuBar == null) + { + // We're adding a menubar where there was no menubar before. + gtkFixedSetVisible (false); + menuBar = (MenuBarPeer) ((MenuBar) bar).getPeer(); + setMenuBarPeer (menuBar); + int menuBarWidth = + awtComponent.getWidth () - insets.left - insets.right; + if (menuBarWidth > 0) + setMenuBarWidth (menuBar, menuBarWidth); + menuBarHeight = getMenuBarHeight (); + insets.top += menuBarHeight; + awtComponent.validate (); + gtkFixedSetVisible (true); + } + else if (bar != null && menuBar != null) + { + // We're swapping the menubar. + gtkFixedSetVisible (false); + removeMenuBarPeer(); + int oldHeight = menuBarHeight; + int menuBarWidth = + awtComponent.getWidth () - insets.left - insets.right; + menuBar = (MenuBarPeer) ((MenuBar) bar).getPeer (); + setMenuBarPeer (menuBar); + if (menuBarWidth > 0) + setMenuBarWidth (menuBar, menuBarWidth); + menuBarHeight = getMenuBarHeight (); + if (oldHeight != menuBarHeight) + { + insets.top += (menuBarHeight - oldHeight); + awtComponent.validate (); + } + gtkFixedSetVisible (true); + } + } + + public void setBounds (int x, int y, int width, int height) + { + int menuBarWidth = width - insets.left - insets.right; + if (menuBar != null && menuBarWidth > 0) + setMenuBarWidth (menuBar, menuBarWidth); + + nativeSetBounds (x, y, + width - insets.left - insets.right, + height - insets.top - insets.bottom + + menuBarHeight); + } + + public void setResizable (boolean resizable) + { + // Call setSize; otherwise when resizable is changed from true to + // false the frame will shrink to the dimensions it had before it + // was resizable. + setSize (awtComponent.getWidth() - insets.left - insets.right, + awtComponent.getHeight() - insets.top - insets.bottom + + menuBarHeight); + gtkWindowSetResizable (resizable); + } + + protected void postInsetsChangedEvent (int top, int left, + int bottom, int right) + { + insets.top = top + menuBarHeight; + insets.left = left; + insets.bottom = bottom; + insets.right = right; + } + + public GtkFramePeer (Frame frame) + { + super (frame); + } + + void create () + { + // Create a normal decorated window. + create (GDK_WINDOW_TYPE_HINT_NORMAL, true); + + Frame frame = (Frame) awtComponent; + + setMenuBar (frame.getMenuBar ()); + + setTitle (frame.getTitle ()); + gtkWindowSetResizable (frame.isResizable ()); + setIconImage(frame.getIconImage()); + } + + native void nativeSetIconImage (GtkImage image); + + public void setIconImage (Image image) + { + if (image != null) + { + if (image instanceof GtkImage) + nativeSetIconImage((GtkImage) image); + else + nativeSetIconImage(new GtkImage(image.getSource())); + } + } + + public Graphics getGraphics () + { + Graphics g; + if (GtkToolkit.useGraphics2D ()) + g = new GdkGraphics2D (this); + else + g = new GdkGraphics (this); + g.translate (-insets.left, -insets.top); + return g; + } + + protected void postConfigureEvent (int x, int y, int width, int height) + { + int frame_x = x - insets.left; + // Since insets.top includes the MenuBar height, we need to add back + // the MenuBar height to the frame's y position. + // If no MenuBar exists in this frame, the MenuBar height will be 0. + int frame_y = y - insets.top + menuBarHeight; + int frame_width = width + insets.left + insets.right; + // Ditto as above. Since insets.top already includes the MenuBar's height, + // we need to subtract the MenuBar's height from the top inset. + int frame_height = height + insets.top + insets.bottom - menuBarHeight; + if (frame_x != awtComponent.getX() + || frame_y != awtComponent.getY() + || frame_width != awtComponent.getWidth() + || frame_height != awtComponent.getHeight()) + { + if (frame_width != awtComponent.getWidth() && menuBar != null + && width > 0) + setMenuBarWidth (menuBar, width); + + setBoundsCallback ((Window) awtComponent, + frame_x, + frame_y, + frame_width, + frame_height); + + awtComponent.validate(); + } + } + + protected void postMouseEvent(int id, long when, int mods, int x, int y, + int clickCount, boolean popupTrigger) + { + super.postMouseEvent (id, when, mods, + x + insets.left, y + insets.top, + clickCount, popupTrigger); + } + + protected void postExposeEvent (int x, int y, int width, int height) + { + if (!isInRepaint) + q().postEvent (new PaintEvent (awtComponent, PaintEvent.PAINT, + new Rectangle (x + insets.left, + y + insets.top, + width, height))); + } + + public int getState () + { + return 0; + } + + public void setState (int state) + { + + } + + public void setMaximizedBounds (Rectangle r) + { + + } +} + + diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkGenericPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkGenericPeer.java new file mode 100644 index 00000000000..705eed2351b --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkGenericPeer.java @@ -0,0 +1,98 @@ +/* GtkGenericPeer.java - Has a hashcode. Yuck. + Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.EventQueue; +import java.awt.Toolkit; +import java.awt.event.ActionEvent; + +public class GtkGenericPeer +{ + final int native_state = getUniqueInteger (); + + // Next native state value we will assign. + private static int next_native_state = 0; + + // The widget or other java-side object we wrap. + protected Object awtWidget; + + // Global event queue. + protected static EventQueue q = null; + + // Dispose of our native state. + public native void dispose (); + + static EventQueue q () + { + return Toolkit.getDefaultToolkit ().getSystemEventQueue (); + } + + protected GtkGenericPeer (Object awtWidget) + { + this.awtWidget = awtWidget; + } + + public static void enableQueue (EventQueue sq) + { + if (q == null) + q = sq; + } + + protected void postActionEvent (String command, int mods) + { + q().postEvent (new ActionEvent (awtWidget, ActionEvent.ACTION_PERFORMED, + command, mods)); + } + + // Return a unique integer for use in the native state mapping + // code. We can't use a hash code since that is not guaranteed to + // be unique. + static synchronized int getUniqueInteger () + { + // Let's assume this will never wrap. + return next_native_state++; + } + + native void gtkWidgetModifyFont (String name, int style, int size); + + static void printCurrentThread () + { + System.out.println ("gtkgenericpeer, thread: " + Thread.currentThread ()); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java new file mode 100644 index 00000000000..abb4137f4ae --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java @@ -0,0 +1,494 @@ +/* GtkImage.java + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Graphics; +import java.awt.Color; +import java.awt.Image; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.MemoryImageSource; +import java.awt.image.ImageConsumer; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; +import java.io.File; +import java.io.IOException; +import java.util.Hashtable; +import java.util.Vector; +import gnu.classpath.RawData; + +/** + * GtkImage - wraps a GdkPixbuf or GdkPixmap. + * + * The constructor GtkImage(int, int) creates an 'off-screen' GdkPixmap, + * this can be drawn to (it's a GdkDrawable), and correspondingly, you can + * create a GdkGraphics object for it. + * + * This corresponds to the Image implementation returned by + * Component.createImage(int, int). + * + * A GdkPixbuf is 'on-screen' and the gdk cannot draw to it, + * this is used for the other constructors (and other createImage methods), and + * corresponds to the Image implementations returned by the Toolkit.createImage + * methods, and is basically immutable. + * + * @author Sven de Marothy + */ +public class GtkImage extends Image +{ + int width = -1, height = -1; + + /** + * Properties. + */ + Hashtable props; + + /** + * Loaded or not flag, for asynchronous compatibility. + */ + boolean isLoaded; + + /** + * Pointer to the GdkPixbuf + */ + RawData pixmap; + + /** + * Observer queue. + */ + Vector observers; + + /** + * If offScreen is set, a GdkBitmap is wrapped and not a Pixbuf. + */ + boolean offScreen; + + /** + * Error flag for loading. + */ + boolean errorLoading; + + /** + * Original source, if created from an ImageProducer. + */ + ImageProducer source; + + /* + * The 32-bit AABBGGRR format the GDK uses. + */ + static ColorModel nativeModel = new DirectColorModel(32, + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0xFF000000); + + /** + * Returns a copy of the pixel data as a java array. + */ + private native int[] getPixels(); + + /** + * Sets the pixel data from a java array. + */ + private native void setPixels(int[] pixels); + + /** + * Loads an image using gdk-pixbuf. + */ + private native boolean loadPixbuf(String name); + + /** + * Allocates a Gtk Pixbuf or pixmap + */ + private native void createPixmap(); + + /** + * Frees the above. + */ + private native void freePixmap(); + + /** + * Sets the pixmap to scaled copy of src image. hints are rendering hints. + */ + private native void createScaledPixmap(GtkImage src, int hints); + + /** + * Draws the image, optionally scaled and composited. + */ + private native void drawPixelsScaled (GdkGraphics gc, + int bg_red, int bg_green, int bg_blue, + int x, int y, int width, int height, + boolean composite); + + /** + * Draws the image, optionally scaled flipped and composited. + */ + private native void drawPixelsScaledFlipped (GdkGraphics gc, + int bg_red, int bg_green, + int bg_blue, + boolean flipX, boolean flipY, + int srcX, int srcY, + int srcWidth, int srcHeight, + int dstX, int dstY, + int dstWidth, int dstHeight, + boolean composite); + + /** + * Constructs a GtkImage from an ImageProducer. Asynchronity is handled in + * the following manner: + * A GtkImageConsumer gets the image data, and calls setImage() when + * completely finished. The GtkImage is not considered loaded until the + * GtkImageConsumer is completely finished. We go for all "all or nothing". + */ + public GtkImage (ImageProducer producer) + { + isLoaded = false; + observers = new Vector(); + source = producer; + errorLoading = false; + source.startProduction(new GtkImageConsumer(this, source)); + offScreen = false; + } + + /** + * Constructs a GtkImage by loading a given file. + * + * @throws IllegalArgumentException if the image could not be loaded. + */ + public GtkImage (String filename) + { + File f = new File(filename); + try + { + if (loadPixbuf(f.getCanonicalPath()) != true) + throw new IllegalArgumentException("Couldn't load image: "+filename); + } + catch(IOException e) + { + throw new IllegalArgumentException("Couldn't load image: "+filename); + } + + isLoaded = true; + observers = null; + offScreen = false; + props = new Hashtable(); + } + + /** + * Constructs an empty GtkImage. + */ + public GtkImage (int width, int height) + { + this.width = width; + this.height = height; + props = new Hashtable(); + isLoaded = true; + observers = null; + offScreen = true; + createPixmap(); + } + + /** + * Constructs a scaled version of the src bitmap, using the GDK. + */ + private GtkImage (GtkImage src, int width, int height, int hints) + { + this.width = width; + this.height = height; + props = new Hashtable(); + isLoaded = true; + observers = null; + offScreen = false; + + // Use the GDK scaling method. + createScaledPixmap(src, hints); + } + + /** + * Callback from the image consumer. + */ + public void setImage(int width, int height, + int[] pixels, Hashtable properties) + { + this.width = width; + this.height = height; + props = (properties != null) ? properties : new Hashtable(); + + if (width <= 0 || height <= 0 || pixels == null) + { + errorLoading = true; + return; + } + + isLoaded = true; + deliver(); + createPixmap(); + setPixels(pixels); + } + + // java.awt.Image methods //////////////////////////////////////////////// + + public synchronized int getWidth (ImageObserver observer) + { + if (addObserver(observer)) + return -1; + + return width; + } + + public synchronized int getHeight (ImageObserver observer) + { + if (addObserver(observer)) + return -1; + + return height; + } + + public synchronized Object getProperty (String name, ImageObserver observer) + { + if (addObserver(observer)) + return UndefinedProperty; + + Object value = props.get (name); + return (value == null) ? UndefinedProperty : value; + } + + /** + * Returns the source of this image. + */ + public ImageProducer getSource () + { + if (!isLoaded) + return null; + return new MemoryImageSource(width, height, nativeModel, getPixels(), + 0, width); + } + + /** + * Creates a GdkGraphics context for this pixmap. + */ + public Graphics getGraphics () + { + if (!isLoaded) + return null; + if (offScreen) + return new GdkGraphics(this); + else + throw new IllegalAccessError("This method only works for off-screen" + +" Images."); + } + + /** + * Returns a scaled instance of this pixmap. + */ + public Image getScaledInstance(int width, + int height, + int hints) + { + if (width <= 0 || height <= 0) + throw new IllegalArgumentException("Width and height of scaled bitmap"+ + "must be >= 0"); + + return new GtkImage(this, width, height, hints); + } + + /** + * If the image is loaded and comes from an ImageProducer, + * regenerate the image from there. + * + * I have no idea if this is ever actually used. Since GtkImage can't be + * instantiated directly, how is the user to know if it was created from + * an ImageProducer or not? + */ + public synchronized void flush () + { + if (isLoaded && source != null) + { + observers = new Vector(); + isLoaded = false; + freePixmap(); + source.startProduction(new GtkImageConsumer(this, source)); + } + } + + public void finalize() + { + if (isLoaded) + freePixmap(); + } + + /** + * Returns the image status, used by GtkToolkit + */ + public int checkImage (ImageObserver observer) + { + if (addObserver(observer)) + { + if (errorLoading == true) + return ImageObserver.ERROR; + else + return 0; + } + + return ImageObserver.ALLBITS | ImageObserver.WIDTH | ImageObserver.HEIGHT; + } + + // Drawing methods //////////////////////////////////////////////// + + /** + * Draws an image with eventual scaling/transforming. + */ + public boolean drawImage (GdkGraphics g, int dx1, int dy1, int dx2, int dy2, + int sx1, int sy1, int sx2, int sy2, + Color bgcolor, ImageObserver observer) + { + if (addObserver(observer)) + return false; + + boolean flipX = (dx1 > dx2)^(sx1 > sx2); + boolean flipY = (dy1 > dy2)^(sy1 > sy2); + int dstWidth = Math.abs (dx2 - dx1); + int dstHeight = Math.abs (dy2 - dy1); + int srcWidth = Math.abs (sx2 - sx1); + int srcHeight = Math.abs (sy2 - sy1); + int srcX = (sx1 < sx2) ? sx1 : sx2; + int srcY = (sy1 < sy2) ? sy1 : sy2; + int dstX = (dx1 < dx2) ? dx1 : dx2; + int dstY = (dy1 < dy2) ? dy1 : dy2; + + // Clipping. This requires the dst to be scaled as well, + if (srcWidth > width) + { + dstWidth = (int)((double)dstWidth*((double)width/(double)srcWidth)); + srcWidth = width - srcX; + } + + if (srcHeight > height) + { + dstHeight = (int)((double)dstHeight*((double)height/(double)srcHeight)); + srcHeight = height - srcY; + } + + if (srcWidth + srcX > width) + { + dstWidth = (int)((double)dstWidth * (double)(width - srcX)/(double)srcWidth); + srcWidth = width - srcX; + } + + if (srcHeight + srcY > height) + { + dstHeight = (int)((double)dstHeight * (double)(width - srcY)/(double)srcHeight); + srcHeight = height - srcY; + } + + if ( srcWidth <= 0 || srcHeight <= 0 || dstWidth <= 0 || dstHeight <= 0) + return true; + + if(bgcolor != null) + drawPixelsScaledFlipped (g, bgcolor.getRed (), bgcolor.getGreen (), + bgcolor.getBlue (), + flipX, flipY, + srcX, srcY, + srcWidth, srcHeight, + dstX, dstY, + dstWidth, dstHeight, + true); + else + drawPixelsScaledFlipped (g, 0, 0, 0, flipX, flipY, + srcX, srcY, srcWidth, srcHeight, + dstX, dstY, dstWidth, dstHeight, + false); + return true; + } + + /** + * Draws an image to the GdkGraphics context, at (x,y) scaled to + * width and height, with optional compositing with a background color. + */ + public boolean drawImage (GdkGraphics g, int x, int y, int width, int height, + Color bgcolor, ImageObserver observer) + { + if (addObserver(observer)) + return false; + + if(bgcolor != null) + drawPixelsScaled(g, bgcolor.getRed (), bgcolor.getGreen (), + bgcolor.getBlue (), x, y, width, height, true); + else + drawPixelsScaled(g, 0, 0, 0, x, y, width, height, false); + + return true; + } + + // Private methods //////////////////////////////////////////////// + + /** + * Delivers notifications to all queued observers. + */ + private void deliver() + { + int flags = ImageObserver.HEIGHT | + ImageObserver.WIDTH | + ImageObserver.PROPERTIES | + ImageObserver.ALLBITS; + + if (observers != null) + for(int i=0; i < observers.size(); i++) + ((ImageObserver)observers.elementAt(i)). + imageUpdate(this, flags, 0, 0, width, height); + + observers = null; + } + + /** + * Adds an observer, if we need to. + * @return true if an observer was added. + */ + private boolean addObserver(ImageObserver observer) + { + if (!isLoaded) + { + if(observer != null) + if (!observers.contains (observer)) + observers.addElement (observer); + return true; + } + return false; + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkImageConsumer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkImageConsumer.java new file mode 100644 index 00000000000..564cc8d668c --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkImageConsumer.java @@ -0,0 +1,155 @@ +/* GtkImageConsumer.java + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Graphics; +import java.awt.Image; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.ImageConsumer; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; +import java.util.Hashtable; +import java.util.Vector; + +/** + * Helper class to GtkImage. Sits and gathers pixels for a GtkImage and then + * calls GtkImage.setImage(). + * + * @author Sven de Marothy + */ +public class GtkImageConsumer implements ImageConsumer +{ + private GtkImage target; + private int width, height; + private Hashtable properties; + private int[] pixelCache = null; + private ImageProducer source; + + public GtkImageConsumer(GtkImage target, ImageProducer source) + { + this.target = target; + this.source = source; + } + + public synchronized void imageComplete (int status) + { + source.removeConsumer(this); + target.setImage(width, height, pixelCache, properties); + } + + public synchronized void setColorModel (ColorModel model) + { + // This method is to inform on what the most used color model + // in the image is, for optimization reasons. We ignore this + // information. + } + + public synchronized void setDimensions (int width, int height) + { + pixelCache = new int[width*height]; + + this.width = width; + this.height = height; + } + + public synchronized void setHints (int flags) + { + // This method informs us in which order the pixels are + // delivered, for progressive-loading support, etc. + // Since we wait until it's all loaded, we can ignore the hints. + } + + public synchronized void setPixels (int x, int y, int width, int height, + ColorModel cm, byte[] pixels, + int offset, int scansize) + { + setPixels (x, y, width, height, cm, convertPixels (pixels), offset, + scansize); + } + + public synchronized void setPixels (int x, int y, int width, int height, + ColorModel cm, int[] pixels, + int offset, int scansize) + { + if (pixelCache == null) + return; // Not sure this should ever happen. + + if (cm.equals(GtkImage.nativeModel)) + for (int i = 0; i < height; i++) + System.arraycopy (pixels, offset + (i * scansize), + pixelCache, (y + i) * this.width + x, + width); + else + { + for (int i = 0; i < height; i++) + for (int j = 0; j < width; j++) + { + // get in AARRGGBB and convert to AABBGGRR + int pix = cm.getRGB(pixels[offset + (i * scansize) + x + j]); + byte b = (byte)(pix & 0xFF); + byte r = (byte)(((pix & 0x00FF0000) >> 16) & 0xFF); + pix &= 0xFF00FF00; + pix |= ((b & 0xFF) << 16); + pix |= (r & 0xFF); + pixelCache[(y + i) * this.width + x + j] = pix; + } + } + } + + /** + * This is an old method, no idea if it's correct. + */ + private int[] convertPixels (byte[] pixels) + { + int ret[] = new int[pixels.length]; + + for (int i = 0; i < pixels.length; i++) + ret[i] = pixels[i] & 0xFF; + + return ret; + } + + public synchronized void setProperties (Hashtable props) + { + this.properties = props; + } +} + + diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkLabelPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkLabelPeer.java new file mode 100644 index 00000000000..fdd5fd15f63 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkLabelPeer.java @@ -0,0 +1,84 @@ +/* GtkLabelPeer.java -- Implements LabelPeer with GTK + Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Label; +import java.awt.peer.LabelPeer; + +public class GtkLabelPeer extends GtkComponentPeer + implements LabelPeer +{ + native void create (String text, float alignment); + native void gtkWidgetModifyFont (String name, int style, int size); + native void nativeSetAlignment (float alignment); + + public native void setText(String text); + native void setNativeBounds (int x, int y, int width, int height); + + void create () + { + Label label = (Label) awtComponent; + create (label.getText (), getGtkAlignment (label.getAlignment ())); + } + + public GtkLabelPeer (Label l) + { + super (l); + } + + public void setAlignment (int alignment) + { + nativeSetAlignment (getGtkAlignment (alignment)); + } + + float getGtkAlignment (int alignment) + { + switch (alignment) + { + case Label.LEFT: + return 0.0f; + case Label.CENTER: + return 0.5f; + case Label.RIGHT: + return 1.0f; + } + + return 0.0f; + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkListPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkListPeer.java new file mode 100644 index 00000000000..ff12fe34bba --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkListPeer.java @@ -0,0 +1,180 @@ +/* GtkListPeer.java -- Implements ListPeer with GTK + Copyright (C) 1998, 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTEvent; +import java.awt.Dimension; +import java.awt.List; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.peer.ListPeer; + +public class GtkListPeer extends GtkComponentPeer + implements ListPeer +{ + void create () + { + List list = (List) awtComponent; + + create (list.getRows ()); + + setMultipleMode (list.isMultipleMode ()); + } + + native void create (int rows); + native void connectSignals (); + native void gtkWidgetModifyFont (String name, int style, int size); + native void gtkWidgetRequestFocus (); + + native void getSize (int rows, int visibleRows, int dims[]); + + public GtkListPeer (List list) + { + super (list); + + setMultipleMode (list.isMultipleMode ()); + + if (list.getItemCount () > 0) + append (list.getItems ()); + } + + native void append (String items[]); + + public native void add (String item, int index); + + public void addItem (String item, int index) + { + add (item, index); + } + + public void clear () + { + removeAll (); + } + + public native void delItems (int start, int end); + public native void deselect (int index); + + public Dimension getMinimumSize (int rows) + { + return minimumSize (rows); + } + + public Dimension getPreferredSize (int rows) + { + return preferredSize (rows); + } + + public native int[] getSelectedIndexes (); + public native void makeVisible (int index); + + public Dimension minimumSize (int rows) + { + int dims[] = new int[2]; + + int visibleRows = ((List) awtComponent).getRows(); + getSize (rows, visibleRows, dims); + return new Dimension (dims[0], dims[1]); + } + + public Dimension preferredSize (int rows) + { + int dims[] = new int[2]; + + int visibleRows = ((List) awtComponent).getRows(); + getSize (rows, visibleRows, dims); + return new Dimension (dims[0], dims[1]); + } + + public void removeAll () + { + delItems (0, -1); + } + + public native void select (int index); + public native void setMultipleMode (boolean b); + + public void setMultipleSelections (boolean b) + { + setMultipleMode (b); + } + + public void handleEvent (AWTEvent e) + { + if (e.getID () == MouseEvent.MOUSE_CLICKED && isEnabled ()) + { + // Only generate the ActionEvent on the second click of a + // multiple click. + MouseEvent me = (MouseEvent) e; + if (!me.isConsumed () + && (me.getModifiersEx () & MouseEvent.BUTTON1_DOWN_MASK) != 0 + && me.getClickCount() == 2) + { + String selectedItem = ((List) awtComponent).getSelectedItem (); + + // Double-click only generates an Action event if + // something is selected. + if (selectedItem != null) + postActionEvent (((List) awtComponent).getSelectedItem (), + me.getModifiersEx ()); + } + } + + if (e.getID () == KeyEvent.KEY_PRESSED) + { + KeyEvent ke = (KeyEvent) e; + if (!ke.isConsumed () && ke.getKeyCode () == KeyEvent.VK_ENTER) + { + String selectedItem = ((List) awtComponent).getSelectedItem (); + + // Enter only generates an Action event if something is + // selected. + if (selectedItem != null) + postActionEvent (selectedItem, ke.getModifiersEx ()); + } + } + + super.handleEvent (e); + } + + protected void postItemEvent (int item, int stateChange) + { + postItemEvent (new Integer (item), stateChange); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuBarPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuBarPeer.java new file mode 100644 index 00000000000..79eeaf9ba6a --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuBarPeer.java @@ -0,0 +1,80 @@ +/* GtkMenuBarPeer.java -- Implements MenuBarPeer with GTK+ + Copyright (C) 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Font; +import java.awt.Menu; +import java.awt.MenuBar; +import java.awt.MenuComponent; +import java.awt.peer.MenuBarPeer; +import java.awt.peer.MenuPeer; + +public class GtkMenuBarPeer extends GtkMenuComponentPeer + implements MenuBarPeer +{ + + native void create (); + native void addMenu (MenuPeer menu); + + public GtkMenuBarPeer (MenuBar target) + { + super (target); + } + + void setFont () + { + MenuComponent mc = (MenuComponent) awtWidget; + Font f = mc.getFont (); + + if (f == null) + mc.setFont (new Font ("Dialog", Font.PLAIN, 12)); + } + + // FIXME: remove this method or replace it with one that does + // something useful. + /* In Gnome, help menus are no longer right flushed. */ + native void nativeSetHelpMenu(MenuPeer menuPeer); + + public void addHelpMenu (Menu menu) + { + // nativeSetHelpMenu((MenuPeer) menu.getPeer()); + } + + public native void delMenu(int index); +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuComponentPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuComponentPeer.java new file mode 100644 index 00000000000..8d9d1ca04a0 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuComponentPeer.java @@ -0,0 +1,63 @@ +/* GtkMenuComponentPeer.java -- Implements MenuComponentPeer with GTK+ + Copyright (C) 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.peer.MenuComponentPeer; + +public class GtkMenuComponentPeer extends GtkGenericPeer + implements MenuComponentPeer +{ + void create () + { + throw new RuntimeException (); + } + + void setFont () + { + } + + public GtkMenuComponentPeer (Object awtWidget) + { + super (awtWidget); + create (); + setFont (); + } + + public native void dispose(); +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuItemPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuItemPeer.java new file mode 100644 index 00000000000..5728f262b13 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuItemPeer.java @@ -0,0 +1,120 @@ +/* GtkMenuItemPeer.java -- Implements MenuItemPeer with GTK+ + Copyright (C) 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Font; +import java.awt.Menu; +import java.awt.MenuBar; +import java.awt.MenuComponent; +import java.awt.MenuItem; +import java.awt.peer.MenuItemPeer; +import java.awt.peer.MenuPeer; + +public class GtkMenuItemPeer extends GtkMenuComponentPeer + implements MenuItemPeer +{ + native void create (String label); + native void connectSignals (); + native void gtkWidgetModifyFont (String name, int style, int size); + + void create () + { + create (((MenuItem) awtWidget).getLabel()); + } + + public GtkMenuItemPeer (MenuItem item) + { + super (item); + setEnabled (item.isEnabled ()); + setParent (item); + + if (item.getParent() instanceof Menu && ! (item instanceof Menu)) + connectSignals(); + } + + void setFont () + { + MenuComponent mc = ((MenuComponent) awtWidget); + Font f = mc.getFont (); + + if (f == null) + { + MenuComponent parent = (MenuComponent) mc.getParent (); + Font pf = parent.getFont (); + gtkWidgetModifyFont (pf.getName (), pf.getStyle (), pf.getSize ()); + } + else + gtkWidgetModifyFont(f.getName(), f.getStyle(), f.getSize()); + } + + void setParent (MenuItem item) + { + // add ourself differently, based on what type of parent we have + // yes, the typecasting here is nasty. + Object parent = item.getParent (); + if (parent instanceof MenuBar) + { + ((GtkMenuBarPeer)((MenuBar)parent).getPeer ()).addMenu ((MenuPeer) this); + } + else // parent instanceof Menu + { + ((GtkMenuPeer)((Menu)parent).getPeer ()).addItem (this, + item.getShortcut ()); + } + } + + public void disable () + { + setEnabled (false); + } + + public void enable () + { + setEnabled (true); + } + + public native void setEnabled(boolean b); + + public native void setLabel(String label); + + protected void postMenuActionEvent () + { + postActionEvent (((MenuItem)awtWidget).getActionCommand (), 0); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuPeer.java new file mode 100644 index 00000000000..80332dd6a72 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkMenuPeer.java @@ -0,0 +1,103 @@ +/* GtkMenuPeer.java -- Implements MenuPeer with GTK+ + Copyright (C) 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Component; +import java.awt.Menu; +import java.awt.MenuContainer; +import java.awt.MenuItem; +import java.awt.MenuShortcut; +import java.awt.peer.MenuItemPeer; +import java.awt.peer.MenuPeer; + +public class GtkMenuPeer extends GtkMenuItemPeer + implements MenuPeer +{ + native void create (String label); + native void addItem (MenuItemPeer item, int key, boolean shiftModifier); + native void setupAccelGroup (GtkGenericPeer container); + native void addTearOff (); + + public GtkMenuPeer (Menu menu) + { + super (menu); + + if (menu.isTearOff()) + addTearOff(); + + MenuContainer parent = menu.getParent (); + if (parent instanceof Menu) + setupAccelGroup ((GtkGenericPeer)((Menu)parent).getPeer ()); + else if (parent instanceof Component) + setupAccelGroup ((GtkGenericPeer)((Component)parent).getPeer ()); + else + setupAccelGroup (null); + } + + public void addItem (MenuItem item) + { + int key = 0; + boolean shiftModifier = false; + + MenuShortcut ms = item.getShortcut (); + if (ms != null) + { + key = ms.getKey (); + shiftModifier = ms.usesShiftModifier (); + } + + addItem ((MenuItemPeer) item.getPeer (), key, shiftModifier); + } + + public void addItem (MenuItemPeer item, MenuShortcut ms) + { + int key = 0; + boolean shiftModifier = false; + + if (ms != null) + { + key = ms.getKey (); + shiftModifier = ms.usesShiftModifier (); + } + + addItem (item, key, shiftModifier); + } + + public native void delItem(int index); +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkPanelPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkPanelPeer.java new file mode 100644 index 00000000000..fb5addeb442 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkPanelPeer.java @@ -0,0 +1,70 @@ +/* GtkPanelPeer.java -- Implements PanelPeer with GTK + Copyright (C) 1998, 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTEvent; +import java.awt.Panel; +import java.awt.event.MouseEvent; +import java.awt.peer.PanelPeer; + +public class GtkPanelPeer extends GtkContainerPeer + implements PanelPeer +{ + native void create (); + + public GtkPanelPeer (Panel p) + { + super (p); + } + + public void handleEvent (AWTEvent event) + { + int id = event.getID(); + + switch (id) + { + case MouseEvent.MOUSE_PRESSED: + awtComponent.requestFocusInWindow (); + break; + } + super.handleEvent (event); + } + + native void connectSignals (); +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkPopupMenuPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkPopupMenuPeer.java new file mode 100644 index 00000000000..d14c16dd79d --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkPopupMenuPeer.java @@ -0,0 +1,74 @@ +/* GtkPopupMenuPeer.java -- Implements PopupMenuPeer with GTK+ + Copyright (C) 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Component; +import java.awt.Event; +import java.awt.MenuItem; +import java.awt.Point; +import java.awt.PopupMenu; +import java.awt.peer.PopupMenuPeer; + +public class GtkPopupMenuPeer extends GtkMenuPeer + implements PopupMenuPeer +{ + public GtkPopupMenuPeer (PopupMenu menu) + { + super (menu); + } + + native void setupAccelGroup (GtkGenericPeer container); + + void setParent (MenuItem item) + { + // we don't need to "add" ourselves to our parent + } + + native void show (int x, int y, long time); + public void show (Component origin, int x, int y) + { + Point abs = origin.getLocationOnScreen (); + show (abs.x + x, abs.y + y, 0); + } + + public void show (Event e) + { + + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkScrollPanePeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkScrollPanePeer.java new file mode 100644 index 00000000000..69f8b494625 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkScrollPanePeer.java @@ -0,0 +1,113 @@ +/* GtkScrollPanePeer.java -- Implements ScrollPanePeer with GTK + Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Adjustable; +import java.awt.Dimension; +import java.awt.ScrollPane; +import java.awt.peer.ScrollPanePeer; + +public class GtkScrollPanePeer extends GtkContainerPeer + implements ScrollPanePeer +{ + native void create (int width, int height); + + void create () + { + create (awtComponent.getWidth (), awtComponent.getHeight ()); + } + + // native void gtkScrolledWindowSetScrollPosition(int x, int y); + native void gtkScrolledWindowSetHScrollIncrement (int u); + native void gtkScrolledWindowSetVScrollIncrement (int u); + + public GtkScrollPanePeer (ScrollPane sp) + { + super (sp); + + setPolicy (sp.getScrollbarDisplayPolicy ()); + } + + native void setPolicy (int policy); + public void childResized (int width, int height) + { + int dim[] = new int[2]; + + gtkWidgetGetDimensions (dim); + + // If the child is in this range, GTK adds both scrollbars, but + // the AWT doesn't. So set the peer's scroll policy to + // GTK_POLICY_NEVER. + if ((width > dim[0] - getVScrollbarWidth () + && width <= dim[0]) + && (height > dim[1] - getHScrollbarHeight () + && height <= dim[1])) + setPolicy (ScrollPane.SCROLLBARS_NEVER); + else + setPolicy (((ScrollPane) awtComponent).getScrollbarDisplayPolicy ()); + } + + public native int getHScrollbarHeight(); + public native int getVScrollbarWidth(); + public native void setScrollPosition(int x, int y); + + public Dimension getPreferredSize () + { + return awtComponent.getSize(); + } + + public void setUnitIncrement (Adjustable adj, int u) + { + if (adj.getOrientation()==Adjustable.HORIZONTAL) + gtkScrolledWindowSetHScrollIncrement (u); + else + gtkScrolledWindowSetVScrollIncrement (u); + } + + public void setValue (Adjustable adj, int v) + { +// System.out.println("SPP: setVal: "+adj+":"+v); +// Point p=myScrollPane.getScrollPosition (); +// if (adj.getOrientation()==Adjustable.HORIZONTAL) +// gtkScrolledWindowSetScrollPosition (v,p.y); +// else +// gtkScrolledWindowSetScrollPosition (p.x,v); +// adj.setValue(v); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkScrollbarPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkScrollbarPeer.java new file mode 100644 index 00000000000..aa3a26e34ad --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkScrollbarPeer.java @@ -0,0 +1,80 @@ +/* GtkScrollbarPeer.java -- Implements ScrollbarPeer with GTK+ + Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Adjustable; +import java.awt.Scrollbar; +import java.awt.event.AdjustmentEvent; +import java.awt.peer.ScrollbarPeer; + +public class GtkScrollbarPeer extends GtkComponentPeer + implements ScrollbarPeer +{ + void create () + { + Scrollbar sb = (Scrollbar) awtComponent; + + create (sb.getOrientation (), sb.getValue (), + sb.getMinimum (), sb.getMaximum (), + sb.getUnitIncrement (), sb.getBlockIncrement (), + sb.getVisibleAmount ()); + } + + native void create (int orientation, int value, + int min, int max, int stepIncr, int pageIncr, + int visibleAmount); + + native void connectSignals (); + + public GtkScrollbarPeer (Scrollbar s) + { + super (s); + } + + public native void setLineIncrement(int amount); + public native void setPageIncrement(int amount); + public native void setValues(int value, int visible, int min, int max); + + protected void postAdjustmentEvent (int type, int value) + { + q().postEvent (new AdjustmentEvent ((Adjustable)awtComponent, + AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, + type, value)); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkTextAreaPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkTextAreaPeer.java new file mode 100644 index 00000000000..a842b8318a2 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkTextAreaPeer.java @@ -0,0 +1,212 @@ +/* GtkTextAreaPeer.java -- Implements TextAreaPeer with GTK + Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Rectangle; +import java.awt.TextArea; +import java.awt.peer.TextAreaPeer; +import java.awt.peer.TextComponentPeer; + +public class GtkTextAreaPeer extends GtkComponentPeer + implements TextComponentPeer, TextAreaPeer +{ + private static transient int DEFAULT_ROWS = 10; + private static transient int DEFAULT_COLS = 80; + + native void create (int width, int height, int scrollbarVisibility); + + native void gtkWidgetModifyFont (String name, int style, int size); + native void gtkWidgetRequestFocus (); + + public native void connectSignals (); + + public native int getCaretPosition (); + public native void setCaretPosition (int pos); + public native int getSelectionStart (); + public native int getSelectionEnd (); + public native String getText (); + public native void select (int start, int end); + public native void setEditable (boolean state); + public native void setText (String text); + + public int getIndexAtPoint(int x, int y) + { + // FIXME + return 0; + } + + public Rectangle getCharacterBounds (int pos) + { + // FIXME + return null; + } + + public long filterEvents (long filter) + { + // FIXME + return filter; + } + + void create () + { + Font f = awtComponent.getFont (); + + // By default, Sun sets a TextArea's font when its peer is + // created. If f != null then the peer's font is set by + // GtkComponent.create. + if (f == null) + { + f = new Font ("Dialog", Font.PLAIN, 12); + awtComponent.setFont (f); + } + + FontMetrics fm = getFontMetrics (f); + + TextArea ta = ((TextArea) awtComponent); + int sizeRows = ta.getRows (); + int sizeCols = ta.getColumns (); + + sizeRows = sizeRows == 0 ? DEFAULT_ROWS : sizeRows; + sizeCols = sizeCols == 0 ? DEFAULT_COLS : sizeCols; + + int width = sizeCols * fm.getMaxAdvance (); + int height = sizeRows * (fm.getMaxDescent () + fm.getMaxAscent ()); + + create (width, height, ta.getScrollbarVisibility ()); + setEditable (ta.isEditable ()); + } + + public GtkTextAreaPeer (TextArea ta) + { + super (ta); + + setText (ta.getText ()); + setCaretPosition (0); + } + + public native void insert (String str, int pos); + public native void replaceRange (String str, int start, int end); + + public Dimension getMinimumSize (int rows, int cols) + { + return minimumSize (rows == 0 ? DEFAULT_ROWS : rows, + cols == 0 ? DEFAULT_COLS : cols); + } + + public Dimension getPreferredSize (int rows, int cols) + { + return preferredSize (rows == 0 ? DEFAULT_ROWS : rows, + cols == 0 ? DEFAULT_COLS : cols); + } + + native int getHScrollbarHeight (); + native int getVScrollbarWidth (); + + // Deprecated + public Dimension minimumSize (int rows, int cols) + { + TextArea ta = ((TextArea) awtComponent); + int height = 0; + int width = 0; + + if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH + || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_HORIZONTAL_ONLY) + height = getHScrollbarHeight (); + + if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH + || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_VERTICAL_ONLY) + width = getVScrollbarWidth (); + + Font f = awtComponent.getFont (); + if (f == null) + return new Dimension (width, height); + + FontMetrics fm = getFontMetrics (f); + + int sizeRows = rows == 0 ? DEFAULT_ROWS : rows; + int sizeCols = cols == 0 ? DEFAULT_COLS : cols; + + width += sizeCols * fm.getMaxAdvance (); + height += sizeRows * (fm.getMaxDescent () + fm.getMaxAscent ()); + + return new Dimension (width, height); + } + + public Dimension preferredSize (int rows, int cols) + { + TextArea ta = ((TextArea) awtComponent); + int height = 0; + int width = 0; + + if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH + || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_HORIZONTAL_ONLY) + height = getHScrollbarHeight (); + + if (ta.getScrollbarVisibility () == TextArea.SCROLLBARS_BOTH + || ta.getScrollbarVisibility () == TextArea.SCROLLBARS_VERTICAL_ONLY) + width = getVScrollbarWidth (); + + Font f = awtComponent.getFont (); + if (f == null) + return new Dimension (width, height); + + FontMetrics fm = getFontMetrics (f); + + int sizeRows = rows == 0 ? DEFAULT_ROWS : rows; + int sizeCols = cols == 0 ? DEFAULT_COLS : cols; + + width += sizeCols * fm.getMaxAdvance (); + height += sizeRows * (fm.getMaxDescent () + fm.getMaxAscent ()); + + return new Dimension (width, height); + } + + public void replaceText (String str, int start, int end) + { + replaceRange (str, start, end); + } + + public void insertText (String str, int pos) + { + insert (str, pos); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java new file mode 100644 index 00000000000..73a976bba6d --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java @@ -0,0 +1,196 @@ +/* GtkTextFieldPeer.java -- Implements TextFieldPeer with GTK + Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.AWTEvent; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Rectangle; +import java.awt.TextField; +import java.awt.event.KeyEvent; +import java.awt.peer.TextFieldPeer; +import java.awt.peer.TextComponentPeer; + +public class GtkTextFieldPeer extends GtkComponentPeer + implements TextComponentPeer, TextFieldPeer +{ + native void create (int width); + native void gtkWidgetSetBackground (int red, int green, int blue); + native void gtkWidgetSetForeground (int red, int green, int blue); + + public native void connectSignals (); + + public native int getCaretPosition (); + public native void setCaretPosition (int pos); + public native int getSelectionStart (); + public native int getSelectionEnd (); + public native String getText (); + public native void select (int start, int end); + public native void setEditable (boolean state); + public native void setText (String text); + + public int getIndexAtPoint(int x, int y) + { + // FIXME + return 0; + } + + public Rectangle getCharacterBounds (int pos) + { + // FIXME + return null; + } + + public long filterEvents (long filter) + { + // FIXME + return filter; + } + + void create () + { + Font f = awtComponent.getFont (); + + // By default, Sun sets a TextField's font when its peer is + // created. If f != null then the peer's font is set by + // GtkComponent.create. + if (f == null) + { + f = new Font ("Dialog", Font.PLAIN, 12); + awtComponent.setFont (f); + } + + FontMetrics fm = getFontMetrics (f); + + TextField tf = ((TextField) awtComponent); + int cols = tf.getColumns (); + + int text_width = cols * fm.getMaxAdvance (); + + create (text_width); + + setEditable (tf.isEditable ()); + } + + native int gtkEntryGetBorderWidth (); + + native void gtkWidgetModifyFont (String name, int style, int size); + + public GtkTextFieldPeer (TextField tf) + { + super (tf); + + setText (tf.getText ()); + setCaretPosition (0); + + if (tf.echoCharIsSet ()) + setEchoChar (tf.getEchoChar ()); + } + + public Dimension getMinimumSize (int cols) + { + return minimumSize (cols); + } + + public Dimension getPreferredSize (int cols) + { + return preferredSize (cols); + } + + public native void setEchoChar (char c); + + // Deprecated + public Dimension minimumSize (int cols) + { + int dim[] = new int[2]; + + gtkWidgetGetPreferredDimensions (dim); + + Font f = awtComponent.getFont (); + if (f == null) + return new Dimension (2 * gtkEntryGetBorderWidth (), dim[1]); + + FontMetrics fm = getFontMetrics (f); + + int text_width = cols * fm.getMaxAdvance (); + + int width = text_width + 2 * gtkEntryGetBorderWidth (); + + return new Dimension (width, dim[1]); + } + + public Dimension preferredSize (int cols) + { + int dim[] = new int[2]; + + gtkWidgetGetPreferredDimensions (dim); + + Font f = awtComponent.getFont (); + if (f == null) + return new Dimension (2 * gtkEntryGetBorderWidth (), dim[1]); + + FontMetrics fm = getFontMetrics (f); + + int text_width = cols * fm.getMaxAdvance (); + + int width = text_width + 2 * gtkEntryGetBorderWidth (); + + return new Dimension (width, dim[1]); + } + + public void setEchoCharacter (char c) + { + setEchoChar (c); + } + + public void handleEvent (AWTEvent e) + { + if (e.getID () == KeyEvent.KEY_PRESSED) + { + KeyEvent ke = (KeyEvent) e; + + if (!ke.isConsumed () + && ke.getKeyCode () == KeyEvent.VK_ENTER) + postActionEvent (getText (), ke.getModifiersEx ()); + } + + super.handleEvent (e); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkToolkit.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkToolkit.java new file mode 100644 index 00000000000..69901102faf --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkToolkit.java @@ -0,0 +1,653 @@ +/* GtkToolkit.java -- Implements an AWT Toolkit using GTK for peers + Copyright (C) 1998, 1999, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import gnu.classpath.Configuration; +import gnu.java.awt.EmbeddedWindow; +import gnu.java.awt.peer.ClasspathFontPeer; +import gnu.java.awt.peer.ClasspathTextLayoutPeer; +import gnu.java.awt.peer.EmbeddedWindowPeer; + +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.dnd.DragGestureEvent; +import java.awt.dnd.peer.DragSourceContextPeer; +import java.awt.font.FontRenderContext; +import java.awt.im.InputMethodHighlight; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.ImageConsumer; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; +import java.awt.peer.*; +import java.io.InputStream; +import java.net.URL; +import java.text.AttributedString; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +import javax.imageio.spi.IIORegistry; + +/* This class uses a deprecated method java.awt.peer.ComponentPeer.getPeer(). + This merits comment. We are basically calling Sun's bluff on this one. + We think Sun has deprecated it simply to discourage its use as it is + bad programming style. However, we need to get at a component's peer in + this class. If getPeer() ever goes away, we can implement a hash table + that will keep up with every window's peer, but for now this is faster. */ + +/** + * This class accesses a system property called + * <tt>gnu.java.awt.peer.gtk.Graphics</tt>. If the property is defined and + * equal to "Graphics2D", the cairo-based GdkGraphics2D will be used in + * drawing contexts. Any other value will cause the older GdkGraphics + * object to be used. + */ +public class GtkToolkit extends gnu.java.awt.ClasspathToolkit +{ + Hashtable containers = new Hashtable(); + static EventQueue q; + static Clipboard systemClipboard; + static boolean useGraphics2dSet; + static boolean useGraphics2d; + + public static boolean useGraphics2D() + { + if (useGraphics2dSet) + return useGraphics2d; + useGraphics2d = System.getProperty("gnu.java.awt.peer.gtk.Graphics", + "Graphics").equals("Graphics2D"); + useGraphics2dSet = true; + return useGraphics2d; + } + + static native void gtkInit(int portableNativeSync); + + static + { + if (Configuration.INIT_LOAD_LIBRARY) + System.loadLibrary("gtkpeer"); + + int portableNativeSync; + String portNatSyncProp = + System.getProperty("gnu.classpath.awt.gtk.portable.native.sync"); + + if (portNatSyncProp == null) + portableNativeSync = -1; // unset + else if (Boolean.valueOf(portNatSyncProp).booleanValue()) + portableNativeSync = 1; // true + else + portableNativeSync = 0; // false + + gtkInit(portableNativeSync); + } + + public GtkToolkit () + { + systemClipboard = new GtkClipboard (); + } + + public native void beep(); + private native void getScreenSizeDimensions(int[] xy); + + public int checkImage (Image image, int width, int height, + ImageObserver observer) + { + int status = ImageObserver.ALLBITS + | ImageObserver.WIDTH + | ImageObserver.HEIGHT; + + if (image instanceof GtkImage) + return ((GtkImage) image).checkImage (observer); + + if (observer != null) + observer.imageUpdate (image, status, + -1, -1, + image.getWidth (observer), + image.getHeight (observer)); + + return status; + } + + /** + * A helper class to return to clients in cases where a BufferedImage is + * desired but its construction fails. + */ + private class GtkErrorImage extends Image + { + public GtkErrorImage() + { + } + + public int getWidth(ImageObserver observer) + { + return -1; + } + + public int getHeight(ImageObserver observer) + { + return -1; + } + + public ImageProducer getSource() + { + + return new ImageProducer() + { + HashSet consumers = new HashSet(); + public void addConsumer(ImageConsumer ic) + { + consumers.add(ic); + } + + public boolean isConsumer(ImageConsumer ic) + { + return consumers.contains(ic); + } + + public void removeConsumer(ImageConsumer ic) + { + consumers.remove(ic); + } + + public void startProduction(ImageConsumer ic) + { + consumers.add(ic); + Iterator i = consumers.iterator(); + while(i.hasNext()) + { + ImageConsumer c = (ImageConsumer) i.next(); + c.imageComplete(ImageConsumer.IMAGEERROR); + } + } + public void requestTopDownLeftRightResend(ImageConsumer ic) + { + startProduction(ic); + } + }; + } + + public Graphics getGraphics() + { + return null; + } + + public Object getProperty(String name, ImageObserver observer) + { + return null; + } + public Image getScaledInstance(int width, int height, int flags) + { + return new GtkErrorImage(); + } + + public void flush() + { + } + } + + + /** + * Helper to return either a BufferedImage -- the argument -- or a + * GtkErrorImage if the argument is null. + */ + + private Image bufferedImageOrError(BufferedImage b) + { + if (b == null) + return new GtkErrorImage(); + else + return b; + } + + + public Image createImage (String filename) + { + if (useGraphics2D()) + return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (filename)); + else + return new GtkImage (filename); + } + + public Image createImage (URL url) + { + if (useGraphics2D()) + return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (url)); + else + { + GdkPixbufDecoder d = new GdkPixbufDecoder (url); + GtkImage image = new GtkImage (d); + return image; + } + } + + public Image createImage (ImageProducer producer) + { + if (useGraphics2D()) + return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (producer)); + else + return new GtkImage (producer); + } + + public Image createImage (byte[] imagedata, int imageoffset, + int imagelength) + { + if (useGraphics2D()) + return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (imagedata, + imageoffset, + imagelength)); + else + { + GdkPixbufDecoder d = new GdkPixbufDecoder (imagedata, + imageoffset, + imagelength); + GtkImage image = new GtkImage (d); + return image; + } + } + + /** + * Creates an ImageProducer from the specified URL. The image is assumed + * to be in a recognised format. + * + * @param url URL to read image data from. + */ + public ImageProducer createImageProducer(URL url) + { + return new GdkPixbufDecoder(url); + } + + /** + * Returns the native color model (which isn't the same as the default + * ARGB color model, but doesn't have to be). + */ + public ColorModel getColorModel () + { + /* Return the GDK-native ABGR format */ + return new DirectColorModel(32, + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0xFF000000); + } + + public String[] getFontList () + { + return (new String[] { "Dialog", + "DialogInput", + "Monospaced", + "Serif", + "SansSerif" }); + } + + private class LRUCache extends LinkedHashMap + { + int max_entries; + public LRUCache(int max) + { + super(max, 0.75f, true); + max_entries = max; + } + protected boolean removeEldestEntry(Map.Entry eldest) + { + return size() > max_entries; + } + } + + private LRUCache fontCache = new LRUCache(50); + private LRUCache metricsCache = new LRUCache(50); + private LRUCache imageCache = new LRUCache(50); + + public FontMetrics getFontMetrics (Font font) + { + synchronized (metricsCache) + { + if (metricsCache.containsKey(font)) + return (FontMetrics) metricsCache.get(font); + } + + FontMetrics m = new GdkFontMetrics (font); + synchronized (metricsCache) + { + metricsCache.put(font, m); + } + return m; + } + + public Image getImage (String filename) + { + if (imageCache.containsKey(filename)) + return (Image) imageCache.get(filename); + else + { + Image im = createImage(filename); + imageCache.put(filename, im); + return im; + } + } + + public Image getImage (URL url) + { + if (imageCache.containsKey(url)) + return (Image) imageCache.get(url); + else + { + Image im = createImage(url); + imageCache.put(url, im); + return im; + } + } + + public PrintJob getPrintJob (Frame frame, String jobtitle, Properties props) + { + return null; + } + + public native int getScreenResolution(); + + public Dimension getScreenSize () + { + int dim[] = new int[2]; + getScreenSizeDimensions(dim); + return new Dimension(dim[0], dim[1]); + } + + public Clipboard getSystemClipboard() + { + return systemClipboard; + } + + /** + * Prepares a GtkImage. For every other kind of Image it just + * assumes the image is already prepared for rendering. + */ + public boolean prepareImage (Image image, int width, int height, + ImageObserver observer) + { + /* GtkImages are always prepared, as long as they're loaded. */ + if (image instanceof GtkImage) + return ((((GtkImage)image).checkImage (observer) & + ImageObserver.ALLBITS) != 0); + + /* Assume anything else is too */ + return true; + } + + public native void sync(); + + protected void setComponentState (Component c, GtkComponentPeer cp) + { + /* Make the Component reflect Peer defaults */ + if (c.getForeground () == null) + c.setForeground (cp.getForeground ()); + if (c.getBackground () == null) + c.setBackground (cp.getBackground ()); + // if (c.getFont () == null) + // c.setFont (cp.getFont ()); + + /* Make the Peer reflect the state of the Component */ + if (! (c instanceof Window)) + { + cp.setCursor (c.getCursor ()); + + Rectangle bounds = c.getBounds (); + cp.setBounds (bounds.x, bounds.y, bounds.width, bounds.height); + cp.setVisible (c.isVisible ()); + } + } + + protected ButtonPeer createButton (Button b) + { + return new GtkButtonPeer (b); + } + + protected CanvasPeer createCanvas (Canvas c) + { + return new GtkCanvasPeer (c); + } + + protected CheckboxPeer createCheckbox (Checkbox cb) + { + return new GtkCheckboxPeer (cb); + } + + protected CheckboxMenuItemPeer createCheckboxMenuItem (CheckboxMenuItem cmi) + { + return new GtkCheckboxMenuItemPeer (cmi); + } + + protected ChoicePeer createChoice (Choice c) + { + return new GtkChoicePeer (c); + } + + protected DialogPeer createDialog (Dialog d) + { + return new GtkDialogPeer (d); + } + + protected FileDialogPeer createFileDialog (FileDialog fd) + { + return new GtkFileDialogPeer (fd); + } + + protected FramePeer createFrame (Frame f) + { + return new GtkFramePeer (f); + } + + protected LabelPeer createLabel (Label label) + { + return new GtkLabelPeer (label); + } + + protected ListPeer createList (List list) + { + return new GtkListPeer (list); + } + + protected MenuPeer createMenu (Menu m) + { + return new GtkMenuPeer (m); + } + + protected MenuBarPeer createMenuBar (MenuBar mb) + { + return new GtkMenuBarPeer (mb); + } + + protected MenuItemPeer createMenuItem (MenuItem mi) + { + return new GtkMenuItemPeer (mi); + } + + protected PanelPeer createPanel (Panel p) + { + return new GtkPanelPeer (p); + } + + protected PopupMenuPeer createPopupMenu (PopupMenu target) + { + return new GtkPopupMenuPeer (target); + } + + protected ScrollPanePeer createScrollPane (ScrollPane sp) + { + return new GtkScrollPanePeer (sp); + } + + protected ScrollbarPeer createScrollbar (Scrollbar sb) + { + return new GtkScrollbarPeer (sb); + } + + protected TextAreaPeer createTextArea (TextArea ta) + { + return new GtkTextAreaPeer (ta); + } + + protected TextFieldPeer createTextField (TextField tf) + { + return new GtkTextFieldPeer (tf); + } + + protected WindowPeer createWindow (Window w) + { + return new GtkWindowPeer (w); + } + + public EmbeddedWindowPeer createEmbeddedWindow (EmbeddedWindow w) + { + return new GtkEmbeddedWindowPeer (w); + } + + /** + * @deprecated part of the older "logical font" system in earlier AWT + * implementations. Our newer Font class uses getClasspathFontPeer. + */ + protected FontPeer getFontPeer (String name, int style) { + // All fonts get a default size of 12 if size is not specified. + return getFontPeer(name, style, 12); + } + + /** + * Private method that allows size to be set at initialization time. + */ + private FontPeer getFontPeer (String name, int style, int size) + { + Map attrs = new HashMap (); + ClasspathFontPeer.copyStyleToAttrs (style, attrs); + ClasspathFontPeer.copySizeToAttrs (size, attrs); + return getClasspathFontPeer (name, attrs); + } + + /** + * Newer method to produce a peer for a Font object, even though Sun's + * design claims Font should now be peerless, we do not agree with this + * model, hence "ClasspathFontPeer". + */ + + public ClasspathFontPeer getClasspathFontPeer (String name, Map attrs) + { + Map keyMap = new HashMap (attrs); + // We don't know what kind of "name" the user requested (logical, face, + // family), and we don't actually *need* to know here. The worst case + // involves failure to consolidate fonts with the same backend in our + // cache. This is harmless. + keyMap.put ("GtkToolkit.RequestedFontName", name); + if (fontCache.containsKey (keyMap)) + return (ClasspathFontPeer) fontCache.get (keyMap); + else + { + ClasspathFontPeer newPeer = new GdkFontPeer (name, attrs); + fontCache.put (keyMap, newPeer); + return newPeer; + } + } + + public ClasspathTextLayoutPeer getClasspathTextLayoutPeer (AttributedString str, + FontRenderContext frc) + { + return new GdkTextLayout(str, frc); + } + + protected EventQueue getSystemEventQueueImpl() + { + synchronized (GtkToolkit.class) + { + if (q == null) + { + q = new EventQueue(); + GtkGenericPeer.enableQueue (q); + } + } + return q; + } + + protected native void loadSystemColors (int[] systemColors); + + public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent e) + { + throw new Error("not implemented"); + } + + public Map mapInputMethodHighlight(InputMethodHighlight highlight) + { + throw new Error("not implemented"); + } + + public Rectangle getBounds() + { + int[] dims = new int[2]; + getScreenSizeDimensions(dims); + return new Rectangle(0, 0, dims[0], dims[1]); + } + + // ClasspathToolkit methods + + public GraphicsEnvironment getLocalGraphicsEnvironment() + { + return new GdkGraphicsEnvironment(this); + } + + public Font createFont(int format, InputStream stream) + { + throw new UnsupportedOperationException(); + } + + public RobotPeer createRobot (GraphicsDevice screen) throws AWTException + { + return new GdkRobotPeer (screen); + } + + public void registerImageIOSpis(IIORegistry reg) + { + GdkPixbufDecoder.registerSpis(reg); + } + + public native boolean nativeQueueEmpty(); + public native void wakeNativeQueue(); + public native void iterateNativeQueue(EventQueue locked, boolean block); + +} // class GtkToolkit diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkVolatileImage.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkVolatileImage.java new file mode 100644 index 00000000000..496090a0940 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkVolatileImage.java @@ -0,0 +1,118 @@ +/* GtkVolatileImage.java -- a hardware-accelerated image buffer + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.awt.peer.gtk; + +import java.awt.ImageCapabilities; +import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; +import java.awt.image.BufferedImage; +import java.awt.image.ImageObserver; +import java.awt.image.VolatileImage; + +public class GtkVolatileImage extends VolatileImage +{ + private int width; + private int height; + private ImageCapabilities caps; + + public GtkVolatileImage(int width, int height) + { + this(width, height, null); + } + + public GtkVolatileImage(int width, int height, ImageCapabilities caps) + { + this.width = width; + this.height = height; + this.caps = caps; + } + + // FIXME: should return a buffered image snapshot of the accelerated + // visual + public BufferedImage getSnapshot() + { + return null; + } + + public int getWidth() + { + return width; + } + + public int getHeight() + { + return height; + } + + // FIXME: should return a graphics wrapper around this image's + // visual + public Graphics2D createGraphics() + { + return null; + } + + public int validate(GraphicsConfiguration gc) + { + return VolatileImage.IMAGE_OK; + } + + public boolean contentsLost() + { + return false; + } + + public ImageCapabilities getCapabilities() + { + return caps; + } + + public synchronized Object getProperty (String name, ImageObserver observer) + { + return null; + } + + public synchronized int getWidth (ImageObserver observer) + { + return width; + } + + public synchronized int getHeight (ImageObserver observer) + { + return height; + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkWindowPeer.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkWindowPeer.java new file mode 100644 index 00000000000..71e05a87dad --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkWindowPeer.java @@ -0,0 +1,212 @@ +/* GtkWindowPeer.java -- Implements WindowPeer with GTK + Copyright (C) 1998, 1999, 2002, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; + +import java.awt.Component; +import java.awt.Frame; +import java.awt.Window; +import java.awt.event.WindowEvent; +import java.awt.peer.WindowPeer; + +public class GtkWindowPeer extends GtkContainerPeer + implements WindowPeer +{ + protected static final int GDK_WINDOW_TYPE_HINT_NORMAL = 0; + protected static final int GDK_WINDOW_TYPE_HINT_DIALOG = 1; + protected static final int GDK_WINDOW_TYPE_HINT_MENU = 2; + protected static final int GDK_WINDOW_TYPE_HINT_TOOLBAR = 3; + protected static final int GDK_WINDOW_TYPE_HINT_SPLASHSCREEN = 4; + protected static final int GDK_WINDOW_TYPE_HINT_UTILITY = 5; + protected static final int GDK_WINDOW_TYPE_HINT_DOCK = 6; + protected static final int GDK_WINDOW_TYPE_HINT_DESKTOP = 7; + + private boolean hasBeenShown = false; + private int oldState = Frame.NORMAL; + + native void gtkWindowSetTitle (String title); + native void gtkWindowSetResizable (boolean resizable); + native void gtkWindowSetModal (boolean modal); + + native void realize (); + + int getWidth () + { + return awtComponent.getWidth(); + } + + int getHeight () + { + return awtComponent.getHeight(); + } + + native void create (int type, boolean decorated, GtkWindowPeer parent); + + void create (int type, boolean decorated) + { + GtkWindowPeer parent_peer = null; + Component parent = awtComponent.getParent(); + + if (parent != null) + parent_peer = (GtkWindowPeer) awtComponent.getParent().getPeer(); + + create (type, decorated, parent_peer); + } + + void create () + { + // Create a normal undecorated window. + create (GDK_WINDOW_TYPE_HINT_NORMAL, false); + } + + void setParent () + { + setVisible (awtComponent.isVisible ()); + setEnabled (awtComponent.isEnabled ()); + } + + void setVisibleAndEnabled () + { + } + + native void connectSignals (); + + public GtkWindowPeer (Window window) + { + super (window); + } + + public native void toBack(); + public native void toFront(); + + native void nativeSetBounds (int x, int y, int width, int height); + + public void setBounds (int x, int y, int width, int height) + { + nativeSetBounds (x, y, + width - insets.left - insets.right, + height - insets.top - insets.bottom); + } + + public void setTitle (String title) + { + gtkWindowSetTitle (title); + } + + native void setSize (int width, int height); + + public void setResizable (boolean resizable) + { + // Call setSize; otherwise when resizable is changed from true to + // false the window will shrink to the dimensions it had before it + // was resizable. + setSize (awtComponent.getWidth() - insets.left - insets.right, + awtComponent.getHeight() - insets.top - insets.bottom); + gtkWindowSetResizable (resizable); + } + + native void setBoundsCallback (Window window, + int x, int y, + int width, int height); + + protected void postInsetsChangedEvent (int top, int left, + int bottom, int right) + { + insets.top = top; + insets.left = left; + insets.bottom = bottom; + insets.right = right; + } + + protected void postConfigureEvent (int x, int y, int width, int height) + { + int frame_x = x - insets.left; + int frame_y = y - insets.top; + int frame_width = width + insets.left + insets.right; + int frame_height = height + insets.top + insets.bottom; + + if (frame_x != awtComponent.getX() + || frame_y != awtComponent.getY() + || frame_width != awtComponent.getWidth() + || frame_height != awtComponent.getHeight()) + { + setBoundsCallback ((Window) awtComponent, + frame_x, frame_y, frame_width, frame_height); + + awtComponent.validate(); + } + } + + native void nativeSetVisible (boolean b); + public void setVisible (boolean b) + { + // Prevent the window manager from automatically placing this + // window when it is shown. + if (b) + setBounds (awtComponent.getX(), + awtComponent.getY(), + awtComponent.getWidth(), + awtComponent.getHeight()); + nativeSetVisible (b); + } + + void postWindowEvent (int id, Window opposite, int newState) + { + if (id == WindowEvent.WINDOW_OPENED) + { + // Post a WINDOW_OPENED event the first time this window is shown. + if (!hasBeenShown) + { + q().postEvent (new WindowEvent ((Window) awtComponent, id, + opposite)); + hasBeenShown = true; + } + } + else if (id == WindowEvent.WINDOW_STATE_CHANGED) + { + if (oldState != newState) + { + q().postEvent (new WindowEvent ((Window) awtComponent, id, opposite, + oldState, newState)); + oldState = newState; + } + } + else + q().postEvent (new WindowEvent ((Window) awtComponent, id, opposite)); + } +} diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/package.html b/libjava/classpath/gnu/java/awt/peer/gtk/package.html new file mode 100644 index 00000000000..8dd4dd8922e --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/gtk/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.awt.peer.gtk package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.awt.peer.gtk</title></head> + +<body> +<p>This package implements the GTK peer for java.awt.</p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/awt/peer/package.html b/libjava/classpath/gnu/java/awt/peer/package.html new file mode 100644 index 00000000000..846759a284e --- /dev/null +++ b/libjava/classpath/gnu/java/awt/peer/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.awt.peer package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.awt.peer</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/beans/BeanInfoEmbryo.java b/libjava/classpath/gnu/java/beans/BeanInfoEmbryo.java new file mode 100644 index 00000000000..0cf73e5b9ae --- /dev/null +++ b/libjava/classpath/gnu/java/beans/BeanInfoEmbryo.java @@ -0,0 +1,171 @@ +/* gnu.java.beans.BeanInfoEmbryo + Copyright (C) 1998, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans; + +import java.beans.BeanDescriptor; +import java.beans.BeanInfo; +import java.beans.EventSetDescriptor; +import java.beans.IndexedPropertyDescriptor; +import java.beans.MethodDescriptor; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; +import java.util.Vector; + +/** + ** A BeanInfoEmbryo accumulates information about a Bean + ** while it is in the process of being created, and then + ** when you are done accumulating the information, the + ** getBeanInfo() method may be called to create a BeanInfo + ** object based on the information.<P> + ** + ** This class is not well-synchronized. (It can be, it + ** just isn't yet.) + ** + ** @author John Keiser + ** @version 1.1.0, 30 Jul 1998 + ** @see java.beans.BeanInfo + **/ + +public class BeanInfoEmbryo { + + // by using a TreeMap the properties will be sorted alphabetically by name + // which matches the (undocumented) behavior of jdk + TreeMap properties = new TreeMap(); + Hashtable events = new Hashtable(); + Vector methods = new Vector(); + + BeanDescriptor beanDescriptor; + BeanInfo[] additionalBeanInfo; + java.awt.Image[] im; + String defaultPropertyName; + String defaultEventName; + + public BeanInfoEmbryo() { + } + + public BeanInfo getBeanInfo() { + int defaultProperty = -1; + int defaultEvent = -1; + + PropertyDescriptor[] Aproperties = new PropertyDescriptor[properties.size()]; + int i = 0; + Iterator it = properties.entrySet().iterator(); + while (it.hasNext()) { + Aproperties[i] = (PropertyDescriptor) (((Map.Entry)it.next()).getValue()); + if(defaultPropertyName != null && Aproperties[i].getName().equals(defaultPropertyName)) { + defaultProperty = i; + } + i++; + } + + EventSetDescriptor[] Aevents = new EventSetDescriptor[events.size()]; + i = 0; + Enumeration e = events.elements(); + while (e.hasMoreElements()) { + Aevents[i] = (EventSetDescriptor) e.nextElement(); + if(defaultEventName != null && Aevents[i].getName().equals(defaultEventName)) { + defaultEvent = i; + } + i++; + } + + MethodDescriptor[] Amethods = new MethodDescriptor[methods.size()]; + methods.copyInto(Amethods); + + return new ExplicitBeanInfo(beanDescriptor,additionalBeanInfo,Aproperties,defaultProperty,Aevents,defaultEvent,Amethods,im); + } + + public void setBeanDescriptor(BeanDescriptor b) { + beanDescriptor = b; + } + + public void setAdditionalBeanInfo(BeanInfo[] b) { + additionalBeanInfo = b; + } + + public boolean hasProperty(PropertyDescriptor p) { + return properties.get(p.getName()) != null; + } + public void addProperty(PropertyDescriptor p) { + properties.put(p.getName(),p); + } + public void addIndexedProperty(IndexedPropertyDescriptor p) { + properties.put(p.getName(),p); + } + + public boolean hasEvent(EventSetDescriptor e) { + return events.get(e.getName()) != null; + } + public void addEvent(EventSetDescriptor e) { + events.put(e.getName(),e); + } + + public boolean hasMethod(MethodDescriptor m) { + for(int i=0;i<methods.size();i++) { + Method thisMethod = ((MethodDescriptor)methods.elementAt(i)).getMethod(); + if(m.getMethod().getName().equals(thisMethod.getName()) + && Arrays.equals(m.getMethod().getParameterTypes(), + thisMethod.getParameterTypes())) { + return true; + } + } + return false; + } + public void addMethod(MethodDescriptor m) { + methods.addElement(m); + } + + public void setDefaultPropertyName(String defaultPropertyName) { + this.defaultPropertyName = defaultPropertyName; + } + + public void setDefaultEventName(String defaultEventName) { + this.defaultEventName = defaultEventName; + } + + public void setIcons(java.awt.Image[] im) { + this.im = im; + } +} diff --git a/libjava/classpath/gnu/java/beans/DummyAppletContext.java b/libjava/classpath/gnu/java/beans/DummyAppletContext.java new file mode 100644 index 00000000000..4facb470d95 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/DummyAppletContext.java @@ -0,0 +1,200 @@ +/* gnu.java.beans.DummyAppletContext + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans; + +import java.applet.Applet; +import java.applet.AppletContext; +import java.applet.AudioClip; +import java.awt.Image; +import java.awt.Toolkit; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; + +/** A placeholder <code>AppletContext</code> implementation that does nothing. + * + * <p>This is the default implementation for GNU Classpath and is used for <code>Applet</code> + * beans being created with {@link java.beans.Beans.instantiate}.</p> + * + * <p>It has no functionality in order to allow it to be used without any dependencies + * (e.g. sound, network access, ...).</p> + * + * @author Robert Schuster + */ +class DummyAppletContext implements AppletContext +{ + private static final Enumeration EMPTY_ENUMERATION = Collections.enumeration(Collections.EMPTY_SET); + private static final AudioClip DUMMY_CLIP = new DummyAudioClip(); + + DummyAppletContext() + { + } + + /** Implementation is VM neutral and returns a dummy {@link AudioClip} instance + * for every URL that returns a non-<code>null</code> object on + * <code>URL.openConnection()</code>. + * + * @see java.applet.AppletContext#getAudioClip(java.net.URL) + * + * FIXME: When Java Sound API (javax.sound) is included in Classpath or URL is able to handle + * sampled sound this should be adjusted. + */ + public AudioClip getAudioClip(URL url) + { + try + { + return (url.openConnection() != null ? DUMMY_CLIP : null); + } + catch (IOException ioe) + { + return null; + } + } + + /** Loads the <code>Image</code> instance by delegating to + * {@link java.awt.Toolkit.createImage(URL) }. + * + * @see java.applet.AppletContext#getImage(java.net.URL) + * @see java.awt.Toolkit#createImage(java.net.URL) + */ + public Image getImage(URL url) + { + return Toolkit.getDefaultToolkit().createImage(url); + } + + /** Returns <code>null</code> for every argument. + * + * @see java.applet.AppletContext#getApplet(java.lang.String) + */ + public Applet getApplet(String name) + { + return null; + } + + /** Returns always an empty <code>Enumeration</code>. + * + * @see java.applet.AppletContext#getApplets() + */ + public Enumeration getApplets() + { + return EMPTY_ENUMERATION; + } + + /** Does nothing. + * + * @see java.applet.AppletContext#showDocument(java.net.URL) + */ + public void showDocument(URL url) + { + } + + /** Does nothing. + * + * @see java.applet.AppletContext#showDocument(java.net.URL, java.lang.String) + */ + public void showDocument(URL url, String target) + { + } + + /** Does nothing. + * + * @see java.applet.AppletContext#showStatus(java.lang.String) + */ + public void showStatus(String message) + { + } + + /** Does nothing. + * + * @see java.applet.AppletContext#setStream(java.lang.String, java.io.InputStream) + */ + public void setStream(String key, InputStream stream) + throws IOException + { + throw new IOException("Dummy implementation imposes zero InputStream associations."); + } + + /** Returns <code>null</code> for every argument. + * + * @see java.applet.AppletContext#getStream(java.lang.String) + */ + public InputStream getStream(String key) + { + return null; + } + + /** Returns always an empty iterator. + * + * @see java.applet.AppletContext#getStreamKeys() + */ + public Iterator getStreamKeys() + { + return Collections.EMPTY_SET.iterator(); + } + + /** Dummy <code>AudioClip</code> implementation that does nothing but + * preventing <code>NullPointerException</code>S being thrown in programs + * that expect a valid <code>AudioClip</code> instance to be returned by + * their Applet. + * + * @author Robert Schuster + */ + static class DummyAudioClip implements AudioClip + { + public void play() + { + } + + public void stop() + { + } + + public void loop() + { + } + + public String toString() + { + return "DummyAudioClip never plays anything - implement javax.sound and make us happy :)"; + } + } +} diff --git a/libjava/classpath/gnu/java/beans/DummyAppletStub.java b/libjava/classpath/gnu/java/beans/DummyAppletStub.java new file mode 100644 index 00000000000..3bcb43534cf --- /dev/null +++ b/libjava/classpath/gnu/java/beans/DummyAppletStub.java @@ -0,0 +1,115 @@ +/* gnu.java.beans.DummyAppletStub + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans; + +import java.applet.AppletContext; +import java.applet.AppletStub; +import java.net.URL; + +/** Placeholder implementation of <code>AppletStub</code> providing no functionality. + * <p>This class is used for <code>Applet</code> being created with + * {@link java.beans.Bean.instantiate}.</p> + * + * @author Robert Schuster + */ +public class DummyAppletStub implements AppletStub +{ + private URL documentBase; + private URL codeBase; + private DummyAppletContext context; + + public DummyAppletStub(URL newCodeBase, URL newDocumentBase) + { + codeBase = newCodeBase; + documentBase = newDocumentBase; + + context = new DummyAppletContext(); + } + + /** Returns always <code>true</code>. + * + * @see java.applet.AppletStub#isActive() + */ + public boolean isActive() + { + return true; + } + + /** + * @see java.applet.AppletStub#getDocumentBase() + */ + public URL getDocumentBase() + { + return documentBase; + } + + /** + * @see java.applet.AppletStub#getCodeBase() + */ + public URL getCodeBase() + { + return codeBase; + } + + /** Implementation returns <code>null</code> for every parameter name. + * + * @see java.applet.AppletStub#getParameter(java.lang.String) + */ + public String getParameter(String name) + { + return null; + } + + /** Returns a non-functional context instance. + * + * @see java.applet.AppletStub#getAppletContext() + */ + public AppletContext getAppletContext() + { + return context; + } + + /** Does nothing. + * + * @see java.applet.AppletStub#appletResize(int, int) + */ + public void appletResize(int width, int height) + { + } +} diff --git a/libjava/classpath/gnu/java/beans/ExplicitBeanInfo.java b/libjava/classpath/gnu/java/beans/ExplicitBeanInfo.java new file mode 100644 index 00000000000..6da5e869695 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/ExplicitBeanInfo.java @@ -0,0 +1,149 @@ +/* ExplicitBeanInfo.java -- + Copyright (C) 1998, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans; + +import java.awt.Image; +import java.beans.BeanDescriptor; +import java.beans.BeanInfo; +import java.beans.EventSetDescriptor; +import java.beans.MethodDescriptor; +import java.beans.PropertyDescriptor; + +/** + ** ExplicitBeanInfo lets you specify in the constructor + ** all the various parts of the BeanInfo. + ** + ** @author John Keiser + ** @version 1.1.0, 30 Jul 1998 + ** @see java.beans.BeanInfo + **/ + +public class ExplicitBeanInfo implements BeanInfo { + /** The BeanDescriptor returned by getBeanDescriptor. **/ + protected BeanDescriptor beanDescriptor; + + /** The EventSetDescriptor array returned by + ** getEventSetDescriptors(). + **/ + protected EventSetDescriptor[] eventSetDescriptors = new EventSetDescriptor[0]; + + /** The PropertyDescriptor array returned by + ** getPropertyDescriptors(). + **/ + protected PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[0]; + + /** The MethodDescriptor array returned by + ** getMethodDescriptors(). + **/ + protected MethodDescriptor[] methodDescriptors; + + /** The default property index. **/ + protected int defaultPropertyIndex; + + /** The default event index. **/ + protected int defaultEventIndex; + + /** The BeanInfo array returned by + ** getAdditionalBeanInfo(). + **/ + protected BeanInfo[] additionalBeanInfo; + + /** The set of icons. **/ + protected Image[] icons; + + public ExplicitBeanInfo(BeanDescriptor beanDescriptor, + BeanInfo[] additionalBeanInfo, + PropertyDescriptor[] propertyDescriptors, + int defaultPropertyIndex, + EventSetDescriptor[] eventSetDescriptors, + int defaultEventIndex, + MethodDescriptor[] methodDescriptors, + Image[] icons) { + this.beanDescriptor = beanDescriptor; + this.additionalBeanInfo = additionalBeanInfo; + this.propertyDescriptors = propertyDescriptors; + this.defaultPropertyIndex = defaultPropertyIndex; + this.eventSetDescriptors = eventSetDescriptors; + this.defaultEventIndex = defaultEventIndex; + this.methodDescriptors = methodDescriptors; + this.icons = icons; + } + + /** Get Bean descriptor. **/ + public BeanDescriptor getBeanDescriptor() { + return beanDescriptor; + } + + /** Get Bean events. **/ + public EventSetDescriptor[] getEventSetDescriptors() { + return eventSetDescriptors; + } + + /** Get default event set. **/ + public int getDefaultEventIndex() { + return defaultEventIndex; + } + + /** Get Bean properties. **/ + public PropertyDescriptor[] getPropertyDescriptors() { + return propertyDescriptors; + } + + /** Get "default" property. **/ + public int getDefaultPropertyIndex() { + return defaultPropertyIndex; + } + + /** Get Bean methods. **/ + public MethodDescriptor[] getMethodDescriptors() { + return methodDescriptors; + } + + /** Get additional Bean info. **/ + public BeanInfo[] getAdditionalBeanInfo() { + return additionalBeanInfo; + } + + /** Get Bean icons. + ** @param iconType the type of icon + **/ + public Image getIcon(int iconType) { + return icons != null ? icons[iconType - 1] : null; + } +} diff --git a/libjava/classpath/gnu/java/beans/IntrospectionIncubator.java b/libjava/classpath/gnu/java/beans/IntrospectionIncubator.java new file mode 100644 index 00000000000..e0d9480e327 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/IntrospectionIncubator.java @@ -0,0 +1,441 @@ +/* gnu.java.beans.IntrospectionIncubator + Copyright (C) 1998, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans; + +import gnu.java.lang.ArrayHelper; +import gnu.java.lang.ClassHelper; + +import java.beans.BeanInfo; +import java.beans.EventSetDescriptor; +import java.beans.IndexedPropertyDescriptor; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.MethodDescriptor; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Array; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; + +/** + ** IntrospectionIncubator takes in a bunch of Methods, and + ** Introspects only those Methods you give it.<br/> + ** + ** See {@link addMethod(Method)} for details which rules apply to + ** the methods. + ** + ** @author John Keiser + ** @author Robert Schuster + ** @see gnu.java.beans.ExplicitBeanInfo + ** @see java.beans.BeanInfo + **/ + +public class IntrospectionIncubator { + Hashtable propertyMethods = new Hashtable(); + Hashtable listenerMethods = new Hashtable(); + Vector otherMethods = new Vector(); + + Class propertyStopClass; + Class eventStopClass; + Class methodStopClass; + + public IntrospectionIncubator() { + } + + /** Examines the given method and files it in a suitable collection. + * It files the method as a property method if it finds: + * <ul> + * <li>boolean "is" getter</li> + * <li>"get" style getter</li> + * <li>single argument setter</li> + * <li>indiced setter and getter</li> + * </ul> + * It files the method as a listener method if all of these rules apply: + * <ul> + * <li>the method name starts with "add" or "remove"</li> + * <li>there is only a single argument</li> + * <li>the argument type is a subclass of <code>java.util.EventListener</code></li> + * </ul> + * All public methods are filed as such. + * + * @param method The method instance to examine. + */ + public void addMethod(Method method) { + if(Modifier.isPublic(method.getModifiers())) { + String name = ClassHelper.getTruncatedName(method.getName()); + Class retType = method.getReturnType(); + Class[] params = method.getParameterTypes(); + boolean isVoid = retType.equals(java.lang.Void.TYPE); + Class methodClass = method.getDeclaringClass(); + + /* Accepts the method for examination if no stop class is given or the method is declared in a subclass of the stop class. + * The rules for this are described in {@link java.beans.Introspector.getBeanInfo(Class, Class)}. + * This block finds out whether the method is a suitable getter or setter method (or read/write method). + */ + if(isReachable(propertyStopClass, methodClass)) { + /* At this point a method may regarded as a property's read or write method if its name + * starts with "is", "get" or "set". However, if a method is static it cannot be part + * of a property. + */ + if(Modifier.isStatic(method.getModifiers())) { + // files method as other because it is static + otherMethods.addElement(method); + } else if(name.startsWith("is") + && retType.equals(java.lang.Boolean.TYPE) + && params.length == 0) { + // files method as boolean "is" style getter + addToPropertyHash(name,method,IS); + } else if(name.startsWith("get") && !isVoid) { + if(params.length == 0) { + // files as legal non-argument getter + addToPropertyHash(name,method,GET); + } else if(params.length == 1 && params[0].equals(java.lang.Integer.TYPE)) { + // files as legal indiced getter + addToPropertyHash(name,method,GET_I); + } else { + // files as other because the method's signature is not Bean-like + otherMethods.addElement(method); + } + } else if(name.startsWith("set") && isVoid) { + if(params.length == 1) { + // files as legal single-argument setter method + addToPropertyHash(name,method,SET); + } else if(params.length == 2 && params[0].equals(java.lang.Integer.TYPE)) { + // files as legal indiced setter method + addToPropertyHash(name,method,SET_I); + } else { + // files as other because the method's signature is not Bean-like + otherMethods.addElement(method); + } + } + } + + if(isReachable(eventStopClass, methodClass)) { + if(name.startsWith("add") + && isVoid + && params.length == 1 + && java.util.EventListener.class.isAssignableFrom(params[0])) { + addToListenerHash(name,method,ADD); + } else if(name.startsWith("remove") + && isVoid + && params.length == 1 + && java.util.EventListener.class.isAssignableFrom(params[0])) { + addToListenerHash(name,method,REMOVE); + } + } + + if(isReachable(methodStopClass, methodClass)) { + // files as reachable public method + otherMethods.addElement(method); + } + + } + } + + public void addMethods(Method[] m) { + for(int i=0;i<m.length;i++) { + addMethod(m[i]); + } + } + + public void setPropertyStopClass(Class c) { + propertyStopClass = c; + } + + public void setEventStopClass(Class c) { + eventStopClass = c; + } + + public void setMethodStopClass(Class c) { + methodStopClass = c; + } + + + public BeanInfoEmbryo getBeanInfoEmbryo() throws IntrospectionException { + BeanInfoEmbryo b = new BeanInfoEmbryo(); + findXXX(b,IS); + findXXXInt(b,GET_I); + findXXXInt(b,SET_I); + findXXX(b,GET); + findXXX(b,SET); + findAddRemovePairs(b); + for(int i=0;i<otherMethods.size();i++) { + MethodDescriptor newMethod = new MethodDescriptor((Method)otherMethods.elementAt(i)); + if(!b.hasMethod(newMethod)) { + b.addMethod(new MethodDescriptor((Method)otherMethods.elementAt(i))); + } + } + return b; + } + + public BeanInfo getBeanInfo() throws IntrospectionException { + return getBeanInfoEmbryo().getBeanInfo(); + } + + + void findAddRemovePairs(BeanInfoEmbryo b) throws IntrospectionException { + Enumeration listenerEnum = listenerMethods.keys(); + while(listenerEnum.hasMoreElements()) { + DoubleKey k = (DoubleKey)listenerEnum.nextElement(); + Method[] m = (Method[])listenerMethods.get(k); + if(m[ADD] != null && m[REMOVE] != null) { + EventSetDescriptor e = new EventSetDescriptor(Introspector.decapitalize(k.getName()), + k.getType(), k.getType().getMethods(), + m[ADD],m[REMOVE]); + e.setUnicast(ArrayHelper.contains(m[ADD].getExceptionTypes(),java.util.TooManyListenersException.class)); + if(!b.hasEvent(e)) { + b.addEvent(e); + } + } + } + } + + void findXXX(BeanInfoEmbryo b, int funcType) throws IntrospectionException { + Enumeration keys = propertyMethods.keys(); + while(keys.hasMoreElements()) { + DoubleKey k = (DoubleKey)keys.nextElement(); + Method[] m = (Method[])propertyMethods.get(k); + if(m[funcType] != null) { + PropertyDescriptor p = new PropertyDescriptor(Introspector.decapitalize(k.getName()), + m[IS] != null ? m[IS] : m[GET], + m[SET]); + if(m[SET] != null) { + p.setConstrained(ArrayHelper.contains(m[SET].getExceptionTypes(),java.beans.PropertyVetoException.class)); + } + if(!b.hasProperty(p)) { + b.addProperty(p); + } + } + } + } + + void findXXXInt(BeanInfoEmbryo b, int funcType) throws IntrospectionException { + Enumeration keys = propertyMethods.keys(); + while(keys.hasMoreElements()) { + DoubleKey k = (DoubleKey)keys.nextElement(); + Method[] m = (Method[])propertyMethods.get(k); + if(m[funcType] != null) { + boolean constrained; + if(m[SET_I] != null) { + constrained = ArrayHelper.contains(m[SET_I].getExceptionTypes(),java.beans.PropertyVetoException.class); + } else { + constrained = false; + } + + /** Find out if there is an array type get or set **/ + Class arrayType = Array.newInstance(k.getType(),0).getClass(); + DoubleKey findSetArray = new DoubleKey(arrayType,k.getName()); + Method[] m2 = (Method[])propertyMethods.get(findSetArray); + IndexedPropertyDescriptor p; + if(m2 == null) { + p = new IndexedPropertyDescriptor(Introspector.decapitalize(k.getName()), + null,null, + m[GET_I],m[SET_I]); + } else { + if(constrained && m2[SET] != null) { + constrained = ArrayHelper.contains(m2[SET].getExceptionTypes(),java.beans.PropertyVetoException.class); + } + p = new IndexedPropertyDescriptor(Introspector.decapitalize(k.getName()), + m2[GET],m2[SET], + m[GET_I],m[SET_I]); + } + p.setConstrained(constrained); + if(!b.hasProperty(p)) { + b.addProperty(p); + } + } + } + } + + static final int IS=0; + static final int GET_I=1; + static final int SET_I=2; + static final int GET=3; + static final int SET=4; + + static final int ADD=0; + static final int REMOVE=1; + + void addToPropertyHash(String name, Method method, int funcType) { + String newName; + Class type; + + switch(funcType) { + case IS: + type = java.lang.Boolean.TYPE; + newName = name.substring(2); + break; + case GET_I: + type = method.getReturnType(); + newName = name.substring(3); + break; + case SET_I: + type = method.getParameterTypes()[1]; + newName = name.substring(3); + break; + case GET: + type = method.getReturnType(); + newName = name.substring(3); + break; + case SET: + type = method.getParameterTypes()[0]; + newName = name.substring(3); + break; + default: + return; + } + newName = capitalize(newName); + if (newName.length() == 0) + return; + + DoubleKey k = new DoubleKey(type,newName); + Method[] methods = (Method[])propertyMethods.get(k); + if(methods == null) { + methods = new Method[5]; + propertyMethods.put(k,methods); + } + methods[funcType] = method; + } + + void addToListenerHash(String name, Method method, int funcType) { + String newName; + Class type; + + switch(funcType) { + case ADD: + type = method.getParameterTypes()[0]; + newName = name.substring(3,name.length()-8); + break; + case REMOVE: + type = method.getParameterTypes()[0]; + newName = name.substring(6,name.length()-8); + break; + default: + return; + } + newName = capitalize(newName); + if (newName.length() == 0) + return; + + DoubleKey k = new DoubleKey(type,newName); + Method[] methods = (Method[])listenerMethods.get(k); + if(methods == null) { + methods = new Method[2]; + listenerMethods.put(k,methods); + } + methods[funcType] = method; + } + + /* Determines whether <code>stopClass</code> is <code>null</code> + * or <code>declaringClass<code> is a true subclass of <code>stopClass</code>. + * This expression is useful to detect whether a method should be introspected or not. + * The rules for this are described in {@link java.beans.Introspector.getBeanInfo(Class, Class)}. + */ + static boolean isReachable(Class stopClass, Class declaringClass) { + return stopClass == null || (stopClass.isAssignableFrom(declaringClass) && !stopClass.equals(declaringClass)); + } + + /** Transforms a property name into a part of a method name. + * E.g. "value" becomes "Value" which can then concatenated with + * "set", "get" or "is" to form a valid method name. + * + * Implementation notes: + * If "" is the argument, it is returned without changes. + * If <code>null</code> is the argument, <code>null</code> is returned. + * + * @param name Name of a property. + * @return Part of a method name of a property. + */ + static String capitalize(String name) { + try { + if(Character.isUpperCase(name.charAt(0))) { + return name; + } else { + char[] c = name.toCharArray(); + c[0] = Character.toLowerCase(c[0]); + return new String(c); + } + } catch(StringIndexOutOfBoundsException E) { + return name; + } catch(NullPointerException E) { + return null; + } + } +} + +/** This class is a hashmap key that consists of a <code>Class</code> and a + * <code>String</code> element. + * + * It is used for XXX: find out what this is used for + * + * @author John Keiser + * @author Robert Schuster + */ +class DoubleKey { + Class type; + String name; + + DoubleKey(Class type, String name) { + this.type = type; + this.name = name; + } + + Class getType() { + return type; + } + + String getName() { + return name; + } + + public boolean equals(Object o) { + if(o instanceof DoubleKey) { + DoubleKey d = (DoubleKey)o; + return d.type.equals(type) && d.name.equals(name); + } else { + return false; + } + } + + public int hashCode() { + return type.hashCode() ^ name.hashCode(); + } +} diff --git a/libjava/classpath/gnu/java/beans/TODO b/libjava/classpath/gnu/java/beans/TODO new file mode 100644 index 00000000000..9112806ba70 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/TODO @@ -0,0 +1 @@ +- overhaul efficiency diff --git a/libjava/classpath/gnu/java/beans/decoder/AbstractContext.java b/libjava/classpath/gnu/java/beans/decoder/AbstractContext.java new file mode 100644 index 00000000000..0e95d432986 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/AbstractContext.java @@ -0,0 +1,70 @@ +/* gnu.java.beans.decoder.AbstractContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** AbstractContext implements some basic functionality of the Context + * interface and is therefore the base of all Context implementations. + * + * @author Robert Schuster + */ +abstract class AbstractContext implements Context +{ + private boolean isStatement; + private String id; + + public String getId() + { + return id; + } + + public void setId(String newId) + { + id = newId; + } + + public boolean isStatement() + { + return isStatement; + } + + public void setStatement(boolean b) + { + isStatement = b; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/AbstractCreatableObjectContext.java b/libjava/classpath/gnu/java/beans/decoder/AbstractCreatableObjectContext.java new file mode 100644 index 00000000000..47f04fa1cbe --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/AbstractCreatableObjectContext.java @@ -0,0 +1,113 @@ +/* gnu.java.beans.decoder.AbstractCreatableContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.beans.decoder; + + +/** AbstractCreatableObjectContext is the base class for all Context implementations + * which create a result object in their lifetime. It provides means for preventing + * to create the object twice. + * + * @author Robert Schuster + * + */ +abstract class AbstractCreatableObjectContext extends AbstractObjectContext +{ + AbstractCreatableObjectContext() + { + } + + /** Adds a parameter object to this Context if the result object has not been + * created yet. Otherwise an AssemblyException is thrown that indicates a wrong + * behavior of the decoder. + */ + public final void addParameterObject(Object o) throws AssemblyException + { + if (object == null) + addParameterObjectImpl(o); + else + throw new AssemblyException(new IllegalStateException("No more parameter objects are allowed when the object as already been created.")); + } + + /** Adds a parameter object to this Context. Implement this without caring + * for illegal states because this has been done already. + * + * @param obj The parameter object to be added. + */ + protected abstract void addParameterObjectImpl(Object obj); + + /** Creates the result object if it does not exist already. + */ + public final void notifyStatement(Context outerContext) + throws AssemblyException + { + if (object != null) + return; + + object = createObject(outerContext); + } + + /** Creates the result object. This method is called only once. Implement this + * without checking for double invocations as this is already being prevented. + * + * @param outerContext The Context that exists around this one. + * @return The result object. + * @throws AssemblerException if the object creation fails somehow. + */ + protected abstract Object createObject(Context outerContext) + throws AssemblyException; + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public final Object endContext(Context outerContext) + throws AssemblyException + { + notifyStatement(outerContext); + return object; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + /* Returns true when the AbstractCreatableObjectContext has not created the result object yet + * (A failed subcontext automatically lets this context fail too.) + */ + return object == null; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/AbstractElementHandler.java b/libjava/classpath/gnu/java/beans/decoder/AbstractElementHandler.java new file mode 100644 index 00000000000..e5578a990bc --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/AbstractElementHandler.java @@ -0,0 +1,316 @@ +/* gnu.java.beans.decoder.AbstractElementHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +import org.xml.sax.Attributes; + +/** ElementHandler manages a Context instance and interacts with + * its parent and child handlers. + * + * @author Robert Schuster + */ +abstract class AbstractElementHandler implements ElementHandler +{ + /** The Context instance of this handler. The instance is available after the startElement() + * method was called. Otherwise the handler is marked as failed. + */ + private Context context; + + /** The parent handler. */ + private ElementHandler parent; + + /** Stores whether this handler is marked as failed. */ + private boolean hasFailed; + + /** Stores the character data which is contained in the body of the XML tag. */ + private StringBuffer buffer = new StringBuffer(); + + /** Stores whether this ElementHandler can have subelements. The information for this is taken from + * javabeans.dtd which can be found here: + * <a href="http://java.sun.com/products/jfc/tsc/articles/persistence3/">Java Persistence Article</a> + */ + private boolean allowsSubelements; + + /** Creates a new ElementHandler with the given ElementHandler instance + * as parent. + * + * @param parentHandler The parent handler. + */ + protected AbstractElementHandler(ElementHandler parentHandler, + boolean allowsSubs) + { + parent = parentHandler; + allowsSubelements = allowsSubs; + } + + /** Evaluates the attributes and creates a Context instance. + * If the creation of the Context instance fails the ElementHandler + * is marked as failed which may affect the parent handler other. + * + * @param attributes Attributes of the XML tag. + */ + public final void start(Attributes attributes, + ExceptionListener exceptionListener) + { + try + { + // lets the subclass create the appropriate Context instance + context = startElement(attributes, exceptionListener); + } + catch (AssemblyException pe) + { + Throwable t = pe.getCause(); + + if (t instanceof Exception) + exceptionListener.exceptionThrown((Exception) t); + else + throw new InternalError("Unexpected Throwable type in AssemblerException. Please file a bug report."); + + notifyContextFailed(); + + return; + } + } + + /** Analyses the content of the Attributes instance and creates a Context + * object accordingly. + * An AssemblerException is thrown when the Context instance could not + * be created. + * + * @param attributes Attributes of the XML tag. + * @return A Context instance. + * @throws AssemblerException when Context instance could not be created. + */ + protected abstract Context startElement(Attributes attributes, ExceptionListener exceptionListener) + throws AssemblyException; + + /** Post-processes the Context. + */ + public final void end(ExceptionListener exceptionListener) + { + // skips processing if the handler is marked as failed (because the Context + // is then invalid or may not exist at all) + if (!hasFailed) + { + try + { + // note: the order of operations is very important here + // sends the stored character data to the Context + endElement(buffer.toString()); + + // reports to the parent handler if this handler's Context is a + // statement (returning no value BACK to the parent's Context) + if (context.isStatement()) + { + // This may create a valid result in the parent's Context + // or let it fail + parent.notifyStatement(exceptionListener); + + // skips any further processing if the parent handler is now marked + // as failed + if (parent.hasFailed()) + return; + } + + // processes the Context and stores the result + putObject(context.getId(), context.endContext(parent.getContext())); + + // transfers the Context's results to the parent's Context + // if it is an expression (rather than a statement) + if (! context.isStatement()) + parent.getContext().addParameterObject(context.getResult()); + } + catch (AssemblyException pe) + { + // notifies that an exception was thrown in this handler's Context + Throwable t = pe.getCause(); + + if (t instanceof Exception) + exceptionListener.exceptionThrown((Exception) t); + else + throw (InternalError) new InternalError("Severe problem while decoding XML data.") + .initCause(t); + + // marks the handler as failed + notifyContextFailed(); + } + } + } + + /** Notifies the handler's Context that its child Context will not return + * a value back. Some Context variants need this information to know when + * a method or a constructor call can be made. + * + * This method is called by a child handler. + */ + public void notifyStatement(ExceptionListener exceptionListener) + { + try + { + + // propagates to parent handler first to generate objects + // needed by this Context instance + if(context.isStatement()) + { + parent.notifyStatement(exceptionListener); + } + + // Some Context instances do stuff which can fail now. If that + // happens this handler is marked as failed. + context.notifyStatement(parent.getContext()); + } + catch (AssemblyException ae) + { + // notifies that an exception was thrown in this handler's Context + Throwable t = ae.getCause(); + + if (t instanceof Exception) + exceptionListener.exceptionThrown((Exception) t); + else + throw (InternalError) new InternalError("Severe problem while decoding XML data.") + .initCause(t); + + // marks the handler as failed + notifyContextFailed(); + } + } + + /** Marks this and any depending parent handlers as failed. Which means that on their end + * no result is calculated. + * + * When a handler has failed no more handlers are accepted within it. + */ + public final void notifyContextFailed() + { + hasFailed = true; + + // marks the parent handler as failed if its Context + // is affected by the failure of this handler's Context + if (parent.getContext().subContextFailed()) + parent.notifyContextFailed(); + } + + /** Returns whether this handler has failed. + * + * This is used to skip child elements. + * + * @return Whether this handler has failed. + */ + public final boolean hasFailed() + { + return hasFailed; + } + + /** Processes the character data when the element ends. + * + * The default implementation does nothing for convenience. + * + * @param characters + * @throws AssemblerException + */ + protected void endElement(String characters) throws AssemblyException + { + // XXX: throw an exception when unexpected character data is available? + } + + /** Adds characters from the body of the XML tag to the buffer. + * + * @param ch + * @param start + * @param length + * @throws SAXException + */ + public final void characters(char[] ch, int start, int length) + { + // simply appends character data + buffer.append(ch, start, length); + } + + /** Stores an object globally under a unique id. If the id is + * null the object is not stored. + * + * @param objectId + * @param o + */ + public void putObject(String objectId, Object o) + { + if (objectId != null) + parent.putObject(objectId, o); + } + + /** Returns a previously stored object. If the id is null the + * result is null, too. + * + * @param objectId + * @return Returns a previously stored object or null. + */ + public Object getObject(String objectId) throws AssemblyException + { + return objectId == null ? null : parent.getObject(objectId); + } + + /** Returns the Class instance as if called Class.forName() but + * uses a ClassLoader given by the user. + * + * @param className + * @return + * @throws ClassNotFoundException + */ + public Class instantiateClass(String className) + throws ClassNotFoundException + { + return parent.instantiateClass(className); + } + + public final boolean isSubelementAllowed(String subElementName) + { + return allowsSubelements && ! subElementName.equals("java"); + } + + public final Context getContext() + { + return context; + } + + public final ElementHandler getParent() + { + return parent; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/AbstractObjectContext.java b/libjava/classpath/gnu/java/beans/decoder/AbstractObjectContext.java new file mode 100644 index 00000000000..202c33b3413 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/AbstractObjectContext.java @@ -0,0 +1,127 @@ +/* gnu.java.beans.decoder.AbstractObjectContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.beans.decoder; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** AbstractObjectContext is the base for all Context implementations which + * create or provide a result object during their lifetime. + * + * <p>This class provides the implementation for an indexed get and set method. + * But this does not mean that the result object supports these operation.</p> + * + * @author Robert Schuster + * + */ +abstract class AbstractObjectContext extends AbstractContext +{ + protected Object object; + + AbstractObjectContext() + {} + + /** Sets the result object of the Context. + * + * @param obj The result object to be set. + */ + protected final void setObject(Object obj) + { + object = obj; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#set(int, java.lang.Object) + */ + public final void set(int index, Object o) throws AssemblyException + { + try + { + Method method = + object.getClass().getMethod( + "set", + new Class[] { Integer.TYPE, Object.class }); + + method.invoke(object, new Object[] { new Integer(index), o }); + } + catch (NoSuchMethodException nsme) + { + throw new AssemblyException(nsme); + } + catch (InvocationTargetException ite) + { + throw new AssemblyException(ite.getCause()); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#get(int) + */ + public final Object get(int index) throws AssemblyException + { + try + { + Method method = + object.getClass().getMethod( + "get", + new Class[] { Integer.TYPE }); + + return method.invoke(object, new Object[] { new Integer(index)}); + } + catch (NoSuchMethodException nsme) + { + throw new AssemblyException(nsme); + } + catch (InvocationTargetException ite) + { + throw new AssemblyException(ite.getCause()); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + } + + public final Object getResult() + { + return object; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ArrayContext.java b/libjava/classpath/gnu/java/beans/decoder/ArrayContext.java new file mode 100644 index 00000000000..cf4267f2779 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ArrayContext.java @@ -0,0 +1,122 @@ +/* gnu.java.beans.decoder.ArrayContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.lang.reflect.Array; + +/** A Context implementation for a fixed size array. The array + * elements have to be set using IndexContext instances. + * + * @author Robert Schuster + */ +class ArrayContext extends AbstractContext +{ + private Object array; + + ArrayContext(String id, Class klass, int length) + { + setId(id); + array = Array.newInstance(klass, length); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + throw new AssemblyException(new IllegalStateException("Adding objects without an index to a fixed array is not possible.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#reportStatement() + */ + public void notifyStatement(Context outerContext) + { + // method call intentionally ignored because there is not any useful effect + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public Object endContext(Context outerContext) throws AssemblyException + { + return array; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + // returns false to indicate that assembling the array does not fail only because + // a subelement failed. + return false; + } + + public void set(int index, Object o) throws AssemblyException + { + try + { + Array.set(array, index, o); + } + catch (ArrayIndexOutOfBoundsException aioobe) + { + throw new AssemblyException(aioobe); + } + } + + public Object get(int index) throws AssemblyException + { + try + { + return Array.get(array, index); + } + catch (ArrayIndexOutOfBoundsException aioobe) + { + throw new AssemblyException(aioobe); + } + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#getResult() + */ + public Object getResult() + { + return array; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ArrayHandler.java b/libjava/classpath/gnu/java/beans/decoder/ArrayHandler.java new file mode 100644 index 00000000000..23f0285192b --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ArrayHandler.java @@ -0,0 +1,118 @@ +/* gnu.java.beans.decoder.ArrayHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; +import java.util.HashMap; + +import org.xml.sax.Attributes; + +/** ArrayHandler processes the <array> tag. Depending on the existance of the 'length' attribute a Context for + * a fixed-size or growable array is created. + * + * @author Robert Schuster + */ +class ArrayHandler extends AbstractElementHandler +{ + /** Contains a mapping between a textual description of a primitive type (like "byte") and + * its corresponding wrapper class. This allows it to easily construct Array objects for + * primitive data types. + */ + private static HashMap typeMap = new HashMap(); + + static + { + typeMap.put("byte", Byte.TYPE); + typeMap.put("short", Short.TYPE); + typeMap.put("int", Integer.TYPE); + typeMap.put("long", Long.TYPE); + + typeMap.put("float", Float.TYPE); + typeMap.put("double", Double.TYPE); + + typeMap.put("boolean", Boolean.TYPE); + + typeMap.put("char", Character.TYPE); + } + + /** + * @param PersistenceParser + */ + ArrayHandler(ElementHandler parent) + { + super(parent, true); + } + + protected Context startElement(Attributes attributes, ExceptionListener exceptionListener) + throws AssemblyException, AssemblyException + { + String id = attributes.getValue("id"); + String className = attributes.getValue("class"); + + if (className != null) + { + try + { + Class klass; + + if (typeMap.containsKey(className)) + klass = (Class) typeMap.get(className); + else + klass = instantiateClass(className); + + String length = attributes.getValue("length"); + if (length != null) + // creates Array with predefined length + return new ArrayContext(id, klass, Integer.parseInt(length)); + else + // creates Array without length restriction + return new GrowableArrayContext(id, klass); + } + catch (ClassNotFoundException cnfe) + { + throw new AssemblyException(cnfe); + } + catch (NumberFormatException nfe) + { + throw new AssemblyException(nfe); + } + } + + throw new AssemblyException(new IllegalArgumentException("Missing 'class' attribute in <array> tag.")); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/AssemblyException.java b/libjava/classpath/gnu/java/beans/decoder/AssemblyException.java new file mode 100644 index 00000000000..29dfa65ac7c --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/AssemblyException.java @@ -0,0 +1,57 @@ +/* gnu.java.beans.decoder.AssemblyException + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** The AssemblyException is used to wrap the cause of problems when assembling objects. + * In all cases only the wrapped exception is given to the PersistenceParser's + * ExceptionListener instance (never the AssemblyException itself). + * + * <p>Note: Often multiple steps are needed to construct a fully usuable object instance. + * Such a construction can be called assembly and thats why this exception was + * named AssemblyException.</p> + * + * @author Robert Schuster + */ +class AssemblyException extends Exception +{ + AssemblyException(Throwable cause) + { + super(cause); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/BooleanHandler.java b/libjava/classpath/gnu/java/beans/decoder/BooleanHandler.java new file mode 100644 index 00000000000..a34fe346e0f --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/BooleanHandler.java @@ -0,0 +1,67 @@ +/* gnu.java.beans.decoder.BooleanHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Boolean instance from the character data in a <boolean> tag. + * + * @author Robert Schuster + */ +class BooleanHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + BooleanHandler(ElementHandler parent) + { + super(parent); + + // TODO Auto-generated constructor stub + } + + protected Object parse(String number) throws AssemblyException + { + if (number.equals("true")) + return new Boolean(true); + + if (number.equals("false")) + return new Boolean(false); + + throw new AssemblyException(new IllegalArgumentException("Element contained no valid boolean value.")); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ByteHandler.java b/libjava/classpath/gnu/java/beans/decoder/ByteHandler.java new file mode 100644 index 00000000000..d6c33d14d21 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ByteHandler.java @@ -0,0 +1,59 @@ +/* gnu.java.beans.decoder.ByteHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Byte instance from the character data in a <byte> tag. + * + * @author Robert Schuster + */ +class ByteHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + ByteHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws NumberFormatException + { + return Byte.valueOf(number); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/CharHandler.java b/libjava/classpath/gnu/java/beans/decoder/CharHandler.java new file mode 100644 index 00000000000..1b31e5b0559 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/CharHandler.java @@ -0,0 +1,62 @@ +/* gnu.java.beans.decoder.CharHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Character instance from the character data in a <char> tag. + * + * @author Robert Schuster + */ +class CharHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + CharHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws AssemblyException + { + if (number.length() > 1) + throw new AssemblyException(new IllegalArgumentException("Element contained no valid character.")); + + return new Character(number.charAt(0)); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ClassHandler.java b/libjava/classpath/gnu/java/beans/decoder/ClassHandler.java new file mode 100644 index 00000000000..1dbdd63e79a --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ClassHandler.java @@ -0,0 +1,66 @@ +/* gnu.java.beans.decoder.ClassHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Class instance from the character data in a <class> tag. + * + * @author Robert Schuster + */ +class ClassHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + ClassHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String characters) throws AssemblyException + { + try + { + return instantiateClass(characters); + } + catch (ClassNotFoundException cnfe) + { + throw new AssemblyException(cnfe); + } + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ConstructorContext.java b/libjava/classpath/gnu/java/beans/decoder/ConstructorContext.java new file mode 100644 index 00000000000..7838fb7e263 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ConstructorContext.java @@ -0,0 +1,102 @@ +/* gnu.java.beans.decoder.ConstructorContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +/** A ConstructorContext is a {@link Context} implementation which collects the parameters for a constructor + * call and instantiates the result object using that constructor. After that sub-contexts can invoke + * methods on the result object. + * + * <p>The constructor is invoked when a sub-context is a statement or the Context ends.</p> + * + * @author Robert Schuster + */ +class ConstructorContext extends AbstractCreatableObjectContext +{ + private ArrayList arguments = new ArrayList(); + private Class klass; + + ConstructorContext(String id, Class newClass) + { + setId(id); + // sets superclass field + klass = newClass; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + protected void addParameterObjectImpl(Object o) + { + arguments.add(o); + } + + protected Object createObject(Context outerContext) + throws AssemblyException + { + Object[] args = arguments.toArray(); + + try + { + Constructor constructor = MethodFinder.getConstructor(klass, args); + + // instantiates object (klass field gets re-set by superclass) + return constructor.newInstance(args); + } + catch (NoSuchMethodException nsme) + { + throw new AssemblyException(nsme); + } + catch (InvocationTargetException ite) + { + throw new AssemblyException(ite.getCause()); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + catch (InstantiationException ie) + { + throw new AssemblyException(ie); + } + } + +} diff --git a/libjava/classpath/gnu/java/beans/decoder/Context.java b/libjava/classpath/gnu/java/beans/decoder/Context.java new file mode 100644 index 00000000000..a2db09732d1 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/Context.java @@ -0,0 +1,137 @@ +/* gnu.java.beans.decoder.Context + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +/** A Context is the environment for an object which is being assembler. If there + * are no errors each handler creates one Context. + * <p>Depending on the result of isStatement() a Context can be statement or an + * expression. An expression returns a value to the Context of its parent handler, + * a statement does not. Whenever a Context is a statement the parent handler's + * Context is informed about that through the {@link notifyStatement}-method.</p> + * + * @author Robert Schuster + */ +interface Context +{ + /** Adds a parameter object to the context. This method is used when + * sub-Contexts return their result. + * + * Some Contexts do not accept more than a certain amount of objects + * and throw an AssemblerException if the amount is exceeded. + * + * @param o The object added to this context. + */ + void addParameterObject(Object o) throws AssemblyException; + + /** Notifies that the next element is a statement. This can mean + * that an argument list is complete to be called. + * + */ + void notifyStatement(Context outerContext) throws AssemblyException; + + /** Notifies that the context ends and the returns the appropriate result + * object. + * + * @param outerContext + * @return + */ + Object endContext(Context outerContext) throws AssemblyException; + + /** Notifies that the assembly of a subcontext failed and returns + * whether this Context is affected in a way that it fails too. + * + * @return Whether the failure of a subcontext lets this context fail, too. + */ + boolean subContextFailed(); + + /** Calls an appropriate indexed set method if it is available or + * throws an AssemblerException if that is not allowed on this Context. + * + * The behaviour of this method is equal to List.set(int, Object). + * + * @param index Index position to be set. + * @param o Object to be set at the given index position. + * @throws AssemblerException Indexed set is not allowed or otherwise failed. + */ + void set(int index, Object o) throws AssemblyException; + + /** Calls an appropriate indexed get method if it is available or + * throws an AssemblerException if that is not allowed on this Context. + * + * The behaviour of this method is equal to List.get(int). + * + * @param index Index position of the object return. + * @throws AssemblerException Indexed get is not allowed or otherwise failed. + */ + Object get(int index) throws AssemblyException; + + /** Returns the result which was calculated by calling endContext() or reportStatement(). + * Its the handler's responsibility to care that any of these two methods was called. + * + * This is used by sub-Contexts to access this Context's result. + * + * @return + */ + Object getResult(); + + /** Gives this Context a unique id. For convenience the id may be null which means + * that no id exists at all. + * + * @param id + */ + void setId(String id); + + /** Returns this Context's unique id or null if does not have such an id. + * + * @return This Context's id or null. + */ + String getId(); + + /** Returns whether this Context is a statement (not returning result back + * to parent handler's Context) or not (= expression). + * + * @return + */ + boolean isStatement(); + + /** Sets whether this Context is a statement or not. + * + * @param b + */ + void setStatement(boolean b); +} diff --git a/libjava/classpath/gnu/java/beans/decoder/DecoderContext.java b/libjava/classpath/gnu/java/beans/decoder/DecoderContext.java new file mode 100644 index 00000000000..69d84e57656 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/DecoderContext.java @@ -0,0 +1,124 @@ +/* gnu.java.beans.decoder.DecoderContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.XMLDecoder; +import java.util.ArrayList; +import java.util.Iterator; + +/** DecoderContext is a Context implementation which allows access to + * the XMLDecoder instance itself. This is used for the <java> tag. + * + * @author Robert Schuster + */ +public class DecoderContext extends AbstractContext +{ + private XMLDecoder decoder; + + public DecoderContext(XMLDecoder xmlDecoder) + { + decoder = xmlDecoder; + } + + private ArrayList objects = new ArrayList(); + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + objects.add(o); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#reportStatement() + */ + public void notifyStatement(Context outerContext) throws AssemblyException + { + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public Object endContext(Context outerContext) throws AssemblyException + { + return decoder; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + return false; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#set(int, java.lang.Object) + */ + public void set(int index, Object o) throws AssemblyException + { + throw new AssemblyException(new IllegalArgumentException("Set method is not allowed in decoder context.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#get(int) + */ + public Object get(int index) throws AssemblyException + { + throw new AssemblyException(new IllegalArgumentException("Get method is not allowed in decoder context.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#getResult() + */ + public Object getResult() + { + return decoder; + } + + /** Returns an Iterator that retrieves the assembled objects. + * + * @return An Iterator retrieving assembled objects. + */ + public Iterator iterator() + { + return objects.iterator(); + } + +} diff --git a/libjava/classpath/gnu/java/beans/decoder/DefaultExceptionListener.java b/libjava/classpath/gnu/java/beans/decoder/DefaultExceptionListener.java new file mode 100644 index 00000000000..71e7158e7d2 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/DefaultExceptionListener.java @@ -0,0 +1,57 @@ +/* gnu.java.beans.DefaultExceptionListener + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +/** The DefaultExceptionListener is the default implementation of the ExceptionListener + * interface. An instance of this class is used whenever the user provided no + * ExceptionListener instance on its own. + * + * <p>The implementation just writes the exception's message to <code>System.err</code>.</p> + * + * @author Robert Schuster + */ +public class DefaultExceptionListener implements ExceptionListener +{ + public void exceptionThrown(Exception e) + { + System.err.println("non-critical exception: " + e + " - message: " + + e.getMessage()); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/DoubleHandler.java b/libjava/classpath/gnu/java/beans/decoder/DoubleHandler.java new file mode 100644 index 00000000000..8f6be7ec092 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/DoubleHandler.java @@ -0,0 +1,59 @@ +/* gnu.java.beans.decoder.DoubleHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Double instance from the character data in a <double> tag. + * + * @author Robert Schuster + */ +class DoubleHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + DoubleHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws NumberFormatException + { + return Double.valueOf(number); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/DummyContext.java b/libjava/classpath/gnu/java/beans/decoder/DummyContext.java new file mode 100644 index 00000000000..f3b24549de2 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/DummyContext.java @@ -0,0 +1,116 @@ +/* gnu.java.beans.decoder.DummyContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +/** The DummyContext is used as the Context implementation for the DummyHandler. It + * just prevents having a null-reference. + * + * <p>When the implementation is correct none of this class' methods + * (except <code>notifyStatement()</code>) is called.</p> + * + * @author Robert Schuster + */ +public class DummyContext extends AbstractContext +{ + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#reportStatement() + */ + public void notifyStatement(Context outerContext) throws AssemblyException + { + // intentionally ignored + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public Object endContext(Context outerContext) throws AssemblyException + { + fail(); + return null; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + fail(); + return false; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#set(int, java.lang.Object) + */ + public void set(int index, Object o) throws AssemblyException + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#get(int) + */ + public Object get(int index) throws AssemblyException + { + fail(); + return null; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#getResult() + */ + public Object getResult() + { + fail(); + return null; + } + + private void fail() + { + throw new InternalError("Invoking the DummyContext is not expected" + + " - Please file a bug report at" + + " http://www/gnu.org/software/classpath/."); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/DummyHandler.java b/libjava/classpath/gnu/java/beans/decoder/DummyHandler.java new file mode 100644 index 00000000000..880d76adc0e --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/DummyHandler.java @@ -0,0 +1,156 @@ +/* gnu.java.beans.decoder.DummyHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +import org.xml.sax.Attributes; + +/** An ElementHandler implementation that is used as an artificial root + * element. This avoids having to check for a null element. + * + * @author Robert Schuster + */ +class DummyHandler implements ElementHandler +{ + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#start(org.xml.sax.Attributes, java.beans.ExceptionListener) + */ + public void start( + Attributes attributes, + ExceptionListener exceptionListener) + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#end(java.beans.ExceptionListener) + */ + public void end(ExceptionListener exceptionListener) + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#characters(char[], int, int) + */ + public void characters(char[] ch, int start, int length) + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#isSubelementAllowed(java.lang.String) + */ + public boolean isSubelementAllowed(String subElementName) + { + return true; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#instantiateClass(java.lang.String) + */ + public Class instantiateClass(String className) + throws ClassNotFoundException + { + fail(); + return null; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#reportStatement(java.beans.ExceptionListener) + */ + public void notifyStatement(ExceptionListener exceptionListener) + { + // ignore + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#hasFailed() + */ + public boolean hasFailed() + { + return false; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#getContext() + */ + public Context getContext() + { + return new DummyContext(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#contextFailed() + */ + public void notifyContextFailed() + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#putObject(java.lang.String, java.lang.Object) + */ + public void putObject(String objectId, Object o) + { + fail(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.ElementHandler#getObject(java.lang.String) + */ + public Object getObject(String objectId) + { + fail(); + return null; + } + + public ElementHandler getParent() + { + fail(); + return null; + } + + private void fail() + { + throw new InternalError("Invoking the DummyHandler is not expected" + + " - Please file a bug report at " + + " http://www.gnu.org/software/classpath/."); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ElementHandler.java b/libjava/classpath/gnu/java/beans/decoder/ElementHandler.java new file mode 100644 index 00000000000..e6ae60af886 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ElementHandler.java @@ -0,0 +1,130 @@ +/* gnu.java.beans.decoder.ElementHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +import org.xml.sax.Attributes; + +/** ElementHandler manages a Context instance and interacts with + * its parent and child handlers. + * + * @author Robert Schuster + */ +interface ElementHandler +{ + /** Evaluates the attributes and creates a Context instance. + * If the creation of the Context instance fails the ElementHandler + * is marked as failed which may affect the parent handler other. + * + * @param attributes Attributes of the XML tag. + */ + void start(Attributes attributes, ExceptionListener exceptionListener); + + /** Post-processes the Context. + */ + void end(ExceptionListener exceptionListener); + + /** Adds characters from the body of the XML tag to the buffer. + * + * @param ch + * @param start + * @param length + * @throws SAXException + */ + void characters(char[] ch, int start, int length); + + /** Returns whether a subelement of the given name is allowed. The rules + * for evaluating this are derived from the javabeans.dtd which can be found + * here: <a href="http://java.sun.com/products/jfc/tsc/articles/persistence3">Java Persistence Article</a>. + * + * @param subElementName + * @return + */ + boolean isSubelementAllowed(String subElementName); + + /** Provides the same functionality as Class.forName() but allows the decoder + * to use a different class loader. + * + * @param className + * @return + * @throws ClassNotFoundException + */ + Class instantiateClass(String className) throws ClassNotFoundException; + + /** Notifies the handler's Context that its child Context will not return + * a value back. Some Context variants need this information to know when + * a method or a constructor call can be made. + * + * This method is called by a child handler. + */ + void notifyStatement(ExceptionListener exceptionListener); + + /** Returns whether this handler has failed. + * + * This is used to skip child elements. + * + * @return Whether this handler has failed. + */ + boolean hasFailed(); + + /** Returns the Context instance this handler is working on. + * + * @return The handler's Context instance. + */ + Context getContext(); + + /** Notifies the handler that its Context failed and starts a recursive + * invocation of the parent handler if it is affected by that failure. + * + * Although the method is a public API member it is only used internally. + */ + void notifyContextFailed(); + + /** Stores the object under the given id. The object is not stored if the + * id is null. + * + * @param objectId + * @param o + */ + void putObject(String objectId, Object o); + + Object getObject(String objectId) throws AssemblyException; + + ElementHandler getParent(); +} diff --git a/libjava/classpath/gnu/java/beans/decoder/FloatHandler.java b/libjava/classpath/gnu/java/beans/decoder/FloatHandler.java new file mode 100644 index 00000000000..5f0e15cefed --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/FloatHandler.java @@ -0,0 +1,59 @@ +/* gnu.java.beans.decoder.FloatHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Float instance from the character data in a <float> tag. + * + * @author Robert Schuster + */ +class FloatHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + FloatHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws NumberFormatException + { + return Float.valueOf(number); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/GrowableArrayContext.java b/libjava/classpath/gnu/java/beans/decoder/GrowableArrayContext.java new file mode 100644 index 00000000000..f24a60b4a4b --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/GrowableArrayContext.java @@ -0,0 +1,138 @@ +/* gnu.java.beans.decoder.GrowableArrayContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.beans.decoder; + +import java.lang.reflect.Array; + +/** A Context implementation for a growable array. The array + * elements have to be set using expressions. + * + * @author Robert Schuster + */ +class GrowableArrayContext extends AbstractContext +{ + private static final int INITIAL_SIZE = 16; + + private Class klass; + private Object array; + private int length; + + GrowableArrayContext(String id, Class newClass) + { + setId(id); + klass = newClass; + array = Array.newInstance(klass, INITIAL_SIZE); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + if (length == Array.getLength(array)) + { + Object tmp = Array.newInstance(klass, length * 2); + System.arraycopy(array, 0, tmp, 0, length); + array = tmp; + } + + try { + Array.set(array, length++, o); + } catch(IllegalArgumentException iae) { + throw new AssemblyException(iae); + } + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#reportStatement() + */ + public void notifyStatement(Context outerContext) throws AssemblyException + { + throw new AssemblyException( + new IllegalArgumentException("Statements inside a growable array are not allowed.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public Object endContext(Context outerContext) throws AssemblyException + { + if (length != Array.getLength(array)) + { + Object tmp = Array.newInstance(klass, length); + System.arraycopy(array, 0, tmp, 0, length); + array = tmp; + } + + return array; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + // returns false to indicate that assembling the array does not fail only because + // a subelement failed + return false; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#set(int, java.lang.Object) + */ + public void set(int index, Object o) throws AssemblyException + { + try { + Array.set(array, index, o); + } catch(IllegalArgumentException iae) { + throw new AssemblyException(iae); + } + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#get(int) + */ + public Object get(int index) throws AssemblyException + { + return Array.get(array, index); + } + + public Object getResult() + { + return array; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/IndexContext.java b/libjava/classpath/gnu/java/beans/decoder/IndexContext.java new file mode 100644 index 00000000000..11f840caeec --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/IndexContext.java @@ -0,0 +1,130 @@ +/* gnu.java.beans.decoder.IndexContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +/** IndexContext is Context implementation that senses whether it is an indexed get or set + * operation and invokes this operation. + * + * <p>An IndexContent is a get operation when no argument is provided and a set operation if one + * argument is provided.</p> + * + * @author Robert Schuster + */ +class IndexContext extends AbstractContext +{ + private Object result; + private Object argument; + private int index; + private boolean isSetter; + + IndexContext(String id, int newIndex) + { + setId(id); + index = newIndex; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + if (! isSetter) + { + argument = o; + isSetter = true; + } + else + throw new AssemblyException(new IllegalStateException("More than one argument for indiced access is not possible.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#reportStatement() + */ + public void notifyStatement(Context outerContext) throws AssemblyException + { + throw new AssemblyException(new IllegalStateException("Statements inside indiced access are not allowed.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public Object endContext(Context outerContext) throws AssemblyException + { + if (isSetter) + { + // setter + outerContext.set(index, argument); + + return null; + } + else + // getter + return result = outerContext.get(index); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + // returns true to indicate that indiced access assembly fails when one of its + // argument could not be assembled + return true; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#set(int, java.lang.Object) + */ + public void set(int index, Object o) throws AssemblyException + { + throw new AssemblyException(new IllegalStateException("Setter is not allowed inside indiced access.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#get(int) + */ + public Object get(int index) throws AssemblyException + { + throw new AssemblyException(new IllegalStateException("getter is not allowed insided indiced access.")); + } + + public Object getResult() + { + return result; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/IntHandler.java b/libjava/classpath/gnu/java/beans/decoder/IntHandler.java new file mode 100644 index 00000000000..a96f4a0315b --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/IntHandler.java @@ -0,0 +1,59 @@ +/* gnu.java.beans.decoder.IntHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Integer instance from the character data in a <int> tag. + * + * @author Robert Schuster + */ +class IntHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + IntHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws NumberFormatException + { + return Integer.valueOf(number); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/JavaHandler.java b/libjava/classpath/gnu/java/beans/decoder/JavaHandler.java new file mode 100644 index 00000000000..3bc8c60288c --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/JavaHandler.java @@ -0,0 +1,93 @@ +/* gnu.java.beans.decoder.JavaHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; +import java.util.HashMap; + +import org.xml.sax.Attributes; + +/** Wraps a DecoderContext instance. + * + * @author Robert Schuster + */ +public class JavaHandler extends AbstractElementHandler +{ + private Context context; + private HashMap objectMap = new HashMap(); + private ClassLoader classLoader; + + /** + * @param PersistenceParser + */ + JavaHandler(DummyHandler parent, Context decoderContext, + ClassLoader cl) + { + super(parent, true); + + classLoader = cl; + + context = decoderContext; + + } + + protected Context startElement(Attributes attributes, ExceptionListener exceptionListener) + throws AssemblyException + { + // may expect version and class attribute but it not used in JDK + // so we do either + return context; + } + + public Object getObject(String objectId) + { + return objectMap.get(objectId); + } + + public void putObject(String objectId, Object o) + { + if (objectId != null) + objectMap.put(objectId, o); + } + + public Class instantiateClass(String className) + throws ClassNotFoundException + { + return Class.forName(className, false, classLoader); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/LongHandler.java b/libjava/classpath/gnu/java/beans/decoder/LongHandler.java new file mode 100644 index 00000000000..d7bfa54e54e --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/LongHandler.java @@ -0,0 +1,59 @@ +/* gnu.java.beans.decoder.LongHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Creates a Long instance from the character data in a <long> tag. + * + * @author Robert Schuster + */ +class LongHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + LongHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws NumberFormatException + { + return Long.valueOf(number); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/MethodContext.java b/libjava/classpath/gnu/java/beans/decoder/MethodContext.java new file mode 100644 index 00000000000..bad0a213f7f --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/MethodContext.java @@ -0,0 +1,107 @@ +/* gnu.java.beans.decoder.MethodContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.beans.decoder; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; + +/** MethodContext collects arguments for a method call and creates the result object + * using it. The method is called using the result object of the parent Context. + * + * <p>When the result object is available methods can be called on it using sub-Contexts.</p> + * + * @author Robert Schuster + */ +class MethodContext extends AbstractCreatableObjectContext +{ + private ArrayList arguments = new ArrayList(); + private String methodName; + + MethodContext(String id, String newMethodName) + { + setId(id); + setStatement(true); + methodName = newMethodName; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObjectImpl(Object o) + { + arguments.add(o); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + protected Object createObject(Context outerContext) + throws AssemblyException + { + Object outerObject = outerContext.getResult(); + + if (outerObject == null) + throw new AssemblyException( + new NullPointerException( + "No object to invoke method " + methodName)); + + Object[] args = arguments.toArray(); + + try + { + Method method = + MethodFinder.getMethod( + outerObject.getClass(), + methodName, + args); + return method.invoke(outerObject, args); + } + catch (NoSuchMethodException nsme) + { + throw new AssemblyException(nsme); + } + catch (InvocationTargetException ite) + { + throw new AssemblyException(ite.getCause()); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/MethodFinder.java b/libjava/classpath/gnu/java/beans/decoder/MethodFinder.java new file mode 100644 index 00000000000..3968b173adb --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/MethodFinder.java @@ -0,0 +1,177 @@ +/* gnu.java.beans.decoder.MethodFinder + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; + +class MethodFinder +{ + /** Provides a mapping between a wrapper class and its corresponding primitive's type. */ + private static HashMap typeMapping = new HashMap(); + + static { + typeMapping.put(Byte.class, Byte.TYPE); + typeMapping.put(Short.class, Short.TYPE); + typeMapping.put(Integer.class, Integer.TYPE); + typeMapping.put(Long.class, Long.TYPE); + typeMapping.put(Float.class, Float.TYPE); + typeMapping.put(Double.class, Double.TYPE); + + typeMapping.put(Character.class, Character.TYPE); + typeMapping.put(Boolean.class, Boolean.TYPE); + } + + private MethodFinder() + { + } + + /** Searches a Method which can accept the given arguments. + * + * @param klass + * @param name + * @param arguments + * @return + * @throws NoSuchMethodException + */ + static Method getMethod(Class klass, String name, Object[] arguments) + throws NoSuchMethodException + { + // prepares array containing the types of the arguments + Class[] argumentTypes = getArgumentTypes(arguments); + + Method[] methods = klass.getMethods(); + + // iterates over all public methods + for (int i = 0; i < methods.length; i++) + { + if (methods[i].getName().equals(name)) + { + if (matchingArgumentTypes(methods[i].getParameterTypes(), + argumentTypes)) + return methods[i]; + } + } + + throw new NoSuchMethodException( + "Could not find a matching method named " + + name + + "() in class " + + klass); + } + + static Constructor getConstructor(Class klass, Object[] arguments) + throws NoSuchMethodException + { + Class[] argumentTypes = getArgumentTypes(arguments); + Constructor[] constructors = klass.getConstructors(); + + // iterates over all public methods + for (int i = 0; i < constructors.length; i++) + { + if (matchingArgumentTypes(constructors[i].getParameterTypes(), + argumentTypes)) + return constructors[i]; + } + + throw new NoSuchMethodException( + "Could not find a matching constructor in class " + klass); + } + + /** Transforms an array of argument objects into an array of argument types. + * For each argument being null the argument is null, too. An argument type + * being null means: Accepts everything (although this can be ambigous). + * + * @param arguments + * @return + */ + private static Class[] getArgumentTypes(Object[] arguments) + { + if (arguments == null) + return new Class[0]; + + // prepares array containing the types of the arguments + Class[] argumentTypes = new Class[arguments.length]; + for (int i = 0; i < arguments.length; i++) + argumentTypes[i] = + (arguments[i] == null) ? null : arguments[i].getClass(); + return argumentTypes; + } + + /** Tests whether the argument types supplied to the method argument types + * are assignable. In addition to the assignment specifications this method + * handles the primitive's wrapper classes as if they were of their + * primitive type (e.g Boolean.class equals Boolean.TYPE). + * When a supplied argument type is null it is assumed that no argument + * object was supplied for it and the test for this particular parameter will + * pass. + * + * @param methodArgTypes + * @param suppliedArgTypes + * @return + */ + private static boolean matchingArgumentTypes( + Class[] methodArgTypes, + Class[] suppliedArgTypes) + { + if (methodArgTypes.length != suppliedArgTypes.length) + return false; + + for (int i = 0; i < methodArgTypes.length; i++) + { + if (suppliedArgTypes[i] == null) + { + // by definition a non-existant argument type (null) can be converted to everything + continue; + } + else if (typeMapping.containsKey(suppliedArgTypes[i])) + { + Class primitiveType = + (Class) typeMapping.get(suppliedArgTypes[i]); + if (!(methodArgTypes[i].isAssignableFrom(suppliedArgTypes[i]) + || methodArgTypes[i].isAssignableFrom(primitiveType))) + return false; + } + else if (!methodArgTypes[i].isAssignableFrom(suppliedArgTypes[i])) + return false; + } + + return true; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/NullHandler.java b/libjava/classpath/gnu/java/beans/decoder/NullHandler.java new file mode 100644 index 00000000000..549617db3cb --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/NullHandler.java @@ -0,0 +1,62 @@ +/* gnu.java.beans.decoder.NullHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + + +/** Just provides the 'null' object. + * + * @author Robert Schuster + */ +class NullHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + NullHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String characters) throws AssemblyException + { + if (! characters.equals("")) + throw new AssemblyException(new IllegalArgumentException("No characters inside <void> tag allowed.")); + + return null; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ObjectContext.java b/libjava/classpath/gnu/java/beans/decoder/ObjectContext.java new file mode 100644 index 00000000000..cf88a2c2c5c --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ObjectContext.java @@ -0,0 +1,100 @@ +/* gnu.java.beans.decoder.ObjectHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +/** ObjectContext is a {@link Context} implementation that wraps a simple Object instance. + * The instance can be provided when the Context is created (due to an 'idref' + * attribute) or later (eg. <int> tag) + * + * <p>The ObjectContext does not accept any parameter object and ignores notifications + * about sub-contexts being statements.</p> + * + * @author Robert Schuster + */ +final class ObjectContext extends AbstractObjectContext +{ + ObjectContext(Object newObject) + { + setObject(newObject); + } + + ObjectContext(String id, Object newObject) + { + setId(id); + setObject(newObject); + } + + ObjectContext() + { + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + throw new AssemblyException(new IllegalArgumentException("Adding objects to an ObjectContext is not allowed.")); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#reportStatement() + */ + public void notifyStatement(Context outerContext) throws AssemblyException + { + // can ignore that + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public Object endContext(Context outerContext) throws AssemblyException + { + // just returns the object which is encapsuled (may be null) + return getResult(); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#subContextFailed() + */ + public boolean subContextFailed() + { + // this context will not fail when a subcontext fails because the result is + // already available when the context is created. + return false; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ObjectHandler.java b/libjava/classpath/gnu/java/beans/decoder/ObjectHandler.java new file mode 100644 index 00000000000..dc5b3290bb1 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ObjectHandler.java @@ -0,0 +1,169 @@ +/* gnu.java.beans.decoder.ObjectHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +import org.xml.sax.Attributes; + +/** An ObjectHandler parses the <object> tag and thereby creates various + * Context implementations. + * + * @author Robert Schuster + * + */ +public class ObjectHandler extends AbstractElementHandler +{ + /** + * XXX: Can all results be stored with an object id? + * + * + * @param PersistenceParser + */ + ObjectHandler(ElementHandler parent) + { + super(parent, true); + } + + protected Context startElement(Attributes attributes, ExceptionListener exceptionListener) + throws AssemblyException + { + String className = attributes.getValue("class"); + String methodName = attributes.getValue("method"); + String fieldName = attributes.getValue("field"); + String index = attributes.getValue("index"); + String propertyName = attributes.getValue("property"); + String id = attributes.getValue("id"); + String idRef = attributes.getValue("idref"); + + /* first check if we just want to access an existing object (idref present) + * + * note: <object idref="foo" method="bar"/> is not valid to call method "bar" + * on the object with id "foo". Instead this should return the object "foo" + * itself. The right way to this is: + * <object idref="foo"> + * <object method="bar"/> + * </object> + * + * This means that if idref is present class, method, field, index and + * property are obsolete. + */ + if (idRef != null) + // reactivates an existing object and giving it another name if id exists + return new ObjectContext(id, getObject(idRef)); + + // decides whether we are in a static (className present) or dynamic context + if (className != null) + { + try + { + Class klass = instantiateClass(className); + + // class name exists which means that we are in a static context. + // so we may want to ... + // access a static field if the fieldName exists + if (fieldName != null) + { + try + { + return new ObjectContext(id, + klass.getField(fieldName).get(null)); + } + catch (NoSuchFieldException nsfe) + { + throw new AssemblyException(nsfe); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + } + + // (falling through is important!) + // run a constructor if methodName is "new" or null + if (methodName == null || methodName.equals("new")) + return new ConstructorContext(id, klass); + + // (falling through is important!) + // run a static method on the given class (if methodName exists, which is implied already) + return new StaticMethodContext(id, klass, methodName); + // XXX: should fail if unexpected attributes are present? + } + catch (ClassNotFoundException cnfe) + { + throw new AssemblyException(cnfe); + } + } + else + { + // className does not exist which means we are in the context of + // some object and want to ... + // access the get(int index) method if index != null + if (index != null) + { + try + { + // Note: http://java.sun.com/products/jfc/tsc/articles/persistence3/ says + // that <void index="4"/> will make up a get()-call. But this is wrong because + // <void/> tags never return values (to the surrounding context) + return new IndexContext(id, Integer.parseInt(index)); + } + catch (NumberFormatException nfe) + { + throw new AssemblyException(nfe); + } + } + + // access a method if methodName exists + if (methodName != null) + return new MethodContext(id, methodName); + + // (falling through is important!) + // access a property if a propertyName exists + if (propertyName != null && propertyName.length() > 0) + // this is reported as an ordinary method access where the propertyName is + // converted into a 'getter'-method name: convert first character of property name + // to upper case and prepend 'get' + // Note: This will be a getter-method because the <object> tag implies that a return + // value is expected. + return new PropertyContext(id, propertyName); + } + + throw new AssemblyException(new IllegalArgumentException("Wrong or missing attributes for <object> tag.")); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/PersistenceParser.java b/libjava/classpath/gnu/java/beans/decoder/PersistenceParser.java new file mode 100644 index 00000000000..4eb37abef97 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/PersistenceParser.java @@ -0,0 +1,485 @@ +/* gnu.java.beans.PersistenceParser + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; +import java.beans.XMLDecoder; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +/** The PersistenceParser parses an XML data stream and delegates actions to ElementHandler + * instances. The parser catches and recovers from all exception which reside from wrong usage + * of attributes and tags. + * + * @author Robert Schuster + */ +public class PersistenceParser extends DefaultHandler implements Context +{ + /** The ExceptionListener instance which is informed of non-critical parsing exceptions. + */ + private ExceptionListener exceptionListener; + + /** When an element was not usable all elements inside it should be skipped. + * This is done by skipping startElement() and endElement() invocations whenever + * this value is above 0. + */ + private int skipElement; + + /** Stores the Creator instances which can instantiate the appropriate handler implementation + * for a given element. + */ + private HashMap handlerCreators = new HashMap(); + + /** Denotes the current ElementHandler. To avoid checking for null-values it is pre-assigned + * with a DummyHandler instance which must not be used but acts as a root element. + */ + private ElementHandler currentHandler; + + /** The real root element that stores all objects created during parsing. + * Package-private to avoid an accessor method. + */ + JavaHandler javaHandler; + + /** Stores the decoded objects. */ + private List objects = new LinkedList(); + + /** The XMLDecoder instance that started this PersistenceParser */ + private XMLDecoder decoder; + + /** Creates a PersistenceParser which reads XML data from the given InputStream, reports + * exceptions to ExceptionListener instance, stores resulting object in the DecoderContext + * and uses the given ClassLoader to resolve classes. + * + * @param inputStream + * @param exceptionListener + * @param decoderContext + * @param cl + */ + public PersistenceParser( + InputStream inputStream, + ExceptionListener exceptionListener, + ClassLoader cl, + XMLDecoder decoder) + { + + this.exceptionListener = exceptionListener; + this.decoder = decoder; + + DummyHandler dummyHandler = new DummyHandler(); + currentHandler = dummyHandler; + javaHandler = new JavaHandler(dummyHandler, this, cl); + + SAXParserFactory factory = SAXParserFactory.newInstance(); + + SAXParser parser; + try + { + parser = factory.newSAXParser(); + } + catch (ParserConfigurationException pce) + { + // should not happen when a parser is available because we did + // not request any requirements on the XML parser + throw (InternalError) new InternalError( + "No SAX Parser available.").initCause( + pce); + } + catch (SAXException saxe) + { + // should not happen when a parser is available because we did + // not request any requirements on the XML parser + throw (InternalError) new InternalError( + "No SAX Parser available.").initCause( + saxe); + } + + // prepares a map of Creator instances which can instantiate a handler which is + // appropriate for the tag that is used as a key for the Creator + handlerCreators.put("java", new JavaHandlerCreator()); + + // calls methods (properties), constructors, access fields + handlerCreators.put("object", new ObjectHandlerCreator()); + handlerCreators.put("void", new VoidHandlerCreator()); + + handlerCreators.put("array", new ArrayHandlerCreator()); + + // these handler directly create an Object (or null) + handlerCreators.put("class", new ClassHandlerCreator()); + handlerCreators.put("null", new NullHandlerCreator()); + + handlerCreators.put("char", new CharHandlerCreator()); + handlerCreators.put("string", new StringHandlerCreator()); + handlerCreators.put("boolean", new BooleanHandlerCreator()); + handlerCreators.put("byte", new ByteHandlerCreator()); + handlerCreators.put("short", new ShortHandlerCreator()); + handlerCreators.put("int", new IntHandlerCreator()); + handlerCreators.put("long", new LongHandlerCreator()); + handlerCreators.put("float", new FloatHandlerCreator()); + handlerCreators.put("double", new DoubleHandlerCreator()); + + // parses the data and sends all exceptions to the ExceptionListener + try + { + parser.parse(inputStream, this); + } + catch (SAXException saxe) + { + exceptionListener.exceptionThrown( + new IllegalArgumentException("XML data not well-formed.")); + } + catch (IOException ioe) + { + exceptionListener.exceptionThrown(ioe); + } + } + + public void startElement( + String uri, + String localName, + String qName, + Attributes attributes) + throws SAXException + { + /* The element is skipped if + * a) the current handler has already failed or a previous error occured + * which makes all children obsolete + */ + if (currentHandler.hasFailed() || skipElement > 0) + { + exceptionListener.exceptionThrown( + new IllegalArgumentException( + "Element unusable due to previous error: " + qName)); + + skipElement++; + + return; + } + + /* b) Subelements are not allowed within the current ElementHandler. + */ + if (!currentHandler.isSubelementAllowed(qName)) + { + exceptionListener.exceptionThrown( + new IllegalArgumentException( + "Element is not allowed here: " + qName)); + + skipElement++; + + return; + } + + /* c) The tag name is not a key in the map of Creator instances. This means that + * either the XML data is of a newer version or simply contains a miss-spelled element. + */ + if (!handlerCreators.containsKey(qName)) + { + exceptionListener.exceptionThrown( + new IllegalArgumentException( + "Element unusable because tag is unknown: " + qName)); + + skipElement++; + + return; + } + + // creates a new handler for the new element + AbstractElementHandler handler = + ((Creator) handlerCreators.get(qName)).createHandler( + currentHandler); + + // makes it the current handler to receive character data + currentHandler = handler; + + // starts the handler + currentHandler.start(attributes, exceptionListener); + } + + public void endElement(String uri, String localName, String qName) + throws SAXException + { + // skips processing the current handler if we are parsing an element + // which was marked invalid (in startElement() ) + if (skipElement > 0) + { + skipElement--; + return; + } + + // invokes the handler's finishing method + currentHandler.end(exceptionListener); + + // removes the current handler and reactivates its parent + currentHandler = currentHandler.getParent(); + } + + /** Transfers character data to the current handler + */ + public void characters(char[] ch, int start, int length) + throws SAXException + { + // prevents sending character data of invalid elements + if (skipElement > 0) + return; + + currentHandler.characters(ch, start, length); + } + + /** Creator interface provided a mechanism to instantiate ElementHandler instances + * for the appropriate tag. + * + * @author Robert Schuster + */ + interface Creator + { + /** Creates an ElementHandler instance using the given ElementHandler as its parent. + * + * @param parent The parent ElementHandler of the result. + * @return A new ElementHandler instance. + */ + AbstractElementHandler createHandler(ElementHandler parent); + } + + class BooleanHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new BooleanHandler(parent); + } + } + + class ByteHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new ByteHandler(parent); + } + } + + class ShortHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new ShortHandler(parent); + } + } + + class IntHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new IntHandler(parent); + } + } + + class LongHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new LongHandler(parent); + } + } + + class FloatHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new FloatHandler(parent); + } + } + + class DoubleHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new DoubleHandler(parent); + } + } + + class CharHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new CharHandler(parent); + } + } + + class StringHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new StringHandler(parent); + } + } + + class JavaHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return javaHandler; + } + } + + class ObjectHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new ObjectHandler(parent); + } + } + + class VoidHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new VoidHandler(parent); + } + } + + class ClassHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new ClassHandler(parent); + } + } + + class NullHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new NullHandler(parent); + } + } + + class ArrayHandlerCreator implements Creator + { + public AbstractElementHandler createHandler(ElementHandler parent) + { + return new ArrayHandler(parent); + } + } + + /** Adds a decoded object to the Context. */ + public void addParameterObject(Object o) throws AssemblyException + { + objects.add(o); + } + + public void notifyStatement(Context outerContext) throws AssemblyException + { + // can be ignored because theis Context does not react to statement and expressions + // differently + } + + public Object endContext(Context outerContext) throws AssemblyException + { + return null; + } + + public boolean subContextFailed() + { + // failing of subcontexts is no problem for the mother of all contexts + return false; + } + + public void set(int index, Object o) throws AssemblyException + { + // not supported + throw new AssemblyException( + new IllegalArgumentException("Set method is not allowed in decoder context.")); + } + + public Object get(int index) throws AssemblyException + { + // not supported + throw new AssemblyException( + new IllegalArgumentException("Get method is not allowed in decoder context.")); + } + + public Object getResult() + { + // returns the XMLDecoder instance which is requested by child contexts this way. + // That is needed to invoke methods on the decoder. + return decoder; + } + + public void setId(String id) + { + exceptionListener.exceptionThrown(new IllegalArgumentException("id attribute is not allowed for <java> tag.")); + } + + public String getId() + { + // appears to have no id + return null; + } + + public boolean isStatement() + { + // this context is a statement by definition because it never returns anything to a parent because + // there is no such parent (DummyContext does not count!) + return true; + } + + public void setStatement(boolean b) + { + // ignores that because this Context is always a statement + } + + /** Returns an Iterator instance which returns the decoded objects. + * + * This method is used by the XMLDecoder directly. + */ + public Iterator iterator() + { + return objects.iterator(); + } + +} diff --git a/libjava/classpath/gnu/java/beans/decoder/PropertyContext.java b/libjava/classpath/gnu/java/beans/decoder/PropertyContext.java new file mode 100644 index 00000000000..838ef814b01 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/PropertyContext.java @@ -0,0 +1,137 @@ +/* gnu.java.beans.decoder.PropertyContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** PropertyContext is a Context implementation that is very similar to MethodContext + * and IndexContext. The sole purpose of PropertyContext to find out whether it should + * 'set' or 'get' a certain property. This decision is made using the number of + * arguments. + * <p>When the method call has to be made and there is no argument we 'get' the property. + * With one argument it is 'set'.</p> + * + * @author Robert Schuster + */ +class PropertyContext extends AbstractObjectContext +{ + private Object argument; + private String propertyName; + private String prefix = "get"; + private boolean methodCalled; + + PropertyContext(String id, String newPropertyName) + { + setId(id); + propertyName = newPropertyName; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObject(Object o) throws AssemblyException + { + if (methodCalled) + throw new AssemblyException(new IllegalArgumentException("Cannot add parameter object when method was already called.")); + + if (argument != null) + throw new AssemblyException(new IllegalArgumentException("Property attribut allows zero or one argument only.")); + + argument = o; + setStatement(true); + prefix = "set"; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + public void notifyStatement(Context outerContext) throws AssemblyException + { + if (methodCalled) + return; + methodCalled = true; + + Object outerObject = outerContext.getResult(); + + if (outerObject == null) + throw new AssemblyException(new NullPointerException("No object to access property " + + propertyName)); + + + // converts property name into a method name + String methodName = prefix + propertyName.substring(0, 1).toUpperCase() + + propertyName.substring(1); + + // prepares the argument + Object[] args = (argument != null) ? new Object[] { argument } : null; + + try + { + Method method = MethodFinder.getMethod(outerObject.getClass(), + methodName, args); + + // stores the result whether it is available or not + setObject(method.invoke(outerObject, args)); + } + catch (NoSuchMethodException nsme) + { + throw new AssemblyException(nsme); + } + catch (InvocationTargetException ite) + { + throw new AssemblyException(ite.getCause()); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + } + + public Object endContext(Context outerContext) throws AssemblyException + { + notifyStatement(outerContext); + + return getResult(); + } + + public boolean subContextFailed() + { + return ! methodCalled; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/ShortHandler.java b/libjava/classpath/gnu/java/beans/decoder/ShortHandler.java new file mode 100644 index 00000000000..ab6ddbece26 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/ShortHandler.java @@ -0,0 +1,58 @@ +/* gnu.java.beans.decoder.ShortHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +/** Creates a Short instance from the character data in a <short> tag. + * + * @author Robert Schuster + */ +class ShortHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + ShortHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String number) throws NumberFormatException + { + return Short.valueOf(number); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/SimpleHandler.java b/libjava/classpath/gnu/java/beans/decoder/SimpleHandler.java new file mode 100644 index 00000000000..bc187e8a052 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/SimpleHandler.java @@ -0,0 +1,111 @@ +/* gnu.java.beans.decoder.SimpleHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +import org.xml.sax.Attributes; + +/** XML element handler that is specialized on tags that contains a simple string in their + * body which has to be parsed in a specific way. + * <p>All of these tags have in common that they do not accept attributes. A warning is + * send to the parser's ExceptionListener when one or more attributes exist.</p> + * + * @author Robert Schuster + */ +abstract class SimpleHandler extends AbstractElementHandler +{ + private ObjectContext context; + + /** + * @param PersistenceParser + */ + SimpleHandler(ElementHandler parent) + { + super(parent, false); + + // SimpleHandler do not accept any subelements + } + + protected final Context startElement(Attributes attributes, ExceptionListener exceptionListener) + throws AssemblyException + { + + // note: simple elements should not have any attributes. We inform + // the user of this syntactical but uncritical problem by sending + // an IllegalArgumentException for each unneccessary attribute + int size = attributes.getLength(); + for (int i = 0; i < size; i++) { + String attributeName = attributes.getQName(i); + Exception e = + new IllegalArgumentException( + "Unneccessary attribute '" + + attributeName + + "' discarded."); + exceptionListener.exceptionThrown(e); + } + + return context = new ObjectContext(); + } + + public void endElement(String characters) + throws AssemblyException, AssemblyException + { + // reports the number when the character data can be parsed + try + { + context.setObject(parse(characters)); + } + catch (NumberFormatException nfe) + { + throw new AssemblyException(nfe); + } + } + + /** Returns an object that is created from the given characters. If the string is + * converted into a number a NumberFormatException is cathed and reported + * appropriately. + * + * @param characters A string of characters that has to be processed in some way. + * @return An Object instance generated from the given data. + * @throws AssemblerException When the string was invalid. + * @throws NumberFormatException When the string could not be parsed into a number. + */ + protected abstract Object parse(String characters) + throws AssemblyException, NumberFormatException; +} diff --git a/libjava/classpath/gnu/java/beans/decoder/StaticMethodContext.java b/libjava/classpath/gnu/java/beans/decoder/StaticMethodContext.java new file mode 100644 index 00000000000..959c949f77b --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/StaticMethodContext.java @@ -0,0 +1,95 @@ +/* gnu.java.beans.decoder.StaticMethodContext + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; + +/** + * @author Robert Schuster + */ +class StaticMethodContext extends AbstractCreatableObjectContext +{ + private ArrayList arguments = new ArrayList(); + private Class klass; + private String methodName; + + StaticMethodContext(String id, Class newClass, String newMethodName) + { + setId(id); + klass = newClass; + methodName = newMethodName; + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#addObject(java.lang.Object) + */ + public void addParameterObjectImpl(Object o) + { + arguments.add(o); + } + + /* (non-Javadoc) + * @see gnu.java.beans.decoder.Context#endContext(gnu.java.beans.decoder.Context) + */ + protected Object createObject(Context outerContext) + throws AssemblyException + { + Object[] args = arguments.toArray(); + + try + { + Method method = MethodFinder.getMethod(klass, methodName, args); + return method.invoke(null, args); + } + catch (NoSuchMethodException nsme) + { + throw new AssemblyException(nsme); + } + catch (InvocationTargetException ite) + { + // rethrows the reason for the InvocationTargetsException (ie. the exception in the called code) + throw new AssemblyException(ite.getCause()); + } + catch (IllegalAccessException iae) + { + throw new AssemblyException(iae); + } + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/StringHandler.java b/libjava/classpath/gnu/java/beans/decoder/StringHandler.java new file mode 100644 index 00000000000..6f2311a9e62 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/StringHandler.java @@ -0,0 +1,54 @@ +/* gnu.java.beans.decoder.StringHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +class StringHandler extends SimpleHandler +{ + /** + * @param PersistenceParser + */ + StringHandler(ElementHandler parent) + { + super(parent); + } + + protected Object parse(String characters) + { + return characters; + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/VoidHandler.java b/libjava/classpath/gnu/java/beans/decoder/VoidHandler.java new file mode 100644 index 00000000000..ca3664bbed1 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/VoidHandler.java @@ -0,0 +1,140 @@ +/* gnu.java.beans.decoder.VoidHandler + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.beans.decoder; + +import java.beans.ExceptionListener; + +import org.xml.sax.Attributes; + +public class VoidHandler extends AbstractElementHandler +{ + /** + * @param PersistenceParser + */ + VoidHandler(ElementHandler parent) + { + super(parent, true); + } + + protected Context startElement( + Attributes attributes, + ExceptionListener exceptionListener) + throws AssemblyException + { + Context ctx = startElementImpl(attributes); + ctx.setStatement(true); + + return ctx; + } + + private Context startElementImpl(Attributes attributes) + throws AssemblyException + { + String id = attributes.getValue("id"); + String className = attributes.getValue("class"); + String methodName = attributes.getValue("method"); + String propertyName = attributes.getValue("property"); + String index = attributes.getValue("index"); + + if (className != null) + { + try + { + Class klass = instantiateClass(className); + + // class name exists which means that we are in a static context. + // so we may want to ... + // run a constructor if methodName is "new" or null + if (methodName == null || methodName.equals("new")) + // if the id is null the result cannot be by the decoder accessed but the + // constructor may have side effects (e.g. registering itself in a global registry) + return new ConstructorContext(id, klass); + + // (falling through is important!) + // run a static method on the given class (if methodName exists, which is implied already) + return new StaticMethodContext(id, klass, methodName); + } + catch (ClassNotFoundException cnfe) + { + throw new AssemblyException(cnfe); + } + } + else + { + // className does not exist which means we are in the context of + // some object and want to ... + // access an element by index + if (index != null) + { + // note: whether this resolves into get(i) or set(i, o) depends on the + // number of arguments and is decided by the ObjectAssembler + try + { + return new IndexContext(id, Integer.parseInt(index)); + } + catch (NumberFormatException nfe) + { + throw new AssemblyException(nfe); + } + } + + // access a method if methodName exists + if (methodName != null) + return new MethodContext(id, methodName); + + // (falling through is important!) + // access a property if a propertyName exists + if (propertyName != null && propertyName.length() > 0) + // this is reported as an ordinary method invocation where the propertyName is + // converted into a 'setter'-method name: convert first character of property name + // to upper case and prepend 'set' + // Note: This will be a setter-method because the <void> tag implies that no return + // value is expected (but a side effect) + return new PropertyContext(id, propertyName); + } + + // if code reaches this point the tag has wrong attributes. The following test + // does not make it better but can provide are more specific error message for + // a common mistake: <void> tags are not allowed to have an idref attribute + throw new AssemblyException( + new IllegalArgumentException( + (attributes.getValue("idref") == null) + ? "Missing attributes for <void> tag" + : "<void> does not support 'idref' attribute.")); + } +} diff --git a/libjava/classpath/gnu/java/beans/decoder/package.html b/libjava/classpath/gnu/java/beans/decoder/package.html new file mode 100644 index 00000000000..8fe65eeed78 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/decoder/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.beans.decoder package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.beans.decoder</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/beans/editors/ColorEditor.java b/libjava/classpath/gnu/java/beans/editors/ColorEditor.java new file mode 100644 index 00000000000..b099eb2f0e9 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/ColorEditor.java @@ -0,0 +1,100 @@ +/* gnu.java.beans.editors.ColorEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.awt.Color; +import java.beans.PropertyEditorSupport; + +/** + ** NativeByteEditor is a property editor for the + ** byte type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class ColorEditor extends PropertyEditorSupport { + Color[] stdColors = {Color.black,Color.blue,Color.cyan, + Color.darkGray,Color.gray,Color.green, + Color.lightGray,Color.magenta,Color.orange, + Color.pink,Color.red,Color.white, + Color.yellow}; + String[] stdColorNames = {"black","blue","cyan", + "dark gray","gray","green", + "light gray","magenta","orange", + "pink","red","white", + "yellow"}; + + /** setAsText for Color checks for standard color names + ** and then checks for a #RRGGBB value or just RRGGBB, + ** both in hex. + **/ + public void setAsText(String val) throws IllegalArgumentException { + if(val.length() == 0) { + throw new IllegalArgumentException("Tried to set empty value!"); + } + for(int i=0;i<stdColorNames.length;i++) { + if(stdColorNames[i].equalsIgnoreCase(val)) { + setValue(stdColors[i]); + return; + } + } + if(val.charAt(0) == '#') { + setValue(new Color(Integer.parseInt(val.substring(1),16))); + } else { + setValue(new Color(Integer.parseInt(val,16))); + } + } + + /** getAsText for Color turns the color into either one of the standard + ** colors or into an RGB hex value with # prepended. **/ + public String getAsText() { + for(int i=0;i<stdColors.length;i++) { + if(stdColors[i].equals(getValue())) { + return stdColorNames[i]; + } + } + return "#" + Integer.toHexString(((Color)getValue()).getRGB() & 0x00FFFFFF); + } + + /** getTags for Color returns a list of standard colors. **/ + public String[] getTags() { + return stdColorNames; + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/FontEditor.java b/libjava/classpath/gnu/java/beans/editors/FontEditor.java new file mode 100644 index 00000000000..904f7bebfbe --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/FontEditor.java @@ -0,0 +1,77 @@ +/* gnu.java.beans.editors.FontEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.awt.Font; +import java.beans.PropertyEditorSupport; + +/** + ** FontEditor is a property editor for java.awt.Font. + ** + ** <STRONG>To Do:</STRONG> Add custom font chooser + ** component. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class FontEditor extends PropertyEditorSupport { + /** setAsText for Font calls Font.decode(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Font.decode(val)); + } + + /** getAsText for Font returns a value in the format + ** expected by Font.decode(). + **/ + public String getAsText() { + Font f = (Font)getValue(); + if(f.isBold()) { + if(f.isItalic()) { + return f.getName()+"-bolditalic-"+f.getSize(); + } else { + return f.getName()+"-bold-"+f.getSize(); + } + } else if(f.isItalic()) { + return f.getName()+"-italic-"+f.getSize(); + } else { + return f.getName()+"-"+f.getSize(); + } + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeBooleanEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeBooleanEditor.java new file mode 100644 index 00000000000..3620cc1747a --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeBooleanEditor.java @@ -0,0 +1,76 @@ +/* gnu.java.beans.editors.NativeBooleanEditor + Copyright (C) 1998, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeBooleanEditor is a property editor for the + ** boolean type.<P> + ** + ** <STRONG>To Do:</STRONG> add support for a checkbox + ** as the custom editor. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeBooleanEditor extends PropertyEditorSupport { + String[] tags = {"true","false"}; + + /** + * setAsText for boolean checks for true or false or t or f. + * "" also means false. + **/ + public void setAsText(String val) throws IllegalArgumentException { + if(val.equalsIgnoreCase("true") || val.equalsIgnoreCase("t")) { + setValue(Boolean.TRUE); + } else if(val.equalsIgnoreCase("false") || val.equalsIgnoreCase("f") || val.equals("")) { + setValue(Boolean.FALSE); + } else { + throw new IllegalArgumentException("Value must be true, false, t, f or empty."); + } + } + + + /** getAsText for boolean calls Boolean.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeByteEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeByteEditor.java new file mode 100644 index 00000000000..f3ec5fa1945 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeByteEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.NativeByteEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeByteEditor is a property editor for the + ** byte type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeByteEditor extends PropertyEditorSupport { + /** setAsText for byte calls Byte.valueOf(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Byte.valueOf(val)); + } + + /** getAsText for byte calls Byte.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeDoubleEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeDoubleEditor.java new file mode 100644 index 00000000000..8d8aae15337 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeDoubleEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.NativeDoubleEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeDoubleEditor is a property editor for the + ** double type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeDoubleEditor extends PropertyEditorSupport { + /** setAsText for double calls Double.valueOf(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Double.valueOf(val)); + } + + /** getAsText for double calls Double.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeFloatEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeFloatEditor.java new file mode 100644 index 00000000000..801377e7df9 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeFloatEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.NativeFloatEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeFloatEditor is a property editor for the + ** float type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeFloatEditor extends PropertyEditorSupport { + /** setAsText for float calls Float.valueOf(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Float.valueOf(val)); + } + + /** getAsText for float calls Float.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeIntEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeIntEditor.java new file mode 100644 index 00000000000..a6148841c46 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeIntEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.NativeIntEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeIntEditor is a property editor for the + ** int type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeIntEditor extends PropertyEditorSupport { + /** setAsText for int calls Integer.valueOf(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Integer.valueOf(val)); + } + + /** getAsText for int calls Integer.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeLongEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeLongEditor.java new file mode 100644 index 00000000000..95e9dc87028 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeLongEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.NativeLongEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeLongEditor is a property editor for the + ** long type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeLongEditor extends PropertyEditorSupport { + /** setAsText for long calls Long.valueOf(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Long.valueOf(val)); + } + + /** getAsText for long calls Long.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/NativeShortEditor.java b/libjava/classpath/gnu/java/beans/editors/NativeShortEditor.java new file mode 100644 index 00000000000..ffaa266492e --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/NativeShortEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.NativeShortEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeShortEditor is a property editor for the + ** short type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class NativeShortEditor extends PropertyEditorSupport { + /** setAsText for short calls Short.valueOf(). **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(Short.valueOf(val)); + } + + /** getAsText for short calls Short.toString(). **/ + public String getAsText() { + return getValue().toString(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/StringEditor.java b/libjava/classpath/gnu/java/beans/editors/StringEditor.java new file mode 100644 index 00000000000..8242d5475a5 --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/StringEditor.java @@ -0,0 +1,61 @@ +/* gnu.java.beans.editors.StringEditor + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.beans.editors; + +import java.beans.PropertyEditorSupport; + +/** + ** NativeByteEditor is a property editor for the + ** byte type. + ** + ** @author John Keiser + ** @version 1.1.0, 29 Jul 1998 + **/ + +public class StringEditor extends PropertyEditorSupport { + /** setAsText just sets the value. **/ + public void setAsText(String val) throws IllegalArgumentException { + setValue(val); + } + + /** getAsText just returns the value. **/ + public String getAsText() { + return (String)getValue(); + } +} diff --git a/libjava/classpath/gnu/java/beans/editors/TODO b/libjava/classpath/gnu/java/beans/editors/TODO new file mode 100644 index 00000000000..6877f4caf4b --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/TODO @@ -0,0 +1,4 @@ +- write tests for all editors +- add some sort of ColorChooser as a custom editor for ColorEditor +- add a FileNameEditor +- add a FontChooser as a custom editor for FontEditor diff --git a/libjava/classpath/gnu/java/beans/editors/package.html b/libjava/classpath/gnu/java/beans/editors/package.html new file mode 100644 index 00000000000..465f68d174d --- /dev/null +++ b/libjava/classpath/gnu/java/beans/editors/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.beans.editors package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.beans.editors</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/beans/package.html b/libjava/classpath/gnu/java/beans/package.html new file mode 100644 index 00000000000..f3b0526834c --- /dev/null +++ b/libjava/classpath/gnu/java/beans/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.beans package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.beans</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/io/ASN1ParsingException.java b/libjava/classpath/gnu/java/io/ASN1ParsingException.java new file mode 100644 index 00000000000..5d2c64ab54f --- /dev/null +++ b/libjava/classpath/gnu/java/io/ASN1ParsingException.java @@ -0,0 +1,56 @@ +/* ASN1ParsingException.java -- ASN.1 parsing exception. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.io; + +/** + * Signals a malformed ASN.1 sequence. + */ +public class ASN1ParsingException extends java.io.IOException +{ + + public ASN1ParsingException() + { + super(); + } + + public ASN1ParsingException(String msg) + { + super(msg); + } +} diff --git a/libjava/classpath/gnu/java/io/Base64InputStream.java b/libjava/classpath/gnu/java/io/Base64InputStream.java new file mode 100644 index 00000000000..b0824ab6935 --- /dev/null +++ b/libjava/classpath/gnu/java/io/Base64InputStream.java @@ -0,0 +1,216 @@ +/* Base64InputStream.java -- base-64 input stream. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.io; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * A filter input stream that decodes data encoded in the Base-64 + * encoding scheme. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class Base64InputStream extends FilterInputStream +{ + + // Constants and fields. + // ------------------------------------------------------------------------ + + /** Base-64 digits. */ + private static final String BASE_64 = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + /** Base-64 padding character. */ + private static final char BASE_64_PAD = '='; + + /** Decoding state. */ + private int state; + + /** Intermediate decoded value. */ + private int temp; + + /** EOF flag. */ + private boolean eof; + + private final byte[] one = new byte[1]; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new Base-64 input stream. The input bytes must be the + * ASCII characters A-Z, a-z, 0-9, + and /, with optional whitespace, + * and will be decoded into a byte stream. + * + * @param in The source of Base-64 input. + */ + public Base64InputStream(InputStream in) + { + super(in); + state = 0; + temp = 0; + eof = false; + } + + // Class method. + // ------------------------------------------------------------------------ + + /** + * Decode a single Base-64 string to a byte array. + * + * @param base64 The Base-64 encoded data. + * @return The decoded bytes. + * @throws IOException If the given data do not compose a valid Base-64 + * sequence. + */ + public static byte[] decode(String base64) throws IOException + { + Base64InputStream in = + new Base64InputStream(new ByteArrayInputStream(base64.getBytes())); + ByteArrayOutputStream out = + new ByteArrayOutputStream((int) (base64.length() / 0.666)); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) != -1) + out.write(buf, 0, len); + return out.toByteArray(); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + public int available() + { + return 0; + } + + public int read() throws IOException + { + if (read(one) == 1) + return one[0]; + return -1; + } + + public int read(byte[] buf, int off, int len) throws IOException + { + if (eof) + return -1; + int count = 0; + while (count < len) + { + int i; + while (Character.isWhitespace((char) (i = in.read()))); + int pos = BASE_64.indexOf((char) i); + if (pos >= 0) + { + switch (state) + { + case 0: + temp = pos << 2; + state = 1; + break; + case 1: + buf[count++] = (byte) (temp | (pos >>> 4)); + temp = (pos & 0x0F) << 4; + state = 2; + break; + case 2: + buf[count++] = (byte) (temp | (pos >>> 2)); + temp = (pos & 0x03) << 6; + state = 3; + break; + case 3: + buf[count++] = (byte) (temp | pos); + state = 0; + break; + } + } + else if (i == BASE_64_PAD) + { + switch (state) + { + case 0: + case 1: + throw new IOException("malformed Base-64 input"); + case 2: + while (Character.isWhitespace((char) (i = in.read()))); + if (i != BASE_64_PAD) + throw new IOException("malformed Base-64 input"); + case 3: + while (Character.isWhitespace((char) (i = in.read()))); + } + eof = true; + break; + } + else // First non-Base-64 character, consider it end-of-stream. + { + if (state != 0) + throw new IOException("malformed Base-64 input"); + eof = true; + break; + } + } + return count; + } + + public boolean markSupported() + { + return false; + } + + public void mark(int markLimit) { } + + public void reset() throws IOException + { + throw new IOException("reset not supported"); + } + + public long skip(long n) throws IOException + { + long skipped; + for (skipped = 0; skipped < n; skipped++) + if (read() == -1) + break; + return skipped; + } +} diff --git a/libjava/classpath/gnu/java/io/ClassLoaderObjectInputStream.java b/libjava/classpath/gnu/java/io/ClassLoaderObjectInputStream.java new file mode 100644 index 00000000000..238ab26b1b9 --- /dev/null +++ b/libjava/classpath/gnu/java/io/ClassLoaderObjectInputStream.java @@ -0,0 +1,73 @@ +/* gnu.java.io.ClassLoaderObjectInputStream + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.io; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.StreamCorruptedException; + +/** + * ClassLoaderObjectInputStream is ObjectInputStream, with + * the ability to use a specific ClassLoader. + * + * @author Geoff Berry + * @version 1.1.0, 29 Jul 1998 + */ + +public class ClassLoaderObjectInputStream extends ObjectInputStream { + ClassLoader myClassLoader; + + /** Create the new ClassLoaderObjectInputStream. + * @param in the InputStream to read the Objects from. + * @param myClassLoader the ClassLoader to load classes + * with. + */ + public ClassLoaderObjectInputStream(InputStream in, ClassLoader myClassLoader) throws IOException,StreamCorruptedException { + super(in); + this.myClassLoader = myClassLoader; + } + + /** Overriden method to use the loadClass() method from + * the ClassLoader. + */ + public Class resolveClass(String name) throws IOException, ClassNotFoundException { + return myClassLoader.loadClass(name); + } +} diff --git a/libjava/classpath/gnu/java/io/NullOutputStream.java b/libjava/classpath/gnu/java/io/NullOutputStream.java new file mode 100644 index 00000000000..526bd26e830 --- /dev/null +++ b/libjava/classpath/gnu/java/io/NullOutputStream.java @@ -0,0 +1,56 @@ +/* NullOutputStream.java -- OutputStream that does absolutely nothing + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.io; + +import java.io.OutputStream; + +/** + This is a placeholder OutputStream that does absolutley nothing + when written to. It is intended to be used in the same manner as + /dev/null. None of this class's methods do anything at all. +*/ +public class NullOutputStream extends OutputStream +{ + public NullOutputStream() {} + public void write( int b ) {} + public void write( byte b[] ) {} + public void write( byte b[], int off, int len ) {} + public void flush() {} + public void close() {} +} diff --git a/libjava/classpath/gnu/java/io/ObjectIdentityWrapper.java b/libjava/classpath/gnu/java/io/ObjectIdentityWrapper.java new file mode 100644 index 00000000000..6db2e3a52a6 --- /dev/null +++ b/libjava/classpath/gnu/java/io/ObjectIdentityWrapper.java @@ -0,0 +1,100 @@ +/* ObjectIdentityWrapper.java -- Wrapper class used to override equals() + and hashCode() to be as discriminating as possible + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.io; + +/** + This class is a thin wrapper around <code>Object</code> that makes + the methods <code>hashCode()</code> and <code>equals(Object)</code> + as discriminating as possible. +*/ +public class ObjectIdentityWrapper +{ + + /** + Constructs a <code>ObjectIdentityWrapper</code> that is wrapped + around o. + */ + public ObjectIdentityWrapper( Object o ) + { + object = o; + } + + /** + Uses <code>System.identityHashCode(Object)</code> to compute a + hash code for the object wrapped by this + <code>ObjectIdentityWrapper</code>. + + @see java.lang.System#identityHashCode(java.lang.Object) + @see java.util.Hashtable + @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return System.identityHashCode( object ); + } + + /** + Uses the <code>==</code> operator to test for equality between + the object wrapped by this <code>ObjectIdentityWrapper</code> and + the object wrapped by the <code>ObjectIdentityWrapper</code> o. + Returns false if o is not a <code>ObjectIdentityWrapper</code>. + + @see java.util.Hashtable + @see java.lang.Object#equals() + */ + public boolean equals( Object o ) + { + if( o instanceof ObjectIdentityWrapper ) + return object == ((ObjectIdentityWrapper)o).object; + else + return false; + } + + public String toString() + { + return "ObjectIdentityWrapper< " + object + ", " + hashCode() + " >"; + } + + /** + The <code>Object</code> wrapped by this + <code>ObjectIdentityWrapper</code>. + */ + public Object object; +} diff --git a/libjava/classpath/gnu/java/io/PlatformHelper.java b/libjava/classpath/gnu/java/io/PlatformHelper.java new file mode 100644 index 00000000000..d2c60123192 --- /dev/null +++ b/libjava/classpath/gnu/java/io/PlatformHelper.java @@ -0,0 +1,228 @@ +/* PlatformHelper.java -- Isolate OS-specific IO helper methods and variables + Copyright (C) 1998, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.io; + +import java.util.StringTokenizer; + +/** + * We had many changes in File.java, URLStreamHandler.java etc. to handle + * path representations on different platforms (Windows/Unix-family). + * Finally we'd like to collect all these ad hoc codes into this utility class. + * --Gansha + */ +public class PlatformHelper +{ + public static final boolean isWindows = System.getProperty("os.name").indexOf("Windows") >= 0; + public static final String separator = System.getProperty("file.separator"); + public static final char separatorChar = separator.charAt(0); + public static final String pathSeparator = System.getProperty("path.separator"); + public static final char pathSeparatorChar = pathSeparator.charAt(0); + + /** + * On most platforms 260 is equal or greater than a max path value, + * so we can set the initial buffer size of StringBuffer to half of this value + * to improve performance. + */ + public static final int INITIAL_MAX_PATH = 260/2; + + /** + * This routine checks the input param "path" whether it begins with root path + * prefix. + * if not, return 0; + * if yes, return the len of root path prefix; + * --for Unix-family platform, root path begins with "/" and len is 1 + * --for Windows platform, root path begins with "drive:\\" and len is 3 + */ + public static final int beginWithRootPathPrefix(String path) + { + if (path.startsWith("/") || path.startsWith("\\")) + return 1; + + if (!isWindows) + return 0; + + if (path.length() > 2 + && Character.isLetter(path.charAt(0)) + && path.charAt(1) == ':' + && (path.charAt(2) == '/' || path.charAt(2) == '\\')) + return 3; + + return 0; + } + + /** + * This routine checks the input param "path" whether it's root directory. + * --for Unix-family platform, root directory is "/" + * --for Windows platform, root directory is "\\" or "drive:\\". + */ + public static final boolean isRootDirectory(String path) + { + int len = path.length(); + return len > 0 && beginWithRootPathPrefix(path) == len; + } + + /** + * This routine canonicalizes input param "path" to formal path representation + * for current platform, including interpreting ".." and "." . + */ + public static final String toCanonicalForm(String path) + { + /*?? + if(path.indexOf('.') < 0 && path.indexOf("..") < 0) + return path; + */ + String tmppath = path.replace('/', separatorChar); + StringBuffer canonpath; + + // We found it'll be more efficient and easy to handle to + // return a lowercased canonical path + if(isWindows) + tmppath = tmppath.toLowerCase(); + + int i; + + if ((i = beginWithRootPathPrefix(tmppath)) == 0 ) + return path; + + /* The original + "canonpath = new StringBuffer(tmppath.substring(0, i))" + isn't very efficient because StringBuffer's + ensureCapacity_unsynchronized will fail definitely each time + and will enlarge buffer and copy contents. . + */ + canonpath = new StringBuffer(INITIAL_MAX_PATH); + canonpath.append(tmppath.substring(0, i)); + tmppath = tmppath.substring(i); + // pathdepth==0 indicates there're only root path in the buffer + int pathdepth = 0; + + StringTokenizer st = new StringTokenizer(tmppath, separator); + + // Traverse each element of the path, handling "." and ".." + // Should handle "~" too? + if (st.hasMoreTokens()) + do + { + String s = st.nextToken(); + + // Handle "." or an empty element. + if (s.equals(".") || s.equals("")) + continue; + + // Handle ".." by deleting the last element from the path + if (s.equals("..")) + { + if (pathdepth == 0) + continue; + + // Strip of trailing separator + canonpath.setLength(canonpath.length() - 1/*separator.length()*/); + String tmpstr = canonpath.toString(); + int idx = tmpstr.lastIndexOf(separator); + + if ((idx == -1) || ((idx + 1/*separator.length()*/) > tmpstr.length())) + //throw new IOException("Can't happen error"); + return path; // Shouldn't happen + + canonpath.setLength(idx + 1/*separator.length()*/); + pathdepth--; + continue; + } + + canonpath.append(s); + pathdepth++; //now it's more than root path + + if (st.hasMoreTokens()) + canonpath.append(separator); + } + while (st.hasMoreTokens()); + + if (endWithSeparator(path)) + canonpath.append(separator); + + String tmpstr = canonpath.toString(); + //if (pathdepth > 0 && endWithSeparator(tmpstr) ) + // tmpstr = tmpstr.substring(0, tmpstr.length() - 1/*separator.length()*/); + + return tmpstr; + } + + /** + * This routine canonicalizes input param "path" to formal path representation + * for current platform, and normalize all separators to "sepchar". + */ + public static final String toCanonicalForm(String path, char sepchar) + { + String tmpstr = toCanonicalForm(path); + tmpstr = tmpstr.replace(separatorChar, sepchar); + return tmpstr; + } + + /** + * This routine checks whether input param "path" ends with separator + */ + public static final boolean endWithSeparator(String path) + { + if (path.endsWith("\\") || path.endsWith("/")) + return true; + + return false; + } + + /** + * This routine removes from input param "path" the tail separator if it exists, + * and return the remain part. + */ + public static final String removeTailSeparator(String path) + { + if (endWithSeparator(path) && !isRootDirectory(path)) + return path.substring(0, path.length() - 1); + + return path; + } + + /** + * This routine returns last index of separator in input param "path", + * and return it. + */ + public static final int lastIndexOfSeparator(String path) + { + return Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")); + } + +} diff --git a/libjava/classpath/gnu/java/io/class-dependencies.conf b/libjava/classpath/gnu/java/io/class-dependencies.conf new file mode 100644 index 00000000000..2500f6b14b5 --- /dev/null +++ b/libjava/classpath/gnu/java/io/class-dependencies.conf @@ -0,0 +1,75 @@ +# This property file contains dependencies of classes, methods, and +# field on other methods or classes. +# +# Syntax: +# +# <used>: <needed 1> [... <needed N>] +# +# means that when <used> is included, <needed 1> (... <needed N>) must +# be included as well. +# +# <needed X> and <used> are of the form +# +# <class.methodOrField(signature)> +# +# or just +# +# <class> +# +# Within dependencies, variables can be used. A variable is defined as +# follows: +# +# {variable}: value1 value2 ... value<n> +# +# variables can be used on the right side of dependencies as follows: +# +# <used>: com.bla.blu.{variable}.Class.m()V +# +# The use of the variable will expand to <n> dependencies of the form +# +# <used>: com.bla.blu.value1.Class.m()V +# <used>: com.bla.blu.value2.Class.m()V +# ... +# <used>: com.bla.blu.value<n>.Class.m()V +# +# Variables can be redefined when building a system to select the +# required support for features like encodings, protocols, etc. +# +# Hints: +# +# - For methods and fields, the signature is mandatory. For +# specification, please see the Java Virtual Machine Specification by +# SUN. Unlike in the spec, field signatures (types) are in brackets. +# +# - Package names must be separated by '/' (and not '.'). E.g., +# java/lang/Class (this is necessary, because the '.' is used to +# separate method or field names from classes) +# +# - In case <needed> refers to a class, only the class itself will be +# included in the resulting binary, NOT necessarily all its methods +# and fields. If you want to refer to all methods and fields, you can +# write class.* as an abbreviation. +# +# - Abbreviations for packages are also possible: my/package/* means all +# methods and fields of all classes in my/package. +# +# - A line with a trailing '\' continues in the next line. + + +# All encodings supported are loaded via gnu/java/io/EncodingManager.findEncoderConstructor +# or gnu/java/io/EncodingManager.findDecoderConstructor from class +# gnu/java/io/decode/Decoder<encoding>. +# +# This introduces a dependency for all encodings. To allow an easy selection +# and addition of encodings, the library variable {encodings} can be set to +# the set of supported encodings. +# +{encodings}: 8859_1 8859_2 8859_3 8859_4 8859_5 UTF8 + +gnu/java/io/EncodingManager.findEncoderConstructor(Ljava/lang/String;Z)Ljava/lang/reflect/Constructor;: \ + gnu/java/io/decode/Decoder{encodings}.* + +gnu/java/io/EncodingManager.findDecoderConstructor(Ljava/lang/String;Z)Ljava/lang/reflect/Constructor;: \ + gnu/java/io/encode/Encoder{encodings}.* \ + +# end of file diff --git a/libjava/classpath/gnu/java/io/package.html b/libjava/classpath/gnu/java/io/package.html new file mode 100644 index 00000000000..74da8277f4c --- /dev/null +++ b/libjava/classpath/gnu/java/io/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.io package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.io</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/lang/ArrayHelper.java b/libjava/classpath/gnu/java/lang/ArrayHelper.java new file mode 100644 index 00000000000..5f675831e9f --- /dev/null +++ b/libjava/classpath/gnu/java/lang/ArrayHelper.java @@ -0,0 +1,78 @@ +/* ArrayHelper.java -- Helper methods for handling array operations + Copyright (C) 1998, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.lang; + +/** + * ArrayHelper helps you do things with arrays. + * + * @author John Keiser + */ +public class ArrayHelper +{ + /** + * Counterpart to java.util.Collection.contains. + * + * @param array the array to search + * @param searchFor the object to locate + * @return true if some array element <code>equals(searchFor)</code> + */ + public static boolean contains(Object[] array, Object searchFor) + { + return indexOf(array, searchFor) != -1; + } + + /** + * Counterpart to java.util.Collection.indexOf. + * + * @param array the array to search + * @param searchFor the object to locate + * @return the index of the first equal object, or -1 + */ + public static int indexOf(Object[] array, Object searchFor) + { + for (int i = 0; i < array.length; i++) + { + if(array[i].equals(searchFor)) + { + return i; + } + } + return -1; + } +} diff --git a/libjava/classpath/gnu/java/lang/CharData.java b/libjava/classpath/gnu/java/lang/CharData.java new file mode 100644 index 00000000000..b12078d9f98 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/CharData.java @@ -0,0 +1,1001 @@ +/* gnu/java/lang/CharData -- Database for java.lang.Character Unicode info + Copyright (C) 2002 Free Software Foundation, Inc. + *** This file is generated by scripts/unicode-muncher.pl *** + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.lang; + +/** + * This contains the info about the unicode characters, that + * java.lang.Character needs. It is generated automatically from + * <code>doc/unicode/UnicodeData-3.0.0.txt</code> and + * <code>doc/unicode/SpecialCasing-2.txt</code>, by some + * perl scripts. These Unicode definition file can be found on the + * <a href="http://www.unicode.org">http://www.unicode.org</a> website. + * JDK 1.4 uses Unicode version 3.0.0. + * + * The data is stored as string constants, but Character will convert these + * Strings to their respective <code>char[]</code> components. The field + * <code>BLOCKS</code> stores the offset of a block of 2<sup>SHIFT</sup> + * characters within <code>DATA</code>. The DATA field, in turn, stores + * information about each character in the low order bits, and an offset + * into the attribute tables <code>UPPER</code>, <code>LOWER</code>, + * <code>NUM_VALUE</code>, and <code>DIRECTION</code>. Notice that the + * attribute tables are much smaller than 0xffff entries; as many characters + * in Unicode share common attributes. The DIRECTION table also contains + * a field for detecting characters with multi-character uppercase expansions. + * Next, there is a listing for <code>TITLE</code> exceptions (most characters + * just have the same title case as upper case). Finally, there are two + * tables for multi-character capitalization, <code>UPPER_SPECIAL</code> + * which lists the characters which are special cased, and + * <code>UPPER_EXPAND</code>, which lists their expansion. + * + * @author scripts/unicode-muncher.pl (written by Jochen Hoenicke, + * Eric Blake) + * @see Character + * @see String + */ +public interface CharData +{ + /** + * The Unicode definition file that was parsed to build this database. + */ + String SOURCE = "doc/unicode/UnicodeData-3.0.0.txt"; + + /** + * The character shift amount to look up the block offset. In other words, + * <code>(char) (BLOCKS.value[ch >> SHIFT] + ch)</code> is the index where + * <code>ch</code> is described in <code>DATA</code>. + */ + int SHIFT = 5; + + /** + * The mapping of character blocks to their location in <code>DATA</code>. + * Each entry has been adjusted so that the 16-bit sum with the desired + * character gives the actual index into <code>DATA</code>. + */ + String BLOCKS + = "\u01c2\u01c2\u01c1\u012c\u012b\u01a0\u01f8\u02dc\u025f\u02ee\u0215" + + "\u0346\u02dc\u0326\u02bc\u0216\u015f\u02d4\u0376\u0376\u0376\u0369" + + "\ufe8f\u0344\uff85\uff65\ufdb5\ufda1\033\u02c4\034G\ufea8" + + "\uff8c\u0235\ufeff\032\ufebf&\ufb20\ufe28\u0113\u0104\ufb61" + + "\ufb5a\u010b\u0109\u00fe\uff08\u0229\u025e\u01c7\u01fc\u01dc\ufc46" + + "\u0229\ufe27\ufb55\u0169\ufbc8\u00fc\u0103\ufb68\ufb48\ufb28\ufb08" + + "\ufae8\ufac8\ufaa8\ufa88\ufa68\ufa48eP\u00ab\u0139\ufe0e" + + "c\u0155\u01a8\uf669\u0129\u0128\uf91f\ufe56\u0108\u0107\ufac0" + + "\ufc8e\ufead\u00c6\ufca7\ufb95\uf47d\u009f\ufb17\ufe20\ufd28\ufb2f" + + ";\uf3b9\ufe57\ufcce\uffbb\uf339\ufa98\uff8b\uff3b\ufa54\uf7e3" + + "\uff2b\ufad7\ufb69\ufc3a\ufee5\uf4c8\ufcb0\ufa88\ufdbf\uf448\ufe45" + + "\ufcc7\ufe4f\uf7f1\uf715\uf2e8\ufd9f\uf348\uf96a\ufc02\ufd97\uf2c8" + + "\uf2a8\uf4b9\uf4b3\uef6b\uf86a\uf84a\ufc58\uf80a\uf7ea\ufc0f\uf7aa" + + "\uee9c\ufb90\uf74a\uf7fa\uf70a\uf7ca\uf792\uf471\uf4d2\uf732\uf64a" + + "\uf401\uf64d\uefa8\uf5ca\uf5aa\ueca1\uf569\uf54a\uf52a\uf50a\uf4ea" + + "\uf4ca\uf4aa\uf48a\uf46a\uf44a\uf42a\uf40a\uf3ea\uf3ca\uf3aa\uf38a" + + "\uf36a\uf34a\uf32a\uf289\uf777\uf2ca\uf2aa\uf737\uec28\uec08\uebe8" + + "\uebc8\uf1ea\uf4a2\uf545\uedc6\uf2d7\uf14a\ue8ed\ue81e\uf0ea\uf597" + + "\uea68\uea48\uea28\uea08\ue9e8\ue9c8\ue9a8\ue988\ue968\ue948\ue928" + + "\ue908\ue8e8\ue8c8\ue8a8\ue888\ue868\ue848\ue828\ue808\ue7e8\ue7c8" + + "\ue7a8\ue788\ue768\ue748\ue728\ue708\ue6e8\ue6c8\ue6a8\ue688\ue668" + + "\ue648\ue628\ue608\ue5e8\ue5c8\ue5a8\ue588\ue568\ue548\ue55f\ue53f" + + "\ue51f\ue4ff\uefd7\ue4bf\ue49f\ue485\uef87\uef57\uef57\uef57\uef57" + + "\uef47\ue1ad\uef46\uef46\uef46\ue1e0\ue3dd\uef06\ue9d9\uebeb\ue244" + + "\ueed4\uef65\ue1f5\uef45\ueee9\uef7c\uee74\uef70\uef7d\uef78\uee91" + + "\uefd3\uee7d\uee25\uee27\uef65\uefdd\uee96\uefd3\uefe1\uef69\udf88" + + "\udf68\udf48\ued2b\ued3d\ued19\uef1c\uef08\ued47\ued3d\ued33\uec2b" + + "\uec0b\uebeb\uebcb\uebce\uea7c\ueb69\ueb6c\ue9b6\ueb0b\ueaeb\ue9e9" + + "\udca8\udc88\udc68\udc48\ue910\uea23\ueb58\ueb4f\ueb45\ueae5\udb68" + + "\udb48\ue92b\ue90b\ue8eb\ue8cb\ue8ab\ue88b\ue86b\ue84b\uda28\uda08" + + "\ud9e8\ud9c8\ud9a8\ud988\ud968\ud948\ud928\ud908\ud8e8\ud8c8\ud8a8" + + "\ud888\ud868\ud848\ud828\ud808\ud7e8\ud7c8\ud7a8\ud788\ud768\ud748" + + "\ud728\ud708\ud6e8\ud6c8\ud6a8\ud688\ud668\ud648\ud628\ud608\ud5e8" + + "\ud5c8\ud5a8\ud588\ud568\ud548\ud528\ud508\ud4e8\ud4c8\ue2b1\ue28b" + + "\ue26b\ue270\ue22b\ue20b\ue1eb\ue1cb\ue1ab\ue18b\ue18e\udd8f\ue3a8" + + "\udfd3\ud929\ud90a\ue348\ud8c9\ud8aa\udcd7\udcb2\ud681\ud82a\ud80a" + + "\ue268\ucede\ud168\ud148\ue116\ue0e9\ue1cb\ue0b7\ue0b7\ue15e\udf17" + + "\ue034\ue013\udff3\udfd3\ude6c\udf93\udf73\udf55\udf34\ud56a\ud54a" + + "\ud52a\ud50a\ud4ea\ud4ca\ud4aa\ud48a\ud46a\ud44a\ud42a\ud40a\ud3ea" + + "\ud3ca\ud3aa\ud38a\ud36a\ud34a\ud32a\ud30a\ud2ea\ud2ca\ud2aa\ud28a" + + "\ud26a\ud24a\ud22a\ud20a\ud1ea\ud1ca\ud1aa\ud18a\ud16a\ud14a\ud12a" + + "\ud10a\ud0ea\ud0ca\ud0aa\ud08a\ud06a\ud04a\ud02a\ud00a\ucfea\ucfca" + + "\ucfaa\ucf8a\ucf6a\ucf4a\ucf2a\ucf0a\uceea\uceca\uceaa\uce8a\uce6a" + + "\uce4a\uce2a\uce0a\ucdea\ucdca\ucdaa\ucd8a\ucd6a\ucd4a\ucd2a\ucd0a" + + "\uccea\uccca\uccaa\ucc8a\ucc6a\ucc4a\ucc2a\ucc0a\ucbea\ucbca\ucbaa" + + "\ucb8a\ucb6a\ucb4a\ucb2a\ucb0a\ucaea\ucaca\ucaaa\uca8a\uca6a\uca4a" + + "\uca2a\uca0a\uc9ea\uc9ca\uc9aa\uc98a\uc96a\uc94a\uc92a\uc90a\uc8ea" + + "\uc8ca\uc8aa\uc88a\uc86a\uc84a\uc82a\uc80a\uc7ea\uc7ca\uc7aa\uc78a" + + "\uc76a\uc74a\uc72a\uc70a\uc6ea\uc6ca\uc6aa\uc68a\uc66a\uc64a\uc62a" + + "\uc60a\uc5ea\uc5ca\uc5aa\uc58a\uc56a\uc54a\uc52a\uc50a\uc4ea\uc4ca" + + "\uc4aa\uc48a\uc46a\uc44a\uc42a\uc40a\uc3ea\uc3ca\uc3aa\uc38a\uc36a" + + "\uc34a\uc32a\uc30a\uc2ea\uc2ca\uc2aa\uc28a\uc26a\uc24a\uc22a\uc20a" + + "\uc1ea\uc1ca\uc1aa\uc18a\uc16a\uc14a\uc12a\uc10a\uc0ea\uc0ca\uc0aa" + + "\uc08a\uc06a\uc04a\uc02a\uc00a\ubfea\ubfca\ubfaa\ubf8a\ubf6a\ubf4a" + + "\ubf2a\ubf0a\ubeea\ubeca\ubeaa\ube8a\ube6a\ube4a\ube2a\ube0a\ubdea" + + "\ubdca\ubdaa\ubd8a\ubd6a\ubd4a\ubd2a\ubd0a\ubcea\ubcca\ubcaa\ubc8a" + + "\ubc6a\ubc4a\ubc2a\ubc0a\ubbea\ub2e0\ub568\ub548\ubb6a\ubb4a\ubb2a" + + "\ubb0a\ubaea\ubaca\ubaaa\uba8a\uba6a\uba4a\uba2a\uba0a\ub9ea\ub9ca" + + "\ub9aa\ub98a\ub96a\ub94a\ub92a\ub90a\ub8ea\ub8ca\ub8aa\ub88a\ub86a" + + "\ub84a\ub82a\ub80a\ub7ea\ub7ca\ub7aa\ub78a\ub76a\ub74a\ub72a\ub70a" + + "\ub6ea\ub6ca\ub6aa\ub68a\ub66a\ub64a\ub62a\ub60a\ub5ea\ub5ca\ub5aa" + + "\ub58a\ub56a\ub54a\ub52a\ub50a\ub4ea\ub4ca\ub4aa\ub48a\ub46a\ub44a" + + "\ub42a\ub40a\ub3ea\ub3ca\ub3aa\ub38a\ub36a\ub34a\ub32a\ub30a\ub2ea" + + "\ub2ca\ub2aa\ub28a\ub26a\ub24a\ub22a\ub20a\ub1ea\ub1ca\ub1aa\ub18a" + + "\ub16a\ub14a\ub12a\ub10a\ub0ea\ub0ca\ub0aa\ub08a\ub06a\ub04a\ub02a" + + "\ub00a\uafea\uafca\uafaa\uaf8a\uaf6a\uaf4a\uaf2a\uaf0a\uaeea\uaeca" + + "\uaeaa\uae8a\uae6a\uae4a\uae2a\uae0a\uadea\uadca\uadaa\uad8a\uad6a" + + "\uad4a\uad2a\uad0a\uacea\uacca\uacaa\uac8a\uac6a\uac4a\uac2a\uac0a" + + "\uabea\uabca\uabaa\uab8a\uab6a\uab4a\uab2a\uab0a\uaaea\uaaca\uaaaa" + + "\uaa8a\uaa6a\uaa4a\uaa2a\uaa0a\ua9ea\ua9ca\ua9aa\ua98a\ua96a\ua94a" + + "\ua92a\ua90a\ua8ea\ua8ca\ua8aa\ua88a\ua86a\ua84a\ua82a\ua80a\ua7ea" + + "\ua7ca\ua7aa\ua78a\ua76a\ua74a\ua72a\ua70a\ua6ea\ua6ca\ua6aa\ua68a" + + "\ua66a\ua64a\ua62a\ua60a\ua5ea\ua5ca\ua5aa\ua58a\ua56a\ua54a\ua52a" + + "\ua50a\ua4ea\ua4ca\ua4aa\ua48a\ua46a\ua44a\ua42a\ua40a\ua3ea\ua3ca" + + "\ua3aa\ua38a\ua36a\ua34a\ua32a\ua30a\ua2ea\ua2ca\ua2aa\ua28a\ua26a" + + "\ua24a\ua22a\ua20a\ua1ea\ua1ca\ua1aa\ua18a\ua16a\ua14a\ua12a\ua10a" + + "\ua0ea\ua0ca\ua0aa\ua08a\ua06a\ua04a\ua02a\ua00a\u9fea\u9fca\u9faa" + + "\u9f8a\u9f6a\u9f4a\u9f2a\u9f0a\u9eea\u9eca\u9eaa\u9e8a\u9e6a\u9e4a" + + "\u9e2a\u9e0a\u9dea\u9dca\u9daa\u9d8a\u9d6a\u9d4a\u9d2a\u9d0a\u9cea" + + "\u9cca\u9caa\u9c8a\u9c6a\u9c4a\u9c2a\u9c0a\u9bea\u9bca\u9baa\u9b8a" + + "\u9b6a\u9b4a\u9b2a\u9b0a\u9aea\u9aca\u9aaa\u9a8a\u9a6a\u9a4a\u9a2a" + + "\u9a0a\u99ea\u99ca\u99aa\u998a\u996a\u994a\u992a\u990a\u98ea\u98ca" + + "\u98aa\u988a\u986a\u984a\u982a\u980a\u97ea\u97ca\u97aa\u978a\u976a" + + "\u974a\u972a\u970a\u96ea\u96ca\u96aa\u968a\u966a\u964a\u962a\u960a" + + "\u95ea\u95ca\u95aa\u958a\u956a\u954a\u952a\u950a\u94ea\u94ca\u94aa" + + "\u948a\u946a\u944a\u942a\u940a\u93ea\u93ca\u93aa\u938a\u936a\u934a" + + "\u932a\u930a\u92ea\u92ca\u92aa\u928a\u926a\u924a\u922a\u920a\u91ea" + + "\u91ca\u91aa\u918a\u916a\u914a\u912a\u910a\u90ea\u90ca\u90aa\u908a" + + "\u906a\u904a\u902a\u900a\u8fea\u8fca\u8faa\u8f8a\u8f6a\u8f4a\u8f2a" + + "\u8f0a\u8eea\u8eca\u8eaa\u8e8a\u8e6a\u8e4a\u8e2a\u8e0a\u8dea\u8dca" + + "\u8daa\u8d8a\u8d6a\u8d4a\u8d2a\u8d0a\u8cea\u8cca\u8caa\u8c8a\u8c6a" + + "\u8c4a\u8c2a\u8c0a\u8bea\u8bca\u8baa\u8b8a\u8b6a\u8b4a\u8b2a\u8b0a" + + "\u8aea\u8aca\u8aaa\u8a8a\u8a6a\u8a4a\u8a2a\u8a0a\u89ea\u89ca\u89aa" + + "\u898a\u896a\u894a\u892a\u890a\u88ea\u88ca\u88aa\u888a\u886a\u884a" + + "\u882a\u880a\u87ea\u87ca\u87aa\u878a\u876a\u874a\u872a\u870a\u86ea" + + "\u86ca\u86aa\u868a\u866a\u864a\u862a\u860a\u85ea\u85ca\u85aa\u858a" + + "\u856a\u854a\u852a\u850a\u84ea\u84ca\u84aa\u848a\u846a\u844a\u842a" + + "\u840a\u83ea\u83ca\u83aa\u838a\u836a\u834a\u832a\u830a\u82ea\u82ca" + + "\u82aa\u828a\u826a\u824a\u822a\u820a\u81ea\u81ca\u81aa\u818a\u816a" + + "\u814a\u812a\u810a\u80ea\u80ca\u80aa\u808a\u806a\u804a\u802a\u800a" + + "\u7fea\u7fca\u7faa\u7f8a\u7f6a\u7f4a\u7f2a\u7f0a\u7eea\u7eca\u7eaa" + + "\u7e8a\u7e6a\u7e4a\u7e2a\u7e0a\u7dea\u7dca\u7daa\u7d8a\u7d6a\u7d4a" + + "\u7d2a\u7d0a\u7cea\u7cca\u7caa\u7c8a\u7c6a\u7c4a\u7c2a\u7c0a\u7bea" + + "\u7bca\u7baa\u7b8a\u7b6a\u7b4a\u7b2a\u7b0a\u7aea\u7aca\u7aaa\u7a8a" + + "\u7a6a\u7a4a\u7a2a\u7a0a\u79ea\u79ca\u79aa\u798a\u796a\u794a\u792a" + + "\u790a\u78ea\u78ca\u78aa\u788a\u786a\u784a\u782a\u780a\u77ea\u77ca" + + "\u77aa\u778a\u776a\u774a\u772a\u770a\u76ea\u76ca\u76aa\u768a\u766a" + + "\u764a\u762a\u760a\u75ea\u75ca\u75aa\u758a\u756a\u754a\u752a\u750a" + + "\u74ea\u74ca\u74aa\u748a\u746a\u744a\u742a\u740a\u73ea\u73ca\u73aa" + + "\u738a\u736a\u734a\u732a\u730a\u72ea\u72ca\u72aa\u728a\u726a\u724a" + + "\u722a\u720a\u71ea\u71ca\u71aa\u718a\u716a\u714a\u712a\u710a\u70ea" + + "\u70ca\u70aa\u708a\u706a\u704a\u702a\u700a\u6fea\u6fca\u6faa\u6f8a" + + "\u6f6a\u6f4a\u6f2a\u6f0a\u6eea\u6eca\u6eaa\u6e8a\u6e6a\u6e4a\u6e2a" + + "\u6e0a\u6dea\u6dca\u6daa\u6d8a\u6d6a\u6d4a\u6d2a\u6d0a\u6cea\u6cca" + + "\u6caa\u6c8a\u6c6a\u6c4a\u6c2a\u6c0a\u6bea\u6bca\u6baa\u6b8a\u6b6a" + + "\u6b4a\u6b2a\u6b0a\u6aea\u6aca\u6aaa\u6a8a\u6a6a\u6a4a\u6a2a\u6a0a" + + "\u69ea\u60f0\u6368\u6348\u696a\u694a\u692a\u690a\u68ea\u68ca\u68aa" + + "\u688a\u686a\u684a\u682a\u680a\u67ea\u67ca\u67aa\u678a\u676a\u674a" + + "\u672a\u670a\u66ea\u66ca\u66aa\u668a\u666a\u664a\u662a\u660a\u65ea" + + "\u65ca\u65aa\u658a\u656a\u654a\u652a\u650a\u6b26\u6de1\u6e9c\u5e48" + + "\u5e28\u5e08\u5de8\u5dc8\u5da8\u5d88\u5d68\u5d48\u5d28\u5d08\u5ce8" + + "\u5cc8\u5ca8\u5c88\u5c68\u5c48\u5c28\u5c08\u5be8\u5bc8\u5ba8\u5b88" + + "\u5b68\u5b48\u5b28\u5b08\u5ae8\u5ac8\u5aa8\u5a88\u5a68\u5a48\u5a28" + + "\u5a08\u59e8\u59c8\u59a8\u5988\u5968\u5948\u5928\u5908\u58e8\u58c8" + + "\u58a8\u5888\u5868\u5848\u5828\u5808\u57e8\u57c8\u57a8\u5788\u5768" + + "\u5748\u5d6a\u5d4a\u5d2a\u5d0a\u5cea\u5cca\u5caa\u5c8a\u5c6a\u5c4a" + + "\u5c2a\u5c0a\u5bea\u5bca\u5baa\u5b8a\u5b6a\u5b4a\u5b2a\u5b0a\u5aea" + + "\u5aca\u5aaa\u5a8a\u5a6a\u5a4a\u5a2a\u5a0a\u59ea\u59ca\u59aa\u598a" + + "\u596a\u594a\u592a\u590a\u58ea\u58ca\u58aa\u588a\u586a\u584a\u582a" + + "\u580a\u57ea\u57ca\u57aa\u578a\u576a\u574a\u572a\u570a\u56ea\u56ca" + + "\u56aa\u568a\u566a\u564a\u562a\u560a\u55ea\u55ca\u55aa\u558a\u556a" + + "\u554a\u552a\u550a\u54ea\u54ca\u54aa\u548a\u546a\u544a\u542a\u540a" + + "\u53ea\u53ca\u53aa\u538a\u536a\u534a\u532a\u530a\u52ea\u52ca\u52aa" + + "\u528a\u526a\u524a\u522a\u520a\u51ea\u51ca\u51aa\u518a\u516a\u514a" + + "\u512a\u510a\u50ea\u50ca\u50aa\u508a\u506a\u504a\u502a\u500a\u4fea" + + "\u4fca\u4faa\u4f8a\u4f6a\u4f4a\u4f2a\u4f0a\u4eea\u4eca\u4eaa\u4e8a" + + "\u4e6a\u4e4a\u4e2a\u4e0a\u4dea\u4dca\u4daa\u4d8a\u4d6a\u4d4a\u4d2a" + + "\u4d0a\u4cea\u4cca\u4caa\u4c8a\u4c6a\u4c4a\u4c2a\u4c0a\u4bea\u4bca" + + "\u4baa\u4b8a\u4b6a\u4b4a\u4b2a\u4b0a\u4aea\u4aca\u4aaa\u4a8a\u4a6a" + + "\u4a4a\u4a2a\u4a0a\u49ea\u49ca\u49aa\u498a\u496a\u494a\u492a\u490a" + + "\u48ea\u48ca\u48aa\u488a\u486a\u484a\u482a\u480a\u47ea\u47ca\u47aa" + + "\u478a\u476a\u474a\u472a\u470a\u46ea\u46ca\u46aa\u468a\u466a\u464a" + + "\u462a\u460a\u45ea\u45ca\u45aa\u458a\u456a\u454a\u452a\u450a\u44ea" + + "\u44ca\u44aa\u448a\u446a\u444a\u442a\u440a\u43ea\u43ca\u43aa\u438a" + + "\u436a\u434a\u432a\u430a\u42ea\u42ca\u42aa\u428a\u426a\u424a\u422a" + + "\u420a\u41ea\u41ca\u41aa\u418a\u416a\u414a\u412a\u410a\u40ea\u40ca" + + "\u40aa\u408a\u406a\u404a\u402a\u400a\u3fea\u3fca\u3faa\u3f8a\u3f6a" + + "\u3f4a\u3f2a\u3f0a\u3eea\u3eca\u3eaa\u3e8a\u3e6a\u3e4a\u3e2a\u3e0a" + + "\u3dea\u3dca\u3daa\u3d8a\u3d6a\u3d4a\u3d2a\u3d0a\u3cea\u3cca\u3caa" + + "\u3c8a\u3c6a\u3c4a\u3c2a\u3c0a\u3bea\u3bca\u3baa\u3b8a\u3b6a\u3b4a" + + "\u3b2a\u3b0a\u3aea\u3aca\u3aaa\u3a8a\u3a6a\u3a4a\u3a2a\u3a0a\u39ea" + + "\u39ca\u39aa\u398a\u396a\u394a\u392a\u390a\u38ea\u38ca\u38aa\u388a" + + "\u386a\u384a\u382a\u380a\u37ea\u37ca\u37aa\u378a\u376a\u374a\u372a" + + "\u370a\u36ea\u36ca\u36aa\u368a\u366a\u364a\u362a\u360a\u35ea\u35ca" + + "\u35aa\u358a\u356a\u354a\u352a\u350a\u34ea\u34ca\u34aa\u348a\u346a" + + "\u344a\u342a\u340a\u33ea\u33ca\u33aa\u338a\u336a\u334a\u332a\u330a" + + "\u32ea\u32ca\u32aa\u328a\u326a\u324a\u322a\u320a\u31ea\u28f2\u2b68" + + "\u2b48\u3c2b\u3c0b\u3beb\u3bcb\u3bab\u3b8b\u3b6b\u3b4b\u3b2b\u3b0b" + + "\u3aeb\u3acb\u3aab\u3a8b\u3a6b\u3a4b\u3a2b\u3a0b\u39eb\u39cb\u39ab" + + "\u398b\u396b\u394b\u392b\u390b\u38eb\u38cb\u38ab\u388b\u386b\u384b" + + "\u382b\u380b\u37eb\u37cb\u37ab\u378b\u376b\u374b\u372b\u370b\u36eb" + + "\u36cb\u36ab\u368b\u366b\u364b\u362b\u360b\u35eb\u35cb\u35ab\u358b" + + "\u356b\u354b\u352b\u350b\u34eb\u34cb\u34ab\u348b\u346b\u344b\u344b" + + "\u342b\u340b\u33eb\u33cb\u33ab\u338b\u336b\u334b\u332b\u330b\u32eb" + + "\u32cb\u32ab\u328b\u326b\u324b\u322b\u320b\u31eb\u31cb\u31ab\u318b" + + "\u316b\u314b\u312b\u310b\u30eb\u30cb\u30ab\u308b\u306b\u304b\u302b" + + "\u300b\u2feb\u2fcb\u2fab\u2f8b\u2f6b\u2f4b\u2f2b\u2f0b\u2eeb\u2ecb" + + "\u2eab\u2e8b\u2e6b\u2e4b\u2e2b\u2e0b\u2deb\u2dcb\u2dab\u2d8b\u2d6b" + + "\u2d4b\u2d2b\u2d0b\u2ceb\u2ccb\u2cab\u2c8b\u2c6b\u2c4b\u2c2b\u2c0b" + + "\u2beb\u2bcb\u2bab\u2b8b\u2b6b\u2b4b\u2b2b\u2b0b\u2aeb\u2acb\u2aab" + + "\u2a8b\u2a6b\u2a4b\u2a2b\u2a0b\u29eb\u29cb\u29ab\u298b\u296b\u294b" + + "\u292b\u290b\u28eb\u28cb\u28ab\u288b\u286b\u284b\u282b\u280b\u27eb" + + "\u27cb\u27ab\u278b\u276b\u274b\u272b\u270b\u26eb\u26cb\u26ab\u268b" + + "\u266b\u264b\u262b\u260b\u25eb\u25cb\u25ab\u258b\u256b\u254b\u252b" + + "\u250b\u24eb\u24cb\u24ab\u248b\u246b\u244b\u242b\u240b\u23eb\u23cb" + + "\u23ab\u238b\u236b\u234b\u232b\u230b\u22eb\u22cb\u22ab\u228b\u226b" + + "\u224b\u222b\u220b\u21eb\u21cb\u21ab\u218b\u216b\u214b\u212b\u210b" + + "\u20eb\u20cb\u20ab\u208b\u206b\u204b\u202b\u200b\u1feb\u1fcb\u1fab" + + "\u1f8b\u1f6b\u1f4b\u1f2b\u1f0b\u1eeb\u1ecb\u1eab\u1e8b\u1e6b\u1e4b" + + "\u1e2b\u1e0b\u1deb\u1dcb\u1dab\u1d8b\u1d6b\u1d4b\u1d2b\u1d0b\u1ceb" + + "\u1ccb\u1cab\u1c8b\u1c6b\u1c4b\u1c2b\u1c0b\u1beb\u1bcb\u1bab\u1b8b" + + "\u1b6b\u106a\u104a\u102a\u100a\u0fea\u0fca\u0faa\u0f8a\u0f6a\u0668" + + "\u08e8\u08c8\u08a8\u0888\u0868\u0848\u07d7\u194b\u07b6\u0d1c\u0cfc" + + "\u0cb2\u0ca9\u0c9c\u0c7c\u0c5c\u0c3c\u0c1c\u0bfc\u0bdc\u0bbc\u0b9c" + + "\u0b7c\u0b5e\u0b2c\u0b1c\u0ab8\u0adc\u0a9c\u02c2\u0528\u166b\u1667" + + "\u03ff\u09fc\u09dc\u09bc\u0659\u0bb8\u15a7\u0fc6\u01c0\u01b1\u09cb" + + "\u082c\u1285"; + + /** + * Information about each character. The low order 5 bits form the + * character type, the next bit is a flag for non-breaking spaces, and the + * next bit is a flag for mirrored directionality. The high order 9 bits + * form the offset into the attribute tables. Note that this limits the + * number of unique character attributes to 512, which is not a problem + * as of Unicode version 3.2.0, but may soon become one. + */ + String DATA + = "\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3e80\u3e80\u3001\u3082\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3a85" + + "\u3a85\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85" + + "\u3e80\u3e80\u3e80\u3e80\u5b88\u5b88\u3e80\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80" + + "\u3e80\u3e80\u3e80\u5198\u3e80\u3e80\u3e80\u3e80\u4606\u3e80\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u3a85" + + "\u3a85\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u3e80\u3e80\u3e80\u3e80\u5202\u5202\u5202\u5202" + + "\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202" + + "\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202\u5202" + + "\u5202\u5202\u5202\u5202\u5202\u2e82\u3e80\u5198\u2a14\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4686\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u1a1b\u1a1b\u3e80\u3e80\u3e80\u3e80\u4584\u3e80\u3e80" + + "\u3e80\u0298\u3e80\u0298\u6615\u6696\u0298\u1a97\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u4584\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u4584\u4584\u1a1b\u1a1b\u1a1b\u1a1b" + + "\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u4584" + + "\u4584\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b" + + "\u1a1b\u1a1b\u1a1b\u1a1b\u2e82\u7282\u2e82\u3e80\u2e82\u4902\u7481" + + "\u7481\u7481\u7481\u7383\u1a1b\u1a1b\u1a1b\u6d82\u6d82\u4902\u4902" + + "\u3e80\u3e80\u2e82\u4902\u6e01\u6e01\u7501\u7501\u3e80\u1a1b\u1a1b" + + "\u1a1b\u1b02\u1b82\u1c02\u1c82\u1d02\u1d82\u1e02\u1e82\u1f02\u1f82" + + "\u2002\u2082\u2102\u2182\u2202\u2282\u2302\u2382\u2402\u2482\u2502" + + "\u2582\u2602\u2682\u2702\u2782\u0455\u0c99\u04d6\u0c99\017\017" + + "\017\017\017\u010f\017\017\017\017\017\017\017" + + "\017\017\017\017\017\017\017\017\017\017\017" + + "\017\017\017\017\017\017\017\017\u008f\u010f\u008f" + + "\u018f\u010f\017\017\017\017\017\017\017\017\017" + + "\017\017\017\017\017\u010f\u010f\u010f\u008f\u020c\u0298" + + "\u0298\u0318\u039a\u0318\u0298\u0298\u0455\u04d6\u0298\u0519\u0598" + + "\u0614\u0598\u0698\u0709\u0789\u0809\u0889\u0909\u0989\u0a09\u0a89" + + "\u0b09\u0b89\u0598\u0298\u0c59\u0c99\u0c59\u0298\u0d01\u0d81\u0e01" + + "\u0e81\u0f01\u0f81\u1001\u1081\u1101\u1181\u1201\u1281\u1301\u1381" + + "\u1401\u1481\u1501\u1581\u1601\u1681\u1701\u1781\u1801\u1881\u1901" + + "\u1981\u0455\u0298\u04d6\u1a1b\u1a97\u0298\u0298\u0298\u0c99\u0455" + + "\u04d6\u3e80\u0298\u0298\u0298\u0298\u0298\u0298\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u282c\u0298\u039a\u039a\u039a\u039a\u289c" + + "\u289c\u1a1b\u289c\u2902\u29dd\u0c99\u2a14\u289c\u1a1b\u2a9c\u0519" + + "\u2b0b\u2b8b\u1a1b\u2c02\u289c\u0298\u1a1b\u2c8b\u2902\u2d5e\u2d8b" + + "\u2d8b\u2d8b\u0298\u0298\u0519\u0614\u0c99\u0c99\u0c99\u3e80\u0298" + + "\u039a\u0318\u0298\u3e80\u3e80\u3e80\u3e80\u5405\u5405\u5405\u3e80" + + "\u5405\u3e80\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u3e80\u3e80\u3e80\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u3e80\u501c\u501c\u4f81\u4f81" + + "\u4f81\u4f81\u4f81\u4f81\u4f81\u4f81\u4f81\u4f81\u4f81\u4f81\u4f81" + + "\u4f81\u4f81\u4f81\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01" + + "\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01" + + "\u2e01\u2e01\u2e01\u2e01\u0c99\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01" + + "\u2e01\u2e82\u2e82\u2e82\u4902\u4902\u2e82\u2e82\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u2e82\u2e82" + + "\u2e82\u2e82\u2e82\u3e80\u3e80\u3e80\u3e80\u3e80\u5305\u4606\u5305" + + "\u5305\u3e80\u5305\u5305\u3e80\u5305\u5305\u5305\u5305\u5305\u5305" + + "\u5305\u5305\u5305\u5305\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5398\u5405\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u5087\u5087\u4606\u5087\u5087\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u2d8b\u2d8b\u2d8b\u2d8b\u2d8b\u2d8b\u2d8b" + + "\u2d8b\u2d8b\u2d8b\u2d8b\u2d8b\u840b\u3082\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u2e82\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5c09" + + "\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u4606\u4606" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u1a1b\u1a1b\u4701\u0298\u4781\u4781\u4781\u3e80" + + "\u4801\u3e80\u4881\u4881\u4902\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01" + + "\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2f02" + + "\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02" + + "\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02" + + "\u0c99\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f82\u2f02\u2f02" + + "\u4a82\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u4b02" + + "\u4b82\u4b82\u3e80\u4c02\u4c82\u4d01\u4d01\u4d01\u4d82\u4e02\u2902" + + "\u3e80\u3e80\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u2e82\u3b81\u3c03\u3c82" + + "\u3001\u3082\u3d81\u3e01\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3101\u3182" + + "\u3001\u3082\u3001\u3082\u3001\u3082\u2902\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u4e82\u4f02\u3d02\u2902\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5b10\u5b10\u5b10\u5b10\u5b10" + + "\u5b10\u7f0b\u3e80\u3e80\u3e80\u7f8b\u800b\u808b\u810b\u818b\u820b" + + "\u0519\u0519\u0c99\u0455\u04d6\u2902\u3301\u3001\u3082\u3001\u3082" + + "\u3381\u3001\u3082\u3401\u3401\u3001\u3082\u2902\u3481\u3501\u3581" + + "\u3001\u3082\u3401\u3601\u3682\u3701\u3781\u3001\u3082\u2902\u2902" + + "\u3701\u3801\u2902\u3881\u3a85\u3a85\u3a85\u3a85\u3b81\u3c03\u3c82" + + "\u3b81\u3c03\u3c82\u3b81\u3c03\u3c82\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3d02\u3001\u3082\u501c\u4606\u4606\u4606\u4606\u3e80\u5087\u5087" + + "\u3e80\u3e80\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u3201\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3282\u3001\u3082\u3001\u3082\u3001\u3082\u3901\u3001\u3082\u3901" + + "\u2902\u2902\u3001\u3082\u3901\u3001\u3082\u3981\u3981\u3001\u3082" + + "\u3001\u3082\u3a01\u3001\u3082\u2902\u3a85\u3001\u3082\u2902\u3b02" + + "\u4d01\u3001\u3082\u3001\u3082\u3e80\u3e80\u3001\u3082\u3e80\u3e80" + + "\u3001\u3082\u3e80\u3e80\u3e80\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001" + + "\u3082\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u0598\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5398\u3e80\u3e80\u3e80\u5398" + + "\u5398\u5398\u5398\u5398\u5398\u5398\u5398\u5398\u5398\u5398\u5398" + + "\u5398\u5398\u3e80\u5b10\u5405\u4606\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u3e80\u3e80\u5b10\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01" + + "\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01" + + "\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01\u4d01" + + "\u4d01\u4d01\u4d01\u4d01\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89" + + "\u5f09\u5f89\u6009\u6089\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u2902\u2902\u2902\u3f02\u3f82\u2902\u4002" + + "\u4002\u2902\u4082\u2902\u4102\u2902\u2902\u2902\u2902\u4002\u2902" + + "\u2902\u4182\u2902\u2902\u2902\u2902\u4202\u4282\u2902\u2902\u2902" + + "\u2902\u2902\u4282\u2902\u2902\u4302\u2902\u2902\u4382\u2902\u2902" + + "\u2902\u2902\u2902\u2902\u2902\u2902\u2902\u2902\u4402\u2902\u2902" + + "\u4402\u2902\u2902\u2902\u2902\u4402\u2902\u4482\u4482\u2902\u2902" + + "\u2902\u2902\u2902\u2902\u4502\u2902\u2902\u2902\u2902\u2902\u2902" + + "\u2902\u2902\u2902\u2902\u2902\u2902\u2902\u2902\u3e80\u3e80\u4584" + + "\u4584\u4584\u4584\u4584\u4584\u4584\u4584\u4584\u1a1b\u1a1b\u4584" + + "\u4584\u4584\u4584\u4584\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b\u1a1b" + + "\u1a1b\u1a1b\u4584\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5101\u5101" + + "\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101" + + "\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u5101\u3e80" + + "\u3e80\u4584\u5198\u5198\u5198\u5198\u5198\u5198\u2e01\u2e01\u3e80" + + "\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u2e01\u4982\u4a02" + + "\u4a02\u4a02\u4902\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02" + + "\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u2f02\u4f02\u4f02\u4f02" + + "\u4f02\u4f02\u4f02\u4f02\u4f02\u4f02\u4f02\u4f02\u4f02\u4f02\u4f02" + + "\u4f02\u4f02\u4606\u4606\u4606\u4606\u4606\u5198\u4606\u4606\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u3e80\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u3e80\u4606\u4606\u4606\u5298" + + "\u4606\u4606\u5298\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u5305\u5305\u5305\u5305\u5305\u5305\u5305" + + "\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u5305\u5305\u5305\u5298\u5298\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5c89\u5d09\u5d89" + + "\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u640b\u648b\u650b\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u4606\u5b88\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80" + + "\u3e80\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80" + + "\u3e80\u3a85\u3a85\u3e80\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3e80" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u5b88\u5b88\u5b88\u5b88\u3e80\u4606\u4606\u4606\u3e80\u4606" + + "\u4606\u4606\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u4606" + + "\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u4606" + + "\u5b88\u5b88\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3e80\u3e80\u3a85\u3a85\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3e80\u3e80\u3e80\u3e80\u5b88\u5b88\u3e80\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3a85\u3a85\u3a85" + + "\u3e80\u3e80\u3e80\u3e80\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09" + + "\u5f89\u6009\u6089\u501c\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5509\u5589\u5609" + + "\u5689\u5709\u5789\u5809\u5889\u5909\u5989\u0318\u5a18\u5a18\u5398" + + "\u3e80\u3e80\u4606\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u3e80\u3e80\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u6615\u6696\u5484\u5405\u5405\u5405\u5405" + + "\u5405\u5405\u5405\u5405\u5405\u5405\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u5b88\u5b88\u5198\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u4606\u4606\u5b88" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3e80" + + "\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u5198\u5198\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u4606\u4606\u4606\u4606\u4606\u5484\u5484\u4606" + + "\u4606\u289c\u4606\u4606\u4606\u4606\u3e80\u3e80\u0709\u0789\u0809" + + "\u0889\u0909\u0989\u0a09\u0a89\u0b09\u0b89\u5405\u5405\u5405\u5a9c" + + "\u5a9c\u3e80\u3a85\u3a85\u3a85\u3e80\u3a85\u3e80\u3a85\u3e80\u3e80" + + "\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u4606\u3a85\u3a85\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u3e80\u4606\u4606\u3a85\u3e80\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u4606\u4606\u5b88" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3e80\u3e80\u4606\u3a85\u5b88\u5b88\u5b88\u5b88\u5b88\u3e80\u4606" + + "\u5b88\u5b88\u3e80\u5b88\u5b88\u4606\u4606\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u5b88\u5b88\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3a85\u3e80\u5198\u5198\u5198\u5198\u5198\u5198\u5198\u5198" + + "\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u640b\u670b" + + "\u678b\u680b\u688b\u690b\u698b\u6a0b\u6a8b\u648b\u6b0b\u3e80\u3e80" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3e80" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u4606\u3a85\u5b88\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u5b88\u5b88\u5b88\u5b88" + + "\u4606\u3e80\u3e80\u3a85\u4606\u4606\u4606\u4606\u3e80\u3e80\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3e80\u3e80\u3e80" + + "\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u4606\u3e80\u5b88\u5b88\u5b88" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85" + + "\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3e80\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u4606\u3a85\u3a85\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u3e80\u3e80\u3e80\u3e80\u039a\u039a\u039a" + + "\u039a\u039a\u039a\u039a\u039a\u039a\u039a\u039a\u039a\u039a\u039a" + + "\u039a\u039a\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85\u4606\u4606" + + "\u5198\u5198\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009" + + "\u6089\u5198\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u0298\u0298\u0318\u039a\u0318" + + "\u0298\u0298\u6615\u6696\u0298\u0519\u0598\u0614\u0598\u0698\u0709" + + "\u0789\u0809\u0889\u0909\u0989\u0a09\u0a89\u0b09\u0b89\u0598\u0298" + + "\u0c99\u0c99\u0c99\u0298\u0298\u0298\u0298\u0298\u0298\u2a14\u0298" + + "\u0298\u0298\u0298\u5b10\u5b10\u5b10\u5b10\u3e80\u5c09\u5c89\u5d09" + + "\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u5b88" + + "\u4606\u4606\u4606\u4606\u3e80\u3e80\u5b88\u5b88\u3e80\u3e80\u5b88" + + "\u5b88\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u5b88\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85\u3e80\u3a85\u3e80\u3e80" + + "\u3a85\u3a85\u3e80\u3a85\u3e80\u3e80\u3a85\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3e80\u3a85\u3a85\u3e80\u3a85\u3a85\u3e80\u3a85\u3a85" + + "\u3e80\u3e80\u4606\u3e80\u5b88\u5b88\u4606\u4606\u3e80\u3e80\u3e80" + + "\u3e80\u4606\u4606\u3e80\u3e80\u4606\u4606\u4606\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85" + + "\u3a85\u3e80\u3a85\u3e80\u3a85\u3a85\u4606\u4606\u3e80\u3e80\u5c09" + + "\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u3a85\u3a85" + + "\u039a\u039a\u610b\u618b\u620b\u628b\u630b\u638b\u501c\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3a85" + + "\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u4606\u3a85" + + "\u5b88\u5b88\u4606\u4606\u4606\u4606\u4606\u3e80\u4606\u4606\u5b88" + + "\u3e80\u5b88\u5b88\u4606\u3e80\u3e80\u3a85\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u630b" + + "\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u501c\u4606" + + "\u501c\u4606\u501c\u4606\u6615\u6696\u6615\u6696\u5b88\u5b88\u4606" + + "\u4606\u4606\u3e80\u3e80\u3e80\u5b88\u5b88\u3e80\u3e80\u5b88\u5b88" + + "\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u4606\u5b88" + + "\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3e80\u3a85\u3a85\u3e80\u5b88\u4606\u4606\u4606\u4606\u5b88" + + "\u4606\u3e80\u3e80\u3e80\u4606\u4606\u5b88\u4606\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u5b88\u5b88\u5b88\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u5b88\u5b88\u3e80\u3e80\u3e80\u5b88\u5b88\u5b88\u3e80\u5b88\u5b88" + + "\u5b88\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u5b88\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u4584\u3e80\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u3e80\u3e80\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89" + + "\u6009\u6089\u3e80\u3e80\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u5c09" + + "\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u5087\u5087\u5087\u5b88\u4606\u4606" + + "\u4606\u3e80\u3e80\u5b88\u5b88\u5b88\u3e80\u5b88\u5b88\u5b88\u4606" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u5b88\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u4606\u3e80\u3e80\u3e80\u3e80" + + "\u5b88\u5b88\u5b88\u4606\u4606\u4606\u3e80\u4606\u3e80\u5b88\u5b88" + + "\u5b88\u5b88\u5b88\u5b88\u5b88\u5b88\u4606\u5b88\u5b88\u4606\u4606" + + "\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u4606\u5198\u5198" + + "\u5198\u5198\u5198\u5198\u5198\u039a\u5198\u3e80\u3e80\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u4584\u4606\u4606\u4606\u4606\u4606" + + "\u4606\u4606\u4606\u5198\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09" + + "\u5f89\u6009\u6089\u5198\u5198\u3e80\u3e80\u3e80\u3e80\u3a85\u501c" + + "\u501c\u501c\u5198\u5198\u5198\u5198\u5198\u5198\u5198\u5198\u65b8" + + "\u5198\u5198\u5198\u5198\u5198\u5198\u501c\u501c\u501c\u501c\u501c" + + "\u4606\u4606\u501c\u501c\u501c\u501c\u501c\u501c\u4606\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u3e80\u3e80\u501c\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u1a97\u4584\u4584\u4584" + + "\u3e80\u5c09\u5c89\u5d09\u5d89\u5e09\u5e89\u5f09\u5f89\u6009\u6089" + + "\u5198\u5198\u5198\u5198\u5198\u5198\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u5b88\u5b88\u4606\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u020c\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u6615\u6696\u3e80\u3e80\u3e80\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u5198" + + "\u5198\u5198\u6b8b\u6c0b\u6c8b\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u4606\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3001\u3082\u3001" + + "\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082" + + "\u3001\u3082\u3001\u3082\u3001\u3082\u3001\u3082\u2e82\u2e82\u2e82" + + "\u2e82\u2e82\u6d02\u3e80\u3e80\u3e80\u3e80\u6d82\u6d82\u6d82\u6d82" + + "\u6d82\u6d82\u6d82\u6d82\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01" + + "\u6e01\u6d82\u6d82\u6d82\u6d82\u6d82\u6d82\u6d82\u6d82\u6e01\u6e01" + + "\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01\u6d82\u6d82\u6d82\u6d82\u6d82" + + "\u6d82\u3e80\u3e80\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01\u3e80\u3e80" + + "\u2e82\u6d82\u4902\u6d82\u4902\u6d82\u4902\u6d82\u3e80\u6e01\u3e80" + + "\u6e01\u3e80\u6e01\u3e80\u6e01\u6d82\u6d82\u6d82\u6d82\u6d82\u6d82" + + "\u6d82\u6d82\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01\u6e01\u6e82" + + "\u6e82\u6f02\u6f02\u6f02\u6f02\u6f82\u6f82\u7002\u7002\u7082\u7082" + + "\u7102\u7102\u3e80\u3e80\u7182\u7182\u7182\u7182\u7182\u7182\u7182" + + "\u7182\u7203\u7203\u7203\u7203\u7203\u7203\u7203\u7203\u7182\u7182" + + "\u7182\u7182\u7182\u7182\u7182\u7182\u7203\u7203\u7203\u7203\u7203" + + "\u7203\u7203\u7203\u6d82\u6d82\u2e82\u7282\u2e82\u3e80\u2e82\u4902" + + "\u6e01\u6e01\u7301\u7301\u7383\u1a1b\u7402\u1a1b\u1b02\u1b82\u1c02" + + "\u1c82\u1d02\u1d82\u1e02\u1e82\u1f02\u1f82\u2002\u2082\u2102\u2182" + + "\u2202\u2282\u2302\u2382\u2402\u2482\u2502\u2582\u2602\u2682\u2702" + + "\u2782\u6615\u0c99\u6696\u0c99\u3e80\u6d82\u6d82\u4902\u4902\u2e82" + + "\u7582\u2e82\u4902\u6e01\u6e01\u7601\u7601\u7681\u1a1b\u1a1b\u1a1b" + + "\u3e80\u3e80\u2e82\u7282\u2e82\u3e80\u2e82\u4902\u7701\u7701\u7781" + + "\u7781\u7383\u1a1b\u1a1b\u3e80\u020c\u020c\u020c\u020c\u020c\u020c" + + "\u020c\u782c\u020c\u020c\u020c\u788c\u5b10\u5b10\u7910\u7990\u2a14" + + "\u7a34\u2a14\u2a14\u2a14\u2a14\u0298\u0298\u7a9d\u7b1e\u6615\u7a9d" + + "\u7a9d\u7b1e\u6615\u7a9d\u0298\u0298\u0298\u0298\u0298\u0298\u0298" + + "\u0298\u7b8d\u7c0e\u7c90\u7d10\u7d90\u7e10\u7e90\u782c\u0318\u0318" + + "\u0318\u0318\u0318\u0298\u0298\u0298\u0298\u29dd\u2d5e\u0298\u0298" + + "\u0298\u0298\u1a97\u7f0b\u2c8b\u2b0b\u2b8b\u7f8b\u800b\u808b\u810b" + + "\u818b\u820b\u0519\u0519\u0c99\u0455\u04d6\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u3e80\u3e80\u3e80\u3e80\u3e80\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u4d01\u289c\u289c\u289c\u289c" + + "\u4d01\u289c\u289c\u2902\u4d01\u4d01\u4d01\u2902\u2902\u4d01\u4d01" + + "\u4d01\u2902\u289c\u4d01\u289c\u289c\u289c\u4d01\u4d01\u4d01\u4d01" + + "\u4d01\u289c\u289c\ua20a\ua28a\ua30a\ua38a\ua40a\ua48a\ua50a\ua58a" + + "\ua60a\u4606\u4606\u4606\u4606\u4606\u4606\u2a14\u4584\u4584\u4584" + + "\u4584\u4584\u289c\u289c\ua68a\ua70a\ua78a\u3e80\u3e80\u3e80\u289c" + + "\u289c\u289c\u289c\u3e80\u289c\u289c\u289c\u289c\u3e80\u3e80\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u0c99\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u0c99\u0c99\u289c\u289c\u0c99\u289c\u0c99" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u0c99\u289c\u289c\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u948a" + + "\u950a\u958a\u960a\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u0c99\u0c99\u0c99\u0c99\u0c99\u289c\u289c" + + "\u289c\u289c\u289c\u0c99\u0c99\u289c\u289c\u289c\u289c\u4d01\u289c" + + "\u8281\u289c\u4d01\u289c\u8301\u8381\u4d01\u4d01\u2a9c\u2902\u4d01" + + "\u4d01\u289c\u4d01\u2902\u3a85\u3a85\u3a85\u3a85\u2902\u289c\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u848a\u850a\u858a\u860a\u868a\u870a\u878a" + + "\u880a\u888a\u890a\u898a\u8a0a\u8a8a\u8b0a\u8b8a\u8c0a\u8c8a\u8d0a" + + "\u8d8a\u8e0a\u8e8a\u8f0a\u8f8a\u900a\u908a\u910a\u918a\u920a\u928a" + + "\u930a\u938a\u940a\u0c99\u0c99\u0c59\u0c59\u0c99\u0c99\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c99\u0c99\u0c99" + + "\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c99\u0c99\u0c99\u0c99\u0c99" + + "\u0c99\u0c99\u0c59\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c59\u0c59" + + "\u0c99\u0c99\u0c99\u0c99\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c59\u0c59\u0c59" + + "\u0c59\u0c99\u0c99\u0c99\u0c99\u0c99\u0c59\u0c99\u0c99\u0c99\u0c99" + + "\u0c99\u0c99\u0c99\u289c\u289c\u0c99\u289c\u289c\u0c99\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u0c99\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u3e80\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u0c99\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c99\u0c59\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c99\u0c99\u0c99\u0c59\u0519\u0519\u0c99\u0c59" + + "\u0c59\u0c99\u0c99\u0c99\u0c59\u0c59\u0c59\u0c59\u0c99\u0c59\u0c99" + + "\u0c59\u0c99\u0c99\u0c99\u0c99\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c99\u0c99\u0c99\u0c99\u0c99\u0c59\u0c99\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u0455\u04d6\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u3e80\u3e80\u3e80\u3e80\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u9c1c\u9c1c\u9c1c\u9c1c" + + "\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c\u9c1c" + + "\u9c1c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c" + + "\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u9c9c\u7f0b\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u0c59\u0c99\u0c59\u0c99\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59\u0c59" + + "\u0c59\u0c59\u0c59\u0c59\u0c59\u0c99\u0c99\u0c59\u0c59\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u039a\u039a\u0c99\u1a1b\u289c\u039a\u039a\u3e80\u289c\u0c99" + + "\u0c99\u0c99\u0c99\u289c\u289c\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u5b10\u5b10\u5b10\u289c\u289c\u3e80\u3e80" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u3e80\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u3e80\u289c" + + "\u3e80\u289c\u289c\u289c\u289c\u3e80\u3e80\u3e80\u289c\u3e80\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u840b\u9d0b" + + "\u9d8b\u9e0b\u9e8b\u9f0b\u9f8b\ua00b\ua08b\ua10b\u840b\u9d0b\u9d8b" + + "\u9e0b\u9e8b\u9f0b\u9f8b\ua00b\ua08b\ua10b\u289c\u3e80\u3e80\u3e80" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u0c59\u0c59\u0c59" + + "\u0c59\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c\u289c" + + "\u501c\u289c\u289c\u289c\u289c\u289c\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u630b\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u3e80\u3e80" + + "\u3e80\u501c\u610b\u618b\u620b\u628b\ua80b\ua88b\ua90b\ua98b\uaa0b" + + "\u640b\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u289c\u3e80\u289c\u289c\u289c" + + "\u3e80\u289c\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u2c8b\u2b0b\u2b8b\u7f8b\u800b\u808b" + + "\u810b\u818b\u820b\u968b\u970b\u978b\u980b\u988b\u990b\u998b\u9a0b" + + "\u9a8b\u9b0b\u9b8b\u2c8b\u2b0b\u2b8b\u7f8b\u800b\u808b\u810b\u818b" + + "\u820b\u968b\u970b\u978b\u980b\u988b\u990b\u998b\u9a0b\u9a8b\u9b0b" + + "\u9b8b\u501c\u501c\u501c\u501c\u020c\u0298\u0298\u0298\u289c\u4584" + + "\u3a85\ua18a\u0455\u04d6\u0455\u04d6\u0455\u04d6\u0455\u04d6\u0455" + + "\u04d6\u289c\u289c\u0455\u04d6\u0455\u04d6\u0455\u04d6\u0455\u04d6" + + "\u2a14\u6615\u6696\u6696\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3e80\u3e80\u3e80\u3e80\u4606\u4606\u1a1b\u1a1b" + + "\u4584\u4584\u3e80\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85" + + "\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3a85\u3e80\u501c\u501c\u630b" + + "\u630b\u630b\u630b\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c" + + "\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u501c\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93" + + "\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93" + + "\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93\uaa93" + + "\uaa93\uaa93\uaa93\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12" + + "\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12" + + "\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12\uab12" + + "\uab12\uab12\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305" + + "\u0519\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305\u5305" + + "\u5305\u5305\u5305\u3e80\u5305\u5305\u5305\u5305\u5305\u3e80\u5305" + + "\u3e80\u4606\u4606\u4606\u4606\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80" + + "\u3e80\u3e80\u3e80\u3e80\u3e80\u3e80\u0298\u2a14\u2a14\u1a97\u1a97" + + "\u6615\u6696\u6615\u6696\u6615\u6696\u6615\u6696\u6615\u6696\u6615" + + "\u6696\u3e80\u3e80\u3e80\u3e80\u0298\u0298\u0298\u0298\u1a97\u1a97" + + "\u1a97\u0598\u0298\u0598\u3e80\u0298\u0598\u0298\u0298\u2a14\u6615" + + "\u6696\u6615\u6696\u6615\u6696\u0318\u0298\u0d01\u0d81\u0e01\u0e81" + + "\u0f01\u0f81\u1001\u1081\u1101\u1181\u1201\u1281\u1301\u1381\u1401" + + "\u1481\u1501\u1581\u1601\u1681\u1701\u1781\u1801\u1881\u1901\u1981" + + "\u6615\u0298\u6696\u1a1b\u1a97"; + + /** + * This is the attribute table for computing the numeric value of a + * character. The value is -1 if Unicode does not define a value, -2 + * if the value is not a positive integer, otherwise it is the value. + * Note that this is a signed value, but stored as an unsigned char + * since this is a String literal. + */ + String NUM_VALUE + = "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\000\001\002\003\004\005\006\007" + + "\010\011\uffff\uffff\012\013\014\015\016\017\020" + + "\021\022\023\024\025\026\027\030\031\032\033" + + "\034\035\036\037 !\"#\uffff\uffff\012" + + "\013\014\015\016\017\020\021\022\023\024\025" + + "\026\027\030\031\032\033\034\035\036\037 " + + "!\"#\uffff\uffff\uffff\uffff\uffff\uffff\002\003" + + "\uffff\001\uffff\ufffe\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\000\001\002\003\004\005" + + "\006\007\010\011\uffff\uffff\uffff\uffff\000\001\002" + + "\003\004\005\006\007\010\011\001\002\003\004" + + "\uffff\020\012d\u03e8\uffff\uffff\uffff\024\036(" + + "2<FPZ\u2710\021\022\023\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff\uffff" + + "\uffff\000\004\005\006\007\010\011\uffff\uffff\uffff" + + "\001\001\002\003\004\005\006\007\010\011\012" + + "\013\0142d\u01f4\u03e8\001\002\003\004\005" + + "\006\007\010\011\012\013\0142d\u01f4\u03e8" + + "\u03e8\u1388\u2710\uffff\012\013\014\015\016\017\020" + + "\021\022\023\024\uffff\uffff\002\003\004\005\006" + + "\007\010\011\012\000\001\002\003\004\005\006" + + "\007\010\011\012\024\036\005\006\007\010\011" + + "\uffff\uffff"; + + /** + * This is the attribute table for computing the single-character uppercase + * representation of a character. The value is the signed difference + * between the character and its uppercase version. Note that this is + * stored as an unsigned char since this is a String literal. When + * capitalizing a String, you must first check if a multi-character uppercase + * sequence exists before using this character. + */ + String UPPER + = "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\uffe0" + + "\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0" + + "\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0\uffe0" + + "\uffe0\uffe0\uffe0\000\000\000\000\000\000\000\000" + + "\u02e7\000\000\000\000\000\uffe0y\000\uffff\000" + + "\uff18\000\ufed4\000\000\000\000\000\000\000a" + + "\000\000\000\000\000\000\000\0008\000\uffff" + + "\ufffe\uffb1\000\000\000\uff2e\uff32\uff33\uff36\uff35\uff31" + + "\uff2f\uff2d\uff2b\uff2a\uff26\uff27\uff25\000\000T\000" + + "\000\000\000\000\uffda\uffdb\uffe1\uffc0\uffc1\uffc2\uffc7" + + "\000\uffd1\uffca\uffaa\uffb0\000\000\000\000\000\uffd0" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\uffc5\010" + + "\000JVd\u0080p~\010\000\011\000" + + "\000\ue3db\000\000\007\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\ufff0\ufff0\ufff0\ufff0\ufff0" + + "\ufff0\ufff0\ufff0\ufff0\ufff0\ufff0\ufff0\ufff0\ufff0\ufff0\ufff0" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\uffe6\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000"; + + /** + * This is the attribute table for computing the lowercase representation + * of a character. The value is the signed difference between the + * character and its lowercase version. Note that this is stored as an + * unsigned char since this is a String literal. + */ + String LOWER + = "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + " " + + " " + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000 \000\000\000\001\000\uff39\000\uff87\000\u00d2\u00ce" + + "\u00cdO\u00ca\u00cb\u00cf\000\u00d3\u00d1\u00d5\u00d6\u00da\u00d9\u00db" + + "\000\000\002\001\000\000\uff9f\uffc8\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000&" + + "%@?\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000P\000\0000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\ufff8" + + "\000\000\000\000\000\000\000\ufff8\000\uffb6\ufff7\000\uffaa" + + "\uff9c\000\uff90\ufff9\uff80\uff82\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\ue2a3\udf41\udfba\000\020\020\020\020\020\020\020\020" + + "\020\020\020\020\020\020\020\020\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\032\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000"; + + /** + * This is the attribute table for computing the directionality class + * of a character, as well as a marker of characters with a multi-character + * capitalization. The direction is taken by performing a signed shift + * right by 2 (where a result of -1 means an unknown direction, such as + * for undefined characters). The lower 2 bits form a count of the + * additional characters that will be added to a String when performing + * multi-character uppercase expansion. This count is also used, along with + * the offset in UPPER_SPECIAL, to determine how much of UPPER_EXPAND to use + * when performing the case conversion. Note that this information is stored + * as an unsigned char since this is a String literal. + */ + String DIRECTION + = "$,(004\024\02444\024\034\024\020\014\014\014" + + "\014\014\014\014\014\014\01444\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\00044\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\0344\00044" + + "\024\014\014\000\01444\000\001\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\ufffc\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000 \000\000\000\000\002\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000 \000\000\000\004\004\010\010\010" + + "\030\030\030\030\030\030\030\030\030\030\030\010$\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "44\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\001\001\001\000\001\000\000\000\000\000\000" + + "\000\0000$\000\0044440(8@H<D\014" + + "\014\014\014\014\014\014\000\000\0004\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000\000\000\000\000\000\000\000\000\000\014\014\014\014\014" + + "\014\014\014\014\014\014\000\000444444444" + + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + + "\000\000\000"; + + /** + * This is the listing of titlecase special cases (all other characters + * can use <code>UPPER</code> to determine their titlecase). The listing + * is a sorted sequence of character pairs; converting the first character + * of the pair to titlecase produces the second character. + */ + String TITLE + = "\u01c4\u01c5\u01c5\u01c5\u01c6\u01c5\u01c7\u01c8\u01c8\u01c8\u01c9" + + "\u01c8\u01ca\u01cb\u01cb\u01cb\u01cc\u01cb\u01f1\u01f2\u01f2\u01f2" + + "\u01f3\u01f2"; + + /** + * This is a listing of characters with multi-character uppercase sequences. + * A character appears in this list exactly when it has a non-zero entry + * in the low-order 2-bit field of DIRECTION. The listing is a sorted + * sequence of pairs (hence a binary search on the even elements is an + * efficient way to lookup a character). The first element of a pair is the + * character with the expansion, and the second is the index into + * UPPER_EXPAND where the expansion begins. Use the 2-bit field of + * DIRECTION to determine where the expansion ends. + */ + String UPPER_SPECIAL + = "\u00df\000\u0149\002\u01f0\004\u0390\006\u03b0\011" + + "\u0587\014\u1e96\016\u1e97\020\u1e98\022\u1e99\024" + + "\u1e9a\026\u1f50\030\u1f52\032\u1f54\035\u1f56 " + + "\u1f80#\u1f81%\u1f82'\u1f83)\u1f84+" + + "\u1f85-\u1f86/\u1f871\u1f883\u1f895" + + "\u1f8a7\u1f8b9\u1f8c;\u1f8d=\u1f8e?" + + "\u1f8fA\u1f90C\u1f91E\u1f92G\u1f93I" + + "\u1f94K\u1f95M\u1f96O\u1f97Q\u1f98S" + + "\u1f99U\u1f9aW\u1f9bY\u1f9c[\u1f9d]" + + "\u1f9e_\u1f9fa\u1fa0c\u1fa1e\u1fa2g" + + "\u1fa3i\u1fa4k\u1fa5m\u1fa6o\u1fa7q" + + "\u1fa8s\u1fa9u\u1faaw\u1faby\u1fac{" + + "\u1fad}\u1fae\u007f\u1faf\u0081\u1fb2\u0083\u1fb3\u0085" + + "\u1fb4\u0087\u1fb6\u0089\u1fb7\u008b\u1fbc\u008e\u1fc2\u0090" + + "\u1fc3\u0092\u1fc4\u0094\u1fc6\u0096\u1fc7\u0098\u1fcc\u009b" + + "\u1fd2\u009d\u1fd3\u00a0\u1fd6\u00a3\u1fd7\u00a5\u1fe2\u00a8" + + "\u1fe3\u00ab\u1fe4\u00ae\u1fe6\u00b0\u1fe7\u00b2\u1ff2\u00b5" + + "\u1ff3\u00b7\u1ff4\u00b9\u1ff6\u00bb\u1ff7\u00bd\u1ffc\u00c0" + + "\ufb00\u00c2\ufb01\u00c4\ufb02\u00c6\ufb03\u00c8\ufb04\u00cb" + + "\ufb05\u00ce\ufb06\u00d0\ufb13\u00d2\ufb14\u00d4\ufb15\u00d6" + + "\ufb16\u00d8\ufb17\u00da"; + + /** + * This is the listing of special case multi-character uppercase sequences. + * Characters listed in UPPER_SPECIAL index into this table to find their + * uppercase expansion. Remember that you must also perform special-casing + * on two single-character sequences in the Turkish locale, which are not + * covered here in CharData. + */ + String UPPER_EXPAND + = "SS\u02bcNJ\u030c\u0399\u0308\u0301\u03a5\u0308" + + "\u0301\u0535\u0552H\u0331T\u0308W\u030aY\u030a" + + "A\u02be\u03a5\u0313\u03a5\u0313\u0300\u03a5\u0313\u0301\u03a5" + + "\u0313\u0342\u1f08\u0399\u1f09\u0399\u1f0a\u0399\u1f0b\u0399\u1f0c" + + "\u0399\u1f0d\u0399\u1f0e\u0399\u1f0f\u0399\u1f08\u0399\u1f09\u0399" + + "\u1f0a\u0399\u1f0b\u0399\u1f0c\u0399\u1f0d\u0399\u1f0e\u0399\u1f0f" + + "\u0399\u1f28\u0399\u1f29\u0399\u1f2a\u0399\u1f2b\u0399\u1f2c\u0399" + + "\u1f2d\u0399\u1f2e\u0399\u1f2f\u0399\u1f28\u0399\u1f29\u0399\u1f2a" + + "\u0399\u1f2b\u0399\u1f2c\u0399\u1f2d\u0399\u1f2e\u0399\u1f2f\u0399" + + "\u1f68\u0399\u1f69\u0399\u1f6a\u0399\u1f6b\u0399\u1f6c\u0399\u1f6d" + + "\u0399\u1f6e\u0399\u1f6f\u0399\u1f68\u0399\u1f69\u0399\u1f6a\u0399" + + "\u1f6b\u0399\u1f6c\u0399\u1f6d\u0399\u1f6e\u0399\u1f6f\u0399\u1fba" + + "\u0399\u0391\u0399\u0386\u0399\u0391\u0342\u0391\u0342\u0399\u0391" + + "\u0399\u1fca\u0399\u0397\u0399\u0389\u0399\u0397\u0342\u0397\u0342" + + "\u0399\u0397\u0399\u0399\u0308\u0300\u0399\u0308\u0301\u0399\u0342" + + "\u0399\u0308\u0342\u03a5\u0308\u0300\u03a5\u0308\u0301\u03a1\u0313" + + "\u03a5\u0342\u03a5\u0308\u0342\u1ffa\u0399\u03a9\u0399\u038f\u0399" + + "\u03a9\u0342\u03a9\u0342\u0399\u03a9\u0399FFFI" + + "FLFFIFFLSTS" + + "T\u0544\u0546\u0544\u0535\u0544\u053b\u054e\u0546\u0544\u053d"; +} diff --git a/libjava/classpath/gnu/java/lang/ClassHelper.java b/libjava/classpath/gnu/java/lang/ClassHelper.java new file mode 100644 index 00000000000..14c8a39c478 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/ClassHelper.java @@ -0,0 +1,177 @@ +/* ClassHelper.java -- Utility methods to augment java.lang.Class + Copyright (C) 1998, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.lang; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * ClassHelper has various methods that ought to have been in Class. + * + * @author John Keiser + * @author Eric Blake (ebb9@email.byu.edu) + */ +public class ClassHelper +{ + /** + * Strip the package part from the class name. + * + * @param clazz the class to get the truncated name from + * @return the truncated class name + */ + public static String getTruncatedClassName(Class clazz) + { + return getTruncatedName(clazz.getName()); + } + + /** + * Strip the package part from the class name, or the class part from + * the method or field name. + * + * @param name the name to truncate + * @return the truncated name + */ + public static String getTruncatedName(String name) + { + int lastInd = name.lastIndexOf('.'); + if (lastInd == -1) + return name; + return name.substring(lastInd + 1); + } + + /** Cache of methods found in getAllMethods(). */ + private static Map allMethods = new HashMap(); + + /** + * Get all the methods, public, private and otherwise, from the class, + * getting them from the most recent class to find them. This may not + * be quite the correct approach, as this includes methods that are not + * inherited or accessible from clazz, so beware. + * + * @param clazz the class to start at + * @return all methods declared or inherited in clazz + */ + public static Method[] getAllMethods(Class clazz) + { + Method[] retval = (Method[]) allMethods.get(clazz); + if (retval == null) + { + Set methods = new HashSet(); + Class c = clazz; + while (c != null) + { + Method[] currentMethods = c.getDeclaredMethods(); + loop: + for (int i = 0; i < currentMethods.length; i++) + { + Method current = currentMethods[i]; + int size = methods.size(); + Iterator iter = methods.iterator(); + while (--size >= 0) + { + Method override = (Method) iter.next(); + if (current.getName().equals(override.getName()) + && Arrays.equals(current.getParameterTypes(), + override.getParameterTypes()) + && current.getReturnType() == override.getReturnType()) + continue loop; + } + methods.add(current); + } + c = c.getSuperclass(); + } + retval = new Method[methods.size()]; + methods.toArray(retval); + allMethods.put(clazz, retval); + } + return retval; + } + + /** Cache of fields found in getAllFields(). */ + private static Map allFields = new HashMap(); + + /** + * Get all the fields, public, private and otherwise, from the class, + * getting them from the most recent class to find them. This may not + * be quite the correct approach, as this includes fields that are not + * inherited or accessible from clazz, so beware. + * + * @param clazz the class to start at + * @return all fields declared or inherited in clazz + */ + public static Field[] getAllFields(Class clazz) + { + Field[] retval = (Field[]) allFields.get(clazz); + if (retval == null) + { + Set fields = new HashSet(); + Class c = clazz; + while (c != null) + { + Field[] currentFields = c.getDeclaredFields(); + loop: + for (int i = 0; i < currentFields.length; i++) + { + Field current = currentFields[i]; + int size = fields.size(); + Iterator iter = fields.iterator(); + while (--size >= 0) + { + Field override = (Field) iter.next(); + if (current.getName().equals(override.getName()) + && current.getType() == override.getType()) + continue loop; + } + fields.add(current); + } + c = c.getSuperclass(); + } + retval = new Field[fields.size()]; + fields.toArray(retval); + allFields.put(clazz, retval); + } + return retval; + } +} diff --git a/libjava/classpath/gnu/java/lang/MainThread.java b/libjava/classpath/gnu/java/lang/MainThread.java new file mode 100644 index 00000000000..ab529efd1a3 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/MainThread.java @@ -0,0 +1,83 @@ +/* MainThread.java -- + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.lang; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * MainThread is a Thread which uses the main() method of some class. + * + * @author John Keiser + * @author Tom Tromey (tromey@redhat.com) + */ +public class MainThread +{ + // Private data. + String[] args; + Method mainMethod; + + public MainThread(String classname, String[] args) + throws ClassNotFoundException, NoSuchMethodException + { + Class found = Class.forName(classname, true, + ClassLoader.getSystemClassLoader()); + Class[] argTypes = new Class[1]; + argTypes[0] = args.getClass(); + mainMethod = found.getMethod("main", argTypes); + this.args = args; + } + + public void run() + { + try + { + mainMethod.invoke(null,args); + } + catch(IllegalAccessException e) + { + // Ignore. + } + catch(InvocationTargetException e) + { + // Ignore. + } + } +} diff --git a/libjava/classpath/gnu/java/lang/package.html b/libjava/classpath/gnu/java/lang/package.html new file mode 100644 index 00000000000..1f16776b655 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.lang package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.lang</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/lang/reflect/TypeSignature.java b/libjava/classpath/gnu/java/lang/reflect/TypeSignature.java new file mode 100644 index 00000000000..842e5bdf0f0 --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/TypeSignature.java @@ -0,0 +1,288 @@ +/* TypeSignature.java -- Class used to compute type signatures + Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.lang.reflect; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; + +/** + * This class provides static methods that can be used to compute + * type-signatures of <code>Class</code>s or <code>Member</code>s. + * More specific methods are also provided for computing the + * type-signature of <code>Constructor</code>s and + * <code>Method</code>s. Methods are also provided to go in the + * reverse direction. + * + * @author Eric Blake (ebb9@email.byu.edu) + */ +public class TypeSignature +{ + /** + * Returns a <code>String</code> representing the type-encoding of a class. + * The .class file format has different encodings for classes, depending + * on whether it must be disambiguated from primitive types or not; hence + * the descriptor parameter to choose between them. If you are planning + * on decoding primitive types along with classes, then descriptor should + * be true for correct results. Type-encodings are computed as follows: + * + * <pre> + * boolean -> "Z" + * byte -> "B" + * char -> "C" + * double -> "D" + * float -> "F" + * int -> "I" + * long -> "J" + * short -> "S" + * void -> "V" + * arrays -> "[" + descriptor format of component type + * object -> class format: fully qualified name with '.' replaced by '/' + * descriptor format: "L" + class format + ";" + * </pre> + * + * @param type the class name to encode + * @param descriptor true to return objects in descriptor format + * @return the class name, as it appears in bytecode constant pools + * @see #getClassForEncoding(String) + */ + public static String getEncodingOfClass(String type, boolean descriptor) + { + if (! descriptor || type.charAt(0) == '[') + return type.replace('.', '/'); + if (type.equals("boolean")) + return "Z"; + if (type.equals("byte")) + return "B"; + if (type.equals("short")) + return "S"; + if (type.equals("char")) + return "C"; + if (type.equals("int")) + return "I"; + if (type.equals("long")) + return "J"; + if (type.equals("float")) + return "F"; + if (type.equals("double")) + return "D"; + if (type.equals("void")) + return "V"; + return 'L' + type.replace('.', '/') + ';'; + } + + /** + * Gets the descriptor encoding for a class. + * + * @param clazz the class to encode + * @param descriptor true to return objects in descriptor format + * @return the class name, as it appears in bytecode constant pools + * @see #getEncodingOfClass(String, boolean) + */ + public static String getEncodingOfClass(Class clazz, boolean descriptor) + { + return getEncodingOfClass(clazz.getName(), descriptor); + } + + /** + * Gets the descriptor encoding for a class. + * + * @param clazz the class to encode + * @return the class name, as it appears in bytecode constant pools + * @see #getEncodingOfClass(String, boolean) + */ + public static String getEncodingOfClass(Class clazz) + { + return getEncodingOfClass(clazz.getName(), true); + } + + + /** + * This function is the inverse of <code>getEncodingOfClass</code>. This + * accepts both object and descriptor formats, but must know which style + * of string is being passed in (usually, descriptor should be true). In + * descriptor format, "I" is treated as int.class, in object format, it + * is treated as a class named I in the unnamed package. This method is + * strictly equivalent to {@link #getClassForEncoding(java.lang.String, boolean, java.lang.ClassLoader)} + * with a class loader equal to <code>null</code>. In that case, it + * uses the default class loader on the calling stack. + * + * @param type_code the class name to decode + * @param descriptor if the string is in descriptor format + * @return the corresponding Class object + * @throws ClassNotFoundException if the class cannot be located + * @see #getEncodingOfClass(Class, boolean) + */ + public static Class getClassForEncoding(String type_code, boolean descriptor) + throws ClassNotFoundException + { + return getClassForEncoding(type_code, descriptor, null); + } + + /** + * This function is the inverse of <code>getEncodingOfClass</code>. This + * accepts both object and descriptor formats, but must know which style + * of string is being passed in (usually, descriptor should be true). In + * descriptor format, "I" is treated as int.class, in object format, it + * is treated as a class named I in the unnamed package. + * + * @param type_code The class name to decode. + * @param descriptor If the string is in descriptor format. + * @param loader The class loader when resolving generic object name. If + * <code>loader</code> is null then it uses the default class loader on the + * calling stack. + * @return the corresponding Class object. + * @throws ClassNotFoundException if the class cannot be located. + * @see #getEncodingOfClass(Class, boolean) + * @see #getClassForEncoding(String, boolean) + */ + public static Class getClassForEncoding(String type_code, boolean descriptor, + ClassLoader loader) + throws ClassNotFoundException + { + if (descriptor) + { + switch (type_code.charAt(0)) + { + case 'B': + return byte.class; + case 'C': + return char.class; + case 'D': + return double.class; + case 'F': + return float.class; + case 'I': + return int.class; + case 'J': + return long.class; + case 'S': + return short.class; + case 'V': + return void.class; + case 'Z': + return boolean.class; + default: + throw new ClassNotFoundException("Invalid class name: " + + type_code); + case 'L': + type_code = type_code.substring(1, type_code.length() - 1); + // Fallthrough. + case '[': + } + } + return Class.forName(type_code.replace('/', '.'), true, loader); + } + + /** + * Gets the Class object for a type name. + * + * @param type_code the class name to decode + * @return the corresponding Class object + * @throws ClassNotFoundException if the class cannot be located + * @see #getClassForEncoding(String, boolean) + */ + public static Class getClassForEncoding(String type_code) + throws ClassNotFoundException + { + return getClassForEncoding(type_code, true); + } + + /** + * Returns a <code>String</code> representing the type-encoding of a + * method. The type-encoding of a method is: + * + * "(" + parameter type descriptors + ")" + return type descriptor + * + * XXX This could be faster if it were implemented natively. + * + * @param m the method to encode + * @return the encoding + */ + public static String getEncodingOfMethod(Method m) + { + Class[] paramTypes = m.getParameterTypes(); + StringBuffer buf = new StringBuffer().append('('); + for (int i = 0; i < paramTypes.length; i++) + buf.append(getEncodingOfClass(paramTypes[i].getName(), true)); + buf.append(')').append(getEncodingOfClass(m.getReturnType().getName(), + true)); + return buf.toString(); + } + + /** + * Returns a <code>String</code> representing the type-encoding of a + * constructor. The type-encoding of a method is: + * + * "(" + parameter type descriptors + ")V" + * + * XXX This could be faster if it were implemented natively. + * + * @param c the constructor to encode + * @return the encoding + */ + public static String getEncodingOfConstructor(Constructor c) + { + Class[] paramTypes = c.getParameterTypes(); + StringBuffer buf = new StringBuffer().append('('); + for (int i = 0; i < paramTypes.length; i++) + buf.append(getEncodingOfClass(paramTypes[i].getName(), true)); + buf.append(")V"); + return buf.toString(); + } + + /** + * Returns a <code>String</code> representing the type-encoding of a + * class member. This appropriately handles Constructors, Methods, and + * Fields. + * + * @param mem the member to encode + * @return the encoding + */ + public static String getEncodingOfMember(Member mem) + { + if (mem instanceof Constructor) + return getEncodingOfConstructor((Constructor) mem); + if (mem instanceof Method) + return getEncodingOfMethod((Method) mem); + else // Field + return getEncodingOfClass(((Field) mem).getType().getName(), true); + } +} // class TypeSignature diff --git a/libjava/classpath/gnu/java/lang/reflect/package.html b/libjava/classpath/gnu/java/lang/reflect/package.html new file mode 100644 index 00000000000..a837d37d26e --- /dev/null +++ b/libjava/classpath/gnu/java/lang/reflect/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.lang.reflect package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.lang.reflect</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/locale/LocaleHelper.java b/libjava/classpath/gnu/java/locale/LocaleHelper.java new file mode 100644 index 00000000000..ff00293907b --- /dev/null +++ b/libjava/classpath/gnu/java/locale/LocaleHelper.java @@ -0,0 +1,158 @@ +/* LocaleHelper.java -- helper routines for localization + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.locale; + +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * This class provides common helper methods + * for handling localized data. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * + * @see java.util.Locale + * @see java.util.ResourceBundle + */ +public class LocaleHelper +{ + /** + * This method is used by the localized name lookup methods to retrieve + * the localized name of a particular piece of locale data. + * If the display name can not be localized to the supplied + * locale, it will fall back on other output in the following order: + * + * <ul> + * <li>the localized name in the default locale</li> + * <li>the localized name in English (optional)</li> + * <li>the localized name in the root locale bundle (optional)</li> + * <li>the localized input string</li> + * </ul> + * <p> + * If the supplied key is merely the empty string, then the empty string is + * returned. + * </p> + * + * @param inLocale the locale to use for formatting the display string. + * @param key the locale data used as a key to the localized lookup tables. + * @param name the name of the hashtable containing the localized data. + * @param checkEnglish true if the method should fall back on data + * from the English locale. + * @param checkRoot true if the method should fall back on data from the + * unlocalized root locale. + * @return a <code>String</code>, hopefully containing the localized + * variant of the input data. + * @throws NullPointerException if <code>inLocale</code> is null. + */ + public static String getLocalizedString(Locale inLocale, String key, + String name, boolean checkEnglish, + boolean checkRoot) + { + String localizedString; + String property; + + if (key.equals("")) + return ""; + property = name + "." + key; + /* Localize to inLocale */ + try + { + localizedString = + ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", + inLocale).getString(property); + } + catch (MissingResourceException exception) + { + localizedString = null; + } + /* Localize to default locale */ + if (localizedString == null) + { + try + { + ResourceBundle bundle; + + bundle = + ResourceBundle.getBundle("gnu.java.locale.LocaleInformation"); + localizedString = bundle.getString(property); + } + catch (MissingResourceException exception) + { + localizedString = null; + } + } + /* Localize to English */ + if (localizedString == null && checkEnglish) + { + try + { + localizedString = + ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", + Locale.ENGLISH).getString(property); + } + catch (MissingResourceException exception) + { + localizedString = null; + } + } + /* Return unlocalized version */ + if (localizedString == null && checkRoot) + { + try + { + localizedString = + ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", + new Locale("","","") + ).getString(property); + } + catch (MissingResourceException exception) + { + localizedString = null; + } + } + /* Return original input string */ + if (localizedString == null) + { + localizedString = key; + } + return localizedString; + } +} + diff --git a/libjava/classpath/gnu/java/locale/package.html b/libjava/classpath/gnu/java/locale/package.html new file mode 100644 index 00000000000..c4bc7c3e8e5 --- /dev/null +++ b/libjava/classpath/gnu/java/locale/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.locale package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.locale</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/math/MPN.java b/libjava/classpath/gnu/java/math/MPN.java new file mode 100644 index 00000000000..05491bb7e84 --- /dev/null +++ b/libjava/classpath/gnu/java/math/MPN.java @@ -0,0 +1,771 @@ +/* gnu.java.math.MPN + Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +// Included from Kawa 1.6.62 with permission of the author, +// Per Bothner <per@bothner.com>. + +package gnu.java.math; + +/** This contains various low-level routines for unsigned bigints. + * The interfaces match the mpn interfaces in gmp, + * so it should be easy to replace them with fast native functions + * that are trivial wrappers around the mpn_ functions in gmp + * (at least on platforms that use 32-bit "limbs"). + */ + +public class MPN +{ + /** Add x[0:size-1] and y, and write the size least + * significant words of the result to dest. + * Return carry, either 0 or 1. + * All values are unsigned. + * This is basically the same as gmp's mpn_add_1. */ + public static int add_1 (int[] dest, int[] x, int size, int y) + { + long carry = (long) y & 0xffffffffL; + for (int i = 0; i < size; i++) + { + carry += ((long) x[i] & 0xffffffffL); + dest[i] = (int) carry; + carry >>= 32; + } + return (int) carry; + } + + /** Add x[0:len-1] and y[0:len-1] and write the len least + * significant words of the result to dest[0:len-1]. + * All words are treated as unsigned. + * @return the carry, either 0 or 1 + * This function is basically the same as gmp's mpn_add_n. + */ + public static int add_n (int dest[], int[] x, int[] y, int len) + { + long carry = 0; + for (int i = 0; i < len; i++) + { + carry += ((long) x[i] & 0xffffffffL) + + ((long) y[i] & 0xffffffffL); + dest[i] = (int) carry; + carry >>>= 32; + } + return (int) carry; + } + + /** Subtract Y[0:size-1] from X[0:size-1], and write + * the size least significant words of the result to dest[0:size-1]. + * Return borrow, either 0 or 1. + * This is basically the same as gmp's mpn_sub_n function. + */ + + public static int sub_n (int[] dest, int[] X, int[] Y, int size) + { + int cy = 0; + for (int i = 0; i < size; i++) + { + int y = Y[i]; + int x = X[i]; + y += cy; /* add previous carry to subtrahend */ + // Invert the high-order bit, because: (unsigned) X > (unsigned) Y + // iff: (int) (X^0x80000000) > (int) (Y^0x80000000). + cy = (y^0x80000000) < (cy^0x80000000) ? 1 : 0; + y = x - y; + cy += (y^0x80000000) > (x ^ 0x80000000) ? 1 : 0; + dest[i] = y; + } + return cy; + } + + /** Multiply x[0:len-1] by y, and write the len least + * significant words of the product to dest[0:len-1]. + * Return the most significant word of the product. + * All values are treated as if they were unsigned + * (i.e. masked with 0xffffffffL). + * OK if dest==x (not sure if this is guaranteed for mpn_mul_1). + * This function is basically the same as gmp's mpn_mul_1. + */ + + public static int mul_1 (int[] dest, int[] x, int len, int y) + { + long yword = (long) y & 0xffffffffL; + long carry = 0; + for (int j = 0; j < len; j++) + { + carry += ((long) x[j] & 0xffffffffL) * yword; + dest[j] = (int) carry; + carry >>>= 32; + } + return (int) carry; + } + + /** + * Multiply x[0:xlen-1] and y[0:ylen-1], and + * write the result to dest[0:xlen+ylen-1]. + * The destination has to have space for xlen+ylen words, + * even if the result might be one limb smaller. + * This function requires that xlen >= ylen. + * The destination must be distinct from either input operands. + * All operands are unsigned. + * This function is basically the same gmp's mpn_mul. */ + + public static void mul (int[] dest, + int[] x, int xlen, + int[] y, int ylen) + { + dest[xlen] = MPN.mul_1 (dest, x, xlen, y[0]); + + for (int i = 1; i < ylen; i++) + { + long yword = (long) y[i] & 0xffffffffL; + long carry = 0; + for (int j = 0; j < xlen; j++) + { + carry += ((long) x[j] & 0xffffffffL) * yword + + ((long) dest[i+j] & 0xffffffffL); + dest[i+j] = (int) carry; + carry >>>= 32; + } + dest[i+xlen] = (int) carry; + } + } + + /* Divide (unsigned long) N by (unsigned int) D. + * Returns (remainder << 32)+(unsigned int)(quotient). + * Assumes (unsigned int)(N>>32) < (unsigned int)D. + * Code transcribed from gmp-2.0's mpn_udiv_w_sdiv function. + */ + public static long udiv_qrnnd (long N, int D) + { + long q, r; + long a1 = N >>> 32; + long a0 = N & 0xffffffffL; + if (D >= 0) + { + if (a1 < ((D - a1 - (a0 >>> 31)) & 0xffffffffL)) + { + /* dividend, divisor, and quotient are nonnegative */ + q = N / D; + r = N % D; + } + else + { + /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */ + long c = N - ((long) D << 31); + /* Divide (c1*2^32 + c0) by d */ + q = c / D; + r = c % D; + /* Add 2^31 to quotient */ + q += 1 << 31; + } + } + else + { + long b1 = D >>> 1; /* d/2, between 2^30 and 2^31 - 1 */ + //long c1 = (a1 >> 1); /* A/2 */ + //int c0 = (a1 << 31) + (a0 >> 1); + long c = N >>> 1; + if (a1 < b1 || (a1 >> 1) < b1) + { + if (a1 < b1) + { + q = c / b1; + r = c % b1; + } + else /* c1 < b1, so 2^31 <= (A/2)/b1 < 2^32 */ + { + c = ~(c - (b1 << 32)); + q = c / b1; /* (A/2) / (d/2) */ + r = c % b1; + q = (~q) & 0xffffffffL; /* (A/2)/b1 */ + r = (b1 - 1) - r; /* r < b1 => new r >= 0 */ + } + r = 2 * r + (a0 & 1); + if ((D & 1) != 0) + { + if (r >= q) { + r = r - q; + } else if (q - r <= ((long) D & 0xffffffffL)) { + r = r - q + D; + q -= 1; + } else { + r = r - q + D + D; + q -= 2; + } + } + } + else /* Implies c1 = b1 */ + { /* Hence a1 = d - 1 = 2*b1 - 1 */ + if (a0 >= ((long)(-D) & 0xffffffffL)) + { + q = -1; + r = a0 + D; + } + else + { + q = -2; + r = a0 + D + D; + } + } + } + + return (r << 32) | (q & 0xFFFFFFFFl); + } + + /** Divide divident[0:len-1] by (unsigned int)divisor. + * Write result into quotient[0:len-1. + * Return the one-word (unsigned) remainder. + * OK for quotient==dividend. + */ + + public static int divmod_1 (int[] quotient, int[] dividend, + int len, int divisor) + { + int i = len - 1; + long r = dividend[i]; + if ((r & 0xffffffffL) >= ((long)divisor & 0xffffffffL)) + r = 0; + else + { + quotient[i--] = 0; + r <<= 32; + } + + for (; i >= 0; i--) + { + int n0 = dividend[i]; + r = (r & ~0xffffffffL) | (n0 & 0xffffffffL); + r = udiv_qrnnd (r, divisor); + quotient[i] = (int) r; + } + return (int)(r >> 32); + } + + /* Subtract x[0:len-1]*y from dest[offset:offset+len-1]. + * All values are treated as if unsigned. + * @return the most significant word of + * the product, minus borrow-out from the subtraction. + */ + public static int submul_1 (int[] dest, int offset, int[] x, int len, int y) + { + long yl = (long) y & 0xffffffffL; + int carry = 0; + int j = 0; + do + { + long prod = ((long) x[j] & 0xffffffffL) * yl; + int prod_low = (int) prod; + int prod_high = (int) (prod >> 32); + prod_low += carry; + // Invert the high-order bit, because: (unsigned) X > (unsigned) Y + // iff: (int) (X^0x80000000) > (int) (Y^0x80000000). + carry = ((prod_low ^ 0x80000000) < (carry ^ 0x80000000) ? 1 : 0) + + prod_high; + int x_j = dest[offset+j]; + prod_low = x_j - prod_low; + if ((prod_low ^ 0x80000000) > (x_j ^ 0x80000000)) + carry++; + dest[offset+j] = prod_low; + } + while (++j < len); + return carry; + } + + /** Divide zds[0:nx] by y[0:ny-1]. + * The remainder ends up in zds[0:ny-1]. + * The quotient ends up in zds[ny:nx]. + * Assumes: nx>ny. + * (int)y[ny-1] < 0 (i.e. most significant bit set) + */ + + public static void divide (int[] zds, int nx, int[] y, int ny) + { + // This is basically Knuth's formulation of the classical algorithm, + // but translated from in scm_divbigbig in Jaffar's SCM implementation. + + // Correspondance with Knuth's notation: + // Knuth's u[0:m+n] == zds[nx:0]. + // Knuth's v[1:n] == y[ny-1:0] + // Knuth's n == ny. + // Knuth's m == nx-ny. + // Our nx == Knuth's m+n. + + // Could be re-implemented using gmp's mpn_divrem: + // zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny). + + int j = nx; + do + { // loop over digits of quotient + // Knuth's j == our nx-j. + // Knuth's u[j:j+n] == our zds[j:j-ny]. + int qhat; // treated as unsigned + if (zds[j]==y[ny-1]) + qhat = -1; // 0xffffffff + else + { + long w = (((long)(zds[j])) << 32) + ((long)zds[j-1] & 0xffffffffL); + qhat = (int) udiv_qrnnd (w, y[ny-1]); + } + if (qhat != 0) + { + int borrow = submul_1 (zds, j - ny, y, ny, qhat); + int save = zds[j]; + long num = ((long)save&0xffffffffL) - ((long)borrow&0xffffffffL); + while (num != 0) + { + qhat--; + long carry = 0; + for (int i = 0; i < ny; i++) + { + carry += ((long) zds[j-ny+i] & 0xffffffffL) + + ((long) y[i] & 0xffffffffL); + zds[j-ny+i] = (int) carry; + carry >>>= 32; + } + zds[j] += carry; + num = carry - 1; + } + } + zds[j] = qhat; + } while (--j >= ny); + } + + /** Number of digits in the conversion base that always fits in a word. + * For example, for base 10 this is 9, since 10**9 is the + * largest number that fits into a words (assuming 32-bit words). + * This is the same as gmp's __mp_bases[radix].chars_per_limb. + * @param radix the base + * @return number of digits */ + public static int chars_per_word (int radix) + { + if (radix < 10) + { + if (radix < 8) + { + if (radix <= 2) + return 32; + else if (radix == 3) + return 20; + else if (radix == 4) + return 16; + else + return 18 - radix; + } + else + return 10; + } + else if (radix < 12) + return 9; + else if (radix <= 16) + return 8; + else if (radix <= 23) + return 7; + else if (radix <= 40) + return 6; + // The following are conservative, but we don't care. + else if (radix <= 256) + return 4; + else + return 1; + } + + /** Count the number of leading zero bits in an int. */ + public static int count_leading_zeros (int i) + { + if (i == 0) + return 32; + int count = 0; + for (int k = 16; k > 0; k = k >> 1) { + int j = i >>> k; + if (j == 0) + count += k; + else + i = j; + } + return count; + } + + public static int set_str (int dest[], byte[] str, int str_len, int base) + { + int size = 0; + if ((base & (base - 1)) == 0) + { + // The base is a power of 2. Read the input string from + // least to most significant character/digit. */ + + int next_bitpos = 0; + int bits_per_indigit = 0; + for (int i = base; (i >>= 1) != 0; ) bits_per_indigit++; + int res_digit = 0; + + for (int i = str_len; --i >= 0; ) + { + int inp_digit = str[i]; + res_digit |= inp_digit << next_bitpos; + next_bitpos += bits_per_indigit; + if (next_bitpos >= 32) + { + dest[size++] = res_digit; + next_bitpos -= 32; + res_digit = inp_digit >> (bits_per_indigit - next_bitpos); + } + } + + if (res_digit != 0) + dest[size++] = res_digit; + } + else + { + // General case. The base is not a power of 2. + int indigits_per_limb = MPN.chars_per_word (base); + int str_pos = 0; + + while (str_pos < str_len) + { + int chunk = str_len - str_pos; + if (chunk > indigits_per_limb) + chunk = indigits_per_limb; + int res_digit = str[str_pos++]; + int big_base = base; + + while (--chunk > 0) + { + res_digit = res_digit * base + str[str_pos++]; + big_base *= base; + } + + int cy_limb; + if (size == 0) + cy_limb = res_digit; + else + { + cy_limb = MPN.mul_1 (dest, dest, size, big_base); + cy_limb += MPN.add_1 (dest, dest, size, res_digit); + } + if (cy_limb != 0) + dest[size++] = cy_limb; + } + } + return size; + } + + /** Compare x[0:size-1] with y[0:size-1], treating them as unsigned integers. + * @result -1, 0, or 1 depending on if x<y, x==y, or x>y. + * This is basically the same as gmp's mpn_cmp function. + */ + public static int cmp (int[] x, int[] y, int size) + { + while (--size >= 0) + { + int x_word = x[size]; + int y_word = y[size]; + if (x_word != y_word) + { + // Invert the high-order bit, because: + // (unsigned) X > (unsigned) Y iff + // (int) (X^0x80000000) > (int) (Y^0x80000000). + return (x_word ^ 0x80000000) > (y_word ^0x80000000) ? 1 : -1; + } + } + return 0; + } + + /** + * Compare x[0:xlen-1] with y[0:ylen-1], treating them as unsigned integers. + * + * @return -1, 0, or 1 depending on if x<y, x==y, or x>y. + */ + public static int cmp (int[] x, int xlen, int[] y, int ylen) + { + return xlen > ylen ? 1 : xlen < ylen ? -1 : cmp (x, y, xlen); + } + + /** + * Shift x[x_start:x_start+len-1] count bits to the "right" + * (i.e. divide by 2**count). + * Store the len least significant words of the result at dest. + * The bits shifted out to the right are returned. + * OK if dest==x. + * Assumes: 0 < count < 32 + */ + public static int rshift (int[] dest, int[] x, int x_start, + int len, int count) + { + int count_2 = 32 - count; + int low_word = x[x_start]; + int retval = low_word << count_2; + int i = 1; + for (; i < len; i++) + { + int high_word = x[x_start+i]; + dest[i-1] = (low_word >>> count) | (high_word << count_2); + low_word = high_word; + } + dest[i-1] = low_word >>> count; + return retval; + } + + /** + * Shift x[x_start:x_start+len-1] count bits to the "right" + * (i.e. divide by 2**count). + * Store the len least significant words of the result at dest. + * OK if dest==x. + * Assumes: 0 <= count < 32 + * Same as rshift, but handles count==0 (and has no return value). + */ + public static void rshift0 (int[] dest, int[] x, int x_start, + int len, int count) + { + if (count > 0) + rshift(dest, x, x_start, len, count); + else + for (int i = 0; i < len; i++) + dest[i] = x[i + x_start]; + } + + /** Return the long-truncated value of right shifting. + * @param x a two's-complement "bignum" + * @param len the number of significant words in x + * @param count the shift count + * @return (long)(x[0..len-1] >> count). + */ + public static long rshift_long (int[] x, int len, int count) + { + int wordno = count >> 5; + count &= 31; + int sign = x[len-1] < 0 ? -1 : 0; + int w0 = wordno >= len ? sign : x[wordno]; + wordno++; + int w1 = wordno >= len ? sign : x[wordno]; + if (count != 0) + { + wordno++; + int w2 = wordno >= len ? sign : x[wordno]; + w0 = (w0 >>> count) | (w1 << (32-count)); + w1 = (w1 >>> count) | (w2 << (32-count)); + } + return ((long)w1 << 32) | ((long)w0 & 0xffffffffL); + } + + /* Shift x[0:len-1] left by count bits, and store the len least + * significant words of the result in dest[d_offset:d_offset+len-1]. + * Return the bits shifted out from the most significant digit. + * Assumes 0 < count < 32. + * OK if dest==x. + */ + + public static int lshift (int[] dest, int d_offset, + int[] x, int len, int count) + { + int count_2 = 32 - count; + int i = len - 1; + int high_word = x[i]; + int retval = high_word >>> count_2; + d_offset++; + while (--i >= 0) + { + int low_word = x[i]; + dest[d_offset+i] = (high_word << count) | (low_word >>> count_2); + high_word = low_word; + } + dest[d_offset+i] = high_word << count; + return retval; + } + + /** Return least i such that word & (1<<i). Assumes word!=0. */ + + public static int findLowestBit (int word) + { + int i = 0; + while ((word & 0xF) == 0) + { + word >>= 4; + i += 4; + } + if ((word & 3) == 0) + { + word >>= 2; + i += 2; + } + if ((word & 1) == 0) + i += 1; + return i; + } + + /** Return least i such that words & (1<<i). Assumes there is such an i. */ + + public static int findLowestBit (int[] words) + { + for (int i = 0; ; i++) + { + if (words[i] != 0) + return 32 * i + findLowestBit (words[i]); + } + } + + /** Calculate Greatest Common Divisior of x[0:len-1] and y[0:len-1]. + * Assumes both arguments are non-zero. + * Leaves result in x, and returns len of result. + * Also destroys y (actually sets it to a copy of the result). */ + + public static int gcd (int[] x, int[] y, int len) + { + int i, word; + // Find sh such that both x and y are divisible by 2**sh. + for (i = 0; ; i++) + { + word = x[i] | y[i]; + if (word != 0) + { + // Must terminate, since x and y are non-zero. + break; + } + } + int initShiftWords = i; + int initShiftBits = findLowestBit (word); + // Logically: sh = initShiftWords * 32 + initShiftBits + + // Temporarily devide both x and y by 2**sh. + len -= initShiftWords; + MPN.rshift0 (x, x, initShiftWords, len, initShiftBits); + MPN.rshift0 (y, y, initShiftWords, len, initShiftBits); + + int[] odd_arg; /* One of x or y which is odd. */ + int[] other_arg; /* The other one can be even or odd. */ + if ((x[0] & 1) != 0) + { + odd_arg = x; + other_arg = y; + } + else + { + odd_arg = y; + other_arg = x; + } + + for (;;) + { + // Shift other_arg until it is odd; this doesn't + // affect the gcd, since we divide by 2**k, which does not + // divide odd_arg. + for (i = 0; other_arg[i] == 0; ) i++; + if (i > 0) + { + int j; + for (j = 0; j < len-i; j++) + other_arg[j] = other_arg[j+i]; + for ( ; j < len; j++) + other_arg[j] = 0; + } + i = findLowestBit(other_arg[0]); + if (i > 0) + MPN.rshift (other_arg, other_arg, 0, len, i); + + // Now both odd_arg and other_arg are odd. + + // Subtract the smaller from the larger. + // This does not change the result, since gcd(a-b,b)==gcd(a,b). + i = MPN.cmp(odd_arg, other_arg, len); + if (i == 0) + break; + if (i > 0) + { // odd_arg > other_arg + MPN.sub_n (odd_arg, odd_arg, other_arg, len); + // Now odd_arg is even, so swap with other_arg; + int[] tmp = odd_arg; odd_arg = other_arg; other_arg = tmp; + } + else + { // other_arg > odd_arg + MPN.sub_n (other_arg, other_arg, odd_arg, len); + } + while (odd_arg[len-1] == 0 && other_arg[len-1] == 0) + len--; + } + if (initShiftWords + initShiftBits > 0) + { + if (initShiftBits > 0) + { + int sh_out = MPN.lshift (x, initShiftWords, x, len, initShiftBits); + if (sh_out != 0) + x[(len++)+initShiftWords] = sh_out; + } + else + { + for (i = len; --i >= 0;) + x[i+initShiftWords] = x[i]; + } + for (i = initShiftWords; --i >= 0; ) + x[i] = 0; + len += initShiftWords; + } + return len; + } + + public static int intLength (int i) + { + return 32 - count_leading_zeros (i < 0 ? ~i : i); + } + + /** Calcaulte the Common Lisp "integer-length" function. + * Assumes input is canonicalized: len==BigInteger.wordsNeeded(words,len) */ + public static int intLength (int[] words, int len) + { + len--; + return intLength (words[len]) + 32 * len; + } + + /* DEBUGGING: + public static void dprint (BigInteger x) + { + if (x.words == null) + System.err.print(Long.toString((long) x.ival & 0xffffffffL, 16)); + else + dprint (System.err, x.words, x.ival); + } + public static void dprint (int[] x) { dprint (System.err, x, x.length); } + public static void dprint (int[] x, int len) { dprint (System.err, x, len); } + public static void dprint (java.io.PrintStream ps, int[] x, int len) + { + ps.print('('); + for (int i = 0; i < len; i++) + { + if (i > 0) + ps.print (' '); + ps.print ("#x" + Long.toString ((long) x[i] & 0xffffffffL, 16)); + } + ps.print(')'); + } + */ +} diff --git a/libjava/classpath/gnu/java/math/package.html b/libjava/classpath/gnu/java/math/package.html new file mode 100644 index 00000000000..97e10183180 --- /dev/null +++ b/libjava/classpath/gnu/java/math/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.math package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.math</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/net/BASE64.java b/libjava/classpath/gnu/java/net/BASE64.java new file mode 100644 index 00000000000..901aa0c35fc --- /dev/null +++ b/libjava/classpath/gnu/java/net/BASE64.java @@ -0,0 +1,190 @@ +/* BASE.java -- + Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +/** + * Encodes and decodes text according to the BASE64 encoding. + * + * @author Chris Burdess (dog@gnu.org) + */ +public final class BASE64 +{ + private static final byte[] src = { + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, + 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, + 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, + 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x2b, 0x2f + }; + + private static final byte[] dst; + static + { + dst = new byte[0x100]; + for (int i = 0x0; i < 0xff; i++) + { + dst[i] = -1; + } + for (int i = 0; i < src.length; i++) + { + dst[src[i]] = (byte) i; + } + } + + private BASE64() + { + } + + /** + * Encode the specified byte array using the BASE64 algorithm. + * + * @param bs the source byte array + */ + public static byte[] encode(byte[] bs) + { + int si = 0, ti = 0; // source/target array indices + byte[] bt = new byte[((bs.length + 2) * 4) / 3]; // target byte array + for (; si < bs.length; si += 3) + { + int buflen = bs.length - si; + if (buflen == 1) + { + byte b = bs[si]; + int i = 0; + bt[ti++] = src[b >>> 2 & 0x3f]; + bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)]; + } + else if (buflen == 2) + { + byte b1 = bs[si], b2 = bs[si + 1]; + int i = 0; + bt[ti++] = src[b1 >>> 2 & 0x3f]; + bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)]; + bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)]; + } + else + { + byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2]; + bt[ti++] = src[b1 >>> 2 & 0x3f]; + bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)]; + bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)]; + bt[ti++] = src[b3 & 0x3f]; + } + } + if (ti < bt.length) + { + byte[] tmp = new byte[ti]; + System.arraycopy(bt, 0, tmp, 0, ti); + bt = tmp; + } + /*while (ti < bt.length) + { + bt[ti++] = 0x3d; + }*/ + return bt; + } + + /** + * Decode the specified byte array using the BASE64 algorithm. + * + * @param bs the source byte array + */ + public static byte[] decode(byte[] bs) + { + int srclen = bs.length; + while (srclen > 0 && bs[srclen - 1] == 0x3d) + { + srclen--; /* strip padding character */ + } + byte[] buffer = new byte[srclen]; + int buflen = 0; + int si = 0; + int len = srclen - si; + while (len > 0) + { + byte b0 = dst[bs[si++] & 0xff]; + byte b2 = dst[bs[si++] & 0xff]; + buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3); + if (len > 2) + { + b0 = b2; + b2 = dst[bs[si++] & 0xff]; + buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf); + if (len > 3) + { + b0 = b2; + b2 = dst[bs[si++] & 0xff]; + buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f); + } + } + len = srclen - si; + } + byte[] bt = new byte[buflen]; + System.arraycopy(buffer, 0, bt, 0, buflen); + return bt; + } + + public static void main(String[] args) + { + boolean decode = false; + for (int i = 0; i < args.length; i++) + { + if (args[i].equals("-d")) + { + decode = true; + } + else + { + try + { + byte[] in = args[i].getBytes("US-ASCII"); + byte[] out = decode ? decode(in) : encode(in); + System.out.println(args[i] + " = " + + new String(out, "US-ASCII")); + } + catch (java.io.UnsupportedEncodingException e) + { + e.printStackTrace(System.err); + } + } + } + } +} diff --git a/libjava/classpath/gnu/java/net/CRLFInputStream.java b/libjava/classpath/gnu/java/net/CRLFInputStream.java new file mode 100644 index 00000000000..d0f9e8c4130 --- /dev/null +++ b/libjava/classpath/gnu/java/net/CRLFInputStream.java @@ -0,0 +1,174 @@ +/* CRLFInputStream.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import java.io.BufferedInputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * An input stream that filters out CR/LF pairs into LFs. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class CRLFInputStream + extends FilterInputStream +{ + /** + * The CR octet. + */ + public static final int CR = 13; + + /** + * The LF octet. + */ + public static final int LF = 10; + + private boolean doReset; + + /** + * Constructs a CR/LF input stream connected to the specified input + * stream. + */ + public CRLFInputStream(InputStream in) + { + super(in.markSupported() ? in : new BufferedInputStream(in)); + } + + /** + * Reads the next byte of data from this input stream. + * Returns -1 if the end of the stream has been reached. + * @exception IOException if an I/O error occurs + */ + public int read() + throws IOException + { + int c = in.read(); + if (c == CR) + { + in.mark(1); + int d = in.read(); + if (d == LF) + { + c = d; + } + else + { + in.reset(); + } + } + return c; + } + + /** + * Reads up to b.length bytes of data from this input stream into + * an array of bytes. + * Returns -1 if the end of the stream has been reached. + * @exception IOException if an I/O error occurs + */ + public int read(byte[] b) + throws IOException + { + return read(b, 0, b.length); + } + + /** + * Reads up to len bytes of data from this input stream into an + * array of bytes, starting at the specified offset. + * Returns -1 if the end of the stream has been reached. + * @exception IOException if an I/O error occurs + */ + public int read(byte[] b, int off, int len) + throws IOException + { + in.mark(len + 1); + int l = in.read(b, off, len); + if (l > 0) + { + int i = indexOfCRLF(b, off, l); + if (doReset) + { + in.reset(); + if (i != -1) + { + l = in.read(b, off, i + 1); // read to CR + in.read(); // skip LF + b[i] = LF; // fix CR as LF + } + else + { + l = in.read(b, off, len); // CR(s) but no LF + } + } + } + return l; + } + + private int indexOfCRLF(byte[] b, int off, int len) + throws IOException + { + doReset = false; + int lm1 = len - 1; + for (int i = off; i < len; i++) + { + if (b[i] == CR) + { + int d; + if (i == lm1) + { + d = in.read(); + doReset = true; + } + else + { + d = b[i + 1]; + } + if (d == LF) + { + doReset = true; + return i; + } + } + } + return -1; + } + +} + diff --git a/libjava/classpath/gnu/java/net/CRLFOutputStream.java b/libjava/classpath/gnu/java/net/CRLFOutputStream.java new file mode 100644 index 00000000000..f27e0f218ad --- /dev/null +++ b/libjava/classpath/gnu/java/net/CRLFOutputStream.java @@ -0,0 +1,183 @@ +/* CRLFOutputStream.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +/** + * An output stream that filters LFs into CR/LF pairs. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class CRLFOutputStream + extends FilterOutputStream +{ + static final String US_ASCII = "US-ASCII"; + + /** + * The CR octet. + */ + public static final int CR = 13; + + /** + * The LF octet. + */ + public static final int LF = 10; + + /** + * The CR/LF pair. + */ + public static final byte[] CRLF = { CR, LF }; + + /** + * The last byte read. + */ + protected int last; + + /** + * Constructs a CR/LF output stream connected to the specified output stream. + */ + public CRLFOutputStream(OutputStream out) + { + super(out); + last = -1; + } + + /** + * Writes a character to the underlying stream. + * @exception IOException if an I/O error occurred + */ + public void write(int ch) throws IOException + { + if (ch == CR) + { + out.write(CRLF); + } + else if (ch == LF) + { + if (last != CR) + { + out.write(CRLF); + } + } + else + { + out.write(ch); + } + last = ch; + } + + /** + * Writes a byte array to the underlying stream. + * @exception IOException if an I/O error occurred + */ + public void write(byte[] b) + throws IOException + { + write(b, 0, b.length); + } + + /** + * Writes a portion of a byte array to the underlying stream. + * @exception IOException if an I/O error occurred + */ + public void write(byte[] b, int off, int len) + throws IOException + { + int d = off; + len += off; + for (int i = off; i < len; i++) + { + switch (b[i]) + { + case CR: + out.write (b, d, i - d); + out.write (CRLF, 0, 2); + d = i + 1; + break; + case LF: + if (last != CR) + { + out.write (b, d, i - d); + out.write (CRLF, 0, 2); + } + d = i + 1; + break; + } + last = b[i]; + } + if (len - d > 0) + { + out.write (b, d, len - d); + } + } + + /** + * Writes the specified ASCII string to the underlying stream. + * @exception IOException if an I/O error occurred + */ + public void write(String text) + throws IOException + { + try + { + byte[] bytes = text.getBytes(US_ASCII); + write(bytes, 0, bytes.length); + } + catch (UnsupportedEncodingException e) + { + throw new IOException("The US-ASCII encoding is not supported " + + "on this system"); + } + } + + /** + * Writes a newline to the underlying stream. + * @exception IOException if an I/O error occurred + */ + public void writeln() + throws IOException + { + out.write(CRLF, 0, 2); + } +} + diff --git a/libjava/classpath/gnu/java/net/EmptyX509TrustManager.java b/libjava/classpath/gnu/java/net/EmptyX509TrustManager.java new file mode 100644 index 00000000000..b8faf41add7 --- /dev/null +++ b/libjava/classpath/gnu/java/net/EmptyX509TrustManager.java @@ -0,0 +1,70 @@ +/* EmptyX509TrustManager.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.X509TrustManager; + +/** + * Empty implementation of an X509 trust manager. + * This implementation does not check any certificates in the chain. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class EmptyX509TrustManager + implements X509TrustManager +{ + public void checkClientTrusted(X509Certificate[] chain, String authType) + throws CertificateException + { + } + + public void checkServerTrusted(X509Certificate[] chain, String authType) + throws CertificateException + { + } + + public X509Certificate[] getAcceptedIssuers() + { + return new X509Certificate[0]; + } +} + diff --git a/libjava/classpath/gnu/java/net/GetLocalHostAction.java b/libjava/classpath/gnu/java/net/GetLocalHostAction.java new file mode 100644 index 00000000000..7483b025773 --- /dev/null +++ b/libjava/classpath/gnu/java/net/GetLocalHostAction.java @@ -0,0 +1,65 @@ +/* GetLocalHostAction.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.security.PrivilegedAction; + +/** + * Privileged action to retrieve the local host InetAddress. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class GetLocalHostAction + implements PrivilegedAction +{ + public Object run() + { + try + { + return InetAddress.getLocalHost(); + } + catch (UnknownHostException e) + { + return null; + } + } +} + diff --git a/libjava/classpath/gnu/java/net/HeaderFieldHelper.java b/libjava/classpath/gnu/java/net/HeaderFieldHelper.java new file mode 100644 index 00000000000..0fb8d953d21 --- /dev/null +++ b/libjava/classpath/gnu/java/net/HeaderFieldHelper.java @@ -0,0 +1,138 @@ +/* HeaderFieldHelper.java -- Helps manage headers fields + Copyright (C) 1998, 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; + +/** + * This class manages header field keys and values. + * + * @author Aaron M. Renn (arenn@urbanophile.com) + */ +public class HeaderFieldHelper +{ + private Vector headerFieldKeys; + private Vector headerFieldValues; + + public HeaderFieldHelper() + { + this (10); + } + + public HeaderFieldHelper (int size) + { + headerFieldKeys = new Vector (size); + headerFieldValues = new Vector (size); + } + + public void addHeaderField (String key, String value) + { + headerFieldKeys.addElement (key); + headerFieldValues.addElement (value); + } + + public String getHeaderFieldKeyByIndex (int index) + { + String key = null; + + try + { + key = (String) headerFieldKeys.elementAt (index); + } + catch (ArrayIndexOutOfBoundsException e) + { + } + + return key; + } + + public String getHeaderFieldValueByIndex(int index) + { + String value = null; + + try + { + value = (String) headerFieldValues.elementAt (index); + } + catch (ArrayIndexOutOfBoundsException e) + { + } + + return value; + } + + public String getHeaderFieldValueByKey(String key) + { + String value = null; + + try + { + value = (String) headerFieldValues.elementAt + (headerFieldKeys.indexOf(key)); + } + catch (ArrayIndexOutOfBoundsException e) + { + } + + return value; + } + + public Map getHeaderFields() + { + HashMap headers = new HashMap(); + int max = headerFieldKeys.size(); + + for (int index = 0; index < max; index++) + { + headers.put(headerFieldKeys.elementAt(index), + headerFieldValues.elementAt(index)); + } + + return headers; + } + + public int getNumberOfEntries() + { + return headerFieldKeys.size(); + } + +} // class HeaderFieldHelper + diff --git a/libjava/classpath/gnu/java/net/LineInputStream.java b/libjava/classpath/gnu/java/net/LineInputStream.java new file mode 100644 index 00000000000..5ca068618da --- /dev/null +++ b/libjava/classpath/gnu/java/net/LineInputStream.java @@ -0,0 +1,198 @@ +/* LineInputStream.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import java.io.ByteArrayOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * An input stream that can read lines of input. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class LineInputStream + extends FilterInputStream +{ + /* + * Line buffer. + */ + private ByteArrayOutputStream buf; + + /* + * Encoding to use when translating bytes to characters. + */ + private String encoding; + + /* + * End-of-stream flag. + */ + private boolean eof; + + /** + * Whether we can use block reads. + */ + private final boolean blockReads; + + /** + * Constructor using the US-ASCII character encoding. + * @param in the underlying input stream + */ + public LineInputStream(InputStream in) + { + this(in, "US-ASCII"); + } + + /** + * Constructor. + * @param in the underlying input stream + * @param encoding the character encoding to use + */ + public LineInputStream(InputStream in, String encoding) + { + super(in); + buf = new ByteArrayOutputStream(); + this.encoding = encoding; + eof = false; + blockReads = in.markSupported(); + } + + /** + * Read a line of input. + */ + public String readLine() + throws IOException + { + if (eof) + { + return null; + } + do + { + if (blockReads) + { + // Use mark and reset to read chunks of bytes + final int MIN_LENGTH = 1024; + int len, pos; + + len = in.available(); + len = (len < MIN_LENGTH) ? MIN_LENGTH : len; + byte[] b = new byte[len]; + in.mark(len); + // Read into buffer b + len = in.read(b, 0, len); + // Handle EOF + if (len == -1) + { + eof = true; + if (buf.size() == 0) + { + return null; + } + else + { + // We don't care about resetting buf + return buf.toString(encoding); + } + } + // Get index of LF in b + pos = indexOf(b, len, (byte) 0x0a); + if (pos != -1) + { + // Write pos bytes to buf + buf.write(b, 0, pos); + // Reset stream, and read pos + 1 bytes + in.reset(); + pos += 1; + while (pos > 0) + { + len = in.read(b, 0, pos); + pos = (len == -1) ? -1 : pos - len; + } + // Return line + String ret = buf.toString(encoding); + buf.reset(); + return ret; + } + else + { + // Append everything to buf and fall through to re-read. + buf.write(b, 0, len); + } + } + else + { + // We must use character reads in order not to read too much + // from the underlying stream. + int c = in.read(); + switch (c) + { + case -1: + eof = true; + if (buf.size() == 0) + { + return null; + } + // Fall through and return contents of buffer. + case 0x0a: // LF + String ret = buf.toString(encoding); + buf.reset(); + return ret; + default: + buf.write(c); + } + } + } + while (true); + } + + private int indexOf(byte[] b, int len, byte c) + { + for (int pos = 0; pos < len; pos++) + { + if (b[pos] == c) + { + return pos; + } + } + return -1; + } +} + diff --git a/libjava/classpath/gnu/java/net/PlainDatagramSocketImpl.java b/libjava/classpath/gnu/java/net/PlainDatagramSocketImpl.java new file mode 100644 index 00000000000..339b5561cf2 --- /dev/null +++ b/libjava/classpath/gnu/java/net/PlainDatagramSocketImpl.java @@ -0,0 +1,321 @@ +/* PlainDatagramSocketImpl.java -- Default DatagramSocket implementation + Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import gnu.classpath.Configuration; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocketImpl; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketAddress; +import java.net.SocketException; + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +/** + * This is the default socket implementation for datagram sockets. + * It makes native calls to C routines that implement BSD style + * SOCK_DGRAM sockets in the AF_INET family. + * + * @author Aaron M. Renn (arenn@urbanophile.com) + * @author Warren Levy (warrenl@cygnus.com) + */ +public final class PlainDatagramSocketImpl extends DatagramSocketImpl +{ + // Static initializer to load native library + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("javanet"); + } + } + + /** + * Option id for the IP_TTL (time to live) value. + */ + private static final int IP_TTL = 0x1E61; // 7777 + + /** + * This is the actual underlying file descriptor + */ + int native_fd = -1; + + /** + * Lock object to serialize threads wanting to receive + */ + private final Object RECEIVE_LOCK = new Object(); + + /** + * Lock object to serialize threads wanting to send + */ + private final Object SEND_LOCK = new Object(); + + /** + * Default do nothing constructor + */ + public PlainDatagramSocketImpl() + { + } + + protected void finalize() throws Throwable + { + synchronized (this) + { + if (native_fd != -1) + close(); + } + super.finalize(); + } + + public int getNativeFD() + { + return native_fd; + } + + /** + * Binds this socket to a particular port and interface + * + * @param port The port to bind to + * @param addr The address to bind to + * + * @exception SocketException If an error occurs + */ + protected synchronized native void bind(int port, InetAddress addr) + throws SocketException; + + /** + * Creates a new datagram socket + * + * @exception SocketException If an error occurs + */ + protected synchronized native void create() throws SocketException; + + /** + * Sets the Time to Live value for the socket + * + * @param ttl The new TTL value + * + * @exception IOException If an error occurs + */ + protected synchronized void setTimeToLive(int ttl) throws IOException + { + setOption(IP_TTL, new Integer(ttl)); + } + + /** + * Gets the Time to Live value for the socket + * + * @return The TTL value + * + * @exception IOException If an error occurs + */ + protected synchronized int getTimeToLive() throws IOException + { + Object obj = getOption(IP_TTL); + + if (! (obj instanceof Integer)) + throw new IOException("Internal Error"); + + return ((Integer) obj).intValue(); + } + + /** + * Sends a packet of data to a remote host + * + * @param addr The address to send to + * @param port The port to send to + * @param buf The buffer to send + * @param offset The offset of the data in the buffer to send + * @param len The length of the data to send + * + * @exception IOException If an error occurs + */ + private native void sendto (InetAddress addr, int port, + byte[] buf, int offset, int len) + throws IOException; + + /** + * Sends a packet of data to a remote host + * + * @param packet The packet to send + * + * @exception IOException If an error occurs + */ + protected void send(DatagramPacket packet) throws IOException + { + synchronized(SEND_LOCK) + { + sendto(packet.getAddress(), packet.getPort(), packet.getData(), + packet.getOffset(), packet.getLength()); + } + + } + + /** + * Receives a UDP packet from the network + * + * @param packet The packet to fill in with the data received + * + * @exception IOException IOException If an error occurs + */ + protected void receive(DatagramPacket packet) + throws IOException + { + synchronized(RECEIVE_LOCK) + { + receive0(packet); + } + } + + /** + * Native call to receive a UDP packet from the network + * + * @param packet The packet to fill in with the data received + * + * @exception IOException IOException If an error occurs + */ + private native void receive0(DatagramPacket packet) throws IOException; + + /** + * Sets the value of an option on the socket + * + * @param option_id The identifier of the option to set + * @param val The value of the option to set + * + * @exception SocketException If an error occurs + */ + public synchronized native void setOption(int option_id, Object val) + throws SocketException; + + /** + * Retrieves the value of an option on the socket + * + * @param option_id The identifier of the option to retrieve + * + * @return The value of the option + * + * @exception SocketException If an error occurs + */ + public synchronized native Object getOption(int option_id) + throws SocketException; + + /** + * Closes the socket + */ + protected synchronized native void close(); + + /** + * Gets the Time to Live value for the socket + * + * @return The TTL value + * + * @exception IOException If an error occurs + * + * @deprecated 1.2 + */ + protected synchronized byte getTTL() throws IOException + { + return (byte) getTimeToLive(); + } + + /** + * Sets the Time to Live value for the socket + * + * @param ttl The new TTL value + * + * @exception IOException If an error occurs + * + * @deprecated 1.2 + */ + protected synchronized void setTTL(byte ttl) throws IOException + { + setTimeToLive(((int) ttl) & 0xFF); + } + + /** + * Joins a multicast group + * + * @param addr The group to join + * + * @exception IOException If an error occurs + */ + protected synchronized native void join(InetAddress addr) throws IOException; + + /** + * Leaves a multicast group + * + * @param addr The group to leave + * + * @exception IOException If an error occurs + */ + protected synchronized native void leave(InetAddress addr) throws IOException; + + /** + * What does this method really do? + */ + protected synchronized int peek(InetAddress addr) throws IOException + { + throw new IOException("Not Implemented Yet"); + } + + public int peekData(DatagramPacket packet) + { + throw new InternalError + ("PlainDatagramSocketImpl::peekData is not implemented"); + } + + public void joinGroup(SocketAddress address, NetworkInterface netIf) + { + throw new InternalError + ("PlainDatagramSocketImpl::joinGroup is not implemented"); + } + + public void leaveGroup(SocketAddress address, NetworkInterface netIf) + { + throw new InternalError + ("PlainDatagramSocketImpl::leaveGroup is not implemented"); + } +} diff --git a/libjava/classpath/gnu/java/net/PlainSocketImpl.java b/libjava/classpath/gnu/java/net/PlainSocketImpl.java new file mode 100644 index 00000000000..05221ff88e9 --- /dev/null +++ b/libjava/classpath/gnu/java/net/PlainSocketImpl.java @@ -0,0 +1,498 @@ +/* PlainSocketImpl.java -- Default socket implementation + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net; + +import gnu.classpath.Configuration; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.net.SocketException; +import java.net.SocketImpl; +import java.net.SocketOptions; + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +/** + * Unless the application installs its own SocketImplFactory, this is the + * default socket implemetation that will be used. It simply uses a + * combination of Java and native routines to implement standard BSD + * style sockets of family AF_INET and types SOCK_STREAM and SOCK_DGRAM + * + * @author Per Bothner (bothner@cygnus.com) + * @author Nic Ferrier (nferrier@tapsellferrier.co.uk) + * @author Aaron M. Renn (arenn@urbanophile.com) + */ +public final class PlainSocketImpl extends SocketImpl +{ + // Static initializer to load native library. + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("javanet"); + } + } + + /** + * The OS file handle representing the socket. + * This is used for reads and writes to/from the socket and + * to close it. + * + * When the socket is closed this is reset to -1. + */ + int native_fd = -1; + + /** + * A cached copy of the in stream for reading from the socket. + */ + private InputStream in; + + /** + * A cached copy of the out stream for writing to the socket. + */ + private OutputStream out; + + /** + * Indicates whether a channel initiated whatever operation + * is being invoked on this socket. + */ + private boolean inChannelOperation; + + /** + * Indicates whether we should ignore whether any associated + * channel is set to non-blocking mode. Certain operations + * throw an <code>IllegalBlockingModeException</code> if the + * associated channel is in non-blocking mode, <i>except</i> + * if the operation is invoked by the channel itself. + */ + public final boolean isInChannelOperation() + { + return inChannelOperation; + } + + /** + * Sets our indicator of whether an I/O operation is being + * initiated by a channel. + */ + public final void setInChannelOperation(boolean b) + { + inChannelOperation = b; + } + + /** + * Default do nothing constructor + */ + public PlainSocketImpl() + { + } + + protected void finalize() throws Throwable + { + synchronized (this) + { + if (native_fd != -1) + try + { + close(); + } + catch (IOException ex) + { + } + } + super.finalize(); + } + + public int getNativeFD() + { + return native_fd; + } + + /** + * Sets the specified option on a socket to the passed in object. For + * options that take an integer argument, the passed in object is an + * Integer. The option_id parameter is one of the defined constants in + * this interface. + * + * @param option_id The identifier of the option + * @param val The value to set the option to + * + * @exception SocketException If an error occurs + */ + public native void setOption(int optID, Object value) throws SocketException; + + /** + * Returns the current setting of the specified option. The Object returned + * will be an Integer for options that have integer values. The option_id + * is one of the defined constants in this interface. + * + * @param option_id The option identifier + * + * @return The current value of the option + * + * @exception SocketException If an error occurs + */ + public native Object getOption(int optID) throws SocketException; + + /** + * Flushes the input stream and closes it. If you read from the input stream + * after calling this method a <code>IOException</code> will be thrown. + * + * @throws IOException if an error occurs + */ + public native void shutdownInput() throws IOException; + + /** + * Flushes the output stream and closes it. If you write to the output stream + * after calling this method a <code>IOException</code> will be thrown. + * + * @throws IOException if an error occurs + */ + public native void shutdownOutput() throws IOException; + + /** + * Creates a new socket that is not bound to any local address/port and + * is not connected to any remote address/port. This will be created as + * a stream socket if the stream parameter is true, or a datagram socket + * if the stream parameter is false. + * + * @param stream true for a stream socket, false for a datagram socket + */ + protected synchronized native void create(boolean stream) throws IOException; + + /** + * Connects to the remote hostname and port specified as arguments. + * + * @param hostname The remote hostname to connect to + * @param port The remote port to connect to + * + * @exception IOException If an error occurs + */ + protected synchronized void connect(String host, int port) throws IOException + { + connect(InetAddress.getByName(host), port); + } + + /** + * Connects to the remote address and port specified as arguments. + * + * @param addr The remote address to connect to + * @param port The remote port to connect to + * + * @exception IOException If an error occurs + */ + protected native void connect(InetAddress addr, int port) throws IOException; + + /** + * Connects to the remote socket address with a specified timeout. + * + * @param timeout The timeout to use for this connect, 0 means infinite. + * + * @exception IOException If an error occurs + */ + protected synchronized void connect(SocketAddress address, int timeout) throws IOException + { + InetSocketAddress sockAddr = (InetSocketAddress) address; + InetAddress addr = sockAddr.getAddress(); + + if (addr == null) + throw new IllegalArgumentException("address is unresolved: " + sockAddr); + + int port = sockAddr.getPort(); + + if (timeout < 0) + throw new IllegalArgumentException("negative timeout"); + + Object oldTimeoutObj = null; + + try + { + oldTimeoutObj = this.getOption (SocketOptions.SO_TIMEOUT); + this.setOption (SocketOptions.SO_TIMEOUT, new Integer (timeout)); + connect (addr, port); + } + finally + { + if (oldTimeoutObj != null) + this.setOption (SocketOptions.SO_TIMEOUT, oldTimeoutObj); + } + } + + /** + * Binds to the specified port on the specified addr. Note that this addr + * must represent a local IP address. **** How bind to INADDR_ANY? **** + * + * @param addr The address to bind to + * @param port The port number to bind to + * + * @exception IOException If an error occurs + */ + protected synchronized native void bind(InetAddress addr, int port) + throws IOException; + + /** + * Starts listening for connections on a socket. The queuelen parameter + * is how many pending connections will queue up waiting to be serviced + * before being accept'ed. If the queue of pending requests exceeds this + * number, additional connections will be refused. + * + * @param queuelen The length of the pending connection queue + * + * @exception IOException If an error occurs + */ + protected synchronized native void listen(int queuelen) + throws IOException; + + /** + * Accepts a new connection on this socket and returns in in the + * passed in SocketImpl. + * + * @param impl The SocketImpl object to accept this connection. + */ + protected synchronized native void accept(SocketImpl impl) + throws IOException; + + /** + * Returns the number of bytes that the caller can read from this socket + * without blocking. + * + * @return The number of readable bytes before blocking + * + * @exception IOException If an error occurs + */ + protected native int available() throws IOException; + + /** + * Closes the socket. This will cause any InputStream or OutputStream + * objects for this Socket to be closed as well. + * <p> + * Note that if the SO_LINGER option is set on this socket, then the + * operation could block. + * + * @exception IOException If an error occurs + */ + protected native void close() throws IOException; + + public void sendUrgentData(int data) + { + throw new InternalError ("PlainSocketImpl::sendUrgentData not implemented"); + } + + /** + * Internal method used by SocketInputStream for reading data from + * the connection. Reads up to len bytes of data into the buffer + * buf starting at offset bytes into the buffer. + * + * @return The actual number of bytes read or -1 if end of stream. + * + * @exception IOException If an error occurs + */ + protected native int read(byte[] buf, int offset, int len) + throws IOException; + + /** + * Internal method used by SocketOuputStream for writing data to + * the connection. Writes up to len bytes of data from the buffer + * buf starting at offset bytes into the buffer. + * + * @exception IOException If an error occurs + */ + protected native void write(byte[] buf, int offset, int len) + throws IOException; + + /** + * Returns an InputStream object for reading from this socket. This will + * be an instance of SocketInputStream. + * + * @return An input stream attached to the socket. + * + * @exception IOException If an error occurs + */ + protected synchronized InputStream getInputStream() throws IOException + { + if (in == null) + in = new SocketInputStream(); + + return in; + } + + /** + * Returns an OutputStream object for writing to this socket. This will + * be an instance of SocketOutputStream. + * + * @return An output stream attached to the socket. + * + * @exception IOException If an error occurs + */ + protected synchronized OutputStream getOutputStream() throws IOException + { + if (out == null) + out = new SocketOutputStream(); + + return out; + } + + /** + * This class contains an implementation of <code>InputStream</code> for + * sockets. It in an internal only class used by <code>PlainSocketImpl</code>. + * + * @author Nic Ferrier (nferrier@tapsellferrier.co.uk) + */ + final class SocketInputStream + extends InputStream + { + /** + * Returns the number of bytes available to be read before blocking + */ + public int available() throws IOException + { + return PlainSocketImpl.this.available(); + } + + /** + * This method not only closes the stream, it closes the underlying socket + * (and thus any connection) and invalidates any other Input/Output streams + * for the underlying impl object + */ + public void close() throws IOException + { + PlainSocketImpl.this.close(); + } + + /** + * Reads the next byte of data and returns it as an int. + * + * @return The byte read (as an int) or -1 if end of stream); + * + * @exception IOException If an error occurs. + */ + public int read() throws IOException + { + byte buf[] = new byte [1]; + int bytes_read = read(buf, 0, 1); + + if (bytes_read == -1) + return -1; + + return buf[0] & 0xFF; + } + + /** + * Reads up to len bytes of data into the caller supplied buffer starting + * at offset bytes from the start of the buffer + * + * @param buf The buffer + * @param offset Offset into the buffer to start reading from + * @param len The number of bytes to read + * + * @return The number of bytes actually read or -1 if end of stream + * + * @exception IOException If an error occurs. + */ + public int read (byte[] buf, int offset, int len) throws IOException + { + int bytes_read = PlainSocketImpl.this.read (buf, offset, len); + + if (bytes_read == 0) + return -1; + + return bytes_read; + } + } + + /** + * This class is used internally by <code>PlainSocketImpl</code> to be the + * <code>OutputStream</code> subclass returned by its + * <code>getOutputStream method</code>. It expects only to be used in that + * context. + * + * @author Nic Ferrier (nferrier@tapsellferrier.co.uk) + */ + final class SocketOutputStream + extends OutputStream + { + /** + * This method closes the stream and the underlying socket connection. This + * action also effectively closes any other InputStream or OutputStream + * object associated with the connection. + * + * @exception IOException If an error occurs + */ + public void close() throws IOException + { + PlainSocketImpl.this.close(); + } + + /** + * Writes a byte (passed in as an int) to the given output stream + * + * @param b The byte to write + * + * @exception IOException If an error occurs + */ + public void write(int b) throws IOException + { + byte buf[] = { (byte) b }; + write(buf, 0, 1); + } + + /** + * Writes len number of bytes from the array buf to the stream starting + * at offset bytes into the buffer. + * + * @param buf The buffer + * @param offset Offset into the buffer to start writing from + * @param len The number of bytes to write + * + * @exception IOException If an error occurs. + */ + public void write (byte[] buf, int offset, int len) throws IOException + { + PlainSocketImpl.this.write (buf, offset, len); + } + } +} diff --git a/libjava/classpath/gnu/java/net/URLParseError.java b/libjava/classpath/gnu/java/net/URLParseError.java new file mode 100644 index 00000000000..90907480f11 --- /dev/null +++ b/libjava/classpath/gnu/java/net/URLParseError.java @@ -0,0 +1,57 @@ +/* URLParseError.java -- Helps bypassing the exception limitation for + URLStreamHandler.parseURL(). + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.net; + +/** + * This class helps the people writing protocols to report URL parse + * errors in parseUrl as this method cannot report other exceptions + * than Errors. + * + * The main drawback is that it uses the Error mechanism which should not + * be used for that type of error reporting. + * + * @author Guilhem Lavaux (guilhem@kaffe.org) + */ +public class URLParseError extends Error +{ + public URLParseError(String msg) + { + super(msg); + } +} diff --git a/libjava/classpath/gnu/java/net/package.html b/libjava/classpath/gnu/java/net/package.html new file mode 100644 index 00000000000..5641fbd92cf --- /dev/null +++ b/libjava/classpath/gnu/java/net/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.net package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.net</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/net/protocol/file/Connection.java b/libjava/classpath/gnu/java/net/protocol/file/Connection.java new file mode 100644 index 00000000000..52bd0484510 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/file/Connection.java @@ -0,0 +1,319 @@ +/* FileURLConnection.java -- URLConnection class for "file" protocol + Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.net.protocol.file; + +import gnu.classpath.SystemProperties; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FilePermission; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.ProtocolException; +import java.net.URL; +import java.net.URLConnection; +import java.security.Permission; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * This subclass of java.net.URLConnection models a URLConnection via + * the "file" protocol. + * + * @author Aaron M. Renn (arenn@urbanophile.com) + * @author Nic Ferrier (nferrier@tapsellferrier.co.uk) + * @author Warren Levy (warrenl@cygnus.com) + */ +public class Connection extends URLConnection +{ + /** + * Default permission for a file + */ + private static final String DEFAULT_PERMISSION = "read"; + + private static class StaticData + { + /** + * HTTP-style DateFormat, used to format the last-modified header. + */ + static SimpleDateFormat dateFormat + = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'", + new Locale ("En", "Us", "Unix")); + + static String lineSeparator = + SystemProperties.getProperty("line.separator"); + } + + + /** + * This is a File object for this connection + */ + private File file; + + /** + * If a directory, contains a list of files in the directory. + */ + private byte[] directoryListing; + + /** + * InputStream if we are reading from the file + */ + private InputStream inputStream; + + /** + * OutputStream if we are writing to the file + */ + private OutputStream outputStream; + + /** + * FilePermission to read the file + */ + private FilePermission permission; + + /** + * Calls superclass constructor to initialize. + */ + public Connection(URL url) + { + super (url); + + permission = new FilePermission(getURL().getFile(), DEFAULT_PERMISSION); + } + + /** + * "Connects" to the file by opening it. + */ + public void connect() throws IOException + { + // Call is ignored if already connected. + if (connected) + return; + + // If not connected, then file needs to be openned. + file = new File (getURL().getFile()); + + if (! file.isDirectory()) + { + if (doInput) + inputStream = new BufferedInputStream(new FileInputStream(file)); + + if (doOutput) + outputStream = new BufferedOutputStream(new FileOutputStream(file)); + } + else + { + if (doInput) + { + inputStream = new ByteArrayInputStream(getDirectoryListing()); + } + + if (doOutput) + throw new ProtocolException + ("file: protocol does not support output on directories"); + } + + connected = true; + } + + /** + * Populates the <code>directoryListing</code> field with a byte array + * containing a representation of the directory listing. + */ + byte[] getDirectoryListing() + throws IOException + { + if (directoryListing == null) + { + ByteArrayOutputStream sink = new ByteArrayOutputStream(); + // NB uses default character encoding for this system + Writer writer = new OutputStreamWriter(sink); + + String[] files = file.list(); + + for (int i = 0; i < files.length; i++) + { + writer.write(files[i]); + writer.write(StaticData.lineSeparator); + } + + directoryListing = sink.toByteArray(); + } + return directoryListing; + } + + /** + * Opens the file for reading and returns a stream for it. + * + * @return An InputStream for this connection. + * + * @exception IOException If an error occurs + */ + public InputStream getInputStream() + throws IOException + { + if (!doInput) + throw new ProtocolException("Can't open InputStream if doInput is false"); + + if (!connected) + connect(); + + return inputStream; + } + + /** + * Opens the file for writing and returns a stream for it. + * + * @return An OutputStream for this connection. + * + * @exception IOException If an error occurs. + */ + public OutputStream getOutputStream() + throws IOException + { + if (!doOutput) + throw new + ProtocolException("Can't open OutputStream if doOutput is false"); + + if (!connected) + connect(); + + return outputStream; + } + + /** + * Get the last modified time of the resource. + * + * @return the time since epoch that the resource was modified. + */ + public long getLastModified() + { + try + { + if (!connected) + connect(); + + return file.lastModified(); + } + catch (IOException e) + { + return -1; + } + } + + /** + * Get an http-style header field. Just handle a few common ones. + */ + public String getHeaderField(String field) + { + try + { + if (!connected) + connect(); + + if (field.equals("content-type")) + return guessContentTypeFromName(file.getName()); + else if (field.equals("content-length")) + { + if (file.isDirectory()) + { + return Integer.toString(getContentLength()); + } + return Long.toString(file.length()); + } + else if (field.equals("last-modified")) + { + synchronized (StaticData.dateFormat) + { + return StaticData.dateFormat.format( + new Date(file.lastModified())); + } + } + } + catch (IOException e) + { + // Fall through. + } + return null; + } + + /** + * Get the length of content. + * + * @return the length of the content. + */ + public int getContentLength() + { + try + { + if (!connected) + connect(); + + if (file.isDirectory()) + { + return getDirectoryListing().length; + } + return (int) file.length(); + } + catch (IOException e) + { + return -1; + } + } + + /** + * This method returns a <code>Permission</code> object representing the + * permissions required to access this URL. This method returns a + * <code>java.io.FilePermission</code> for the file's path with a read + * permission. + * + * @return A Permission object + */ + public Permission getPermission() throws IOException + { + return permission; + } +} diff --git a/libjava/classpath/gnu/java/net/protocol/file/Handler.java b/libjava/classpath/gnu/java/net/protocol/file/Handler.java new file mode 100644 index 00000000000..fc560491d19 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/file/Handler.java @@ -0,0 +1,91 @@ +/* Handler.java -- "file" protocol handler for java.net + Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.net.protocol.file; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * This is the protocol handler for the "file" protocol. + * It implements the abstract openConnection() method from + * URLStreamHandler by returning a new FileURLConnection object (from + * this package). All other methods are inherited + * + * @author Aaron M. Renn (arenn@urbanophile.com) + * @author Warren Levy (warrenl@cygnus.com) + */ +public class Handler extends URLStreamHandler +{ + /** + * A do nothing constructor + */ + public Handler() + { + } + + /** + * This method returs a new FileURLConnection for the specified URL + * + * @param url The URL to return a connection for + * + * @return The URLConnection + * + * @exception IOException If an error occurs + */ + protected URLConnection openConnection(URL url) throws IOException + { + // If a hostname is set, then we need to switch protocols to ftp + // in order to transfer this from the remote host. + String host = url.getHost(); + if ((host != null) && (! host.equals(""))) + { + // Reset the protocol (and implicitly the handler) for this URL. + // Then have the URL attempt the connection again, as it will + // get the changed handler the next time around. + // If the ftp protocol handler is not installed, an + // exception will be thrown from the new openConnection() call. + setURL (url, "ftp", url.getHost(), url.getPort(), url.getFile(), + url.getRef()); + return url.openConnection(); + } + + return new Connection(url); + } +} // class Handler diff --git a/libjava/classpath/gnu/java/net/protocol/file/package.html b/libjava/classpath/gnu/java/net/protocol/file/package.html new file mode 100644 index 00000000000..cbce7413fb9 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/file/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.net.protocol.file package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.net.protocol.file</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/ActiveModeDTP.java b/libjava/classpath/gnu/java/net/protocol/ftp/ActiveModeDTP.java new file mode 100644 index 00000000000..3755e8beb8d --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/ActiveModeDTP.java @@ -0,0 +1,251 @@ +/* ActiveModeDTP.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * An active mode FTP data transfer process. + * This starts a server on the specified port listening for a data + * connection. It converts the socket input into a file stream for reading. + * + * @author Chris Burdess (dog@gnu.org) + */ +final class ActiveModeDTP + implements DTP, Runnable +{ + + ServerSocket server; + Socket socket; + DTPInputStream in; + DTPOutputStream out; + boolean completed; + boolean inProgress; + int transferMode; + IOException exception; + Thread acceptThread; + int connectionTimeout; + + ActiveModeDTP(InetAddress localhost, int port, + int connectionTimeout, int timeout) + throws IOException + { + completed = false; + inProgress = false; + server = new ServerSocket(port, 1, localhost); + if (timeout > 0) + { + server.setSoTimeout(timeout); + } + if (connectionTimeout <= 0) + { + connectionTimeout = 20000; + } + this.connectionTimeout = connectionTimeout; + acceptThread = new Thread(this, "ActiveModeDTP"); + acceptThread.start(); + } + + /** + * Start listening. + */ + public void run() + { + try + { + socket = server.accept(); + //System.err.println("Accepted connection from "+socket.getInetAddress()+":"+socket.getPort()); + } + catch (IOException e) + { + exception = e; + } + } + + /** + * Waits until a client has connected. + */ + public void waitFor() + throws IOException + { + try + { + acceptThread.join(connectionTimeout); + } + catch (InterruptedException e) + { + } + if (exception != null) + { + throw exception; + } + if (socket == null) + { + server.close(); + throw new IOException("client did not connect before timeout"); + } + acceptThread = null; + } + + /** + * Returns an input stream from which a remote file can be read. + */ + public InputStream getInputStream() + throws IOException + { + if (inProgress) + { + throw new IOException("Transfer in progress"); + } + if (acceptThread != null) + { + waitFor(); + } + switch (transferMode) + { + case FTPConnection.MODE_STREAM: + in = new StreamInputStream(this, socket.getInputStream()); + break; + case FTPConnection.MODE_BLOCK: + in = new BlockInputStream(this, socket.getInputStream()); + break; + case FTPConnection.MODE_COMPRESSED: + in = new CompressedInputStream(this, socket.getInputStream()); + break; + default: + throw new IllegalStateException("invalid transfer mode"); + } + in.setTransferComplete(false); + return in; + } + + /** + * Returns an output stream to which a local file can be written for + * upload. + */ + public OutputStream getOutputStream() throws IOException + { + if (inProgress) + { + throw new IOException("Transfer in progress"); + } + if (acceptThread != null) + { + waitFor(); + } + switch (transferMode) + { + case FTPConnection.MODE_STREAM: + out = new StreamOutputStream(this, socket.getOutputStream()); + break; + case FTPConnection.MODE_BLOCK: + out = new BlockOutputStream(this, socket.getOutputStream()); + break; + case FTPConnection.MODE_COMPRESSED: + out = new CompressedOutputStream(this, socket.getOutputStream()); + break; + default: + throw new IllegalStateException("invalid transfer mode"); + } + out.setTransferComplete(false); + return out; + } + + public void setTransferMode(int mode) + { + transferMode = mode; + } + + public void complete() + { + completed = true; + if (!inProgress) + { + transferComplete(); + } + } + + public boolean abort() + { + completed = true; + transferComplete(); + return inProgress; + } + + public void transferComplete() + { + if (socket == null) + { + return; + } + if (in != null) + { + in.setTransferComplete(true); + } + if (out != null) + { + out.setTransferComplete(true); + } + completed = completed || (transferMode == FTPConnection.MODE_STREAM); + if (completed && socket != null) + { + try + { + socket.close(); + } + catch (IOException e) + { + } + try + { + server.close(); + } + catch (IOException e) + { + } + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/BlockInputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/BlockInputStream.java new file mode 100644 index 00000000000..63897f1d6db --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/BlockInputStream.java @@ -0,0 +1,150 @@ +/* BlockInputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.InputStream; + +/** + * A DTP input stream that implements the FTP block transfer mode. + * + * @author Chris Burdess (dog@gnu.org) + */ +class BlockInputStream + extends DTPInputStream +{ + + static final int EOF = 64; + + int descriptor; + int max = -1; + int count = -1; + + BlockInputStream(DTP dtp, InputStream in) + { + super(dtp, in); + } + + public int read() + throws IOException + { + if (transferComplete) + { + return -1; + } + if (count == -1) + { + readHeader(); + } + if (max < 1) + { + close(); + return -1; + } + int c = in.read(); + if (c == -1) + { + dtp.transferComplete(); + } + count++; + if (count >= max) + { + count = -1; + if (descriptor == EOF) + { + close(); + } + } + return c; + } + + public int read(byte[] buf) + throws IOException + { + return read(buf, 0, buf.length); + } + + public int read(byte[] buf, int off, int len) + throws IOException + { + if (transferComplete) + { + return -1; + } + if (count == -1) + { + readHeader(); + } + if (max < 1) + { + close(); + return -1; + } + int l = in.read(buf, off, len); + if (l == -1) + { + dtp.transferComplete(); + } + count += l; + if (count >= max) + { + count = -1; + if (descriptor == EOF) + { + close(); + } + } + return l; + } + + /** + * Reads the block header. + */ + void readHeader() + throws IOException + { + descriptor = in.read(); + int max_hi = in.read(); + int max_lo = in.read(); + max = (max_hi << 8) | max_lo; + count = 0; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/BlockOutputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/BlockOutputStream.java new file mode 100644 index 00000000000..85481c95bd5 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/BlockOutputStream.java @@ -0,0 +1,111 @@ +/* BlockOutputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * A DTP output stream that implements the FTP block transfer mode. + * + * @author Chris Burdess (dog@gnu.org) + */ +class BlockOutputStream + extends DTPOutputStream +{ + + static final byte RECORD = -128; // 0x80 + static final byte EOF = 64; // 0x40 + + BlockOutputStream(DTP dtp, OutputStream out) + { + super(dtp, out); + } + + public void write(int c) + throws IOException + { + if (transferComplete) + { + return; + } + byte[] buf = new byte[] + { + RECORD, /* record descriptor */ + 0x00, 0x01, /* one byte */ + (byte) c /* the byte */ + }; + out.write(buf, 0, 4); + } + + public void write(byte[] b) + throws IOException + { + write(b, 0, b.length); + } + + public void write(byte[] b, int off, int len) + throws IOException + { + if (transferComplete) + { + return; + } + byte[] buf = new byte[len + 3]; + buf[0] = RECORD; /* record descriptor */ + buf[1] = (byte) ((len & 0x00ff) >> 8); /* high byte of bytecount */ + buf[2] = (byte) (len & 0xff00); /* low byte of bytecount */ + System.arraycopy(b, off, buf, 3, len); + out.write(buf, 0, len); + } + + public void close() + throws IOException + { + byte[] buf = new byte[] + { + EOF, /* eof descriptor */ + 0x00, 0x00 /* no bytes */ + }; + out.write(buf, 0, 3); + super.close(); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/CompressedInputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/CompressedInputStream.java new file mode 100644 index 00000000000..f2e65b7d37e --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/CompressedInputStream.java @@ -0,0 +1,215 @@ +/* CompressedInputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.InputStream; +import java.net.ProtocolException; + +/** + * A DTP input stream that implements the FTP compressed transfer mode. + * + * @author Chris Burdess (dog@gnu.org) + */ +class CompressedInputStream + extends DTPInputStream +{ + + static final int EOF = 64; + + static final int RAW = 0x00; + static final int COMPRESSED = 0x80; + static final int FILLER = 0xc0; + + int descriptor; + int max = -1; + int count = -1; + + int state = RAW; // RAW | STATE | FILLER + int rep; // the compressed byte + int n = 0; // the number of compressed or raw bytes + + CompressedInputStream(DTP dtp, InputStream in) + { + super(dtp, in); + } + + public int read() + throws IOException + { + if (transferComplete) + { + return -1; + } + if (count == -1) + { + readHeader(); + } + if (max < 1) + { + close(); + return -1; + } + if (n > 0 && (state == COMPRESSED || state == FILLER)) + { + n--; + return rep; + } + int c = in.read(); + if (c == -1) + { + close(); + } + count++; + if (count >= max) + { + count = -1; + if (descriptor == EOF) + { + close(); + } + } + if (c == -1) + { + return c; + } + while (n == 0) // read code header + { + state = (c & 0xc0); + n = (c & 0x3f); + c = in.read(); + if (c == -1) + { + return -1; + } + } + switch (state) + { + case RAW: + break; + case COMPRESSED: + case FILLER: + rep = c; + break; + default: + throw new ProtocolException("Illegal state: " + state); + } + n--; + return c; + } + + public int read(byte[] buf) + throws IOException + { + return read(buf, 0, buf.length); + } + + public int read(byte[] buf, int off, int len) + throws IOException + { + if (transferComplete) + { + return -1; + } + if (count == -1) + { + readHeader(); + } + if (max < 1) + { + close(); + return -1; + } + // TODO improve performance + for (int i = off; i < len; i++) + { + int c = read(); + if (c == -1) + { + close(); + return i; + } + buf[i] = (byte) c; + } + return len; + /* + int l = in.read (buf, off, len); + if (l==-1) + { + close (); + } + count += l; + if (count>=max) + { + count = -1; + if (descriptor==EOF) + { + close (); + } + } + return l; + */ + } + + /** + * Reads the block header. + */ + void readHeader() + throws IOException + { + descriptor = in.read(); + int max_hi = in.read(); + int max_lo = in.read(); + max = (max_hi << 8) | max_lo; + count = 0; + } + + /** + * Reads the code header. + */ + void readCodeHeader() + throws IOException + { + int code = in.read(); + state = (code & 0xc0); + n = (code & 0x3f); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/CompressedOutputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/CompressedOutputStream.java new file mode 100644 index 00000000000..b960fb3afe8 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/CompressedOutputStream.java @@ -0,0 +1,228 @@ +/* CompressedOutputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * A DTP output stream that implements the FTP compressed transfer mode. + * + * @author Chris Burdess (dog@gnu.org) + */ +class CompressedOutputStream + extends DTPOutputStream +{ + + static final byte RECORD = -128; // 0x80 + static final byte EOF = 64; // 0x40 + + CompressedOutputStream(DTP dtp, OutputStream out) + { + super(dtp, out); + } + + /** + * Just one byte cannot be compressed. + * It takes 5 bytes to transmit - hardly very compressed! + */ + public void write(int c) + throws IOException + { + if (transferComplete) + { + return; + } + byte[] buf = new byte[] + { + RECORD, /* record descriptor */ + 0x00, 0x01, /* one byte */ + 0x01, /* one uncompressed byte */ + (byte) c /* the byte */ + }; + out.write(buf, 0, 5); + } + + public void write(byte[] b) + throws IOException + { + write(b, 0, b.length); + } + + /** + * The larger len is, the better. + */ + public void write(byte[] b, int off, int len) + throws IOException + { + if (transferComplete) + { + return; + } + byte[] buf = compress(b, off, len); + len = buf.length; + buf[0] = RECORD; /* record descriptor */ + buf[1] = (byte) ((len & 0x00ff) >> 8); /* high byte of bytecount */ + buf[2] = (byte) (len & 0xff00); /* low byte of bytecount */ + out.write(buf, 0, len); + } + + /** + * Returns the compressed form of the given byte array. + * The first 3 bytes are left free for header information. + */ + byte[] compress(byte[] b, int off, int len) + { + byte[] buf = new byte[len]; + byte last = 0; + int pos = 0, raw_count = 0, rep_count = 1; + for (int i = off; i < len; i++) + { + byte c = b[i]; + if (i > off && c == last) // compress + { + if (raw_count > 0) // flush raw bytes to buf + { + // need to add raw_count+1 bytes + if (pos + (raw_count + 1) > buf.length) + { + buf = realloc(buf, len); + } + pos = flush_raw(buf, pos, b, (i - raw_count) - 1, + raw_count); + raw_count = 0; + } + rep_count++; // keep looking for same byte + } + else + { + if (rep_count > 1) // flush compressed bytes to buf + { + // need to add 2 bytes + if (pos + 2 > buf.length) + { + buf = realloc(buf, len); + } + pos = flush_compressed(buf, pos, rep_count, last); + rep_count = 1; + } + raw_count++; // keep looking for raw bytes + } + if (rep_count == 127) // flush compressed bytes + { + // need to add 2 bytes + if (pos + 2 > buf.length) + { + buf = realloc(buf, len); + } + pos = flush_compressed(buf, pos, rep_count, last); + rep_count = 1; + } + if (raw_count == 127) // flush raw bytes + { + // need to add raw_count+1 bytes + if (pos + (raw_count + 1) > buf.length) + { + buf = realloc(buf, len); + } + pos = flush_raw(buf, pos, b, (i - raw_count), raw_count); + raw_count = 0; + } + last = c; + } + if (rep_count > 1) // flush compressed bytes + { + // need to add 2 bytes + if (pos + 2 > buf.length) + { + buf = realloc(buf, len); + } + pos = flush_compressed(buf, pos, rep_count, last); + rep_count = 1; + } + if (raw_count > 0) // flush raw bytes + { + // need to add raw_count+1 bytes + if (pos + (raw_count + 1) > buf.length) + { + buf = realloc(buf, len); + } + pos = flush_raw(buf, pos, b, (len - raw_count), raw_count); + raw_count = 0; + } + byte[] ret = new byte[pos + 3]; + System.arraycopy(buf, 0, ret, 3, pos); + return ret; + } + + int flush_compressed(byte[] buf, int pos, int count, byte c) + { + buf[pos++] = (byte) (0x80 | count); + buf[pos++] = c; + return pos; + } + + int flush_raw(byte[] buf, int pos, byte[] src, int off, int len) + { + buf[pos++] = (byte) len; + System.arraycopy(src, off, buf, pos, len); + return pos + len; + } + + byte[] realloc(byte[] buf, int len) + { + byte[] ret = new byte[buf.length + len]; + System.arraycopy(buf, 0, ret, 0, buf.length); + return ret; + } + + public void close() + throws IOException + { + byte[] buf = new byte[] + { + EOF, /* eof descriptor */ + 0x00, 0x00 /* no bytes */ + }; + out.write(buf, 0, 3); + out.close(); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/DTP.java b/libjava/classpath/gnu/java/net/protocol/ftp/DTP.java new file mode 100644 index 00000000000..25580af403a --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/DTP.java @@ -0,0 +1,92 @@ +/* DTP.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * An FTP data transfer process. + * + * @author Chris Burdess (dog@gnu.org) + */ +interface DTP +{ + + /** + * Returns an input stream from which a remote file can be read. + */ + InputStream getInputStream() + throws IOException; + + /** + * Returns an output stream to which a local file can be written for + * upload. + */ + OutputStream getOutputStream() + throws IOException; + + /** + * Sets the transfer mode to be used with this DTP. + */ + void setTransferMode(int mode); + + /** + * Marks this DTP completed. + * When the current transfer has finished, any resources will be released. + */ + void complete(); + + /** + * Aborts any current transfer and releases all resources held by this + * DTP. + * @return true if a transfer was interrupted, false otherwise + */ + boolean abort(); + + /** + * Used to notify the DTP that its current transfer is complete. + * This occurs either when end-of-stream is reached or a 226 response is + * received. + */ + void transferComplete(); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/DTPInputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/DTPInputStream.java new file mode 100644 index 00000000000..363a5590fbb --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/DTPInputStream.java @@ -0,0 +1,88 @@ +/* DTPInputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * An input stream that notifies a DTP on completion. + * + * @author Chris Burdess (dog@gnu.org) + */ +abstract class DTPInputStream + extends FilterInputStream +{ + + DTP dtp; + boolean transferComplete; + + /** + * Constructor. + * @param dtp the controlling data transfer process + * @param in the underlying socket stream + */ + DTPInputStream (DTP dtp, InputStream in) + { + super(in); + this.dtp = dtp; + transferComplete = false; + } + + /** + * Marks this input stream complete. + * This is called by the DTP. + */ + void setTransferComplete(boolean flag) + { + transferComplete = flag; + } + + /** + * Notifies the controlling DTP that this stream has completed transfer. + */ + public void close() + throws IOException + { + dtp.transferComplete(); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/DTPOutputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/DTPOutputStream.java new file mode 100644 index 00000000000..83f0be1e30d --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/DTPOutputStream.java @@ -0,0 +1,85 @@ +/* DTPOutputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * An output stream that notifies a DTP on end of stream. + * + * @author Chris Burdess (dog@gnu.org) + */ +abstract class DTPOutputStream extends FilterOutputStream +{ + + DTP dtp; + boolean transferComplete; + + /** + * Constructor. + * @param dtp the controlling data transfer process + * @param out the socket output stream + */ + DTPOutputStream (DTP dtp, OutputStream out) + { + super (out); + this.dtp = dtp; + transferComplete = false; + } + + /** + * Tells this stream whether transfer has completed or not. + * @param flag true if the process has completed, false otherwise + */ + void setTransferComplete (boolean flag) + { + transferComplete = flag; + } + + /** + * Notifies the controlling DTP that this stream has been terminated. + */ + public void close () throws IOException + { + dtp.transferComplete (); + } + +} diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/FTPConnection.java b/libjava/classpath/gnu/java/net/protocol/ftp/FTPConnection.java new file mode 100644 index 00000000000..d0f48727cfa --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/FTPConnection.java @@ -0,0 +1,1348 @@ +/* FTPConnection.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import gnu.java.net.CRLFInputStream; +import gnu.java.net.CRLFOutputStream; +import gnu.java.net.EmptyX509TrustManager; +import gnu.java.net.LineInputStream; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.BindException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ProtocolException; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.List; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; + +/** + * An FTP client connection, or PI. + * This implements RFC 959, with the following exceptions: + * <ul> + * <li>STAT, HELP, SITE, SMNT, and ACCT commands are not supported.</li> + * <li>the TYPE command does not allow alternatives to the default bytesize + * (Non-print), and local bytesize is not supported.</li> + * </ul> + * + * @author Chris Burdess (dog@gnu.org) + */ +public class FTPConnection +{ + + /** + * The default FTP transmission control port. + */ + public static final int FTP_PORT = 21; + + /** + * The FTP data port. + */ + public static final int FTP_DATA_PORT = 20; + + // -- FTP vocabulary -- + protected static final String USER = "USER"; + protected static final String PASS = "PASS"; + protected static final String ACCT = "ACCT"; + protected static final String CWD = "CWD"; + protected static final String CDUP = "CDUP"; + protected static final String SMNT = "SMNT"; + protected static final String REIN = "REIN"; + protected static final String QUIT = "QUIT"; + + protected static final String PORT = "PORT"; + protected static final String PASV = "PASV"; + protected static final String TYPE = "TYPE"; + protected static final String STRU = "STRU"; + protected static final String MODE = "MODE"; + + protected static final String RETR = "RETR"; + protected static final String STOR = "STOR"; + protected static final String STOU = "STOU"; + protected static final String APPE = "APPE"; + protected static final String ALLO = "ALLO"; + protected static final String REST = "REST"; + protected static final String RNFR = "RNFR"; + protected static final String RNTO = "RNTO"; + protected static final String ABOR = "ABOR"; + protected static final String DELE = "DELE"; + protected static final String RMD = "RMD"; + protected static final String MKD = "MKD"; + protected static final String PWD = "PWD"; + protected static final String LIST = "LIST"; + protected static final String NLST = "NLST"; + protected static final String SITE = "SITE"; + protected static final String SYST = "SYST"; + protected static final String STAT = "STAT"; + protected static final String HELP = "HELP"; + protected static final String NOOP = "NOOP"; + + protected static final String AUTH = "AUTH"; + protected static final String PBSZ = "PBSZ"; + protected static final String PROT = "PROT"; + protected static final String CCC = "CCC"; + protected static final String TLS = "TLS"; + + public static final int TYPE_ASCII = 1; + public static final int TYPE_EBCDIC = 2; + public static final int TYPE_BINARY = 3; + + public static final int STRUCTURE_FILE = 1; + public static final int STRUCTURE_RECORD = 2; + public static final int STRUCTURE_PAGE = 3; + + public static final int MODE_STREAM = 1; + public static final int MODE_BLOCK = 2; + public static final int MODE_COMPRESSED = 3; + + // -- Telnet constants -- + private static final String US_ASCII = "US-ASCII"; + + /** + * The socket used to communicate with the server. + */ + protected Socket socket; + + /** + * The socket input stream. + */ + protected LineInputStream in; + + /** + * The socket output stream. + */ + protected CRLFOutputStream out; + + /** + * The timeout when attempting to connect a socket. + */ + protected int connectionTimeout; + + /** + * The read timeout on sockets. + */ + protected int timeout; + + /** + * If true, print debugging information. + */ + protected boolean debug; + + /** + * The current data transfer process in use by this connection. + */ + protected DTP dtp; + + /** + * The current representation type. + */ + protected int representationType = TYPE_ASCII; + + /** + * The current file structure type. + */ + protected int fileStructure = STRUCTURE_FILE; + + /** + * The current transfer mode. + */ + protected int transferMode = MODE_STREAM; + + /** + * If true, use passive mode. + */ + protected boolean passive = false; + + /** + * Creates a new connection to the server using the default port. + * @param hostname the hostname of the server to connect to + */ + public FTPConnection(String hostname) + throws UnknownHostException, IOException + { + this(hostname, -1, 0, 0, false); + } + + /** + * Creates a new connection to the server. + * @param hostname the hostname of the server to connect to + * @param port the port to connect to(if <=0, use default port) + */ + public FTPConnection(String hostname, int port) + throws UnknownHostException, IOException + { + this(hostname, port, 0, 0, false); + } + + /** + * Creates a new connection to the server. + * @param hostname the hostname of the server to connect to + * @param port the port to connect to(if <=0, use default port) + * @param connectionTimeout the connection timeout, in milliseconds + * @param timeout the I/O timeout, in milliseconds + * @param debug print debugging information + */ + public FTPConnection(String hostname, int port, + int connectionTimeout, int timeout, boolean debug) + throws UnknownHostException, IOException + { + this.connectionTimeout = connectionTimeout; + this.timeout = timeout; + this.debug = debug; + if (port <= 0) + { + port = FTP_PORT; + } + + // Set up socket + socket = new Socket(); + InetSocketAddress address = new InetSocketAddress(hostname, port); + if (connectionTimeout > 0) + { + socket.connect(address, connectionTimeout); + } + else + { + socket.connect(address); + } + if (timeout > 0) + { + socket.setSoTimeout(timeout); + } + + InputStream in = socket.getInputStream(); + in = new BufferedInputStream(in); + in = new CRLFInputStream(in); + this.in = new LineInputStream(in); + OutputStream out = socket.getOutputStream(); + out = new BufferedOutputStream(out); + this.out = new CRLFOutputStream(out); + + // Read greeting + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 220: // hello + break; + default: + throw new FTPException(response); + } + } + + /** + * Authenticate using the specified username and password. + * If the username suffices for the server, the password will not be used + * and may be null. + * @param username the username + * @param password the optional password + * @return true on success, false otherwise + */ + public boolean authenticate(String username, String password) + throws IOException + { + String cmd = USER + ' ' + username; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 230: // User logged in + return true; + case 331: // User name okay, need password + break; + case 332: // Need account for login + case 530: // No such user + return false; + default: + throw new FTPException(response); + } + cmd = PASS + ' ' + password; + send(cmd); + response = getResponse(); + switch (response.getCode()) + { + case 230: // User logged in + case 202: // Superfluous + return true; + case 332: // Need account for login + case 530: // Bad password + return false; + default: + throw new FTPException(response); + } + } + + /** + * Negotiates TLS over the current connection. + * See IETF draft-murray-auth-ftp-ssl-15.txt for details. + * @param confidential whether to provide confidentiality for the + * connection + */ + public boolean starttls(boolean confidential) + throws IOException + { + return starttls(confidential, new EmptyX509TrustManager()); + } + + /** + * Negotiates TLS over the current connection. + * See IETF draft-murray-auth-ftp-ssl-15.txt for details. + * @param confidential whether to provide confidentiality for the + * connection + * @param tm the trust manager used to validate the server certificate. + */ + public boolean starttls(boolean confidential, TrustManager tm) + throws IOException + { + try + { + // Use SSLSocketFactory to negotiate a TLS session and wrap the + // current socket. + SSLContext context = SSLContext.getInstance("TLS"); + // We don't require strong validation of the server certificate + TrustManager[] trust = new TrustManager[] { tm }; + context.init(null, trust, null); + SSLSocketFactory factory = context.getSocketFactory(); + + send(AUTH + ' ' + TLS); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 500: + case 502: + case 504: + case 534: + case 431: + return false; + case 234: + break; + default: + throw new FTPException(response); + } + + String hostname = socket.getInetAddress().getHostName(); + int port = socket.getPort(); + SSLSocket ss = + (SSLSocket) factory.createSocket(socket, hostname, port, true); + String[] protocols = { "TLSv1", "SSLv3" }; + ss.setEnabledProtocols(protocols); + ss.setUseClientMode(true); + ss.startHandshake(); + + // PBSZ:PROT sequence + send(PBSZ + ' ' + Integer.MAX_VALUE); + response = getResponse(); + switch (response.getCode()) + { + case 501: // syntax error + case 503: // not authenticated + return false; + case 200: + break; + default: + throw new FTPException(response); + } + send(PROT + ' ' +(confidential ? 'P' : 'C')); + response = getResponse(); + switch (response.getCode()) + { + case 503: // not authenticated + case 504: // invalid level + case 536: // level not supported + return false; + case 200: + break; + default: + throw new FTPException(response); + } + + if (confidential) + { + // Set up streams + InputStream in = ss.getInputStream(); + in = new BufferedInputStream(in); + in = new CRLFInputStream(in); + this.in = new LineInputStream(in); + OutputStream out = ss.getOutputStream(); + out = new BufferedOutputStream(out); + this.out = new CRLFOutputStream(out); + } + return true; + } + catch (GeneralSecurityException e) + { + return false; + } + } + + /** + * Changes directory to the specified path. + * @param path an absolute or relative pathname + * @return true on success, false if the specified path does not exist + */ + public boolean changeWorkingDirectory(String path) + throws IOException + { + String cmd = CWD + ' ' + path; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 250: + return true; + case 550: + return false; + default: + throw new FTPException(response); + } + } + + /** + * Changes directory to the parent of the current working directory. + * @return true on success, false otherwise + */ + public boolean changeToParentDirectory() + throws IOException + { + send(CDUP); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 250: + return true; + case 550: + return false; + default: + throw new FTPException(response); + } + } + + /** + * Terminates an authenticated login. + * If file transfer is in progress, it remains active for result response + * only. + */ + public void reinitialize() + throws IOException + { + send(REIN); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 220: + if (dtp != null) + { + dtp.complete(); + dtp = null; + } + break; + default: + throw new FTPException(response); + } + } + + /** + * Terminates the control connection. + * The file transfer connection remains open for result response only. + * This connection is invalid and no further commands may be issued. + */ + public void logout() + throws IOException + { + send(QUIT); + try + { + getResponse(); // not required + } + catch (IOException e) + { + } + if (dtp != null) + { + dtp.complete(); + dtp = null; + } + try + { + socket.close(); + } + catch (IOException e) + { + } + } + + /** + * Initialise the data transfer process. + */ + protected void initialiseDTP() + throws IOException + { + if (dtp != null) + { + dtp.complete(); + dtp = null; + } + + InetAddress localhost = socket.getLocalAddress(); + if (passive) + { + send(PASV); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 227: + String message = response.getMessage(); + try + { + int start = message.indexOf(','); + char c = message.charAt(start - 1); + while (c >= 0x30 && c <= 0x39) + { + c = message.charAt((--start) - 1); + } + int mid1 = start; + for (int i = 0; i < 4; i++) + { + mid1 = message.indexOf(',', mid1 + 1); + } + int mid2 = message.indexOf(',', mid1 + 1); + if (mid1 == -1 || mid2 < mid1) + { + throw new ProtocolException("Malformed 227: " + + message); + } + int end = mid2; + c = message.charAt(end + 1); + while (c >= 0x30 && c <= 0x39) + { + c = message.charAt((++end) + 1); + } + + String address = + message.substring(start, mid1).replace(',', '.'); + int port_hi = + Integer.parseInt(message.substring(mid1 + 1, mid2)); + int port_lo = + Integer.parseInt(message.substring(mid2 + 1, end + 1)); + int port = (port_hi << 8) | port_lo; + + /*System.out.println("Entering passive mode: " + address + + ":" + port);*/ + dtp = new PassiveModeDTP(address, port, localhost, + connectionTimeout, timeout); + break; + } + catch (ArrayIndexOutOfBoundsException e) + { + throw new ProtocolException(e.getMessage() + ": " + + message); + } + catch (NumberFormatException e) + { + throw new ProtocolException(e.getMessage() + ": " + + message); + } + default: + throw new FTPException(response); + } + } + else + { + // Get the local port + int port = socket.getLocalPort() + 1; + int tries = 0; + // Bind the active mode DTP + while (dtp == null) + { + try + { + dtp = new ActiveModeDTP(localhost, port, + connectionTimeout, timeout); + /*System.out.println("Listening on: " + port);*/ + } + catch (BindException e) + { + port++; + tries++; + if (tries > 9) + { + throw e; + } + } + } + + // Send PORT command + StringBuffer buf = new StringBuffer(PORT); + buf.append(' '); + // Construct the address/port string form + byte[] address = localhost.getAddress(); + for (int i = 0; i < address.length; i++) + { + int a =(int) address[i]; + if (a < 0) + { + a += 0x100; + } + buf.append(a); + buf.append(','); + } + int port_hi =(port & 0xff00) >> 8; + int port_lo =(port & 0x00ff); + buf.append(port_hi); + buf.append(','); + buf.append(port_lo); + send(buf.toString()); + // Get response + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 200: // OK + break; + default: + dtp.abort(); + dtp = null; + throw new FTPException(response); + } + } + dtp.setTransferMode(transferMode); + } + + /** + * Set passive mode. + * @param flag true if we should use passive mode, false otherwise + */ + public void setPassive(boolean flag) + throws IOException + { + if (passive != flag) + { + passive = flag; + initialiseDTP(); + } + } + + /** + * Returns the current representation type of the transfer data. + * @return TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY + */ + public int getRepresentationType() + { + return representationType; + } + + /** + * Sets the desired representation type of the transfer data. + * @param type TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY + */ + public void setRepresentationType(int type) + throws IOException + { + StringBuffer buf = new StringBuffer(TYPE); + buf.append(' '); + switch (type) + { + case TYPE_ASCII: + buf.append('A'); + break; + case TYPE_EBCDIC: + buf.append('E'); + break; + case TYPE_BINARY: + buf.append('I'); + break; + default: + throw new IllegalArgumentException(Integer.toString(type)); + } + //buf.append(' '); + //buf.append('N'); + send(buf.toString()); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 200: + representationType = type; + break; + default: + throw new FTPException(response); + } + } + + /** + * Returns the current file structure type. + * @return STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE + */ + public int getFileStructure() + { + return fileStructure; + } + + /** + * Sets the desired file structure type. + * @param structure STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE + */ + public void setFileStructure(int structure) + throws IOException + { + StringBuffer buf = new StringBuffer(STRU); + buf.append(' '); + switch (structure) + { + case STRUCTURE_FILE: + buf.append('F'); + break; + case STRUCTURE_RECORD: + buf.append('R'); + break; + case STRUCTURE_PAGE: + buf.append('P'); + break; + default: + throw new IllegalArgumentException(Integer.toString(structure)); + } + send(buf.toString()); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 200: + fileStructure = structure; + break; + default: + throw new FTPException(response); + } + } + + /** + * Returns the current transfer mode. + * @return MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED + */ + public int getTransferMode() + { + return transferMode; + } + + /** + * Sets the desired transfer mode. + * @param mode MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED + */ + public void setTransferMode(int mode) + throws IOException + { + StringBuffer buf = new StringBuffer(MODE); + buf.append(' '); + switch (mode) + { + case MODE_STREAM: + buf.append('S'); + break; + case MODE_BLOCK: + buf.append('B'); + break; + case MODE_COMPRESSED: + buf.append('C'); + break; + default: + throw new IllegalArgumentException(Integer.toString(mode)); + } + send(buf.toString()); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 200: + transferMode = mode; + if (dtp != null) + { + dtp.setTransferMode(mode); + } + break; + default: + throw new FTPException(response); + } + } + + /** + * Retrieves the specified file. + * @param filename the filename of the file to retrieve + * @return an InputStream containing the file content + */ + public InputStream retrieve(String filename) + throws IOException + { + if (dtp == null || transferMode == MODE_STREAM) + { + initialiseDTP(); + } + /* + int size = -1; + String cmd = SIZE + ' ' + filename; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 213: + size = Integer.parseInt(response.getMessage()); + break; + case 550: // File not found + default: + throw new FTPException(response); + } + */ + String cmd = RETR + ' ' + filename; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 125: // Data connection already open; transfer starting + case 150: // File status okay; about to open data connection + return dtp.getInputStream(); + default: + throw new FTPException(response); + } + } + + /** + * Returns a stream for uploading a file. + * If a file with the same filename already exists on the server, it will + * be overwritten. + * @param filename the name of the file to save the content as + * @return an OutputStream to write the file data to + */ + public OutputStream store(String filename) + throws IOException + { + if (dtp == null || transferMode == MODE_STREAM) + { + initialiseDTP(); + } + String cmd = STOR + ' ' + filename; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 125: // Data connection already open; transfer starting + case 150: // File status okay; about to open data connection + return dtp.getOutputStream(); + default: + throw new FTPException(response); + } + } + + /** + * Returns a stream for uploading a file. + * If a file with the same filename already exists on the server, the + * content specified will be appended to the existing file. + * @param filename the name of the file to save the content as + * @return an OutputStream to write the file data to + */ + public OutputStream append(String filename) + throws IOException + { + if (dtp == null || transferMode == MODE_STREAM) + { + initialiseDTP(); + } + String cmd = APPE + ' ' + filename; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 125: // Data connection already open; transfer starting + case 150: // File status okay; about to open data connection + return dtp.getOutputStream(); + default: + throw new FTPException(response); + } + } + + /** + * This command may be required by some servers to reserve sufficient + * storage to accommodate the new file to be transferred. + * It should be immediately followed by a <code>store</code> or + * <code>append</code>. + * @param size the number of bytes of storage to allocate + */ + public void allocate(long size) + throws IOException + { + String cmd = ALLO + ' ' + size; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 200: // OK + case 202: // Superfluous + break; + default: + throw new FTPException(response); + } + } + + /** + * Renames a file. + * @param oldName the current name of the file + * @param newName the new name + * @return true if successful, false otherwise + */ + public boolean rename(String oldName, String newName) + throws IOException + { + String cmd = RNFR + ' ' + oldName; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 450: // File unavailable + case 550: // File not found + return false; + case 350: // Pending + break; + default: + throw new FTPException(response); + } + cmd = RNTO + ' ' + newName; + send(cmd); + response = getResponse(); + switch (response.getCode()) + { + case 250: // OK + return true; + case 450: + case 550: + return false; + default: + throw new FTPException(response); + } + } + + /** + * Aborts the transfer in progress. + * @return true if a transfer was in progress, false otherwise + */ + public boolean abort() + throws IOException + { + send(ABOR); + FTPResponse response = getResponse(); + // Abort client DTP + if (dtp != null) + { + dtp.abort(); + } + switch (response.getCode()) + { + case 226: // successful abort + return false; + case 426: // interrupted + response = getResponse(); + if (response.getCode() == 226) + { + return true; + } + // Otherwise fall through to throw exception + default: + throw new FTPException(response); + } + } + + /** + * Causes the file specified to be deleted at the server site. + * @param filename the file to delete + */ + public boolean delete(String filename) + throws IOException + { + String cmd = DELE + ' ' + filename; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 250: // OK + return true; + case 450: // File unavailable + case 550: // File not found + return false; + default: + throw new FTPException(response); + } + } + + /** + * Causes the directory specified to be deleted. + * This may be an absolute or relative pathname. + * @param pathname the directory to delete + */ + public boolean removeDirectory(String pathname) + throws IOException + { + String cmd = RMD + ' ' + pathname; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 250: // OK + return true; + case 550: // File not found + return false; + default: + throw new FTPException(response); + } + } + + /** + * Causes the directory specified to be created at the server site. + * This may be an absolute or relative pathname. + * @param pathname the directory to create + */ + public boolean makeDirectory(String pathname) + throws IOException + { + String cmd = MKD + ' ' + pathname; + send(cmd); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 257: // Directory created + return true; + case 550: // File not found + return false; + default: + throw new FTPException(response); + } + } + + /** + * Returns the current working directory. + */ + public String getWorkingDirectory() + throws IOException + { + send(PWD); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 257: + String message = response.getMessage(); + if (message.charAt(0) == '"') + { + int end = message.indexOf('"', 1); + if (end == -1) + { + throw new ProtocolException(message); + } + return message.substring(1, end); + } + else + { + int end = message.indexOf(' '); + if (end == -1) + { + return message; + } + else + { + return message.substring(0, end); + } + } + default: + throw new FTPException(response); + } + } + + /** + * Returns a listing of information about the specified pathname. + * If the pathname specifies a directory or other group of files, the + * server should transfer a list of files in the specified directory. + * If the pathname specifies a file then the server should send current + * information on the file. A null argument implies the user's + * current working or default directory. + * @param pathname the context pathname, or null + */ + public InputStream list(String pathname) + throws IOException + { + if (dtp == null || transferMode == MODE_STREAM) + { + initialiseDTP(); + } + if (pathname == null) + { + send(LIST); + } + else + { + String cmd = LIST + ' ' + pathname; + send(cmd); + } + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 125: // Data connection already open; transfer starting + case 150: // File status okay; about to open data connection + return dtp.getInputStream(); + default: + throw new FTPException(response); + } + } + + /** + * Returns a directory listing. The pathname should specify a + * directory or other system-specific file group descriptor; a null + * argument implies the user's current working or default directory. + * @param pathname the directory pathname, or null + * @return a list of filenames(strings) + */ + public List nameList(String pathname) + throws IOException + { + if (dtp == null || transferMode == MODE_STREAM) + { + initialiseDTP(); + } + if (pathname == null) + { + send(NLST); + } + else + { + String cmd = NLST + ' ' + pathname; + send(cmd); + } + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 125: // Data connection already open; transfer starting + case 150: // File status okay; about to open data connection + InputStream in = dtp.getInputStream(); + in = new BufferedInputStream(in); + in = new CRLFInputStream(in); // TODO ensure that TYPE is correct + LineInputStream li = new LineInputStream(in); + List ret = new ArrayList(); + for (String line = li.readLine(); + line != null; + line = li.readLine()) + { + ret.add(line); + } + li.close(); + return ret; + default: + throw new FTPException(response); + } + } + + /** + * Returns the type of operating system at the server. + */ + public String system() + throws IOException + { + send(SYST); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 215: + String message = response.getMessage(); + int end = message.indexOf(' '); + if (end == -1) + { + return message; + } + else + { + return message.substring(0, end); + } + default: + throw new FTPException(response); + } + } + + /** + * Does nothing. + * This method can be used to ensure that the connection does not time + * out. + */ + public void noop() + throws IOException + { + send(NOOP); + FTPResponse response = getResponse(); + switch (response.getCode()) + { + case 200: + break; + default: + throw new FTPException(response); + } + } + + // -- I/O -- + + /** + * Sends the specified command line to the server. + * The CRLF sequence is automatically appended. + * @param cmd the command line to send + */ + protected void send(String cmd) + throws IOException + { + byte[] data = cmd.getBytes(US_ASCII); + out.write(data); + out.writeln(); + out.flush(); + } + + /** + * Reads the next response from the server. + * If the server sends the "transfer complete" code, this is handled here, + * and the next response is passed to the caller. + */ + protected FTPResponse getResponse() + throws IOException + { + FTPResponse response = readResponse(); + if (response.getCode() == 226) + { + if (dtp != null) + { + dtp.transferComplete(); + } + response = readResponse(); + } + return response; + } + + /** + * Reads and parses the next response from the server. + */ + protected FTPResponse readResponse() + throws IOException + { + String line = in.readLine(); + if (line == null) + { + throw new ProtocolException( "EOF"); + } + if (line.length() < 4) + { + throw new ProtocolException(line); + } + int code = parseCode(line); + if (code == -1) + { + throw new ProtocolException(line); + } + char c = line.charAt(3); + if (c == ' ') + { + return new FTPResponse(code, line.substring(4)); + } + else if (c == '-') + { + StringBuffer buf = new StringBuffer(line.substring(4)); + buf.append('\n'); + while(true) + { + line = in.readLine(); + if (line == null) + { + throw new ProtocolException("EOF"); + } + if (line.length() >= 4 && + line.charAt(3) == ' ' && + parseCode(line) == code) + { + return new FTPResponse(code, line.substring(4), + buf.toString()); + } + else + { + buf.append(line); + buf.append('\n'); + } + } + } + else + { + throw new ProtocolException(line); + } + } + + /* + * Parses the 3-digit numeric code at the beginning of the given line. + * Returns -1 on failure. + */ + static final int parseCode(String line) + { + char[] c = { line.charAt(0), line.charAt(1), line.charAt(2) }; + int ret = 0; + for (int i = 0; i < 3; i++) + { + int digit =((int) c[i]) - 0x30; + if (digit < 0 || digit > 9) + { + return -1; + } + // Computing integer powers is way too expensive in Java! + switch (i) + { + case 0: + ret +=(100 * digit); + break; + case 1: + ret +=(10 * digit); + break; + case 2: + ret += digit; + break; + } + } + return ret; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/FTPException.java b/libjava/classpath/gnu/java/net/protocol/ftp/FTPException.java new file mode 100644 index 00000000000..14ad3813f85 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/FTPException.java @@ -0,0 +1,76 @@ +/* FTPException.java -- + Copyright (C) 2003. 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; + +/** + * An FTP control exception. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class FTPException + extends IOException +{ + + /** + * The response that provoked this exception. + */ + protected final FTPResponse response; + + /** + * Constructs a new FTP exception. + * @param response the response that provoked this exception + */ + public FTPException(FTPResponse response) + { + super(response.getMessage()); + this.response = response; + } + + /** + * Returns the response that provoked this exception. + */ + public FTPResponse getResponse() + { + return response; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/FTPResponse.java b/libjava/classpath/gnu/java/net/protocol/ftp/FTPResponse.java new file mode 100644 index 00000000000..ec72c732c1c --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/FTPResponse.java @@ -0,0 +1,112 @@ +/* FTPResponse.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +/** + * An FTP control response. + * + * @author Chris Burdess (dog@gnu.org) + */ +public final class FTPResponse +{ + + /** + * The 3-digit status code. + */ + protected final int code; + + /** + * The human-readable message. + */ + protected final String message; + + /** + * Multiline data, if present. + */ + protected final String data; + + /** + * Constructs a new FTP response. + * @param code the status code + * @param message the message + */ + public FTPResponse(int code, String message) + { + this(code, message, null); + } + + /** + * Constructs a new multiline FTP response. + * @param code the status code + * @param message the message + * @param data multiline data + */ + public FTPResponse(int code, String message, String data) + { + this.code = code; + this.message = message; + this.data = data; + } + + /** + * Returns the 3-digit status code. + */ + public int getCode() + { + return code; + } + + /** + * Returns the human-readable message. + */ + public String getMessage() + { + return message; + } + + /** + * Returns the multiline data, or null if there was no such data. + */ + public String getData() + { + return data; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/FTPURLConnection.java b/libjava/classpath/gnu/java/net/protocol/ftp/FTPURLConnection.java new file mode 100644 index 00000000000..62c40f19e04 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/FTPURLConnection.java @@ -0,0 +1,398 @@ +/* FTPURLConnection.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import gnu.java.net.GetLocalHostAction; +import gnu.java.security.action.GetPropertyAction; + +import java.io.FileNotFoundException; +import java.io.FilterInputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.URL; +import java.net.URLConnection; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.HashMap; +import java.util.Map; + +/** + * An FTP URL connection. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class FTPURLConnection + extends URLConnection +{ + + /** + * The connection managing the protocol exchange. + */ + protected FTPConnection connection; + + protected boolean passive; + protected int representationType; + protected int fileStructure; + protected int transferMode; + + /** + * Constructs an FTP connection to the specified URL. + * @param url the URL + */ + public FTPURLConnection(URL url) + { + super(url); + passive = true; + representationType = FTPConnection.TYPE_BINARY; + fileStructure = -1; + transferMode = -1; + } + + /** + * Establishes the connection. + */ + public void connect() + throws IOException + { + if (connected) + { + return; + } + String host = url.getHost(); + int port = url.getPort(); + String username = url.getUserInfo(); + String password = null; + if (username != null) + { + int ci = username.indexOf(':'); + if (ci != -1) + { + password = username.substring(ci + 1); + username = username.substring(0, ci); + } + } + else + { + username = "anonymous"; + PrivilegedAction a = new GetPropertyAction("user.name"); + String systemUsername =(String) AccessController.doPrivileged(a); + a = new GetLocalHostAction(); + InetAddress localhost =(InetAddress) AccessController.doPrivileged(a); + password = systemUsername + "@" + + ((localhost == null) ? "localhost" : localhost.getHostName()); + } + connection = new FTPConnection(host, port); + if (!connection.authenticate(username, password)) + { + throw new SecurityException("Authentication failed"); + } + connection.setPassive(passive); + if (representationType != -1) + { + connection.setRepresentationType(representationType); + } + if (fileStructure != -1) + { + connection.setFileStructure(fileStructure); + } + if (transferMode != -1) + { + connection.setTransferMode(transferMode); + } + } + + /** + * This connection supports doInput. + */ + public void setDoInput(boolean doinput) + { + doInput = doinput; + } + + /** + * This connection supports doOutput. + */ + public void setDoOutput(boolean dooutput) + { + doOutput = dooutput; + } + + /** + * Returns an input stream that reads from this open connection. + */ + public InputStream getInputStream() + throws IOException + { + if (!connected) + { + connect(); + } + String path = url.getPath(); + String filename = null; + int lsi = path.lastIndexOf('/'); + if (lsi != -1) + { + filename = path.substring(lsi + 1); + path = path.substring(0, lsi); + if (!connection.changeWorkingDirectory(path)) + { + throw new FileNotFoundException(path); + } + } + if (filename != null && filename.length() > 0) + { + return this.new ClosingInputStream(connection.retrieve(filename)); + } + else + { + return this.new ClosingInputStream(connection.list(null)); + } + } + + /** + * Returns an output stream that writes to this connection. + */ + public OutputStream getOutputStream() + throws IOException + { + if (!connected) + { + connect(); + } + String dir = url.getPath(); + String filename = url.getFile(); + if (!connection.changeWorkingDirectory(dir)) + { + throw new FileNotFoundException(dir); + } + if (filename != null) + { + return this.new ClosingOutputStream(connection.store(filename)); + } + else + { + throw new FileNotFoundException(filename); + } + } + + public String getRequestProperty(String key) + { + if ("passive".equals(key)) + { + return Boolean.toString(passive); + } + else if ("representationType".equals(key)) + { + switch (representationType) + { + case FTPConnection.TYPE_ASCII: + return "ASCII"; + case FTPConnection.TYPE_EBCDIC: + return "EBCDIC"; + case FTPConnection.TYPE_BINARY: + return "BINARY"; + } + } + else if ("fileStructure".equals(key)) + { + switch (fileStructure) + { + case FTPConnection.STRUCTURE_FILE: + return "FILE"; + case FTPConnection.STRUCTURE_RECORD: + return "RECORD"; + case FTPConnection.STRUCTURE_PAGE: + return "PAGE"; + } + } + else if ("transferMode".equals(key)) + { + switch (transferMode) + { + case FTPConnection.MODE_STREAM: + return "STREAM"; + case FTPConnection.MODE_BLOCK: + return "BLOCK"; + case FTPConnection.MODE_COMPRESSED: + return "COMPRESSED"; + } + } + return null; + } + + public Map getRequestProperties() + { + Map map = new HashMap(); + addRequestPropertyValue(map, "passive"); + addRequestPropertyValue(map, "representationType"); + addRequestPropertyValue(map, "fileStructure"); + addRequestPropertyValue(map, "transferMode"); + return map; + } + + private void addRequestPropertyValue(Map map, String key) + { + String value = getRequestProperty(key); + map.put(key, value); + } + + public void setRequestProperty(String key, String value) + { + if (connected) + { + throw new IllegalStateException(); + } + if ("passive".equals(key)) + { + passive = Boolean.valueOf(value).booleanValue(); + } + else if ("representationType".equals(key)) + { + if ("A".equalsIgnoreCase(value) || + "ASCII".equalsIgnoreCase(value)) + { + representationType = FTPConnection.TYPE_ASCII; + } + else if ("E".equalsIgnoreCase(value) || + "EBCDIC".equalsIgnoreCase(value)) + { + representationType = FTPConnection.TYPE_EBCDIC; + } + else if ("I".equalsIgnoreCase(value) || + "BINARY".equalsIgnoreCase(value)) + { + representationType = FTPConnection.TYPE_BINARY; + } + else + { + throw new IllegalArgumentException(value); + } + } + else if ("fileStructure".equals(key)) + { + if ("F".equalsIgnoreCase(value) || + "FILE".equalsIgnoreCase(value)) + { + fileStructure = FTPConnection.STRUCTURE_FILE; + } + else if ("R".equalsIgnoreCase(value) || + "RECORD".equalsIgnoreCase(value)) + { + fileStructure = FTPConnection.STRUCTURE_RECORD; + } + else if ("P".equalsIgnoreCase(value) || + "PAGE".equalsIgnoreCase(value)) + { + fileStructure = FTPConnection.STRUCTURE_PAGE; + } + else + { + throw new IllegalArgumentException(value); + } + } + else if ("transferMode".equals(key)) + { + if ("S".equalsIgnoreCase(value) || + "STREAM".equalsIgnoreCase(value)) + { + transferMode = FTPConnection.MODE_STREAM; + } + else if ("B".equalsIgnoreCase(value) || + "BLOCK".equalsIgnoreCase(value)) + { + transferMode = FTPConnection.MODE_BLOCK; + } + else if ("C".equalsIgnoreCase(value) || + "COMPRESSED".equalsIgnoreCase(value)) + { + transferMode = FTPConnection.MODE_COMPRESSED; + } + else + { + throw new IllegalArgumentException(value); + } + } + } + + public void addRequestProperty(String key, String value) + { + setRequestProperty(key, value); + } + + class ClosingInputStream + extends FilterInputStream + { + + ClosingInputStream(InputStream in) + { + super(in); + } + + public void close() + throws IOException + { + super.close(); + connection.logout(); + } + + } + + class ClosingOutputStream + extends FilterOutputStream + { + + ClosingOutputStream(OutputStream out) + { + super(out); + } + + public void close() + throws IOException + { + super.close(); + connection.logout(); + } + + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/Handler.java b/libjava/classpath/gnu/java/net/protocol/ftp/Handler.java new file mode 100644 index 00000000000..88491b3c15a --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/Handler.java @@ -0,0 +1,70 @@ +/* Handler.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * An FTP URL stream handler. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Handler + extends URLStreamHandler +{ + + protected int getDefaultPort() + { + return FTPConnection.FTP_PORT; + } + + /** + * Returns an FTPURLConnection for the given URL. + */ + public URLConnection openConnection(URL url) + throws IOException + { + return new FTPURLConnection(url); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/PassiveModeDTP.java b/libjava/classpath/gnu/java/net/protocol/ftp/PassiveModeDTP.java new file mode 100644 index 00000000000..6f4fd634168 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/PassiveModeDTP.java @@ -0,0 +1,201 @@ +/* PassiveModeDTP.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; + +/** + * A passive mode FTP data transfer process. + * This connects to the host specified and proxies the resulting socket's + * input and output streams. + * + * @author Chris Burdess (dog@gnu.org) + */ +final class PassiveModeDTP + implements DTP +{ + + final String address; + final int port; + Socket socket; + DTPInputStream in; + DTPOutputStream out; + boolean completed; + boolean inProgress; + int transferMode; + + PassiveModeDTP(String address, int port, InetAddress localhost, + int connectionTimeout, int timeout) + throws IOException + { + this.address = address; + this.port = port; + completed = false; + inProgress = false; + socket = new Socket(); + InetSocketAddress remote = new InetSocketAddress(address, port); + InetSocketAddress local = new InetSocketAddress(localhost, port + 1); + socket.bind(local); + if (connectionTimeout > 0) + { + socket.connect(remote, connectionTimeout); + } + else + { + socket.connect(remote); + } + if (timeout > 0) + { + socket.setSoTimeout(timeout); + } + } + + /** + * Returns an input stream from which a remote file can be read. + */ + public InputStream getInputStream() + throws IOException + { + if (inProgress) + { + throw new IOException("Transfer in progress"); + } + switch (transferMode) + { + case FTPConnection.MODE_STREAM: + in = new StreamInputStream(this, socket.getInputStream()); + break; + case FTPConnection.MODE_BLOCK: + in = new BlockInputStream(this, socket.getInputStream()); + break; + case FTPConnection.MODE_COMPRESSED: + in = new CompressedInputStream(this, socket.getInputStream()); + break; + default: + throw new IllegalStateException("Invalid transfer mode"); + } + in.setTransferComplete(false); + return in; + } + + /** + * Returns an output stream to which a local file can be written for + * upload. + */ + public OutputStream getOutputStream() + throws IOException + { + if (inProgress) + { + throw new IOException("Transfer in progress"); + } + switch (transferMode) + { + case FTPConnection.MODE_STREAM: + out = new StreamOutputStream(this, socket.getOutputStream()); + break; + case FTPConnection.MODE_BLOCK: + out = new BlockOutputStream(this, socket.getOutputStream()); + break; + case FTPConnection.MODE_COMPRESSED: + out = new CompressedOutputStream(this, socket.getOutputStream()); + break; + default: + throw new IllegalStateException("Invalid transfer mode"); + } + out.setTransferComplete(false); + return out; + } + + public void setTransferMode(int mode) + { + transferMode = mode; + } + + public void complete() + { + completed = true; + if (!inProgress) + { + transferComplete(); + } + } + + public boolean abort() + { + completed = true; + transferComplete(); + return inProgress; + } + + /* + * Called by DTPInputStream or DTPOutputStream when end of + * stream is reached. + */ + public void transferComplete() + { + if (in != null) + { + in.setTransferComplete(true); + } + if (out != null) + { + out.setTransferComplete(true); + } + inProgress = false; + completed = completed ||(transferMode == FTPConnection.MODE_STREAM); + if (completed && socket != null) + { + try + { + socket.close(); + } + catch (IOException e) + { + } + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/StreamInputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/StreamInputStream.java new file mode 100644 index 00000000000..93eee4e1924 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/StreamInputStream.java @@ -0,0 +1,95 @@ +/* StreamInputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.InputStream; + +/** + * A DTP input stream that implements the FTP stream data transfer mode. + * + * @author Chris Burdess (dog@gnu.org) + */ +class StreamInputStream + extends DTPInputStream +{ + + StreamInputStream(DTP dtp, InputStream in) + { + super(dtp, in); + } + + public int read() + throws IOException + { + if (transferComplete) + { + return -1; + } + int c = in.read(); + if (c == -1) + { + close(); + } + return c; + } + + public int read(byte[] buf) + throws IOException + { + return read(buf, 0, buf.length); + } + + public int read(byte[] buf, int off, int len) + throws IOException + { + if (transferComplete) + { + return -1; + } + int l = in.read(buf, off, len); + if (l == -1) + { + close(); + } + return l; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/StreamOutputStream.java b/libjava/classpath/gnu/java/net/protocol/ftp/StreamOutputStream.java new file mode 100644 index 00000000000..a6e28ece3d4 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/StreamOutputStream.java @@ -0,0 +1,85 @@ +/* StreamOutputStream.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.ftp; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * A DTP output stream that implements the FTP stream transfer mode. + * + * @author Chris Burdess (dog@gnu.org) + */ +class StreamOutputStream + extends DTPOutputStream +{ + + StreamOutputStream(DTP dtp, OutputStream out) + { + super(dtp, out); + } + + public void write(int c) + throws IOException + { + if (transferComplete) + { + return; + } + out.write(c); + } + + public void write(byte[] b) + throws IOException + { + write(b, 0, b.length); + } + + public void write(byte[] b, int off, int len) + throws IOException + { + if (transferComplete) + { + return; + } + out.write(b, off, len); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/ftp/package.html b/libjava/classpath/gnu/java/net/protocol/ftp/package.html new file mode 100644 index 00000000000..fa3e34d7488 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/ftp/package.html @@ -0,0 +1,60 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.net.protocol.ftp package. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.net.protocol.ftp</title></head> + +<body> + +<p> +This package contains an FTP client. It can handle both active and passive +mode connections and the various transfer modes and representation types. +</p> + +<p> +Interaction with the server is via a simple stream interface. Only one +concurrent stream (input or output) is supported. +</p> + +<p> +The control connection to the server can be protected using TLS +(the starttls method). +</p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/net/protocol/http/Authenticator.java b/libjava/classpath/gnu/java/net/protocol/http/Authenticator.java new file mode 100644 index 00000000000..0d7c9881956 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Authenticator.java @@ -0,0 +1,59 @@ +/* Authenticator.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Callback interface for managing authentication. + * @see Request#setAuthenticator + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface Authenticator +{ + + /** + * Returns the credentials to supply for the given realm. + * @param realm the authentication realm + * @param attempt zero on first authentication attempt, increments on each + * unsuccessful attempt + */ + Credentials getCredentials(String realm, int attempt); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/ByteArrayRequestBodyWriter.java b/libjava/classpath/gnu/java/net/protocol/http/ByteArrayRequestBodyWriter.java new file mode 100644 index 00000000000..35ad2bccf45 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/ByteArrayRequestBodyWriter.java @@ -0,0 +1,107 @@ +/* ByteArrayRequestBodyWriter.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * A simple request body writer using a byte array. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class ByteArrayRequestBodyWriter + implements RequestBodyWriter +{ + + /** + * The content. + */ + protected byte[] content; + + /** + * The position within the content at which the next read will occur. + */ + protected int pos; + + /** + * Constructs a new byte array request body writer with the specified + * content. + * @param content the content buffer + */ + public ByteArrayRequestBodyWriter(byte[] content) + { + this.content = content; + pos = 0; + } + + /** + * Returns the total number of bytes that will be written in a single pass + * by this writer. + */ + public int getContentLength() + { + return content.length; + } + + /** + * Initialises the writer. + * This will be called before each pass. + */ + public void reset() + { + pos = 0; + } + + /** + * Writes body content to the supplied buffer. + * @param buffer the content buffer + * @return the number of bytes written + */ + public int write(byte[] buffer) + { + int len = content.length - pos; + len = (buffer.length < len) ? buffer.length : len; + if (len > -1) + { + System.arraycopy(content, pos, buffer, 0, len); + pos += len; + } + return len; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java b/libjava/classpath/gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java new file mode 100644 index 00000000000..680e45d3e49 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java @@ -0,0 +1,123 @@ +/* Authenticator.java --ByteArrayResponseBodyReader.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Simple response body reader that stores content in a byte array. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class ByteArrayResponseBodyReader + implements ResponseBodyReader +{ + + /** + * The content. + */ + protected byte[] content; + + /** + * The position in the content at which the next write will occur. + */ + protected int pos; + + /** + * The length of the buffer. + */ + protected int len; + + /** + * Constructs a new byte array response body reader. + */ + public ByteArrayResponseBodyReader() + { + this(4096); + } + + /** + * Constructs a new byte array response body reader with the specified + * initial buffer size. + * @param size the initial buffer size + */ + public ByteArrayResponseBodyReader(int size) + { + content = new byte[size]; + pos = len = 0; + } + + /** + * This reader accepts all responses. + */ + public boolean accept(Request request, Response response) + { + return true; + } + + public void read(byte[] buffer, int offset, int length) + { + int l = length - offset; + if (pos + l > content.length) + { + byte[] tmp = new byte[content.length * 2]; + System.arraycopy(content, 0, tmp, 0, pos); + content = tmp; + } + System.arraycopy(buffer, offset, content, pos, l); + pos += l; + len = pos; + } + + public void close() + { + pos = 0; + } + + /** + * Retrieves the content of this reader as a byte array. + * The size of the returned array is the number of bytes read. + */ + public byte[] toByteArray() + { + byte[] ret = new byte[len]; + System.arraycopy(content, 0, ret, 0, len); + return ret; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/ChunkedInputStream.java b/libjava/classpath/gnu/java/net/protocol/http/ChunkedInputStream.java new file mode 100644 index 00000000000..c0706b70840 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/ChunkedInputStream.java @@ -0,0 +1,172 @@ +/* ChunkedInputStream.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.ProtocolException; + +/** + * Input stream wrapper for the "chunked" transfer-coding. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class ChunkedInputStream + extends FilterInputStream +{ + + private static final byte CR = 0x0d; + private static final byte LF = 0x0a; + + int size; + int count; + boolean meta; + boolean eof; + Headers headers; + + /** + * Constructor. + * @param in the response socket input stream + * @param headers the headers to receive additional header lines + */ + public ChunkedInputStream(InputStream in, Headers headers) + { + super(in); + this.headers = headers; + size = -1; + count = 0; + meta = true; + } + + public int read() + throws IOException + { + byte[] buf = new byte[1]; + int len = read(buf, 0, 1); + if (len == -1) + { + return -1; + } + int ret = (int) buf[0]; + if (ret < 0) + { + ret += 0x100; + } + return ret; + } + + public int read(byte[] buffer) + throws IOException + { + return read(buffer, 0, buffer.length); + } + + public int read(byte[] buffer, int offset, int length) + throws IOException + { + if (eof) + { + return -1; + } + if (meta) + { + // Read chunk header + int c, last = 0; + boolean seenSemi = false; + StringBuffer buf = new StringBuffer(); + do + { + c = in.read(); + if (c == 0x3b) // ; + { + seenSemi = true; + } + else if (c == 0x0a && last == 0x0d) // CRLF + { + size = Integer.parseInt(buf.toString(), 16); + break; + } + else if (!seenSemi && c >= 0x30) + { + buf.append ((char) c); + } + last = c; + } + while(c != -1); + count = 0; + meta = false; + } + if (size == 0) + { + // Read trailer + headers.parse(in); + eof = true; + return -1; + } + else + { + int diff = length - offset; + int max = size - count; + max = (diff < max) ? diff : max; + int len = (max > 0) ? in.read(buffer, offset, max) : 0; + count += len; + if (count == size) + { + // Read CRLF + int c1 = in.read(); + int c2 = in.read(); + if (c1 == -1 && c2 == -1) + { + // EOF before CRLF: bad, but ignore + eof = true; + return -1; + } + if (c1 != 0x0d || c2 != 0x0a) + { + throw new ProtocolException("expecting CRLF: " + c1 + "," + c2); + } + meta = true; + } + return len; + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/Cookie.java b/libjava/classpath/gnu/java/net/protocol/http/Cookie.java new file mode 100644 index 00000000000..45e2f733ff3 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Cookie.java @@ -0,0 +1,160 @@ +/* Cookie.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.util.Date; + +/** + * An HTTP cookie, as specified in RFC 2109. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Cookie +{ + + /** + * The name of the cookie. + */ + protected final String name; + + /** + * The value of the cookie. + */ + protected final String value; + + /** + * Optional documentation of the intended use of the cookie. + */ + protected final String comment; + + /** + * The domain for which the cookie is valid. + */ + protected final String domain; + + /** + * Optional subset of URL paths within the domain for which the cookie is + * valid. + */ + protected final String path; + + /** + * Indicates that the user-agent should only use secure means to transmit + * this cookie to the server. + */ + protected final boolean secure; + + /** + * The date at which this cookie expires. + */ + protected final Date expires; + + public Cookie(String name, String value, String comment, String domain, + String path, boolean secure, Date expires) + { + this.name = name; + this.value = value; + this.comment = comment; + this.domain = domain; + this.path = path; + this.secure = secure; + this.expires = expires; + } + + public String getName() + { + return name; + } + + public String getValue() + { + return value; + } + + public String getComment() + { + return comment; + } + + public String getDomain() + { + return domain; + } + + public String getPath() + { + return path; + } + + public boolean isSecure() + { + return secure; + } + + public Date getExpiryDate() + { + return expires; + } + + public String toString() + { + return toString(true, true); + } + + public String toString(boolean showPath, boolean showDomain) + { + StringBuffer buf = new StringBuffer(); + buf.append(name); + buf.append('='); + buf.append(value); + if (showPath) + { + buf.append("; $Path="); + buf.append(path); + } + if (showDomain) + { + buf.append("; $Domain="); + buf.append(domain); + } + return buf.toString(); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/CookieManager.java b/libjava/classpath/gnu/java/net/protocol/http/CookieManager.java new file mode 100644 index 00000000000..cc1225c497d --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/CookieManager.java @@ -0,0 +1,65 @@ +/* CookieManager.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Cookie manager interface. + * If an application wants to handle cookies, they should implement this + * interface and register the instance with each HTTPConnection they use. + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface CookieManager +{ + + /** + * Stores a cookie in the cookie manager. + * @param cookie the cookie to store + */ + void setCookie(Cookie cookie); + + /** + * Retrieves the cookies matching the specified criteria. + * @param host the host name + * @param secure whether the connection is secure + * @param path the path to access + */ + Cookie[] getCookies(String host, boolean secure, String path); + +} diff --git a/libjava/classpath/gnu/java/net/protocol/http/Credentials.java b/libjava/classpath/gnu/java/net/protocol/http/Credentials.java new file mode 100644 index 00000000000..9e5fcd172f5 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Credentials.java @@ -0,0 +1,88 @@ +/* Credentials.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Represents a username/password combination that can be used to + * authenticate to an HTTP server. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Credentials +{ + + /** + * The username. + */ + private String username; + + /** + * The password. + */ + private String password; + + /** + * Constructor. + * @param username the username + * @param password the password + */ + public Credentials(String username, String password) + { + this.username = username; + this.password = password; + } + + /** + * Returns the username. + */ + public String getUsername() + { + return username; + } + + /** + * Returns the password. + */ + public String getPassword() + { + return password; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/HTTPConnection.java b/libjava/classpath/gnu/java/net/protocol/http/HTTPConnection.java new file mode 100644 index 00000000000..6d9f447a2ac --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/HTTPConnection.java @@ -0,0 +1,681 @@ +/* HTTPConnection.java -- + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import gnu.classpath.Configuration; +import gnu.classpath.SystemProperties; +import gnu.java.net.EmptyX509TrustManager; +import gnu.java.net.protocol.http.event.ConnectionEvent; +import gnu.java.net.protocol.http.event.ConnectionListener; +import gnu.java.net.protocol.http.event.RequestEvent; +import gnu.java.net.protocol.http.event.RequestListener; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.net.ssl.HandshakeCompletedListener; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; + +/** + * A connection to an HTTP server. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class HTTPConnection +{ + + /** + * The default HTTP port. + */ + public static final int HTTP_PORT = 80; + + /** + * The default HTTPS port. + */ + public static final int HTTPS_PORT = 443; + + private static final String userAgent = SystemProperties.getProperty("http.agent"); + + /** + * The host name of the server to connect to. + */ + protected final String hostname; + + /** + * The port to connect to. + */ + protected final int port; + + /** + * Whether the connection should use transport level security (HTTPS). + */ + protected final boolean secure; + + /** + * The connection timeout for connecting the underlying socket. + */ + protected final int connectionTimeout; + + /** + * The read timeout for reads on the underlying socket. + */ + protected final int timeout; + + /** + * The host name of the proxy to connect to. + */ + protected String proxyHostname; + + /** + * The port on the proxy to connect to. + */ + protected int proxyPort; + + /** + * The major version of HTTP supported by this client. + */ + protected int majorVersion; + + /** + * The minor version of HTTP supported by this client. + */ + protected int minorVersion; + + private final List connectionListeners; + private final List requestListeners; + private final List handshakeCompletedListeners; + + /** + * The socket this connection communicates on. + */ + protected Socket socket; + + /** + * The SSL socket factory to use. + */ + private SSLSocketFactory sslSocketFactory; + + /** + * The socket input stream. + */ + protected InputStream in; + + /** + * The socket output stream. + */ + protected OutputStream out; + + /** + * Nonce values seen by this connection. + */ + private Map nonceCounts; + + /** + * The cookie manager for this connection. + */ + protected CookieManager cookieManager; + + /** + * Creates a new HTTP connection. + * @param hostname the name of the host to connect to + */ + public HTTPConnection(String hostname) + { + this(hostname, HTTP_PORT, false, 0, 0); + } + + /** + * Creates a new HTTP or HTTPS connection. + * @param hostname the name of the host to connect to + * @param secure whether to use a secure connection + */ + public HTTPConnection(String hostname, boolean secure) + { + this(hostname, secure ? HTTPS_PORT : HTTP_PORT, secure, 0, 0); + } + + /** + * Creates a new HTTP or HTTPS connection on the specified port. + * @param hostname the name of the host to connect to + * @param secure whether to use a secure connection + * @param connectionTimeout the connection timeout + * @param timeout the socket read timeout + */ + public HTTPConnection(String hostname, boolean secure, + int connectionTimeout, int timeout) + { + this(hostname, secure ? HTTPS_PORT : HTTP_PORT, secure, + connectionTimeout, timeout); + } + + /** + * Creates a new HTTP connection on the specified port. + * @param hostname the name of the host to connect to + * @param port the port on the host to connect to + */ + public HTTPConnection(String hostname, int port) + { + this(hostname, port, false, 0, 0); + } + + /** + * Creates a new HTTP or HTTPS connection on the specified port. + * @param hostname the name of the host to connect to + * @param port the port on the host to connect to + * @param secure whether to use a secure connection + */ + public HTTPConnection(String hostname, int port, boolean secure) + { + this(hostname, port, secure, 0, 0); + } + + /** + * Creates a new HTTP or HTTPS connection on the specified port. + * @param hostname the name of the host to connect to + * @param port the port on the host to connect to + * @param secure whether to use a secure connection + * @param connectionTimeout the connection timeout + * @param timeout the socket read timeout + */ + public HTTPConnection(String hostname, int port, boolean secure, + int connectionTimeout, int timeout) + { + this.hostname = hostname; + this.port = port; + this.secure = secure; + this.connectionTimeout = connectionTimeout; + this.timeout = timeout; + majorVersion = minorVersion = 1; + connectionListeners = new ArrayList(4); + requestListeners = new ArrayList(4); + handshakeCompletedListeners = new ArrayList(2); + } + + /** + * Returns the name of the host to connect to. + */ + public String getHostName() + { + return hostname; + } + + /** + * Returns the port on the host to connect to. + */ + public int getPort() + { + return port; + } + + /** + * Indicates whether to use a secure connection or not. + */ + public boolean isSecure() + { + return secure; + } + + /** + * Returns the HTTP version string supported by this connection. + * @see #version + */ + public String getVersion() + { + return "HTTP/" + majorVersion + '.' + minorVersion; + } + + /** + * Sets the HTTP version supported by this connection. + * @param majorVersion the major version + * @param minorVersion the minor version + */ + public void setVersion(int majorVersion, int minorVersion) + { + if (majorVersion != 1) + { + throw new IllegalArgumentException("major version not supported: " + + majorVersion); + } + if (minorVersion < 0 || minorVersion > 1) + { + throw new IllegalArgumentException("minor version not supported: " + + minorVersion); + } + this.majorVersion = majorVersion; + this.minorVersion = minorVersion; + } + + /** + * Directs this connection to use the specified proxy. + * @param hostname the proxy host name + * @param port the port on the proxy to connect to + */ + public void setProxy(String hostname, int port) + { + proxyHostname = hostname; + proxyPort = port; + } + + /** + * Indicates whether this connection is using an HTTP proxy. + */ + public boolean isUsingProxy() + { + return (proxyHostname != null && proxyPort > 0); + } + + /** + * Sets the cookie manager to use for this connection. + * @param cookieManager the cookie manager + */ + public void setCookieManager(CookieManager cookieManager) + { + this.cookieManager = cookieManager; + } + + /** + * Returns the cookie manager in use for this connection. + */ + public CookieManager getCookieManager() + { + return cookieManager; + } + + /** + * Creates a new request using this connection. + * @param method the HTTP method to invoke + * @param path the URI-escaped RFC2396 <code>abs_path</code> with + * optional query part + */ + public Request newRequest(String method, String path) + { + if (method == null || method.length() == 0) + { + throw new IllegalArgumentException("method must have non-zero length"); + } + if (path == null || path.length() == 0) + { + path = "/"; + } + Request ret = new Request(this, method, path); + if ((secure && port != HTTPS_PORT) || + (!secure && port != HTTP_PORT)) + { + ret.setHeader("Host", hostname + ":" + port); + } + else + { + ret.setHeader("Host", hostname); + } + ret.setHeader("User-Agent", userAgent); + ret.setHeader("Connection", "keep-alive"); + ret.setHeader("Accept-Encoding", + "chunked;q=1.0, gzip;q=0.9, deflate;q=0.8, " + + "identity;q=0.6, *;q=0"); + if (cookieManager != null) + { + Cookie[] cookies = cookieManager.getCookies(hostname, secure, path); + if (cookies != null && cookies.length > 0) + { + StringBuffer buf = new StringBuffer(); + buf.append("$Version=1"); + for (int i = 0; i < cookies.length; i++) + { + buf.append(','); + buf.append(' '); + buf.append(cookies[i].toString()); + } + ret.setHeader("Cookie", buf.toString()); + } + } + fireRequestEvent(RequestEvent.REQUEST_CREATED, ret); + return ret; + } + + /** + * Closes this connection. + */ + public void close() + throws IOException + { + try + { + closeConnection(); + } + finally + { + fireConnectionEvent(ConnectionEvent.CONNECTION_CLOSED); + } + } + + /** + * Retrieves the socket associated with this connection. + * This creates the socket if necessary. + */ + protected synchronized Socket getSocket() + throws IOException + { + if (socket == null) + { + String connectHostname = hostname; + int connectPort = port; + if (isUsingProxy()) + { + connectHostname = proxyHostname; + connectPort = proxyPort; + } + socket = new Socket(); + InetSocketAddress address = + new InetSocketAddress(connectHostname, connectPort); + if (connectionTimeout > 0) + { + socket.connect(address, connectionTimeout); + } + else + { + socket.connect(address); + } + if (timeout > 0) + { + socket.setSoTimeout(timeout); + } + if (secure) + { + try + { + SSLSocketFactory factory = getSSLSocketFactory(); + SSLSocket ss = + (SSLSocket) factory.createSocket(socket, connectHostname, + connectPort, true); + String[] protocols = { "TLSv1", "SSLv3" }; + ss.setEnabledProtocols(protocols); + ss.setUseClientMode(true); + synchronized (handshakeCompletedListeners) + { + if (!handshakeCompletedListeners.isEmpty()) + { + for (Iterator i = + handshakeCompletedListeners.iterator(); + i.hasNext(); ) + { + HandshakeCompletedListener l = + (HandshakeCompletedListener) i.next(); + ss.addHandshakeCompletedListener(l); + } + } + } + ss.startHandshake(); + socket = ss; + } + catch (GeneralSecurityException e) + { + throw new IOException(e.getMessage()); + } + } + in = socket.getInputStream(); + in = new BufferedInputStream(in); + out = socket.getOutputStream(); + out = new BufferedOutputStream(out); + } + return socket; + } + + SSLSocketFactory getSSLSocketFactory() + throws GeneralSecurityException + { + if (sslSocketFactory == null) + { + TrustManager tm = new EmptyX509TrustManager(); + SSLContext context = SSLContext.getInstance("SSL"); + TrustManager[] trust = new TrustManager[] { tm }; + context.init(null, trust, null); + sslSocketFactory = context.getSocketFactory(); + } + return sslSocketFactory; + } + + void setSSLSocketFactory(SSLSocketFactory factory) + { + sslSocketFactory = factory; + } + + protected synchronized InputStream getInputStream() + throws IOException + { + if (socket == null) + { + getSocket(); + } + return in; + } + + protected synchronized OutputStream getOutputStream() + throws IOException + { + if (socket == null) + { + getSocket(); + } + return out; + } + + /** + * Closes the underlying socket, if any. + */ + protected synchronized void closeConnection() + throws IOException + { + if (socket != null) + { + try + { + socket.close(); + } + finally + { + socket = null; + } + } + } + + /** + * Returns a URI representing the connection. + * This does not include any request path component. + */ + protected String getURI() + { + StringBuffer buf = new StringBuffer(); + buf.append(secure ? "https://" : "http://"); + buf.append(hostname); + if (secure) + { + if (port != HTTPConnection.HTTPS_PORT) + { + buf.append(':'); + buf.append(port); + } + } + else + { + if (port != HTTPConnection.HTTP_PORT) + { + buf.append(':'); + buf.append(port); + } + } + return buf.toString(); + } + + /** + * Get the number of times the specified nonce has been seen by this + * connection. + */ + int getNonceCount(String nonce) + { + if (nonceCounts == null) + { + return 0; + } + return((Integer) nonceCounts.get(nonce)).intValue(); + } + + /** + * Increment the number of times the specified nonce has been seen. + */ + void incrementNonce(String nonce) + { + int current = getNonceCount(nonce); + if (nonceCounts == null) + { + nonceCounts = new HashMap(); + } + nonceCounts.put(nonce, new Integer(current + 1)); + } + + // -- Events -- + + public void addConnectionListener(ConnectionListener l) + { + synchronized (connectionListeners) + { + connectionListeners.add(l); + } + } + + public void removeConnectionListener(ConnectionListener l) + { + synchronized (connectionListeners) + { + connectionListeners.remove(l); + } + } + + protected void fireConnectionEvent(int type) + { + ConnectionEvent event = new ConnectionEvent(this, type); + ConnectionListener[] l = null; + synchronized (connectionListeners) + { + l = new ConnectionListener[connectionListeners.size()]; + connectionListeners.toArray(l); + } + for (int i = 0; i < l.length; i++) + { + switch (type) + { + case ConnectionEvent.CONNECTION_CLOSED: + l[i].connectionClosed(event); + break; + } + } + } + + public void addRequestListener(RequestListener l) + { + synchronized (requestListeners) + { + requestListeners.add(l); + } + } + + public void removeRequestListener(RequestListener l) + { + synchronized (requestListeners) + { + requestListeners.remove(l); + } + } + + protected void fireRequestEvent(int type, Request request) + { + RequestEvent event = new RequestEvent(this, type, request); + RequestListener[] l = null; + synchronized (requestListeners) + { + l = new RequestListener[requestListeners.size()]; + requestListeners.toArray(l); + } + for (int i = 0; i < l.length; i++) + { + switch (type) + { + case RequestEvent.REQUEST_CREATED: + l[i].requestCreated(event); + break; + case RequestEvent.REQUEST_SENDING: + l[i].requestSent(event); + break; + case RequestEvent.REQUEST_SENT: + l[i].requestSent(event); + break; + } + } + } + + void addHandshakeCompletedListener(HandshakeCompletedListener l) + { + synchronized (handshakeCompletedListeners) + { + handshakeCompletedListeners.add(l); + } + } + void removeHandshakeCompletedListener(HandshakeCompletedListener l) + { + synchronized (handshakeCompletedListeners) + { + handshakeCompletedListeners.remove(l); + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/HTTPDateFormat.java b/libjava/classpath/gnu/java/net/protocol/http/HTTPDateFormat.java new file mode 100644 index 00000000000..2f59e43181c --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/HTTPDateFormat.java @@ -0,0 +1,441 @@ +/* HTTPDateFormat.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.text.FieldPosition; +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +/** + * HTTP date formatter and parser. + * Formats dates according to RFC 822 (updated by RFC 1123). + * Parses dates according to the above, <i>or</i> RFC 1036, <i>or</i> the + * ANSI C <code>asctime()</code> format. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class HTTPDateFormat + extends DateFormat +{ + + static final String[] DAYS_OF_WEEK = { + null, "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" + }; + + static final String[] MONTHS = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; + + public HTTPDateFormat() + { + calendar = new GregorianCalendar(TimeZone.getTimeZone ("GMT")); + numberFormat = new DecimalFormat(); + } + + /** + * Appends the textual value for the specified field to the given string + * buffer. This method should be avoided, use <code>format(Date)</code> + * instead. + * @param date the Date object + * @param buf the buffer to append to + * @param field the current field position + * @return the modified buffer + */ + public StringBuffer format(Date date, StringBuffer buf, + FieldPosition field) + { + calendar.clear(); + calendar.setTime(date); + buf.setLength(0); + + // Day of week + buf.append(DAYS_OF_WEEK[calendar.get(Calendar.DAY_OF_WEEK)]); + buf.append(','); + buf.append(' '); + + // Day of month + int day = calendar.get(Calendar.DAY_OF_MONTH); + buf.append(Character.forDigit(day / 10, 10)); + buf.append(Character.forDigit(day % 10, 10)); + buf.append(' '); + + // Month + buf.append(MONTHS[calendar.get(Calendar.MONTH)]); + buf.append(' '); + + // Year + int year = calendar.get(Calendar.YEAR); + if (year < 1000) + { + buf.append('0'); + if (year < 100) + { + buf.append('0'); + if (year < 10) + { + buf.append('0'); + } + } + } + buf.append(Integer.toString(year)); + buf.append(' '); + + // Hour + int hour = calendar.get(Calendar.HOUR_OF_DAY); + buf.append(Character.forDigit(hour / 10, 10)); + buf.append(Character.forDigit(hour % 10, 10)); + buf.append(':'); + + // Minute + int minute = calendar.get(Calendar.MINUTE); + buf.append(Character.forDigit(minute / 10, 10)); + buf.append(Character.forDigit(minute % 10, 10)); + buf.append(':'); + + // Second + int second = calendar.get(Calendar.SECOND); + buf.append(Character.forDigit(second / 10, 10)); + buf.append(Character.forDigit(second % 10, 10)); + buf.append(' '); + + // Timezone + // Get time offset in minutes + int zoneOffset =(calendar.get(Calendar.ZONE_OFFSET) + + calendar.get(Calendar.DST_OFFSET)) / 60000; + + // Apply + or - appropriately + if (zoneOffset < 0) + { + zoneOffset = -zoneOffset; + buf.append('-'); + } + else + { + buf.append('+'); + } + + // Set the 2 2-char fields as specified above + int tzhours = zoneOffset / 60; + buf.append(Character.forDigit(tzhours / 10, 10)); + buf.append(Character.forDigit(tzhours % 10, 10)); + int tzminutes = zoneOffset % 60; + buf.append(Character.forDigit(tzminutes / 10, 10)); + buf.append(Character.forDigit(tzminutes % 10, 10)); + + field.setBeginIndex(0); + field.setEndIndex(buf.length()); + return buf; + } + + /** + * Parses the given date in the current TimeZone. + * @param text the formatted date to be parsed + * @param pos the current parse position + */ + public Date parse(String text, ParsePosition pos) + { + int date, month, year, hour, minute, second; + String monthText; + int start = 0, end = -1; + int len = text.length(); + calendar.clear(); + pos.setIndex(start); + try + { + // Advance to date + if (Character.isLetter(text.charAt(start))) + { + start = skipNonWhitespace(text, start); + } + // Determine mode + switch(start) + { + case 3: + // asctime + start = skipWhitespace(text, start); + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + monthText = text.substring(start, end); + month = -1; + for (int i = 0; i < 12; i++) + { + if (MONTHS[i].equals(monthText)) + { + month = i; + break; + } + } + if (month == -1) + { + pos.setErrorIndex(end); + return null; + } + // Advance to date + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + date = Integer.parseInt(text.substring(start, end)); + // Advance to hour + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipTo(text, start + 1, ':'); + hour = Integer.parseInt(text.substring(start, end)); + // Advance to minute + start = end + 1; + pos.setIndex(start); + end = skipTo(text, start + 1, ':'); + minute = Integer.parseInt(text.substring(start, end)); + // Advance to second + start = end + 1; + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + second = Integer.parseInt(text.substring(start, end)); + // Advance to year + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + year = Integer.parseInt(text.substring(start, end)); + break; + case 0: + case 4: + // rfc822 + start = skipWhitespace(text, start); + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + date = Integer.parseInt(text.substring(start, end)); + // Advance to month + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + monthText = text.substring(start, end); + month = -1; + for (int i = 0; i < 12; i++) + { + if (MONTHS[i].equals(monthText)) + { + month = i; + break; + } + } + if (month == -1) + { + pos.setErrorIndex(end); + return null; + } + // Advance to year + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + year = Integer.parseInt(text.substring(start, end)); + // Advance to hour + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipTo(text, start + 1, ':'); + hour = Integer.parseInt(text.substring(start, end)); + // Advance to minute + start = end + 1; + pos.setIndex(start); + end = skipTo(text, start + 1, ':'); + minute = Integer.parseInt(text.substring(start, end)); + // Advance to second + start = end + 1; + pos.setIndex(start); + end = start + 1; + while (end < len && !Character.isWhitespace(text.charAt(end))) + { + end++; + } + second = Integer.parseInt(text.substring(start, end)); + break; + default: + // rfc850(obsolete) + start = skipWhitespace(text, start); + pos.setIndex(start); + end = skipTo(text, start + 1, '-'); + date = Integer.parseInt(text.substring(start, end)); + // Advance to month + start = end + 1; + pos.setIndex(start); + end = skipTo(text, start + 1, '-'); + monthText = text.substring(start, end); + month = -1; + for (int i = 0; i < 12; i++) + { + if (MONTHS[i].equals(monthText)) + { + month = i; + break; + } + } + if (month == -1) + { + pos.setErrorIndex(end); + return null; + } + // Advance to year + start = end + 1; + pos.setIndex(start); + end = skipNonWhitespace(text, start + 1); + year = 1900 + Integer.parseInt(text.substring(start, end)); + // Advance to hour + start = skipWhitespace(text, end + 1); + pos.setIndex(start); + end = skipTo(text, start + 1, ':'); + hour = Integer.parseInt(text.substring(start, end)); + // Advance to minute + start = end + 1; + pos.setIndex(start); + end = skipTo(text, start + 1, ':'); + minute = Integer.parseInt(text.substring(start, end)); + // Advance to second + start = end + 1; + pos.setIndex(start); + end = start + 1; + while (end < len && !Character.isWhitespace(text.charAt(end))) + { + end++; + } + second = Integer.parseInt(text.substring(start, end)); + } + + calendar.set(Calendar.YEAR, year); + calendar.set(Calendar.MONTH, month); + calendar.set(Calendar.DAY_OF_MONTH, date); + calendar.set(Calendar.HOUR, hour); + calendar.set(Calendar.MINUTE, minute); + calendar.set(Calendar.SECOND, second); + + if (end != len) + { + // Timezone + start = skipWhitespace(text, end + 1); + end = start + 1; + while (end < len && !Character.isWhitespace(text.charAt(end))) + { + end++; + } + char pm = text.charAt(start); + if (Character.isLetter(pm)) + { + TimeZone tz = + TimeZone.getTimeZone(text.substring(start, end)); + calendar.set(Calendar.ZONE_OFFSET, tz.getRawOffset()); + } + else + { + int zoneOffset = 0; + zoneOffset += 600 * Character.digit(text.charAt(++start), 10); + zoneOffset += 60 * Character.digit(text.charAt(++start), 10); + zoneOffset += 10 * Character.digit(text.charAt(++start), 10); + zoneOffset += Character.digit(text.charAt(++start), 10); + zoneOffset *= 60000; // minutes -> ms + if ('-' == pm) + { + zoneOffset = -zoneOffset; + } + calendar.set(Calendar.ZONE_OFFSET, zoneOffset); + } + } + pos.setIndex(end); + + return calendar.getTime(); + } + catch (NumberFormatException e) + { + pos.setErrorIndex(Math.max(start, end)); + } + catch (StringIndexOutOfBoundsException e) + { + pos.setErrorIndex(Math.max(start, end)); + } + return null; + } + + private int skipWhitespace(String text, int pos) + { + while(Character.isWhitespace(text.charAt(pos))) + { + pos++; + } + return pos; + } + + private int skipNonWhitespace(String text, int pos) + { + while(!Character.isWhitespace(text.charAt(pos))) + { + pos++; + } + return pos; + } + + private int skipTo(String text, int pos, char c) + { + while(text.charAt(pos) != c) + { + pos++; + } + return pos; + } + + /** + * Don't allow setting the calendar. + */ + public void setCalendar(Calendar newCalendar) + { + throw new UnsupportedOperationException(); + } + + /** + * Don't allow setting the NumberFormat. + */ + public void setNumberFormat(NumberFormat newNumberFormat) + { + throw new UnsupportedOperationException(); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java b/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java new file mode 100644 index 00000000000..9f2055fe658 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java @@ -0,0 +1,688 @@ +/* HTTPURLConnection.java -- + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ProtocolException; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.cert.Certificate; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.net.ssl.HandshakeCompletedEvent; +import javax.net.ssl.HandshakeCompletedListener; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSocketFactory; + +/** + * A URLConnection that uses the HTTPConnection class. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class HTTPURLConnection + extends HttpsURLConnection + implements HandshakeCompletedListener +{ + + /** + * Pool of reusable connections, used if keepAlive is true. + */ + private static final Map connectionPool = new LinkedHashMap(); + + /* + * The underlying connection. + */ + private HTTPConnection connection; + + // These are package private for use in anonymous inner classes. + String proxyHostname; + int proxyPort; + String agent; + boolean keepAlive; + int maxConnections; + + private Request request; + private Headers requestHeaders; + private ByteArrayOutputStream requestSink; + private boolean requestMethodSetExplicitly; + + private Response response; + private ByteArrayInputStream responseSink; + private ByteArrayInputStream errorSink; + + private HandshakeCompletedEvent handshakeEvent; + + /** + * Constructor. + * @param url the URL + */ + public HTTPURLConnection(URL url) + throws IOException + { + super(url); + requestHeaders = new Headers(); + AccessController.doPrivileged(this.new GetHTTPPropertiesAction()); + } + + class GetHTTPPropertiesAction + implements PrivilegedAction + { + + public Object run() + { + proxyHostname = System.getProperty("http.proxyHost"); + if (proxyHostname != null && proxyHostname.length() > 0) + { + String port = System.getProperty("http.proxyPort"); + if (port != null && port.length() > 0) + { + proxyPort = Integer.parseInt(port); + } + else + { + proxyHostname = null; + proxyPort = -1; + } + } + agent = System.getProperty("http.agent"); + String ka = System.getProperty("http.keepAlive"); + keepAlive = !(ka != null && "false".equals(ka)); + String mc = System.getProperty("http.maxConnections"); + maxConnections = (mc != null && mc.length() > 0) ? + Math.max(Integer.parseInt(mc), 1) : 5; + return null; + } + + } + + public void connect() + throws IOException + { + if (connected) + { + return; + } + String protocol = url.getProtocol(); + boolean secure = "https".equals(protocol); + String host = url.getHost(); + int port = url.getPort(); + if (port < 0) + { + port = secure ? HTTPConnection.HTTPS_PORT : + HTTPConnection.HTTP_PORT; + } + String file = url.getFile(); + String username = url.getUserInfo(); + String password = null; + if (username != null) + { + int ci = username.indexOf(':'); + if (ci != -1) + { + password = username.substring(ci + 1); + username = username.substring(0, ci); + } + } + final Credentials creds = (username == null) ? null : + new Credentials (username, password); + + boolean retry; + do + { + retry = false; + if (connection == null) + { + connection = getConnection(host, port, secure); + if (secure) + { + SSLSocketFactory factory = getSSLSocketFactory(); + HostnameVerifier verifier = getHostnameVerifier(); + if (factory != null) + { + connection.setSSLSocketFactory(factory); + } + connection.addHandshakeCompletedListener(this); + // TODO verifier + } + } + if (proxyHostname != null) + { + if (proxyPort < 0) + { + proxyPort = secure ? HTTPConnection.HTTPS_PORT : + HTTPConnection.HTTP_PORT; + } + connection.setProxy(proxyHostname, proxyPort); + } + request = connection.newRequest(method, file); + if (!keepAlive) + { + request.setHeader("Connection", "close"); + } + if (agent != null) + { + request.setHeader("User-Agent", agent); + } + request.getHeaders().putAll(requestHeaders); + if (requestSink != null) + { + byte[] content = requestSink.toByteArray(); + RequestBodyWriter writer = new ByteArrayRequestBodyWriter(content); + request.setRequestBodyWriter(writer); + } + ByteArrayResponseBodyReader reader = new ByteArrayResponseBodyReader(); + request.setResponseBodyReader(reader); + if (creds != null) + { + request.setAuthenticator(new Authenticator() { + public Credentials getCredentials(String realm, int attempts) + { + return (attempts < 2) ? creds : null; + } + }); + } + response = request.dispatch(); + if (response.getCodeClass() == 3 && getInstanceFollowRedirects()) + { + // Follow redirect + String location = response.getHeader("Location"); + if (location != null) + { + String connectionUri = connection.getURI(); + int start = connectionUri.length(); + if (location.startsWith(connectionUri) && + location.charAt(start) == '/') + { + file = location.substring(start); + retry = true; + } + else if (location.startsWith("http:")) + { + connection.close(); + connection = null; + secure = false; + start = 7; + int end = location.indexOf('/', start); + host = location.substring(start, end); + int ci = host.lastIndexOf(':'); + if (ci != -1) + { + port = Integer.parseInt(host.substring (ci + 1)); + host = host.substring(0, ci); + } + else + { + port = HTTPConnection.HTTP_PORT; + } + file = location.substring(end); + retry = true; + } + else if (location.startsWith("https:")) + { + connection.close(); + connection = null; + secure = true; + start = 8; + int end = location.indexOf('/', start); + host = location.substring(start, end); + int ci = host.lastIndexOf(':'); + if (ci != -1) + { + port = Integer.parseInt(host.substring (ci + 1)); + host = host.substring(0, ci); + } + else + { + port = HTTPConnection.HTTPS_PORT; + } + file = location.substring(end); + retry = true; + } + else if (location.length() > 0) + { + // Malformed absolute URI, treat as file part of URI + if (location.charAt(0) == '/') + { + // Absolute path + file = location; + } + else + { + // Relative path + int lsi = file.lastIndexOf('/'); + file = (lsi == -1) ? "/" : file.substring(0, lsi + 1); + file += location; + } + retry = true; + } + } + } + else + { + responseSink = new ByteArrayInputStream(reader.toByteArray ()); + if (response.getCode() == 404) + { + errorSink = responseSink; + throw new FileNotFoundException(url.toString()); + } + } + } + while (retry); + connected = true; + } + + /** + * Returns a connection, from the pool if necessary. + */ + HTTPConnection getConnection(String host, int port, boolean secure) + throws IOException + { + HTTPConnection connection; + if (keepAlive) + { + StringBuffer buf = new StringBuffer(secure ? "https://" : "http://"); + buf.append(Thread.currentThread().hashCode()); + buf.append('@'); + buf.append(host); + buf.append(':'); + buf.append(port); + String key = buf.toString(); + synchronized (connectionPool) + { + connection = (HTTPConnection) connectionPool.get(key); + if (connection == null) + { + connection = new HTTPConnection(host, port, secure); + // Good housekeeping + if (connectionPool.size() == maxConnections) + { + // maxConnections must always be >= 1 + Object lru = connectionPool.keySet().iterator().next(); + connectionPool.remove(lru); + } + connectionPool.put(key, connection); + } + } + } + else + { + connection = new HTTPConnection(host, port, secure); + } + return connection; + } + + public void disconnect() + { + if (connection != null) + { + try + { + connection.close(); + } + catch (IOException e) + { + } + } + } + + public boolean usingProxy() + { + return (proxyHostname != null); + } + + /** + * Overrides the corresponding method in HttpURLConnection to permit + * arbitrary methods, as long as they're valid ASCII alphabetic + * characters. This is to permit WebDAV and other HTTP extensions to + * function. + * @param method the method + */ + public void setRequestMethod(String method) + throws ProtocolException + { + if (connected) + { + throw new ProtocolException("Already connected"); + } + // Validate + method = method.toUpperCase(); + int len = method.length(); + if (len == 0) + { + throw new ProtocolException("Empty method name"); + } + for (int i = 0; i < len; i++) + { + char c = method.charAt(i); + if (c < 0x41 || c > 0x5a) + { + throw new ProtocolException("Illegal character '" + c + + "' at index " + i); + } + } + // OK + this.method = method; + requestMethodSetExplicitly = true; + } + + public String getRequestProperty(String key) + { + return requestHeaders.getValue(key); + } + + public Map getRequestProperties() + { + return requestHeaders; + } + + public void setRequestProperty(String key, String value) + { + requestHeaders.put(key, value); + } + + public void addRequestProperty(String key, String value) + { + String old = requestHeaders.getValue(key); + if (old == null) + { + requestHeaders.put(key, value); + } + else + { + requestHeaders.put(key, old + "," + value); + } + } + + public OutputStream getOutputStream() + throws IOException + { + if (connected) + { + throw new ProtocolException("Already connected"); + } + if (!doOutput) + { + throw new ProtocolException("doOutput is false"); + } + else if (!requestMethodSetExplicitly) + { + /* + * Silently change the method to POST if no method was set + * explicitly. This is due to broken applications depending on this + * behaviour (Apache XMLRPC for one). + */ + method = "POST"; + } + if (requestSink == null) + { + requestSink = new ByteArrayOutputStream(); + } + return requestSink; + } + + // -- Response -- + + public InputStream getInputStream() + throws IOException + { + if (!connected) + { + connect(); + } + if (!doInput) + { + throw new ProtocolException("doInput is false"); + } + return responseSink; + } + + public InputStream getErrorStream() + { + return errorSink; + } + + public Map getHeaderFields() + { + if (!connected) + { + try + { + connect(); + } + catch (IOException e) + { + return null; + } + } + Map headers = response.getHeaders(); + Map ret = new LinkedHashMap(); + ret.put("", Collections.singletonList(getStatusLine(response))); + for (Iterator i = headers.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + String key = (String) entry.getKey(); + String value = (String) entry.getValue(); + ret.put(key, Collections.singletonList(value)); + } + return ret; + } + + String getStatusLine(Response response) + { + return "HTTP/" + response.getMajorVersion() + + "." + response.getMinorVersion() + + " " + response.getCode() + + " " + response.getMessage(); + } + + public String getHeaderField(int index) + { + if (!connected) + { + try + { + connect(); + } + catch (IOException e) + { + return null; + } + } + if (index == 0) + { + return getStatusLine(response); + } + Iterator i = response.getHeaders().entrySet().iterator(); + Map.Entry entry; + int count = 1; + do + { + if (!i.hasNext()) + { + return null; + } + entry = (Map.Entry) i.next(); + count++; + } + while (count <= index); + return (String) entry.getValue(); + } + + public String getHeaderFieldKey(int index) + { + if (!connected) + { + try + { + connect(); + } + catch (IOException e) + { + return null; + } + } + if (index == 0) + { + return null; + } + Iterator i = response.getHeaders().entrySet().iterator(); + Map.Entry entry; + int count = 1; + do + { + if (!i.hasNext()) + { + return null; + } + entry = (Map.Entry) i.next(); + count++; + } + while (count <= index); + return (String) entry.getKey(); + } + + public String getHeaderField(String name) + { + if (!connected) + { + try + { + connect(); + } + catch (IOException e) + { + return null; + } + } + return (String) response.getHeader(name); + } + + public long getHeaderFieldDate(String name, long def) + { + if (!connected) + { + try + { + connect(); + } + catch (IOException e) + { + return def; + } + } + Date date = response.getDateHeader(name); + return (date == null) ? def : date.getTime(); + } + + public String getContentType() + { + return getHeaderField("Content-Type"); + } + + public int getResponseCode() + throws IOException + { + if (!connected) + { + connect(); + } + return response.getCode(); + } + + public String getResponseMessage() + throws IOException + { + if (!connected) + { + connect(); + } + return response.getMessage(); + } + + // -- HTTPS specific -- + + public String getCipherSuite() + { + if (!connected) + { + throw new IllegalStateException("not connected"); + } + return handshakeEvent.getCipherSuite(); + } + + public Certificate[] getLocalCertificates() + { + if (!connected) + { + throw new IllegalStateException("not connected"); + } + return handshakeEvent.getLocalCertificates(); + } + + public Certificate[] getServerCertificates() + throws SSLPeerUnverifiedException + { + if (!connected) + { + throw new IllegalStateException("not connected"); + } + return handshakeEvent.getPeerCertificates(); + } + + // HandshakeCompletedListener + + public void handshakeCompleted(HandshakeCompletedEvent event) + { + handshakeEvent = event; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/Handler.java b/libjava/classpath/gnu/java/net/protocol/http/Handler.java new file mode 100644 index 00000000000..64054251331 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Handler.java @@ -0,0 +1,73 @@ +/* Handler.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * An HTTP URL stream handler. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Handler + extends URLStreamHandler +{ + + /** + * Returns the default HTTP port (80). + */ + protected int getDefaultPort() + { + return HTTPConnection.HTTP_PORT; + } + + /** + * Returns an HTTPURLConnection for the given URL. + */ + public URLConnection openConnection(URL url) + throws IOException + { + return new HTTPURLConnection(url); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/Headers.java b/libjava/classpath/gnu/java/net/protocol/http/Headers.java new file mode 100644 index 00000000000..847ebefc1f6 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Headers.java @@ -0,0 +1,369 @@ +/* Headers.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import gnu.java.net.LineInputStream; + +import java.io.IOException; +import java.io.InputStream; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +/** + * A collection of HTTP header names and associated values. + * Retrieval of values is case insensitive. An iteration over the keys + * returns the header names in the order they were received. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Headers + implements Map +{ + + static final DateFormat dateFormat = new HTTPDateFormat(); + + static class Header + { + + final String name; + + Header(String name) + { + if (name == null || name.length() == 0) + { + throw new IllegalArgumentException(name); + } + this.name = name; + } + + public int hashCode() + { + return name.toLowerCase().hashCode(); + } + + public boolean equals(Object other) + { + if (other instanceof Header) + { + return ((Header) other).name.equalsIgnoreCase(name); + } + return false; + } + + public String toString() + { + return name; + } + + } + + static class HeaderEntry + implements Map.Entry + { + + final Map.Entry entry; + + HeaderEntry(Map.Entry entry) + { + this.entry = entry; + } + + public Object getKey() + { + return ((Header) entry.getKey()).name; + } + + public Object getValue() + { + return entry.getValue(); + } + + public Object setValue(Object value) + { + return entry.setValue(value); + } + + public int hashCode() + { + return entry.hashCode(); + } + + public boolean equals(Object other) + { + return entry.equals(other); + } + + public String toString() + { + return getKey().toString() + "=" + getValue(); + } + + } + + private LinkedHashMap headers; + + public Headers() + { + headers = new LinkedHashMap(); + } + + public int size() + { + return headers.size(); + } + + public boolean isEmpty() + { + return headers.isEmpty(); + } + + public boolean containsKey(Object key) + { + return headers.containsKey(new Header((String) key)); + } + + public boolean containsValue(Object value) + { + return headers.containsValue(value); + } + + public Object get(Object key) + { + return headers.get(new Header((String) key)); + } + + /** + * Returns the value of the specified header as a string. + */ + public String getValue(String header) + { + return (String) headers.get(new Header(header)); + } + + /** + * Returns the value of the specified header as an integer, + * or -1 if the header is not present or not an integer. + */ + public int getIntValue(String header) + { + String val = getValue(header); + if (val == null) + { + return -1; + } + try + { + return Integer.parseInt(val); + } + catch (NumberFormatException e) + { + } + return -1; + } + + /** + * Returns the value of the specified header as a date, + * or <code>null</code> if the header is not present or not a date. + */ + public Date getDateValue(String header) + { + String val = getValue(header); + if (val == null) + { + return null; + } + try + { + return dateFormat.parse(val); + } + catch (ParseException e) + { + return null; + } + } + + public Object put(Object key, Object value) + { + return headers.put(new Header((String) key), value); + } + + public Object remove(Object key) + { + return headers.remove(new Header((String) key)); + } + + public void putAll(Map t) + { + for (Iterator i = t.keySet().iterator(); i.hasNext(); ) + { + String key = (String) i.next(); + String value = (String) t.get(key); + headers.put(new Header(key), value); + } + } + + public void clear() + { + headers.clear(); + } + + public Set keySet() + { + Set keys = headers.keySet(); + Set ret = new LinkedHashSet(); + for (Iterator i = keys.iterator(); i.hasNext(); ) + { + ret.add(((Header) i.next()).name); + } + return ret; + } + + public Collection values() + { + return headers.values(); + } + + public Set entrySet() + { + Set entries = headers.entrySet(); + Set ret = new LinkedHashSet(); + for (Iterator i = entries.iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + ret.add(new HeaderEntry(entry)); + } + return ret; + } + + public boolean equals(Object other) + { + return headers.equals(other); + } + + public int hashCode() + { + return headers.hashCode(); + } + + /** + * Parse the specified input stream, adding headers to this collection. + */ + public void parse(InputStream in) + throws IOException + { + LineInputStream lin = (in instanceof LineInputStream) ? + (LineInputStream) in : new LineInputStream(in); + + String name = null; + StringBuffer value = new StringBuffer(); + while (true) + { + String line = lin.readLine(); + if (line == null) + { + if (name != null) + { + addValue(name, value.toString()); + } + break; + } + int len = line.length(); + if (len < 2) + { + if (name != null) + { + addValue(name, value.toString()); + } + break; + } + char c1 = line.charAt(0); + if (c1 == ' ' || c1 == '\t') + { + // Continuation + int last = len - 1; + if (line.charAt(last) != '\r') + ++last; + value.append(line.substring(0, last)); + } + else + { + if (name != null) + { + addValue(name, value.toString()); + } + + int di = line.indexOf(':'); + name = line.substring(0, di); + value.setLength(0); + do + { + di++; + } + while (di < len && line.charAt(di) == ' '); + int last = len - 1; + if (line.charAt(last) != '\r') + ++last; + value.append(line.substring(di, last)); + } + } + } + + private void addValue(String name, String value) + { + Header key = new Header(name); + String old = (String) headers.get(key); + if (old == null) + { + headers.put(key, value); + } + else + { + headers.put(key, old + ", " + value); + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/Request.java b/libjava/classpath/gnu/java/net/protocol/http/Request.java new file mode 100644 index 00000000000..21205e6bba8 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Request.java @@ -0,0 +1,915 @@ +/* Request.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import gnu.java.net.BASE64; +import gnu.java.net.LineInputStream; +import gnu.java.net.protocol.http.event.RequestEvent; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ProtocolException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; +import java.util.zip.GZIPInputStream; +import java.util.zip.InflaterInputStream; + +/** + * A single HTTP request. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Request +{ + + /** + * The connection context in which this request is invoked. + */ + protected final HTTPConnection connection; + + /** + * The HTTP method to invoke. + */ + protected final String method; + + /** + * The path identifying the resource. + * This string must conform to the abs_path definition given in RFC2396, + * with an optional "?query" part, and must be URI-escaped by the caller. + */ + protected final String path; + + /** + * The headers in this request. + */ + protected final Headers requestHeaders; + + /** + * The request body provider. + */ + protected RequestBodyWriter requestBodyWriter; + + /** + * Request body negotiation threshold for 100-continue expectations. + */ + protected int requestBodyNegotiationThreshold; + + /** + * The response body reader. + */ + protected ResponseBodyReader responseBodyReader; + + /** + * Map of response header handlers. + */ + protected Map responseHeaderHandlers; + + /** + * The authenticator. + */ + protected Authenticator authenticator; + + /** + * Whether this request has been dispatched yet. + */ + private boolean dispatched; + + /** + * Constructor for a new request. + * @param connection the connection context + * @param method the HTTP method + * @param path the resource path including query part + */ + protected Request(HTTPConnection connection, String method, + String path) + { + this.connection = connection; + this.method = method; + this.path = path; + requestHeaders = new Headers(); + responseHeaderHandlers = new HashMap(); + requestBodyNegotiationThreshold = 4096; + } + + /** + * Returns the connection associated with this request. + * @see #connection + */ + public HTTPConnection getConnection() + { + return connection; + } + + /** + * Returns the HTTP method to invoke. + * @see #method + */ + public String getMethod() + { + return method; + } + + /** + * Returns the resource path. + * @see #path + */ + public String getPath() + { + return path; + } + + /** + * Returns the full request-URI represented by this request, as specified + * by HTTP/1.1. + */ + public String getRequestURI() + { + return connection.getURI() + path; + } + + /** + * Returns the headers in this request. + */ + public Headers getHeaders() + { + return requestHeaders; + } + + /** + * Returns the value of the specified header in this request. + * @param name the header name + */ + public String getHeader(String name) + { + return requestHeaders.getValue(name); + } + + /** + * Returns the value of the specified header in this request as an integer. + * @param name the header name + */ + public int getIntHeader(String name) + { + return requestHeaders.getIntValue(name); + } + + /** + * Returns the value of the specified header in this request as a date. + * @param name the header name + */ + public Date getDateHeader(String name) + { + return requestHeaders.getDateValue(name); + } + + /** + * Sets the specified header in this request. + * @param name the header name + * @param value the header value + */ + public void setHeader(String name, String value) + { + requestHeaders.put(name, value); + } + + /** + * Convenience method to set the entire request body. + * @param requestBody the request body content + */ + public void setRequestBody(byte[] requestBody) + { + setRequestBodyWriter(new ByteArrayRequestBodyWriter(requestBody)); + } + + /** + * Sets the request body provider. + * @param requestBodyWriter the handler used to obtain the request body + */ + public void setRequestBodyWriter(RequestBodyWriter requestBodyWriter) + { + this.requestBodyWriter = requestBodyWriter; + } + + /** + * Sets the response body reader. + * @param responseBodyReader the handler to receive notifications of + * response body content + */ + public void setResponseBodyReader(ResponseBodyReader responseBodyReader) + { + this.responseBodyReader = responseBodyReader; + } + + /** + * Sets a callback handler to be invoked for the specified header name. + * @param name the header name + * @param handler the handler to receive the value for the header + */ + public void setResponseHeaderHandler(String name, + ResponseHeaderHandler handler) + { + responseHeaderHandlers.put(name, handler); + } + + /** + * Sets an authenticator that can be used to handle authentication + * automatically. + * @param authenticator the authenticator + */ + public void setAuthenticator(Authenticator authenticator) + { + this.authenticator = authenticator; + } + + /** + * Sets the request body negotiation threshold. + * If this is set, it determines the maximum size that the request body + * may be before body negotiation occurs(via the + * <code>100-continue</code> expectation). This ensures that a large + * request body is not sent when the server wouldn't have accepted it + * anyway. + * @param threshold the body negotiation threshold, or <=0 to disable + * request body negotation entirely + */ + public void setRequestBodyNegotiationThreshold(int threshold) + { + requestBodyNegotiationThreshold = threshold; + } + + /** + * Dispatches this request. + * A request can only be dispatched once; calling this method a second + * time results in a protocol exception. + * @exception IOException if an I/O error occurred + * @return an HTTP response object representing the result of the operation + */ + public Response dispatch() + throws IOException + { + if (dispatched) + { + throw new ProtocolException("request already dispatched"); + } + final String CRLF = "\r\n"; + final String HEADER_SEP = ": "; + final String US_ASCII = "US-ASCII"; + final String version = connection.getVersion(); + Response response; + int contentLength = -1; + boolean retry = false; + int attempts = 0; + boolean expectingContinue = false; + if (requestBodyWriter != null) + { + contentLength = requestBodyWriter.getContentLength(); + if (contentLength > requestBodyNegotiationThreshold) + { + expectingContinue = true; + setHeader("Expect", "100-continue"); + } + else + { + setHeader("Content-Length", Integer.toString(contentLength)); + } + } + + try + { + // Loop while authentication fails or continue + do + { + retry = false; + // Send request + connection.fireRequestEvent(RequestEvent.REQUEST_SENDING, this); + + // Get socket output and input streams + OutputStream out = connection.getOutputStream(); + LineInputStream in = + new LineInputStream(connection.getInputStream()); + // Request line + String requestUri = path; + if (connection.isUsingProxy() && + !"*".equals(requestUri) && + !"CONNECT".equals(method)) + { + requestUri = getRequestURI(); + } + String line = method + ' ' + requestUri + ' ' + version + CRLF; + out.write(line.getBytes(US_ASCII)); + // Request headers + for (Iterator i = requestHeaders.keySet().iterator(); + i.hasNext(); ) + { + String name =(String) i.next(); + String value =(String) requestHeaders.get(name); + line = name + HEADER_SEP + value + CRLF; + out.write(line.getBytes(US_ASCII)); + } + out.write(CRLF.getBytes(US_ASCII)); + // Request body + if (requestBodyWriter != null && !expectingContinue) + { + byte[] buffer = new byte[4096]; + int len; + int count = 0; + + requestBodyWriter.reset(); + do + { + len = requestBodyWriter.write(buffer); + if (len > 0) + { + out.write(buffer, 0, len); + } + count += len; + } + while (len > -1 && count < contentLength); + out.write(CRLF.getBytes(US_ASCII)); + } + out.flush(); + // Sent event + connection.fireRequestEvent(RequestEvent.REQUEST_SENT, this); + // Get response + response = readResponse(in); + int sc = response.getCode(); + if (sc == 401 && authenticator != null) + { + if (authenticate(response, attempts++)) + { + retry = true; + } + } + else if (sc == 100 && expectingContinue) + { + requestHeaders.remove("Expect"); + setHeader("Content-Length", Integer.toString(contentLength)); + expectingContinue = false; + retry = true; + } + } + while (retry); + } + catch (IOException e) + { + connection.close(); + throw e; + } + return response; + } + + Response readResponse(LineInputStream in) + throws IOException + { + String line; + int len; + + // Read response status line + line = in.readLine(); + if (line == null) + { + throw new ProtocolException("Peer closed connection"); + } + if (!line.startsWith("HTTP/")) + { + throw new ProtocolException(line); + } + len = line.length(); + int start = 5, end = 6; + while (line.charAt(end) != '.') + { + end++; + } + int majorVersion = Integer.parseInt(line.substring(start, end)); + start = end + 1; + end = start + 1; + while (line.charAt(end) != ' ') + { + end++; + } + int minorVersion = Integer.parseInt(line.substring(start, end)); + start = end + 1; + end = start + 3; + int code = Integer.parseInt(line.substring(start, end)); + String message = line.substring(end + 1, len - 1); + // Read response headers + Headers responseHeaders = new Headers(); + responseHeaders.parse(in); + notifyHeaderHandlers(responseHeaders); + // Construct response + int codeClass = code / 100; + Response ret = new Response(majorVersion, minorVersion, code, + codeClass, message, responseHeaders); + switch (code) + { + case 204: + case 205: + case 304: + break; + default: + // Does response body reader want body? + boolean notify = (responseBodyReader != null); + if (notify) + { + if (!responseBodyReader.accept(this, ret)) + { + notify = false; + } + } + readResponseBody(ret, in, notify); + } + return ret; + } + + void notifyHeaderHandlers(Headers headers) + { + for (Iterator i = headers.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + String name =(String) entry.getKey(); + // Handle Set-Cookie + if ("Set-Cookie".equalsIgnoreCase(name)) + { + String value = (String) entry.getValue(); + handleSetCookie(value); + } + ResponseHeaderHandler handler = + (ResponseHeaderHandler) responseHeaderHandlers.get(name); + if (handler != null) + { + String value = (String) entry.getValue(); + handler.setValue(value); + } + } + } + + void readResponseBody(Response response, InputStream in, + boolean notify) + throws IOException + { + byte[] buffer = new byte[4096]; + int contentLength = -1; + Headers trailer = null; + + String transferCoding = response.getHeader("Transfer-Encoding"); + if ("chunked".equalsIgnoreCase(transferCoding)) + { + trailer = new Headers(); + in = new ChunkedInputStream(in, trailer); + } + else + { + contentLength = response.getIntHeader("Content-Length"); + } + String contentCoding = response.getHeader("Content-Encoding"); + if (contentCoding != null && !"identity".equals(contentCoding)) + { + if ("gzip".equals(contentCoding)) + { + in = new GZIPInputStream(in); + } + else if ("deflate".equals(contentCoding)) + { + in = new InflaterInputStream(in); + } + else + { + throw new ProtocolException("Unsupported Content-Encoding: " + + contentCoding); + } + } + + // Persistent connections are the default in HTTP/1.1 + boolean doClose = "close".equalsIgnoreCase(getHeader("Connection")) || + "close".equalsIgnoreCase(response.getHeader("Connection")) || + (connection.majorVersion == 1 && connection.minorVersion == 0) || + (response.majorVersion == 1 && response.minorVersion == 0); + + int count = contentLength; + int len = (count > -1) ? count : buffer.length; + len = (len > buffer.length) ? buffer.length : len; + while (len > -1) + { + len = in.read(buffer, 0, len); + if (len < 0) + { + // EOF + connection.closeConnection(); + break; + } + if (notify) + { + responseBodyReader.read(buffer, 0, len); + } + if (count > -1) + { + count -= len; + if (count < 1) + { + if (doClose) + { + connection.closeConnection(); + } + break; + } + } + } + if (notify) + { + responseBodyReader.close(); + } + if (trailer != null) + { + response.getHeaders().putAll(trailer); + notifyHeaderHandlers(trailer); + } + } + + boolean authenticate(Response response, int attempts) + throws IOException + { + String challenge = response.getHeader("WWW-Authenticate"); + if (challenge == null) + { + challenge = response.getHeader("Proxy-Authenticate"); + } + int si = challenge.indexOf(' '); + String scheme = (si == -1) ? challenge : challenge.substring(0, si); + if ("Basic".equalsIgnoreCase(scheme)) + { + Properties params = parseAuthParams(challenge.substring(si + 1)); + String realm = params.getProperty("realm"); + Credentials creds = authenticator.getCredentials(realm, attempts); + String userPass = creds.getUsername() + ':' + creds.getPassword(); + byte[] b_userPass = userPass.getBytes("US-ASCII"); + byte[] b_encoded = BASE64.encode(b_userPass); + String authorization = + scheme + " " + new String(b_encoded, "US-ASCII"); + setHeader("Authorization", authorization); + return true; + } + else if ("Digest".equalsIgnoreCase(scheme)) + { + Properties params = parseAuthParams(challenge.substring(si + 1)); + String realm = params.getProperty("realm"); + String nonce = params.getProperty("nonce"); + String qop = params.getProperty("qop"); + String algorithm = params.getProperty("algorithm"); + String digestUri = getRequestURI(); + Credentials creds = authenticator.getCredentials(realm, attempts); + String username = creds.getUsername(); + String password = creds.getPassword(); + connection.incrementNonce(nonce); + try + { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + final byte[] COLON = { 0x3a }; + + // Calculate H(A1) + md5.reset(); + md5.update(username.getBytes("US-ASCII")); + md5.update(COLON); + md5.update(realm.getBytes("US-ASCII")); + md5.update(COLON); + md5.update(password.getBytes("US-ASCII")); + byte[] ha1 = md5.digest(); + if ("md5-sess".equals(algorithm)) + { + byte[] cnonce = generateNonce(); + md5.reset(); + md5.update(ha1); + md5.update(COLON); + md5.update(nonce.getBytes("US-ASCII")); + md5.update(COLON); + md5.update(cnonce); + ha1 = md5.digest(); + } + String ha1Hex = toHexString(ha1); + + // Calculate H(A2) + md5.reset(); + md5.update(method.getBytes("US-ASCII")); + md5.update(COLON); + md5.update(digestUri.getBytes("US-ASCII")); + if ("auth-int".equals(qop)) + { + byte[] hEntity = null; // TODO hash of entity body + md5.update(COLON); + md5.update(hEntity); + } + byte[] ha2 = md5.digest(); + String ha2Hex = toHexString(ha2); + + // Calculate response + md5.reset(); + md5.update(ha1Hex.getBytes("US-ASCII")); + md5.update(COLON); + md5.update(nonce.getBytes("US-ASCII")); + if ("auth".equals(qop) || "auth-int".equals(qop)) + { + String nc = getNonceCount(nonce); + byte[] cnonce = generateNonce(); + md5.update(COLON); + md5.update(nc.getBytes("US-ASCII")); + md5.update(COLON); + md5.update(cnonce); + md5.update(COLON); + md5.update(qop.getBytes("US-ASCII")); + } + md5.update(COLON); + md5.update(ha2Hex.getBytes("US-ASCII")); + String digestResponse = toHexString(md5.digest()); + + String authorization = scheme + + " username=\"" + username + "\"" + + " realm=\"" + realm + "\"" + + " nonce=\"" + nonce + "\"" + + " uri=\"" + digestUri + "\"" + + " response=\"" + digestResponse + "\""; + setHeader("Authorization", authorization); + return true; + } + catch (NoSuchAlgorithmException e) + { + return false; + } + } + // Scheme not recognised + return false; + } + + Properties parseAuthParams(String text) + { + int len = text.length(); + String key = null; + StringBuffer buf = new StringBuffer(); + Properties ret = new Properties(); + boolean inQuote = false; + for (int i = 0; i < len; i++) + { + char c = text.charAt(i); + if (c == '"') + { + inQuote = !inQuote; + } + else if (c == '=' && key == null) + { + key = buf.toString().trim(); + buf.setLength(0); + } + else if (c == ' ' && !inQuote) + { + String value = unquote(buf.toString().trim()); + ret.put(key, value); + key = null; + buf.setLength(0); + } + else if (c != ',' || (i <(len - 1) && text.charAt(i + 1) != ' ')) + { + buf.append(c); + } + } + if (key != null) + { + String value = unquote(buf.toString().trim()); + ret.put(key, value); + } + return ret; + } + + String unquote(String text) + { + int len = text.length(); + if (len > 0 && text.charAt(0) == '"' && text.charAt(len - 1) == '"') + { + return text.substring(1, len - 1); + } + return text; + } + + /** + * Returns the number of times the specified nonce value has been seen. + * This always returns an 8-byte 0-padded hexadecimal string. + */ + String getNonceCount(String nonce) + { + int nc = connection.getNonceCount(nonce); + String hex = Integer.toHexString(nc); + StringBuffer buf = new StringBuffer(); + for (int i = 8 - hex.length(); i > 0; i--) + { + buf.append('0'); + } + buf.append(hex); + return buf.toString(); + } + + /** + * Client nonce value. + */ + byte[] nonce; + + /** + * Generates a new client nonce value. + */ + byte[] generateNonce() + throws IOException, NoSuchAlgorithmException + { + if (nonce == null) + { + long time = System.currentTimeMillis(); + MessageDigest md5 = MessageDigest.getInstance("MD5"); + md5.update(Long.toString(time).getBytes("US-ASCII")); + nonce = md5.digest(); + } + return nonce; + } + + String toHexString(byte[] bytes) + { + char[] ret = new char[bytes.length * 2]; + for (int i = 0, j = 0; i < bytes.length; i++) + { + int c =(int) bytes[i]; + if (c < 0) + { + c += 0x100; + } + ret[j++] = Character.forDigit(c / 0x10, 0x10); + ret[j++] = Character.forDigit(c % 0x10, 0x10); + } + return new String(ret); + } + + /** + * Parse the specified cookie list and notify the cookie manager. + */ + void handleSetCookie(String text) + { + CookieManager cookieManager = connection.getCookieManager(); + if (cookieManager == null) + { + return; + } + String name = null; + String value = null; + String comment = null; + String domain = connection.getHostName(); + String path = this.path; + int lsi = path.lastIndexOf('/'); + if (lsi != -1) + { + path = path.substring(0, lsi); + } + boolean secure = false; + Date expires = null; + + int len = text.length(); + String attr = null; + StringBuffer buf = new StringBuffer(); + boolean inQuote = false; + for (int i = 0; i <= len; i++) + { + char c =(i == len) ? '\u0000' : text.charAt(i); + if (c == '"') + { + inQuote = !inQuote; + } + else if (!inQuote) + { + if (c == '=' && attr == null) + { + attr = buf.toString().trim(); + buf.setLength(0); + } + else if (c == ';' || i == len || c == ',') + { + String val = unquote(buf.toString().trim()); + if (name == null) + { + name = attr; + value = val; + } + else if ("Comment".equalsIgnoreCase(attr)) + { + comment = val; + } + else if ("Domain".equalsIgnoreCase(attr)) + { + domain = val; + } + else if ("Path".equalsIgnoreCase(attr)) + { + path = val; + } + else if ("Secure".equalsIgnoreCase(val)) + { + secure = true; + } + else if ("Max-Age".equalsIgnoreCase(attr)) + { + int delta = Integer.parseInt(val); + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(System.currentTimeMillis()); + cal.add(Calendar.SECOND, delta); + expires = cal.getTime(); + } + else if ("Expires".equalsIgnoreCase(attr)) + { + DateFormat dateFormat = new HTTPDateFormat(); + try + { + expires = dateFormat.parse(val); + } + catch (ParseException e) + { + // if this isn't a valid date, it may be that + // the value was returned unquoted; in that case, we + // want to continue buffering the value + buf.append(c); + continue; + } + } + attr = null; + buf.setLength(0); + // case EOL + if (i == len || c == ',') + { + Cookie cookie = new Cookie(name, value, comment, domain, + path, secure, expires); + cookieManager.setCookie(cookie); + } + if (c == ',') + { + // Reset cookie fields + name = null; + value = null; + comment = null; + domain = connection.getHostName(); + path = this.path; + if (lsi != -1) + { + path = path.substring(0, lsi); + } + secure = false; + expires = null; + } + } + else + { + buf.append(c); + } + } + else + { + buf.append(c); + } + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/RequestBodyWriter.java b/libjava/classpath/gnu/java/net/protocol/http/RequestBodyWriter.java new file mode 100644 index 00000000000..05d98ebb81a --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/RequestBodyWriter.java @@ -0,0 +1,69 @@ +/* RequestBodyWriter.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Callback interface for writing request body content. + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface RequestBodyWriter +{ + + /** + * Returns the total number of bytes that will be written in a single pass + * by this writer. + */ + int getContentLength(); + + /** + * Initialises the writer. + * This will be called before each pass. + */ + void reset(); + + /** + * Writes body content to the supplied buffer. + * @param buffer the content buffer + * @return the number of bytes written + */ + int write(byte[] buffer); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/Response.java b/libjava/classpath/gnu/java/net/protocol/http/Response.java new file mode 100644 index 00000000000..29dc28b17d3 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/Response.java @@ -0,0 +1,185 @@ +/* Response.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.util.Date; + +/** + * An HTTP response. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Response +{ + + /** + * The HTTP major version of the server issuing the response. + */ + protected final int majorVersion; + + /** + * The HTTP minor version of the server issuing the response. + */ + protected final int minorVersion; + + /** + * The HTTP status code of the response. + */ + protected final int code; + + /** + * The class of the response. This is the most significant digit of the + * status code. + * <dl> + * <dt><code>1xx</code></dt> <dd>Informational response</dd> + * <dt><code>2xx</code></dt> <dd>Success</dd> + * <dt><code>3xx</code></dt> <dd>Redirection</dd> + * <dt><code>4xx</code></dt> <dd>Client error</dd> + * <dt><code>5xx</code></dt> <dd>Server error</dd> + * </dl> + */ + protected final int codeClass; + + /** + * Human-readable text of the response. + */ + protected final String message; + + /** + * The response headers. + */ + protected final Headers headers; + + /** + * Constructs a new response with the specified parameters. + */ + protected Response(int majorVersion, int minorVersion, int code, + int codeClass, String message, + Headers headers) + { + this.majorVersion = majorVersion; + this.minorVersion = minorVersion; + this.code = code; + this.codeClass = codeClass; + this.message = message; + this.headers = headers; + } + + /** + * Returns the HTTP major version of the server issuing the response. + * @see #majorVersion + */ + public int getMajorVersion() + { + return majorVersion; + } + + /** + * Returns the HTTP minor version of the server issuing the response. + * @see #minorVersion + */ + public int getMinorVersion() + { + return minorVersion; + } + + /** + * Returns the HTTP status code of the response. + * @see #code + */ + public int getCode() + { + return code; + } + + /** + * Returns the class of the response. + * @see #codeClass + */ + public int getCodeClass() + { + return codeClass; + } + + /** + * Returns the human-readable text of the response. + * @see #message + */ + public String getMessage() + { + return message; + } + + /** + * Returns the headers in the response. + */ + public Headers getHeaders() + { + return headers; + } + + /** + * Returns the header value for the specified name. + * @param name the header name + */ + public String getHeader(String name) + { + return headers.getValue(name); + } + + /** + * Returns the header value for the specified name as an integer. + * @param name the header name + */ + public int getIntHeader(String name) + { + return headers.getIntValue(name); + } + + /** + * Returns the header value for the specified name as a date. + * @param name the header name + */ + public Date getDateHeader(String name) + { + return headers.getDateValue(name); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/ResponseBodyReader.java b/libjava/classpath/gnu/java/net/protocol/http/ResponseBodyReader.java new file mode 100644 index 00000000000..49e1b376f0f --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/ResponseBodyReader.java @@ -0,0 +1,70 @@ +/* ResponseBodyReader.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Callback interface for receiving notification of response body content. + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface ResponseBodyReader +{ + + /** + * Indicate whether this reader is interested in the specified response. + * If it returns false, it will not receive body content notifications for + * that response. + */ + boolean accept(Request request, Response response); + + /** + * Receive notification of body content. + * @param buffer the content buffer + * @param offset the offset within the buffer that content starts + * @param length the length of the content + */ + void read(byte[] buffer, int offset, int length); + + /** + * Notifies the reader that the end of the content was reached. + */ + void close(); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/ResponseHeaderHandler.java b/libjava/classpath/gnu/java/net/protocol/http/ResponseHeaderHandler.java new file mode 100644 index 00000000000..8e4e6492acf --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/ResponseHeaderHandler.java @@ -0,0 +1,57 @@ +/* ResponseHeaderHandler.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +/** + * Callback interface for objects that wish to be notified of response + * header values. + * @see Request#setHeaderHandler(String) + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface ResponseHeaderHandler +{ + + /** + * Sets the value for the header associated with this handler. + */ + void setValue(String value); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/SimpleCookieManager.java b/libjava/classpath/gnu/java/net/protocol/http/SimpleCookieManager.java new file mode 100644 index 00000000000..8947471885c --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/SimpleCookieManager.java @@ -0,0 +1,140 @@ +/* CookieManager.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * A simple non-persistent cookie manager. This class can be extended to + * provide cookie persistence. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class SimpleCookieManager + implements CookieManager +{ + + /** + * The cookie cache. + * This is a dictionary mapping domains to maps of cookies by name. + */ + protected Map cookies; + + /** + * Constructor. + */ + public SimpleCookieManager() + { + cookies = new HashMap(); + } + + public void setCookie(Cookie cookie) + { + String domain = cookie.getDomain(); + Map map =(Map) cookies.get(domain); + if (map == null) + { + map = new HashMap(); + cookies.put(domain, map); + } + String name = cookie.getName(); + map.put(name, cookie); // will replace a cookie of the same name + } + + public Cookie[] getCookies(String host, boolean secure, String path) + { + List matches = new ArrayList(); + Date now = new Date(); + if (Character.isLetter(host.charAt(0))) + { + int di = host.indexOf('.'); + while (di != -1) + { + addCookies(matches, host, secure, path, now); + host = host.substring(di); + di = host.indexOf('.', 1); + } + } + addCookies(matches, host, secure, path, now); + Cookie[] ret = new Cookie[matches.size()]; + matches.toArray(ret); + return ret; + } + + private void addCookies(List matches, String domain, boolean secure, + String path, Date now) + { + Map map = (Map) cookies.get(domain); + if (map != null) + { + List expired = new ArrayList(); + for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + Cookie cookie = (Cookie) entry.getValue(); + Date expires = cookie.getExpiryDate(); + if (expires != null && expires.before(now)) + { + expired.add(entry.getKey()); + continue; + } + if (secure && !cookie.isSecure()) + { + continue; + } + if (path.startsWith(cookie.getPath())) + { + matches.add(cookie); + } + } + // Good housekeeping + for (Iterator i = expired.iterator(); i.hasNext(); ) + { + map.remove(i.next()); + } + } + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/event/ConnectionEvent.java b/libjava/classpath/gnu/java/net/protocol/http/event/ConnectionEvent.java new file mode 100644 index 00000000000..3f6f5454e73 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/event/ConnectionEvent.java @@ -0,0 +1,81 @@ +/* ConnectionEvent.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http.event; + +import java.util.EventObject; + +/** + * A connection event. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class ConnectionEvent + extends EventObject +{ + + /** + * The connection closed event type. + */ + public static final int CONNECTION_CLOSED = 0; + + /** + * The type of this event. + */ + protected int type; + + /** + * Constructs a connection event with the specified source and type. + */ + public ConnectionEvent(Object source, int type) + { + super(source); + this.type = type; + } + + /** + * Returns the type of this event. + * @see #type + */ + public int getType() + { + return type; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/event/ConnectionListener.java b/libjava/classpath/gnu/java/net/protocol/http/event/ConnectionListener.java new file mode 100644 index 00000000000..073e89d4407 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/event/ConnectionListener.java @@ -0,0 +1,58 @@ +/* ConnectionListener.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http.event; + +import java.util.EventListener; + +/** + * A connection listener. + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface ConnectionListener + extends EventListener +{ + + /** + * Callback invoked when the associated connection is closed. + */ + void connectionClosed(ConnectionEvent event); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/event/RequestEvent.java b/libjava/classpath/gnu/java/net/protocol/http/event/RequestEvent.java new file mode 100644 index 00000000000..281c621f33f --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/event/RequestEvent.java @@ -0,0 +1,107 @@ +/* RequestEvent.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http.event; + +import gnu.java.net.protocol.http.Request; + +import java.util.EventObject; + +/** + * A request event. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class RequestEvent + extends EventObject +{ + + /** + * The request created event type. + */ + public static final int REQUEST_CREATED = 0; + + /** + * The request sending event type. + */ + public static final int REQUEST_SENDING = 1; + + /** + * The request sent event type. + */ + public static final int REQUEST_SENT = 2; + + /** + * The type of this event. + */ + protected int type; + + /** + * The request associated with this event. + */ + protected Request request; + + /** + * Constructs a request event with the specified source, type, and request. + */ + public RequestEvent(Object source, int type, Request request) + { + super(source); + this.type = type; + this.request = request; + } + + /** + * Returns the type of this event. + * @see #type + */ + public int getType() + { + return type; + } + + /** + * Returns the request associated with this event. + */ + public Request getRequest() + { + return request; + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/event/RequestListener.java b/libjava/classpath/gnu/java/net/protocol/http/event/RequestListener.java new file mode 100644 index 00000000000..c880fbce6f0 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/event/RequestListener.java @@ -0,0 +1,70 @@ +/* RequestListener.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http.event; + +import java.util.EventListener; + +/** + * A request listener. + * + * @author Chris Burdess (dog@gnu.org) + */ +public interface RequestListener + extends EventListener +{ + + /** + * Callback invoked when a request is created from the associated + * connection. + */ + void requestCreated(RequestEvent event); + + /** + * Callback invoked when the request has been initialised with all data + * and before sending this data to the server. + */ + void requestSending(RequestEvent event); + + /** + * Callback invoked after all request data has been sent to the server. + */ + void requestSent(RequestEvent event); + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/http/event/package.html b/libjava/classpath/gnu/java/net/protocol/http/event/package.html new file mode 100644 index 00000000000..6aed0fc0169 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/event/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.net.protocol.http.event package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.net.protocol.http.event</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/net/protocol/http/package.html b/libjava/classpath/gnu/java/net/protocol/http/package.html new file mode 100644 index 00000000000..8cf7c1e1663 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/http/package.html @@ -0,0 +1,76 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.net.protocol.http package. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.net.protocol.http</title></head> + +<body> + +<p> +This package contains an HTTP/1.1 client, as described in RFC 2616. +It supports the following features: +<ul> +<li>Persistent connections</li> +<li>Basic and Digest authentication (RFC 2617)</li> +<li>HTTPS</li> +<li>HTTP proxies</li> +<li>HTTP/1.0 compatibility</li> +<li>Support for WebDAV methods and other HTTP extensions</li> +<li>Automatic decoding of the chunked transfer-coding</li> +<li>Parsing of HTTP date headers</li> +<li>Support for the 100-continue expectation</li> +</ul> +</p> + +<p> +The API is similar to the <a href='http://www.webdav.org/neon/'>neon</a> +WebDAV/HTTP library. A logical connection to the server is instantiated, +and multiple requests can be issued for this connection. Each request +has an atomic <code>dispatch</code> method which returns the response. +All I/O, authentication, etc is handled by registering callback objects +with the request prior to dispatch, which are notified during the dispatch +procedure as necessary. Simple byte-array content callbacks are supplied +which can manage any request/response content that fits in available memory. +</p> + +<p> +An URL stream handler is provided, supporting the full HttpURLConnection +specification. +</p> + +</body> diff --git a/libjava/classpath/gnu/java/net/protocol/https/Handler.java b/libjava/classpath/gnu/java/net/protocol/https/Handler.java new file mode 100644 index 00000000000..2b137517021 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/https/Handler.java @@ -0,0 +1,76 @@ +/* Handler.java -- + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.https; + +import gnu.java.net.protocol.http.HTTPConnection; +import gnu.java.net.protocol.http.HTTPURLConnection; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * An HTTPS URL stream handler. + * + * @author Chris Burdess (dog@gnu.org) + */ +public class Handler + extends URLStreamHandler +{ + + /** + * Returns the default HTTPS port (443). + */ + protected int getDefaultPort() + { + return HTTPConnection.HTTPS_PORT; + } + + /** + * Returns an HTTPURLConnection for the given URL. + */ + public URLConnection openConnection(URL url) + throws IOException + { + return new HTTPURLConnection(url); + } + +} + diff --git a/libjava/classpath/gnu/java/net/protocol/jar/Connection.java b/libjava/classpath/gnu/java/net/protocol/jar/Connection.java new file mode 100644 index 00000000000..bd7a80da739 --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/jar/Connection.java @@ -0,0 +1,170 @@ +/* Connection - jar url connection for java.net + Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.jar; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.JarURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Hashtable; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.zip.ZipFile; + +/** + * This subclass of java.net.JarURLConnection models a URLConnection via + * the "jar" protocol. + * + * @author Kresten Krab Thorup (krab@gnu.org) + */ +public final class Connection extends JarURLConnection +{ + private JarFile jar_file; + private JarEntry jar_entry; + private URL jar_url; + + public static class JarFileCache + { + private static Hashtable cache = new Hashtable(); + private static final int READBUFSIZE = 4*1024; + + public static synchronized JarFile get (URL url) throws IOException + { + JarFile jf = (JarFile) cache.get (url); + + if (jf != null) + return jf; + + if ("file".equals (url.getProtocol())) + { + File f = new File (url.getFile()); + jf = new JarFile (f, true, ZipFile.OPEN_READ); + } + else + { + URLConnection urlconn = url.openConnection(); + InputStream is = urlconn.getInputStream(); + byte[] buf = new byte [READBUFSIZE]; + File f = File.createTempFile ("cache", "jar"); + FileOutputStream fos = new FileOutputStream (f); + int len = 0; + + while ((len = is.read (buf)) != -1) + { + fos.write (buf, 0, len); + } + + fos.close(); + // Always verify the Manifest, open read only and delete when done. + jf = new JarFile (f, true, + ZipFile.OPEN_READ | ZipFile.OPEN_DELETE); + } + + cache.put (url, jf); + + return jf; + } + } + + protected Connection(URL url) + throws MalformedURLException + { + super(url); + } + + public synchronized void connect() throws IOException + { + // Call is ignored if already connected. + if (connected) + return; + + jar_url = getJarFileURL(); + jar_file = JarFileCache.get (jar_url); + String entry_name = getEntryName(); + + if (entry_name != null + && !entry_name.equals ("")) + { + jar_entry = (JarEntry) jar_file.getEntry (entry_name); + + if(jar_entry == null) + throw new IOException ("No entry for " + entry_name + " exists."); + } + + connected = true; + } + + public InputStream getInputStream() throws IOException + { + if (!connected) + connect(); + + if (! doInput) + throw new ProtocolException("Can't open InputStream if doInput is false"); + + if (jar_entry == null) + throw new IOException (jar_url + " couldn't be found."); + + return jar_file.getInputStream (jar_entry); + } + + public synchronized JarFile getJarFile() throws IOException + { + if (!connected) + connect(); + + if (! doInput) + throw new ProtocolException("Can't open JarFile if doInput is false"); + + return jar_file; + } + + public int getContentLength() + { + if (!connected) + return -1; + + return (int) jar_entry.getSize(); + } +} diff --git a/libjava/classpath/gnu/java/net/protocol/jar/Handler.java b/libjava/classpath/gnu/java/net/protocol/jar/Handler.java new file mode 100644 index 00000000000..316d8cb02be --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/jar/Handler.java @@ -0,0 +1,173 @@ +/* gnu.java.net.protocol.jar.Handler - jar protocol handler for java.net + Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.jar; + +import gnu.java.net.URLParseError; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * @author Kresten Krab Thorup (krab@gnu.org) + */ +public class Handler extends URLStreamHandler +{ + /** + * A do nothing constructor + */ + public Handler() + { + } + + /** + * This method returs a new JarURLConnection for the specified URL + * + * @param url The URL to return a connection for + * + * @return The URLConnection + * + * @exception IOException If an error occurs + */ + protected URLConnection openConnection(URL url) throws IOException + { + return new Connection(url); + } + + /** + * This method overrides URLStreamHandler's for parsing url of protocol "jar" + * + * @param url The URL object in which to store the results + * @param url_string The String-ized URL to parse + * @param start The position in the string to start scanning from + * @param end The position in the string to stop scanning + */ + protected void parseURL (URL url, String url_string, int start, int end) + { + // This method does not throw an exception or return a value. Thus our + // strategy when we encounter an error in parsing is to return without + // doing anything. + String file = url.getFile(); + + if (!file.equals("")) + { //has context url + url_string = url_string.substring (start, end); + if (url_string.startsWith("/")) + { //url string is an absolute path + int idx = file.lastIndexOf ("!/"); + + if (idx < 0) + throw new URLParseError("no !/ in spec"); + + file = file.substring (0, idx + 1) + url_string; + } + else if (url_string.length() > 0) + { + int idx = file.lastIndexOf ("/"); + if (idx == -1) //context path is weird + file = "/" + url_string; + else if (idx == (file.length() - 1)) + //just concatenate two parts + file = file + url_string; + else + // according to Java API Documentation, here is a little different + // with URLStreamHandler.parseURL + // but JDK seems doesn't handle it well + file = file.substring(0, idx + 1) + url_string; + } + + setURL (url, "jar", url.getHost(), url.getPort(), file, null); + return; + } + + // Bunches of things should be true. Make sure. + if (end < start) + return; + if (end - start < 2) + return; + if (start > url_string.length()) + return; + + // Skip remains of protocol + url_string = url_string.substring (start, end); + + int jar_stop; + if ((jar_stop = url_string.indexOf("!/")) < 0) + throw new URLParseError("no !/ in spec"); + + try + { + new URL(url_string.substring (0, jar_stop)); + } + catch (MalformedURLException e) + { + throw new URLParseError("invalid inner URL: " + e.getMessage()); + } + + if (!url.getProtocol().equals ("jar") ) + throw new URLParseError("unexpected protocol " + url.getProtocol()); + + setURL (url, "jar", url.getHost(), url.getPort(), url_string, null); + } + + /** + * This method converts a Jar URL object into a String. + * + * @param url The URL object to convert + */ + protected String toExternalForm (URL url) + { + String file = url.getFile(); + String ref = url.getRef(); + + // return "jar:" + file; + // Performance!!: + // Do the concatenation manually to avoid resize StringBuffer's + // internal buffer. The length of ref is not taken into consideration + // as it's a rare path. + StringBuffer sb = new StringBuffer (file.length() + 5); + sb.append ("jar:"); + sb.append (file); + if (ref != null) + sb.append('#').append(ref); + return sb.toString(); + } +} diff --git a/libjava/classpath/gnu/java/net/protocol/jar/package.html b/libjava/classpath/gnu/java/net/protocol/jar/package.html new file mode 100644 index 00000000000..dcd263d59dc --- /dev/null +++ b/libjava/classpath/gnu/java/net/protocol/jar/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.net.protocol.jar package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.net.protocol.jar</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/nio/ChannelInputStream.java b/libjava/classpath/gnu/java/nio/ChannelInputStream.java new file mode 100644 index 00000000000..675a62f3d88 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/ChannelInputStream.java @@ -0,0 +1,79 @@ +/* ChannelInputStream.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.IllegalBlockingModeException; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.SelectableChannel; + +/** + * @author Michael Koch + */ +public final class ChannelInputStream extends InputStream +{ + private ReadableByteChannel ch; + + public ChannelInputStream (ReadableByteChannel ch) + { + super(); + + this.ch = ch; + } + + public int read() throws IOException + { + if (ch instanceof SelectableChannel + && (! ((SelectableChannel) ch).isBlocking())) + throw new IllegalBlockingModeException(); + + ByteBuffer buffer = ByteBuffer.allocate(1); + int result = ch.read(buffer); + + if (result == -1) + return -1; + + if (result == 0) + throw new IOException("Could not read from channel"); + + return buffer.get(0); + } +} diff --git a/libjava/classpath/gnu/java/nio/ChannelOutputStream.java b/libjava/classpath/gnu/java/nio/ChannelOutputStream.java new file mode 100644 index 00000000000..08323eacec4 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/ChannelOutputStream.java @@ -0,0 +1,67 @@ +/* ChannelOutputStream.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.WritableByteChannel; + +/** + * @author Michael Koch + */ +public final class ChannelOutputStream extends OutputStream +{ + private WritableByteChannel ch; + + public ChannelOutputStream (WritableByteChannel ch) + { + super(); + + this.ch = ch; + } + + public void write (int value) throws IOException + { + ByteBuffer buffer = ByteBuffer.allocate (1); + buffer.put ((byte) (value & 0xff)); + buffer.flip(); + ch.write (buffer); + } +} diff --git a/libjava/classpath/gnu/java/nio/ChannelReader.java b/libjava/classpath/gnu/java/nio/ChannelReader.java new file mode 100644 index 00000000000..44fe6625a3b --- /dev/null +++ b/libjava/classpath/gnu/java/nio/ChannelReader.java @@ -0,0 +1,211 @@ +/* ChannelReader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.io.Reader; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.channels.ReadableByteChannel; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CoderResult; +import java.nio.charset.CodingErrorAction; + +/** + * A Reader implementation that works using a ReadableByteChannel and a + * CharsetDecoder. + * + * <p> + * This is a bridge between NIO <-> IO character decoding. + * </p> + * + * @author Robert Schuster + */ +public class ChannelReader extends Reader +{ + + private static final int DEFAULT_BUFFER_CAP = 8192; + + private ReadableByteChannel channel; + + private CharsetDecoder decoder; + + private ByteBuffer byteBuffer; + + private CharBuffer charBuffer; + + public ChannelReader(ReadableByteChannel channel, CharsetDecoder decoder, + int minBufferCap) + { + this.channel = channel; + this.decoder = decoder; + + // JDK reports errors, so we do the same. + decoder.onMalformedInput(CodingErrorAction.REPORT); + decoder.onUnmappableCharacter(CodingErrorAction.REPORT); + decoder.reset(); + + int size = (minBufferCap == -1) ? DEFAULT_BUFFER_CAP : minBufferCap; + + // Allocates the buffers and prepares them for reading, because that is the + // first operation being done on them. + byteBuffer = ByteBuffer.allocate(size); + byteBuffer.flip(); + charBuffer = CharBuffer.allocate((int) (size * decoder.averageCharsPerByte())); + } + + public int read(char[] buf, int offset, int count) throws IOException + { + // I declared channel being null meaning that the reader is closed. + if (!channel.isOpen()) + throw new IOException("Reader was already closed."); + + // I declared decoder being null meaning that there is no more data to read + // and convert. + if (decoder == null) + return -1; + + // Stores the amount of character being read. It -1 so that if no conversion + // occured the caller will see this as an 'end of file'. + int sum = -1; + + // Copies any characters which may be left from the last invocation into the + // destination array. + if (charBuffer.remaining() > 0) + { + sum = Math.min(count, charBuffer.remaining()); + charBuffer.get(buf, offset, sum); + + // Updates the control variables according to the latest copy operation. + offset += sum; + count -= sum; + } + + // Copies the character which have not been put in the destination array to + // the beginning. If data is actually copied count will be 0. If no data is + // copied count is >0 and we can now convert some more characters. + charBuffer.compact(); + + int converted = 0; + boolean last = false; + + while (count != 0) + { + // Tries to convert some bytes (Which will intentionally fail in the + // first place because we have not read any bytes yet.) + CoderResult result = decoder.decode(byteBuffer, charBuffer, last); + if (result.isMalformed() || result.isUnmappable()) + { + // JDK throws exception when bytes are malformed for sure. + // FIXME: Unsure what happens when a character is simply + // unmappable. + result.throwException(); + } + + // Marks that we should end this loop regardless whether the caller + // wants more chars or not, when this was the last conversion. + if (last) + { + decoder = null; + } + else if (result.isUnderflow()) + { + // We need more bytes to do the conversion. + + // Copies the not yet converted bytes to the beginning making it + // being able to receive more bytes. + byteBuffer.compact(); + + // Reads in another bunch of bytes for being converted. + if (channel.read(byteBuffer) == -1) + { + // If there is no more data available in the channel we mark + // that state for the final character conversion run which is + // done in the next loop iteration. + last = true; + } + + // Prepares the byteBuffer for the next character conversion run. + byteBuffer.flip(); + } + + // Prepares the charBuffer for being drained. + charBuffer.flip(); + + converted = Math.min(count, charBuffer.remaining()); + charBuffer.get(buf, offset, converted); + + // Copies characters which have not yet being copied into the char-Array + // to the beginning making it possible to read them later (If data is + // really copied here, then the caller has received enough characters so + // far.). + charBuffer.compact(); + + // Updates the control variables according to the latest copy operation. + offset += converted; + count -= converted; + + // Updates the amount of transferred characters. + sum += converted; + + if (decoder == null) + { + break; + } + + // Now that more characters have been transfered we let the loop decide + // what to do next. + } + + // Makes the charBuffer ready for reading on the next invocation. + charBuffer.flip(); + + return sum; + } + + public void close() throws IOException + { + channel.close(); + + // Makes sure all intermediate data is released by the decoder. + if (decoder != null) + decoder.reset(); + } + +} diff --git a/libjava/classpath/gnu/java/nio/DatagramChannelImpl.java b/libjava/classpath/gnu/java/nio/DatagramChannelImpl.java new file mode 100644 index 00000000000..51c7031fe69 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/DatagramChannelImpl.java @@ -0,0 +1,297 @@ +/* DatagramChannelImpl.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.net.PlainDatagramSocketImpl; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.net.SocketTimeoutException; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.DatagramChannel; +import java.nio.channels.NotYetConnectedException; +import java.nio.channels.spi.SelectorProvider; + +/** + * @author Michael Koch + */ +public final class DatagramChannelImpl extends DatagramChannel +{ + private NIODatagramSocket socket; + + /** + * Indicates whether this channel initiated whatever operation + * is being invoked on our datagram socket. + */ + private boolean inChannelOperation; + + /** + * Indicates whether our datagram socket should ignore whether + * we are set to non-blocking mode. Certain operations on our + * socket throw an <code>IllegalBlockingModeException</code> if + * we are in non-blocking mode, <i>except</i> if the operation + * is initiated by us. + */ + public final boolean isInChannelOperation() + { + return inChannelOperation; + } + + /** + * Sets our indicator of whether we are initiating an I/O operation + * on our socket. + */ + public final void setInChannelOperation(boolean b) + { + inChannelOperation = b; + } + + protected DatagramChannelImpl (SelectorProvider provider) + throws IOException + { + super (provider); + socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this); + configureBlocking(true); + } + + public DatagramSocket socket () + { + return socket; + } + + protected void implCloseSelectableChannel () + throws IOException + { + socket.close (); + } + + protected void implConfigureBlocking (boolean blocking) + throws IOException + { + socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT); + } + + public DatagramChannel connect (SocketAddress remote) + throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + socket.connect (remote); + return this; + } + + public DatagramChannel disconnect () + throws IOException + { + socket.disconnect (); + return this; + } + + public boolean isConnected () + { + return socket.isConnected (); + } + + public int write (ByteBuffer src) + throws IOException + { + if (!isConnected ()) + throw new NotYetConnectedException (); + + return send (src, socket.getRemoteSocketAddress()); + } + + public long write (ByteBuffer[] srcs, int offset, int length) + throws IOException + { + if (!isConnected()) + throw new NotYetConnectedException(); + + if ((offset < 0) + || (offset > srcs.length) + || (length < 0) + || (length > (srcs.length - offset))) + throw new IndexOutOfBoundsException(); + + long result = 0; + + for (int index = offset; index < offset + length; index++) + result += write (srcs [index]); + + return result; + } + + public int read (ByteBuffer dst) + throws IOException + { + if (!isConnected ()) + throw new NotYetConnectedException (); + + int remaining = dst.remaining(); + receive (dst); + return remaining - dst.remaining(); + } + + public long read (ByteBuffer[] dsts, int offset, int length) + throws IOException + { + if (!isConnected()) + throw new NotYetConnectedException(); + + if ((offset < 0) + || (offset > dsts.length) + || (length < 0) + || (length > (dsts.length - offset))) + throw new IndexOutOfBoundsException(); + + long result = 0; + + for (int index = offset; index < offset + length; index++) + result += read (dsts [index]); + + return result; + } + + public SocketAddress receive (ByteBuffer dst) + throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + try + { + DatagramPacket packet; + int len = dst.capacity() - dst.position(); + + if (dst.hasArray()) + { + packet = new DatagramPacket (dst.array(), + dst.arrayOffset() + dst.position(), + len); + } + else + { + packet = new DatagramPacket (new byte [len], len); + } + + boolean completed = false; + + try + { + begin(); + setInChannelOperation(true); + socket.receive (packet); + completed = true; + } + finally + { + end (completed); + setInChannelOperation(false); + } + + if (!dst.hasArray()) + { + dst.put (packet.getData(), packet.getOffset(), packet.getLength()); + } + else + { + dst.position (dst.position() + packet.getLength()); + } + + return packet.getSocketAddress(); + } + catch (SocketTimeoutException e) + { + return null; + } + } + + public int send (ByteBuffer src, SocketAddress target) + throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + if (target instanceof InetSocketAddress + && ((InetSocketAddress) target).isUnresolved()) + throw new IOException("Target address not resolved"); + + byte[] buffer; + int offset = 0; + int len = src.remaining(); + + if (src.hasArray()) + { + buffer = src.array(); + offset = src.arrayOffset() + src.position(); + } + else + { + buffer = new byte [len]; + src.get (buffer); + } + + DatagramPacket packet = new DatagramPacket (buffer, offset, len, target); + + boolean completed = false; + try + { + begin(); + setInChannelOperation(true); + socket.send(packet); + completed = true; + } + finally + { + end (completed); + setInChannelOperation(false); + } + + if (src.hasArray()) + { + src.position (src.position() + len); + } + + return len; + } +} diff --git a/libjava/classpath/gnu/java/nio/DatagramChannelSelectionKey.java b/libjava/classpath/gnu/java/nio/DatagramChannelSelectionKey.java new file mode 100644 index 00000000000..698e07e348f --- /dev/null +++ b/libjava/classpath/gnu/java/nio/DatagramChannelSelectionKey.java @@ -0,0 +1,61 @@ +/* DatagramChannelSelectionKey.java -- + Copyright (C) 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.nio.channels.spi.AbstractSelectableChannel; + +/** + * @author Michael Koch + */ +public final class DatagramChannelSelectionKey + extends SelectionKeyImpl +{ + public DatagramChannelSelectionKey (AbstractSelectableChannel channel, + SelectorImpl selector) + { + super (channel, selector); + } + + public int getNativeFD() + { + NIODatagramSocket socket = + (NIODatagramSocket) ((DatagramChannelImpl) ch).socket(); + return socket.getPlainDatagramSocketImpl().getNativeFD(); + } +} diff --git a/libjava/classpath/gnu/java/nio/FileLockImpl.java b/libjava/classpath/gnu/java/nio/FileLockImpl.java new file mode 100644 index 00000000000..245fa73402a --- /dev/null +++ b/libjava/classpath/gnu/java/nio/FileLockImpl.java @@ -0,0 +1,82 @@ +/* FileLockImpl.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.nio.channels.FileChannelImpl; + +import java.io.IOException; +import java.nio.channels.FileLock; + +/** + * @author Michael Koch + * @since 1.4 + */ +public class FileLockImpl extends FileLock +{ + private FileChannelImpl ch; + + public FileLockImpl (FileChannelImpl channel, long position, + long size, boolean shared) + { + super (channel, position, size, shared); + ch = channel; + } + + protected void finalize() + { + try + { + release(); + } + catch (IOException e) + { + // Ignore this. + } + } + + public boolean isValid () + { + return channel().isOpen(); + } + + public synchronized void release () throws IOException + { + ch.unlock(position(), size()); + } +} diff --git a/libjava/classpath/gnu/java/nio/InputStreamChannel.java b/libjava/classpath/gnu/java/nio/InputStreamChannel.java new file mode 100644 index 00000000000..a3dffe24554 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/InputStreamChannel.java @@ -0,0 +1,88 @@ +/* InputStreamChannel.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.ReadableByteChannel; + +/** + * @author Michael Koch + */ +public final class InputStreamChannel implements ReadableByteChannel +{ + private boolean closed = false; + private InputStream in; + + public InputStreamChannel (InputStream in) + { + super(); + this.in = in; + } + + public void close() throws IOException + { + if (!closed) + { + in.close(); + closed = true; + } + } + + public boolean isOpen() + { + return !closed; + } + + public int read (ByteBuffer dst) throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + byte[] buffer = new byte [dst.remaining()]; + int readBytes = in.read (buffer); + + if (readBytes > 0) + dst.put (buffer, 0, readBytes); + + return readBytes; + } +} diff --git a/libjava/classpath/gnu/java/nio/NIOConstants.java b/libjava/classpath/gnu/java/nio/NIOConstants.java new file mode 100644 index 00000000000..bb5b3b7f940 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/NIOConstants.java @@ -0,0 +1,47 @@ +/* NIOConstants.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +/** + * @author Michael Koch + */ +public final class NIOConstants +{ + public static final int DEFAULT_TIMEOUT = 50; +} diff --git a/libjava/classpath/gnu/java/nio/NIODatagramSocket.java b/libjava/classpath/gnu/java/nio/NIODatagramSocket.java new file mode 100644 index 00000000000..f23236eca88 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/NIODatagramSocket.java @@ -0,0 +1,71 @@ +/* NIODatagramSocket.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.net.PlainDatagramSocketImpl; + +import java.net.DatagramSocket; +import java.nio.channels.DatagramChannel; + +/** + * @author Michael Koch + */ +public final class NIODatagramSocket extends DatagramSocket +{ + private PlainDatagramSocketImpl impl; + private DatagramChannelImpl channel; + + public NIODatagramSocket (PlainDatagramSocketImpl impl, + DatagramChannelImpl channel) + { + super (impl); + this.impl = impl; + this.channel = channel; + } + + public final PlainDatagramSocketImpl getPlainDatagramSocketImpl() + { + return impl; + } + + public final DatagramChannel getChannel() + { + return channel; + } +} diff --git a/libjava/classpath/gnu/java/nio/NIOServerSocket.java b/libjava/classpath/gnu/java/nio/NIOServerSocket.java new file mode 100644 index 00000000000..fc4d0dbe0da --- /dev/null +++ b/libjava/classpath/gnu/java/nio/NIOServerSocket.java @@ -0,0 +1,106 @@ +/* NIOServerSocket.java -- + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.net.PlainSocketImpl; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; + +/** + * @author Michael Koch (konqueror@gmx.de) + */ +public final class NIOServerSocket extends ServerSocket +{ + private ServerSocketChannelImpl channel; + + protected NIOServerSocket (ServerSocketChannelImpl channel) + throws IOException + { + super(); + this.channel = channel; + } + + public PlainSocketImpl getPlainSocketImpl() + { + try + { + final Object t = this; + final Method method = ServerSocket.class.getDeclaredMethod("getImpl", new Class[0]); + method.setAccessible(true); + PrivilegedExceptionAction action = new PrivilegedExceptionAction() + { + public Object run() throws Exception + { + return method.invoke(t, new Object[0]); + } + }; + return (PlainSocketImpl) AccessController.doPrivileged(action); + } + catch (Exception e) + { + // This should never happen. + Error error = new InternalError("unable to invoke method ServerSocket.getImpl()"); + error.initCause(e); + throw error; + } + } + + public ServerSocketChannel getChannel() + { + return channel; + } + + public Socket accept() throws IOException + { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.checkListen (getLocalPort()); + + SocketChannel socketChannel = channel.provider().openSocketChannel(); + implAccept (socketChannel.socket()); + return socketChannel.socket(); + } +} diff --git a/libjava/classpath/gnu/java/nio/NIOSocket.java b/libjava/classpath/gnu/java/nio/NIOSocket.java new file mode 100644 index 00000000000..4d812bf44ba --- /dev/null +++ b/libjava/classpath/gnu/java/nio/NIOSocket.java @@ -0,0 +1,77 @@ +/* NIOSocket.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.net.PlainSocketImpl; +import java.io.IOException; +import java.net.Socket; +import java.nio.channels.SocketChannel; + +/** + * @author Michael Koch + */ +public final class NIOSocket extends Socket +{ + private PlainSocketImpl impl; + private SocketChannelImpl channel; + + protected NIOSocket (PlainSocketImpl impl, SocketChannelImpl channel) + throws IOException + { + super (impl); + this.impl = impl; + this.channel = channel; + } + + public final PlainSocketImpl getPlainSocketImpl() + { + return impl; + } + + final void setChannel (SocketChannelImpl channel) + { + this.impl = channel.getPlainSocketImpl(); + this.channel = channel; + } + + public final SocketChannel getChannel() + { + return channel; + } +} diff --git a/libjava/classpath/gnu/java/nio/OutputStreamChannel.java b/libjava/classpath/gnu/java/nio/OutputStreamChannel.java new file mode 100644 index 00000000000..8167426b07d --- /dev/null +++ b/libjava/classpath/gnu/java/nio/OutputStreamChannel.java @@ -0,0 +1,87 @@ +/* OutputStreamChannel.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.WritableByteChannel; + +/** + * @author Michael Koch + */ +public final class OutputStreamChannel implements WritableByteChannel +{ + private boolean closed = false; + private OutputStream out; + + public OutputStreamChannel (OutputStream out) + { + super(); + + this.out = out; + } + + public void close() throws IOException + { + if (!closed) + { + out.close(); + closed = true; + } + } + + public boolean isOpen() + { + return !closed; + } + + public int write (ByteBuffer src) throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + int len = src.remaining(); + byte[] buffer = new byte [len]; + src.get (buffer); + out.write (buffer); + return len; + } +} diff --git a/libjava/classpath/gnu/java/nio/PipeImpl.java b/libjava/classpath/gnu/java/nio/PipeImpl.java new file mode 100644 index 00000000000..f7b01c8b740 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/PipeImpl.java @@ -0,0 +1,184 @@ +/* PipeImpl.java -- + Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.Pipe; +import java.nio.channels.spi.SelectorProvider; + +class PipeImpl extends Pipe +{ + public static final class SourceChannelImpl extends Pipe.SourceChannel + { + private int native_fd; + + public SourceChannelImpl (SelectorProvider selectorProvider, + int native_fd) + { + super (selectorProvider); + this.native_fd = native_fd; + } + + protected final void implCloseSelectableChannel() + throws IOException + { + throw new Error ("Not implemented"); + } + + protected void implConfigureBlocking (boolean blocking) + throws IOException + { + throw new Error ("Not implemented"); + } + + public final int read (ByteBuffer src) + throws IOException + { + throw new Error ("Not implemented"); + } + + public final long read (ByteBuffer[] srcs) + throws IOException + { + return read (srcs, 0, srcs.length); + } + + public final synchronized long read (ByteBuffer[] srcs, int offset, + int len) + throws IOException + { + if (offset < 0 + || offset > srcs.length + || len < 0 + || len > srcs.length - offset) + throw new IndexOutOfBoundsException(); + + long bytesRead = 0; + + for (int index = 0; index < len; index++) + bytesRead += read (srcs [offset + index]); + + return bytesRead; + + } + + public final int getNativeFD() + { + return native_fd; + } + } + + public static final class SinkChannelImpl extends Pipe.SinkChannel + { + private int native_fd; + + public SinkChannelImpl (SelectorProvider selectorProvider, + int native_fd) + { + super (selectorProvider); + this.native_fd = native_fd; + } + + protected final void implCloseSelectableChannel() + throws IOException + { + throw new Error ("Not implemented"); + } + + protected final void implConfigureBlocking (boolean blocking) + throws IOException + { + throw new Error ("Not implemented"); + } + + public final int write (ByteBuffer dst) + throws IOException + { + throw new Error ("Not implemented"); + } + + public final long write (ByteBuffer[] srcs) + throws IOException + { + return write (srcs, 0, srcs.length); + } + + public final synchronized long write (ByteBuffer[] srcs, int offset, int len) + throws IOException + { + if (offset < 0 + || offset > srcs.length + || len < 0 + || len > srcs.length - offset) + throw new IndexOutOfBoundsException(); + + long bytesWritten = 0; + + for (int index = 0; index < len; index++) + bytesWritten += write (srcs [offset + index]); + + return bytesWritten; + } + + public final int getNativeFD() + { + return native_fd; + } + } + + private SinkChannelImpl sink; + private SourceChannelImpl source; + + public PipeImpl (SelectorProvider provider) + throws IOException + { + super(); + VMPipe.init (this, provider); + } + + public Pipe.SinkChannel sink() + { + return sink; + } + + public Pipe.SourceChannel source() + { + return source; + } +} diff --git a/libjava/classpath/gnu/java/nio/SelectionKeyImpl.java b/libjava/classpath/gnu/java/nio/SelectionKeyImpl.java new file mode 100644 index 00000000000..a1f125e9f8b --- /dev/null +++ b/libjava/classpath/gnu/java/nio/SelectionKeyImpl.java @@ -0,0 +1,104 @@ +/* SelectionKeyImpl.java -- + Copyright (C) 2002, 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio; + +import java.nio.channels.CancelledKeyException; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelectionKey; + +public abstract class SelectionKeyImpl extends AbstractSelectionKey +{ + private int readyOps; + private int interestOps; + private SelectorImpl impl; + SelectableChannel ch; + + public SelectionKeyImpl (SelectableChannel ch, SelectorImpl impl) + { + this.ch = ch; + this.impl = impl; + } + + public SelectableChannel channel () + { + return ch; + } + + public int readyOps () + { + if (!isValid()) + throw new CancelledKeyException(); + + return readyOps; + } + + public SelectionKey readyOps (int ops) + { + if (!isValid()) + throw new CancelledKeyException(); + + readyOps = ops; + return this; + } + + public int interestOps () + { + if (!isValid()) + throw new CancelledKeyException(); + + return interestOps; + } + + public SelectionKey interestOps (int ops) + { + if (!isValid()) + throw new CancelledKeyException(); + + interestOps = ops; + return this; + } + + public Selector selector () + { + return impl; + } + + public abstract int getNativeFD(); +} diff --git a/libjava/classpath/gnu/java/nio/SelectorImpl.java b/libjava/classpath/gnu/java/nio/SelectorImpl.java new file mode 100644 index 00000000000..dcafedeb8bb --- /dev/null +++ b/libjava/classpath/gnu/java/nio/SelectorImpl.java @@ -0,0 +1,394 @@ +/* SelectorImpl.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.nio.channels.ClosedSelectorException; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.AbstractSelector; +import java.nio.channels.spi.SelectorProvider; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public class SelectorImpl extends AbstractSelector +{ + private Set keys; + private Set selected; + + /** + * A dummy object whose monitor regulates access to both our + * selectThread and unhandledWakeup fields. + */ + private Object selectThreadMutex = new Object (); + + /** + * Any thread that's currently blocked in a select operation. + */ + private Thread selectThread; + + /** + * Indicates whether we have an unhandled wakeup call. This can + * be due to either wakeup() triggering a thread interruption while + * a thread was blocked in a select operation (in which case we need + * to reset this thread's interrupt status after interrupting the + * select), or else that no thread was on a select operation at the + * time that wakeup() was called, in which case the following select() + * operation should return immediately with nothing selected. + */ + private boolean unhandledWakeup; + + public SelectorImpl (SelectorProvider provider) + { + super (provider); + + keys = new HashSet (); + selected = new HashSet (); + } + + protected void finalize() throws Throwable + { + close(); + } + + protected final void implCloseSelector() + throws IOException + { + // Cancel any pending select operation. + wakeup(); + + synchronized (keys) + { + synchronized (selected) + { + synchronized (cancelledKeys ()) + { + // FIXME: Release resources here. + } + } + } + } + + public final Set keys() + { + if (!isOpen()) + throw new ClosedSelectorException(); + + return Collections.unmodifiableSet (keys); + } + + public final int selectNow() + throws IOException + { + // FIXME: We're simulating an immediate select + // via a select with a timeout of one millisecond. + return select (1); + } + + public final int select() + throws IOException + { + return select (0); + } + + private final int[] getFDsAsArray (int ops) + { + int[] result; + int counter = 0; + Iterator it = keys.iterator (); + + // Count the number of file descriptors needed + while (it.hasNext ()) + { + SelectionKeyImpl key = (SelectionKeyImpl) it.next (); + + if ((key.interestOps () & ops) != 0) + { + counter++; + } + } + + result = new int[counter]; + + counter = 0; + it = keys.iterator (); + + // Fill the array with the file descriptors + while (it.hasNext ()) + { + SelectionKeyImpl key = (SelectionKeyImpl) it.next (); + + if ((key.interestOps () & ops) != 0) + { + result[counter] = key.getNativeFD(); + counter++; + } + } + + return result; + } + + public synchronized int select (long timeout) + throws IOException + { + if (!isOpen()) + throw new ClosedSelectorException(); + + synchronized (keys) + { + synchronized (selected) + { + deregisterCancelledKeys(); + + // Set only keys with the needed interest ops into the arrays. + int[] read = getFDsAsArray (SelectionKey.OP_READ + | SelectionKey.OP_ACCEPT); + int[] write = getFDsAsArray (SelectionKey.OP_WRITE + | SelectionKey.OP_CONNECT); + + // FIXME: We dont need to check this yet + int[] except = new int [0]; + + // Test to see if we've got an unhandled wakeup call, + // in which case we return immediately. Otherwise, + // remember our current thread and jump into the select. + // The monitor for dummy object selectThreadMutex regulates + // access to these fields. + + // FIXME: Not sure from the spec at what point we should + // return "immediately". Is it here or immediately upon + // entry to this function? + + // NOTE: There's a possibility of another thread calling + // wakeup() immediately after our thread releases + // selectThreadMutex's monitor here, in which case we'll + // do the select anyway. Since calls to wakeup() and select() + // among different threads happen in non-deterministic order, + // I don't think this is an issue. + synchronized (selectThreadMutex) + { + if (unhandledWakeup) + { + unhandledWakeup = false; + return 0; + } + else + { + selectThread = Thread.currentThread (); + } + } + + // Call the native select() on all file descriptors. + int result = 0; + try + { + begin(); + result = VMSelector.select (read, write, except, timeout); + } + finally + { + end(); + } + + // If our unhandled wakeup flag is set at this point, + // reset our thread's interrupt flag because we were + // awakened by wakeup() instead of an external thread + // interruption. + // + // NOTE: If we were blocked in a select() and one thread + // called Thread.interrupt() on the blocked thread followed + // by another thread calling Selector.wakeup(), then race + // conditions could make it so that the thread's interrupt + // flag is reset even though the Thread.interrupt() call + // "was there first". I don't think we need to care about + // this scenario. + synchronized (selectThreadMutex) + { + if (unhandledWakeup) + { + unhandledWakeup = false; + Thread.interrupted (); + } + selectThread = null; + } + + Iterator it = keys.iterator (); + + while (it.hasNext ()) + { + int ops = 0; + SelectionKeyImpl key = (SelectionKeyImpl) it.next (); + + // If key is already selected retrieve old ready ops. + if (selected.contains (key)) + { + ops = key.readyOps (); + } + + // Set new ready read/accept ops + for (int i = 0; i < read.length; i++) + { + if (key.getNativeFD() == read[i]) + { + if (key.channel () instanceof ServerSocketChannelImpl) + { + ops = ops | SelectionKey.OP_ACCEPT; + } + else + { + ops = ops | SelectionKey.OP_READ; + } + } + } + + // Set new ready write ops + for (int i = 0; i < write.length; i++) + { + if (key.getNativeFD() == write[i]) + { + ops = ops | SelectionKey.OP_WRITE; + + // if (key.channel ().isConnected ()) + // { + // ops = ops | SelectionKey.OP_WRITE; + // } + // else + // { + // ops = ops | SelectionKey.OP_CONNECT; + // } + } + } + + // FIXME: We dont handle exceptional file descriptors yet. + + // If key is not yet selected add it. + if (!selected.contains (key)) + { + selected.add (key); + } + + // Set new ready ops + key.readyOps (key.interestOps () & ops); + } + deregisterCancelledKeys(); + + return result; + } + } + } + + public final Set selectedKeys() + { + if (!isOpen()) + throw new ClosedSelectorException(); + + return selected; + } + + public final Selector wakeup() + { + // IMPLEMENTATION NOTE: Whereas the specification says that + // thread interruption should trigger a call to wakeup, we + // do the reverse under the covers: wakeup triggers a thread + // interrupt followed by a subsequent reset of the thread's + // interrupt status within select(). + + // First, acquire the monitor of the object regulating + // access to our selectThread and unhandledWakeup fields. + synchronized (selectThreadMutex) + { + unhandledWakeup = true; + + // Interrupt any thread which is currently blocked in + // a select operation. + if (selectThread != null) + selectThread.interrupt (); + } + + return this; + } + + private final void deregisterCancelledKeys() + { + Set ckeys = cancelledKeys (); + synchronized (ckeys) + { + Iterator it = ckeys.iterator(); + + while (it.hasNext ()) + { + keys.remove ((SelectionKeyImpl) it.next ()); + it.remove (); + } + } + } + + protected SelectionKey register (SelectableChannel ch, int ops, Object att) + { + return register ((AbstractSelectableChannel) ch, ops, att); + } + + protected final SelectionKey register (AbstractSelectableChannel ch, int ops, + Object att) + { + SelectionKeyImpl result; + + if (ch instanceof SocketChannelImpl) + result = new SocketChannelSelectionKey (ch, this); + else if (ch instanceof DatagramChannelImpl) + result = new DatagramChannelSelectionKey (ch, this); + else if (ch instanceof ServerSocketChannelImpl) + result = new ServerSocketChannelSelectionKey (ch, this); + else + throw new InternalError ("No known channel type"); + + synchronized (keys) + { + keys.add (result); + } + + result.interestOps (ops); + result.attach (att); + return result; + } +} diff --git a/libjava/classpath/gnu/java/nio/SelectorProviderImpl.java b/libjava/classpath/gnu/java/nio/SelectorProviderImpl.java new file mode 100644 index 00000000000..47521107e90 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/SelectorProviderImpl.java @@ -0,0 +1,83 @@ +/* SelectorProviderImpl.java -- + Copyright (C) 2002, 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio; + +import java.io.IOException; +import java.nio.channels.DatagramChannel; +import java.nio.channels.Pipe; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.AbstractSelector; +import java.nio.channels.spi.SelectorProvider; + +public class SelectorProviderImpl extends SelectorProvider +{ + public SelectorProviderImpl () + { + } + + public DatagramChannel openDatagramChannel () + throws IOException + { + return new DatagramChannelImpl (this); + } + + public Pipe openPipe () + throws IOException + { + return new PipeImpl (this); + } + + public AbstractSelector openSelector () + throws IOException + { + return new SelectorImpl (this); + } + + public ServerSocketChannel openServerSocketChannel () + throws IOException + { + return new ServerSocketChannelImpl (this); + } + + public SocketChannel openSocketChannel () + throws IOException + { + return new SocketChannelImpl (this); + } +} diff --git a/libjava/classpath/gnu/java/nio/ServerSocketChannelImpl.java b/libjava/classpath/gnu/java/nio/ServerSocketChannelImpl.java new file mode 100644 index 00000000000..c538ea802e1 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/ServerSocketChannelImpl.java @@ -0,0 +1,124 @@ +/* ServerSocketChannelImpl.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.SocketTimeoutException; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.NotYetBoundException; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; + +public final class ServerSocketChannelImpl extends ServerSocketChannel +{ + private NIOServerSocket serverSocket; + private boolean connected; + + protected ServerSocketChannelImpl (SelectorProvider provider) + throws IOException + { + super (provider); + serverSocket = new NIOServerSocket (this); + configureBlocking(true); + } + + public void finalizer() + { + if (connected) + { + try + { + close (); + } + catch (Exception e) + { + } + } + } + + protected void implCloseSelectableChannel () throws IOException + { + connected = false; + serverSocket.close(); + } + + protected void implConfigureBlocking (boolean blocking) throws IOException + { + serverSocket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT); + } + + public SocketChannel accept () throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + if (!serverSocket.isBound()) + throw new NotYetBoundException(); + + boolean completed = false; + + try + { + begin(); + serverSocket.getPlainSocketImpl().setInChannelOperation(true); + // indicate that a channel is initiating the accept operation + // so that the socket ignores the fact that we might be in + // non-blocking mode. + NIOSocket socket = (NIOSocket) serverSocket.accept(); + completed = true; + return socket.getChannel(); + } + catch (SocketTimeoutException e) + { + return null; + } + finally + { + serverSocket.getPlainSocketImpl().setInChannelOperation(false); + end (completed); + } + } + + public ServerSocket socket () + { + return serverSocket; + } +} diff --git a/libjava/classpath/gnu/java/nio/ServerSocketChannelSelectionKey.java b/libjava/classpath/gnu/java/nio/ServerSocketChannelSelectionKey.java new file mode 100644 index 00000000000..d00c2b7482a --- /dev/null +++ b/libjava/classpath/gnu/java/nio/ServerSocketChannelSelectionKey.java @@ -0,0 +1,58 @@ +/* ServerSocketChannelSelectionKey.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.nio.channels.spi.AbstractSelectableChannel; + +public final class ServerSocketChannelSelectionKey + extends SelectionKeyImpl +{ + public ServerSocketChannelSelectionKey (AbstractSelectableChannel channel, + SelectorImpl selector) + { + super (channel, selector); + } + + public int getNativeFD() + { + NIOServerSocket socket = + (NIOServerSocket) ((ServerSocketChannelImpl) ch).socket(); + return socket.getPlainSocketImpl().getNativeFD(); + } +} diff --git a/libjava/classpath/gnu/java/nio/SocketChannelImpl.java b/libjava/classpath/gnu/java/nio/SocketChannelImpl.java new file mode 100644 index 00000000000..9fb71c1ae1c --- /dev/null +++ b/libjava/classpath/gnu/java/nio/SocketChannelImpl.java @@ -0,0 +1,351 @@ +/* SocketChannelImpl.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.net.PlainSocketImpl; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketTimeoutException; +import java.nio.ByteBuffer; +import java.nio.channels.AlreadyConnectedException; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.ConnectionPendingException; +import java.nio.channels.NoConnectionPendingException; +import java.nio.channels.NotYetConnectedException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; +import java.nio.channels.UnresolvedAddressException; +import java.nio.channels.UnsupportedAddressTypeException; +import java.nio.channels.spi.SelectorProvider; + +public final class SocketChannelImpl extends SocketChannel +{ + private PlainSocketImpl impl; + private NIOSocket socket; + private boolean connectionPending; + + SocketChannelImpl (SelectorProvider provider) + throws IOException + { + super (provider); + impl = new PlainSocketImpl(); + socket = new NIOSocket (impl, this); + configureBlocking(true); + } + + SocketChannelImpl (SelectorProvider provider, + NIOSocket socket) + throws IOException + { + super (provider); + this.impl = socket.getPlainSocketImpl(); + this.socket = socket; + } + + public void finalizer() + { + if (isConnected()) + { + try + { + close (); + } + catch (Exception e) + { + } + } + } + + PlainSocketImpl getPlainSocketImpl() + { + return impl; + } + + protected void implCloseSelectableChannel () throws IOException + { + socket.close(); + } + + protected void implConfigureBlocking (boolean blocking) throws IOException + { + socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT); + } + + public boolean connect (SocketAddress remote) throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + if (isConnected()) + throw new AlreadyConnectedException(); + + if (connectionPending) + throw new ConnectionPendingException(); + + if (!(remote instanceof InetSocketAddress)) + throw new UnsupportedAddressTypeException(); + + if (((InetSocketAddress) remote).isUnresolved()) + throw new UnresolvedAddressException(); + + try + { + socket.getPlainSocketImpl().setInChannelOperation(true); + // indicate that a channel is initiating the accept operation + // so that the socket ignores the fact that we might be in + // non-blocking mode. + + if (isBlocking()) + { + // Do blocking connect. + socket.connect (remote); + return true; + } + + // Do non-blocking connect. + try + { + socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT); + return true; + } + catch (SocketTimeoutException e) + { + connectionPending = true; + return false; + } + } + finally + { + socket.getPlainSocketImpl().setInChannelOperation(false); + } + } + + public boolean finishConnect () + throws IOException + { + if (!isOpen()) + throw new ClosedChannelException(); + + if (!isConnected() && !connectionPending) + throw new NoConnectionPendingException(); + + if (isConnected()) + return true; + + // FIXME: Handle blocking/non-blocking mode. + + Selector selector = provider().openSelector(); + register(selector, SelectionKey.OP_CONNECT); + + if (isBlocking()) + { + selector.select(); // blocking until channel is connected. + connectionPending = false; + return true; + } + + int ready = selector.selectNow(); // non-blocking + if (ready == 1) + { + connectionPending = false; + return true; + } + + return false; + } + + public boolean isConnected () + { + return socket.isConnected(); + } + + public boolean isConnectionPending () + { + return connectionPending; + } + + public Socket socket () + { + return socket; + } + + public int read(ByteBuffer dst) throws IOException + { + if (!isConnected()) + throw new NotYetConnectedException(); + + byte[] data; + int offset = 0; + InputStream input = socket.getInputStream(); + int available = input.available(); + int len = dst.capacity() - dst.position(); + + if ((! isBlocking()) && available == 0) + return 0; + + if (dst.hasArray()) + { + offset = dst.arrayOffset() + dst.position(); + data = dst.array(); + } + else + { + data = new byte [len]; + } + + int readBytes = 0; + boolean completed = false; + + try + { + begin(); + socket.getPlainSocketImpl().setInChannelOperation(true); + readBytes = input.read (data, offset, len); + completed = true; + } + finally + { + end (completed); + socket.getPlainSocketImpl().setInChannelOperation(false); + } + + if (readBytes > 0) + if (dst.hasArray()) + { + dst.position (dst.position() + readBytes); + } + else + { + dst.put (data, offset, len); + } + + return readBytes; + } + + public long read (ByteBuffer[] dsts, int offset, int length) + throws IOException + { + if (!isConnected()) + throw new NotYetConnectedException(); + + if ((offset < 0) + || (offset > dsts.length) + || (length < 0) + || (length > (dsts.length - offset))) + throw new IndexOutOfBoundsException(); + + long readBytes = 0; + + for (int index = offset; index < length; index++) + readBytes += read (dsts [index]); + + return readBytes; + } + + public int write (ByteBuffer src) + throws IOException + { + if (!isConnected()) + throw new NotYetConnectedException(); + + byte[] data; + int offset = 0; + int len = src.remaining(); + + if (!src.hasArray()) + { + data = new byte [len]; + src.get (data, 0, len); + } + else + { + offset = src.arrayOffset() + src.position(); + data = src.array(); + } + + OutputStream output = socket.getOutputStream(); + boolean completed = false; + + try + { + begin(); + socket.getPlainSocketImpl().setInChannelOperation(true); + output.write (data, offset, len); + completed = true; + } + finally + { + end (completed); + socket.getPlainSocketImpl().setInChannelOperation(false); + } + + if (src.hasArray()) + { + src.position (src.position() + len); + } + + return len; + } + + public long write (ByteBuffer[] srcs, int offset, int length) + throws IOException + { + if (!isConnected()) + throw new NotYetConnectedException(); + + if ((offset < 0) + || (offset > srcs.length) + || (length < 0) + || (length > (srcs.length - offset))) + throw new IndexOutOfBoundsException(); + + long writtenBytes = 0; + + for (int index = offset; index < length; index++) + writtenBytes += write (srcs [index]); + + return writtenBytes; + } +} diff --git a/libjava/classpath/gnu/java/nio/SocketChannelSelectionKey.java b/libjava/classpath/gnu/java/nio/SocketChannelSelectionKey.java new file mode 100644 index 00000000000..75b4dfd87e5 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/SocketChannelSelectionKey.java @@ -0,0 +1,58 @@ +/* SocketChannelSelectionKey.java -- + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.nio.channels.spi.AbstractSelectableChannel; + +public final class SocketChannelSelectionKey + extends SelectionKeyImpl +{ + public SocketChannelSelectionKey (AbstractSelectableChannel channel, + SelectorImpl selector) + { + super (channel, selector); + } + + public int getNativeFD() + { + NIOSocket socket = + (NIOSocket) ((SocketChannelImpl) ch).socket(); + return socket.getPlainSocketImpl().getNativeFD(); + } +} diff --git a/libjava/classpath/gnu/java/nio/channels/FileChannelImpl.java b/libjava/classpath/gnu/java/nio/channels/FileChannelImpl.java new file mode 100644 index 00000000000..a87a2e8c597 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/channels/FileChannelImpl.java @@ -0,0 +1,535 @@ +/* FileChannelImpl.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.channels; + +import gnu.classpath.Configuration; +import gnu.java.nio.FileLockImpl; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; +import java.nio.channels.NonReadableChannelException; +import java.nio.channels.NonWritableChannelException; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; + +/** + * This file is not user visible ! + * But alas, Java does not have a concept of friendly packages + * so this class is public. + * Instances of this class are created by invoking getChannel + * Upon a Input/Output/RandomAccessFile object. + */ +public final class FileChannelImpl extends FileChannel +{ + // These are mode values for open(). + public static final int READ = 1; + public static final int WRITE = 2; + public static final int APPEND = 4; + + // EXCL is used only when making a temp file. + public static final int EXCL = 8; + public static final int SYNC = 16; + public static final int DSYNC = 32; + + private static native void init(); + + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("javanio"); + } + + init(); + } + + /** + * This is the actual native file descriptor value + */ + // System's notion of file descriptor. It might seem redundant to + // initialize this given that it is reassigned in the constructors. + // However, this is necessary because if open() throws an exception + // we want to make sure this has the value -1. This is the most + // efficient way to accomplish that. + private int fd = -1; + + private int mode; + + /* Open a file. MODE is a combination of the above mode flags. */ + /* This is a static factory method, so that VM implementors can decide + * substitute subclasses of FileChannelImpl. */ + public static FileChannelImpl create(File file, int mode) + throws FileNotFoundException + { + return new FileChannelImpl(file, mode); + } + + private FileChannelImpl(File file, int mode) + throws FileNotFoundException + { + final String path = file.getPath(); + fd = open (path, mode); + this.mode = mode; + + // First open the file and then check if it is a a directory + // to avoid race condition. + if (file.isDirectory()) + { + try + { + close(); + } + catch (IOException e) + { + /* ignore it */ + } + + throw new FileNotFoundException(path + " is a directory"); + } + } + + /* Used by init() (native code) */ + FileChannelImpl (int fd, int mode) + { + this.fd = fd; + this.mode = mode; + } + + public static FileChannelImpl in; + public static FileChannelImpl out; + public static FileChannelImpl err; + + private native int open (String path, int mode) throws FileNotFoundException; + + public native int available () throws IOException; + private native long implPosition () throws IOException; + private native void seek (long newPosition) throws IOException; + private native void implTruncate (long size) throws IOException; + + public native void unlock (long pos, long len) throws IOException; + + public native long size () throws IOException; + + protected native void implCloseChannel() throws IOException; + + /** + * Makes sure the Channel is properly closed. + */ + protected void finalize() throws IOException + { + this.close(); + } + + public int read (ByteBuffer dst) throws IOException + { + int result; + byte[] buffer = new byte [dst.remaining ()]; + + result = read (buffer, 0, buffer.length); + + if (result > 0) + dst.put (buffer, 0, result); + + return result; + } + + public int read (ByteBuffer dst, long position) + throws IOException + { + if (position < 0) + throw new IllegalArgumentException (); + long oldPosition = implPosition (); + position (position); + int result = read(dst); + position (oldPosition); + + return result; + } + + public native int read () + throws IOException; + + public native int read (byte[] buffer, int offset, int length) + throws IOException; + + public long read (ByteBuffer[] dsts, int offset, int length) + throws IOException + { + long result = 0; + + for (int i = offset; i < offset + length; i++) + { + result += read (dsts [i]); + } + + return result; + } + + public int write (ByteBuffer src) throws IOException + { + int len = src.remaining (); + if (src.hasArray()) + { + byte[] buffer = src.array(); + write(buffer, src.arrayOffset() + src.position(), len); + src.position(src.position() + len); + } + else + { + // Use a more efficient native method! FIXME! + byte[] buffer = new byte [len]; + src.get (buffer, 0, len); + write (buffer, 0, len); + } + return len; + } + + public int write (ByteBuffer src, long position) + throws IOException + { + if (position < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & WRITE) == 0) + throw new NonWritableChannelException (); + + int result; + long oldPosition; + + oldPosition = implPosition (); + seek (position); + result = write(src); + seek (oldPosition); + + return result; + } + + public native void write (byte[] buffer, int offset, int length) + throws IOException; + + public native void write (int b) throws IOException; + + public long write(ByteBuffer[] srcs, int offset, int length) + throws IOException + { + long result = 0; + + for (int i = offset;i < offset + length;i++) + { + result += write (srcs[i]); + } + + return result; + } + + public native MappedByteBuffer mapImpl (char mode, long position, int size) + throws IOException; + + public MappedByteBuffer map (FileChannel.MapMode mode, + long position, long size) + throws IOException + { + char nmode = 0; + if (mode == MapMode.READ_ONLY) + { + nmode = 'r'; + if ((this.mode & READ) == 0) + throw new NonReadableChannelException(); + } + else if (mode == MapMode.READ_WRITE || mode == MapMode.PRIVATE) + { + nmode = mode == MapMode.READ_WRITE ? '+' : 'c'; + if ((this.mode & (READ|WRITE)) != (READ|WRITE)) + throw new NonWritableChannelException(); + } + else + throw new IllegalArgumentException (); + + if (position < 0 || size < 0 || size > Integer.MAX_VALUE) + throw new IllegalArgumentException (); + return mapImpl(nmode, position, (int) size); + } + + /** + * msync with the disk + */ + public void force (boolean metaData) throws IOException + { + if (!isOpen ()) + throw new ClosedChannelException (); + + force (); + } + + private native void force (); + + // like transferTo, but with a count of less than 2Gbytes + private int smallTransferTo (long position, int count, + WritableByteChannel target) + throws IOException + { + ByteBuffer buffer; + try + { + // Try to use a mapped buffer if we can. If this fails for + // any reason we'll fall back to using a ByteBuffer. + buffer = map (MapMode.READ_ONLY, position, count); + } + catch (IOException e) + { + buffer = ByteBuffer.allocate (count); + read (buffer, position); + buffer.flip(); + } + + return target.write (buffer); + } + + public long transferTo (long position, long count, + WritableByteChannel target) + throws IOException + { + if (position < 0 + || count < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & READ) == 0) + throw new NonReadableChannelException (); + + final int pageSize = 65536; + long total = 0; + + while (count > 0) + { + int transferred + = smallTransferTo (position, (int)Math.min (count, pageSize), + target); + if (transferred < 0) + break; + total += transferred; + position += transferred; + count -= transferred; + } + + return total; + } + + // like transferFrom, but with a count of less than 2Gbytes + private int smallTransferFrom (ReadableByteChannel src, long position, + int count) + throws IOException + { + ByteBuffer buffer = null; + + if (src instanceof FileChannel) + { + try + { + // Try to use a mapped buffer if we can. If this fails + // for any reason we'll fall back to using a ByteBuffer. + buffer = ((FileChannel)src).map (MapMode.READ_ONLY, position, + count); + } + catch (IOException e) + { + } + } + + if (buffer == null) + { + buffer = ByteBuffer.allocate ((int) count); + src.read (buffer); + buffer.flip(); + } + + return write (buffer, position); + } + + public long transferFrom (ReadableByteChannel src, long position, + long count) + throws IOException + { + if (position < 0 + || count < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & WRITE) == 0) + throw new NonWritableChannelException (); + + final int pageSize = 65536; + long total = 0; + + while (count > 0) + { + int transferred = smallTransferFrom (src, position, + (int)Math.min (count, pageSize)); + if (transferred < 0) + break; + total += transferred; + position += transferred; + count -= transferred; + } + + return total; + } + + public FileLock tryLock (long position, long size, boolean shared) + throws IOException + { + if (position < 0 + || size < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if (shared && (mode & READ) == 0) + throw new NonReadableChannelException (); + + if (!shared && (mode & WRITE) == 0) + throw new NonWritableChannelException (); + + boolean completed = false; + + try + { + begin(); + boolean lockable = lock(position, size, shared, false); + completed = true; + return (lockable + ? new FileLockImpl(this, position, size, shared) + : null); + } + finally + { + end(completed); + } + } + + /** Try to acquire a lock at the given position and size. + * On success return true. + * If wait as specified, block until we can get it. + * Otherwise return false. + */ + private native boolean lock(long position, long size, + boolean shared, boolean wait) throws IOException; + + public FileLock lock (long position, long size, boolean shared) + throws IOException + { + if (position < 0 + || size < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + boolean completed = false; + + try + { + boolean lockable = lock(position, size, shared, true); + completed = true; + return (lockable + ? new FileLockImpl(this, position, size, shared) + : null); + } + finally + { + end(completed); + } + } + + public long position () + throws IOException + { + if (!isOpen ()) + throw new ClosedChannelException (); + + return implPosition (); + } + + public FileChannel position (long newPosition) + throws IOException + { + if (newPosition < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + // FIXME note semantics if seeking beyond eof. + // We should seek lazily - only on a write. + seek (newPosition); + return this; + } + + public FileChannel truncate (long size) + throws IOException + { + if (size < 0) + throw new IllegalArgumentException (); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & WRITE) == 0) + throw new NonWritableChannelException (); + + if (size < size ()) + implTruncate (size); + + return this; + } +} diff --git a/libjava/classpath/gnu/java/nio/channels/package.html b/libjava/classpath/gnu/java/nio/channels/package.html new file mode 100644 index 00000000000..0c3821bea7b --- /dev/null +++ b/libjava/classpath/gnu/java/nio/channels/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.nio.channels package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.nio.channels</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/nio/charset/ByteCharset.java b/libjava/classpath/gnu/java/nio/charset/ByteCharset.java new file mode 100644 index 00000000000..2cc91b850f6 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ByteCharset.java @@ -0,0 +1,187 @@ +/* ByteCharset.java -- Abstract class for generic 1-byte encodings. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +/** + * A generic encoding framework for single-byte encodings, + * utilizing a look-up table. + * + * This replaces the gnu.java.io.EncoderEightBitLookup class, + * created by Aron Renn. + * + * @author Sven de Marothy + */ +abstract class ByteCharset extends Charset +{ + protected char[] lookupTable; + /** + * Char to signify the character in the table is undefined + */ + protected static final char NONE = (char)0xFFFD; + + ByteCharset (String canonicalName, String[] aliases) + { + super (canonicalName, aliases); + } + + /** + * Most western charsets include ASCII, but this should + * be overloaded for others. + */ + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || (cs.getClass() == getClass()); + } + + char[] getLookupTable() + { + return lookupTable; + } + + public CharsetDecoder newDecoder () + { + return new Decoder (this); + } + + public CharsetEncoder newEncoder () + { + return new Encoder (this); + } + + private static final class Decoder extends CharsetDecoder + { + private char[] lookup; + + // Package-private to avoid a trampoline constructor. + Decoder (ByteCharset cs) + { + super (cs, 1.0f, 1.0f); + lookup = cs.getLookupTable(); + } + + protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + byte b = in.get (); + char c; + + if (!out.hasRemaining ()) + { + in.position (in.position () - 1); + return CoderResult.OVERFLOW; + } + + if((c = lookup[(int) (b & 0xFF)]) == NONE); + // return CoderResult.unmappableForLength (1); + out.put (c); + } + + return CoderResult.UNDERFLOW; + } + } + + private static final class Encoder extends CharsetEncoder + { + private byte[] lookup; + + // Package-private to avoid a trampoline constructor. + Encoder (ByteCharset cs) + { + super (cs, 1.0f, 1.0f); + + char[] lookup_table = cs.getLookupTable(); + + // Create the inverse look-up table. + // determine required size of encoding_table: + int max = 0; + for (int i = 0; i < lookup_table.length; i++) + { + int c = (int)lookup_table[i]; + max = (c > max && c < NONE) ? c : max; + } + + lookup = new byte[max+1]; + + for (int i = 0; i < lookup_table.length; i++) + { + int c = (int)lookup_table[i]; + if (c != 0 && c < NONE) + { + lookup[c] = (byte)i; + } + } + } + + protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + int c = (int)in.get (); + + if (!out.hasRemaining ()) + { + in.position (in.position () - 1); + return CoderResult.OVERFLOW; + } + + // lookup byte encoding + byte b = (c < lookup.length) ? lookup[c] : (byte)0; + + if ((int)b != 0 || (int)c == 0) + { + out.put (b); + } else { + in.position (in.position () - 1); + return CoderResult.unmappableForLength (1); + } + } + + return CoderResult.UNDERFLOW; + } + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/Cp424.java b/libjava/classpath/gnu/java/nio/charset/Cp424.java new file mode 100644 index 00000000000..9733a76a4c5 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp424.java @@ -0,0 +1,89 @@ +/* Cp424.java -- Charset implementation for the Cp424 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp424 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x009C, 0x0009, 0x0086, 0x007F, + 0x0097, 0x008D, 0x008E, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x009D, 0x0085, 0x0008, 0x0087, + 0x0018, 0x0019, 0x0092, 0x008F, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x000A, 0x0017, 0x001B, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x0005, 0x0006, 0x0007, + 0x0090, 0x0091, 0x0016, 0x0093, 0x0094, 0x0095, 0x0096, 0x0004, + 0x0098, 0x0099, 0x009A, 0x009B, 0x0014, 0x0015, 0x009E, 0x001A, + 0x0020, 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, + 0x05D7, 0x05D8, 0x00A2, 0x002E, 0x003C, 0x0028, 0x002B, 0x007C, + 0x0026, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF, + 0x05E0, 0x05E1, 0x0021, 0x0024, 0x002A, 0x0029, 0x003B, 0x00AC, + 0x002D, 0x002F, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, + 0x05E8, 0x05E9, 0x00A6, 0x002C, 0x0025, 0x005F, 0x003E, 0x003F, + NONE, 0x05EA, NONE, NONE, 0x00A0, NONE, NONE, NONE, + 0x2017, 0x0060, 0x003A, 0x0023, 0x0040, 0x0027, 0x003D, 0x0022, + NONE, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x00AB, 0x00BB, NONE, NONE, NONE, 0x00B1, + 0x00B0, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, + 0x0071, 0x0072, NONE, NONE, NONE, 0x00B8, NONE, 0x00A4, + 0x00B5, 0x007E, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, + 0x0079, 0x007A, NONE, NONE, NONE, NONE, NONE, 0x00AE, + 0x005E, 0x00A3, 0x00A5, 0x00B7, 0x00A9, 0x00A7, 0x00B6, 0x00BC, + 0x00BD, 0x00BE, 0x005B, 0x005D, 0x00AF, 0x00A8, 0x00B4, 0x00D7, + 0x007B, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x00AD, NONE, NONE, NONE, NONE, NONE, + 0x007D, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, + 0x0051, 0x0052, 0x00B9, NONE, NONE, NONE, NONE, NONE, + 0x005C, 0x00F7, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, + 0x0059, 0x005A, 0x00B2, NONE, NONE, NONE, NONE, NONE, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x00B3, NONE, NONE, NONE, NONE, 0x009F + }; + + public Cp424() + { + super("Cp424", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp424 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp437.java b/libjava/classpath/gnu/java/nio/charset/Cp437.java new file mode 100644 index 00000000000..d6083579d11 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp437.java @@ -0,0 +1,89 @@ +/* Cp437.java -- Charset implementation for the Cp437 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp437 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, + 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, + 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, + 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, + 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, + 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp437() + { + super("Cp437", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp437 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp737.java b/libjava/classpath/gnu/java/nio/charset/Cp737.java new file mode 100644 index 00000000000..548da21912b --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp737.java @@ -0,0 +1,89 @@ +/* Cp737.java -- Charset implementation for the Cp737 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp737 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, + 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0, + 0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, + 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, + 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, + 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD, + 0x03CB, 0x03CE, 0x0386, 0x0388, 0x0389, 0x038A, 0x038C, 0x038E, + 0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp737() + { + super("Cp737", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp737 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp775.java b/libjava/classpath/gnu/java/nio/charset/Cp775.java new file mode 100644 index 00000000000..4d3f1c36c8d --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp775.java @@ -0,0 +1,89 @@ +/* Cp775.java -- Charset implementation for the Cp775 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp775 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0106, 0x00FC, 0x00E9, 0x0101, 0x00E4, 0x0123, 0x00E5, 0x0107, + 0x0142, 0x0113, 0x0156, 0x0157, 0x012B, 0x0179, 0x00C4, 0x00C5, + 0x00C9, 0x00E6, 0x00C6, 0x014D, 0x00F6, 0x0122, 0x00A2, 0x015A, + 0x015B, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x00A4, + 0x0100, 0x012A, 0x00F3, 0x017B, 0x017C, 0x017A, 0x201D, 0x00A6, + 0x00A9, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x0141, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0104, 0x010C, 0x0118, + 0x0116, 0x2563, 0x2551, 0x2557, 0x255D, 0x012E, 0x0160, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0172, 0x016A, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x017D, + 0x0105, 0x010D, 0x0119, 0x0117, 0x012F, 0x0161, 0x0173, 0x016B, + 0x017E, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x00D3, 0x00DF, 0x014C, 0x0143, 0x00F5, 0x00D5, 0x00B5, 0x0144, + 0x0136, 0x0137, 0x013B, 0x013C, 0x0146, 0x0112, 0x0145, 0x2019, + 0x00AD, 0x00B1, 0x201C, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x201E, + 0x00B0, 0x2219, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp775() + { + super("Cp775", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp775 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp850.java b/libjava/classpath/gnu/java/nio/charset/Cp850.java new file mode 100644 index 00000000000..9122105ad08 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp850.java @@ -0,0 +1,89 @@ +/* Cp850.java -- Charset implementation for the Cp850 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp850 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, + 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, + 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, + 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, + 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, + 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, + 0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, + 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, + 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, + 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4, + 0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, + 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp850() + { + super("Cp850", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp850 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp852.java b/libjava/classpath/gnu/java/nio/charset/Cp852.java new file mode 100644 index 00000000000..a859530a8ef --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp852.java @@ -0,0 +1,89 @@ +/* Cp852.java -- Charset implementation for the Cp852 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp852 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7, + 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106, + 0x00C9, 0x0139, 0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A, + 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x0104, 0x0105, 0x017D, 0x017E, + 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x011A, + 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, + 0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE, + 0x011B, 0x2518, 0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580, + 0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161, + 0x0154, 0x00DA, 0x0155, 0x0170, 0x00FD, 0x00DD, 0x0163, 0x00B4, + 0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8, + 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159, 0x25A0, 0x00A0 + }; + + public Cp852() + { + super("Cp852", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp852 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp855.java b/libjava/classpath/gnu/java/nio/charset/Cp855.java new file mode 100644 index 00000000000..fbec999c794 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp855.java @@ -0,0 +1,90 @@ +/* Cp855.java -- Charset implementation for the Cp855 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp855 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0452, 0x0402, 0x0453, 0x0403, 0x0451, 0x0401, 0x0454, 0x0404, + 0x0455, 0x0405, 0x0456, 0x0406, 0x0457, 0x0407, 0x0458, 0x0408, + 0x0459, 0x0409, 0x045A, 0x040A, 0x045B, 0x040B, 0x045C, 0x040C, + 0x045E, 0x040E, 0x045F, 0x040F, 0x044E, 0x042E, 0x044A, 0x042A, + 0x0430, 0x0410, 0x0431, 0x0411, 0x0446, 0x0426, 0x0434, 0x0414, + 0x0435, 0x0415, 0x0444, 0x0424, 0x0433, 0x0413, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0445, 0x0425, 0x0438, + 0x0418, 0x2563, 0x2551, 0x2557, 0x255D, 0x0439, 0x0419, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x043A, 0x041A, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, + 0x043B, 0x041B, 0x043C, 0x041C, 0x043D, 0x041D, 0x043E, 0x041E, + 0x043F, 0x2518, 0x250C, 0x2588, 0x2584, 0x041F, 0x044F, 0x2580, + 0x042F, 0x0440, 0x0420, 0x0441, 0x0421, 0x0442, 0x0422, 0x0443, + 0x0423, 0x0436, 0x0416, 0x0432, 0x0412, 0x044C, 0x042C, 0x2116, + 0x00AD, 0x044B, 0x042B, 0x0437, 0x0417, 0x0448, 0x0428, 0x044D, + 0x042D, 0x0449, 0x0429, 0x0447, 0x0427, 0x00A7, 0x25A0, 0x00A0 + }; + + public Cp855() + { + super("Cp855", new String[] { + "cp-855", + }); + lookupTable = lookup; + } + +} // class Cp855 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp857.java b/libjava/classpath/gnu/java/nio/charset/Cp857.java new file mode 100644 index 00000000000..78f8dbd2d15 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp857.java @@ -0,0 +1,91 @@ +/* Cp857.java -- Charset implementation for the Cp857 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp857 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, + 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5, + 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, + 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x011E, 0x011F, + 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, + 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, + 0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, NONE, 0x00CD, 0x00CE, + 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, + 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, NONE, + 0x00D7, 0x00DA, 0x00DB, 0x00D9, 0x00EC, 0x00FF, 0x00AF, 0x00B4, + 0x00AD, 0x00B1, NONE, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, + 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp857() + { + super("Cp857", new String[] { + "cp-857" + }); + lookupTable = lookup; + } + +} // class Cp857 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp860.java b/libjava/classpath/gnu/java/nio/charset/Cp860.java new file mode 100644 index 00000000000..e3c7f46b8f5 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp860.java @@ -0,0 +1,91 @@ +/* Cp860.java -- Charset implementation for the Cp860 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp860 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E3, 0x00E0, 0x00C1, 0x00E7, + 0x00EA, 0x00CA, 0x00E8, 0x00CD, 0x00D4, 0x00EC, 0x00C3, 0x00C2, + 0x00C9, 0x00C0, 0x00C8, 0x00F4, 0x00F5, 0x00F2, 0x00DA, 0x00F9, + 0x00CC, 0x00D5, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x20A7, 0x00D3, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, + 0x00BF, 0x00D2, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, + 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp860() + { + super("Cp860", new String[] { + "cp-860" + }); + lookupTable = lookup; + } + +} // class Cp860 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp861.java b/libjava/classpath/gnu/java/nio/charset/Cp861.java new file mode 100644 index 00000000000..6e17ea2e842 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp861.java @@ -0,0 +1,91 @@ +/* Cp861.java -- Charset implementation for the Cp861 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp861 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, + 0x00EA, 0x00EB, 0x00E8, 0x00D0, 0x00F0, 0x00DE, 0x00C4, 0x00C5, + 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00FE, 0x00FB, 0x00DD, + 0x00FD, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00C1, 0x00CD, 0x00D3, 0x00DA, + 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, + 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp861() + { + super("Cp861", new String[] { + "cp-861" + }); + lookupTable = lookup; + } + +} // class Cp861 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp862.java b/libjava/classpath/gnu/java/nio/charset/Cp862.java new file mode 100644 index 00000000000..ccd74fa9ddb --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp862.java @@ -0,0 +1,91 @@ +/* Cp862.java -- Charset implementation for the Cp862 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp862 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, + 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF, + 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, + 0x05E8, 0x05E9, 0x05EA, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, + 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, + 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp862() + { + super("Cp862", new String[] { + "Cp-862" + }); + lookupTable = lookup; + } + +} // class Cp862 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp863.java b/libjava/classpath/gnu/java/nio/charset/Cp863.java new file mode 100644 index 00000000000..97812a6f66c --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp863.java @@ -0,0 +1,91 @@ +/* Cp863.java -- Charset implementation for the Cp863 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp863 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00C2, 0x00E0, 0x00B6, 0x00E7, + 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x2017, 0x00C0, 0x00A7, + 0x00C9, 0x00C8, 0x00CA, 0x00F4, 0x00CB, 0x00CF, 0x00FB, 0x00F9, + 0x00A4, 0x00D4, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x00DB, 0x0192, + 0x00A6, 0x00B4, 0x00F3, 0x00FA, 0x00A8, 0x00B8, 0x00B3, 0x00AF, + 0x00CE, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00BE, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, + 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp863() + { + super("Cp863", new String[] { + "Cp-863" + }); + lookupTable = lookup; + } + +} // class Cp863 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp864.java b/libjava/classpath/gnu/java/nio/charset/Cp864.java new file mode 100644 index 00000000000..f136f43eccc --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp864.java @@ -0,0 +1,91 @@ +/* Cp864.java -- Charset implementation for the Cp864 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp864 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x066A, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00B0, 0x00B7, 0x2219, 0x221A, 0x2592, 0x2500, 0x2502, 0x253C, + 0x2524, 0x252C, 0x251C, 0x2534, 0x2510, 0x250C, 0x2514, 0x2518, + 0x03B2, 0x221E, 0x03C6, 0x00B1, 0x00BD, 0x00BC, 0x2248, 0x00AB, + 0x00BB, 0xFEF7, 0xFEF8, NONE, NONE, 0xFEFB, 0xFEFC, NONE, + 0x00A0, 0x00AD, 0xFE82, 0x00A3, 0x00A4, 0xFE84, NONE, NONE, + 0xFE8E, 0xFE8F, 0xFE95, 0xFE99, 0x060C, 0xFE9D, 0xFEA1, 0xFEA5, + 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, + 0x0668, 0x0669, 0xFED1, 0x061B, 0xFEB1, 0xFEB5, 0xFEB9, 0x061F, + 0x00A2, 0xFE80, 0xFE81, 0xFE83, 0xFE85, 0xFECA, 0xFE8B, 0xFE8D, + 0xFE91, 0xFE93, 0xFE97, 0xFE9B, 0xFE9F, 0xFEA3, 0xFEA7, 0xFEA9, + 0xFEAB, 0xFEAD, 0xFEAF, 0xFEB3, 0xFEB7, 0xFEBB, 0xFEBF, 0xFEC1, + 0xFEC5, 0xFECB, 0xFECF, 0x00A6, 0x00AC, 0x00F7, 0x00D7, 0xFEC9, + 0x0640, 0xFED3, 0xFED7, 0xFEDB, 0xFEDF, 0xFEE3, 0xFEE7, 0xFEEB, + 0xFEED, 0xFEEF, 0xFEF3, 0xFEBD, 0xFECC, 0xFECE, 0xFECD, 0xFEE1, + 0xFE7D, 0x0651, 0xFEE5, 0xFEE9, 0xFEEC, 0xFEF0, 0xFEF2, 0xFED0, + 0xFED5, 0xFEF5, 0xFEF6, 0xFEDD, 0xFED9, 0xFEF1, 0x25A0, NONE + }; + + public Cp864() + { + super("Cp864", new String[] { + "Cp-864" + }); + lookupTable = lookup; + } + +} // class Cp864 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp865.java b/libjava/classpath/gnu/java/nio/charset/Cp865.java new file mode 100644 index 00000000000..a1332a74f95 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp865.java @@ -0,0 +1,91 @@ +/* Cp865.java -- Charset implementation for the Cp865 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp865 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, + 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, + 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, + 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192, + 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, + 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00A4, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, + 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229, + 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0 + }; + + public Cp865() + { + super("Cp865", new String[] { + "Cp-865" + }); + lookupTable = lookup; + } + +} // class Cp865 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp866.java b/libjava/classpath/gnu/java/nio/charset/Cp866.java new file mode 100644 index 00000000000..ca6958949f6 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp866.java @@ -0,0 +1,91 @@ +/* Cp866.java -- Charset implementation for the Cp866 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp866 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, + 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, + 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, + 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, + 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, + 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, + 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, + 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, + 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, + 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0 + }; + + public Cp866() + { + super("Cp866", new String[] { + "cp-866" + }); + lookupTable = lookup; + } + +} // class Cp866 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp869.java b/libjava/classpath/gnu/java/nio/charset/Cp869.java new file mode 100644 index 00000000000..f5e05298473 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp869.java @@ -0,0 +1,91 @@ +/* Cp869.java -- Charset implementation for the Cp869 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +public class Cp869 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + NONE, NONE, NONE, NONE, NONE, NONE, 0x0386, NONE, + 0x00B7, 0x00AC, 0x00A6, 0x2018, 0x2019, 0x0388, 0x2015, 0x0389, + 0x038A, 0x03AA, 0x038C, NONE, NONE, 0x038E, 0x03AB, 0x00A9, + 0x038F, 0x00B2, 0x00B3, 0x03AC, 0x00A3, 0x03AD, 0x03AE, 0x03AF, + 0x03CA, 0x0390, 0x03CC, 0x03CD, 0x0391, 0x0392, 0x0393, 0x0394, + 0x0395, 0x0396, 0x0397, 0x00BD, 0x0398, 0x0399, 0x00AB, 0x00BB, + 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x039A, 0x039B, 0x039C, + 0x039D, 0x2563, 0x2551, 0x2557, 0x255D, 0x039E, 0x039F, 0x2510, + 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x03A0, 0x03A1, + 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x03A3, + 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, + 0x03B3, 0x2518, 0x250C, 0x2588, 0x2584, 0x03B4, 0x03B5, 0x2580, + 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, + 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x0384, + 0x00AD, 0x00B1, 0x03C5, 0x03C6, 0x03C7, 0x00A7, 0x03C8, 0x0385, + 0x00B0, 0x00A8, 0x03C9, 0x03CB, 0x03B0, 0x03CE, 0x25A0, 0x00A0 + }; + + public Cp869() + { + super("Cp869", new String[] { + "Cp-869" + }); + lookupTable = lookup; + } + +} // class Cp869 diff --git a/libjava/classpath/gnu/java/nio/charset/Cp874.java b/libjava/classpath/gnu/java/nio/charset/Cp874.java new file mode 100644 index 00000000000..d81755c1e0e --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Cp874.java @@ -0,0 +1,89 @@ +/* Cp874.java -- Charset implementation for the Cp874 character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class Cp874 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, NONE, NONE, NONE, 0x2026, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, + 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, + 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17, + 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F, + 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27, + 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, + 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, + 0x0E38, 0x0E39, 0x0E3A, NONE, NONE, NONE, NONE, 0x0E3F, + 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, + 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, + 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, + 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, NONE, NONE, NONE, NONE + }; + + public Cp874() + { + super("Cp874", new String[] { + }); + lookupTable = lookup; + } + +} // class Cp874 diff --git a/libjava/classpath/gnu/java/nio/charset/EncodingHelper.java b/libjava/classpath/gnu/java/nio/charset/EncodingHelper.java new file mode 100644 index 00000000000..033440d5da2 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/EncodingHelper.java @@ -0,0 +1,154 @@ +/* EncodingHelper.java -- Useful character encoding methods. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +import java.util.HashMap; +import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.UnsupportedCharsetException; +import java.nio.charset.Charset; +import java.io.UnsupportedEncodingException; + +/** + * This class provides some useful utility methods + * for charset encoding for the java.lang and java.io methods. + * + * @author Sven de Marothy + */ +public class EncodingHelper +{ + + /** + * Contains the mapping from java.io canonical names + * to java.nio canonical names. + */ + private static HashMap canonicalNames; + + static { + canonicalNames = new HashMap(); + canonicalNames.put("US-ASCII", "ASCII"); + canonicalNames.put("windows-1250", "Cp1250"); + canonicalNames.put("windows-1251", "Cp1251"); + canonicalNames.put("windows-1252", "Cp1252"); + canonicalNames.put("windows-1253", "Cp1253"); + canonicalNames.put("windows-1254", "Cp1254"); + canonicalNames.put("windows-1257", "Cp1257"); + canonicalNames.put("ISO-8859-1", "ISO8859_1"); + canonicalNames.put("ISO-8859-2", "ISO8859_2"); + canonicalNames.put("ISO-8859-4", "ISO8859_4"); + canonicalNames.put("ISO-8859-5", "ISO8859_5"); + canonicalNames.put("ISO-8859-7", "ISO8859_7"); + canonicalNames.put("ISO-8859-9", "ISO8859_9"); + canonicalNames.put("ISO-8859-13", "ISO8859_13"); + canonicalNames.put("ISO-8859-15", "ISO8859_15"); + canonicalNames.put("KOI8-R", "KOI8_R"); + canonicalNames.put("UTF-8", "UTF8"); + canonicalNames.put("UTF-16BE", "UnicodeBigUnmarked"); + canonicalNames.put("UTF-16LE", "UnicodeLittleUnmarked"); + canonicalNames.put("windows-1255", "Cp1255"); + canonicalNames.put("windows-1256", "Cp1256"); + canonicalNames.put("windows-1258", "Cp1258"); + canonicalNames.put("ISO-8859-3", "ISO8859_3"); + canonicalNames.put("ISO-8859-6", "ISO8859_6"); + canonicalNames.put("ISO-8859-8", "ISO8859_8"); + } + + /** + * Returns the name of the default encoding, + * falls back on defaults to Latin-1 if there's a problem. + */ + public static String getDefaultEncoding() + { + String encoding; + try + { + return System.getProperty("file.encoding"); + } catch(SecurityException e) { + } catch(IllegalArgumentException e) { + } + // XXX - Throw an error here? For now, default to the 'safe' encoding. + return "8859_1"; + } + + /** + * Returns the java.io canonical name of a charset given with the + * java.nio canonical name. If the charset does not have a java.io + * canonical name, the input string is returned. + */ + public static String getOldCanonical(String newCanonical) + { + String oldCanonical = (String) canonicalNames.get(newCanonical); + return (oldCanonical != null)?oldCanonical : newCanonical; + } + + public static boolean isISOLatin1(String s) + { + if(s.equals("ISO-8859-1") || + s.equals("8859_1") || + s.equals("ISO_8859-1") || + s.equals("latin1") || + s.equals("ISO8859_1") || + s.equals("ISO_8859_1")) + return true; + return false; + } + + /** + * Gets a charset, throwing the java.io exception and not + * the java.nio exception if an error occurs. + */ + public static Charset getCharset(String name) + throws UnsupportedEncodingException + { + try + { + return Charset.forName(name); + } + catch(IllegalCharsetNameException e) + { + throw new UnsupportedEncodingException("Charset "+name+" not found."); + } + catch(UnsupportedCharsetException e) + { + throw new UnsupportedEncodingException("Charset "+name+" not found."); + } + } +} + + + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_1.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_1.java new file mode 100644 index 00000000000..cc06ecd5ad0 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_1.java @@ -0,0 +1,155 @@ +/* ISO_8859_1.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +/** + * ISO-8859-1 charset. + * + * @author Jesse Rosenstock + */ +final class ISO_8859_1 extends Charset +{ + ISO_8859_1 () + { + /* Canonical charset name chosen according to: + * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html + */ + super ("ISO-8859-1", new String[] { + /* These names are provided by + * http://www.iana.org/assignments/character-sets + */ + "iso-ir-100", + "ISO_8859-1", + "latin1", + "l1", + "IBM819", + "CP819", + "csISOLatin1", + "8859_1", + /* These names are provided by + * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL + */ + "ISO8859_1", "ISO_8859_1", "ibm-819", "ISO_8859-1:1987", + "819" + }); + + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || cs instanceof ISO_8859_1; + } + + public CharsetDecoder newDecoder () + { + return new Decoder (this); + } + + public CharsetEncoder newEncoder () + { + return new Encoder (this); + } + + private static final class Decoder extends CharsetDecoder + { + // Package-private to avoid a trampoline constructor. + Decoder (Charset cs) + { + super (cs, 1.0f, 1.0f); + } + + protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + byte b = in.get (); + + if (!out.hasRemaining ()) + { + in.position (in.position () - 1); + return CoderResult.OVERFLOW; + } + + out.put ((char) (b & 0xFF)); + } + + return CoderResult.UNDERFLOW; + } + } + + private static final class Encoder extends CharsetEncoder + { + // Package-private to avoid a trampoline constructor. + Encoder (Charset cs) + { + super (cs, 1.0f, 1.0f); + } + + protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + char c = in.get (); + + if (c > 0xFF) + { + in.position (in.position () - 1); + return CoderResult.unmappableForLength (1); + } + if (!out.hasRemaining ()) + { + in.position (in.position () - 1); + return CoderResult.OVERFLOW; + } + + out.put ((byte) c); + } + + return CoderResult.UNDERFLOW; + } + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_13.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_13.java new file mode 100644 index 00000000000..c10eef305c3 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_13.java @@ -0,0 +1,104 @@ +/* ISO_8859_13.java -- Charset for ISO-8859-13 iso latin character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-13, ISO Latin-7 char set. + */ +public class ISO_8859_13 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x201D, 0x00A2, 0x00A3, 0x00A4, 0x201E, 0x00A6, 0x00A7, + 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x201C, 0x00B5, 0x00B6, 0x00B7, + 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6, + 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112, + 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B, + 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, + 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF, + 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, + 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C, + 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, + 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x2019 + }; + + public ISO_8859_13() + { + super("ISO-8859-13", new String[] { + "ISO8859_13", + "8859_13", + "ibm-921_P100-1995", + "ibm-921", + "iso_8859_13", + "iso8859_13", + "iso-8859-13", + "8859_13", + "cp921", + "921" + }); + lookupTable = lookup; + } + +} // class ISO_8859_13 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_15.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_15.java new file mode 100644 index 00000000000..973fe1c947d --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_15.java @@ -0,0 +1,111 @@ +/* ISO_8859_15.java -- Charset for ISO-8859-15 iso latin character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-15, ISO Latin-9 char set. + */ +public class ISO_8859_15 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AC, 0x00A5, 0x0160, 0x00A7, + 0x0161, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x017D, 0x00B5, 0x00B6, 0x00B7, + 0x017E, 0x00B9, 0x00BA, 0x00BB, 0x0152, 0x0153, 0x0178, 0x00BF, + 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, + 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, + 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, + 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF + }; + + public ISO_8859_15() + { + super("ISO-8859-15", new String[] { + "8859_15", + "iso8859_15", + "iso-8859-15", + "8859-15", + "latin9", + "iso_8859_15", + "ibm-923_P100-1998", + "ibm-923", + "Latin-9", + "l9", + "latin0", + "csisolatin0", + "csisolatin9", + "iso8859_15_fdis", + "cp923", + "923", + "windows-28605" + }); + lookupTable = lookup; + } + +} // class ISO_8859_15 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_2.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_2.java new file mode 100644 index 00000000000..2de96df9b68 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_2.java @@ -0,0 +1,110 @@ +/* ISO_8859_2.java -- Charset for ISO-8859-2 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-2, ISO Latin-2 char set. + */ +public class ISO_8859_2 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + * Note: ranges 0-1F and 7f-97 aren't defined in the spec file aron used. + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, + 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, + 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, + 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C, + 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, + 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, + 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, + 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, + 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, + 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, + 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, + 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9 + }; + + public ISO_8859_2() + { + super("ISO-8859-2", new String[] { + "ISO8859_2", + "8859_2", + "ibm-912_P100-1995", + "ibm-912", + "iso_8859_2", + "iso8859_2", + "iso-8859-2", + "ISO_8859-2:1987", + "latin2", + "csISOLatin2", + "iso-ir-101", + "l2", + "cp912", + "912", + "windows-28592" + }); + lookupTable = lookup; + } + +} // class ISO_8859_2 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_3.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_3.java new file mode 100644 index 00000000000..6e718719a2d --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_3.java @@ -0,0 +1,109 @@ +/* ISO_8859_3.java -- Charset for ISO-8859-3 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-3, ISO Latin-3 char set. + */ +public class ISO_8859_3 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, NONE, 0x0124, 0x00A7, + 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, NONE, 0x017B, + 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7, + 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, NONE, 0x017C, + 0x00C0, 0x00C1, 0x00C2, NONE, 0x00C4, 0x010A, 0x0108, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, + NONE, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x0120, 0x00D6, 0x00D7, + 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x016C, 0x015C, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, NONE, 0x00E4, 0x010B, 0x0109, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + NONE, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7, + 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9 + }; + + public ISO_8859_3() + { + super("ISO-8859-3", new String[] { + "ISO8859_3", + "8859_3", + "ibm-913_P100-2000", + "ibm-913", + "iso_8859_3", + "iso8859_3", + "iso-8859-3", + "ISO_8859-3:1988", + "latin3", + "csISOLatin3", + "iso-ir-109", + "l3", + "cp913", + "913", + "windows-28593" + }); + lookupTable = lookup; + } + +} // class ISO_8859_3 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_4.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_4.java new file mode 100644 index 00000000000..96dc4675476 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_4.java @@ -0,0 +1,110 @@ +/* ISO_8859_4.java -- Charset for ISO-8859-4 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-4, ISO Latin-4 char set. + */ +public class ISO_8859_4 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + * Note: ranges 0-1F and 7f-9f aren't defined in the spec file aron used. + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7, + 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF, + 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7, + 0x00B8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014A, 0x017E, 0x014B, + 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E, + 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x012A, + 0x0110, 0x0145, 0x014C, 0x0136, 0x00D4, 0x00D5, 0x00D6, 0x00D7, + 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x0168, 0x016A, 0x00DF, + 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F, + 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B, + 0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7, + 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9 + }; + + public ISO_8859_4() + { + super("ISO-8859-4", new String[] { + "ISO8859_4", + "8859_4", + "ibm-914_P100-1995", + "ibm-914", + "iso_8859_4", + "iso8859_4", + "iso-8859-4", + "latin4", + "csISOLatin4", + "iso-ir-110", + "ISO_8859-4:1988", + "l4", + "cp914", + "914", + "windows-28594" + }); + lookupTable = lookup; + } + +} // class ISO_8859_4 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_5.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_5.java new file mode 100644 index 00000000000..ad208729c85 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_5.java @@ -0,0 +1,108 @@ +/* ISO_8859_5.java -- Charset for ISO-8859-5 cyrillic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-5, ISO cyrillic char set. + */ +public class ISO_8859_5 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, + 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00AD, 0x040E, 0x040F, + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, + 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, + 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, + 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, + 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, + 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, + 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F + }; + + public ISO_8859_5() + { + super("ISO-8859-5", new String[] { + "ISO8859_5", + "8859_5", + "ibm-915_P100-1995", + "ibm-915", + "iso_8859_5", + "iso8859_5", + "iso-8859-5", + "cyrillic", + "csISOLatinCyrillic", + "iso-ir-144", + "ISO_8859-5:1988", + "cp915", + "915", + "windows-28595" + }); + lookupTable = lookup; + } + +} // class ISO_8859_5 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_6.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_6.java new file mode 100644 index 00000000000..5600e792386 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_6.java @@ -0,0 +1,112 @@ +/* ISO_8859_6.java -- Charset for ISO-8859-6 iso Arabic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-6, ISO Arabic char set. + */ +public class ISO_8859_6 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, NONE, NONE, NONE, 0x00A4, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, 0x060C, 0x00AD, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, 0x061B, NONE, NONE, NONE, 0x061F, + NONE, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, + 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, + 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637, + 0x0638, 0x0639, 0x063A, NONE, NONE, NONE, NONE, NONE, + 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, + 0x0648, 0x0649, 0x064A, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F, + 0x0650, 0x0651, 0x0652, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE + }; + + public ISO_8859_6() + { + super("ISO-8859-6", new String[] { + "8859_6", + "ibm-1089_P100-1995", + "ibm-1089", + "iso_8859_6", + "iso8859_6", + "iso-8859-6", + "arabic", + "csISOLatinArabic", + "iso-ir-127", + "ISO_8859-6:1987", + "ECMA-114", + "ASMO-708", + "8859_6", + "cp1089", + "1089", + "windows-28596", + "ISO-8859-6-I", + "ISO-8859-6-E" + }); + lookupTable = lookup; + } + +} // class ISO_8859_6 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_7.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_7.java new file mode 100644 index 00000000000..9262a607723 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_7.java @@ -0,0 +1,111 @@ +/* ISO_8859_7.java -- Charset for ISO-8859-7 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-7, ISO Latin/Greek char set. + */ +public class ISO_8859_7 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x02BD, 0x02BC, 0x00A3, NONE, NONE, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, NONE, 0x00AB, 0x00AC, 0x00AD, NONE, 0x2015, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x0385, 0x0386, 0x00B7, + 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F, + 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, + 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, + 0x03A0, 0x03A1, NONE, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, + 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, + 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, + 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, + 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, + 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, NONE + }; + + public ISO_8859_7() + { + super("ISO-8859-7", new String[] { + "ISO8859_7", + "8859_7", + "ibm-813_P100-1995", + "ibm-813", + "iso_8859_7", + "iso8859_7", + "iso-8859-7", + "greek", + "greek8", + "ELOT_928", + "ECMA-118", + "csISOLatinGreek", + "iso-ir-126", + "ISO_8859-7:1987", + "cp813", + "813", + "windows-28597" + }); + lookupTable = lookup; + } + +} // class ISO_8859_7 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_8.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_8.java new file mode 100644 index 00000000000..96fb0f48b82 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_8.java @@ -0,0 +1,110 @@ +/* ISO_8859_8.java -- Charset for ISO-8859-8 iso Hebrew character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-8, ISO Latin/Hebrew char set. + */ +public class ISO_8859_8 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, NONE, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x203E, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, 0x2017, + 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, + 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF, + 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, + 0x05E8, 0x05E9, 0x05EA, NONE, NONE, NONE, NONE, NONE + }; + + public ISO_8859_8() + { + super("ISO-8859-8", new String[] { + "ISO8859_8", + "8859_8", + "ibm-916_P100-1995", + "ibm-916", + "iso_8859_8", + "iso8859_8", + "iso-8859-8", + "hebrew", + "csISOLatinHebrew", + "iso-ir-138", + "ISO_8859-8:1988", + "ISO-8859-8-I", + "ISO-8859-8-E", + "cp916", + "916", + "windows-28598" + }); + lookupTable = lookup; + } + +} // class ISO_8859_8 + diff --git a/libjava/classpath/gnu/java/nio/charset/ISO_8859_9.java b/libjava/classpath/gnu/java/nio/charset/ISO_8859_9.java new file mode 100644 index 00000000000..28be34cf877 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/ISO_8859_9.java @@ -0,0 +1,110 @@ +/* ISO_8859_9.java -- Charset for ISO-8859-9 iso latin character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for ISO-8859-9, ISO Latin-5 char set. + */ +public class ISO_8859_9 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, + 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, + 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, + 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, + 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF + }; + + public ISO_8859_9() + { + super("ISO-8859-9", new String[] { + "ISO8859_9", + "8859_9", + "ibm-920_P100-1995", + "ibm-920", + "iso8859_9", + "iso-8859-9", + "iso_8859_9", + "latin5", + "csISOLatin5", + "iso-ir-148", + "ISO_8859-9:1989", + "l5", + "cp920", + "920", + "windows-28599", + "ECMA-128" + }); + lookupTable = lookup; + } + +} // class ISO_8859_9 + diff --git a/libjava/classpath/gnu/java/nio/charset/KOI_8.java b/libjava/classpath/gnu/java/nio/charset/KOI_8.java new file mode 100644 index 00000000000..c6706560702 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/KOI_8.java @@ -0,0 +1,102 @@ +/* KOI_8.java -- Charset for KOI-8 cyrillic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for the KOI8 cyrillic char set. + */ +public class KOI_8 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, + 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590, + 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248, + 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7, + 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, + 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E, + 0x255F, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, + 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9, + 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, + 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, + 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, + 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A, + 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, + 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, + 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, + 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A + }; + + public KOI_8() + { + super("KOI8-R", new String[] { + "KOI8_R", + "KOI8", + "KOI-8", + "KOI_8", + "koi8-r", + "koi8r", + "koi-8-r", + "koi" + }); + lookupTable = lookup; + } + +} // class KOI_8 + diff --git a/libjava/classpath/gnu/java/nio/charset/MS874.java b/libjava/classpath/gnu/java/nio/charset/MS874.java new file mode 100644 index 00000000000..b16e53f4243 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MS874.java @@ -0,0 +1,89 @@ +/* MS874.java -- Charset implementation for the MS874 Thai character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MS874 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, NONE, NONE, NONE, 0x2026, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, + 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, + 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17, + 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F, + 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27, + 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, + 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, + 0x0E38, 0x0E39, 0x0E3A, NONE, NONE, NONE, NONE, 0x0E3F, + 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, + 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, + 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, + 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, NONE, NONE, NONE, NONE + }; + + public MS874() + { + super("MS874", new String[] { + }); + lookupTable = lookup; + } + +} // class MS874 diff --git a/libjava/classpath/gnu/java/nio/charset/MacCentralEurope.java b/libjava/classpath/gnu/java/nio/charset/MacCentralEurope.java new file mode 100644 index 00000000000..5496db2b5ea --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacCentralEurope.java @@ -0,0 +1,89 @@ +/* MacCentralEurope.java -- Charset implementation for the MacCentralEurope character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacCentralEurope extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x0100, 0x0101, 0x00C9, 0x0104, 0x00D6, 0x00DC, 0x00E1, + 0x0105, 0x010C, 0x00E4, 0x010D, 0x0106, 0x0107, 0x00E9, 0x0179, + 0x017A, 0x010E, 0x00ED, 0x010F, 0x0112, 0x0113, 0x0116, 0x00F3, + 0x0117, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x011A, 0x011B, 0x00FC, + 0x2020, 0x00B0, 0x0118, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, + 0x00AE, 0x00A9, 0x2122, 0x0119, 0x00A8, 0x2260, 0x0123, 0x012E, + 0x012F, 0x012A, 0x2264, 0x2265, 0x012B, 0x0136, 0x2202, 0x2211, + 0x0142, 0x013B, 0x013C, 0x013D, 0x013E, 0x0139, 0x013A, 0x0145, + 0x0146, 0x0143, 0x00AC, 0x221A, 0x0144, 0x0147, 0x2206, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x0148, 0x0150, 0x00D5, 0x0151, 0x014C, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, + 0x014D, 0x0154, 0x0155, 0x0158, 0x2039, 0x203A, 0x0159, 0x0156, + 0x0157, 0x0160, 0x201A, 0x201E, 0x0161, 0x015A, 0x015B, 0x00C1, + 0x0164, 0x0165, 0x00CD, 0x017D, 0x017E, 0x016A, 0x00D3, 0x00D4, + 0x016B, 0x016E, 0x00DA, 0x016F, 0x0170, 0x0171, 0x0172, 0x0173, + 0x00DD, 0x00FD, 0x0137, 0x017B, 0x0141, 0x017C, 0x0122, 0x02C7 + }; + + public MacCentralEurope() + { + super("MacCentralEurope", new String[] { + }); + lookupTable = lookup; + } + +} // class MacCentralEurope diff --git a/libjava/classpath/gnu/java/nio/charset/MacCroatian.java b/libjava/classpath/gnu/java/nio/charset/MacCroatian.java new file mode 100644 index 00000000000..f71ac5199b7 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacCroatian.java @@ -0,0 +1,89 @@ +/* MacCroatian.java -- Charset implementation for the MacCroatian character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacCroatian extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, + 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, + 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, + 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, + 0x00AE, 0x0160, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x017D, 0x00D8, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x2206, 0x00B5, 0x2202, 0x2211, + 0x220F, 0x0161, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x017E, 0x00F8, + 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x0106, 0x00AB, + 0x010C, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, + 0x0110, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, + 0xF8FF, 0x00A9, 0x2044, 0x20AC, 0x2039, 0x203A, 0x00C6, 0x00BB, + 0x2013, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x0107, 0x00C1, + 0x010D, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, + 0x0111, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, + 0x00AF, 0x03C0, 0x00CB, 0x02DA, 0x00B8, 0x00CA, 0x00E6, 0x02C7 + }; + + public MacCroatian() + { + super("MacCroatian", new String[] { + }); + lookupTable = lookup; + } + +} // class MacCroatian diff --git a/libjava/classpath/gnu/java/nio/charset/MacCyrillic.java b/libjava/classpath/gnu/java/nio/charset/MacCyrillic.java new file mode 100644 index 00000000000..f152f6b1c1b --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacCyrillic.java @@ -0,0 +1,89 @@ +/* MacCyrillic.java -- Charset implementation for the MacCyrillic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacCyrillic extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, + 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, + 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, + 0x2020, 0x00B0, 0x0490, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x0406, + 0x00AE, 0x00A9, 0x2122, 0x0402, 0x0452, 0x2260, 0x0403, 0x0453, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x0456, 0x00B5, 0x0491, 0x0408, + 0x0404, 0x0454, 0x0407, 0x0457, 0x0409, 0x0459, 0x040A, 0x045A, + 0x0458, 0x0405, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x040B, 0x045B, 0x040C, 0x045C, 0x0455, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x201E, + 0x040E, 0x045E, 0x040F, 0x045F, 0x2116, 0x0401, 0x0451, 0x044F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, + 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, + 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x20AC + }; + + public MacCyrillic() + { + super("MacCyrillic", new String[] { + }); + lookupTable = lookup; + } + +} // class MacCyrillic diff --git a/libjava/classpath/gnu/java/nio/charset/MacDingbat.java b/libjava/classpath/gnu/java/nio/charset/MacDingbat.java new file mode 100644 index 00000000000..84102d56f67 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacDingbat.java @@ -0,0 +1,89 @@ +/* MacDingbat.java -- Charset implementation for the MacDingbat character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacDingbat extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x2701, 0x2702, 0x2703, 0x2704, 0x260E, 0x2706, 0x2707, + 0x2708, 0x2709, 0x261B, 0x261E, 0x270C, 0x270D, 0x270E, 0x270F, + 0x2710, 0x2711, 0x2712, 0x2713, 0x2714, 0x2715, 0x2716, 0x2717, + 0x2718, 0x2719, 0x271A, 0x271B, 0x271C, 0x271D, 0x271E, 0x271F, + 0x2720, 0x2721, 0x2722, 0x2723, 0x2724, 0x2725, 0x2726, 0x2727, + 0x2605, 0x2729, 0x272A, 0x272B, 0x272C, 0x272D, 0x272E, 0x272F, + 0x2730, 0x2731, 0x2732, 0x2733, 0x2734, 0x2735, 0x2736, 0x2737, + 0x2738, 0x2739, 0x273A, 0x273B, 0x273C, 0x273D, 0x273E, 0x273F, + 0x2740, 0x2741, 0x2742, 0x2743, 0x2744, 0x2745, 0x2746, 0x2747, + 0x2748, 0x2749, 0x274A, 0x274B, 0x25CF, 0x274D, 0x25A0, 0x274F, + 0x2750, 0x2751, 0x2752, 0x25B2, 0x25BC, 0x25C6, 0x2756, 0x25D7, + 0x2758, 0x2759, 0x275A, 0x275B, 0x275C, 0x275D, 0x275E, NONE, + 0x2768, 0x2769, 0x276A, 0x276B, 0x276C, 0x276D, 0x276E, 0x276F, + 0x2770, 0x2771, 0x2772, 0x2773, 0x2774, 0x2775, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, 0x2761, 0x2762, 0x2763, 0x2764, 0x2765, 0x2766, 0x2767, + 0x2663, 0x2666, 0x2665, 0x2660, 0x2460, 0x2461, 0x2462, 0x2463, + 0x2464, 0x2465, 0x2466, 0x2467, 0x2468, 0x2469, 0x2776, 0x2777, + 0x2778, 0x2779, 0x277A, 0x277B, 0x277C, 0x277D, 0x277E, 0x277F, + 0x2780, 0x2781, 0x2782, 0x2783, 0x2784, 0x2785, 0x2786, 0x2787, + 0x2788, 0x2789, 0x278A, 0x278B, 0x278C, 0x278D, 0x278E, 0x278F, + 0x2790, 0x2791, 0x2792, 0x2793, 0x2794, 0x2192, 0x2194, 0x2195, + 0x2798, 0x2799, 0x279A, 0x279B, 0x279C, 0x279D, 0x279E, 0x279F, + 0x27A0, 0x27A1, 0x27A2, 0x27A3, 0x27A4, 0x27A5, 0x27A6, 0x27A7, + 0x27A8, 0x27A9, 0x27AA, 0x27AB, 0x27AC, 0x27AD, 0x27AE, 0x27AF, + NONE, 0x27B1, 0x27B2, 0x27B3, 0x27B4, 0x27B5, 0x27B6, 0x27B7, + 0x27B8, 0x27B9, 0x27BA, 0x27BB, 0x27BC, 0x27BD, 0x27BE, NONE + }; + + public MacDingbat() + { + super("MacDingbat", new String[] { + }); + lookupTable = lookup; + } + +} // class MacDingbat diff --git a/libjava/classpath/gnu/java/nio/charset/MacGreek.java b/libjava/classpath/gnu/java/nio/charset/MacGreek.java new file mode 100644 index 00000000000..07624d59eff --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacGreek.java @@ -0,0 +1,89 @@ +/* MacGreek.java -- Charset implementation for the MacGreek character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacGreek extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x00B9, 0x00B2, 0x00C9, 0x00B3, 0x00D6, 0x00DC, 0x0385, + 0x00E0, 0x00E2, 0x00E4, 0x0384, 0x00A8, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00A3, 0x2122, 0x00EE, 0x00EF, 0x2022, 0x00BD, + 0x2030, 0x00F4, 0x00F6, 0x00A6, 0x20AC, 0x00F9, 0x00FB, 0x00FC, + 0x2020, 0x0393, 0x0394, 0x0398, 0x039B, 0x039E, 0x03A0, 0x00DF, + 0x00AE, 0x00A9, 0x03A3, 0x03AA, 0x00A7, 0x2260, 0x00B0, 0x00B7, + 0x0391, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x0392, 0x0395, 0x0396, + 0x0397, 0x0399, 0x039A, 0x039C, 0x03A6, 0x03AB, 0x03A8, 0x03A9, + 0x03AC, 0x039D, 0x00AC, 0x039F, 0x03A1, 0x2248, 0x03A4, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x03A5, 0x03A7, 0x0386, 0x0388, 0x0153, + 0x2013, 0x2015, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x0389, + 0x038A, 0x038C, 0x038E, 0x03AD, 0x03AE, 0x03AF, 0x03CC, 0x038F, + 0x03CD, 0x03B1, 0x03B2, 0x03C8, 0x03B4, 0x03B5, 0x03C6, 0x03B3, + 0x03B7, 0x03B9, 0x03BE, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BF, + 0x03C0, 0x03CE, 0x03C1, 0x03C3, 0x03C4, 0x03B8, 0x03C9, 0x03C2, + 0x03C7, 0x03C5, 0x03B6, 0x03CA, 0x03CB, 0x0390, 0x03B0, 0x00AD + }; + + public MacGreek() + { + super("MacGreek", new String[] { + }); + lookupTable = lookup; + } + +} // class MacGreek diff --git a/libjava/classpath/gnu/java/nio/charset/MacIceland.java b/libjava/classpath/gnu/java/nio/charset/MacIceland.java new file mode 100644 index 00000000000..7918e026622 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacIceland.java @@ -0,0 +1,89 @@ +/* MacIceland.java -- Charset implementation for the MacIceland character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacIceland extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, + 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, + 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, + 0x00DD, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, + 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, + 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, + 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, + 0x00FF, 0x0178, 0x2044, 0x20AC, 0x00D0, 0x00F0, 0x00DE, 0x00FE, + 0x00FD, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, + 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, + 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, + 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7 + }; + + public MacIceland() + { + super("MacIceland", new String[] { + }); + lookupTable = lookup; + } + +} // class MacIceland diff --git a/libjava/classpath/gnu/java/nio/charset/MacRoman.java b/libjava/classpath/gnu/java/nio/charset/MacRoman.java new file mode 100644 index 00000000000..b413caf2be7 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacRoman.java @@ -0,0 +1,89 @@ +/* MacRoman.java -- Charset implementation for the MacRoman character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacRoman extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, + 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, + 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, + 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, + 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, + 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, + 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, + 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, + 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, + 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, + 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, + 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7 + }; + + public MacRoman() + { + super("MacRoman", new String[] { + }); + lookupTable = lookup; + } + +} // class MacRoman diff --git a/libjava/classpath/gnu/java/nio/charset/MacRomania.java b/libjava/classpath/gnu/java/nio/charset/MacRomania.java new file mode 100644 index 00000000000..d1779a40eb8 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacRomania.java @@ -0,0 +1,89 @@ +/* MacRomania.java -- Charset implementation for the MacRomania character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacRomania extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, + 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, + 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, + 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, + 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x0102, 0x0218, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, + 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x0103, 0x0219, + 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, + 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0x021A, 0x021B, + 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, + 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, + 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, + 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7 + }; + + public MacRomania() + { + super("MacRomania", new String[] { + }); + lookupTable = lookup; + } + +} // class MacRomania diff --git a/libjava/classpath/gnu/java/nio/charset/MacSymbol.java b/libjava/classpath/gnu/java/nio/charset/MacSymbol.java new file mode 100644 index 00000000000..869de194736 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacSymbol.java @@ -0,0 +1,89 @@ +/* MacSymbol.java -- Charset implementation for the MacSymbol character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacSymbol extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220D, + 0x0028, 0x0029, 0x2217, 0x002B, 0x002C, 0x2212, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x2245, 0x0391, 0x0392, 0x03A7, 0x0394, 0x0395, 0x03A6, 0x0393, + 0x0397, 0x0399, 0x03D1, 0x039A, 0x039B, 0x039C, 0x039D, 0x039F, + 0x03A0, 0x0398, 0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03C2, 0x03A9, + 0x039E, 0x03A8, 0x0396, 0x005B, 0x2234, 0x005D, 0x22A5, 0x005F, + 0xF8E5, 0x03B1, 0x03B2, 0x03C7, 0x03B4, 0x03B5, 0x03C6, 0x03B3, + 0x03B7, 0x03B9, 0x03D5, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BF, + 0x03C0, 0x03B8, 0x03C1, 0x03C3, 0x03C4, 0x03C5, 0x03D6, 0x03C9, + 0x03BE, 0x03C8, 0x03B6, 0x007B, 0x007C, 0x007D, 0x223C, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + 0x20AC, 0x03D2, 0x2032, 0x2264, 0x2044, 0x221E, 0x0192, 0x2663, + 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193, + 0x00B0, 0x00B1, 0x2033, 0x2265, 0x00D7, 0x221D, 0x2202, 0x2022, + 0x00F7, 0x2260, 0x2261, 0x2248, 0x2026, 0xF8E6, 0x23AF, 0x21B5, + 0x2135, 0x2111, 0x211C, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229, + 0x222A, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209, + 0x2220, 0x2207, 0x00AE, 0x00A9, 0x2122, 0x220F, 0x221A, 0x22C5, + 0x00AC, 0x2227, 0x2228, 0x21D4, 0x21D0, 0x21D1, 0x21D2, 0x21D3, + 0x22C4, 0x3008, NONE, NONE, NONE, 0x2211, 0x239B, 0x239C, + 0x239D, 0x23A1, 0x23A2, 0x23A3, 0x23A7, 0x23A8, 0x23A9, 0x23AA, + 0xF8FF, 0x3009, 0x222B, 0x2320, 0x23AE, 0x2321, 0x239E, 0x239F, + 0x23A0, 0x23A4, 0x23A5, 0x23A6, 0x23AB, 0x23AC, 0x23AD, NONE + }; + + public MacSymbol() + { + super("MacSymbol", new String[] { + }); + lookupTable = lookup; + } + +} // class MacSymbol diff --git a/libjava/classpath/gnu/java/nio/charset/MacThai.java b/libjava/classpath/gnu/java/nio/charset/MacThai.java new file mode 100644 index 00000000000..498b9e6bb4c --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacThai.java @@ -0,0 +1,89 @@ +/* MacThai.java -- Charset implementation for the MacThai character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacThai extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00AB, 0x00BB, 0x2026, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, 0x201C, 0x201D, NONE, + NONE, 0x2022, NONE, NONE, NONE, NONE, NONE, NONE, + NONE, NONE, NONE, NONE, NONE, 0x2018, 0x2019, NONE, + 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, + 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, + 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17, + 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F, + 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27, + 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, + 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, + 0x0E38, 0x0E39, 0x0E3A, 0x2060, 0x200B, 0x2013, 0x2014, 0x0E3F, + 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, + 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x2122, 0x0E4F, + 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, + 0x0E58, 0x0E59, 0x00AE, 0x00A9, NONE, NONE, NONE, NONE + }; + + public MacThai() + { + super("MacThai", new String[] { + }); + lookupTable = lookup; + } + +} // class MacThai diff --git a/libjava/classpath/gnu/java/nio/charset/MacTurkish.java b/libjava/classpath/gnu/java/nio/charset/MacTurkish.java new file mode 100644 index 00000000000..68ea27b8cee --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/MacTurkish.java @@ -0,0 +1,89 @@ +/* MacTurkish.java -- Charset implementation for the MacTurkish character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +public class MacTurkish extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, NONE, + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, + 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, + 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, + 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, + 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, + 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, + 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, + 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, + 0x00FF, 0x0178, 0x011E, 0x011F, 0x0130, 0x0131, 0x015E, 0x015F, + 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, + 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, + 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0xF8A0, 0x02C6, 0x02DC, + 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7 + }; + + public MacTurkish() + { + super("MacTurkish", new String[] { + }); + lookupTable = lookup; + } + +} // class MacTurkish diff --git a/libjava/classpath/gnu/java/nio/charset/Provider.java b/libjava/classpath/gnu/java/nio/charset/Provider.java new file mode 100644 index 00000000000..3f25c598851 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Provider.java @@ -0,0 +1,239 @@ +/* Provider.java -- + Copyright (C) 2002, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.charset.Charset; +import java.nio.charset.spi.CharsetProvider; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; + +/** + * Charset provider for the required charsets. Used by + * {@link Charset#charsetForName} and * {@link Charset#availableCharsets}. + * + * @author Jesse Rosenstock + * @author Robert Schuster (thebohemian@gmx.net) + * @see Charset + */ +public final class Provider extends CharsetProvider +{ + private static Provider singleton; + + static + { + synchronized (Provider.class) + { + singleton = null; + } + } + + /** + * Map from charset name to charset canonical name. The strings + * are all lower-case to allow case-insensitive retrieval of + * Charset instances. + */ + private final HashMap canonicalNames; + + /** + * Map from lower-case canonical name to Charset. + * TODO: We may want to use soft references. We would then need to keep + * track of the class name to regenerate the object. + */ + private final HashMap charsets; + + /** + * We don't load all available charsets at the start + */ + private boolean extendedLoaded; + + private Provider () + { + extendedLoaded = false; + canonicalNames = new HashMap (); + charsets = new HashMap (); + + // US-ASCII aka ISO646-US + addCharset (new US_ASCII ()); + + // ISO-8859-1 aka ISO-LATIN-1 + addCharset (new ISO_8859_1 ()); + + // UTF-8 + addCharset (new UTF_8 ()); + + // UTF-16BE + addCharset (new UTF_16BE ()); + + // UTF-16LE + addCharset (new UTF_16LE ()); + + // UTF-16 + addCharset (new UTF_16 ()); + + // UTF-16LE (marked) + addCharset (new UnicodeLittle ()); + + // Windows-1250 aka cp-1250 (East European) + addCharset (new Windows1250 ()); + + // Windows-1251 (Cyrillic) + addCharset (new Windows1251 ()); + + // Windows-1252 aka cp-1252 (Latin-1) + addCharset (new Windows1252 ()); + + // Windows-1253 (Greek) + addCharset (new Windows1253 ()); + + // Windows-1254 (Turkish) + addCharset (new Windows1254 ()); + + // Windows-1257 (Baltic) + addCharset (new Windows1257 ()); + + // ISO-8859-2 aka ISO-LATIN-2 + addCharset (new ISO_8859_2 ()); + + // ISO-8859-4 aka ISO-LATIN-4 + addCharset (new ISO_8859_4 ()); + + // ISO-8859-5 (Cyrillic) + addCharset (new ISO_8859_5 ()); + + // ISO-8859-7 (Greek) + addCharset (new ISO_8859_7 ()); + + // ISO-8859-9 aka ISO-LATIN-5 + addCharset (new ISO_8859_9 ()); + + // ISO-8859-13 aka ISO-LATIN-7 + addCharset (new ISO_8859_13 ()); + + // ISO-8859-15 aka ISO-LATIN-9 + addCharset (new ISO_8859_15 ()); + + // KOI8 (Cyrillic) + addCharset (new KOI_8 ()); + } + + /** + * Load non-mandatory charsets. + */ + private void loadExtended () + { + if(extendedLoaded) + return; + + addCharset (new ISO_8859_3 ()); // ISO-8859-3 aka ISO-LATIN-3 + addCharset (new ISO_8859_6 ()); // ISO-8859-6 (Arabic) + addCharset (new ISO_8859_8 ()); // ISO-8859-8 (Hebrew) + + // Some more codepages + addCharset (new Cp855()); // IBM Cyrillic + addCharset (new Cp857()); // IBM Turkish + addCharset (new Cp860()); // MSDOS Portugese + addCharset (new Cp861()); // MSDOS Icelandic + addCharset (new Cp862()); // PC Hebrew + addCharset (new Cp863()); // MSDOS Can. French + addCharset (new Cp864()); // PC Arabic + addCharset (new Cp865()); // MSDOS Nordic + addCharset (new Cp866()); // MSDOS Russian + addCharset (new Cp869()); // IBM modern Greek + addCharset (new Cp874()); // IBM Thai + extendedLoaded = true; + } + + public Iterator charsets () + { + loadExtended(); + return Collections.unmodifiableCollection (charsets.values ()) + .iterator (); + } + + /** + * Returns a Charset instance by converting the given + * name to lower-case, looking up the canonical charset + * name and finally looking up the Charset with that name. + * + * <p>The lookup is therefore case-insensitive.</p> + * + * @returns The Charset having <code>charsetName</code> + * as its alias or null if no such Charset exist. + */ + public Charset charsetForName (String charsetName) + { + Charset cs = (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase())); + if(cs == null && !extendedLoaded) + { + loadExtended(); + cs = (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase())); + } + return cs; + } + + /** + * Puts a Charset under its canonical name into the 'charsets' map. + * Then puts a mapping from all its alias names to the canonical name. + * + * <p>All names are converted to lower-case</p>. + * + * @param cs + */ + private void addCharset (Charset cs) + { + String canonicalName = cs.name().toLowerCase(); + charsets.put (canonicalName, cs); + + /* Adds a mapping between the canonical name + * itself making a lookup using that name + * no special case. + */ + canonicalNames.put(canonicalName, canonicalName); + + for (Iterator i = cs.aliases ().iterator (); i.hasNext (); ) + canonicalNames.put (((String) i.next()).toLowerCase(), canonicalName); + } + + public static synchronized Provider provider () + { + if (singleton == null) + singleton = new Provider (); + return singleton; + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/US_ASCII.java b/libjava/classpath/gnu/java/nio/charset/US_ASCII.java new file mode 100644 index 00000000000..d26f7ffc9ab --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/US_ASCII.java @@ -0,0 +1,161 @@ +/* US_ASCII.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +/** + * US-ASCII charset. + * + * @author Jesse Rosenstock + */ +final class US_ASCII extends Charset +{ + US_ASCII () + { + /* Canonical charset name chosen according to: + * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html + */ + super ("US-ASCII", new String[] { + /* These names are provided by + * http://www.iana.org/assignments/character-sets + */ + "iso-ir-6", + "ANSI_X3.4-1986", + "ISO_646.irv:1991", + "ASCII", + "ISO646-US", + "ASCII", + "us", + "IBM367", + "cp367", + "csASCII", + /* These names are provided by + * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL + */ + "ANSI_X3.4-1968", "iso_646.irv:1983", "ascii7", "646", + "windows-20127" + }); + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII; + } + + public CharsetDecoder newDecoder () + { + return new Decoder (this); + } + + public CharsetEncoder newEncoder () + { + return new Encoder (this); + } + + private static final class Decoder extends CharsetDecoder + { + // Package-private to avoid a trampoline constructor. + Decoder (Charset cs) + { + super (cs, 1.0f, 1.0f); + } + + protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + byte b = in.get (); + + if (b < 0) + { + in.position (in.position () - 1); + return CoderResult.malformedForLength (1); + } + if (!out.hasRemaining ()) + { + in.position (in.position () - 1); + return CoderResult.OVERFLOW; + } + + out.put ((char) b); + } + + return CoderResult.UNDERFLOW; + } + } + + private static final class Encoder extends CharsetEncoder + { + // Package-private to avoid a trampoline constructor. + Encoder (Charset cs) + { + super (cs, 1.0f, 1.0f); + } + + protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + char c = in.get (); + + if (c > Byte.MAX_VALUE) + { + in.position (in.position () - 1); + return CoderResult.unmappableForLength (1); + } + if (!out.hasRemaining ()) + { + in.position (in.position () - 1); + return CoderResult.OVERFLOW; + } + + out.put ((byte) c); + } + + return CoderResult.UNDERFLOW; + } + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/UTF_16.java b/libjava/classpath/gnu/java/nio/charset/UTF_16.java new file mode 100644 index 00000000000..b1d7705d41b --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UTF_16.java @@ -0,0 +1,80 @@ +/* UTF_16.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +/** + * UTF-16 charset. + * + * @author Jesse Rosenstock + */ +final class UTF_16 extends Charset +{ + UTF_16 () + { + super ("UTF-16", new String[] { + // witnessed by the internet + "UTF16", + /* These names are provided by + * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL + */ + "ISO-10646-UCS-2", "unicode", "csUnicode", "ucs-2", "UnicodeBig" + }); + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || cs instanceof ISO_8859_1 + || cs instanceof UTF_8 || cs instanceof UTF_16BE + || cs instanceof UTF_16LE || cs instanceof UTF_16; + } + + public CharsetDecoder newDecoder () + { + return new UTF_16Decoder (this, UTF_16Decoder.UNKNOWN_ENDIAN); + } + + public CharsetEncoder newEncoder () + { + return new UTF_16Encoder (this, UTF_16Encoder.BIG_ENDIAN, true); + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/UTF_16BE.java b/libjava/classpath/gnu/java/nio/charset/UTF_16BE.java new file mode 100644 index 00000000000..1fe3b5d7caf --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UTF_16BE.java @@ -0,0 +1,84 @@ +/* UTF_16BE.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +/** + * UTF-16BE charset. + * + * @author Jesse Rosenstock + */ +final class UTF_16BE extends Charset +{ + UTF_16BE () + { + super ("UTF-16BE", new String[] { + // witnessed by the internet + "UTF16BE", + /* These names are provided by + * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL + */ + "x-utf-16be", "ibm-1200", "ibm-1201", "ibm-5297", + "ibm-13488", "ibm-17584", "windows-1201", "cp1200", "cp1201", + "UTF16_BigEndian", + // see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html + "UnicodeBigUnmarked" + }); + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || cs instanceof ISO_8859_1 + || cs instanceof UTF_8 || cs instanceof UTF_16BE + || cs instanceof UTF_16LE || cs instanceof UTF_16; + } + + public CharsetDecoder newDecoder () + { + return new UTF_16Decoder (this, UTF_16Decoder.BIG_ENDIAN); + } + + public CharsetEncoder newEncoder () + { + return new UTF_16Encoder (this, UTF_16Encoder.BIG_ENDIAN, false); + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/UTF_16Decoder.java b/libjava/classpath/gnu/java/nio/charset/UTF_16Decoder.java new file mode 100644 index 00000000000..302c83e62bd --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UTF_16Decoder.java @@ -0,0 +1,152 @@ +/* UTF_16Decoder.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CoderResult; + +/** + * Decoder for UTF-16, UTF-15LE, and UTF-16BE. + * + * @author Jesse Rosenstock + */ +final class UTF_16Decoder extends CharsetDecoder +{ + // byte orders + static final int BIG_ENDIAN = 0; + static final int LITTLE_ENDIAN = 1; + static final int UNKNOWN_ENDIAN = 2; + + private static final char BYTE_ORDER_MARK = 0xFEFF; + private static final char REVERSED_BYTE_ORDER_MARK = 0xFFFE; + + private final int originalByteOrder; + private int byteOrder; + + UTF_16Decoder (Charset cs, int byteOrder) + { + super (cs, 0.5f, 1.0f); + this.originalByteOrder = byteOrder; + this.byteOrder = byteOrder; + } + + protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + + int inPos = in.position (); + try + { + while (in.remaining () >= 2) + { + byte b1 = in.get (); + byte b2 = in.get (); + + // handle byte order mark + if (byteOrder == UNKNOWN_ENDIAN) + { + char c = (char) (((b1 & 0xFF) << 8) | (b2 & 0xFF)); + if (c == BYTE_ORDER_MARK) + { + byteOrder = BIG_ENDIAN; + inPos += 2; + continue; + } + else if (c == REVERSED_BYTE_ORDER_MARK) + { + byteOrder = LITTLE_ENDIAN; + inPos += 2; + continue; + } + else + { + // assume big endian, do not consume bytes, + // continue with normal processing + byteOrder = BIG_ENDIAN; + } + } + + // FIXME: Change so you only do a single comparison here. + char c = byteOrder == BIG_ENDIAN ? (char) ((b1 << 8) | b2) + : (char) ((b2 << 8) | b1); + + if (0xD800 <= c && c <= 0xDFFF) + { + // c is a surrogate + + // make sure c is a high surrogate + if (c > 0xDBFF) + return CoderResult.malformedForLength (2); + if (in.remaining () < 2) + return CoderResult.UNDERFLOW; + byte b3 = in.get (); + byte b4 = in.get (); + char d = byteOrder == BIG_ENDIAN ? (char) ((b3 << 8) | b4) + : (char) ((b4 << 8) | b3); + // make sure d is a low surrogate + if (d < 0xDC00 || d > 0xDFFF) + return CoderResult.malformedForLength (2); + out.put (c); + out.put (d); + inPos += 4; + } + else + { + if (!out.hasRemaining ()) + return CoderResult.UNDERFLOW; + out.put (c); + inPos += 2; + } + } + + return CoderResult.UNDERFLOW; + } + finally + { + in.position (inPos); + } + } + + protected void implReset () + { + byteOrder = originalByteOrder; + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/UTF_16Encoder.java b/libjava/classpath/gnu/java/nio/charset/UTF_16Encoder.java new file mode 100644 index 00000000000..d5ab744e086 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UTF_16Encoder.java @@ -0,0 +1,145 @@ +/* UTF_16Encoder.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +/** + * Encoder for UTF-16, UTF-15LE, and UTF-16BE. + * + * @author Jesse Rosenstock + */ +final class UTF_16Encoder extends CharsetEncoder +{ + // byte orders + static final int BIG_ENDIAN = 0; + static final int LITTLE_ENDIAN = 1; + + private static final char BYTE_ORDER_MARK = 0xFEFF; + + private final ByteOrder byteOrder; + private final boolean useByteOrderMark; + private boolean needsByteOrderMark; + + UTF_16Encoder (Charset cs, int byteOrder, boolean useByteOrderMark) + { + super (cs, 2.0f, + useByteOrderMark ? 4.0f : 2.0f, + byteOrder == BIG_ENDIAN + ? new byte[] { (byte) 0xFF, (byte) 0xFD } + : new byte[] { (byte) 0xFD, (byte) 0xFF }); + this.byteOrder = (byteOrder == BIG_ENDIAN) ? + ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; + this.useByteOrderMark = useByteOrderMark; + this.needsByteOrderMark = useByteOrderMark; + } + + protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + + ByteOrder originalBO = out.order(); + out.order(byteOrder); + + if (needsByteOrderMark) + { + if (out.remaining () < 2) + { + out.order(originalBO); + return CoderResult.OVERFLOW; + } + out.putChar (BYTE_ORDER_MARK); + needsByteOrderMark = false; + } + + int inPos = in.position (); + try + { + while (in.hasRemaining ()) + { + char c = in.get (); + if (0xD800 <= c && c <= 0xDFFF) + { + // c is a surrogate + + // make sure c is a high surrogate + if (c > 0xDBFF) + return CoderResult.malformedForLength (1); + if (in.remaining () < 1) + return CoderResult.UNDERFLOW; + char d = in.get (); + // make sure d is a low surrogate + if (d < 0xDC00 || d > 0xDFFF) + return CoderResult.malformedForLength (1); + out.putChar (c); + out.putChar (d); + inPos += 2; + } + else + { + if (out.remaining () < 2) + { + out.order(originalBO); + return CoderResult.OVERFLOW; + } + out.putChar (c); + inPos++; + } + } + out.order(originalBO); + return CoderResult.UNDERFLOW; + } + finally + { + in.position (inPos); + } + } + + protected void implReset () + { + needsByteOrderMark = useByteOrderMark; + } + + // TODO: override canEncode(char) and canEncode(CharSequence) + // for performance +} diff --git a/libjava/classpath/gnu/java/nio/charset/UTF_16LE.java b/libjava/classpath/gnu/java/nio/charset/UTF_16LE.java new file mode 100644 index 00000000000..6d72a324cbd --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UTF_16LE.java @@ -0,0 +1,83 @@ +/* UTF_16LE.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +/** + * UTF-16LE charset. + * + * @author Jesse Rosenstock + */ +final class UTF_16LE extends Charset +{ + UTF_16LE () + { + super ("UTF-16LE", new String[] { + // witnessed by the internet + "UTF16LE", + /* These names are provided by + * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL + */ + "x-utf-16le", "ibm-1202", "ibm-13490", "ibm-17586", + "UTF16_LittleEndian", + // see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html + "UnicodeLittleUnmarked" + }); + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || cs instanceof ISO_8859_1 + || cs instanceof UTF_8 || cs instanceof UTF_16BE + || cs instanceof UTF_16LE || cs instanceof UTF_16; + } + + public CharsetDecoder newDecoder () + { + return new UTF_16Decoder (this, UTF_16Decoder.LITTLE_ENDIAN); + } + + public CharsetEncoder newEncoder () + { + return new UTF_16Encoder (this, UTF_16Encoder.LITTLE_ENDIAN, false); + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/UTF_8.java b/libjava/classpath/gnu/java/nio/charset/UTF_8.java new file mode 100644 index 00000000000..0e0730eece3 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UTF_8.java @@ -0,0 +1,311 @@ +/* UTF_8.java -- + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.nio.charset; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +/** + * UTF-8 charset. + * + * <p> UTF-8 references: + * <ul> + * <li> <a href="http://ietf.org/rfc/rfc2279.txt">RFC 2279</a> + * <li> The <a href="http://www.unicode.org/unicode/standard/standard.html"> + * Unicode standard</a> and + * <a href="http://www.unicode.org/versions/corrigendum1.html"> + * Corrigendum</a> + * </ul> + * + * @author Jesse Rosenstock + */ +final class UTF_8 extends Charset +{ + UTF_8 () + { + super ("UTF-8", new String[] { + /* These names are provided by + * http://oss.software.ibm.com/cgi-bin/icu/convexp?s=ALL + */ + "ibm-1208", "ibm-1209", "ibm-5304", "ibm-5305", + "windows-65001", "cp1208", + // see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html + "UTF8" + }); + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || cs instanceof ISO_8859_1 + || cs instanceof UTF_8 || cs instanceof UTF_16BE + || cs instanceof UTF_16LE || cs instanceof UTF_16; + } + + public CharsetDecoder newDecoder () + { + return new Decoder (this); + } + + public CharsetEncoder newEncoder () + { + return new Encoder (this); + } + + private static final class Decoder extends CharsetDecoder + { + // Package-private to avoid a trampoline constructor. + Decoder (Charset cs) + { + super (cs, 1f, 1f); + } + + protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out) + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + int inPos = in.position(); + try + { + while (in.hasRemaining ()) + { + char c; + byte b1 = in.get (); + int highNibble = ((b1 & 0xFF) >> 4) & 0xF; + switch (highNibble) + { + case 0: case 1: case 2: case 3: + case 4: case 5: case 6: case 7: + if (out.remaining () < 1) + return CoderResult.OVERFLOW; + out.put ((char) b1); + inPos++; + break; + + case 0xC: case 0xD: + byte b2; + if (in.remaining () < 1) + return CoderResult.UNDERFLOW; + if (out.remaining () < 1) + return CoderResult.OVERFLOW; + if (!isContinuation (b2 = in.get ())) + return CoderResult.malformedForLength (1); + c = (char) (((b1 & 0x1F) << 6) | (b2 & 0x3F)); + // check that we had the shortest encoding + if (c <= 0x7F) + return CoderResult.malformedForLength (2); + out.put (c); + inPos += 2; + break; + + case 0xE: + byte b3; + if (in.remaining () < 2) + return CoderResult.UNDERFLOW; + if (out.remaining () < 1) + return CoderResult.OVERFLOW; + if (!isContinuation (b2 = in.get ())) + return CoderResult.malformedForLength (1); + if (!isContinuation (b3 = in.get ())) + return CoderResult.malformedForLength (1); + c = (char) (((b1 & 0x0F) << 12) + | ((b2 & 0x3F) << 6) + | (b3 & 0x3F)); + // check that we had the shortest encoding + if (c <= 0x7FF) + return CoderResult.malformedForLength (3); + out.put (c); + inPos += 3; + break; + + case 0xF: + byte b4; + if (in.remaining () < 3) + return CoderResult.UNDERFLOW; + if((b1&0x0F) > 4) + return CoderResult.malformedForLength (4); + if (out.remaining () < 2) + return CoderResult.OVERFLOW; + if (!isContinuation (b2 = in.get ())) + return CoderResult.malformedForLength (3); + if (!isContinuation (b3 = in.get ())) + return CoderResult.malformedForLength (2); + if (!isContinuation (b4 = in.get ())) + return CoderResult.malformedForLength (1); + int n = (((b1 & 0x3) << 18) + | ((b2 & 0x3F) << 12) + | ((b3 & 0x3F) << 6) + | (b4 & 0x3F)) - 0x10000; + char c1 = (char)(0xD800 | (n & 0xFFC00)>>10); + char c2 = (char)(0xDC00 | (n & 0x003FF)); + out.put (c1); + out.put (c2); + inPos += 4; + break; + + default: + return CoderResult.malformedForLength (1); + } + } + + return CoderResult.UNDERFLOW; + } + finally + { + // In case we did a get(), then encountered an error, reset the + // position to before the error. If there was no error, this + // will benignly reset the position to the value it already has. + in.position (inPos); + } + } + + private static boolean isContinuation (byte b) + { + return (b & 0xC0) == 0x80; + } + } + + private static final class Encoder extends CharsetEncoder + { + // Package-private to avoid a trampoline constructor. + Encoder (Charset cs) + { + // According to + // http://www-106.ibm.com/developerworks/unicode/library/utfencodingforms/index.html + // On average, English takes slightly over one unit per code point. + // Most Latin-script languages take about 1.1 bytes. Greek, Russian, + // Arabic and Hebrew take about 1.7 bytes, and most others (including + // Japanese, Chinese, Korean and Hindi) take about 3 bytes. + // We assume we will be dealing with latin scripts, and use 1.1 + // for averageBytesPerChar. + super (cs, 1.1f, 4.0f); + } + + protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out) + { + int inPos = in.position(); + try + { + // TODO: Optimize this in the case in.hasArray() / out.hasArray() + while (in.hasRemaining ()) + { + int remaining = out.remaining (); + char c = in.get (); + + // UCS-4 range (hex.) UTF-8 octet sequence (binary) + // 0000 0000-0000 007F 0xxxxxxx + // 0000 0080-0000 07FF 110xxxxx 10xxxxxx + // 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx + + // Scalar Value UTF-16 byte 1 byte 2 byte 3 byte 4 + // 0000 0000 0xxx xxxx 0000 0000 0xxx xxxx 0xxx xxxx + // 0000 0yyy yyxx xxxx 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx + // zzzz yyyy yyxx xxxx zzzz yyyy yyxx xxxx 1110 zzzz 10yy yyyy 10xx xxxx + // u uuuu zzzz yyyy yyxx xxxx 1101 10ww wwzz zzyy 1111 0uuu 10uu zzzz 10yy yyyy 10xx xxxx + // + 1101 11yy yyxx xxxx + // Note: uuuuu = wwww + 1 + if (c <= 0x7F) + { + if (remaining < 1) + return CoderResult.OVERFLOW; + out.put ((byte) c); + inPos++; + } + else if (c <= 0x7FF) + { + if (remaining < 2) + return CoderResult.OVERFLOW; + out.put ((byte) (0xC0 | (c >> 6))); + out.put ((byte) (0x80 | (c & 0x3F))); + inPos++; + } + else if (0xD800 <= c && c <= 0xDFFF) + { + if (remaining < 4) + return CoderResult.OVERFLOW; + + // we got a low surrogate without a preciding high one + if (c > 0xDBFF) + return CoderResult.malformedForLength (1); + + // high surrogates + if (!in.hasRemaining ()) + return CoderResult.UNDERFLOW; + + char d = in.get (); + + // make sure d is a low surrogate + if (d < 0xDC00 || d > 0xDFFF) + return CoderResult.malformedForLength (1); + + // make the 32 bit value + // int value2 = (c - 0xD800) * 0x400 + (d - 0xDC00) + 0x10000; + int value = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000; + // assert value == value2; + out.put ((byte) (0xF0 | ((value >> 18) & 0x07))); + out.put ((byte) (0x80 | ((value >> 12) & 0x3F))); + out.put ((byte) (0x80 | ((value >> 6) & 0x3F))); + out.put ((byte) (0x80 | ((value ) & 0x3F))); + inPos += 2; + } + else + { + if (remaining < 3) + return CoderResult.OVERFLOW; + + out.put ((byte) (0xE0 | (c >> 12))); + out.put ((byte) (0x80 | ((c >> 6) & 0x3F))); + out.put ((byte) (0x80 | (c & 0x3F))); + inPos++; + } + } + + return CoderResult.UNDERFLOW; + } + finally + { + // In case we did a get(), then encountered an error, reset the + // position to before the error. If there was no error, this + // will benignly reset the position to the value it already has. + in.position (inPos); + } + } + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/UnicodeLittle.java b/libjava/classpath/gnu/java/nio/charset/UnicodeLittle.java new file mode 100644 index 00000000000..d354e04e5de --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/UnicodeLittle.java @@ -0,0 +1,74 @@ +/* UnicodeLittle.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +/** + * UTF-16 little endian with a byte-order mark + * Included for java.io completeness. + * ("UTF-16" is equal to UnicodeBig, and + * UTF-16BE/LE do not have a BOM + */ +final class UnicodeLittle extends Charset +{ + UnicodeLittle () + { + super ("UnicodeLittle", new String[] {}); + } + + public boolean contains (Charset cs) + { + return cs instanceof US_ASCII || cs instanceof ISO_8859_1 + || cs instanceof UTF_8 || cs instanceof UTF_16BE + || cs instanceof UTF_16LE || cs instanceof UTF_16; + } + + public CharsetDecoder newDecoder () + { + return new UTF_16Decoder (this, UTF_16Decoder.UNKNOWN_ENDIAN); + } + + public CharsetEncoder newEncoder () + { + return new UTF_16Encoder (this, UTF_16Encoder.LITTLE_ENDIAN, true); + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1250.java b/libjava/classpath/gnu/java/nio/charset/Windows1250.java new file mode 100644 index 00000000000..9d7ab8edd8c --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1250.java @@ -0,0 +1,103 @@ +/* Windows1250.java -- Charset for Windows-1250 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1250-Latin-1, + * aka cp1250 or Windows-1250 or whatever. + */ +public class Windows1250 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, + 0xFFFD, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179, + 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0xFFFD, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A, + 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B, + 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C, + 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, + 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, + 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, + 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, + 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, + 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, + 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, + 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9 + }; + + public Windows1250() + { + super("windows-1250", new String[] { + "Windows1250", + "ibm-5346_P100-1998", + "ibm-5346", + "cp1250", + "cp-1250", + "cp_1250", + "windows1250", + "windows_1250" + }); + lookupTable = lookup; + } + +} // class Windows1250 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1251.java b/libjava/classpath/gnu/java/nio/charset/Windows1251.java new file mode 100644 index 00000000000..bf3227e87f2 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1251.java @@ -0,0 +1,101 @@ +/* Windows1251.java -- Charset for Windows-1251 Cyrillic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1251 Cyrillic char set. + * aka cp1251 or Windows-1251 or whatever. + */ +public class Windows1251 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, + 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F, + 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + NONE, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F, + 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7, + 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407, + 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7, + 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457, + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, + 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, + 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, + 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, + 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F + }; + + public Windows1251() + { + super("windows-1251", new String[] { + "Windows1251", + "cp1251", + "cp-1251", + "cp_1251", + "windows1251", + "windows_1251" + }); + lookupTable = lookup; + } + +} // class Windows1251 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1252.java b/libjava/classpath/gnu/java/nio/charset/Windows1252.java new file mode 100644 index 00000000000..9391c87d48a --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1252.java @@ -0,0 +1,100 @@ +/* Windows1252.java -- Charset for Windows-1252 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1252-Latin-1, + * aka cp1252 or Windows-1252 or whatever. + */ +public class Windows1252 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, NONE, 0x017D, NONE, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, NONE, 0x017E, 0x0178, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, + 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, + 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, + 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, + 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF + }; + + public Windows1252() + { + super("windows-1252", new String[] { + "Windows1252", + "ibm-5348_P100-1997", + "ibm-5348", + "windows-1252", + "cp1252", + "cp-1252" + }); + lookupTable = lookup; + } + +} // class Windows1252 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1253.java b/libjava/classpath/gnu/java/nio/charset/Windows1253.java new file mode 100644 index 00000000000..02150b4afb9 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1253.java @@ -0,0 +1,101 @@ +/* Windows1253.java -- Charset for Windows-1253 Greek character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1253 Greek char set. + * aka cp1253 or Windows-1253 or whatever. + */ +public class Windows1253 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + NONE, 0x2030, NONE, 0x2039, NONE, NONE, NONE, NONE, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + NONE, 0x2122, NONE, 0x203A, NONE, NONE, NONE, NONE, + 0x00A0, 0x0385, 0x0386, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, NONE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, 0x00B6, 0x00B7, + 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F, + 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, + 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, + 0x03A0, 0x03A1, NONE, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, + 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, + 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, + 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, + 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, + 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, NONE + }; + + public Windows1253() + { + super("windows-1253", new String[] { + "Windows1253", + "cp1253", + "cp-1253", + "cp_1253", + "windows1253", + "windows_1253" + }); + lookupTable = lookup; + } + +} // class Windows1253 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1254.java b/libjava/classpath/gnu/java/nio/charset/Windows1254.java new file mode 100644 index 00000000000..7cdad3c4693 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1254.java @@ -0,0 +1,101 @@ +/* Windows1254.java -- Charset for Windows-1254 Turkish character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1254 Turkish char set. + * aka cp1254 or Windows-1254 or whatever. + */ +public class Windows1254 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, NONE, NONE, NONE, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, NONE, NONE, 0x0178, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, + 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, + 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, + 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, + 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF + }; + + public Windows1254() + { + super("windows-1254", new String[] { + "Windows1254", + "cp1254", + "cp-1254", + "cp_1254", + "windows1254", + "windows_1254" + }); + lookupTable = lookup; + } + +} // class Windows1254 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1255.java b/libjava/classpath/gnu/java/nio/charset/Windows1255.java new file mode 100644 index 00000000000..b706d193006 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1255.java @@ -0,0 +1,101 @@ +/* Windows1255.java -- Charset for Windows-1255 Character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1255 Hebrew char set. + * aka cp1255 or Windows-1255 or whatever. + */ +public class Windows1255 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x02C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, + 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x02DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, + 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7, + 0x05B8, 0x05B9, 0xFFFD, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF, + 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3, + 0x05F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, + 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, + 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF, + 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, + 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD + }; + + public Windows1255() + { + super("windows-1255", new String[] { + "Windows1255", + "cp1255", + "cp-1255", + "cp_1255", + "windows1255", + "windows_1255" + }); + lookupTable = lookup; + } + +} // class Windows1255 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1256.java b/libjava/classpath/gnu/java/nio/charset/Windows1256.java new file mode 100644 index 00000000000..6924420e1bc --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1256.java @@ -0,0 +1,101 @@ +/* Windows1256.java -- Charset for Windows-1256 Arabic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1256 Arabic char set. + * aka cp1256 or Windows-1256 or whatever. + */ +public class Windows1256 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688, + 0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA, + 0x00A0, 0x060C, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F, + 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, + 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, + 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7, + 0x0637, 0x0638, 0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643, + 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF, + 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7, + 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2 + }; + + public Windows1256() + { + super("windows-1256", new String[] { + "Windows1256", + "cp1256", + "cp-1256", + "cp_1256", + "windows1256", + "windows_1256" + }); + lookupTable = lookup; + } + +} // class Windows1256 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1257.java b/libjava/classpath/gnu/java/nio/charset/Windows1257.java new file mode 100644 index 00000000000..2f95d64ac74 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1257.java @@ -0,0 +1,101 @@ +/* Windows1257.java -- Charset for Windows-1257 Baltic character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1257 Baltic char set. + * aka cp1257 or Windows-1257 or whatever. + */ +public class Windows1257 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, 0x201A, NONE, 0x201E, 0x2026, 0x2020, 0x2021, + NONE, 0x2030, NONE, 0x2039, NONE, 0x00A8, 0x02C7, 0x00B8, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + NONE, 0x2122, NONE, 0x203A, NONE, 0x00AF, 0x02DB, NONE, + 0x00A0, NONE, 0x00A2, 0x00A3, 0x00A4, NONE, 0x00A6, 0x00A7, + 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6, + 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112, + 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B, + 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, + 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF, + 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, + 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C, + 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, + 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9 + }; + + public Windows1257() + { + super("windows-1257", new String[] { + "Windows1257", + "cp1257", + "cp-1257", + "cp_1257", + "windows1257", + "windows_1257" + }); + lookupTable = lookup; + } + +} // class Windows1257 + diff --git a/libjava/classpath/gnu/java/nio/charset/Windows1258.java b/libjava/classpath/gnu/java/nio/charset/Windows1258.java new file mode 100644 index 00000000000..7d653b49780 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/Windows1258.java @@ -0,0 +1,101 @@ +/* Windows1258.java -- Charset for Windows-1258 Vietnamese character set. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset; + +/** + * Encoding table for Windows-1258 Arabic char set. + * aka cp1258 or Windows-1258 or whatever. + */ +public class Windows1258 extends ByteCharset +{ + + /** + * This is the lookup table for this encoding + */ + private static final char[] lookup = + { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x20AC, NONE, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x02C6, 0x2030, NONE, 0x2039, 0x0152, NONE, NONE, NONE, + NONE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x02DC, 0x2122, NONE, 0x203A, 0x0153, NONE, NONE, 0x0178, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, + 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF, + 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7, + 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0301, 0x00ED, 0x00EE, 0x00EF, + 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7, + 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF + }; + + public Windows1258() + { + super("windows-1258", new String[] { + "Windows1258", + "cp1258", + "cp-1258", + "cp_1258", + "windows1258", + "windows_1258" + }); + lookupTable = lookup; + } + +} // class Windows1258 + diff --git a/libjava/classpath/gnu/java/nio/charset/iconv/IconvCharset.java b/libjava/classpath/gnu/java/nio/charset/iconv/IconvCharset.java new file mode 100644 index 00000000000..ffd2a103f98 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/iconv/IconvCharset.java @@ -0,0 +1,85 @@ +/* IconvCharset.java -- Wrapper for iconv charsets. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset.iconv; + +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +public final class IconvCharset extends Charset +{ + private IconvMetaData info; + + public IconvCharset(IconvMetaData info) + { + super(info.nioCanonical(), info.aliases()); + this.info = info; + if (newEncoder() == null || newDecoder() == null) + throw new IllegalArgumentException(); + } + + public boolean contains(Charset cs) + { + return false; + } + + public CharsetDecoder newDecoder() + { + try + { + return new IconvDecoder(this, info); + } + catch (IllegalArgumentException e) + { + return null; + } + } + + public CharsetEncoder newEncoder() + { + try + { + return new IconvEncoder(this, info); + } + catch (IllegalArgumentException e) + { + return null; + } + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/iconv/IconvDecoder.java b/libjava/classpath/gnu/java/nio/charset/iconv/IconvDecoder.java new file mode 100644 index 00000000000..fa265a92387 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/iconv/IconvDecoder.java @@ -0,0 +1,113 @@ +/* IconvDecoder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset.iconv; + +import gnu.classpath.RawData; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +final class IconvDecoder extends CharsetDecoder +{ + IconvDecoder(Charset cs, IconvMetaData info) + { + super(cs, info.averageCharsPerByte(), info.maxCharsPerByte()); + openIconv(info.iconvName()); + } + + private RawData data; + private int inremaining; + private int outremaining; + + private native void openIconv(String name); + + private native int decode(byte[] in, char[] out, int posIn, int remIn, + int posOut, int remOut); + + private native void closeIconv(); + + protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) + { + int remIn = in.remaining(); + int inPos = in.position(); + int outPos = out.position(); + int remOut = out.remaining(); + byte[] inArr; + int ret; + + if (in.hasArray()) + inArr = in.array(); + else + { + inArr = new byte[remIn]; + in.get(inArr); + } + + if (out.hasArray()) + { + ret = decode(inArr, out.array(), inPos, remIn, outPos, remOut); + out.position(outPos + (remOut - outremaining)); + } + else + { + char[] outArr = new char[remOut]; + ret = decode(inArr, outArr, inPos, remIn, outPos, remOut); + out.put(outArr, 0, (remOut - outremaining)); + } + in.position(inPos + (remIn - inremaining)); + + if (ret == 1) + return CoderResult.malformedForLength(1); + + if (in.remaining() == 0) + return CoderResult.UNDERFLOW; + return CoderResult.OVERFLOW; + } + + protected void finalize() + { + closeIconv(); + } +} + + diff --git a/libjava/classpath/gnu/java/nio/charset/iconv/IconvEncoder.java b/libjava/classpath/gnu/java/nio/charset/iconv/IconvEncoder.java new file mode 100644 index 00000000000..bb10c9b6347 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/iconv/IconvEncoder.java @@ -0,0 +1,111 @@ +/* IconvEncoder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset.iconv; + +import gnu.classpath.RawData; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; + +final class IconvEncoder extends CharsetEncoder +{ + private RawData data; + private int inremaining; + private int outremaining; + + private native void openIconv(String name); + + private native int encode(char[] in, byte[] out, int posIn, int remIn, + int posOut, int remOut); + + private native void closeIconv(); + + IconvEncoder(Charset cs, IconvMetaData info) + { + super(cs, info.averageBytesPerChar(), info.maxBytesPerChar()); + openIconv(info.iconvName()); + } + + protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) + { + int inPos = in.position(); + int outPos = out.position(); + int remIn = in.remaining(); + int remOut = out.remaining(); + char[] inArr; + int ret; + + if (in.hasArray()) + inArr = in.array(); + else + { + inArr = new char[remIn]; + in.get(inArr); + } + + if (out.hasArray()) + { + ret = encode(inArr, out.array(), inPos, remIn, outPos, remOut); + out.position(outPos + (remOut - outremaining)); + } + else + { + byte[] outArr = new byte[remOut]; + ret = encode(inArr, outArr, inPos, remIn, outPos, remOut); + out.put(outArr, 0, (remOut - outremaining)); + } + in.position(inPos + (remIn - inremaining)); + + if (ret == 1) + return CoderResult.malformedForLength(1); + + if (in.remaining() == 0) + return CoderResult.UNDERFLOW; + return CoderResult.OVERFLOW; + } + + protected void finalize() + { + closeIconv(); + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/iconv/IconvMetaData.java b/libjava/classpath/gnu/java/nio/charset/iconv/IconvMetaData.java new file mode 100644 index 00000000000..c4686a25a28 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/iconv/IconvMetaData.java @@ -0,0 +1,450 @@ +/* IconvMetaData.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset.iconv; + +import java.util.HashMap; +import java.util.Vector; + +/** + * This is ugly glue. iconv doesn't have character metadata, + * so we include it here. + * + * TODO: Add more charsets which GNU iconv and the JDK support which aren't + * included here. + * + * @author Sven de Marothy + */ +final class IconvMetaData +{ + /** + * Map of names (and aliases) to metadata instances + */ + private static HashMap names; + + /** + * Vector of MetaData instances + */ + private static Vector charsets; + + /** + * Name to use with iconv (may differ from the nio canonical. + */ + private String iconvName; + + /** + * Average number of bytes per char. + */ + private float averageBperC; + + /** + * Maximum number of bytes per char. + */ + private float maxBperC; + + /** + * Average number of chars per byte. + */ + private float averageCperB; + + /** + * Maximum number of chars per byte. + */ + private float maxCperB; + + /** + * NIO canonical name. + */ + private String nioCanonical; + + /** + * Charset aliases. + */ + private String[] aliases; + + IconvMetaData(String nioCanonical, float averageBperC, float maxBperC, + float averageCperB, float maxCperB, String[] aliases, + String iconvName) + { + this.nioCanonical = nioCanonical; + this.iconvName = iconvName; + + this.averageBperC = averageBperC; + this.maxBperC = maxBperC; + this.averageCperB = averageCperB; + this.maxCperB = maxCperB; + this.aliases = aliases; + + names.put(nioCanonical, this); + names.put(iconvName, this); + for (int i = 0; i < aliases.length; i++) + names.put(aliases[i], this); + charsets.add(this); + } + + static Vector charsets() + { + return charsets; + } + + String[] aliases() + { + return aliases; + } + + String nioCanonical() + { + return nioCanonical; + } + + String iconvName() + { + return iconvName; + } + + float maxBytesPerChar() + { + return maxBperC; + } + + float maxCharsPerByte() + { + return maxCperB; + } + + float averageBytesPerChar() + { + return averageBperC; + } + + float averageCharsPerByte() + { + return averageCperB; + } + + static IconvMetaData get(String s) + { + return (IconvMetaData) names.get(s); + } + + static void setup() + { + names = new HashMap(); + charsets = new Vector(); + new IconvMetaData("Big5", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] { "big-5", "csBig5" }, "Big5"); + + new IconvMetaData("Big5-HKSCS", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] { "big5-hkscs", "Big5_HKSCS", "big5hkscs" }, + "Big5-HKSCS"); + + new IconvMetaData("EUC-CN", 2.0f, 2.0f, 0.5f, 1.0f, new String[] { }, + "EUC-CN"); + + new IconvMetaData("EUC-JP", 3.0f, 3.0f, 0.5f, 1.0f, + new String[] + { + "eucjis", "x-eucjp", "csEUCPkdFmtjapanese", "eucjp", + "Extended_UNIX_Code_Packed_Format_for_Japanese", + "x-euc-jp", "euc_jp" + }, "EUC-JP"); + + new IconvMetaData("EUC-KR", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] + { + "ksc5601", "5601", "ksc5601_1987", "ksc_5601", + "ksc5601-1987", "euc_kr", "ks_c_5601-1987", "euckr", + "csEUCKR" + }, "EUC-KR"); + + new IconvMetaData("EUC-TW", 4.0f, 4.0f, 2.0f, 2.0f, + new String[] { "cns11643", "euc_tw", "euctw", }, "EUC-TW"); + + new IconvMetaData("GB18030", 4.0f, 4.0f, 1.0f, 2.0f, + new String[] { "gb18030-2000", }, "GB18030"); + + new IconvMetaData("GBK", 2.0f, 2.0f, 0.5f, 1.0f, new String[] { "GBK" }, + "GBK"); + + new IconvMetaData("ISO-2022-CN-CNS", 4.0f, 4.0f, 2.0f, 2.0f, + new String[] { "ISO2022CN_CNS" }, "ISO-2022-CN"); // will this work? + + new IconvMetaData("ISO-2022-CN-GB", 4.0f, 4.0f, 2.0f, 2.0f, + new String[] { "ISO2022CN_GB" }, "ISO-2022-CN"); // same here? + + new IconvMetaData("ISO-2022-KR", 4.0f, 4.0f, 1.0f, 1.0f, + new String[] { "ISO2022KR", "csISO2022KR" }, + "ISO-2022-KR"); + + new IconvMetaData("ISO-8859-1", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "iso-ir-100", "ISO_8859-1", "latin1", "l1", "IBM819", + "CP819", "csISOLatin1", "8859_1", "ISO8859_1", + "ISO_8859_1", "ibm-819", "ISO_8859-1:1987", "819" + }, "ISO-8859-1"); + + new IconvMetaData("ISO-8859-13", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_13", "8859_13", "ibm-921_P100-1995", "ibm-921", + "iso_8859_13", "iso8859_13", "iso-8859-13", "8859_13", + "cp921", "921" + }, "ISO-8859-13"); + + new IconvMetaData("ISO-8859-15", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "8859_15", "csISOlatin9", "IBM923", "cp923", "923", + "LATIN0", "csISOlatin0", "ISO8859_15_FDIS", "L9", + "IBM-923", "ISO8859-15", "LATIN9", "ISO_8859-15", + "ISO-8859-15", + }, "ISO-8859-15"); + + new IconvMetaData("ISO-8859-2", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_2", "8859_2", "ibm-912_P100-1995", "ibm-912", + "iso_8859_2", "iso8859_2", "iso-8859-2", + "ISO_8859-2:1987", "latin2", "csISOLatin2", + "iso-ir-101", "l2", "cp912", "912", "windows-28592" + }, "ISO-8859-2"); + + new IconvMetaData("ISO-8859-3", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_3", "8859_3", "ibm-913_P100-2000", "ibm-913", + "iso_8859_3", "iso8859_3", "iso-8859-3", + "ISO_8859-3:1988", "latin3", "csISOLatin3", + "iso-ir-109", "l3", "cp913", "913", "windows-28593" + }, "ISO-8859-3"); + + new IconvMetaData("ISO-8859-4", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_4", "8859_4", "ibm-914_P100-1995", "ibm-914", + "iso_8859_4", "iso8859_4", "iso-8859-4", "latin4", + "csISOLatin4", "iso-ir-110", "ISO_8859-4:1988", "l4", + "cp914", "914", "windows-28594" + }, "ISO-8859-4"); + + new IconvMetaData("ISO-8859-5", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_5", "8859_5", "ibm-915_P100-1995", "ibm-915", + "iso_8859_5", "iso8859_5", "iso-8859-5", "cyrillic", + "csISOLatinCyrillic", "iso-ir-144", "ISO_8859-5:1988", + "cp915", "915", "windows-28595" + }, "ISO-8859-5"); + + new IconvMetaData("ISO-8859-6", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "8859_6", "ibm-1089_P100-1995", "ibm-1089", + "iso_8859_6", "iso8859_6", "iso-8859-6", "arabic", + "csISOLatinArabic", "iso-ir-127", "ISO_8859-6:1987", + "ECMA-114", "ASMO-708", "8859_6", "cp1089", "1089", + "windows-28596", "ISO-8859-6-I", "ISO-8859-6-E" + }, "ISO-8859-6"); + + new IconvMetaData("ISO-8859-7", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_7", "8859_7", "ibm-813_P100-1995", "ibm-813", + "iso_8859_7", "iso8859_7", "iso-8859-7", "greek", + "greek8", "ELOT_928", "ECMA-118", "csISOLatinGreek", + "iso-ir-126", "ISO_8859-7:1987", "cp813", "813", + "windows-28597" + }, "ISO-8859-7"); + + new IconvMetaData("ISO-8859-8", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_8", "8859_8", "ibm-916_P100-1995", "ibm-916", + "iso_8859_8", "iso8859_8", "iso-8859-8", "hebrew", + "csISOLatinHebrew", "iso-ir-138", "ISO_8859-8:1988", + "ISO-8859-8-I", "ISO-8859-8-E", "cp916", "916", + "windows-28598" + }, "ISO-8859-8"); + + new IconvMetaData("ISO-8859-9", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "ISO8859_9", "8859_9", "ibm-920_P100-1995", "ibm-920", + "iso8859_9", "iso-8859-9", "iso_8859_9", "latin5", + "csISOLatin5", "iso-ir-148", "ISO_8859-9:1989", "l5", + "cp920", "920", "windows-28599", "ECMA-128" + }, "ISO-8859-9"); + + new IconvMetaData("Johab", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] { "ms1361", "ksc5601_1992", "ksc5601-1992", }, + "Johab"); + + new IconvMetaData("KOI8-R", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "KOI8_R", "KOI8", "KOI-8", "KOI_8", "koi8-r", "koi8r", + "koi-8-r", "koi" + }, "KOI8-R"); + + new IconvMetaData("Shift_JIS", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] + { + "shift-jis", "x-sjis", "ms_kanji", "shift_jis", + "csShiftJIS", "sjis", "pck", + }, "Shift_JIS"); + + new IconvMetaData("TIS-620", 1.0f, 1.0f, 1.0f, 1.0f, new String[] { }, + "TIS-620"); + + new IconvMetaData("US-ASCII", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "IBM367", "ISO646-US", "ANSI_X3.4-1986", "cp367", + "ASCII", "iso_646.irv:1983", "646", "us", "iso-ir-6", + "csASCII", "ANSI_X3.4-1968", "ISO_646.irv:1991", + }, "US-ASCII"); + + new IconvMetaData("UTF-16", 2.0f, 4.0f, 0.5f, 1.0f, + new String[] + { + "UTF_16", "UTF16", "ISO-10646-UCS-2", "unicode", + "csUnicode", "ucs-2", "UnicodeBig" + }, "UTF-16"); + + new IconvMetaData("UTF-16BE", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] + { + "X-UTF-16BE", "UTF_16BE", "UTF16BE", "ISO-10646-UCS-2", + "x-utf-16be", "ibm-1200", "ibm-1201", "ibm-5297", + "ibm-13488", "ibm-17584", "windows-1201", "cp1200", + "cp1201", "UnicodeBigUnmarked" + }, "UTF-16BE"); + + new IconvMetaData("UTF-16LE", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] + { + "UTF_16LE", "UTF16LE", "X-UTF-16LE", "ibm-1202", + "ibm-13490", "ibm-17586", "UTF16_LittleEndian", + "UnicodeLittleUnmarked" + }, "UTF-16LE"); + + new IconvMetaData("UTF-8", 1.1f, 4.0f, 1.0f, 2.0f, + new String[] + { + "UTF8", "ibm-1208", "ibm-1209", "ibm-5304", "ibm-5305", + "windows-65001", "cp1208" + }, "UTF-8"); + + new IconvMetaData("windows-1250", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1250", "ibm-5346_P100-1998", "ibm-5346", + "cp1250", "cp-1250", "cp_1250", "windows1250", + "windows_1250" + }, "windows-1250"); + + new IconvMetaData("windows-1251", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1251", "cp1251", "cp-1251", "cp_1251", + "windows1251", "windows_1251" + }, "windows-1251"); + + new IconvMetaData("windows-1252", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1252", "ibm-5348_P100-1997", "ibm-5348", + "windows-1252", "cp1252", "cp-1252" + }, "windows-1252"); + + new IconvMetaData("windows-1253", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1253", "cp1253", "cp-1253", "cp_1253", + "windows1253", "windows_1253" + }, "windows-1253"); + + new IconvMetaData("windows-1254", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1254", "cp1254", "cp-1254", "cp_1254", + "windows1254", "windows_1254" + }, "windows-1254"); + + new IconvMetaData("windows-1255", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1255", "cp1255", "cp-1255", "cp_1255", + "windows1255", "windows_1255" + }, "windows-1255"); + + new IconvMetaData("windows-1256", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1255", "cp1256", "cp-1256", "cp_1256", + "windows1256", "windows_1256" + }, "windows-1256"); + + new IconvMetaData("windows-1257", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1255", "cp1257", "cp-1257", "cp_1257", + "windows1257", "windows_1257" + }, "windows-1257"); + + new IconvMetaData("windows-1258", 1.0f, 1.0f, 1.0f, 1.0f, + new String[] + { + "Windows1255", "cp1258", "cp-1258", "cp_1258", + "windows1258", "windows_1258" + }, "windows-1258"); + + new IconvMetaData("windows-936", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] { "cp936", "ms-936", "ms936", "ms_936" }, + "windows-936"); + + new IconvMetaData("windows-949", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] { "cp949", "ms-949", "ms_949", "ms949" }, + "cp949"); + + new IconvMetaData("windows-950", 2.0f, 2.0f, 0.5f, 1.0f, + new String[] { "cp950", "ms_950", "ms-950", "ms950", }, + "cp950"); + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/iconv/IconvProvider.java b/libjava/classpath/gnu/java/nio/charset/iconv/IconvProvider.java new file mode 100644 index 00000000000..58eaa85bac4 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/iconv/IconvProvider.java @@ -0,0 +1,111 @@ +/* IconvProvider.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio.charset.iconv; + +import java.nio.charset.Charset; +import java.nio.charset.spi.CharsetProvider; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Vector; + +/** + * Charset provider wrapping iconv. + * + * @author Sven de Marothy + */ +public final class IconvProvider extends CharsetProvider +{ + private static IconvProvider singleton; + + static + { + synchronized (IconvProvider.class) + { + singleton = null; + } + } + + private IconvProvider() + { + IconvMetaData.setup(); + } + + public Iterator charsets() + { + Vector names = IconvMetaData.charsets(); + Vector charsets = new Vector(); + for (int i = 0; i < names.size(); i++) + { + try + { + charsets.add(new IconvCharset((IconvMetaData) names.elementAt(i))); + } + catch (IllegalArgumentException e) + { + } + } + return charsets.iterator(); + } + + public Charset charsetForName(String charsetName) + { + try + { + IconvMetaData info = IconvMetaData.get(charsetName); + + // Try anyway if the set isn't found. + if (info == null) + info = new IconvMetaData(charsetName, 2.0f, 2.0f, 2.0f, 2.0f, + new String[] { }, charsetName); + return new IconvCharset(info); + } + catch (IllegalArgumentException e) + { + return null; + } + } + + public static synchronized IconvProvider provider() + { + if (singleton == null) + singleton = new IconvProvider(); + return singleton; + } +} diff --git a/libjava/classpath/gnu/java/nio/charset/package.html b/libjava/classpath/gnu/java/nio/charset/package.html new file mode 100644 index 00000000000..002516da3b6 --- /dev/null +++ b/libjava/classpath/gnu/java/nio/charset/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.nio.charset package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.nio.charset</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/nio/package.html b/libjava/classpath/gnu/java/nio/package.html new file mode 100644 index 00000000000..87f0d9bf38f --- /dev/null +++ b/libjava/classpath/gnu/java/nio/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.nio package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.nio</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/rmi/RMIMarshalledObjectInputStream.java b/libjava/classpath/gnu/java/rmi/RMIMarshalledObjectInputStream.java new file mode 100644 index 00000000000..140069379bc --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/RMIMarshalledObjectInputStream.java @@ -0,0 +1,71 @@ +/* gnu.java.rmi.RMIMarshalledObjectInputStream + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi; + +import gnu.java.rmi.server.RMIObjectInputStream; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; + +/** + * This class is only for java.rmi.MarshalledObject to deserialize object from + * objBytes and locBytes + */ + +public class RMIMarshalledObjectInputStream extends RMIObjectInputStream +{ + private ObjectInputStream locStream; + + public RMIMarshalledObjectInputStream(byte[] objBytes, byte[] locBytes) throws IOException + { + super(new ByteArrayInputStream(objBytes)); + if(locBytes != null) + locStream = new ObjectInputStream(new ByteArrayInputStream(locBytes)); + } + + //This method overrides RMIObjectInputStream's + protected Object getAnnotation() throws IOException, ClassNotFoundException + { + if(locStream == null) + return null; + return locStream.readObject(); + } + +} // End of RMIMarshalledObjectInputStream diff --git a/libjava/classpath/gnu/java/rmi/RMIMarshalledObjectOutputStream.java b/libjava/classpath/gnu/java/rmi/RMIMarshalledObjectOutputStream.java new file mode 100644 index 00000000000..2bbbaacb45f --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/RMIMarshalledObjectOutputStream.java @@ -0,0 +1,79 @@ +/* gnu.java.rmi.RMIMarshalledObjectOutputStream + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi; + +import gnu.java.rmi.server.RMIObjectOutputStream; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; + +/** + * This class is only for java.rmi.MarshalledObject to serialize object and + * got objBytes and locBytes + */ +public class RMIMarshalledObjectOutputStream extends RMIObjectOutputStream +{ + private ObjectOutputStream locStream; + private ByteArrayOutputStream locBytesStream; + + public RMIMarshalledObjectOutputStream(OutputStream objStream) throws IOException + { + super(objStream); + locBytesStream = new ByteArrayOutputStream(256); + locStream = new ObjectOutputStream(locBytesStream); + } + + //This method overrides RMIObjectOutputStream's. + protected void setAnnotation(String annotation) throws IOException{ + locStream.writeObject(annotation); + } + + public void flush() throws IOException { + super.flush(); + locStream.flush(); + } + + public byte[] getLocBytes(){ + return locBytesStream.toByteArray(); + } + +} // End of RMIMarshalledObjectOutputStream + diff --git a/libjava/classpath/gnu/java/rmi/dgc/DGCImpl.java b/libjava/classpath/gnu/java/rmi/dgc/DGCImpl.java new file mode 100644 index 00000000000..a32445c343b --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/dgc/DGCImpl.java @@ -0,0 +1,118 @@ +/* DGCImpl.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.dgc; + +import gnu.java.rmi.server.UnicastServerRef; + +import java.rmi.RemoteException; +import java.rmi.dgc.DGC; +import java.rmi.dgc.Lease; +import java.rmi.dgc.VMID; +import java.rmi.server.ObjID; +import java.rmi.server.RMISocketFactory; +import java.util.Hashtable; + +/** + * I let DGCImpl to extend UnicastServerRef, but not + * UnicastRemoteObject, because UnicastRemoteObject must + * exportObject automatically. + */ +public class DGCImpl + extends UnicastServerRef implements DGC { + + private static final long LEASE_VALUE = 600000L; + // leaseCache caches a LeaseRecord associated with a vmid + private Hashtable leaseCache = new Hashtable(); + +public DGCImpl() throws RemoteException { + super(new ObjID(ObjID.DGC_ID), 0, RMISocketFactory.getSocketFactory()); +} + +public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease) throws RemoteException { + VMID vmid = lease.getVMID(); + if (vmid == null) + vmid = new VMID(); + long leaseValue = LEASE_VALUE; + //long leaseValue = lease.getValue(); + lease = new Lease(vmid, leaseValue); + synchronized(leaseCache){ + LeaseRecord lr = (LeaseRecord)leaseCache.get(vmid); + if (lr != null) + lr.reset(leaseValue); + else{ + lr = new LeaseRecord(vmid, leaseValue); + leaseCache.put(vmid, lr); + } + } + + return (lease); +} + +public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong) throws RemoteException { + // Not implemented +} + + /** + * LeaseRecord associates a vmid to expireTime. + */ + private static class LeaseRecord{ + private VMID vmid; + private long expireTime; + + LeaseRecord(VMID vmid, long leaseValue){ + this.vmid = vmid; + reset(leaseValue); + } + + // reset expireTime + void reset(long leaseValue){ + long l = System.currentTimeMillis(); + expireTime = l + leaseValue; + } + + boolean isExpired(){ + long l = System.currentTimeMillis(); + if ( l > expireTime) + return true; + return false; + } + + } //End of LeaseRecord + +} //End of DGCImpl diff --git a/libjava/classpath/gnu/java/rmi/dgc/DGCImpl_Skel.java b/libjava/classpath/gnu/java/rmi/dgc/DGCImpl_Skel.java new file mode 100644 index 00000000000..5f032e72f8a --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/dgc/DGCImpl_Skel.java @@ -0,0 +1,144 @@ +/* DGCImpl_Skel.java + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +// Skel class generated by rmic - DO NOT EDIT! + +package gnu.java.rmi.dgc; + +public final class DGCImpl_Skel + implements java.rmi.server.Skeleton +{ + private static final long interfaceHash = -669196253586618813L; + + private static final java.rmi.server.Operation[] operations = { + new java.rmi.server.Operation("void clean(java.rmi.server.ObjID[], long, java.rmi.dgc.VMID, boolean"), + new java.rmi.server.Operation("java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease") + }; + + public java.rmi.server.Operation[] getOperations() { + return ((java.rmi.server.Operation[]) operations.clone()); + } + + public void dispatch(java.rmi.Remote obj, java.rmi.server.RemoteCall call, int opnum, long hash) throws java.lang.Exception { + if (opnum < 0) { + if (hash == -5803803475088455571L) { + opnum = 0; + } + else if (hash == -8139341527526761862L) { + opnum = 1; + } + else { + throw new java.rmi.server.SkeletonMismatchException("interface hash mismatch"); + } + } + else if (hash != interfaceHash) { + throw new java.rmi.server.SkeletonMismatchException("interface hash mismatch"); + } + + gnu.java.rmi.dgc.DGCImpl server = (gnu.java.rmi.dgc.DGCImpl)obj; + switch (opnum) { + case 0: + { + java.rmi.server.ObjID[] $param_0; + long $param_1; + java.rmi.dgc.VMID $param_2; + boolean $param_3; + try { + java.io.ObjectInput in = call.getInputStream(); + $param_0 = (java.rmi.server.ObjID[])in.readObject(); + $param_1 = (long)in.readLong(); + $param_2 = (java.rmi.dgc.VMID)in.readObject(); + $param_3 = (boolean)in.readBoolean(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + catch (java.lang.ClassCastException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + server.clean($param_0, $param_1, $param_2, $param_3); + try { + java.io.ObjectOutput out = call.getResultStream(true); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + case 1: + { + java.rmi.server.ObjID[] $param_0; + long $param_1; + java.rmi.dgc.Lease $param_2; + try { + java.io.ObjectInput in = call.getInputStream(); + $param_0 = (java.rmi.server.ObjID[])in.readObject(); + $param_1 = (long)in.readLong(); + $param_2 = (java.rmi.dgc.Lease)in.readObject(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + catch (java.lang.ClassCastException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + java.rmi.dgc.Lease $result = server.dirty($param_0, $param_1, $param_2); + try { + java.io.ObjectOutput out = call.getResultStream(true); + out.writeObject($result); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + default: + throw new java.rmi.UnmarshalException("invalid method number"); + } + } +} diff --git a/libjava/classpath/gnu/java/rmi/dgc/DGCImpl_Stub.java b/libjava/classpath/gnu/java/rmi/dgc/DGCImpl_Stub.java new file mode 100644 index 00000000000..b1e086a73b2 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/dgc/DGCImpl_Stub.java @@ -0,0 +1,158 @@ +/* DGCImpl_Stub.java + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +// Stub class generated by rmic - DO NOT EDIT! + +package gnu.java.rmi.dgc; + +public final class DGCImpl_Stub + extends java.rmi.server.RemoteStub + implements java.rmi.dgc.DGC +{ + private static final long serialVersionUID = 2L; + + private static final long interfaceHash = -669196253586618813L; + + private static boolean useNewInvoke; + + private static final java.rmi.server.Operation[] operations = { + new java.rmi.server.Operation("void clean(java.rmi.server.ObjID[], long, java.rmi.dgc.VMID, boolean)"), + new java.rmi.server.Operation("java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)") + }; + + private static java.lang.reflect.Method $method_clean_0; + private static java.lang.reflect.Method $method_dirty_1; + + static { + try { + java.rmi.server.RemoteRef.class.getMethod("invoke", new java.lang.Class[] { java.rmi.Remote.class, java.lang.reflect.Method.class, java.lang.Object[].class, long.class }); + useNewInvoke = true; + $method_clean_0 = gnu.java.rmi.dgc.DGCImpl.class.getMethod("clean", new java.lang.Class[] {java.rmi.server.ObjID[].class, long.class, java.rmi.dgc.VMID.class, boolean.class}); + $method_dirty_1 = gnu.java.rmi.dgc.DGCImpl.class.getMethod("dirty", new java.lang.Class[] {java.rmi.server.ObjID[].class, long.class, java.rmi.dgc.Lease.class}); + + } + catch (java.lang.NoSuchMethodException e) { + useNewInvoke = false; + } + } + + public DGCImpl_Stub() { + super(); + } + public DGCImpl_Stub(java.rmi.server.RemoteRef ref) { + super(ref); + } + + public void clean(java.rmi.server.ObjID[] $param_0, long $param_1, java.rmi.dgc.VMID $param_2, boolean $param_3) throws java.rmi.RemoteException { + try { + if (useNewInvoke) { + ref.invoke(this, $method_clean_0, new java.lang.Object[] {$param_0, new java.lang.Long($param_1), $param_2, new java.lang.Boolean($param_3)}, -5803803475088455571L); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 0, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + out.writeObject($param_0); + out.writeLong($param_1); + out.writeObject($param_2); + out.writeBoolean($param_3); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + try { + java.io.ObjectInput in = call.getInputStream(); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + public java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[] $param_0, long $param_1, java.rmi.dgc.Lease $param_2) throws java.rmi.RemoteException { + try { + if (useNewInvoke) { + java.lang.Object $result = ref.invoke(this, $method_dirty_1, new java.lang.Object[] {$param_0, new java.lang.Long($param_1), $param_2}, -8139341527526761862L); + return ((java.rmi.dgc.Lease)$result); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 1, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + out.writeObject($param_0); + out.writeLong($param_1); + out.writeObject($param_2); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + java.rmi.dgc.Lease $result; + try { + java.io.ObjectInput in = call.getInputStream(); + $result = (java.rmi.dgc.Lease)in.readObject(); + return ($result); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + +} diff --git a/libjava/classpath/gnu/java/rmi/dgc/package.html b/libjava/classpath/gnu/java/rmi/dgc/package.html new file mode 100644 index 00000000000..3ed81cfdddb --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/dgc/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.rmi.dgc package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.rmi.dgc</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/rmi/package.html b/libjava/classpath/gnu/java/rmi/package.html new file mode 100644 index 00000000000..53b5f5e5746 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.rmi package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.rmi</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/rmi/registry/RegistryImpl.java b/libjava/classpath/gnu/java/rmi/registry/RegistryImpl.java new file mode 100644 index 00000000000..0c94434d806 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/registry/RegistryImpl.java @@ -0,0 +1,154 @@ +/* RegistryImpl.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.registry; + +import gnu.java.rmi.server.UnicastServerRef; + +import java.rmi.AccessException; +import java.rmi.AlreadyBoundException; +import java.rmi.NotBoundException; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.ObjID; +import java.rmi.server.RMIClientSocketFactory; +import java.rmi.server.RMIServerSocketFactory; +import java.rmi.server.RMISocketFactory; +import java.rmi.server.UnicastRemoteObject; +import java.util.Enumeration; +import java.util.Hashtable; + +public class RegistryImpl + extends UnicastRemoteObject implements Registry { + +private Hashtable bindings = new Hashtable(); + +public RegistryImpl(int port) throws RemoteException { + this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory()); +} + +public RegistryImpl(int port, RMIClientSocketFactory cf, RMIServerSocketFactory sf) throws RemoteException { + super(new UnicastServerRef(new ObjID(ObjID.REGISTRY_ID), port, sf)); + // The following is unnecessary, because UnicastRemoteObject export itself automatically. + //((UnicastServerRef)getRef()).exportObject(this); +} + +public Remote lookup(String name) throws RemoteException, NotBoundException, AccessException { + Object obj = bindings.get(name); + if (obj == null) { + throw new NotBoundException(name); + } + return ((Remote)obj); +} + +public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException { + if (bindings.containsKey(name)) { + throw new AlreadyBoundException(name); + } + bindings.put(name, obj); +} + +public void unbind(String name) throws RemoteException, NotBoundException, AccessException { + Object obj = bindings.remove(name); + if (obj == null) { + throw new NotBoundException(name); + } +} + +public void rebind(String name, Remote obj) throws RemoteException, AccessException { + bindings.put(name, obj); +} + +public String[] list() throws RemoteException, AccessException { + int size = bindings.size(); + String[] strings = new String[size]; + Enumeration e = bindings.keys(); + for (int i = 0; i < size; i++) { + strings[i] = (String)e.nextElement(); + } + return (strings); +} + +public static void version() { + System.out.println("rmiregistry (" + + System.getProperty("java.vm.name") + + ") " + + System.getProperty("java.vm.version")); + System.out.println("Copyright 2005 Free Software Foundation, Inc."); + System.out.println("This is free software; see the source for copying conditions. There is NO"); + System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."); + System.exit(0); +} + +public static void help() { + System.out.println( +"Usage: rmiregistry [OPTION | PORT]\n" + +"\n" + +" --help Print this help, then exit\n" + +" --version Print version number, then exit\n"); + System.exit(0); +} + +public static void main(String[] args) { + int port = Registry.REGISTRY_PORT; + if (args.length > 0) { + if (args[0].equals("--version")) { + version(); + } + else if (args[0].equals("--help")) { + help(); + } + try { + port = Integer.parseInt(args[0]); + } + catch (NumberFormatException _) { + System.err.println("Bad port number - using default"); + } + } + + try { + Registry impl = LocateRegistry.createRegistry(port); + } + catch (RemoteException _) { + System.err.println("Registry failed"); + } +} + +} diff --git a/libjava/classpath/gnu/java/rmi/registry/RegistryImpl_Skel.java b/libjava/classpath/gnu/java/rmi/registry/RegistryImpl_Skel.java new file mode 100644 index 00000000000..8cf14ccc0c4 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/registry/RegistryImpl_Skel.java @@ -0,0 +1,227 @@ +/* RegistryImpl_Skel.java + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +// Skel class generated by rmic - DO NOT EDIT! + +package gnu.java.rmi.registry; + +public final class RegistryImpl_Skel + implements java.rmi.server.Skeleton +{ + private static final long interfaceHash = 4905912898345647071L; + + private static final java.rmi.server.Operation[] operations = { + new java.rmi.server.Operation("void bind(java.lang.String, java.rmi.Remote"), + new java.rmi.server.Operation("java.lang.String[] list("), + new java.rmi.server.Operation("java.rmi.Remote lookup(java.lang.String"), + new java.rmi.server.Operation("void rebind(java.lang.String, java.rmi.Remote"), + new java.rmi.server.Operation("void unbind(java.lang.String") + }; + + public java.rmi.server.Operation[] getOperations() { + return ((java.rmi.server.Operation[]) operations.clone()); + } + + public void dispatch(java.rmi.Remote obj, java.rmi.server.RemoteCall call, int opnum, long hash) throws java.lang.Exception { + if (opnum < 0) { + if (hash == 7583982177005850366L) { + opnum = 0; + } + else if (hash == 2571371476350237748L) { + opnum = 1; + } + else if (hash == -7538657168040752697L) { + opnum = 2; + } + else if (hash == -8381844669958460146L) { + opnum = 3; + } + else if (hash == 7305022919901907578L) { + opnum = 4; + } + else { + throw new java.rmi.server.SkeletonMismatchException("interface hash mismatch"); + } + } + else if (hash != interfaceHash) { + throw new java.rmi.server.SkeletonMismatchException("interface hash mismatch"); + } + + gnu.java.rmi.registry.RegistryImpl server = (gnu.java.rmi.registry.RegistryImpl)obj; + switch (opnum) { + case 0: + { + java.lang.String $param_0; + java.rmi.Remote $param_1; + try { + java.io.ObjectInput in = call.getInputStream(); + $param_0 = (java.lang.String)in.readObject(); + $param_1 = (java.rmi.Remote)in.readObject(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + catch (java.lang.ClassCastException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + server.bind($param_0, $param_1); + try { + java.io.ObjectOutput out = call.getResultStream(true); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + case 1: + { + try { + java.io.ObjectInput in = call.getInputStream(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + java.lang.String[] $result = server.list(); + try { + java.io.ObjectOutput out = call.getResultStream(true); + out.writeObject($result); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + case 2: + { + java.lang.String $param_0; + try { + java.io.ObjectInput in = call.getInputStream(); + $param_0 = (java.lang.String)in.readObject(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + catch (java.lang.ClassCastException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + java.rmi.Remote $result = server.lookup($param_0); + try { + java.io.ObjectOutput out = call.getResultStream(true); + out.writeObject($result); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + case 3: + { + java.lang.String $param_0; + java.rmi.Remote $param_1; + try { + java.io.ObjectInput in = call.getInputStream(); + $param_0 = (java.lang.String)in.readObject(); + $param_1 = (java.rmi.Remote)in.readObject(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + catch (java.lang.ClassCastException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + server.rebind($param_0, $param_1); + try { + java.io.ObjectOutput out = call.getResultStream(true); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + case 4: + { + java.lang.String $param_0; + try { + java.io.ObjectInput in = call.getInputStream(); + $param_0 = (java.lang.String)in.readObject(); + + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + catch (java.lang.ClassCastException e) { + throw new java.rmi.UnmarshalException("error unmarshalling arguments", e); + } + finally { + call.releaseInputStream(); + } + server.unbind($param_0); + try { + java.io.ObjectOutput out = call.getResultStream(true); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling return", e); + } + break; + } + + default: + throw new java.rmi.UnmarshalException("invalid method number"); + } + } +} diff --git a/libjava/classpath/gnu/java/rmi/registry/RegistryImpl_Stub.java b/libjava/classpath/gnu/java/rmi/registry/RegistryImpl_Stub.java new file mode 100644 index 00000000000..b289426b4fc --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/registry/RegistryImpl_Stub.java @@ -0,0 +1,293 @@ +/* RegistryImpl_Stub.java + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +// Stub class generated by rmic - DO NOT EDIT! + +package gnu.java.rmi.registry; + +public final class RegistryImpl_Stub + extends java.rmi.server.RemoteStub + implements java.rmi.registry.Registry +{ + private static final long serialVersionUID = 2L; + + private static final long interfaceHash = 4905912898345647071L; + + private static boolean useNewInvoke; + + private static final java.rmi.server.Operation[] operations = { + new java.rmi.server.Operation("void bind(java.lang.String, java.rmi.Remote)"), + new java.rmi.server.Operation("java.lang.String[] list()"), + new java.rmi.server.Operation("java.rmi.Remote lookup(java.lang.String)"), + new java.rmi.server.Operation("void rebind(java.lang.String, java.rmi.Remote)"), + new java.rmi.server.Operation("void unbind(java.lang.String)") + }; + + private static java.lang.reflect.Method $method_bind_0; + private static java.lang.reflect.Method $method_list_1; + private static java.lang.reflect.Method $method_lookup_2; + private static java.lang.reflect.Method $method_rebind_3; + private static java.lang.reflect.Method $method_unbind_4; + + static { + try { + java.rmi.server.RemoteRef.class.getMethod("invoke", new java.lang.Class[] { java.rmi.Remote.class, java.lang.reflect.Method.class, java.lang.Object[].class, long.class }); + useNewInvoke = false; + $method_bind_0 = gnu.java.rmi.registry.RegistryImpl.class.getMethod("bind", new java.lang.Class[] {java.lang.String.class, java.rmi.Remote.class}); + $method_list_1 = gnu.java.rmi.registry.RegistryImpl.class.getMethod("list", new java.lang.Class[] {}); + $method_lookup_2 = gnu.java.rmi.registry.RegistryImpl.class.getMethod("lookup", new java.lang.Class[] {java.lang.String.class}); + $method_rebind_3 = gnu.java.rmi.registry.RegistryImpl.class.getMethod("rebind", new java.lang.Class[] {java.lang.String.class, java.rmi.Remote.class}); + $method_unbind_4 = gnu.java.rmi.registry.RegistryImpl.class.getMethod("unbind", new java.lang.Class[] {java.lang.String.class}); + + } + catch (java.lang.NoSuchMethodException e) { + useNewInvoke = false; + } + } + + public RegistryImpl_Stub() { + super(); + } + public RegistryImpl_Stub(java.rmi.server.RemoteRef ref) { + super(ref); + } + + public void bind(java.lang.String $param_0, java.rmi.Remote $param_1) throws java.rmi.AccessException, java.rmi.AlreadyBoundException, java.rmi.RemoteException { + try { + if (useNewInvoke) { + ref.invoke(this, $method_bind_0, new java.lang.Object[] {$param_0, $param_1}, 7583982177005850366L); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 0, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + out.writeObject($param_0); + out.writeObject($param_1); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + try { + java.io.ObjectInput in = call.getInputStream(); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.AccessException e) { + throw e; + } + catch (java.rmi.AlreadyBoundException e) { + throw e; + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + public java.lang.String[] list() throws java.rmi.AccessException, java.rmi.RemoteException { + try { + if (useNewInvoke) { + java.lang.Object $result = ref.invoke(this, $method_list_1, null, 2571371476350237748L); + return ((java.lang.String[])$result); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 1, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + java.lang.String[] $result; + try { + java.io.ObjectInput in = call.getInputStream(); + $result = (java.lang.String[])in.readObject(); + return ($result); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.AccessException e) { + throw e; + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + public java.rmi.Remote lookup(java.lang.String $param_0) throws java.rmi.AccessException, java.rmi.NotBoundException, java.rmi.RemoteException { + try { + if (useNewInvoke) { + java.lang.Object $result = ref.invoke(this, $method_lookup_2, new java.lang.Object[] {$param_0}, -7538657168040752697L); + return ((java.rmi.Remote)$result); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 2, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + out.writeObject($param_0); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + java.rmi.Remote $result; + try { + java.io.ObjectInput in = call.getInputStream(); + $result = (java.rmi.Remote)in.readObject(); + return ($result); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.AccessException e) { + throw e; + } + catch (java.rmi.NotBoundException e) { + throw e; + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + public void rebind(java.lang.String $param_0, java.rmi.Remote $param_1) throws java.rmi.AccessException, java.rmi.RemoteException { + try { + if (useNewInvoke) { + ref.invoke(this, $method_rebind_3, new java.lang.Object[] {$param_0, $param_1}, -8381844669958460146L); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 3, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + out.writeObject($param_0); + out.writeObject($param_1); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + try { + java.io.ObjectInput in = call.getInputStream(); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.AccessException e) { + throw e; + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + + public void unbind(java.lang.String $param_0) throws java.rmi.AccessException, java.rmi.NotBoundException, java.rmi.RemoteException { + try { + if (useNewInvoke) { + ref.invoke(this, $method_unbind_4, new java.lang.Object[] {$param_0}, 7305022919901907578L); + } + else { + java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject)this, operations, 4, interfaceHash); + try { + java.io.ObjectOutput out = call.getOutputStream(); + out.writeObject($param_0); + } + catch (java.io.IOException e) { + throw new java.rmi.MarshalException("error marshalling arguments", e); + } + ref.invoke(call); + try { + java.io.ObjectInput in = call.getInputStream(); + } + catch (java.io.IOException e) { + throw new java.rmi.UnmarshalException("error unmarshalling return", e); + } + finally { + ref.done(call); + } + } + } + catch (java.rmi.AccessException e) { + throw e; + } + catch (java.rmi.NotBoundException e) { + throw e; + } + catch (java.rmi.RemoteException e) { + throw e; + } + catch (java.lang.Exception e) { + throw new java.rmi.UnexpectedException("undeclared checked exception", e); + } + } + +} diff --git a/libjava/classpath/gnu/java/rmi/registry/package.html b/libjava/classpath/gnu/java/rmi/registry/package.html new file mode 100644 index 00000000000..3750359d6af --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/registry/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.rmi.registry package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.rmi.registry</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/rmi/server/ConnectionRunnerPool.java b/libjava/classpath/gnu/java/rmi/server/ConnectionRunnerPool.java new file mode 100644 index 00000000000..9cc57b2289f --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/ConnectionRunnerPool.java @@ -0,0 +1,156 @@ +/* gnu.java.rmi.server.ConnectionRunnerPool + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.util.ArrayList; +import java.util.Arrays; + +//Should I generalize this class? + +class ConnectionRunnerPool +{ + + public static + class ConnectionRunner extends Thread{ + private UnicastConnection conn; + private volatile boolean exiting = false; + + public ConnectionRunner(ThreadGroup group, String id){ + super(group, id); + } + + public synchronized void run(){ + while(!exiting){ + if(conn == null) + try{ + wait(); + }catch(InterruptedException e){ + continue; + } + else{ + conn.run(); + conn = null; + synchronized(ConnectionRunnerPool.class){ + freelist.add(this); + if(freelist.size() == 1) + ConnectionRunnerPool.class.notifyAll(); + } + } + } + } + + public synchronized void dispatch(UnicastConnection conn){ + this.conn = conn; + notify(); + } + + void exit(){ + exiting = true; + if(conn != null) + try{ + join(500); + }catch(InterruptedException e){} + interrupt(); + } + + } + + // Should this value equal to number of CPU? + private static int size = 5; + private static int max_size = 10; + + // Package-private to avoid a trampoline. + static ArrayList freelist; + + private static ThreadGroup group = new ThreadGroup("pool"); + + static { + ConnectionRunner[] pools = new ConnectionRunner[size]; + for(int i = 0; i < pools.length; i++){ + pools[i] = new ConnectionRunner(group, new Integer(i).toString()); + pools[i].setContextClassLoader(Thread.currentThread().getContextClassLoader()); + pools[i].start(); + } + freelist = new ArrayList(Arrays.asList(pools)); + } + + public static void setSize(int size_){ + size = size_; + } + + public static void setMaxSize(int size){ + max_size = size; + } + + private static synchronized ConnectionRunner getConnectionRunner() + { + if(freelist.size() == 0){ + if(size < max_size){ + ++size; + ConnectionRunner a = new ConnectionRunner(group, new Integer(size).toString()); + a.start(); + freelist.add(a); + }else + while(freelist.size() == 0) + try{ + ConnectionRunnerPool.class.wait(); + }catch(InterruptedException e){} + } + + // always let the first in pool most busy or other scheduling plan?? + ConnectionRunner a = (ConnectionRunner)freelist.get(0); + freelist.remove(a); + return a; + } + + public static void dispatchConnection(UnicastConnection conn) + { + ConnectionRunner r = getConnectionRunner(); + r.dispatch(conn); + } + + public static void exit() + { + Thread[] list = new Thread[group.activeCount()]; + group.enumerate(list); + for(int i = 0; i < list.length; i++) + ((ConnectionRunner)list[i]).exit(); + } + +} diff --git a/libjava/classpath/gnu/java/rmi/server/ProtocolConstants.java b/libjava/classpath/gnu/java/rmi/server/ProtocolConstants.java new file mode 100644 index 00000000000..30fb5e89b39 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/ProtocolConstants.java @@ -0,0 +1,62 @@ +/* ProtocolConstants.java -- + Copyright (c) 1996, 1997, 1998, 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.server; + +public interface ProtocolConstants +{ + int PROTOCOL_HEADER = 0x4a524d49; // JRMI + int PROTOCOL_VERSION = 2; + + int STREAM_PROTOCOL = 0x4b; + int SINGLE_OP_PROTOCOL = 0x4c; + int MULTIPLEX_PROTOCOL = 0x4d; + + int PROTOCOL_ACK = 0x4e; + int PROTOCOL_NACK = 0x4f; + + int MESSAGE_CALL = 0x50; + int MESSAGE_CALL_ACK = 0x51; + int MESSAGE_PING = 0x52; + int MESSAGE_PING_ACK = 0x53; + int MESSAGE_DGCACK = 0x54; + + int RETURN_ACK = 0x01; + int RETURN_NACK = 0x02; + + int DEFAULT_PROTOCOL = STREAM_PROTOCOL; +} diff --git a/libjava/classpath/gnu/java/rmi/server/RMIDefaultSocketFactory.java b/libjava/classpath/gnu/java/rmi/server/RMIDefaultSocketFactory.java new file mode 100644 index 00000000000..628e1cc8799 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/RMIDefaultSocketFactory.java @@ -0,0 +1,59 @@ +/* RMIDefaultSocketFactory.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.server; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.rmi.server.RMISocketFactory; + +public class RMIDefaultSocketFactory + extends RMISocketFactory { + +public RMIDefaultSocketFactory() { +} + +public Socket createSocket(String host, int port) throws IOException { + return (new Socket(host, port)); +} + +public ServerSocket createServerSocket(int port) throws IOException { + return (new ServerSocket(port)); +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/RMIHashes.java b/libjava/classpath/gnu/java/rmi/server/RMIHashes.java new file mode 100644 index 00000000000..5a414404d4f --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/RMIHashes.java @@ -0,0 +1,98 @@ +/* RMIHashes.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import gnu.java.lang.reflect.TypeSignature; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.lang.reflect.Method; +import java.security.MessageDigest; + +public class RMIHashes +{ + //There're other places using DigestOutputStream to generate hash in classpath, but I think the way I used + //here is more efficient, anyway, you can switch to DigestOutputStream by doing like "//or:" comments say. + + //or:add this statement: private static final NullOutputStream nullOutputStream = new NullOutputStream (); + public static long getMethodHash(Method meth) + { + //Object Serialization Spec 8.3 + try + { + MessageDigest md = MessageDigest.getInstance ("SHA"); + //or:remove this statement: DigestOutputStream digest_out = new DigestOutputStream (nullOutputStream, md); + ByteArrayOutputStream digest_out = new ByteArrayOutputStream(); + DataOutputStream data_out = new DataOutputStream (digest_out); + + StringBuffer sbuf = new StringBuffer(); + sbuf.append(meth.getName()); + sbuf.append('('); + Class params[] = meth.getParameterTypes(); + for(int i = 0; i < params.length; i++) + sbuf.append(TypeSignature.getEncodingOfClass(params[i])); + sbuf.append(')'); + Class rcls = meth.getReturnType(); + if(rcls != Void.TYPE) + sbuf.append(TypeSignature.getEncodingOfClass(rcls)); + else + sbuf.append('V'); + + data_out.writeUTF (sbuf.toString()); + data_out.flush(); + data_out.close (); + + md.update(digest_out.toByteArray()); //or:remove this statement + byte[] sha = md.digest (); + long result = 0; + int len = sha.length < 8 ? sha.length : 8; + for (int i=0; i < len; i++) + result += (long)(sha[i] & 0xFF) << (8 * i); + return result; + }catch(Exception _){ + return -1L; + } + } + + public static long getInterfaceHash(Class clazz) + { + return clazz.hashCode (); + } +} + diff --git a/libjava/classpath/gnu/java/rmi/server/RMIIncomingThread.java b/libjava/classpath/gnu/java/rmi/server/RMIIncomingThread.java new file mode 100644 index 00000000000..990d37bc521 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/RMIIncomingThread.java @@ -0,0 +1,58 @@ +/* RMIIncomingThread.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.server; + +public class RMIIncomingThread extends Thread { + + private String clientHost = null; + + public RMIIncomingThread(Runnable runnable, String s_clientHost) { + super(runnable); + clientHost = s_clientHost; + } + + public String toString() { + return "RMIIncoming from " + clientHost + " " + super.toString(); + } + + public String getClientHost() { + return clientHost; + } + + +} diff --git a/libjava/classpath/gnu/java/rmi/server/RMIObjectInputStream.java b/libjava/classpath/gnu/java/rmi/server/RMIObjectInputStream.java new file mode 100644 index 00000000000..888b30bf6b6 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/RMIObjectInputStream.java @@ -0,0 +1,126 @@ +/* RMIObjectInputStream.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamClass; +import java.lang.reflect.Proxy; +import java.net.MalformedURLException; +import java.rmi.server.RMIClassLoader; + +public class RMIObjectInputStream + extends ObjectInputStream { + +public RMIObjectInputStream(InputStream strm) throws IOException { + super(strm); + enableResolveObject(true); +} + +protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { + String annotation = (String)getAnnotation(); + + try { + if(annotation == null) + return (RMIClassLoader.loadClass(desc.getName())); + else + return (RMIClassLoader.loadClass(annotation, desc.getName())); + } + catch (MalformedURLException _) { + throw new ClassNotFoundException(desc.getName()); + } +} + +//Separate it for override by MarshalledObject +protected Object getAnnotation() + throws IOException, ClassNotFoundException +{ + return readObject(); +} + +protected Class resolveProxyClass(String intfs[]) + throws IOException, ClassNotFoundException +{ + String annotation = (String)getAnnotation(); + + Class clss[] = new Class[intfs.length]; + if(annotation == null) + clss[0] = RMIClassLoader.loadClass(intfs[0]); + else + clss[0] = RMIClassLoader.loadClass(annotation, intfs[0]); + + //assume all interfaces can be loaded by the same classloader + ClassLoader loader = clss[0].getClassLoader(); + for (int i = 0; i < intfs.length; i++) + clss[i] = Class.forName(intfs[i], false, loader); + + try { + return Proxy.getProxyClass(loader, clss); + } catch (IllegalArgumentException e) { + throw new ClassNotFoundException(null, e); + } +} + +protected Object readValue(Class valueClass) throws IOException, ClassNotFoundException { + if(valueClass.isPrimitive()){ + if(valueClass == Boolean.TYPE) + return new Boolean(readBoolean()); + if(valueClass == Byte.TYPE) + return new Byte(readByte()); + if(valueClass == Character.TYPE) + return new Character(readChar()); + if(valueClass == Short.TYPE) + return new Short(readShort()); + if(valueClass == Integer.TYPE) + return new Integer(readInt()); + if(valueClass == Long.TYPE) + return new Long(readLong()); + if(valueClass == Float.TYPE) + return new Float(readFloat()); + if(valueClass == Double.TYPE) + return new Double(readDouble()); + else + throw new Error("Unsupported primitive class: " + valueClass); + } else + return readObject(); +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/RMIObjectOutputStream.java b/libjava/classpath/gnu/java/rmi/server/RMIObjectOutputStream.java new file mode 100644 index 00000000000..d42908a4147 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/RMIObjectOutputStream.java @@ -0,0 +1,114 @@ +/* RMIObjectOutputStream.java - + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.rmi.Remote; +import java.rmi.server.RMIClassLoader; +import java.rmi.server.RemoteStub; + +public class RMIObjectOutputStream + extends ObjectOutputStream { + +public RMIObjectOutputStream(OutputStream strm) throws IOException { + super(strm); + enableReplaceObject(true); +} + +//Separate it for override by MarshalledObject +protected void setAnnotation(String annotation) throws IOException{ + writeObject(annotation); +} + +protected void annotateClass(Class cls) throws IOException { + setAnnotation(RMIClassLoader.getClassAnnotation(cls)); +} + +protected void annotateProxyClass(Class cls) + throws IOException +{ + annotateClass(cls); +} + +protected Object replaceObject(Object obj) + throws IOException +{ + if((obj instanceof Remote) && !(obj instanceof RemoteStub)){ + UnicastServerRef ref = UnicastServer.getExportedRef((Remote)obj); + if (ref != null) + return ref.getStub(); + } + return obj; +} + +protected void writeValue(Object value, Class valueClass) throws IOException{ + if(valueClass.isPrimitive()){ + if(valueClass == Boolean.TYPE) + writeBoolean(((Boolean)value).booleanValue()); + else + if(valueClass == Byte.TYPE) + writeByte(((Byte)value).byteValue()); + else + if(valueClass == Character.TYPE) + writeChar(((Character)value).charValue()); + else + if(valueClass == Short.TYPE) + writeShort(((Short)value).shortValue()); + else + if(valueClass == Integer.TYPE) + writeInt(((Integer)value).intValue()); + else + if(valueClass == Long.TYPE) + writeLong(((Long)value).longValue()); + else + if(valueClass == Float.TYPE) + writeFloat(((Float)value).floatValue()); + else + if(valueClass == Double.TYPE) + writeDouble(((Double)value).doubleValue()); + else + throw new Error("Unsupported primitive class: " + valueClass); + } else + writeObject(value); +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/RMIVoidValue.java b/libjava/classpath/gnu/java/rmi/server/RMIVoidValue.java new file mode 100644 index 00000000000..8e5546d3523 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/RMIVoidValue.java @@ -0,0 +1,51 @@ +/* RMIVoidValue.java -- + Copyright (c) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.server; + +/** + * Package private class used to indicate a void return type. + * INSTANCE is the only object of this class ever made. + */ +final class RMIVoidValue +{ + static RMIVoidValue INSTANCE = new RMIVoidValue(); + + private RMIVoidValue() + { + } +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastConnection.java b/libjava/classpath/gnu/java/rmi/server/UnicastConnection.java new file mode 100644 index 00000000000..216e453e72f --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastConnection.java @@ -0,0 +1,231 @@ +/* UnicastConnection.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; +import java.rmi.RemoteException; + +public class UnicastConnection + implements Runnable, ProtocolConstants { + +UnicastConnectionManager manager; +Socket sock; +DataInputStream din; +DataOutputStream dout; +ObjectInputStream oin; +ObjectOutputStream oout; + +// reviveTime and expireTime make UnicastConnection pool-able +long reviveTime = 0; +long expireTime = Long.MAX_VALUE; + +UnicastConnection(UnicastConnectionManager man, Socket sock) { + this.manager = man; + this.sock = sock; +} + +void acceptConnection() throws IOException { +//System.out.println("Accepting connection on " + sock); + //Use BufferedXXXStream would be more efficient + din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); + dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); + + int sig = din.readInt(); + if (sig != PROTOCOL_HEADER) { + throw new IOException("bad protocol header"); + } + short ver = din.readShort(); + if (ver != PROTOCOL_VERSION) { + throw new IOException("bad protocol version"); + } + int protocol = din.readUnsignedByte(); + if (protocol != SINGLE_OP_PROTOCOL) { + // Send an ACK + dout.writeByte(PROTOCOL_ACK); + + // Send my hostname and port + dout.writeUTF(manager.serverName); + dout.writeInt(manager.serverPort); + dout.flush(); + + // Read their hostname and port + String rhost = din.readUTF(); + int rport = din.readInt(); + } + // Okay, ready to roll ... +} + +void makeConnection(int protocol) throws IOException { + //Use BufferedXXXStream would be more efficient + din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); + + dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); + + // Send header + dout.writeInt(PROTOCOL_HEADER); + dout.writeShort(PROTOCOL_VERSION); + dout.writeByte(protocol); + dout.flush(); + + if (protocol != SINGLE_OP_PROTOCOL) { + // Get back ack. + int ack = din.readUnsignedByte(); + if (ack != PROTOCOL_ACK) { + throw new RemoteException("Unsupported protocol"); + } + + // Read in host and port + String dicard_rhost = din.readUTF(); + int discard_rport = din.readInt(); + + // Send them my endpoint + dout.writeUTF(manager.serverName); + dout.writeInt(manager.serverPort); + dout.flush(); + } + // Okay, ready to roll ... +} + +DataInputStream getDataInputStream() throws IOException { + return (din); +} + +DataOutputStream getDataOutputStream() throws IOException { + return (dout); +} + +/* +* +* get ObjectInputStream for reading more objects +* +*/ +ObjectInputStream getObjectInputStream() throws IOException { + if (oin == null) { + throw new IOException("no ObjectInputtream for reading more objects"); + } + return (oin); +} + +/** +* +* starts ObjectInputStream. +* +*/ +ObjectInputStream startObjectInputStream() throws IOException { + return (oin = new RMIObjectInputStream(din)); +} + +/** +* +* get ObjectOutputStream for sending more objects +* +*/ +ObjectOutputStream getObjectOutputStream() throws IOException { + if (oout == null) { + throw new IOException("no ObjectOutputStream for sending more objects"); + } + return (oout); +} + +/** +* +* starts ObjectOutputStream. +* +*/ +ObjectOutputStream startObjectOutputStream() throws IOException { + return (oout = new RMIObjectOutputStream(dout)); +} + +void disconnect() { + try { + if(oout != null) + oout.close(); + sock.close(); + } + catch (IOException _) { + } + + oin = null; + oout = null; + din = null; + dout = null; + sock = null; +} + +public static final long CONNECTION_TIMEOUT = 10000L; + +static boolean isExpired(UnicastConnection conn, long l){ + if (l <= conn.expireTime ) + return false; + return true; +} + +static void resetTime(UnicastConnection conn){ + long l = System.currentTimeMillis(); + conn.reviveTime = l; + conn.expireTime = l + CONNECTION_TIMEOUT; +} + +/** + * We run connects on the server. Dispatch it then discard it. + */ +public void run() { + do{ + try { + UnicastServer.dispatch(this); + //don't discardConnection explicitly, only when + // exception happens or the connection's expireTime + // comes + } catch (Exception e ){ + manager.discardConnection(this); + break; + } + }while(true); +} + + +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastConnectionManager.java b/libjava/classpath/gnu/java/rmi/server/UnicastConnectionManager.java new file mode 100644 index 00000000000..08f6a9bc394 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastConnectionManager.java @@ -0,0 +1,446 @@ +/* UnicastConnectionManager.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.rmi.RemoteException; +import java.rmi.server.RMIClientSocketFactory; +import java.rmi.server.RMIServerSocketFactory; +import java.rmi.server.RMISocketFactory; +import java.util.ArrayList; +import java.util.ConcurrentModificationException; +import java.util.Hashtable; +import java.util.Iterator; + +public class UnicastConnectionManager + implements Runnable, ProtocolConstants { + +private static String localhost; +// use different maps for server/client type UnicastConnectionManager +private static Hashtable servers = new Hashtable(); +// Package-private to avoid trampolines. +static Hashtable clients = new Hashtable(); +ArrayList connections; //client connection pool + +// make serverThread volatile for poll +private volatile Thread serverThread; +private ServerSocket ssock; +String serverName; +int serverPort; + +// Package-private to avoid a trampoline. +static Thread scavenger; + +// If client and server are in the same VM, serverobj represents server +Object serverobj; + +private static RMISocketFactory defaultSocketFactory = RMISocketFactory.getSocketFactory(); +private RMIServerSocketFactory serverFactory; +private RMIClientSocketFactory clientFactory; + +// The following is for debug +private static int ncsock = 0; //count of client socket +private static int nssock = 0; //count of server socket +private static int ncmanager = 0; //count of client manager +private static int nsmanager = 0; //count of server manager + +private static final boolean debug = false; + +private static final Object GLOBAL_LOCK = new Object(); + +static { + try { + //Use host address instead of host name to avoid name resolving issues + //localhost = InetAddress.getLocalHost().getHostName(); + localhost = InetAddress.getLocalHost().getHostAddress(); + } + catch (UnknownHostException _) { + localhost = "localhost"; + } + + +} + +//Only one scavenger thread running globally +private static void startScavenger(){ + scavenger = new Thread(new Runnable(){ + public void run(){ + if (debug) System.out.println("************* start scavenger."); + boolean liveon = true; + while (liveon){ + // Sleep for the expire timeout + try{ + Thread.sleep(UnicastConnection.CONNECTION_TIMEOUT); + }catch(InterruptedException _ie){ + break; + } + liveon = false; + // Scavenge all clients' connections that're expired + Iterator iter = clients.values().iterator(); + long l = System.currentTimeMillis(); + try{ + while(iter.hasNext()){ + UnicastConnectionManager man = (UnicastConnectionManager)iter.next(); + ArrayList conns = man.connections; + synchronized(conns) { // is the lock a little coarser? + for (int last = conns.size() - 1; + last >= 0; + --last) + { + UnicastConnection conn = (UnicastConnection)conns.get(last); + if (UnicastConnection.isExpired(conn, l)){ + conns.remove(last); + conn.disconnect(); + conn = null; + }else + liveon = true; //there're still live connections + } + } + } + }catch(ConcurrentModificationException cme) { + // handle it lazily + liveon = true; + } + } + scavenger = null; + if (debug) System.out.println("************* exit scavenger."); + } + }); + // As it is used for client connection, we may put this thread + // in daemon state to prevent the VM from blocking when exiting. + scavenger.setDaemon(true); + scavenger.start(); +} + +/** + * Client UnicastConnectionManager constructor + */ +private UnicastConnectionManager(String host, int port, RMIClientSocketFactory csf) { + ssock = null; + serverName = host; + serverPort = port; + serverFactory = null; + clientFactory = csf; + connections = new ArrayList(); +} + +/** + * Server UnicastConnectionManager constructor + */ +private UnicastConnectionManager(int port, RMIServerSocketFactory ssf) throws RemoteException { + + try { + ssock = ssf.createServerSocket(port); + serverPort = ssock.getLocalPort(); + } + catch (IOException ioex) { + ssock = null; + serverPort = 0; + throw new java.rmi.server.ExportException("can not create Server Socket on port " + port,ioex); + } + serverName = localhost; + serverFactory = ssf; + clientFactory = null; +} + +/** + * Return a client connection manager which will connect to the given + * host/port. + */ +public static synchronized UnicastConnectionManager getInstance(String host, int port, RMIClientSocketFactory csf) { +//System.out.println("getInstance: " + host + "," + port + "," + csf); + if (csf == null) { + csf = defaultSocketFactory; + } + // change host name to host address to avoid name resolving issues + try{ + host = InetAddress.getByName(host).getHostAddress(); + }catch(Exception _){} + + TripleKey key = new TripleKey(host, port, csf); + UnicastConnectionManager man = (UnicastConnectionManager)clients.get(key); + if (man == null) { + man = new UnicastConnectionManager(host, port, csf); + if (debug) { + ncmanager++; + System.out.println("\n\n ====== " + ncmanager + " client managers.\n\n"); + } + clients.put(key, man); + + // Detect if client and server are in the same VM, i.e., their keys are equal + UnicastConnectionManager svrman = (UnicastConnectionManager)servers.get(key); + if(svrman != null){ // server and client are in the same VM + man.serverobj = svrman.serverobj; + } + } + return (man); +} + +/** + * Return a server connection manager which will accept connection on the + * given port. + */ +public static synchronized UnicastConnectionManager getInstance(int port, RMIServerSocketFactory ssf) throws RemoteException { +//System.out.println("getInstance: " + port + "," + ssf); + if (ssf == null) { + ssf = defaultSocketFactory; + } + TripleKey key = new TripleKey(localhost, port, ssf); + UnicastConnectionManager man = (UnicastConnectionManager)servers.get(key); + if (man == null) { + man = new UnicastConnectionManager(port, ssf); + if (debug) { + nsmanager++; + System.out.println("\n\n ****** " + nsmanager + " server managers.\n\n"); + } + // The provided port might not be the set port. + key.port = man.serverPort; + servers.put(key, man); + } + return (man); +} + +/** + * Get a connection from this manager. + */ +public UnicastConnection getConnection() throws IOException { + if (ssock == null) { + return (getClientConnection()); + } + else { + return (getServerConnection()); + } +} + +/** + * Accept a connection to this server. + */ +private UnicastConnection getServerConnection() throws IOException { + Socket sock = ssock.accept(); + sock.setTcpNoDelay(true); //?? + UnicastConnection conn = new UnicastConnection(this, sock); + conn.acceptConnection(); + if (debug){ + nssock++; + System.out.println("\n\n ****** " + nssock + " server socks.\n\n"); + } + //System.out.println("Server connection " + sock); + return (conn); +} + +/** + * Make a conection from this client to the server. + */ +private UnicastConnection getClientConnection() throws IOException { + ArrayList conns = connections; + UnicastConnection conn; + + synchronized(conns) { + int nconn = conns.size() - 1; + + // if there're free connections in connection pool + if(nconn >= 0) { + conn = (UnicastConnection)conns.get(nconn); + //Should we check if conn is alive using Ping?? + conns.remove(nconn); + + // Check if the connection is already expired + long l = System.currentTimeMillis(); + if (!UnicastConnection.isExpired(conn, l)){ + return conn; + }else { + conn.disconnect(); + conn = null; + } + } + } + + Socket sock = clientFactory.createSocket(serverName, serverPort); + conn = new UnicastConnection(this, sock); + conn.makeConnection(DEFAULT_PROTOCOL); + + if (debug) { + ncsock++; + System.out.println("\n\n ====== " + ncsock + " client socks.\n\n"); + } + + return (conn); +} + +/** + * Discard a connection when we're done with it - maybe it can be + * recycled. + */ +public void discardConnection(UnicastConnection conn) { +//System.out.println("Discarding connection " + conn); + //conn.disconnect(); + if (ssock != null) //server connection + conn.disconnect(); + else { + // To client connection, we'd like to return back to pool + UnicastConnection.resetTime(conn); + //Ensure there're only one scavenger globally + synchronized(GLOBAL_LOCK) { + connections.add(conn); //borrow this lock to garantee thread safety + if (scavenger == null) + startScavenger(); + } + } +} + +/** + * Start a server on this manager if it's a server socket and we've not + * already got one running. + */ +public void startServer() { + synchronized(this) { + if (ssock == null || serverThread != null) { + return; + } + serverThread = new Thread(this); + // The following is not necessary when java.lang.Thread's constructor do this. + // serverThread.setContextClassLoader(Thread.currentThread().getContextClassLoader()); + } + serverThread.start(); +} + +/** + * Stop a server on this manager + */ +public void stopServer() { + synchronized(this) { + if(serverThread != null){ + serverThread = null; + try{ + ssock.close(); + }catch(Exception _){} + } + } +} + +/** + * Server thread for connection manager. + */ +public void run() { + for (;serverThread != null;) { // if serverThread==null, then exit thread + try { +//System.out.println("Waiting for connection on " + serverPort); + UnicastConnection conn = getServerConnection(); + + // get address of remote host for the RMIIncomingThread object + String remoteHost = null; + if (conn.sock != null) { + remoteHost = conn.sock.getInetAddress().getHostAddress(); + } + + // use a thread pool to improve performance + //ConnectionRunnerPool.dispatchConnection(conn); + (new RMIIncomingThread(conn, remoteHost)).start(); +// (new Thread(conn)).start(); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} + +/** + * Serialization routine. + */ +void write(ObjectOutput out) throws IOException { + out.writeUTF(serverName); + out.writeInt(serverPort); +} + +/** + * Serialization routine. + */ +static UnicastConnectionManager read(ObjectInput in) throws IOException { + String host = in.readUTF(); + int port = in.readInt(); + //RMIClientSocketFactory csf = ((RMIObjectInputStream)in).manager.clientFactory; + //return (getInstance(host, port, csf)); + return (getInstance(host, port, null)); +} + +} + +/** + * This is use as the hashkey for the client/server connections. + */ +class TripleKey { + +String host; +int port; +Object other; + +TripleKey(String host, int port, Object other) { + this.host = host; + this.port = port; + this.other = other; +} + +/** + * Hash code just include the host and other - we ignore the port since + * this has unusual matching behaviour. + */ +public int hashCode() { + return (host.hashCode() ^ other.hashCode()); +} + +public boolean equals(Object obj) { + if (obj instanceof TripleKey) { + TripleKey other = (TripleKey)obj; + if (this.host.equals(other.host) && + this.other == other.other && + (this.port == other.port /* || this.port == 0 || other.port == 0*/)) { + return (true); + } + } + return (false); +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastRef.java b/libjava/classpath/gnu/java/rmi/server/UnicastRef.java new file mode 100644 index 00000000000..c1b871fb8e5 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastRef.java @@ -0,0 +1,278 @@ +/* UnicastRef.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.server; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.server.ObjID; +import java.rmi.server.Operation; +import java.rmi.server.RMIClientSocketFactory; +import java.rmi.server.RemoteCall; +import java.rmi.server.RemoteObject; +import java.rmi.server.RemoteRef; +import java.rmi.server.UID; + +public class UnicastRef + implements RemoteRef, ProtocolConstants { + +public ObjID objid; +UnicastConnectionManager manager; + +/** + * Used by serialization, and let subclass capable of having default constructor + */ +// must be public otherwise java.rmi.RemoteObject cannot instantiate this class +// -- iP +public UnicastRef() { +} + +public UnicastRef(ObjID objid, String host, int port, RMIClientSocketFactory csf) { + this(objid); + manager = UnicastConnectionManager.getInstance(host, port, csf); +} + +public UnicastRef(ObjID objid) { + this.objid = objid; +} + +public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception { + // Check if client and server are in the same VM, then local call can be used to + // replace remote call, but it's somewhat violating remote semantic. + Object svrobj = manager.serverobj; + + // Make sure that the server object is compatible. It could be loaded from a different + // classloader --iP + if(svrobj != null && method.getDeclaringClass().isInstance(svrobj)){ + //local call + Object ret = null; + try{ + ret = method.invoke(svrobj, params); + }catch(InvocationTargetException e){ + throw (Exception)e.getTargetException(); + } + //System.out.println("\n\n ***** local call: " + method + "\nreturn: " + ret + "\n\n"); + return ret; + } + //System.out.println("***************** remote call:" + manager.serverPort); + return (invokeCommon(obj, method, params, -1, opnum)); +} + +private Object invokeCommon(Remote obj, Method method, Object[] params, int opnum, long hash) throws Exception { + UnicastConnection conn; + try { + conn = manager.getConnection(); + } + catch (IOException e1) { + throw new RemoteException("connection failed to host: " + manager.serverName, e1); + } + + ObjectOutputStream out; + DataOutputStream dout; + try { + dout = conn.getDataOutputStream(); + dout.writeByte(MESSAGE_CALL); + + out = conn.startObjectOutputStream(); // (re)start ObjectOutputStream + + objid.write(out); + out.writeInt(opnum); + out.writeLong(hash); + + // must handle primitive class and their wrapper classes + Class clss[] = method.getParameterTypes(); + for(int i = 0; i < clss.length; i++) + ((RMIObjectOutputStream)out).writeValue(params[i], clss[i]); + + out.flush(); + } + catch (IOException e2) { + throw new RemoteException("call failed: ", e2); + } + + int returncode; + Object returnval; + DataInputStream din; + ObjectInputStream in; + UID ack; + try { + din = conn.getDataInputStream(); + + if ((returncode = din.readUnsignedByte()) != MESSAGE_CALL_ACK) { + conn.disconnect(); + throw new RemoteException("Call not acked:" + returncode); + } + + in = conn.startObjectInputStream(); // (re)start ObjectInputStream + returncode = in.readUnsignedByte(); + ack = UID.read(in); + + Class cls = method.getReturnType(); + + if (returncode == RETURN_NACK) { + returnval = in.readObject(); // get Exception + + } else if(cls == Void.TYPE) { + returnval = null; + // in.readObject() // not required! returntype 'void' means no field is returned. + } else { + returnval = ((RMIObjectInputStream)in).readValue(cls); // get returnvalue + } + } catch (IOException e3) { + //for debug: e3.printStackTrace(); + throw new RemoteException("call return failed: ", e3); + } + + /* if DGCAck is necessary?? + //According to RMI wire protocol, send a DGCAck + // to indicate receiving return value + dout.writeByte(MESSAGE_DGCACK); + ack.write(dout); + out.flush(); + */ + + manager.discardConnection(conn); + + if (returncode != RETURN_ACK && returnval != null) { + if (returncode == RETURN_NACK) throw (Exception)returnval; + else throw new RemoteException("unexpected returncode: " + returncode); + } + + return (returnval); +} + +/** + * @deprecated + */ +public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException { + UnicastConnection conn; + + try { + conn = manager.getConnection(); + } + catch (IOException e1) { + throw new RemoteException("connection failed to host: " + manager.serverName, e1); + } + + //obj: useless? + + return (new UnicastRemoteCall(conn, objid, opnum, hash)); +} + +/** + * @deprecated + */ +public void invoke(RemoteCall call) throws Exception { + UnicastRemoteCall c = (UnicastRemoteCall)call; + call.executeCall(); +} + +/** + * @deprecated + */ +public void done(RemoteCall call) throws RemoteException { + UnicastRemoteCall c = (UnicastRemoteCall)call; + try{ + c.done(); + } catch(IOException e){} + UnicastConnection conn = c.getConnection(); + manager.discardConnection(conn); +} + +public void writeExternal(ObjectOutput out) throws IOException { + if (manager == null) { + throw new IOException("no connection"); + } + manager.write(out); + objid.write(out); + // This byte is somewhat confusing when interoperating with JDK + out.writeByte(0); //RETURN_ACK); +} + +public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + manager = UnicastConnectionManager.read(in); + objid = ObjID.read(in); + byte ack = in.readByte(); + // This byte is somewhat confusing when interoperating with JDK + if (ack != RETURN_ACK && ack != 0/*jdk ack value*/) { + throw new IOException("no ack found"); + } +} + +public boolean remoteEquals(RemoteRef ref) { + throw new Error("Not implemented"); +} + +public int remoteHashCode() { + throw new Error("Not implemented"); +} + +public String getRefClass(ObjectOutput out) { + return ("UnicastRef"); +} + +public String remoteToString() { + throw new Error("Not implemented"); +} + +public void dump(UnicastConnection conn) { + try { + DataInputStream din = conn.getDataInputStream(); + for (;;) { + int b = din.readUnsignedByte(); + System.out.print(Integer.toHexString(b)); + if (b >= 32 && b < 128) { + System.out.print(": " + (char)b); + } + System.out.println(); + } + } + catch (IOException _) { + } +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastRemoteCall.java b/libjava/classpath/gnu/java/rmi/server/UnicastRemoteCall.java new file mode 100644 index 00000000000..c5206e76df2 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastRemoteCall.java @@ -0,0 +1,525 @@ +/* UnicastRemoteCall.java + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.StreamCorruptedException; +import java.rmi.MarshalException; +import java.rmi.RemoteException; +import java.rmi.UnmarshalException; +import java.rmi.server.ObjID; +import java.rmi.server.RemoteCall; +import java.rmi.server.UID; +import java.util.Vector; + +public class UnicastRemoteCall + implements RemoteCall, ProtocolConstants +{ + + private UnicastConnection conn; + private Object result; + private Object object; + private int opnum; + private long hash; + // These are package-private due to inner class access. + Vector vec; + int ptr; + private ObjID objid; + + private ObjectOutput oout; + private ObjectInput oin; + + /** + * Incoming call. + */ + UnicastRemoteCall(UnicastConnection conn) + { + this.conn = conn; + } + + /** + * Outgoing call. + */ + UnicastRemoteCall(UnicastConnection conn, ObjID objid, int opnum, long hash) + throws RemoteException + { + this.conn = conn; + this.opnum = opnum; + this.hash = hash; + this.objid = objid; + } + + UnicastConnection getConnection() + { + return conn; + } + + public ObjectOutput getOutputStream() throws IOException + { + if (vec == null) + vec = new Vector(); + return (new DummyObjectOutputStream()); + } + + public void releaseOutputStream() throws IOException + { + if (vec != null) + { + oout = conn.getObjectOutputStream(); + + for (int i = 0; i < vec.size(); i += 2) + { + boolean primitive = ((Boolean)vec.elementAt(i)).booleanValue(); + Object data = vec.elementAt(i+1); + + // No type, this is + if (!primitive) + oout.writeObject(data); + else + { + if (data instanceof Boolean) + oout.writeBoolean(((Boolean)data).booleanValue()); + else if (data instanceof Character) + oout.writeChar(((Character)data).charValue()); + else if (data instanceof Byte) + oout.writeByte(((Byte)data).byteValue()); + else if (data instanceof Short) + oout.writeShort(((Short)data).shortValue()); + else if (data instanceof Integer) + oout.writeInt(((Integer)data).intValue()); + else if (data instanceof Long) + oout.writeLong(((Long)data).longValue()); + } + } + vec = null; + } + if(oout != null) + oout.flush(); + } + + /** + * + * (re)starts ObjectInputStream + * + */ + public ObjectInput startInputStream() throws IOException + { + if (conn != null) { + return (oin = conn.startObjectInputStream()); + } else { + return getInputStream(); // dummy Input Stream + } + + } + + public ObjectInput getInputStream() throws IOException + { + if (conn != null) + { + if(oin == null) + return (oin = conn.getObjectInputStream()); + else + return oin; + } + else + { + ptr = 0; + return (new DummyObjectInputStream()); + } + } + + public void releaseInputStream() throws IOException + { + // Does nothing. + } + + public ObjectOutput getResultStream(boolean success) + throws IOException, StreamCorruptedException + { + vec = new Vector(); + return new DummyObjectOutputStream(); + } + + public void executeCall() throws Exception + { + byte returncode; + ObjectInput oin; + + // signal the call when constructing + try + { + DataOutputStream dout = conn.getDataOutputStream(); + dout.write(MESSAGE_CALL); + + oout = conn.startObjectOutputStream(); // (re)start ObjectOutputStream + objid.write(oout); + oout.writeInt(opnum); + oout.writeLong(hash); + } + catch(IOException ex) + { + throw new MarshalException("Try to write header but failed.", ex); + } + + try + { + releaseOutputStream(); + DataInputStream din = conn.getDataInputStream(); + if (din.readByte() != MESSAGE_CALL_ACK) + throw new RemoteException("Call not acked"); + + oin = startInputStream(); + returncode = oin.readByte(); + UID.read(oin); + } + catch(IOException ex) + { + throw new UnmarshalException("Try to read header but failed:", ex); + } + + //check return code + switch(returncode) + { + case RETURN_ACK: //it's ok + return; + case RETURN_NACK: + Object returnobj; + try + { + returnobj = oin.readObject(); + } + catch(Exception ex2) + { + throw new UnmarshalException + ("Try to read exception object but failed", ex2); + } + + if(!(returnobj instanceof Exception)) + throw new UnmarshalException("Should be Exception type here: " + + returnobj); + throw (Exception)returnobj; + + default: + throw new UnmarshalException("Invalid return code"); + } + } + + public void done() throws IOException + { + // conn.disconnect(); + } + + boolean isReturnValue() + { + return vec.size() > 0; + } + + Object returnValue() + { + // This is not the first one (Boolean) but the second. + return vec.elementAt(1); + } + + Object[] getArguments() + { + return vec.toArray(); + } + + Object getObject() + { + return object; + } + + int getOpnum() + { + return opnum; + } + + long getHash() + { + return hash; + } + + void setReturnValue(Object obj) + { + vec.removeAllElements(); + vec.addElement(obj); + } + + /** + * Dummy object output class. + */ + private class DummyObjectOutputStream implements ObjectOutput + { + /** + * Non-private constructor to reduce bytecode emitted. + */ + DummyObjectOutputStream() + { + } + + public void writeBoolean(boolean v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(Boolean.valueOf(v)); + } + + public void writeByte(int v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Byte((byte) v)); + } + + public void writeChar(int v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Character((char) v)); + } + + public void writeDouble(double v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Double(v)); + } + + public void writeFloat(float v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Float(v)); + } + + public void writeInt(int v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Integer(v)); + } + + public void writeLong(long v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Long(v)); + } + + public void writeShort(int v) throws IOException + { + vec.addElement(Boolean.TRUE); + vec.addElement(new Short((short) v)); + } + + public void writeObject(Object obj) throws IOException + { + vec.addElement(Boolean.FALSE); + vec.addElement(obj); + } + + public void write(byte b[]) throws IOException + { + throw new IOException("not required"); + } + + public void write(byte b[], int off, int len) throws IOException + { + throw new IOException("not required"); + } + + public void write(int b) throws IOException + { + throw new IOException("not required"); + } + + public void writeBytes(String s) throws IOException + { + throw new IOException("not required"); + } + + public void writeChars(String s) throws IOException + { + throw new IOException("not required"); + } + + public void writeUTF(String str) throws IOException + { + throw new IOException("not required"); + } + + public void flush() throws IOException + { + } + + public void close() throws IOException + { + } + } // class DummyObjectOutputStream + + /** + * Dummy object input class. + */ + private class DummyObjectInputStream implements ObjectInput + { + /** + * Non-private constructor to reduce bytecode emitted. + */ + DummyObjectInputStream() + { + } + + public boolean readBoolean() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Boolean) obj).booleanValue(); + } + + public byte readByte() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Byte) obj).byteValue(); + } + + public char readChar() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Character) obj).charValue(); + } + + public double readDouble() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Double) obj).doubleValue(); + } + + public float readFloat() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Float) obj).floatValue(); + } + + public int readInt() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Integer) obj).intValue(); + } + + public long readLong() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Long) obj).longValue(); + } + + public short readShort() throws IOException + { + Object obj = vec.elementAt(ptr++); + return ((Short) obj).shortValue(); + } + + public Object readObject() throws IOException + { + return vec.elementAt(ptr++); + } + + public int read(byte b[]) throws IOException + { + throw new IOException("not required"); + } + + public int read(byte b[], int off, int len) throws IOException + { + throw new IOException("not required"); + } + + public int read() throws IOException + { + throw new IOException("not required"); + } + + public long skip(long n) throws IOException + { + throw new IOException("not required"); + } + + public int available() throws IOException + { + throw new IOException("not required"); + } + + public void readFully(byte b[]) throws IOException + { + throw new IOException("not required"); + } + + public void readFully(byte b[], int off, int len) throws IOException + { + throw new IOException("not required"); + } + + public String readLine() throws IOException + { + throw new IOException("not required"); + } + + public String readUTF() throws IOException + { + throw new IOException("not required"); + } + + public int readUnsignedByte() throws IOException + { + throw new IOException("not required"); + } + + public int readUnsignedShort() throws IOException + { + throw new IOException("not required"); + } + + public int skipBytes(int n) throws IOException + { + throw new IOException("not required"); + } + + public void close() throws IOException + { + } + } // class DummyObjectInputStream + +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastRemoteStub.java b/libjava/classpath/gnu/java/rmi/server/UnicastRemoteStub.java new file mode 100644 index 00000000000..87e932de559 --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastRemoteStub.java @@ -0,0 +1,50 @@ +/* UnicastRemoteStub.java -- + Copyright (c) 1996, 1997, 1998, 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.rmi.server; + +import java.rmi.server.RemoteRef; +import java.rmi.server.RemoteStub; + +public class UnicastRemoteStub + extends RemoteStub { + +public static void setStubRef(RemoteStub stub, RemoteRef ref) { + setRef(stub, ref); +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastServer.java b/libjava/classpath/gnu/java/rmi/server/UnicastServer.java new file mode 100644 index 00000000000..065ef8c77ee --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastServer.java @@ -0,0 +1,167 @@ +/* UnicastServer.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import gnu.java.rmi.dgc.DGCImpl; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.rmi.NoSuchObjectException; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.ServerError; +import java.rmi.server.ObjID; +import java.rmi.server.UID; +import java.util.Hashtable; + +public class UnicastServer + implements ProtocolConstants { + +static private Hashtable objects = new Hashtable(); //mapping OBJID to server ref +static private Hashtable refcache = new Hashtable(); //mapping obj itself to server ref +static private DGCImpl dgc; + +public static void exportObject(UnicastServerRef obj) { + startDGC(); + objects.put(obj.objid, obj); + refcache.put(obj.myself, obj); + obj.manager.startServer(); +} + +// FIX ME: I haven't handle force parameter +public static boolean unexportObject(UnicastServerRef obj, boolean force) { + objects.remove(obj.objid); + refcache.remove(obj.myself); + obj.manager.stopServer(); + return true; +} + +public static UnicastServerRef getExportedRef(Remote remote){ + return (UnicastServerRef)refcache.get(remote); +} + +private static synchronized void startDGC() { + if (dgc == null) { + try { + dgc = new DGCImpl(); + // Changed DGCImpl to inherit UnicastServerRef directly + //((UnicastServerRef)dgc.getRef()).exportObject(dgc); + dgc.exportObject(dgc); + } + catch (RemoteException e) { + e.printStackTrace(); + } + } +} + +public static void dispatch(UnicastConnection conn) throws Exception { + switch (conn.getDataInputStream().readUnsignedByte()) { + case MESSAGE_CALL: + incomingMessageCall(conn); + break; + case MESSAGE_PING: + // jdk sends a ping before each method call -> answer it! + DataOutputStream out = conn.getDataOutputStream(); + out.writeByte(MESSAGE_PING_ACK); + out.flush(); + break; + default: + throw new Exception("bad method type"); + } +} + +private static void incomingMessageCall(UnicastConnection conn) throws IOException { + ObjectInputStream in = conn.startObjectInputStream(); // (re)start ObjectInputStream + + ObjID objid = ObjID.read(in); + int method = in.readInt(); + long hash = in.readLong(); + +//System.out.println("ObjID: " + objid + ", method: " + method + ", hash: " + hash); + + // Use the objid to locate the relevant UnicastServerRef + UnicastServerRef uref = (UnicastServerRef)objects.get(objid); + Object returnval; + int returncode = RETURN_ACK; + // returnval is from Method.invoke(), so we must check the return class to see + // if it's primitive type + Class returncls = null; + if (uref != null) { + try { + // Dispatch the call to it. + returnval = uref.incomingMessageCall(conn, method, hash); + returncls = uref.getMethodReturnType(method, hash); + } + catch (Exception e) { + returnval = e; + returncode = RETURN_NACK; + } + catch (Error e) { + returnval = new ServerError ("An Error is thrown while processing the invocation on the server", e); + returncode = RETURN_NACK; + } + } + else { + returnval = new NoSuchObjectException(""); + returncode = RETURN_NACK; + } + + conn.getDataOutputStream().writeByte(MESSAGE_CALL_ACK); + + ObjectOutputStream out = conn.startObjectOutputStream(); // (re)start ObjectOutputStream + + out.writeByte(returncode); + (new UID()).write(out); + + //System.out.println("returnval=" + returnval + " returncls=" + returncls); + + if(returnval != null && returncls != null) + ((RMIObjectOutputStream)out).writeValue(returnval, returncls); + + // 1.1/1.2 void return type detection: + else if (!(returnval instanceof RMIVoidValue || returncls == Void.TYPE)) + out.writeObject(returnval); + + out.flush(); +} + +} diff --git a/libjava/classpath/gnu/java/rmi/server/UnicastServerRef.java b/libjava/classpath/gnu/java/rmi/server/UnicastServerRef.java new file mode 100644 index 00000000000..1f6eedec68a --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/UnicastServerRef.java @@ -0,0 +1,306 @@ +/* UnicastServerRef.java -- + Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003, 2004 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.rmi.server; + +import java.io.ObjectInputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.server.ObjID; +import java.rmi.server.RMIServerSocketFactory; +import java.rmi.server.RemoteRef; +import java.rmi.server.RemoteServer; +import java.rmi.server.RemoteStub; +import java.rmi.server.ServerNotActiveException; +import java.rmi.server.ServerRef; +import java.rmi.server.Skeleton; +import java.util.Hashtable; + +public class UnicastServerRef + extends UnicastRef + implements ServerRef{ //SHOULD implement ServerRef + +final static private Class[] stubprototype = new Class[] { RemoteRef.class }; + +Remote myself; //save the remote object itself +private Skeleton skel; +private RemoteStub stub; +private Hashtable methods = new Hashtable(); + +/** + * Used by serialization. + */ +UnicastServerRef() +{ +} + +public UnicastServerRef(ObjID id, int port, RMIServerSocketFactory ssf) throws RemoteException { + super(id); + manager = UnicastConnectionManager.getInstance(port, ssf); +} + +public RemoteStub exportObject(Remote obj) throws RemoteException { + if (myself == null) { + myself = obj; + // Save it to server manager, to let client calls in the same VM to issue + // local call + manager.serverobj = obj; + + // Find and install the stub + Class cls = obj.getClass(); + Class expCls; + try { + // where ist the _Stub? (check superclasses also) + expCls = findStubSkelClass(cls); + } catch (Exception ex) { + throw new RemoteException("can not find stubs for class: " + cls, ex); + } + + stub = (RemoteStub)getHelperClass(expCls, "_Stub"); + if (stub == null) { + throw new RemoteException("failed to export: " + cls); + } + + // Find and install the skeleton (if there is one) + skel = (Skeleton)getHelperClass(expCls, "_Skel"); + + // Build hash of methods which may be called. + buildMethodHash(obj.getClass(), true); + + // Export it. + UnicastServer.exportObject(this); + } + + return (stub); +} + +public RemoteStub exportObject(Remote remote, Object obj) + throws RemoteException +{ + //FIX ME + return exportObject(remote); +} + +public RemoteStub getStub(){ + return stub; +} + + +public boolean unexportObject(Remote obj, boolean force) { + // Remove all hashes of methods which may be called. + buildMethodHash(obj.getClass(), false); + return UnicastServer.unexportObject(this, force); +} + +/** +* +* The Subs/Skels might not there for the actual class, but maybe +* for one of the superclasses. +* +*/ +private Class findStubSkelClass(Class startCls) throws Exception { + Class cls = startCls; + + while (true) { + try { + String stubClassname = cls.getName() + "_Stub"; + ClassLoader cl = cls.getClassLoader(); + Class scls = cl == null ? Class.forName(stubClassname) + : cl.loadClass(stubClassname); + return cls; // found it + } catch (ClassNotFoundException e) { + Class superCls = cls.getSuperclass(); + if (superCls == null + || superCls == java.rmi.server.UnicastRemoteObject.class) + { + throw new Exception("Neither " + startCls + + " nor one of their superclasses (like" + cls + ")" + + " has a _Stub"); + } + cls = superCls; + } + } +} + + + +private Object getHelperClass(Class cls, String type) { + try { + String classname = cls.getName(); + ClassLoader cl = cls.getClassLoader(); + Class scls = cl == null ? Class.forName(classname + type) + : cl.loadClass(classname + type); + if (type.equals("_Stub")) { + try { + // JDK 1.2 stubs + Constructor con = scls.getConstructor(stubprototype); + return (con.newInstance(new Object[]{this})); + } + catch (NoSuchMethodException e) { + } + catch (InstantiationException e) { + } + catch (IllegalAccessException e) { + } + catch (IllegalArgumentException e) { + } + catch (InvocationTargetException e) { + } + // JDK 1.1 stubs + RemoteStub stub = (RemoteStub)scls.newInstance(); + UnicastRemoteStub.setStubRef(stub, this); + return (stub); + } + else { + // JDK 1.1 skel + return (scls.newInstance()); + } + } + catch (ClassNotFoundException e) { + } + catch (InstantiationException e) { + } + catch (IllegalAccessException e) { + } + return (null); +} + + + +public String getClientHost() throws ServerNotActiveException { + return RemoteServer.getClientHost(); +} + +private void buildMethodHash(Class cls, boolean build) { + Method[] meths = cls.getMethods(); + for (int i = 0; i < meths.length; i++) { + /* Don't need to include any java.xxx related stuff */ + if (meths[i].getDeclaringClass().getName().startsWith("java.")) { + continue; + } + long hash = RMIHashes.getMethodHash(meths[i]); + if(build) + methods.put(new Long (hash), meths[i]); + else + methods.remove(new Long (hash)); +//System.out.println("meth = " + meths[i] + ", hash = " + hash); + } +} + +Class getMethodReturnType(int method, long hash) throws Exception +{ + if (method == -1) { + Method meth = (Method)methods.get(new Long (hash)); + return meth.getReturnType(); + }else + return null; +} + +public Object incomingMessageCall(UnicastConnection conn, int method, long hash) throws Exception { +//System.out.println("method = " + method + ", hash = " + hash); + // If method is -1 then this is JDK 1.2 RMI - so use the hash + // to locate the method + if (method == -1) { + Method meth = (Method)methods.get(new Long (hash)); +//System.out.println("class = " + myself.getClass() + ", meth = " + meth); + if (meth == null) { + throw new NoSuchMethodException(); + } + + ObjectInputStream in = conn.getObjectInputStream(); + int nrargs = meth.getParameterTypes().length; + Object[] args = new Object[nrargs]; + for (int i = 0; i < nrargs; i++) { + /** + * For debugging purposes - we don't handle CodeBases + * quite right so we don't always find the stubs. This + * lets us know that. + */ + try { + // need to handle primitive types + args[i] = ((RMIObjectInputStream)in).readValue(meth.getParameterTypes()[i]); + + } + catch (Exception t) { + t.printStackTrace(); + throw t; + } + } + //We must reinterpret the exception thrown by meth.invoke() + //return (meth.invoke(myself, args)); + Object ret = null; + try{ + ret = meth.invoke(myself, args); + }catch(InvocationTargetException e){ + Throwable cause = e.getTargetException(); + if (cause instanceof Exception) { + throw (Exception)cause; + } + else if (cause instanceof Error) { + throw (Error)cause; + } + else { + throw new Error("The remote method threw a java.lang.Throwable that is neither java.lang.Exception nor java.lang.Error.", e); + } + } + return ret; + } + // Otherwise this is JDK 1.1 style RMI - we find the skeleton + // and invoke it using the method number. We wrap up our + // connection system in a UnicastRemoteCall so it appears in a + // way the Skeleton can handle. + else { + if (skel == null) { + throw new NoSuchMethodException(); + } + UnicastRemoteCall call = new UnicastRemoteCall(conn); + skel.dispatch(myself, call, method, hash); + if (!call.isReturnValue()) + return RMIVoidValue.INSTANCE; + else + return (call.returnValue()); + } +} + +} + + diff --git a/libjava/classpath/gnu/java/rmi/server/package.html b/libjava/classpath/gnu/java/rmi/server/package.html new file mode 100644 index 00000000000..08c105b495d --- /dev/null +++ b/libjava/classpath/gnu/java/rmi/server/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.rmi.server package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.rmi.server</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/Engine.java b/libjava/classpath/gnu/java/security/Engine.java new file mode 100644 index 00000000000..b75de5caa95 --- /dev/null +++ b/libjava/classpath/gnu/java/security/Engine.java @@ -0,0 +1,237 @@ +/* Engine -- generic getInstance method. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import java.security.NoSuchAlgorithmException; +import java.security.Provider; + +/** + * Generic implementation of the getInstance methods in the various + * engine classes in java.security. + * <p> + * These classes ({@link java.security.Signature} for example) can be + * thought of as the "chrome, upholstery, and steering wheel", and the SPI + * (service provider interface, e.g. {@link java.security.SignatureSpi}) + * classes can be thought of as the "engine" -- providing the actual + * functionality of whatever cryptographic algorithm the instance + * represents. + * + * @see Provider + * @author Casey Marshall + */ +public final class Engine +{ + + // Constants. + // ------------------------------------------------------------------------ + + /** Prefix for aliases. */ + private static final String ALG_ALIAS = "Alg.Alias."; + + /** Maximum number of aliases to try. */ + private static final int MAX_ALIASES = 5; + + /** Argument list for no-argument constructors. */ + private static final Object[] NO_ARGS = new Object[0]; + + // Constructor. + // ------------------------------------------------------------------------ + + /** This class cannot be instantiated. */ + private Engine() { } + + // Class method. + // ------------------------------------------------------------------------ + + /** + * Get the implementation for <i>algorithm</i> for service + * <i>service</i> from <i>provider</i>. The service is e.g. + * "Signature", and the algorithm "DSA". + * + * @param service The service name. + * @param algorithm The name of the algorithm to get. + * @param provider The provider to get the implementation from. + * @return The engine class for the specified algorithm; the object + * returned is typically a subclass of the SPI class for that + * service, but callers should check that this is so. + * @throws NoSuchAlgorithmException If the implementation cannot be + * found or cannot be instantiated. + * @throws InvocationTargetException If the SPI class's constructor + * throws an exception. + * @throws IllegalArgumentException If any of the three arguments are null. + */ + public static Object getInstance(String service, String algorithm, + Provider provider) + throws InvocationTargetException, NoSuchAlgorithmException + { + return getInstance(service, algorithm, provider, NO_ARGS); + } + + /** + * Get the implementation for <i>algorithm</i> for service + * <i>service</i> from <i>provider</i>, passing <i>initArgs</i> to the + * SPI class's constructor (which cannot be null; pass a zero-length + * array if the SPI takes no arguments). The service is e.g. + * "Signature", and the algorithm "DSA". + * + * @param service The service name. + * @param algorithm The name of the algorithm to get. + * @param provider The provider to get the implementation from. + * @param initArgs The arguments to pass to the SPI class's + * constructor (cannot be null). + * @return The engine class for the specified algorithm; the object + * returned is typically a subclass of the SPI class for that + * service, but callers should check that this is so. + * @throws NoSuchAlgorithmException If the implementation cannot be + * found or cannot be instantiated. + * @throws InvocationTargetException If the SPI class's constructor + * throws an exception. + * @throws IllegalArgumentException If any of the four arguments are null. + */ + public static Object getInstance(String service, String algorithm, + Provider provider, Object[] initArgs) + throws InvocationTargetException, NoSuchAlgorithmException + { + if (service == null || algorithm == null + || provider == null || initArgs == null) + throw new IllegalArgumentException(); + + // If there is no property "service.algorithm" + if (provider.getProperty(service + "." + algorithm) == null) + { + // Iterate through aliases, until we find the class name or resolve + // too many aliases. + String alias = null; + int count = 0; + while ((alias = provider.getProperty( + ALG_ALIAS + service + "." + algorithm)) != null) + { + if (algorithm.equals(alias)) // Refers to itself! + break; + algorithm = alias; + if (count++ > MAX_ALIASES) + throw new NoSuchAlgorithmException("too many aliases"); + } + if (provider.getProperty(service + "." + algorithm) == null) + throw new NoSuchAlgorithmException(algorithm); + } + + // Find and instantiate the implementation. + Class clazz = null; + ClassLoader loader = provider.getClass().getClassLoader(); + Constructor constructor = null; + String error = algorithm; + + try + { + if (loader != null) + clazz = loader.loadClass(provider.getProperty(service+"."+algorithm)); + else + clazz = Class.forName(provider.getProperty(service+"."+algorithm)); + constructor = getCompatibleConstructor(clazz, initArgs); + return constructor.newInstance(initArgs); + } + catch (ClassNotFoundException cnfe) + { + error = "class not found: " + algorithm; + } + catch (IllegalAccessException iae) + { + error = "illegal access: " + iae.getMessage(); + } + catch (InstantiationException ie) + { + error = "instantiation exception: " + ie.getMessage(); + } + catch (ExceptionInInitializerError eiie) + { + error = "exception in initializer: " + eiie.getMessage(); + } + catch (SecurityException se) + { + error = "security exception: " + se.getMessage(); + } + catch (NoSuchMethodException nsme) + { + error = "no appropriate constructor found"; + } + + throw new NoSuchAlgorithmException(error); + } + + // Own methods. + // ------------------------------------------------------------------------ + + /** + * Find a constructor in the given class that can take the specified + * argument list, allowing any of which to be null. + * + * @param clazz The class from which to get the constructor. + * @param initArgs The argument list to be passed to the constructor. + * @return The constructor. + * @throws NoSuchMethodException If no constructor of the given class + * can take the specified argument array. + */ + private static Constructor getCompatibleConstructor(Class clazz, + Object[] initArgs) + throws NoSuchMethodException + { + Constructor[] c = clazz.getConstructors(); + outer:for (int i = 0; i < c.length; i++) + { + Class[] argTypes = c[i].getParameterTypes(); + if (argTypes.length != initArgs.length) + continue; + for (int j = 0; j < argTypes.length; j++) + { + if (initArgs[j] != null && + !argTypes[j].isAssignableFrom(initArgs[j].getClass())) + continue outer; + } + // If we reach this point, we know this constructor (c[i]) has + // the same number of parameters as the target parameter list, + // and all our parameters are either (1) null, or (2) assignable + // to the target parameter type. + return c[i]; + } + throw new NoSuchMethodException(); + } +} diff --git a/libjava/classpath/gnu/java/security/OID.java b/libjava/classpath/gnu/java/security/OID.java new file mode 100644 index 00000000000..8cda43eeee8 --- /dev/null +++ b/libjava/classpath/gnu/java/security/OID.java @@ -0,0 +1,509 @@ +/* OID.java -- numeric representation of an object identifier + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security; + +import gnu.java.security.der.DEREncodingException; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.StringTokenizer; + +/** + * This immutable class represents an object identifier, or OID. + * + * <p>OIDs are represented as a series of hierarcical tokens, each of + * which is usually represented as a single, unsigned integer. The + * hierarchy works so that later tokens are considered within the group + * of earlier tokens. Thus, the OID for the Serpent block cipher, + * 1.3.6.1.4.1.11591.13.2, is maintained by the GNU project, whose OID + * is 1.3.6.1.4.1.11591 (which is, in turn, part of bigger, more general + * bodies; the topmost, 1, stands for the OIDs assigned by the + * International Standards Organization, ISO). + * + * <p>OIDs can be represented in a variety of ways, including the + * dotted-decimal form we use here. + * + * <p>OIDs may be relative, in which case the first two elements of the + * OID are omitted. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class OID implements Cloneable, Comparable, java.io.Serializable +{ + + // Fields. + // ------------------------------------------------------------------------ + + /** + * The numeric ID structure. + */ + private int[] components; + + /** + * The string representation of this OID, in dotted-decimal format. + */ + private transient String strRep; + + /** + * The DER encoding of this OID. + */ + private transient byte[] der; + + /** + * Whether or not this OID is relative. + */ + private boolean relative; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new OID from the given byte array. The argument (which can + * neither be null nor zero-length) is copied to prevent subsequent + * modification. + * + * @param components The numeric IDs. + * @throws IllegalArgumentException If <i>components</i> is null or empty. + */ + public OID(int[] components) + { + this(components, false); + } + + /** + * Create a new OID from the given byte array. The argument (which can + * neither be null nor zero-length) is copied to prevent subsequent + * modification. + * + * @param components The numeric IDs. + * @param relative The relative flag. + * @throws IllegalArgumentException If <i>components</i> is null or empty. + */ + public OID(int[] components, boolean relative) + { + if (components == null || components.length == 0) + throw new IllegalArgumentException(); + this.components = (int[]) components.clone(); + this.relative = relative; + } + + /** + * Create a new OID from the given dotted-decimal representation. + * + * @param strRep The string representation of the OID. + * @throws IllegalArgumentException If the string does not contain at + * least one integer. + * @throws NumberFormatException If the string does not contain only + * numbers and periods ('.'). + */ + public OID(String strRep) + { + this(strRep, false); + } + + /** + * Create a new OID from the given dotted-decimal representation. + * + * @param strRep The string representation of the OID. + * @param relative The relative flag. + * @throws IllegalArgumentException If the string does not contain at + * least one integer. + * @throws NumberFormatException If the string does not contain only + * numbers and periods ('.'). + */ + public OID(String strRep, boolean relative) + { + this.relative = relative; + this.strRep = strRep; + components = fromString(strRep); + } + + /** + * Construct a new OID from the DER bytes in an input stream. This method + * does not read the tag or the length field from the input stream, so + * the caller must supply the number of octets in this OID's encoded + * form. + * + * @param derIn The DER input stream. + * @param len The number of bytes in the encoded form. + * @throws IOException If an error occurs reading the OID. + */ + public OID(InputStream derIn, int len) throws IOException + { + this(derIn, len, false); + } + + /** + * Construct a new OID from the DER bytes in an input stream. This method + * does not read the tag or the length field from the input stream, so + * the caller must supply the number of octets in this OID's encoded + * form. + * + * @param derIn The DER input stream. + * @param len The number of bytes in the encoded form. + * @param relative The relative flag. + * @throws IOException If an error occurs reading the OID. + */ + public OID(InputStream derIn, int len, boolean relative) throws IOException + { + der = new byte[len]; + derIn.read(der); + this.relative = relative; + try + { + components = fromDER(der, relative); + } + catch (ArrayIndexOutOfBoundsException aioobe) + { + aioobe.printStackTrace(); + throw aioobe; + } + } + + /** + * Construct a new OID from the given DER bytes. + * + * @param encoded The DER encoded OID. + * @throws IOException If an error occurs reading the OID. + */ + public OID(byte[] encoded) throws IOException + { + this(encoded, false); + } + + /** + * Construct a new OID from the given DER bytes. + * + * @param root The root OID. + * @param encoded The encoded relative OID. + * @param relative The relative flag. + */ + public OID(byte[] encoded, boolean relative) throws IOException + { + der = (byte[]) encoded.clone(); + this.relative = relative; + try + { + components = fromDER(der, relative); + } + catch (ArrayIndexOutOfBoundsException aioobe) + { + aioobe.printStackTrace(); + throw aioobe; + } + } + + /** + * Our private constructor. + */ + private OID() + { + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Return the numeric IDs of this OID. The value returned is copied to + * prevent modification. + * + * @return The IDs in a new integer array. + */ + public int[] getIDs() + { + return (int[]) components.clone(); + } + + /** + * Get the DER encoding of this OID, minus the tag and length fields. + * + * @return The DER bytes. + */ + public byte[] getDER() + { + if (der == null) + { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + int i = 0; + if (!relative) + { + int b = components[i++] * 40 + (components.length > 1 + ? components[i++] : 0); + encodeSubID(bout, b); + } + for ( ; i < components.length; i++) + encodeSubID(bout, components[i]); + der = bout.toByteArray(); + } + return (byte[]) der.clone(); + } + + /** + * Get the parent OID of this OID. That is, if this OID is "1.2.3.4", + * then the parent OID will be "1.2.3". If this OID is a top-level + * OID, this method returns null. + * + * @return The parent OID, or null. + */ + public OID getParent() + { + if (components.length == 1) + return null; + int[] parent = new int[components.length - 1]; + System.arraycopy(components, 0, parent, 0, parent.length); + return new OID(parent); + } + + public OID getChild(int id) + { + int[] child = new int[components.length + 1]; + System.arraycopy(components, 0, child, 0, components.length); + child[child.length - 1] = id; + return new OID(child); + } + + /** + * Get the root OID of this OID. That is, the first two components. + * + * @return The root OID. + */ + public OID getRoot() + { + if (components.length <= 2) + return this; + int[] root = new int[2]; + root[0] = components[0]; + root[1] = components[1]; + return new OID(root); + } + + public boolean isRelative() + { + return relative; + } + + /** + * Returns a copy of this OID. + * + * @return The copy. + */ + public Object clone() + { + OID oid = new OID(); + oid.components = this.components; + oid.strRep = this.strRep; + return oid; + } + + /* Nice idea, but possibly too expensive for whatever benefit it + * provides. + + public String getShortName() + { + return OIDTable.getShortName(this); + } + + public String getLongName() + { + return OIDTable.getLongName(this); + } + + */ + + /** + * Returns the value of this OID in dotted-decimal format. + * + * @return The string representation. + */ + public String toString() + { + if (strRep != null) + return strRep; + else + { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < components.length; i++) + { + buf.append((long) components[i] & 0xFFFFFFFFL); + if (i < components.length - 1) + buf.append('.'); + } + return (strRep = buf.toString()); + } + } + + /** + * Computes a hash code for this OID. + * + * @return The hash code. + */ + public int hashCode() + { + int ret = 0; + for (int i = 0; i < components.length; i++) + ret += components[i] << (i & 31); + return ret; + } + + /** + * Tests whether or not this OID equals another. + * + * @return Whether or not this OID equals the other. + */ + public boolean equals(Object o) + { + if (!(o instanceof OID)) + return false; + return java.util.Arrays.equals(components, ((OID) o).components); + } + + /** + * Compares this OID to another. The comparison is essentially + * lexicographic, where the two OIDs are compared until their + * first difference, then that difference is returned. If one OID is + * shorter, but all elements equal between the two for the shorter + * length, then the shorter OID is lesser than the longer. + * + * @param o The object to compare. + * @return An integer less than, equal to, or greater than zero if + * this object is less than, equal to, or greater than the + * argument. + * @throws ClassCastException If <i>o</i> is not an OID. + */ + public int compareTo(Object o) + { + if (equals(o)) + return 0; + int[] components2 = ((OID) o).components; + int len = Math.min(components.length, components2.length); + for (int i = 0; i < len; i++) + { + if (components[i] != components2[i]) + return (components[i] < components2[i]) ? -1 : 1; + } + if (components.length == components2.length) + return 0; + return (components.length < components2.length) ? -1 : 1; + } + + // Own methods. + // ------------------------------------------------------------------------ + + private static int[] fromDER(byte[] der, boolean relative) + throws DEREncodingException + { + // cannot be longer than this. + int[] components = new int[der.length + 1]; + int count = 0; + int i = 0; + if (!relative && i < der.length) + { + // Non-relative OIDs have the first two arcs coded as: + // + // i = first_arc * 40 + second_arc; + // + int j = (der[i] & 0xFF); + components[count++] = j / 40; + components[count++] = j % 40; + i++; + } + while (i < der.length) + { + int j = 0; + do + { + j = der[i++] & 0xFF; + components[count] <<= 7; + components[count] |= j & 0x7F; + if (i >= der.length && (j & 0x80) != 0) + throw new DEREncodingException("malformed OID"); + } + while ((j & 0x80) != 0); + count++; + } + if (count == components.length) + return components; + int[] ret = new int[count]; + System.arraycopy(components, 0, ret, 0, count); + return ret; + } + + private static int[] fromString(String strRep) throws NumberFormatException + { + if (strRep.startsWith("OID.") || strRep.startsWith("oid.")) + strRep = strRep.substring(4); + StringTokenizer tok = new StringTokenizer(strRep, "."); + if (tok.countTokens() == 0) + throw new IllegalArgumentException(); + int[] components = new int[tok.countTokens()]; + int i = 0; + while (tok.hasMoreTokens()) + { + components[i++] = Integer.parseInt(tok.nextToken()); + } + return components; + } + + private static void encodeSubID(ByteArrayOutputStream out, int id) + { + if (id < 128) + { + out.write(id); + } + else if (id < 16384) + { + out.write((id >>> 7) | 0x80); + out.write(id & 0x7F); + } + else if (id < 2097152) + { + out.write((id >>> 14) | 0x80); + out.write(((id >>> 7) | 0x80) & 0xFF); + out.write(id & 0x7F); + } + else if (id < 268435456) + { + out.write( (id >>> 21) | 0x80); + out.write(((id >>> 14) | 0x80) & 0xFF); + out.write(((id >>> 7) | 0x80) & 0xFF); + out.write(id & 0x7F); + } + } +} diff --git a/libjava/classpath/gnu/java/security/PolicyFile.java b/libjava/classpath/gnu/java/security/PolicyFile.java new file mode 100644 index 00000000000..dd3a4dec4c4 --- /dev/null +++ b/libjava/classpath/gnu/java/security/PolicyFile.java @@ -0,0 +1,667 @@ +/* PolicyFile.java -- policy file reader + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StreamTokenizer; +import java.lang.reflect.Constructor; +import java.net.MalformedURLException; +import java.net.URL; +import java.security.AccessController; +import java.security.CodeSource; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.security.Policy; +import java.security.Principal; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.security.Security; +import java.security.UnresolvedPermission; +import java.security.cert.Certificate; +import java.security.cert.X509Certificate; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + +/** + * An implementation of a {@link java.security.Policy} object whose + * permissions are specified by a <em>policy file</em>. + * + * <p>The approximate syntax of policy files is:</p> + * + * <pre> + * policyFile ::= keystoreOrGrantEntries ; + * + * keystoreOrGrantEntries ::= keystoreOrGrantEntry | + * keystoreOrGrantEntries keystoreOrGrantEntry | + * EMPTY ; + * + * keystoreOrGrantEntry ::= keystoreEntry | grantEntry ; + * + * keystoreEntry ::= "keystore" keystoreUrl ';' | + * "keystore" keystoreUrl ',' keystoreAlgorithm ';' ; + * + * keystoreUrl ::= URL ; + * keystoreAlgorithm ::= STRING ; + * + * grantEntry ::= "grant" domainParameters '{' permissions '}' ';' + * + * domainParameters ::= domainParameter | + * domainParameter ',' domainParameters ; + * + * domainParameter ::= "signedBy" signerNames | + * "codeBase" codeBaseUrl | + * "principal" principalClassName principalName | + * "principal" principalName ; + * + * signerNames ::= quotedString ; + * codeBaseUrl ::= URL ; + * principalClassName ::= STRING ; + * principalName ::= quotedString ; + * + * quotedString ::= quoteChar STRING quoteChar ; + * quoteChar ::= '"' | '\''; + * + * permissions ::= permission | permissions permission ; + * + * permission ::= "permission" permissionClassName permissionTarget permissionAction | + * "permission" permissionClassName permissionTarget | + * "permission" permissionClassName; + * </pre> + * + * <p>Comments are either form of Java comments. Keystore entries only + * affect subsequent grant entries, so if a grant entry preceeds a + * keystore entry, that grant entry is not affected by that keystore + * entry. Certian instances of <code>${property-name}</code> will be + * replaced with <code>System.getProperty("property-name")</code> in + * quoted strings.</p> + * + * <p>This class will load the following files when created or + * refreshed, in order:</p> + * + * <ol> + * <li>The file <code>${java.home}/lib/security/java.policy</code>.</li> + * <li>All URLs specified by security properties + * <code>"policy.file.<i>n</i>"</code>, for increasing <i>n</i> + * starting from 1. The sequence stops at the first undefined + * property, so you must set <code>"policy.file.1"</code> if you also + * set <code>"policy.file.2"</code>, and so on.</li> + * <li>The URL specified by the property + * <code>"java.security.policy"</code>.</li> + * </ol> + * + * @author Casey Marshall (csm@gnu.org) + * @see java.security.Policy + */ +public final class PolicyFile extends Policy +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + private static final boolean DEBUG = true; + // Package-private to avoid a trampoline. + static void debug(String msg) + { + System.err.print(">> PolicyFile: "); + System.err.println(msg); + } + + private static void debug(Throwable t) + { + System.err.println(">> PolicyFile"); + t.printStackTrace(System.err); + } + + private static final String DEFAULT_POLICY = System.getProperty("java.home") + + System.getProperty("file.separator") + "lib" + + System.getProperty("file.separator") + "security" + + System.getProperty("file.separator") + "java.policy"; + + private final Map cs2pc; + + // Constructors. + // ------------------------------------------------------------------------- + + public PolicyFile() + { + cs2pc = new HashMap(); + refresh(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public PermissionCollection getPermissions(CodeSource codeSource) + { + Permissions perms = new Permissions(); + for (Iterator it = cs2pc.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry e = (Map.Entry) it.next(); + CodeSource cs = (CodeSource) e.getKey(); + if (cs.implies(codeSource)) + { + if (DEBUG) debug(cs+" -> "+codeSource); + PermissionCollection pc = (PermissionCollection) e.getValue(); + for (Enumeration ee = pc.elements(); ee.hasMoreElements(); ) + { + perms.add((Permission) ee.nextElement()); + } + } + else + if (DEBUG) debug(cs+" !-> "+codeSource); + } + if (DEBUG) debug ("returning permissions " + perms + " for " + codeSource); + return perms; + } + + public void refresh() + { + cs2pc.clear(); + List policyFiles = new LinkedList(); + try + { + policyFiles.add(new File(DEFAULT_POLICY).toURL()); + if (DEBUG) debug ("defualt policy is " + DEFAULT_POLICY); + policyFiles.addAll((List) AccessController.doPrivileged( + new PrivilegedExceptionAction() + { + public Object run() throws Exception + { + LinkedList l = new LinkedList(); + for (int i = 1; ; i++) + { + String s = Security.getProperty("policy.file."+i); + if (DEBUG) debug("policy.file."+i+"="+s); + if (s == null) + break; + l.add(new URL(s)); + } + String s = System.getProperty("java.security.policy"); + if (DEBUG) debug("java.security.policy="+s); + if (s != null) + l.add(new URL(s)); + return l; + } + })); + } + catch (PrivilegedActionException pae) + { + if (DEBUG) debug(pae); + } + catch (MalformedURLException mue) + { + if (DEBUG) debug(mue); + } + for (Iterator it = policyFiles.iterator(); it.hasNext(); ) + { + try + { + URL url = (URL) it.next(); + parse(url); + } + catch (IOException ioe) + { + if (DEBUG) debug(ioe); + } + } + } + + public String toString() + { + return super.toString() + " [ " + cs2pc.toString() + " ]"; + } + + // Own methods. + // ------------------------------------------------------------------------- + + private static final int STATE_BEGIN = 0; + private static final int STATE_GRANT = 1; + private static final int STATE_PERMS = 2; + + /** + * Parse a policy file, incorporating the permission definitions + * described therein. + * + * @param url The URL of the policy file to read. + * @throws IOException if an I/O error occurs, or if the policy file + * cannot be parsed. + */ + private void parse(final URL url) throws IOException + { + if (DEBUG) debug ("reading policy file from " + url); + final StreamTokenizer in = new StreamTokenizer(new InputStreamReader(url.openStream())); + in.resetSyntax(); + in.slashSlashComments(true); + in.slashStarComments(true); + in.wordChars('A', 'Z'); + in.wordChars('a', 'z'); + in.wordChars('0', '9'); + in.wordChars('.', '.'); + in.wordChars('_', '_'); + in.wordChars('$', '$'); + in.whitespaceChars(' ', ' '); + in.whitespaceChars('\t', '\t'); + in.whitespaceChars('\f', '\f'); + in.whitespaceChars('\n', '\n'); + in.whitespaceChars('\r', '\r'); + in.quoteChar('\''); + in.quoteChar('"'); + + int tok; + int state = STATE_BEGIN; + List keystores = new LinkedList(); + URL currentBase = null; + List currentCerts = new LinkedList(); + Permissions currentPerms = new Permissions(); + while ((tok = in.nextToken()) != StreamTokenizer.TT_EOF) + { + switch (tok) + { + case '{': + if (state != STATE_GRANT) + error(url, in, "spurious '{'"); + state = STATE_PERMS; + tok = in.nextToken(); + break; + case '}': + if (state != STATE_PERMS) + error(url, in, "spurious '}'"); + state = STATE_BEGIN; + currentPerms.setReadOnly(); + Certificate[] c = null; + if (!currentCerts.isEmpty()) + c = (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()]); + cs2pc.put(new CodeSource(currentBase, c), currentPerms); + currentCerts.clear(); + currentPerms = new Permissions(); + currentBase = null; + tok = in.nextToken(); + if (tok != ';') + in.pushBack(); + continue; + } + if (tok != StreamTokenizer.TT_WORD) + { + error(url, in, "expecting word token"); + } + + // keystore "<keystore-path>" [',' "<keystore-type>"] ';' + if (in.sval.equalsIgnoreCase("keystore")) + { + String alg = KeyStore.getDefaultType(); + tok = in.nextToken(); + if (tok != '"' && tok != '\'') + error(url, in, "expecting key store URL"); + String store = in.sval; + tok = in.nextToken(); + if (tok == ',') + { + tok = in.nextToken(); + if (tok != '"' && tok != '\'') + error(url, in, "expecting key store type"); + alg = in.sval; + tok = in.nextToken(); + } + if (tok != ';') + error(url, in, "expecting semicolon"); + try + { + KeyStore keystore = KeyStore.getInstance(alg); + keystore.load(new URL(url, store).openStream(), null); + keystores.add(keystore); + } + catch (Exception x) + { + error(url, in, x.toString()); + } + } + else if (in.sval.equalsIgnoreCase("grant")) + { + if (state != STATE_BEGIN) + error(url, in, "extraneous grant keyword"); + state = STATE_GRANT; + } + else if (in.sval.equalsIgnoreCase("signedBy")) + { + if (state != STATE_GRANT && state != STATE_PERMS) + error(url, in, "spurious 'signedBy'"); + if (keystores.isEmpty()) + error(url, in, "'signedBy' with no keystores"); + tok = in.nextToken(); + if (tok != '"' && tok != '\'') + error(url, in, "expecting signedBy name"); + StringTokenizer st = new StringTokenizer(in.sval, ","); + while (st.hasMoreTokens()) + { + String alias = st.nextToken(); + for (Iterator it = keystores.iterator(); it.hasNext(); ) + { + KeyStore keystore = (KeyStore) it.next(); + try + { + if (keystore.isCertificateEntry(alias)) + currentCerts.add(keystore.getCertificate(alias)); + } + catch (KeyStoreException kse) + { + error(url, in, kse.toString()); + } + } + } + tok = in.nextToken(); + if (tok != ',') + { + if (state != STATE_GRANT) + error(url, in, "spurious ','"); + in.pushBack(); + } + } + else if (in.sval.equalsIgnoreCase("codeBase")) + { + if (state != STATE_GRANT) + error(url, in, "spurious 'codeBase'"); + tok = in.nextToken(); + if (tok != '"' && tok != '\'') + error(url, in, "expecting code base URL"); + String base = expand(in.sval); + if (File.separatorChar != '/') + base = base.replace(File.separatorChar, '/'); + try + { + currentBase = new URL(base); + } + catch (MalformedURLException mue) + { + error(url, in, mue.toString()); + } + tok = in.nextToken(); + if (tok != ',') + in.pushBack(); + } + else if (in.sval.equalsIgnoreCase("principal")) + { + if (state != STATE_GRANT) + error(url, in, "spurious 'principal'"); + tok = in.nextToken(); + if (tok == StreamTokenizer.TT_WORD) + { + tok = in.nextToken(); + if (tok != '"' && tok != '\'') + error(url, in, "expecting principal name"); + String name = in.sval; + Principal p = null; + try + { + Class pclass = Class.forName(in.sval); + Constructor c = + pclass.getConstructor(new Class[] { String.class }); + p = (Principal) c.newInstance(new Object[] { name }); + } + catch (Exception x) + { + error(url, in, x.toString()); + } + for (Iterator it = keystores.iterator(); it.hasNext(); ) + { + KeyStore ks = (KeyStore) it.next(); + try + { + for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) + { + String alias = (String) e.nextElement(); + if (ks.isCertificateEntry(alias)) + { + Certificate cert = ks.getCertificate(alias); + if (!(cert instanceof X509Certificate)) + continue; + if (p.equals(((X509Certificate) cert).getSubjectDN()) || + p.equals(((X509Certificate) cert).getSubjectX500Principal())) + currentCerts.add(cert); + } + } + } + catch (KeyStoreException kse) + { + error(url, in, kse.toString()); + } + } + } + else if (tok == '"' || tok == '\'') + { + String alias = in.sval; + for (Iterator it = keystores.iterator(); it.hasNext(); ) + { + KeyStore ks = (KeyStore) it.next(); + try + { + if (ks.isCertificateEntry(alias)) + currentCerts.add(ks.getCertificate(alias)); + } + catch (KeyStoreException kse) + { + error(url, in, kse.toString()); + } + } + } + else + error(url, in, "expecting principal"); + tok = in.nextToken(); + if (tok != ',') + in.pushBack(); + } + else if (in.sval.equalsIgnoreCase("permission")) + { + if (state != STATE_PERMS) + error(url, in, "spurious 'permission'"); + tok = in.nextToken(); + if (tok != StreamTokenizer.TT_WORD) + error(url, in, "expecting permission class name"); + String className = in.sval; + Class clazz = null; + try + { + clazz = Class.forName(className); + } + catch (ClassNotFoundException cnfe) + { + } + tok = in.nextToken(); + if (tok == ';') + { + if (clazz == null) + { + currentPerms.add(new UnresolvedPermission(className, + null, null, (Certificate[]) currentCerts.toArray(new Certificate[0]))); + continue; + } + try + { + currentPerms.add((Permission) clazz.newInstance()); + } + catch (Exception x) + { + error(url, in, x.toString()); + } + continue; + } + if (tok != '"' && tok != '\'') + error(url, in, "expecting permission target"); + String target = expand(in.sval); + tok = in.nextToken(); + if (tok == ';') + { + if (clazz == null) + { + currentPerms.add(new UnresolvedPermission(className, + target, null, (Certificate[]) currentCerts.toArray(new Certificate[0]))); + continue; + } + try + { + Constructor c = + clazz.getConstructor(new Class[] { String.class }); + currentPerms.add((Permission) c.newInstance( + new Object[] { target })); + } + catch (Exception x) + { + error(url, in, x.toString()); + } + continue; + } + if (tok != ',') + error(url, in, "expecting ','"); + tok = in.nextToken(); + if (tok == StreamTokenizer.TT_WORD) + { + if (!in.sval.equalsIgnoreCase("signedBy")) + error(url, in, "expecting 'signedBy'"); + try + { + Constructor c = + clazz.getConstructor(new Class[] { String.class }); + currentPerms.add((Permission) c.newInstance( + new Object[] { target })); + } + catch (Exception x) + { + error(url, in, x.toString()); + } + in.pushBack(); + continue; + } + if (tok != '"' && tok != '\'') + error(url, in, "expecting permission action"); + String action = in.sval; + if (clazz == null) + { + currentPerms.add(new UnresolvedPermission(className, + target, action, (Certificate[]) currentCerts.toArray(new Certificate[0]))); + continue; + } + else + { + try + { + Constructor c = clazz.getConstructor( + new Class[] { String.class, String.class }); + currentPerms.add((Permission) c.newInstance( + new Object[] { target, action })); + } + catch (Exception x) + { + error(url, in, x.toString()); + } + } + tok = in.nextToken(); + if (tok != ';' && tok != ',') + error(url, in, "expecting ';' or ','"); + } + } + } + + /** + * Expand all instances of <code>"${property-name}"</code> into + * <code>System.getProperty("property-name")</code>. + */ + private static String expand(final String s) + { + final StringBuffer result = new StringBuffer(); + final StringBuffer prop = new StringBuffer(); + int state = 0; + for (int i = 0; i < s.length(); i++) + { + switch (state) + { + case 0: + if (s.charAt(i) == '$') + state = 1; + else + result.append(s.charAt(i)); + break; + case 1: + if (s.charAt(i) == '{') + state = 2; + else + { + state = 0; + result.append('$').append(s.charAt(i)); + } + break; + case 2: + if (s.charAt(i) == '}') + { + String p = prop.toString(); + if (p.equals("/")) + p = "file.separator"; + p = System.getProperty(p); + if (p == null) + p = ""; + result.append(p); + prop.setLength(0); + state = 0; + } + else + prop.append(s.charAt(i)); + break; + } + } + if (state != 0) + result.append('$').append('{').append(prop); + return result.toString(); + } + + /** + * I miss macros. + */ + private static void error(URL base, StreamTokenizer in, String msg) + throws IOException + { + throw new IOException(base+":"+in.lineno()+": "+msg); + } +} diff --git a/libjava/classpath/gnu/java/security/action/GetPropertyAction.java b/libjava/classpath/gnu/java/security/action/GetPropertyAction.java new file mode 100644 index 00000000000..2886deb3474 --- /dev/null +++ b/libjava/classpath/gnu/java/security/action/GetPropertyAction.java @@ -0,0 +1,89 @@ +/* GetPropertyAction.java + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security.action; + +import java.security.PrivilegedAction; + +/** + * PrivilegedAction implementation that calls System.getProperty() with + * the property name passed to its constructor. + * + * Example of use: + * <code> + * GetPropertyAction action = new GetPropertyAction("http.proxyPort"); + * String port = AccessController.doPrivileged(action); + * </code> + */ +public class GetPropertyAction implements PrivilegedAction +{ + String name; + String value = null; + + public GetPropertyAction() + { + } + + public GetPropertyAction(String propName) + { + setParameters(propName); + } + + public GetPropertyAction(String propName, String defaultValue) + { + setParameters(propName, defaultValue); + } + + public Object run() + { + return System.getProperty(name, value); + } + + public GetPropertyAction setParameters(String propName) + { + this.name = propName; + this.value = null; + return this; + } + + public GetPropertyAction setParameters(String propName, String defaultValue) + { + this.name = propName; + this.value = defaultValue; + return this; + } +} diff --git a/libjava/classpath/gnu/java/security/action/GetSecurityPropertyAction.java b/libjava/classpath/gnu/java/security/action/GetSecurityPropertyAction.java new file mode 100644 index 00000000000..97fa15d03e5 --- /dev/null +++ b/libjava/classpath/gnu/java/security/action/GetSecurityPropertyAction.java @@ -0,0 +1,93 @@ +/* GetSecurityPropertyAction.java + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security.action; + +import java.security.PrivilegedAction; +import java.security.Security; + +/** + * PrivilegedAction implementation that calls Security.getProperty() + * with the property name passed to its constructor. + * + * Example of use: + * <code> + * GetSecurityPropertyAction action = new GetSecurityPropertyAction("javax.net.ssl.trustStorePassword"); + * String passwd = AccessController.doPrivileged(action); + * </code> + */ +public class GetSecurityPropertyAction implements PrivilegedAction +{ + private String name; + private String value; + + public GetSecurityPropertyAction() + { + } + + public GetSecurityPropertyAction(String propName) + { + setParameters(propName); + } + + public GetSecurityPropertyAction(String propName, String defaultValue) + { + setParameters(propName, defaultValue); + } + + public GetSecurityPropertyAction setParameters(String propName) + { + this.name = propName; + this.value = null; + return this; + } + + public GetSecurityPropertyAction setParameters(String propName, String defaultValue) + { + this.name = propName; + this.value = defaultValue; + return this; + } + + public Object run() + { + String val = Security.getProperty(name); + if (val == null) + val = value; + return val; + } +} diff --git a/libjava/classpath/gnu/java/security/action/SetAccessibleAction.java b/libjava/classpath/gnu/java/security/action/SetAccessibleAction.java new file mode 100644 index 00000000000..77e5fc988b8 --- /dev/null +++ b/libjava/classpath/gnu/java/security/action/SetAccessibleAction.java @@ -0,0 +1,77 @@ +/* SetAccessibleAction.java + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security.action; + +import java.lang.reflect.AccessibleObject; +import java.security.PrivilegedAction; + +/** + * PrivilegedAction implementation that calls setAccessible(true) on the + * AccessibleObject passed to its constructor. + * + * Example of use: + * <code> + * Field dataField = cl.getDeclaredField("data"); + * AccessController.doPrivileged(new SetAccessibleAction(dataField)); + * </code> + */ +public class SetAccessibleAction implements PrivilegedAction +{ + AccessibleObject member; + + public SetAccessibleAction() + { + } + + public SetAccessibleAction(AccessibleObject member) + { + this.member = member; + } + + public Object run() + { + member.setAccessible(true); + return null; + } + + public SetAccessibleAction setMember(AccessibleObject member) + { + this.member = member; + return this; + } +} diff --git a/libjava/classpath/gnu/java/security/action/package.html b/libjava/classpath/gnu/java/security/action/package.html new file mode 100644 index 00000000000..fc3dfa2293f --- /dev/null +++ b/libjava/classpath/gnu/java/security/action/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.action package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.action</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/ber/BER.java b/libjava/classpath/gnu/java/security/ber/BER.java new file mode 100644 index 00000000000..7efb1bf90cc --- /dev/null +++ b/libjava/classpath/gnu/java/security/ber/BER.java @@ -0,0 +1,46 @@ +/* BER.java -- basic encoding rules (BER) constants. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.ber; + +import gnu.java.security.der.DER; + +public interface BER extends DER +{ + BERValue END_OF_SEQUENCE = new BERValue(0, null); +} diff --git a/libjava/classpath/gnu/java/security/ber/BEREncodingException.java b/libjava/classpath/gnu/java/security/ber/BEREncodingException.java new file mode 100644 index 00000000000..aad10932cc4 --- /dev/null +++ b/libjava/classpath/gnu/java/security/ber/BEREncodingException.java @@ -0,0 +1,54 @@ +/* BEREncodingException.java --- BER Encoding Exception + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.ber; + +import gnu.java.security.der.DEREncodingException; + +public class BEREncodingException extends DEREncodingException +{ + public BEREncodingException() + { + super (); + } + + public BEREncodingException (String msg) + { + super (msg); + } +} diff --git a/libjava/classpath/gnu/java/security/ber/BERReader.java b/libjava/classpath/gnu/java/security/ber/BERReader.java new file mode 100644 index 00000000000..53a3f3ee9a9 --- /dev/null +++ b/libjava/classpath/gnu/java/security/ber/BERReader.java @@ -0,0 +1,103 @@ +/* BERReader.java -- basic encoding rules (BER) reader. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.ber; + +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; + +public class BERReader extends DERReader implements BER +{ + + /** + * Create a new DER reader from a byte array. + * + * @param in The encoded bytes. + */ + public BERReader(byte[] in) + { + super(in); + } + + public BERReader (byte[] in, int off, int len) + { + super(in, off, len); + } + + /** + * Create a new DER readed from an input stream. + * + * @param in The encoded bytes. + */ + public BERReader(InputStream in) + { + super(in); + } + + public DERValue read() throws IOException + { + in.mark(2); + int tag = in.read(); + if (tag == -1) + throw new EOFException(); + int length = in.read(); + if (length == 0) + { + if (tag == 0) + return END_OF_SEQUENCE; + return new BERValue(tag, CONSTRUCTED_VALUE, new byte[] { (byte) tag, 0 }); + } + else + { + in.reset(); + return super.read(); + } + } + + public int peek() throws IOException + { + in.mark(1); + int ret = in.read(); + in.reset(); + return ret; + } +} diff --git a/libjava/classpath/gnu/java/security/ber/BERValue.java b/libjava/classpath/gnu/java/security/ber/BERValue.java new file mode 100644 index 00000000000..aeaef39bf2b --- /dev/null +++ b/libjava/classpath/gnu/java/security/ber/BERValue.java @@ -0,0 +1,82 @@ +/* BERReader.java -- basic encoding rules (BER) value. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.ber; + +import gnu.java.security.der.DERValue; + +public class BERValue extends DERValue +{ + + private boolean indefinite; + + public BERValue(int tag, Object value, byte[] encoded) + { + super(tag, 0, value, encoded); + indefinite = true; + } + + public BERValue(int tag, int length, Object value, byte[] encoded) + { + super(tag, length, value, encoded); + } + + public BERValue(int tag, Object value) + { + super(tag, 0, value, null); + } + + public static boolean isIndefinite(DERValue value) + { + if (value instanceof BERValue) + return ((BERValue) value).getIndefinite(); + return false; + } + + public boolean getIndefinite() + { + return indefinite; + } + + public int getLength() + { + if (indefinite) + return 0; + return super.getLength(); + } +} diff --git a/libjava/classpath/gnu/java/security/ber/package.html b/libjava/classpath/gnu/java/security/ber/package.html new file mode 100644 index 00000000000..348a83c2033 --- /dev/null +++ b/libjava/classpath/gnu/java/security/ber/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.ber package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.ber</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/der/BitString.java b/libjava/classpath/gnu/java/security/der/BitString.java new file mode 100644 index 00000000000..b88b14541b6 --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/BitString.java @@ -0,0 +1,317 @@ +/* BitString.java -- Java representation of the BIT STRING type. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.der; + +import java.math.BigInteger; +import java.util.Arrays; + +/** + * Immutable representation of a bit string, which is equivalent to a + * byte array except some number of the rightmost bits are ignored. For + * example, this could be the bit string: + * + * <pre> 00010101 11101101 11010xxx</pre> + * + * <p>Where the "xxx" represents three bits that should be ignored, and + * can have any value. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class BitString implements Cloneable, Comparable +{ + + // Fields. + // ------------------------------------------------------------------------ + + /** The bits themselves. */ + private final byte[] bytes; + + /** + * The exportable byte array. This array has the ignored bits + * removed. + */ + private transient byte[] externBytes; + + /** The number of bits ignored at the end of the byte array. */ + private final int ignoredBits; + + /** This bit string as a boolean array. */ + private transient boolean[] boolVal; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new bit string, shifting the given byte array if needed. + * + * @param bytes The byte array holding the bit string. + * @param ignoredBits The number of bits to ignore. + * @param doShift Pass true in this parameter if the byte array has + * not yet been shifted left by <i>ignoredBits</i>. + * @throws IllegalArgumentException If <i>ignoredBits</i> is negative + * or greater than 7. + * @throws NullPointerException If <i>bytes</i> is null. + */ + public BitString(byte[] bytes, int ignoredBits, boolean doShift) + { + this(bytes, 0, bytes.length, ignoredBits, doShift); + } + + /** + * Create a new bit string, shifting the given byte array if needed. + * + * @param bytes The byte array holding the bit string. + * @param offset The offset where the meaningful bytes begin. + * @param length The number of meaningful bytes. + * @param ignoredBits The number of bits to ignore. + * @param doShift Pass true in this parameter if the byte array has + * not yet been shifted left by <i>ignoredBits</i>. + * @throws IllegalArgumentException If <i>ignoredBits</i> is negative + * or greater than 7. + * @throws NullPointerException If <i>bytes</i> is null. + */ + public BitString(byte[] bytes, int offset, int length, + int ignoredBits, boolean doShift) + { + if (ignoredBits < 0 || ignoredBits > 7) + throw new IllegalArgumentException(); + if (bytes == null) + throw new NullPointerException(); + if (doShift && ignoredBits > 0) + { + this.externBytes = new byte[length]; + System.arraycopy(bytes, offset, externBytes, 0, length); + this.bytes = new BigInteger(externBytes).shiftLeft(ignoredBits) + .toByteArray(); + } + else + { + this.bytes = new byte[length]; + System.arraycopy(bytes, offset, this.bytes, 0, length); + } + this.ignoredBits = ignoredBits; + } + + /** + * Create a new bit string. + * + * @param bytes The byte array holding the bit string. + * @param offset The offset where the meaningful bytes begin. + * @param length The number of meaningful bytes. + * @param ignoredBits The number of bits to ignore. + * @throws IllegalArgumentException If <i>ignoredBits</i> is negative + * or greater than 7. + * @throws NullPointerException If <i>bytes</i> is null. + */ + public BitString(byte[] bytes, int offset, int length, int ignoredBits) + { + this(bytes, offset, length, ignoredBits, false); + } + + /** + * Create a new bit string. + * + * @param bytes The byte array holding the bit string. + * @param ignoredBits The number of bits to ignore. + * @throws IllegalArgumentException If <i>ignoredBits</i> is negative + * or greater than 7. + * @throws NullPointerException If <i>bytes</i> is null. + */ + public BitString(byte[] bytes, int ignoredBits) + { + this(bytes, 0, bytes.length, ignoredBits, false); + } + + /** + * Create a new bit string. + * + * @param bytes The byte array holding the bit string. + * @param offset The offset where the meaningful bytes begin. + * @param length The number of meaningful bytes. + * @throws NullPointerException If <i>bytes</i> is null. + */ + public BitString(byte[] bytes, int offset, int length) + { + this(bytes, offset, length, 0, false); + } + + /** + * Create a new bit string. + * + * @param bytes The byte array holding the bit string. + * @throws NullPointerException If <i>bytes</i> is null. + */ + public BitString(byte[] bytes) + { + this(bytes, 0, bytes.length, 0, false); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + /** + * Return this bit string as a byte array, with the ignored bits + * trimmed off. The byte array is cloned every time this method is + * called to prevent modification. + * + * @return The trimmed byte array. + */ + public byte[] toByteArray() + { + if (ignoredBits == 0) + return (byte[]) bytes.clone(); + if (externBytes == null) + externBytes = new BigInteger(bytes).shiftRight(ignoredBits).toByteArray(); + return (byte[]) externBytes.clone(); + } + + /** + * Returns this bit string as a byte array, with the ignored bits + * present. The byte array is cloned every time this method is + * called to prevent modification. + * + * @return The byte array. + */ + public byte[] getShiftedByteArray() + { + return (byte[]) bytes.clone(); + } + + /** + * Returns the number of ignored bits. + * + * @return The number of ignored bits. + */ + public int getIgnoredBits() + { + return ignoredBits; + } + + /** + * Returns the size, in bits, of this bit string. + * + * @return The size of this bit string. + */ + public int size() + { + return (bytes.length << 3) - ignoredBits; + } + + /** + * Return this bit string as a boolean array. The value returned is of + * size {@link #size()}, and each <code>true</code> value + * corresponding to each "1" in this bit string. The boolean array is + * cloned before it is returned. + * + * @return The boolean array. + */ + public boolean[] toBooleanArray() + { + if (boolVal == null) + { + boolVal = new boolean[size()]; + for (int i = 0, j = 7, k = 0; i < boolVal.length; i++) + { + boolVal[i] = (bytes[k] & 1 << j--) != 0; + if (j < 0) + { + j = 7; + k++; + } + } + } + return (boolean[]) boolVal.clone(); + } + + public Object clone() + { + try + { + return super.clone(); + } + catch (CloneNotSupportedException cce) + { + throw new InternalError(cce.getMessage()); + } + } + + public int compareTo(Object o) + { + BitString that = (BitString) o; + if (this.equals(that)) + return 0; + if (this.bytes.length != that.bytes.length) + return (this.bytes.length < that.bytes.length) ? -1 : 1; + if (this.ignoredBits != that.ignoredBits) + return (this.ignoredBits < that.ignoredBits) ? -1 : 1; + for (int i = 0; i < this.bytes.length; i++) + if (this.bytes[i] != that.bytes[i]) + return (this.bytes[i] < that.bytes[i]) ? -1 : 1; + return 0; // not reached. + } + + public boolean equals(Object o) + { + if (!(o instanceof BitString)) + return false; + BitString that = (BitString) o; + // True for cloned instances. + if (this.bytes == that.bytes && this.ignoredBits == that.ignoredBits) + return true; + if (this.ignoredBits == that.ignoredBits) + return Arrays.equals(this.bytes, that.bytes); + return false; + } + + public String toString() + { + StringBuffer sb = new StringBuffer(); + for (int i = 0, j = 7, k = 0; i < size(); i++) + { + sb.append((bytes[k] & 1 << j) != 0 ? "1" : "0"); + j--; + if (j < 0) + { + j = 7; + k++; + } + } + return sb.toString(); + } +} diff --git a/libjava/classpath/gnu/java/security/der/DER.java b/libjava/classpath/gnu/java/security/der/DER.java new file mode 100644 index 00000000000..a7eb4a6895e --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/DER.java @@ -0,0 +1,86 @@ +/* DER.java -- Basic constants in DER sequences. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.der; + +/** + * The set of tags for DER types. + * + * @author Casey Marshall (csm@gnu.org) + */ +public interface DER +{ + int UNIVERSAL = 0x00; + int APPLICATION = 0x40; + int CONTEXT = 0x80; + int PRIVATE = 0xC0; + + int CONSTRUCTED = 0x20; + + int ANY = 0x00; + int BOOLEAN = 0x01; + int INTEGER = 0x02; + int BIT_STRING = 0x03; + int OCTET_STRING = 0x04; + int NULL = 0x05; + int OBJECT_IDENTIFIER = 0x06; + int REAL = 0x09; + int ENUMERATED = 0x0a; + int RELATIVE_OID = 0x0d; + + int SEQUENCE = 0x10; + int SET = 0x11; + + Object CONSTRUCTED_VALUE = new Object(); + + int NUMERIC_STRING = 0x12; + int PRINTABLE_STRING = 0x13; + int T61_STRING = 0x14; + int VIDEOTEX_STRING = 0x15; + int IA5_STRING = 0x16; + int GRAPHIC_STRING = 0x19; + int ISO646_STRING = 0x1A; + int GENERAL_STRING = 0x1B; + + int UTF8_STRING = 0x0C; + int UNIVERSAL_STRING = 0x1C; + int BMP_STRING = 0x1E; + + int UTC_TIME = 0x17; + int GENERALIZED_TIME = 0x18; +} diff --git a/libjava/classpath/gnu/java/security/der/DEREncodingException.java b/libjava/classpath/gnu/java/security/der/DEREncodingException.java new file mode 100644 index 00000000000..90042a3fc54 --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/DEREncodingException.java @@ -0,0 +1,54 @@ +/* DEREncodingException.java --- DER Encoding Exception + Copyright (C) 1999,2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.der; + +import java.io.IOException; + +public class DEREncodingException extends IOException +{ + public DEREncodingException() + { + super (); + } + + public DEREncodingException (String msg) + { + super (msg); + } +} diff --git a/libjava/classpath/gnu/java/security/der/DERReader.java b/libjava/classpath/gnu/java/security/der/DERReader.java new file mode 100644 index 00000000000..cb07f14325f --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/DERReader.java @@ -0,0 +1,437 @@ +/* DERReader.java -- parses ASN.1 DER sequences + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.der; + +import gnu.java.security.OID; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +/** + * This class decodes DER sequences into Java objects. The methods of + * this class do not have knowledge of higher-levels of structure in the + * DER stream -- such as ASN.1 constructions -- and it is therefore up + * to the calling application to determine if the data are structured + * properly by inspecting the {@link DERValue} that is returned. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class DERReader implements DER +{ + + // Fields. + // ------------------------------------------------------------------------ + + protected InputStream in; + + protected final ByteArrayOutputStream encBuf; + + // Constructor. + // ------------------------------------------------------------------------ + + /** + * Create a new DER reader from a byte array. + * + * @param in The encoded bytes. + */ + public DERReader(byte[] in) + { + this(new ByteArrayInputStream(in)); + } + + public DERReader (byte[] in, int off, int len) + { + this (new ByteArrayInputStream (in, off, len)); + } + + /** + * Create a new DER readed from an input stream. + * + * @param in The encoded bytes. + */ + public DERReader(InputStream in) + { + if (!in.markSupported()) + this.in = new BufferedInputStream(in, 16384); + else + this.in = in; + encBuf = new ByteArrayOutputStream(2048); + } + + // Class methods. + // ------------------------------------------------------------------------ + + /** + * Convenience method for reading a single primitive value from the + * given byte array. + * + * @param encoded The encoded bytes. + * @throws IOException If the bytes do not represent an encoded + * object. + */ + public static DERValue read(byte[] encoded) throws IOException + { + return new DERReader(encoded).read(); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + public void skip (int bytes) throws IOException + { + in.skip (bytes); + } + + /** + * Decode a single value from the input stream, returning it in a new + * {@link DERValue}. By "single value" we mean any single type in its + * entirety -- including constructed types such as SEQUENCE and all + * the values they contain. Usually it is sufficient to call this + * method once to parse and return the top-level structure, then to + * inspect the returned value for the proper contents. + * + * @return The parsed DER structure. + * @throws IOException If an error occurs reading from the input + * stream. + * @throws DEREncodingException If the input does not represent a + * valid DER stream. + */ + public DERValue read() throws IOException + { + int tag = in.read(); + if (tag == -1) + throw new EOFException(); + encBuf.write(tag); + int len = readLength(); + DERValue value = null; + if ((tag & CONSTRUCTED) == CONSTRUCTED) + { + in.mark(2048); + byte[] encoded = new byte[len]; + in.read(encoded); + encBuf.write(encoded); + value = new DERValue(tag, len, CONSTRUCTED_VALUE, encBuf.toByteArray()); + in.reset(); + encBuf.reset(); + return value; + } + switch (tag & 0xC0) + { + case UNIVERSAL: + value = new DERValue(tag, len, readUniversal(tag, len), + encBuf.toByteArray()); + encBuf.reset(); + break; + case CONTEXT: + byte[] encoded = new byte[len]; + in.read(encoded); + encBuf.write(encoded); + value = new DERValue(tag, len, encoded, encBuf.toByteArray()); + encBuf.reset(); + break; + case APPLICATION: + // This should not be reached, since (I think) APPLICATION is + // always constructed. + throw new DEREncodingException("non-constructed APPLICATION data"); + default: + throw new DEREncodingException("PRIVATE class not supported"); + } + return value; + } + + protected int readLength() throws IOException + { + int i = in.read(); + if (i == -1) + throw new EOFException(); + encBuf.write(i); + if ((i & ~0x7F) == 0) + { + return i; + } + else if (i < 0xFF) + { + byte[] octets = new byte[i & 0x7F]; + in.read(octets); + encBuf.write(octets); + return new BigInteger(1, octets).intValue(); + } + throw new DEREncodingException(); + } + + // Own methods. + // ------------------------------------------------------------------------ + + private Object readUniversal(int tag, int len) throws IOException + { + byte[] value = new byte[len]; + in.read(value); + encBuf.write(value); + switch (tag & 0x1F) + { + case BOOLEAN: + if (value.length != 1) + throw new DEREncodingException(); + return Boolean.valueOf(value[0] != 0); + case NULL: + if (len != 0) + throw new DEREncodingException(); + return null; + case INTEGER: + case ENUMERATED: + return new BigInteger(value); + case BIT_STRING: + byte[] bits = new byte[len - 1]; + System.arraycopy(value, 1, bits, 0, bits.length); + return new BitString(bits, value[0] & 0xFF); + case OCTET_STRING: + return value; + case NUMERIC_STRING: + case PRINTABLE_STRING: + case T61_STRING: + case VIDEOTEX_STRING: + case IA5_STRING: + case GRAPHIC_STRING: + case ISO646_STRING: + case GENERAL_STRING: + case UNIVERSAL_STRING: + case BMP_STRING: + case UTF8_STRING: + return makeString(tag, value); + case UTC_TIME: + case GENERALIZED_TIME: + return makeTime(tag, value); + case OBJECT_IDENTIFIER: + return new OID(value); + case RELATIVE_OID: + return new OID(value, true); + default: + throw new DEREncodingException("unknown tag " + tag); + } + } + + private static String makeString(int tag, byte[] value) + throws IOException + { + switch (tag & 0x1F) + { + case NUMERIC_STRING: + case PRINTABLE_STRING: + case T61_STRING: + case VIDEOTEX_STRING: + case IA5_STRING: + case GRAPHIC_STRING: + case ISO646_STRING: + case GENERAL_STRING: + return fromIso88591(value); + + case UNIVERSAL_STRING: + // XXX The docs say UniversalString is encoded in four bytes + // per character, but Java has no support (yet) for UTF-32. + //return new String(buf, "UTF-32"); + case BMP_STRING: + return fromUtf16Be(value); + + case UTF8_STRING: + return fromUtf8(value); + + default: + throw new DEREncodingException("unknown string tag"); + } + } + + private static String fromIso88591(byte[] bytes) + { + StringBuffer str = new StringBuffer(bytes.length); + for (int i = 0; i < bytes.length; i++) + str.append((char) (bytes[i] & 0xFF)); + return str.toString(); + } + + private static String fromUtf16Be(byte[] bytes) throws IOException + { + if ((bytes.length & 0x01) != 0) + throw new IOException("UTF-16 bytes are odd in length"); + StringBuffer str = new StringBuffer(bytes.length / 2); + for (int i = 0; i < bytes.length; i += 2) + { + char c = (char) ((bytes[i] << 8) & 0xFF); + c |= (char) (bytes[i+1] & 0xFF); + str.append(c); + } + return str.toString(); + } + + private static String fromUtf8(byte[] bytes) throws IOException + { + StringBuffer str = new StringBuffer((int)(bytes.length / 1.5)); + for (int i = 0; i < bytes.length; ) + { + char c = 0; + if ((bytes[i] & 0xE0) == 0xE0) + { + if ((i + 2) >= bytes.length) + throw new IOException("short UTF-8 input"); + c = (char) ((bytes[i++] & 0x0F) << 12); + if ((bytes[i] & 0x80) != 0x80) + throw new IOException("malformed UTF-8 input"); + c |= (char) ((bytes[i++] & 0x3F) << 6); + if ((bytes[i] & 0x80) != 0x80) + throw new IOException("malformed UTF-8 input"); + c |= (char) (bytes[i++] & 0x3F); + } + else if ((bytes[i] & 0xC0) == 0xC0) + { + if ((i + 1) >= bytes.length) + throw new IOException("short input"); + c = (char) ((bytes[i++] & 0x1F) << 6); + if ((bytes[i] & 0x80) != 0x80) + throw new IOException("malformed UTF-8 input"); + c |= (char) (bytes[i++] & 0x3F); + } + else if ((bytes[i] & 0xFF) < 0x80) + { + c = (char) (bytes[i++] & 0xFF); + } + else + throw new IOException("badly formed UTF-8 sequence"); + str.append(c); + } + return str.toString(); + } + + private Date makeTime(int tag, byte[] value) throws IOException + { + Calendar calendar = Calendar.getInstance(); + String str = makeString(PRINTABLE_STRING, value); + + // Classpath's SimpleDateFormat does not work for parsing these + // types of times, so we do this by hand. + String date = str; + String tz = ""; + if (str.indexOf("+") > 0) + { + date = str.substring(0, str.indexOf("+")); + tz = str.substring(str.indexOf("+")); + } + else if (str.indexOf("-") > 0) + { + date = str.substring(0, str.indexOf("-")); + tz = str.substring(str.indexOf("-")); + } + else if (str.endsWith("Z")) + { + date = str.substring(0, str.length()-2); + tz = "Z"; + } + if (!tz.equals("Z") && tz.length() > 0) + calendar.setTimeZone(TimeZone.getTimeZone(tz)); + else + calendar.setTimeZone(TimeZone.getTimeZone("UTC")); + if ((tag & 0x1F) == UTC_TIME) + { + if (date.length() < 10) // must be at least 10 chars long + throw new DEREncodingException("cannot parse date"); + // UTCTime is of the form "yyMMddHHmm[ss](Z|(+|-)hhmm)" + try + { + int year = Integer.parseInt(str.substring(0, 2)); + if (year < 50) + year += 2000; + else + year += 1900; + calendar.set(year, + Integer.parseInt(str.substring( 2, 4))-1, // month + Integer.parseInt(str.substring( 4, 6)), // day + Integer.parseInt(str.substring( 6, 8)), // hour + Integer.parseInt(str.substring( 8, 10))); // minute + if (date.length() == 12); + calendar.set(Calendar.SECOND, + Integer.parseInt(str.substring(10, 12))); + } + catch (NumberFormatException nfe) + { + throw new DEREncodingException("cannot parse date"); + } + } + else + { + if (date.length() < 10) // must be at least 10 chars long + throw new DEREncodingException("cannot parse date"); + // GeneralTime is of the form "yyyyMMddHH[mm[ss[(.|,)SSSS]]]" + // followed by "Z" or "(+|-)hh[mm]" + try + { + calendar.set( + Integer.parseInt(date.substring(0, 4)), // year + Integer.parseInt(date.substring(4, 6))-1, // month + Integer.parseInt(date.substring(6, 8)), // day + Integer.parseInt(date.substring(8, 10)), 0); // hour, min + switch (date.length()) + { + case 19: + case 18: + case 17: + case 16: + calendar.set(Calendar.MILLISECOND, + Integer.parseInt(date.substring(15))); + case 14: + calendar.set(Calendar.SECOND, + Integer.parseInt(date.substring(12, 14))); + case 12: + calendar.set(Calendar.MINUTE, + Integer.parseInt(date.substring(10, 12))); + } + } + catch (NumberFormatException nfe) + { + throw new DEREncodingException("cannot parse date"); + } + } + return calendar.getTime(); + } +} diff --git a/libjava/classpath/gnu/java/security/der/DERValue.java b/libjava/classpath/gnu/java/security/der/DERValue.java new file mode 100644 index 00000000000..9a597d724cc --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/DERValue.java @@ -0,0 +1,170 @@ +/* DERValue.java -- a value read or written to a DER encoding. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.der; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class DERValue implements DER +{ + + // Fields. + // ------------------------------------------------------------------------ + + private final int tagClass; + private final boolean constructed; + private final int tag; + private int length; + private final Object value; + private byte[] encoded; + + // Constructor. + // ------------------------------------------------------------------------ + + public DERValue(int tag, int length, Object value, byte[] encoded) + { + tagClass = tag & 0xC0; + this.tag = tag & 0x1F; + constructed = (tag & CONSTRUCTED) == CONSTRUCTED; + this.length = length; + this.value = value; + if (encoded != null) + this.encoded = (byte[]) encoded.clone(); + } + + public DERValue(int tag, Object value) + { + this(tag, 0, value, null); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + public int getExternalTag() + { + return tagClass | tag | (constructed ? 0x20 : 0x00); + } + + public int getTag() + { + return tag; + } + + public int getTagClass() + { + return tagClass; + } + + public boolean isConstructed() + { + return constructed; + } + + public int getLength() + { + if (encoded == null) + { + try + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + length = DERWriter.write(out, this); + encoded = out.toByteArray(); + } + catch (IOException ioe) + { + encoded = new byte[0]; + } + } + return length; + } + + public Object getValue() + { + return value; + } + + public Object getValueAs (final int derType) throws IOException + { + byte[] encoded = getEncoded (); + encoded[0] = (byte) derType; + return DERReader.read (encoded).getValue (); + } + + public byte[] getEncoded() + { + if (encoded == null) + { + try + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + length = DERWriter.write(out, this); + encoded = out.toByteArray(); + } + catch (IOException ioe) + { + encoded = new byte[0]; + } + } + return (byte[]) encoded.clone(); + } + + public int getEncodedLength() + { + if (encoded == null) + { + try + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + length = DERWriter.write(out, this); + encoded = out.toByteArray(); + } + catch (IOException ioe) + { + encoded = new byte[0]; + } + } + return encoded.length; + } + + public String toString() + { + return "DERValue [ tag=" + tag + ", class=" + tagClass + ", constructed=" + + constructed + ", value=" + value + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/der/DERWriter.java b/libjava/classpath/gnu/java/security/der/DERWriter.java new file mode 100644 index 00000000000..78524fc949f --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/DERWriter.java @@ -0,0 +1,349 @@ +/* DERWriter.java -- write Java types in DER format. + Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.der; + +import gnu.java.security.OID; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.math.BigInteger; + +import java.text.SimpleDateFormat; + +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.TimeZone; + +/** + * Methods that allow various Java types to be written as a DER + * (Distinguished Encoding Rules) stream to the specified output stream. + * DER is used to encode ASN.1 constructions, but this class provides no + * methods for interacting with ASN.1. Rather, callers should construct + * their output objects properly for whatever ASN.1 construct is being + * output. + * + * <p>This class only defines static methods; there are no instance + * variables needed. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class DERWriter implements DER +{ + + // Constructors. + // ------------------------------------------------------------------------ + + /** This class only has static methods. */ + private DERWriter() + { + } + + // Class methods. + // ------------------------------------------------------------------------ + + public static int write(OutputStream out, DERValue object) + throws IOException + { + out.write(object.getExternalTag()); + Object value = object.getValue(); + if (value == null) + { + writeLength(out, 0); + return 0; + } + if (value instanceof Boolean) + return writeBoolean(out, (Boolean) value); + else if (value instanceof BigInteger) + return writeInteger(out, (BigInteger) value); + else if (value instanceof Date) + return writeDate(out, object.getExternalTag(), (Date) value); + else if (value instanceof String) + return writeString(out, object.getExternalTag(), (String) value); + else if (value instanceof List) + return writeSequence(out, (List) value); + else if (value instanceof Set) + return writeSet(out, (Set) value); + else if (value instanceof BitString) + return writeBitString(out, (BitString) value); + else if (value instanceof OID) + return writeOID(out, (OID) value); + else if (value instanceof byte[]) + { + writeLength(out, ((byte[]) value).length); + out.write((byte[]) value); + return ((byte[]) value).length; + } + else if (value instanceof DERValue) + { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + write(bout, (DERValue) value); + byte[] buf = bout.toByteArray(); + writeLength(out, buf.length); + out.write(buf); + return buf.length; + } + else + throw new DEREncodingException("cannot encode " + value.getClass().getName()); + } + + public static int definiteEncodingSize(int length) + { + if (length < 128) + return 1; + else if (length < 256) + return 2; + else if (length < 65536) + return 3; + else if (length < 16777216) + return 4; + else + return 5; + } + + // Own methods. + // ------------------------------------------------------------------------ + + /** + * Write a BOOLEAN type to the given output stream. + * + * @param out The sink output stream. + * @param b The boolean value to write. + */ + private static int writeBoolean(OutputStream out, Boolean b) + throws IOException + { + writeLength(out, 1); + if (b.booleanValue()) + out.write(0xFF); + else + out.write(0); + return 1; + } + + /** + * Write an INTEGER type to the given output stream. + * + * @param out The sink output stream. + * @param integer The integer to write. + */ + private static int writeInteger(OutputStream out, BigInteger integer) + throws IOException + { + byte[] bytes = integer.toByteArray(); + writeLength(out, bytes.length); + out.write(bytes); + return bytes.length; + } + + private static int writeSequence(OutputStream out, List sequence) + throws IOException + { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + for (Iterator i = sequence.iterator(); i.hasNext(); ) + { + write(bout, (DERValue) i.next()); + } + byte[] buf = bout.toByteArray(); + writeLength(out, buf.length); + out.write(buf); + return buf.length; + } + + private static int writeSet(OutputStream out, Set set) + throws IOException + { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + for (Iterator i = set.iterator(); i.hasNext(); ) + { + write(bout, (DERValue) i.next()); + } + byte[] buf = bout.toByteArray(); + writeLength(out, buf.length); + out.write(buf); + return buf.length; + } + + private static int writeOID(OutputStream out, OID oid) + throws IOException + { + byte[] der = oid.getDER(); + writeLength(out, der.length); + out.write(der); + return der.length; + } + + private static int writeBitString(OutputStream out, BitString bs) + throws IOException + { + byte[] buf = bs.getShiftedByteArray(); + out.write(buf.length + 1); + out.write(bs.getIgnoredBits()); + out.write(buf); + return buf.length; + } + + private static int writeString(OutputStream out, int tag, String str) + throws IOException + { + byte[] b = null; + switch (tag & 0x1F) + { + case NUMERIC_STRING: + case PRINTABLE_STRING: + case T61_STRING: + case VIDEOTEX_STRING: + case IA5_STRING: + case GRAPHIC_STRING: + case ISO646_STRING: + case GENERAL_STRING: + b = toIso88591(str); + break; + + case UNIVERSAL_STRING: + case BMP_STRING: + b = toUtf16Be(str); + break; + + case UTF8_STRING: + default: + b = toUtf8(str); + break; + } + writeLength(out, b.length); + out.write(b); + return b.length; + } + + private static byte[] toIso88591(String string) + { + byte[] result = new byte[string.length()]; + for (int i = 0; i < string.length(); i++) + result[i] = (byte) string.charAt(i); + return result; + } + + private static byte[] toUtf16Be(String string) + { + byte[] result = new byte[string.length() * 2]; + for (int i = 0; i < string.length(); i++) + { + result[i*2 ] = (byte) ((string.charAt(i) >>> 8) & 0xFF); + result[i*2+1] = (byte) (string.charAt(i) & 0xFF); + } + return result; + } + + private static byte[] toUtf8(String string) + { + ByteArrayOutputStream buf = + new ByteArrayOutputStream((int)(string.length() * 1.5)); + for (int i = 0; i < string.length(); i++) + { + char c = string.charAt(i); + if (c < 0x0080) + buf.write(c & 0xFF); + else if (c < 0x0800) + { + buf.write(0xC0 | ((c >>> 6) & 0x3F)); + buf.write(0x80 | (c & 0x3F)); + } + else + { + buf.write(0xE0 | ((c >>> 12) & 0x0F)); + buf.write(0x80 | ((c >>> 6) & 0x3F)); + buf.write(0x80 | (c & 0x3F)); + } + } + return buf.toByteArray(); + } + + private static int writeDate(OutputStream out, int tag, Date date) + throws IOException + { + SimpleDateFormat sdf = null; + if ((tag & 0x1F) == UTC_TIME) + sdf = new SimpleDateFormat("yyMMddHHmmss'Z'"); + else + sdf = new SimpleDateFormat("yyyyMMddHHmmss'.'SSS'Z'"); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + byte[] b = sdf.format(date).getBytes("ISO-8859-1"); + writeLength(out, b.length); + out.write(b); + return b.length; + } + + // Package method. + // ------------------------------------------------------------------------ + + static void writeLength(OutputStream out, int len) throws IOException + { + if (len < 128) + out.write(len); + else if (len < 256) + { + out.write(0x81); + out.write(len); + } + else if (len < 65536) + { + out.write(0x82); + out.write(len >> 8); + out.write(len); + } + else if (len < 16777216) + { + out.write(0x83); + out.write(len >> 16); + out.write(len >> 8); + out.write(len); + } + else + { + out.write(0x84); + out.write(len >> 24); + out.write(len >> 16); + out.write(len >> 8); + out.write(len); + } + } +} diff --git a/libjava/classpath/gnu/java/security/der/package.html b/libjava/classpath/gnu/java/security/der/package.html new file mode 100644 index 00000000000..e74b0db4e1c --- /dev/null +++ b/libjava/classpath/gnu/java/security/der/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.der package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.der</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/package.html b/libjava/classpath/gnu/java/security/package.html new file mode 100644 index 00000000000..bb6e91d61c4 --- /dev/null +++ b/libjava/classpath/gnu/java/security/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/pkcs/PKCS7SignedData.java b/libjava/classpath/gnu/java/security/pkcs/PKCS7SignedData.java new file mode 100644 index 00000000000..ba5efc7222f --- /dev/null +++ b/libjava/classpath/gnu/java/security/pkcs/PKCS7SignedData.java @@ -0,0 +1,364 @@ +/* PKCS7SignedData.java -- reader for PKCS#7 signedData objects + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security.pkcs; + +import gnu.java.security.OID; +import gnu.java.security.ber.BER; +import gnu.java.security.ber.BEREncodingException; +import gnu.java.security.ber.BERReader; +import gnu.java.security.ber.BERValue; +import gnu.java.security.der.DERValue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import java.math.BigInteger; + +import java.security.cert.CRL; +import java.security.cert.CRLException; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +/** + * The SignedData object in PKCS #7. This is a read-only implementation of + * this format, and is used to provide signed Jar file support. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class PKCS7SignedData +{ + + public static final OID PKCS7_DATA = new OID("1.2.840.113549.1.7.1"); + public static final OID PKCS7_SIGNED_DATA = new OID("1.2.840.113549.1.7.2"); + + private BigInteger version; + private Set digestAlgorithms; + private OID contentType; + private byte[] content; + private Certificate[] certificates; + private CRL[] crls; + private Set signerInfos; + + private static final boolean DEBUG = false; + private static void debug(String msg) + { + System.err.print("PKCS7SignedData >> "); + System.err.println(msg); + } + + public PKCS7SignedData(InputStream in) + throws CRLException, CertificateException, IOException + { + this(new BERReader(in)); + } + + /** + * Parse an encoded PKCS#7 SignedData object. The ASN.1 format of this + * object is: + * + * <pre> + * SignedData ::= SEQUENCE { + * version Version, + * digestAlgorithms DigestAlgorithmIdentifiers, + * contentInfo ContentInfo, + * certificates + * [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL, + * crls + * [1] IMPLICIT CertificateRevocationLists OPTIONAL, + * signerInfos SignerInfos } + * + * Version ::= INTEGER + * + * DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier + * + * DigestAlgorithmIdentifier ::= AlgorithmIdentifier + * + * ContentInfo ::= SEQUENCE { + * contentType ContentType, + * content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL } + * + * ContentType ::= OBJECT IDENTIFIER + * + * ExtendedCertificatesAndCertificates ::= + * SET OF ExtendedCertificatesAndCertificate + * + * ExtendedCertificatesAndCertificate ::= CHOICE { + * certificate Certificate, -- from X.509 + * extendedCertificate [0] IMPLICIT ExtendedCertificate } + * + * CertificateRevocationLists ::= SET OF CertificateRevocationList + * -- from X.509 + * + * SignerInfos ::= SET OF SignerInfo + * + * SignerInfo ::= SEQUENCE { + * version Version, + * issuerAndSerialNumber IssuerAndSerialNumber, + * digestAlgorithm DigestAlgorithmIdentifier, + * authenticatedAttributes + * [0] IMPLICIT Attributes OPTIONAL, + * digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier, + * encryptedDigest EncryptedDigest, + * unauthenticatedAttributes + * [1] IMPLICIT Attributes OPTIONAL } + * + * EncryptedDigest ::= OCTET STRING + * </pre> + * + * <p>(Readers who are confused as to why it takes 40 levels of indirection + * to specify "data with a signature", rest assured that the present author + * is as confused as you are).</p> + */ + public PKCS7SignedData(BERReader ber) + throws CRLException, CertificateException, IOException + { + CertificateFactory x509 = CertificateFactory.getInstance("X509"); + DERValue val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed ContentInfo"); + + val = ber.read(); + if (val.getTag() != BER.OBJECT_IDENTIFIER) + throw new BEREncodingException("malformed ContentType"); + + if (!PKCS7_SIGNED_DATA.equals(val.getValue())) + throw new BEREncodingException("content is not SignedData"); + + val = ber.read(); + if (val.getTag() != 0) + throw new BEREncodingException("malformed Content"); + + val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed SignedData"); + + if (DEBUG) + debug("SignedData: " + val); + + val = ber.read(); + if (val.getTag() != BER.INTEGER) + throw new BEREncodingException("expecting Version"); + version = (BigInteger) val.getValue(); + + if (DEBUG) + debug(" Version: " + version); + + digestAlgorithms = new HashSet(); + val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed DigestAlgorithmIdentifiers"); + if (DEBUG) + debug(" DigestAlgorithmIdentifiers: " + val); + int count = 0; + DERValue val2 = ber.read(); + while (val2 != BER.END_OF_SEQUENCE && + (val.getLength() > 0 && val.getLength() > count)) + { + if (!val2.isConstructed()) + throw new BEREncodingException("malformed AlgorithmIdentifier"); + if (DEBUG) + debug(" AlgorithmIdentifier: " + val2); + count += val2.getEncodedLength(); + val2 = ber.read(); + if (val2.getTag() != BER.OBJECT_IDENTIFIER) + throw new BEREncodingException("malformed AlgorithmIdentifier"); + if (DEBUG) + debug(" ID: " + val2.getValue()); + List algId = new ArrayList(2); + algId.add(val2.getValue()); + val2 = ber.read(); + if (val2 != BER.END_OF_SEQUENCE) + { + count += val2.getEncodedLength(); + if (val2.getTag() == BER.NULL) + algId.add(null); + else + algId.add(val2.getEncoded()); + if (DEBUG) + debug(" params: " + new BigInteger(1, val2.getEncoded()).toString(16)); + if (val2.isConstructed()) + ber.skip(val2.getLength()); + if (BERValue.isIndefinite(val)) + val2 = ber.read(); + } + else + algId.add(null); + digestAlgorithms.add(algId); + } + + val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed ContentInfo"); + if (DEBUG) + debug(" ContentInfo: " + val); + val2 = ber.read(); + if (val2.getTag() != BER.OBJECT_IDENTIFIER) + throw new BEREncodingException("malformed ContentType"); + contentType = (OID) val2.getValue(); + if (DEBUG) + debug(" ContentType: " + contentType); + if (BERValue.isIndefinite(val) + || (val.getLength() > 0 && val.getLength() > val2.getEncodedLength())) + { + val2 = ber.read(); + if (val2 != BER.END_OF_SEQUENCE) + { + content = val2.getEncoded(); + if (BERValue.isIndefinite(val)) + val2 = ber.read(); + if (DEBUG) + debug(" Content: " + new BigInteger(1, content).toString(16)); + } + } + + val = ber.read(); + if (val.getTag() == 0) + { + if (!val.isConstructed()) + throw new BEREncodingException("malformed ExtendedCertificatesAndCertificates"); + if (DEBUG) + debug(" ExtendedCertificatesAndCertificates: " + val); + count = 0; + val2 = ber.read(); + List certs = new LinkedList(); + while (val2 != BER.END_OF_SEQUENCE && + (val.getLength() > 0 && val.getLength() > count)) + { + Certificate cert = + x509.generateCertificate(new ByteArrayInputStream(val2.getEncoded())); + if (DEBUG) + debug(" Certificate: " + cert); + certs.add(cert); + count += val2.getEncodedLength(); + ber.skip(val2.getLength()); + if (BERValue.isIndefinite(val) || val.getLength() > count) + val2 = ber.read(); + } + certificates = (Certificate[]) certs.toArray(new Certificate[certs.size()]); + val = ber.read(); + } + + if (val.getTag() == 1) + { + if (!val.isConstructed()) + throw new BEREncodingException("malformed CertificateRevocationLists"); + if (DEBUG) + debug(" CertificateRevocationLists: " + val); + count = 0; + val2 = ber.read(); + List crls = new LinkedList(); + while (val2 != BER.END_OF_SEQUENCE && + (val.getLength() > 0 && val.getLength() > count)) + { + CRL crl = x509.generateCRL(new ByteArrayInputStream(val2.getEncoded())); + if (DEBUG) + debug (" CRL: " + crl); + crls.add(crl); + count += val2.getEncodedLength(); + ber.skip(val2.getLength()); + if (BERValue.isIndefinite(val) || val.getLength() > count) + val2 = ber.read(); + } + this.crls = (CRL[]) crls.toArray(new CRL[crls.size()]); + val = ber.read(); + } + + signerInfos = new HashSet(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed SignerInfos"); + + if (DEBUG) + debug(" SignerInfos: " + val); + + // FIXME read this more carefully. + // Since we are just reading a file (probably) we just read until we + // reach the end. + while (true) + { + int i = ber.peek(); + if (i == 0 || i == -1) + break; + signerInfos.add(new SignerInfo(ber)); + } + } + + public BigInteger getVersion() + { + return version; + } + + public Certificate[] getCertificates() + { + return (certificates != null ? (Certificate[]) certificates.clone() + : null); + } + + public OID getContentType() + { + return contentType; + } + + public byte[] getContent() + { + return (content != null ? (byte[]) content.clone() : null); + } + + public Set getDigestAlgorithms() + { + // FIXME copy contents too, they are mutable!!! + return Collections.unmodifiableSet(digestAlgorithms); + } + + public Set getSignerInfos() + { + Set copy = new HashSet(); + for (Iterator it = signerInfos.iterator(); it.hasNext(); ) + copy.add(it.next()); + return Collections.unmodifiableSet(copy); + } +} diff --git a/libjava/classpath/gnu/java/security/pkcs/SignerInfo.java b/libjava/classpath/gnu/java/security/pkcs/SignerInfo.java new file mode 100644 index 00000000000..c976799bb0c --- /dev/null +++ b/libjava/classpath/gnu/java/security/pkcs/SignerInfo.java @@ -0,0 +1,279 @@ +/* SignerInfo.java -- a SignerInfo object, from PKCS #7 + Copyright (C) 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security.pkcs; + +import gnu.java.security.OID; +import gnu.java.security.ber.BER; +import gnu.java.security.ber.BEREncodingException; +import gnu.java.security.ber.BERReader; +import gnu.java.security.ber.BERValue; +import gnu.java.security.der.DERValue; + +import java.io.IOException; + +import java.math.BigInteger; + +import javax.security.auth.x500.X500Principal; + +public class SignerInfo +{ + private final BigInteger version; + private final BigInteger serialNumber; + private final X500Principal issuer; + private final OID digestAlgorithmId; + private final byte[] digestAlgorithmParams; + private final byte[] authenticatedAttributes; + private final OID digestEncryptionAlgorithmId; + private final byte[] digestEncryptionAlgorithmParams; + private final byte[] encryptedDigest; + private final byte[] unauthenticatedAttributes; + + private static final boolean DEBUG = false; + private static void debug(String msg) + { + System.err.print("SignerInfo >> "); + System.err.println(msg); + } + + /** + * Parse a SignerInfo object. + */ + public SignerInfo(BERReader ber) throws IOException + { + DERValue val = ber.read(); + if (DEBUG) + debug("SignerInfo: " + val); + if (!val.isConstructed()) + throw new BEREncodingException("malformed SignerInfo"); + + val = ber.read(); + if (val.getTag() != BER.INTEGER) + throw new BEREncodingException("malformed Version"); + version = (BigInteger) val.getValue(); + + if (DEBUG) + debug(" Version: " + version); + + val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed IssuerAndSerialNumber"); + + if (DEBUG) + debug(" IssuerAndSerialNumber: " + val); + + val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed Issuer"); + issuer = new X500Principal(val.getEncoded()); + ber.skip(val.getLength()); + if (DEBUG) + debug(" Issuer: " + issuer); + + val = ber.read(); + if (val.getTag() != BER.INTEGER) + throw new BEREncodingException("malformed SerialNumber"); + serialNumber = (BigInteger) val.getValue(); + if (DEBUG) + debug(" SerialNumber: " + serialNumber); + + val = ber.read(); + if (!val.isConstructed()) + throw new BEREncodingException("malformed DigestAlgorithmIdentifier"); + if (DEBUG) + debug(" DigestAlgorithmIdentifier: " + val); + + int count = 0; + DERValue val2 = ber.read(); + if (val2.getTag() != BER.OBJECT_IDENTIFIER) + throw new BEREncodingException("malformed AlgorithmIdentifier"); + digestAlgorithmId = (OID) val2.getValue(); + if (DEBUG) + debug(" OID: " + digestAlgorithmId); + + if (BERValue.isIndefinite(val)) + { + val2 = ber.read(); + if (val2 != BER.END_OF_SEQUENCE) + { + digestAlgorithmParams = val2.getEncoded(); + val2 = ber.read(); + if (val2 != BER.END_OF_SEQUENCE) + throw new BEREncodingException("expecting BER end-of-sequence"); + } + else + digestAlgorithmParams = null; + } + else if (val2.getEncodedLength() < val.getLength()) + { + val2 = ber.read(); + digestAlgorithmParams = val2.getEncoded(); + if (val2.isConstructed()) + ber.skip(val2.getLength()); + } + else + digestAlgorithmParams = null; + if(DEBUG) + debug(" params: " + (digestAlgorithmParams == null ? null + : new BigInteger(digestAlgorithmParams).toString(16))); + + val = ber.read(); + if (val.getTag() == 0) + { + authenticatedAttributes = val.getEncoded(); + val = ber.read(); + if (val.isConstructed()) + ber.skip(val.getLength()); + if (DEBUG) + debug(" AuthenticatedAttributes: " + val); + val = ber.read(); + } + else + authenticatedAttributes = null; + + if (!val.isConstructed()) + throw new BEREncodingException("malformed DigestEncryptionAlgorithmIdentifier"); + if (DEBUG) + debug(" DigestEncryptionAlgorithmIdentifier: " + val); + count = 0; + val2 = ber.read(); + if (val2.getTag() != BER.OBJECT_IDENTIFIER) + throw new BEREncodingException("malformed AlgorithmIdentifier"); + digestEncryptionAlgorithmId = (OID) val2.getValue(); + if (DEBUG) + debug(" OID: " + digestEncryptionAlgorithmId); + + if (BERValue.isIndefinite(val)) + { + val2 = ber.read(); + if (val2 != BER.END_OF_SEQUENCE) + { + digestEncryptionAlgorithmParams = val2.getEncoded(); + val2 = ber.read(); + if (val2 != BER.END_OF_SEQUENCE) + throw new BEREncodingException("expecting BER end-of-sequence"); + } + else + digestEncryptionAlgorithmParams = null; + } + else if (val2.getEncodedLength() < val.getLength()) + { + val2 = ber.read(); + digestEncryptionAlgorithmParams = val2.getEncoded(); + if (val2.isConstructed()) + ber.skip(val2.getLength()); + } + else + digestEncryptionAlgorithmParams = null; + if(DEBUG) + debug(" params: " + (digestEncryptionAlgorithmParams == null ? null + : new BigInteger(digestEncryptionAlgorithmParams).toString(16))); + + val = ber.read(); + if (val.getTag() != BER.OCTET_STRING) + throw new BEREncodingException("malformed EncryptedDigest"); + encryptedDigest = (byte[]) val.getValue(); + if (DEBUG) + debug(" EncryptedDigest: " + new BigInteger(1, encryptedDigest).toString(16)); + + if (ber.peek() == 1) + unauthenticatedAttributes = ber.read().getEncoded(); + else + unauthenticatedAttributes = null; + + if (ber.peek() == 0) + ber.read(); + } + + public BigInteger getVersion() + { + return version; + } + + public BigInteger getSerialNumber() + { + return serialNumber; + } + + public X500Principal getIssuer() + { + return issuer; + } + + public OID getDigestAlgorithmId() + { + return digestAlgorithmId; + } + + public byte[] getDigestAlgorithmParams() + { + return (digestAlgorithmParams != null + ? (byte[]) digestAlgorithmParams.clone() + : null); + } + + public byte[] getAuthenticatedAttributes() + { + return (authenticatedAttributes != null + ? (byte[]) authenticatedAttributes.clone() + : null); + } + + public OID getDigestEncryptionAlgorithmId() + { + return digestEncryptionAlgorithmId; + } + + public byte[] getDigestEncryptionAlgorithmParams() + { + return (digestEncryptionAlgorithmParams != null + ? (byte[]) digestEncryptionAlgorithmParams.clone() + : null); + } + + public byte[] getEncryptedDigest() + { + return (encryptedDigest != null ? (byte[]) encryptedDigest.clone() : null); + } + + public byte[] getUnauthenticatedAttributes() + { + return (unauthenticatedAttributes != null + ? (byte[]) unauthenticatedAttributes.clone() + : null); + } +} diff --git a/libjava/classpath/gnu/java/security/pkcs/package.html b/libjava/classpath/gnu/java/security/pkcs/package.html new file mode 100644 index 00000000000..60d658f8eef --- /dev/null +++ b/libjava/classpath/gnu/java/security/pkcs/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.pkcs package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.pkcs</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/provider/CollectionCertStoreImpl.java b/libjava/classpath/gnu/java/security/provider/CollectionCertStoreImpl.java new file mode 100644 index 00000000000..4bf3d5434ef --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/CollectionCertStoreImpl.java @@ -0,0 +1,102 @@ +/* CollectionCertStore.java -- Collection-based cert store. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.InvalidAlgorithmParameterException; +import java.security.cert.CRL; +import java.security.cert.CRLSelector; +import java.security.cert.CertSelector; +import java.security.cert.CertStoreException; +import java.security.cert.CertStoreParameters; +import java.security.cert.CertStoreSpi; +import java.security.cert.Certificate; +import java.security.cert.CollectionCertStoreParameters; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; + +public final class CollectionCertStoreImpl extends CertStoreSpi +{ + + // Fields. + // ------------------------------------------------------------------------- + + private final Collection store; + + // Constructors. + // ------------------------------------------------------------------------- + + public CollectionCertStoreImpl(CertStoreParameters params) + throws InvalidAlgorithmParameterException + { + super(params); + if (! (params instanceof CollectionCertStoreParameters)) + throw new InvalidAlgorithmParameterException("not a CollectionCertStoreParameters object"); + store = ((CollectionCertStoreParameters) params).getCollection(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public Collection engineGetCertificates(CertSelector selector) + throws CertStoreException + { + LinkedList result = new LinkedList(); + for (Iterator it = store.iterator(); it.hasNext(); ) + { + Object o = it.next(); + if ((o instanceof Certificate) && selector.match((Certificate) o)) + result.add(o); + } + return result; + } + + public Collection engineGetCRLs(CRLSelector selector) + throws CertStoreException + { + LinkedList result = new LinkedList(); + for (Iterator it = store.iterator(); it.hasNext(); ) + { + Object o = it.next(); + if ((o instanceof CRL) && selector.match((CRL) o)) + result.add(o); + } + return result; + } +} diff --git a/libjava/classpath/gnu/java/security/provider/DSAKeyFactory.java b/libjava/classpath/gnu/java/security/provider/DSAKeyFactory.java new file mode 100644 index 00000000000..7e154e27473 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/DSAKeyFactory.java @@ -0,0 +1,134 @@ +/* DSAKeyFactory.java -- DSA key factory. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.KeyFactorySpi; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.interfaces.DSAPrivateKey; +import java.security.interfaces.DSAPublicKey; +import java.security.spec.DSAPrivateKeySpec; +import java.security.spec.DSAPublicKeySpec; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; + +/** + * DSA key factory. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class DSAKeyFactory extends KeyFactorySpi +{ + + // Constructor. + // ------------------------------------------------------------------------ + + public DSAKeyFactory() + { + super(); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + protected PrivateKey engineGeneratePrivate(KeySpec keySpec) + throws InvalidKeySpecException + { + if (!(keySpec instanceof DSAPrivateKeySpec)) + throw new InvalidKeySpecException(); + return new GnuDSAPrivateKey( + ((DSAPrivateKeySpec) keySpec).getX(), + ((DSAPrivateKeySpec) keySpec).getP(), + ((DSAPrivateKeySpec) keySpec).getQ(), + ((DSAPrivateKeySpec) keySpec).getG()); + } + + protected PublicKey engineGeneratePublic(KeySpec keySpec) + throws InvalidKeySpecException + { + if (!(keySpec instanceof DSAPublicKeySpec)) + throw new InvalidKeySpecException(); + return new GnuDSAPublicKey( + ((DSAPublicKeySpec) keySpec).getY(), + ((DSAPublicKeySpec) keySpec).getP(), + ((DSAPublicKeySpec) keySpec).getQ(), + ((DSAPublicKeySpec) keySpec).getG()); + } + + protected KeySpec engineGetKeySpec(Key key, Class keySpec) + throws InvalidKeySpecException + { + if ((key instanceof DSAPublicKey) && + keySpec.isAssignableFrom(DSAPublicKeySpec.class)) + { + return new DSAPublicKeySpec(((DSAPublicKey) key).getY(), + ((DSAPublicKey) key).getParams().getP(), + ((DSAPublicKey) key).getParams().getQ(), + ((DSAPublicKey) key).getParams().getG()); + } + if ((key instanceof DSAPrivateKey) && + keySpec.isAssignableFrom(DSAPrivateKeySpec.class)) + { + return new DSAPrivateKeySpec(((DSAPrivateKey) key).getX(), + ((DSAPrivateKey) key).getParams().getP(), + ((DSAPrivateKey) key).getParams().getQ(), + ((DSAPrivateKey) key).getParams().getG()); + } + throw new InvalidKeySpecException(); + } + + protected Key engineTranslateKey(Key key) throws InvalidKeyException + { + if ((key instanceof GnuDSAPublicKey) || (key instanceof GnuDSAPrivateKey)) + return key; + if (key instanceof DSAPublicKey) + return new GnuDSAPublicKey(((DSAPublicKey) key).getY(), + ((DSAPublicKey) key).getParams().getP(), + ((DSAPublicKey) key).getParams().getQ(), + ((DSAPublicKey) key).getParams().getG()); + if (key instanceof DSAPrivateKey) + return new GnuDSAPrivateKey(((DSAPrivateKey) key).getX(), + ((DSAPrivateKey) key).getParams().getP(), + ((DSAPrivateKey) key).getParams().getQ(), + ((DSAPrivateKey) key).getParams().getG()); + throw new InvalidKeyException(); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/DSAKeyPairGenerator.java b/libjava/classpath/gnu/java/security/provider/DSAKeyPairGenerator.java new file mode 100644 index 00000000000..2c643d5c367 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/DSAKeyPairGenerator.java @@ -0,0 +1,171 @@ +/* GnuDSAKeyPairGenerator.java --- Gnu DSA Key Pair Generator + Copyright (C) 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.math.BigInteger; +import java.security.AlgorithmParameterGenerator; +import java.security.AlgorithmParameters; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidParameterException; +import java.security.KeyPair; +import java.security.KeyPairGeneratorSpi; +import java.security.SecureRandom; +import java.security.interfaces.DSAParams; +import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.DSAParameterSpec; +import java.util.Random; + +public class DSAKeyPairGenerator extends KeyPairGeneratorSpi + implements java.security.interfaces.DSAKeyPairGenerator +{ +int keysize; +SecureRandom random; +private BigInteger q = null; // the small prime +private BigInteger p = null; // the big prime +private BigInteger g = null; + +DSAKeyPairGenerator() +{ + keysize = 1024; +} + +public void initialize(int keysize, SecureRandom random) +{ + //if( ((keysize % 64) != 0) || (keysize < 512) || (keysize > 1024) ) + // throw new InvalidAlgorithmParameterException("Invalid key size"); + + this.keysize = keysize; + this.random = random; +} + +public void initialize(AlgorithmParameterSpec params, + SecureRandom random) + throws InvalidAlgorithmParameterException +{ + if( !( params instanceof DSAParameterSpec ) ) + throw new InvalidAlgorithmParameterException("Must be DSAParameterSpec"); + + DSAParameterSpec dsaparameterspec = (DSAParameterSpec)params; + p = dsaparameterspec.getP(); + q = dsaparameterspec.getQ(); + g = dsaparameterspec.getG(); + this.random = random; +} + +public void initialize(DSAParams params, SecureRandom random) + throws InvalidParameterException +{ + if(params.getP() != null) + p = params.getP(); + else + throw new InvalidParameterException(); + + if(params.getQ() != null) + q = params.getQ(); + else + throw new InvalidParameterException(); + + if(params.getG() != null) + g = params.getG(); + else + throw new InvalidParameterException(); + + this.random = random; +} + +public void initialize(int modlen, boolean genParams, SecureRandom random) + throws InvalidParameterException +{ + if( ((modlen % 64) != 0) || (modlen < 512) || (modlen > 1024) ) + throw new InvalidParameterException(); + + if( (genParams == false) && (modlen != 512) && (modlen != 768) && (modlen != 1024) ) + throw new InvalidParameterException(); + this.keysize = modlen; + this.random = random; + p = null; + q = null; + g = null; +} + +public KeyPair generateKeyPair() +{ + if( getDefaults() == false) { + try { + AlgorithmParameterGenerator apgDSA = AlgorithmParameterGenerator.getInstance("DSA"); + AlgorithmParameters apDSA = apgDSA.generateParameters(); + DSAParameterSpec dsaparameterspec = (DSAParameterSpec)apDSA.getParameterSpec( DSAParameterSpec.class ); + p = dsaparameterspec.getP(); + q = dsaparameterspec.getQ(); + g = dsaparameterspec.getG(); + } catch ( Exception e ) { + return null; + } + } + + BigInteger x = new BigInteger( 159, new Random() ); + + BigInteger y = g.modPow( x, p ); + + return new KeyPair( new GnuDSAPublicKey(y,p,q,g), new GnuDSAPrivateKey(x,p,q,g)); + //return new KeyPair( public, private ); +} + +//These constants are Sun's Constants copied from the +//Cryptography Specification +private boolean getDefaults() +{ + if( keysize == 512) { + p = new BigInteger("fca682ce8e12caba26efccf7110e526db078b05edecbcd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e12ed0899bcd132acd50d99151bdc43ee737592e17", 16); + q = new BigInteger("962eddcc369cba8ebb260ee6b6a126d9346e38c5", 16); + g = new BigInteger("678471b27a9cf44ee91a49c5147db1a9aaf244f05a434d6486931d2d14271b9e35030b71fd73da179069b32e2935630e1c2062354d0da20a6c416e50be794ca4", 16); + return true; + } else if( keysize == 768) { + p = new BigInteger("e9e642599d355f37c97ffd3567120b8e25c9cd43e927b3a9670fbec5d890141922d2c3b3ad2480093799869d1e846aab49fab0ad26d2ce6a22219d470bce7d777d4a21fbe9c270b57f607002f3cef8393694cf45ee3688c11a8c56ab127a3daf", 16); + q = new BigInteger("9cdbd84c9f1ac2f38d0f80f42ab952e7338bf511", 16); + g = new BigInteger("30470ad5a005fb14ce2d9dcd87e38bc7d1b1c5facbaecbe95f190aa7a31d23c4dbbcbe06174544401a5b2c020965d8c2bd2171d3668445771f74ba084d2029d83c1c158547f3a9f1a2715be23d51ae4d3e5a1f6a7064f316933a346d3f529252", 16); + } else if( keysize == 512) { + p = new BigInteger("fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7", 16); + q = new BigInteger("9760508f15230bccb292b982a2eb840bf0581cf5", 16); + g = new BigInteger("f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a", 16); + } + return false; +} + +} diff --git a/libjava/classpath/gnu/java/security/provider/DSAParameterGenerator.java b/libjava/classpath/gnu/java/security/provider/DSAParameterGenerator.java new file mode 100644 index 00000000000..ccec1136cd3 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/DSAParameterGenerator.java @@ -0,0 +1,128 @@ +/* DSAParameterGenerator.java --- DSA Parameter Generator Implementation + Copyright (C) 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.util.Prime; + +import java.math.BigInteger; +import java.security.AlgorithmParameterGeneratorSpi; +import java.security.AlgorithmParameters; +import java.security.InvalidAlgorithmParameterException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.DSAParameterSpec; +import java.security.spec.InvalidParameterSpecException; +import java.util.Random; + +public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi +{ + private int size; + private SecureRandom random = null; + + public DSAParameterGenerator() + { + size = 1024; + } + + public void engineInit(int size, SecureRandom random) + { + if( (size < 512) || (size > 1024) || ( (size % 64) != 0) ) + //throw new InvalidAlgorithmParameterException("Invalid Size"); + return; + this.size = size; + this.random = random; + } + + public void engineInit(AlgorithmParameterSpec genParamSpec, SecureRandom random) + throws InvalidAlgorithmParameterException + { + if( !( genParamSpec instanceof DSAParameterSpec ) ) + throw new InvalidAlgorithmParameterException("Must be DSAParameterSpec"); + + DSAParameterSpec dsaparameterspec = (DSAParameterSpec)genParamSpec; + int tmp = dsaparameterspec.getP().bitLength(); + + if( (tmp < 512) || (tmp > 1024) || ( (tmp % 64) != 0) ) + throw new InvalidAlgorithmParameterException("Invalid Size"); + + this.random = random; + } + + //For more information see IEEE P1363 A.16.1 (10/05/98 Draft) + public AlgorithmParameters engineGenerateParameters() + { + DSAParameterSpec dsaparameterspec; + + int L = size; + BigInteger r, p, k, h, g; + + //q 2^159 < q < 2^160 + r = Prime.generateRandomPrime( 159, 160, BigInteger.valueOf(1)); + + // 2^(L-1) < p < 2^L + p = Prime.generateRandomPrime( r, BigInteger.valueOf(1), L - 1, L, BigInteger.valueOf(1)); + + k = p.subtract( BigInteger.valueOf(1) ); + k = k.divide( r ); + + Random rand = new Random(); + h = BigInteger.valueOf(1); + + for(;;) { + h = h.add(BigInteger.valueOf( 1 ) ); + + g = h.modPow(k, p); + + if( g.compareTo( BigInteger.valueOf(1) ) != 1 ) + break; + } + + try { + dsaparameterspec = new DSAParameterSpec(p, r, g); + AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA"); + ap.init( dsaparameterspec ); + return ap; + } catch ( NoSuchAlgorithmException nsae ) { + return null; + } catch ( InvalidParameterSpecException ipse) { + return null; + } + } +} diff --git a/libjava/classpath/gnu/java/security/provider/DSAParameters.java b/libjava/classpath/gnu/java/security/provider/DSAParameters.java new file mode 100644 index 00000000000..77d648956ee --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/DSAParameters.java @@ -0,0 +1,150 @@ +/* DSAParameters.java --- DSA Parameters Implementation + Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.io.ASN1ParsingException; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.der.DERWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.security.AlgorithmParametersSpi; +import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.DSAParameterSpec; +import java.security.spec.InvalidParameterSpecException; +import java.util.ArrayList; + +/* + ASN.1 Encoding for DSA from rfc2459 + + id-dsa ID ::= { iso(1) member-body(2) us(840) x9-57(10040) + x9cm(4) 1 } + + Dss-Parms ::= SEQUENCE { + p INTEGER, + q INTEGER, + g INTEGER } + +*/ +public class DSAParameters extends AlgorithmParametersSpi +{ +private BigInteger q; // the small prime +private BigInteger p; // the big prime +private BigInteger g; + + +public void engineInit(AlgorithmParameterSpec paramSpec) + throws InvalidParameterSpecException +{ + if( paramSpec instanceof DSAParameterSpec ) { + DSAParameterSpec dsaParamSpec = (DSAParameterSpec)paramSpec; + p = dsaParamSpec.getP(); + q = dsaParamSpec.getQ(); + g = dsaParamSpec.getG(); + } + else + throw new InvalidParameterSpecException("Only accepts DSAParameterSpec"); +} + +public void engineInit(byte[] params) + throws IOException +{ + DERReader in = new DERReader(params); + DERValue val = in.read(); + if (val.getValue() != DER.CONSTRUCTED_VALUE) + throw new ASN1ParsingException("badly formed parameters"); + try + { + p = (BigInteger) in.read().getValue(); + q = (BigInteger) in.read().getValue(); + g = (BigInteger) in.read().getValue(); + } + catch (Exception x) + { + throw new ASN1ParsingException("badly formed parameters"); + } +} + +public void engineInit(byte[] params, String format) + throws IOException +{ + if( !format.equals("ASN.1") ) + throw new IOException("Invalid Format: Only accepts ASN.1"); + engineInit( params ); +} + +public AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec) + throws InvalidParameterSpecException +{ + if( paramSpec.isAssignableFrom(DSAParameterSpec.class) ) + return new DSAParameterSpec(p, q, g); + else + throw new InvalidParameterSpecException("Only accepts DSAParameterSpec"); +} + +public byte[] engineGetEncoded() + throws IOException +{ + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + ArrayList seq = new ArrayList(3); + seq.add(new DERValue(DER.INTEGER, p)); + seq.add(new DERValue(DER.INTEGER, q)); + seq.add(new DERValue(DER.INTEGER, g)); + DERWriter.write(bout, new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, seq)); + return bout.toByteArray(); +} + + +public byte[] engineGetEncoded(String format) + throws IOException +{ + if( !format.equals("ASN.1") ) + throw new IOException("Invalid Format: Only accepts ASN.1"); + return engineGetEncoded(); +} + +public String engineToString() +{ + return ("q: " + q + " p: " + p + " g: " + g); +} + +} diff --git a/libjava/classpath/gnu/java/security/provider/DSASignature.java b/libjava/classpath/gnu/java/security/provider/DSASignature.java new file mode 100644 index 00000000000..1d3875d28e3 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/DSASignature.java @@ -0,0 +1,251 @@ +/* DSASignature.java -- + Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.der.DERWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.InvalidParameterException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.SignatureException; +import java.security.SignatureSpi; +import java.security.interfaces.DSAPrivateKey; +import java.security.interfaces.DSAPublicKey; +import java.security.spec.AlgorithmParameterSpec; +import java.util.ArrayList; +import java.util.Random; + +public class DSASignature extends SignatureSpi +{ + private DSAPublicKey publicKey; + private DSAPrivateKey privateKey; + private final MessageDigest digest; + private final SecureRandom random; + + public DSASignature() throws NoSuchAlgorithmException + { + random = new SecureRandom(); + digest = MessageDigest.getInstance ("SHA1"); + } + + private void init() + { + digest.reset(); + } + + public void engineInitVerify (PublicKey publicKey) + throws InvalidKeyException + { + if (publicKey instanceof DSAPublicKey) + this.publicKey = (DSAPublicKey) publicKey; + else + throw new InvalidKeyException(); + init(); + } + + public void engineInitSign (PrivateKey privateKey) + throws InvalidKeyException + { + if (privateKey instanceof DSAPrivateKey) + this.privateKey = (DSAPrivateKey) privateKey; + else + throw new InvalidKeyException ("not a DSA private key"); + + init(); + } + + public void engineInitSign (PrivateKey privateKey, + SecureRandom random) + throws InvalidKeyException + { + if (privateKey instanceof DSAPrivateKey) + this.privateKey = (DSAPrivateKey) privateKey; + else + throw new InvalidKeyException ("not a DSA private key"); + + appRandom = random; + init(); + } + + public void engineUpdate(byte b) + throws SignatureException + { + digest.update (b); + } + + public void engineUpdate (byte[] b, int off, int len) + throws SignatureException + { + digest.update (b, off, len); + } + + public byte[] engineSign() throws SignatureException + { + if (privateKey == null) + throw new SignatureException ("not initialized for signing"); + + try + { + BigInteger g = privateKey.getParams().getG(); + BigInteger p = privateKey.getParams().getP(); + BigInteger q = privateKey.getParams().getQ(); + + BigInteger x = privateKey.getX(); + + BigInteger k = new BigInteger (159, appRandom != null ? appRandom : random); + + BigInteger r = g.modPow(k, p); + r = r.mod(q); + + byte bytes[] = digest.digest(); + BigInteger sha = new BigInteger (1, bytes); + + BigInteger s = sha.add (x.multiply (r)); + s = s.multiply (k.modInverse(q)).mod (q); + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + ArrayList seq = new ArrayList (2); + seq.add(0, new DERValue (DER.INTEGER, r)); + seq.add(1, new DERValue (DER.INTEGER, s)); + DERWriter.write (bout, new DERValue (DER.CONSTRUCTED | DER.SEQUENCE, seq)); + return bout.toByteArray(); + } + catch (IOException ioe) + { + SignatureException se = new SignatureException(); + se.initCause (ioe); + throw se; + } + catch (ArithmeticException ae) + { + SignatureException se = new SignatureException(); + se.initCause (ae); + throw se; + } + } + + public int engineSign (byte[] outbuf, int offset, int len) + throws SignatureException + { + byte tmp[] = engineSign(); + if (tmp.length > len) + throw new SignatureException ("output buffer too short"); + System.arraycopy (tmp, 0, outbuf, offset, tmp.length); + return tmp.length; + } + + public boolean engineVerify (byte[] sigBytes) + throws SignatureException + { + // Decode sigBytes from ASN.1 DER encoding + try + { + DERReader in = new DERReader (sigBytes); + DERValue val = in.read(); + if (!val.isConstructed()) + throw new SignatureException ("badly formed signature"); + BigInteger r = (BigInteger) in.read().getValue(); + BigInteger s = (BigInteger) in.read().getValue(); + + BigInteger g = publicKey.getParams().getG(); + BigInteger p = publicKey.getParams().getP(); + BigInteger q = publicKey.getParams().getQ(); + + BigInteger y = publicKey.getY(); + + BigInteger w = s.modInverse (q); + + byte bytes[] = digest.digest(); + BigInteger sha = new BigInteger (1, bytes); + + BigInteger u1 = w.multiply (sha).mod ( q ); + + BigInteger u2 = r.multiply (w).mod(q); + + BigInteger v = g.modPow (u1, p).multiply (y.modPow (u2, p)).mod (p).mod (q); + + if (v.equals (r)) + return true; + else + return false; + } + catch (IOException ioe) + { + SignatureException se = new SignatureException ("badly formed signature"); + se.initCause (ioe); + throw se; + } + } + + public void engineSetParameter (String param, + Object value) + throws InvalidParameterException + { + throw new InvalidParameterException(); + } + + public void engineSetParameter (AlgorithmParameterSpec params) + throws InvalidAlgorithmParameterException + { + throw new InvalidParameterException(); + + } + + public Object engineGetParameter (String param) + throws InvalidParameterException + { + throw new InvalidParameterException(); + } + + public Object clone() throws CloneNotSupportedException + { + return super.clone(); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/DefaultPolicy.java b/libjava/classpath/gnu/java/security/provider/DefaultPolicy.java new file mode 100644 index 00000000000..d42be6c908f --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/DefaultPolicy.java @@ -0,0 +1,68 @@ +/* DefaultPolicy.java -- + Copyright (C) 2001, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.security.provider; + +import java.security.AllPermission; +import java.security.CodeSource; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.security.Policy; + +/** + * This is just a stub policy implementation which grants all permissions + * to any code source. FIXME: This should be replaced with a real + * implementation that reads the policy configuration from a file, like + * $JAVA_HOME/jre/lib/security/java.security. + */ +public class DefaultPolicy extends Policy +{ + static Permission allPermission = new AllPermission(); + + public PermissionCollection getPermissions(CodeSource codesource) + { + Permissions perms = new Permissions(); + perms.add(allPermission); + return perms; + } + + public void refresh() + { + // Nothing. + } +} diff --git a/libjava/classpath/gnu/java/security/provider/EncodedKeyFactory.java b/libjava/classpath/gnu/java/security/provider/EncodedKeyFactory.java new file mode 100644 index 00000000000..2bf0fff809e --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/EncodedKeyFactory.java @@ -0,0 +1,303 @@ +/* EncodedKeyFactory.java -- encoded key factory. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.math.BigInteger; +import java.security.AlgorithmParameters; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.KeyFactorySpi; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.spec.DSAParameterSpec; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.InvalidParameterSpecException; +import java.security.spec.KeySpec; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.security.spec.RSAPublicKeySpec; +import java.security.spec.X509EncodedKeySpec; + +import javax.crypto.spec.DHParameterSpec; + +/** + * A factory for keys encoded in either the X.509 format (for public + * keys) or the PKCS#8 format (for private keys). + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class EncodedKeyFactory extends KeyFactorySpi +{ + + // Constants. + // ------------------------------------------------------------------------ + + private static final OID ID_DSA = new OID("1.2.840.10040.4.1"); + private static final OID ID_RSA = new OID("1.2.840.113549.1.1.1"); + private static final OID ID_DH = new OID("1.2.840.10046.2.1"); + + // Instance methods. + // ------------------------------------------------------------------------ + + public PublicKey engineGeneratePublic(KeySpec spec) + throws InvalidKeySpecException + { + if (!(spec instanceof X509EncodedKeySpec)) + throw new InvalidKeySpecException("only supports X.509 key specs"); + DERReader der = new DERReader(((X509EncodedKeySpec) spec).getEncoded()); + try + { + DERValue spki = der.read(); + if (!spki.isConstructed()) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + DERValue alg = der.read(); + if (!alg.isConstructed()) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + DERValue val = der.read(); + if (!(val.getValue() instanceof OID)) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + OID algId = (OID) val.getValue(); + byte[] algParams = null; + if (alg.getLength() > val.getEncodedLength()) + { + val = der.read(); + algParams = val.getEncoded(); + if (val.isConstructed()) + der.skip(val.getLength()); + } + val = der.read(); + if (!(val.getValue() instanceof BitString)) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + byte[] publicKey = ((BitString) val.getValue()).toByteArray(); + if (algId.equals(ID_DSA)) + { + BigInteger p = null, g = null, q = null, Y; + if (algParams != null) + { + DERReader dsaParams = new DERReader(algParams); + val = dsaParams.read(); + if (!val.isConstructed()) + throw new InvalidKeySpecException("malformed DSA parameters"); + val = dsaParams.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DSA parameters"); + p = (BigInteger) val.getValue(); + val = dsaParams.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DSA parameters"); + q = (BigInteger) val.getValue(); + val = dsaParams.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DSA parameters"); + g = (BigInteger) val.getValue(); + } + DERReader dsaPub = new DERReader(publicKey); + val = dsaPub.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DSA parameters"); + Y = (BigInteger) val.getValue(); + return new GnuDSAPublicKey(Y, p, q, g); + } + else if (algId.equals(ID_RSA)) + { + DERReader rsaParams = new DERReader(publicKey); + if (!rsaParams.read().isConstructed()) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + return new GnuRSAPublicKey(new RSAPublicKeySpec( + (BigInteger) rsaParams.read().getValue(), + (BigInteger) rsaParams.read().getValue())); + } + else if (algId.equals(ID_DH)) + { + if (algParams == null) + throw new InvalidKeySpecException("missing DH parameters"); + DERReader dhParams = new DERReader(algParams); + val = dhParams.read(); + BigInteger p, g, q, Y; + if (!val.isConstructed()) + throw new InvalidKeySpecException("malformed DH parameters"); + val = dhParams.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DH parameters"); + p = (BigInteger) val.getValue(); + val = dhParams.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DH parameters"); + g = (BigInteger) val.getValue(); + val = dhParams.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DH parameters"); + q = (BigInteger) val.getValue(); + DERReader dhPub = new DERReader(publicKey); + val = dhPub.read(); + if (!(val.getValue() instanceof BigInteger)) + throw new InvalidKeySpecException("malformed DH parameters"); + Y = (BigInteger) val.getValue(); + return (PublicKey) new GnuDHPublicKey(new DHParameterSpec(p, g), Y, q); + } + else + throw new InvalidKeySpecException("unknown algorithm: " + algId); + } + catch (IOException ioe) + { + throw new InvalidKeySpecException(ioe.getMessage()); + } + } + + public PrivateKey engineGeneratePrivate(KeySpec spec) + throws InvalidKeySpecException + { + if (!(spec instanceof PKCS8EncodedKeySpec)) + { + throw new InvalidKeySpecException("only supports PKCS8 key specs"); + } + DERReader der = new DERReader(((PKCS8EncodedKeySpec) spec).getEncoded()); + try + { + DERValue pki = der.read(); + if (!pki.isConstructed()) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + DERValue val = der.read(); + if (!(val.getValue() instanceof BigInteger)) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + DERValue alg = der.read(); + if (!alg.isConstructed()) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + val = der.read(); + if (!(val.getValue() instanceof OID)) + { + throw new InvalidKeySpecException("malformed encoded key"); + } + OID algId = (OID) val.getValue(); + byte[] algParams = null; + if (alg.getLength() > val.getEncodedLength()) + { + val = der.read(); + algParams = val.getEncoded(); + if (val.isConstructed()) + der.skip(val.getLength()); + } + byte[] privateKey = (byte[]) der.read().getValue(); + if (algId.equals(ID_DSA)) + { + if (algParams == null) + { + throw new InvalidKeySpecException("missing DSA parameters"); + } + AlgorithmParameters params = AlgorithmParameters.getInstance("DSA"); + params.init(algParams); + DSAParameterSpec dsaSpec = (DSAParameterSpec) + params.getParameterSpec(DSAParameterSpec.class); + DERReader dsaPriv = new DERReader(privateKey); + return new GnuDSAPrivateKey((BigInteger) dsaPriv.read().getValue(), + dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()); + } + else if (algId.equals(ID_RSA)) + { + DERReader rsaParams = new DERReader(privateKey); + if (!rsaParams.read().isConstructed()) + throw new InvalidKeySpecException("malformed encoded key"); + return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec( + (BigInteger) rsaParams.read().getValue(), // n + (BigInteger) rsaParams.read().getValue(), // e + (BigInteger) rsaParams.read().getValue(), // d + (BigInteger) rsaParams.read().getValue(), // p + (BigInteger) rsaParams.read().getValue(), // q + (BigInteger) rsaParams.read().getValue(), // d mod (p - 1) + (BigInteger) rsaParams.read().getValue(), // d mod (q - 1) + (BigInteger) rsaParams.read().getValue())); // (inv q) mod p + } + else + throw new InvalidKeySpecException("unknown algorithm: " + algId); + } + catch (InvalidParameterSpecException iapse) + { + throw new InvalidKeySpecException(iapse.getMessage()); + } + catch (NoSuchAlgorithmException nsae) + { + throw new InvalidKeySpecException(nsae.getMessage()); + } + catch (IOException ioe) + { + throw new InvalidKeySpecException(ioe.getMessage()); + } + } + + public KeySpec engineGetKeySpec(Key key, Class speClass) + throws InvalidKeySpecException + { + if ((key instanceof PrivateKey) && key.getFormat().equals("PKCS#8") + && speClass.isAssignableFrom(PKCS8EncodedKeySpec.class)) + return new PKCS8EncodedKeySpec(key.getEncoded()); + else if ((key instanceof PublicKey) && key.getFormat().equals("X.509") + && speClass.isAssignableFrom(X509EncodedKeySpec.class)) + return new X509EncodedKeySpec(key.getEncoded()); + else + throw new InvalidKeySpecException(); + } + + public Key engineTranslateKey(Key key) throws InvalidKeyException + { + throw new InvalidKeyException("translating keys not supported"); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/Gnu.java b/libjava/classpath/gnu/java/security/provider/Gnu.java new file mode 100644 index 00000000000..849f63c1601 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/Gnu.java @@ -0,0 +1,168 @@ +/* Gnu.java --- Gnu provider main class + Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.Provider; + +public final class Gnu extends Provider +{ + public Gnu() + { + super("GNU", 1.0, "GNU provider v1.0 implementing SHA-1, MD5, DSA, RSA, X.509 Certificates and CRLs, PKIX certificate path validators, Collection cert stores"); + + AccessController.doPrivileged (new PrivilegedAction() + { + public Object run() + { + // Note that all implementation class names are referenced by using + // Class.getName(). That way when we staticly link the Gnu provider + // we automatically get all the implementation classes. + + // Signature + put("Signature.SHA1withDSA", + gnu.java.security.provider.DSASignature.class.getName()); + + put("Alg.Alias.Signature.DSS", "SHA1withDSA"); + put("Alg.Alias.Signature.DSA", "SHA1withDSA"); + put("Alg.Alias.Signature.SHAwithDSA", "SHA1withDSA"); + put("Alg.Alias.Signature.DSAwithSHA", "SHA1withDSA"); + put("Alg.Alias.Signature.DSAwithSHA1", "SHA1withDSA"); + put("Alg.Alias.Signature.SHA/DSA", "SHA1withDSA"); + put("Alg.Alias.Signature.SHA-1/DSA", "SHA1withDSA"); + put("Alg.Alias.Signature.SHA1/DSA", "SHA1withDSA"); + put("Alg.Alias.Signature.OID.1.2.840.10040.4.3", "SHA1withDSA"); + put("Alg.Alias.Signature.1.2.840.10040.4.3", "SHA1withDSA"); + put("Alg.Alias.Signature.1.3.14.3.2.13", "SHA1withDSA"); + put("Alg.Alias.Signature.1.3.14.3.2.27", "SHA1withDSA"); + + put("Signature.MD2withRSA", MD2withRSA.class.getName()); + put("Signature.MD2withRSA ImplementedIn", "Software"); + put("Alg.Alias.Signature.md2WithRSAEncryption", "MD2withRSA"); + put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.2", "MD2withRSA"); + put("Alg.Alias.Signature.1.2.840.113549.1.1.2", "MD2withRSA"); + + put("Signature.MD4withRSA", MD4withRSA.class.getName()); + put("Signature.MD4withRSA ImplementedIn", "Software"); + put("Alg.Alias.Signature.md4WithRSAEncryption", "MD4withRSA"); + put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.3", "MD4withRSA"); + put("Alg.Alias.Signature.1.2.840.113549.1.1.3", "MD4withRSA"); + + put("Signature.MD5withRSA", MD5withRSA.class.getName()); + put("Signature.MD5withRSA ImplementedIn", "Software"); + put("Alg.Alias.Signature.md5WithRSAEncryption", "MD5withRSA"); + put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.4", "MD5withRSA"); + put("Alg.Alias.Signature.1.2.840.113549.1.1.4", "MD5withRSA"); + + put("Signature.SHA1withRSA", SHA1withRSA.class.getName()); + put("Signature.SHA1withRSA ImplementedIn", "Software"); + put("Alg.Alias.Signature.sha-1WithRSAEncryption", "SHA1withRSA"); + put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.5", "SHA1withRSA"); + put("Alg.Alias.Signature.1.2.840.113549.1.1.5", "SHA1withRSA"); + + // Key Pair Generator + put("KeyPairGenerator.DSA", + gnu.java.security.provider.DSAKeyPairGenerator.class.getName()); + + put("Alg.Alias.KeyPairGenerator.OID.1.2.840.10040.4.1", "DSA"); + put("Alg.Alias.KeyPairGenerator.1.2.840.10040.4.1", "DSA"); + put("Alg.Alias.KeyPairGenerator.1.3.14.3.2.12", "DSA"); + + // Key Factory + put("KeyFactory.DSA", + gnu.java.security.provider.DSAKeyFactory.class.getName()); + + put("KeyFactory.Encoded", EncodedKeyFactory.class.getName()); + put("KeyFactory.Encoded ImplementedIn", "Software"); + put("Alg.Alias.KeyFactory.X.509", "Encoded"); + put("Alg.Alias.KeyFactory.X509", "Encoded"); + put("Alg.Alias.KeyFactory.PKCS#8", "Encoded"); + put("Alg.Alias.KeyFactory.PKCS8", "Encoded"); + + put("KeyFactory.RSA", RSAKeyFactory.class.getName()); + + put("Alg.Alias.KeyFactory.OID.1.2.840.10040.4.1", "DSA"); + put("Alg.Alias.KeyFactory.1.2.840.10040.4.1", "DSA"); + put("Alg.Alias.KeyFactory.1.3.14.3.2.12", "DSA"); + + // Message Digests + put("MessageDigest.SHA", gnu.java.security.provider.SHA.class.getName()); + put("MessageDigest.MD5", gnu.java.security.provider.MD5.class.getName()); + + // Format "Alias", "Actual Name" + put("Alg.Alias.MessageDigest.SHA1", "SHA"); + put("Alg.Alias.MessageDigest.SHA-1", "SHA"); + put("Alg.Alias.MessageDigest.SHA-160", "SHA"); + + // Algorithm Parameters + put("AlgorithmParameters.DSA", + gnu.java.security.provider.DSAParameters.class.getName()); + + put("Alg.Alias.AlgorithmParameters.DSS", "DSA"); + put("Alg.Alias.AlgorithmParameters.SHAwithDSA", "DSA"); + put("Alg.Alias.AlgorithmParameters.OID.1.2.840.10040.4.3", "DSA"); + put("Alg.Alias.AlgorithmParameters.1.2.840.10040.4.3", "DSA"); + + // Algorithm Parameter Generator + put("AlgorithmParameterGenerator.DSA", + gnu.java.security.provider.DSAParameterGenerator.class.getName()); + + // SecureRandom + put("SecureRandom.SHA1PRNG", + gnu.java.security.provider.SHA1PRNG.class.getName()); + + // CertificateFactory + put("CertificateFactory.X509", X509CertificateFactory.class.getName()); + + put("CertificateFactory.X509 ImplementedIn", "Software"); + put("Alg.Alias.CertificateFactory.X.509", "X509"); + + // CertPathValidator + put("CertPathValidator.PKIX", PKIXCertPathValidatorImpl.class.getName()); + put("CertPathValidator.PKIX ImplementedIn", "Software"); + + // CertStore + put("CertStore.Collection", CollectionCertStoreImpl.class.getName()); + + return null; + } + }); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/GnuDHPublicKey.java b/libjava/classpath/gnu/java/security/provider/GnuDHPublicKey.java new file mode 100644 index 00000000000..6e13f6bf20f --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/GnuDHPublicKey.java @@ -0,0 +1,115 @@ +/* GnuDHPublicKey.java -- A Diffie-Hellman public key. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERValue; + +import java.math.BigInteger; +import java.util.ArrayList; + +import javax.crypto.interfaces.DHPublicKey; +import javax.crypto.spec.DHParameterSpec; + +public class GnuDHPublicKey implements DHPublicKey +{ + + // Fields. + // ------------------------------------------------------------------------- + + private byte[] encoded; + private final DHParameterSpec params; + private final BigInteger Y; + private final BigInteger q; + + // Constructor. + // ------------------------------------------------------------------------- + + public GnuDHPublicKey(DHParameterSpec params, BigInteger Y, BigInteger q) + { + this.params = params; + this.Y = Y; + this.q = q; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public BigInteger getY() + { + return Y; + } + + public DHParameterSpec getParams() + { + return params; + } + + public String getAlgorithm() + { + return "DH"; + } + + public String getFormat() + { + return "X.509"; + } + + public byte[] getEncoded() + { + if (encoded != null) + return (byte[]) encoded.clone(); + ArrayList spki = new ArrayList(2); + ArrayList alg = new ArrayList(2); + alg.add(new DERValue(DER.OBJECT_IDENTIFIER, new OID("1.2.840.10046.2.1"))); + ArrayList param = new ArrayList(3); + param.add(new DERValue(DER.INTEGER, params.getP())); + param.add(new DERValue(DER.INTEGER, params.getG())); + param.add(new DERValue(DER.INTEGER, q)); + alg.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, param)); + spki.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, alg)); + spki.add(new DERValue(DER.BIT_STRING, new BitString(Y.toByteArray()))); + encoded = new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, spki).getEncoded(); + if (encoded != null) + return (byte[]) encoded.clone(); + return null; + } +} diff --git a/libjava/classpath/gnu/java/security/provider/GnuDSAPrivateKey.java b/libjava/classpath/gnu/java/security/provider/GnuDSAPrivateKey.java new file mode 100644 index 00000000000..aac2faab229 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/GnuDSAPrivateKey.java @@ -0,0 +1,147 @@ +/* GnuDSAPrivateKey.java --- Gnu DSA Private Key + Copyright (C) 1999,2003,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERValue; +import gnu.java.security.der.DERWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.security.interfaces.DSAParams; +import java.security.interfaces.DSAPrivateKey; +import java.security.spec.DSAParameterSpec; +import java.util.ArrayList; + +public class GnuDSAPrivateKey implements DSAPrivateKey +{ + private byte[] encodedKey; + BigInteger x; + BigInteger p; + BigInteger q; + BigInteger g; + + public GnuDSAPrivateKey(BigInteger x, BigInteger p, BigInteger q, BigInteger g ) + { + this.x = x; + this.p = p; + this.q = q; + this.g = g; + } + + public String getAlgorithm() + { + return "DSA"; + } + + public String getFormat() + { + return "PKCS#8"; + } + + /** + * Encodes this key as a <code>PrivateKeyInfo</code>, as described in + * PKCS #8. The ASN.1 specification for this structure is: + * + * <blockquote><pre> + * PrivateKeyInfo ::= SEQUENCE { + * version Version, + * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, + * privateKey PrivateKey, + * attributes [0] IMPLICIT Attributes OPTIONAL } + * + * Version ::= INTEGER + * + * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier + * + * PrivateKey ::= OCTET STRING + * + * Attributes ::= SET OF Attribute + * </pre></blockquote> + * + * <p>DSA private keys (in Classpath at least) have no attributes. + */ + public byte[] getEncoded() + { + if (encodedKey != null) + return (byte[]) encodedKey.clone(); + try + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ArrayList pki = new ArrayList(3); + pki.add(new DERValue(DER.INTEGER, BigInteger.ZERO)); + ArrayList algId = new ArrayList(2); + algId.add(new DERValue(DER.OBJECT_IDENTIFIER, + new OID("1.2.840.10040.4.1"))); + ArrayList algParams = new ArrayList(3); + algParams.add(new DERValue(DER.INTEGER, p)); + algParams.add(new DERValue(DER.INTEGER, q)); + algParams.add(new DERValue(DER.INTEGER, g)); + algId.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, algParams)); + pki.add(new DERValue(DER.OCTET_STRING, x.toByteArray())); + DERWriter.write(out, new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, pki)); + return (byte[]) (encodedKey = out.toByteArray()).clone(); + } + catch (IOException ioe) + { + return null; + } + } + + public DSAParams getParams() + { + return (DSAParams)(new DSAParameterSpec(p,q,g)); + } + + public BigInteger getX() + { + return x; + } + + public String toString() + { + return "GnuDSAPrivateKey: x=" + + (x != null ? x.toString(16) : "null") + " p=" + + (p != null ? p.toString(16) : "null") + " q=" + + (q != null ? q.toString(16) : "null") + " g=" + + (g != null ? g.toString(16) : "null"); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/GnuDSAPublicKey.java b/libjava/classpath/gnu/java/security/provider/GnuDSAPublicKey.java new file mode 100644 index 00000000000..41195fa992c --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/GnuDSAPublicKey.java @@ -0,0 +1,137 @@ +/* GnuDSAPublicKey.java --- Gnu DSA Public Key + Copyright (C) 1999,2003,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERValue; +import gnu.java.security.der.DERWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.security.interfaces.DSAParams; +import java.security.interfaces.DSAPublicKey; +import java.security.spec.DSAParameterSpec; +import java.util.ArrayList; + +public class GnuDSAPublicKey implements DSAPublicKey +{ + private byte[] encodedKey; + BigInteger y; + BigInteger p; + BigInteger q; + BigInteger g; + + public GnuDSAPublicKey(BigInteger y, BigInteger p, BigInteger q, BigInteger g ) + { + this.y = y; + this.p = p; + this.q = q; + this.g = g; + } + + public String getAlgorithm() + { + return "DSA"; + } + + public String getFormat() + { + return "X.509"; + } + + /** + * The encoded form of DSA public keys is: + * + * <blockquote><pre> + * SubjectPublicKeyInfo ::= SEQUENCE { + * algorithm AlgorithmIdentifier, + * subjectPublicKey BIT STRING } + * </pre></blockquote> + */ + public byte[] getEncoded() + { + if (encodedKey != null) + return (byte[]) encodedKey.clone(); + try + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ArrayList spki = new ArrayList(2); + ArrayList alg = new ArrayList(2); + alg.add(new DERValue(DER.OBJECT_IDENTIFIER, + new OID("1.2.840.113549.1.1.1"))); + ArrayList params = new ArrayList(3); + params.add(new DERValue(DER.INTEGER, p)); + params.add(new DERValue(DER.INTEGER, q)); + params.add(new DERValue(DER.INTEGER, g)); + alg.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, params)); + spki.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, alg)); + spki.add(new DERValue(DER.BIT_STRING, new BitString(y.toByteArray()))); + DERWriter.write(out, new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, spki)); + return (byte[]) (encodedKey = out.toByteArray()).clone(); + } + catch (IOException ioe) + { + return null; + } + } + + public DSAParams getParams() + { + if (p == null || q == null || g == null) + return null; + return (DSAParams)(new DSAParameterSpec(p,q,g)); + } + + public BigInteger getY() + { + return y; + } + + public String toString() + { + return + "GnuDSAPublicKey: y=" + (y != null ? y.toString(16) : "(null)") + + " p=" + (p != null ? p.toString(16) : "(null)") + + " q=" + (q != null ? q.toString(16) : "(null)") + + " g=" + (g != null ? g.toString(16) : "(null)"); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/GnuRSAPrivateKey.java b/libjava/classpath/gnu/java/security/provider/GnuRSAPrivateKey.java new file mode 100644 index 00000000000..b09fc88bc5c --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/GnuRSAPrivateKey.java @@ -0,0 +1,164 @@ +/* GnuRSAPrivateKey.java -- GNU RSA private key. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERValue; + +import java.math.BigInteger; +import java.security.interfaces.RSAPrivateCrtKey; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.util.ArrayList; + +class GnuRSAPrivateKey implements RSAPrivateCrtKey +{ + + // Fields. + // ------------------------------------------------------------------------- + + private final RSAPrivateCrtKeySpec spec; + private byte[] encodedKey; + + // Constructor. + // ------------------------------------------------------------------------- + + public GnuRSAPrivateKey(RSAPrivateCrtKeySpec spec) + { + this.spec = spec; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public BigInteger getModulus() + { + return spec.getModulus(); + } + + public BigInteger getPrivateExponent() + { + return spec.getPrivateExponent(); + } + + public BigInteger getCrtCoefficient() + { + return spec.getCrtCoefficient(); + } + + public BigInteger getPrimeExponentP() + { + return spec.getPrimeExponentP(); + } + + public BigInteger getPrimeExponentQ() + { + return spec.getPrimeExponentQ(); + } + + public BigInteger getPrimeP() + { + return spec.getPrimeP(); + } + + public BigInteger getPrimeQ() + { + return spec.getPrimeQ(); + } + + public BigInteger getPublicExponent() + { + return spec.getPublicExponent(); + } + + public String getAlgorithm() + { + return "RSA"; + } + + public String getFormat() + { + return "PKCS#8"; + } + + /** + * The encoded form is: + * + * <pre> + * RSAPrivateKey ::= SEQUENCE { + * version Version, + * modulus INTEGER, -- n + * publicExponent INTEGER, -- e + * privateExponent INTEGER, -- d + * prime1 INTEGER, -- p + * prime2 INTEGER, -- q + * exponent1 INTEGER, -- d mod (p-1) + * exponent2 INTEGER, -- d mod (q-1) + * coefficient INTEGER -- (inverse of q) mod p } + * </pre> + * + * <p>Which is in turn encoded in a PrivateKeyInfo structure from PKCS#8. + */ + public byte[] getEncoded() + { + if (encodedKey != null) + return (byte[]) encodedKey.clone(); + ArrayList key = new ArrayList(9); + key.add(new DERValue(DER.INTEGER, BigInteger.ZERO)); + key.add(new DERValue(DER.INTEGER, getModulus())); + key.add(new DERValue(DER.INTEGER, getPublicExponent())); + key.add(new DERValue(DER.INTEGER, getPrivateExponent())); + key.add(new DERValue(DER.INTEGER, getPrimeP())); + key.add(new DERValue(DER.INTEGER, getPrimeQ())); + key.add(new DERValue(DER.INTEGER, getPrimeExponentP())); + key.add(new DERValue(DER.INTEGER, getPrimeExponentQ())); + key.add(new DERValue(DER.INTEGER, getCrtCoefficient())); + DERValue pk = new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, key); + ArrayList pki = new ArrayList(3); + pki.add(new DERValue(DER.INTEGER, BigInteger.ZERO)); + ArrayList alg = new ArrayList(2); + alg.add(new DERValue(DER.OBJECT_IDENTIFIER, + new OID("1.2.840.113549.1.1.1"))); + alg.add(new DERValue(DER.NULL, null)); + pki.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, alg)); + pki.add(new DERValue(DER.OCTET_STRING, pk.getEncoded())); + encodedKey = new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, pki).getEncoded(); + return (byte[]) encodedKey.clone(); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/GnuRSAPublicKey.java b/libjava/classpath/gnu/java/security/provider/GnuRSAPublicKey.java new file mode 100644 index 00000000000..a35e761c066 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/GnuRSAPublicKey.java @@ -0,0 +1,109 @@ +/* GnuRSAPublicKey.java -- GNU RSA public key. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERValue; + +import java.math.BigInteger; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.RSAPublicKeySpec; +import java.util.ArrayList; + +class GnuRSAPublicKey implements RSAPublicKey +{ + + // Fields. + // ------------------------------------------------------------------------- + + private final RSAPublicKeySpec spec; + private byte[] encodedKey; + + // Constructor. + // ------------------------------------------------------------------------- + + public GnuRSAPublicKey(RSAPublicKeySpec spec) + { + this.spec = spec; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public BigInteger getModulus() + { + return spec.getModulus(); + } + + public BigInteger getPublicExponent() + { + return spec.getPublicExponent(); + } + + public String getAlgorithm() + { + return "RSA"; + } + + public String getFormat() + { + return "X.509"; + } + + public byte[] getEncoded() + { + if (encodedKey != null) + return (byte[]) encodedKey.clone(); + ArrayList key = new ArrayList(2); + key.add(new DERValue(DER.INTEGER, getModulus())); + key.add(new DERValue(DER.INTEGER, getPublicExponent())); + DERValue rsapk = new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, key); + ArrayList alg = new ArrayList(2); + alg.add(new DERValue(DER.OBJECT_IDENTIFIER, + new OID("1.2.840.113549.1.1.1"))); + alg.add(new DERValue(DER.NULL, null)); + ArrayList spki = new ArrayList(2); + spki.add(new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, alg)); + spki.add(new DERValue(DER.BIT_STRING, new BitString(rsapk.getEncoded()))); + encodedKey = new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, spki).getEncoded(); + return (byte[]) encodedKey.clone(); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/MD2withRSA.java b/libjava/classpath/gnu/java/security/provider/MD2withRSA.java new file mode 100644 index 00000000000..a72ae5588dc --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/MD2withRSA.java @@ -0,0 +1,54 @@ +/* MD2withRSA.java -- MD2 with RSA encryption signatures. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class MD2withRSA extends RSA +{ + + // Constructor. + // ------------------------------------------------------------------------- + + public MD2withRSA() throws NoSuchAlgorithmException + { + super(MessageDigest.getInstance("MD2"), DIGEST_ALGORITHM.getChild(2)); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/MD4withRSA.java b/libjava/classpath/gnu/java/security/provider/MD4withRSA.java new file mode 100644 index 00000000000..76a6a1ad033 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/MD4withRSA.java @@ -0,0 +1,54 @@ +/* MD4withRSA.java -- MD4 with RSA encryption signatures. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class MD4withRSA extends RSA +{ + + // Constructor. + // ------------------------------------------------------------------------- + + public MD4withRSA() throws NoSuchAlgorithmException + { + super(MessageDigest.getInstance("MD4"), DIGEST_ALGORITHM.getChild(4)); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/MD5.java b/libjava/classpath/gnu/java/security/provider/MD5.java new file mode 100644 index 00000000000..1534eb91089 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/MD5.java @@ -0,0 +1,338 @@ +/* MD5.java -- Class implementing the MD5 algorithm as specified in RFC1321. + Copyright (C) 1999, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; +import java.security.MessageDigest; + +/** + This class implements the MD5 algorithm as described in RFC1321. + + @see java.security.MessageDigest +*/ +public class MD5 extends MessageDigest implements Cloneable +{ + private final int W[] = new int[16]; + private long bytecount; + private int A; + private int B; + private int C; + private int D; + + public MD5() + { + super("MD5"); + engineReset (); + } + + public Object clone() + { + return new MD5 (this); + } + + private MD5 (MD5 copy) + { + this (); + bytecount = copy.bytecount; + A = copy.A; + B = copy.B; + C = copy.C; + D = copy.D; + System.arraycopy (copy.W, 0, W, 0, 16); + } + + public int engineGetDigestLength() + { + return 16; + } + + // Intialize the A,B,C,D needed for the hash + public void engineReset() + { + bytecount = 0; + A = 0x67452301; + B = 0xefcdab89; + C = 0x98badcfe; + D = 0x10325476; + for(int i = 0; i < 16; i++) + W[i] = 0; + } + + public void engineUpdate (byte b) + { + int i = (int)bytecount % 64; + int shift = (3 - i % 4) * 8; + int idx = i / 4; + + // if you could index ints, this would be: W[idx][shift/8] = b + W[idx] = (W[idx] & ~(0xff << shift)) | ((b & 0xff) << shift); + + // if we've filled up a block, then process it + if ((++ bytecount) % 64 == 0) + munch (); + } + + public void engineUpdate (byte bytes[], int off, int len) + { + if (len < 0) + throw new ArrayIndexOutOfBoundsException (); + + int end = off + len; + while (off < end) + engineUpdate (bytes[off++]); + } + + public byte[] engineDigest() + { + long bitcount = bytecount * 8; + engineUpdate ((byte)0x80); // 10000000 in binary; the start of the padding + + // add the rest of the padding to fill this block out, but leave 8 + // bytes to put in the original bytecount + while ((int)bytecount % 64 != 56) + engineUpdate ((byte)0); + + // add the length of the original, unpadded block to the end of + // the padding + W[14] = SWAP((int)(0xffffffff & bitcount)); + W[15] = SWAP((int)(0xffffffff & (bitcount >>> 32))); + bytecount += 8; + + // digest the fully padded block + munch (); + + A = SWAP(A); + B = SWAP(B); + C = SWAP(C); + D = SWAP(D); + byte[] result = new byte[] {(byte)(A >>> 24), (byte)(A >>> 16), + (byte)(A >>> 8), (byte)A, + (byte)(B >>> 24), (byte)(B >>> 16), + (byte)(B >>> 8), (byte)B, + (byte)(C >>> 24), (byte)(C >>> 16), + (byte)(C >>> 8), (byte)C, + (byte)(D >>> 24), (byte)(D >>> 16), + (byte)(D >>> 8), (byte)D}; + + engineReset (); + return result; + } + + private int F( int X, int Y, int Z) + { + return ((X & Y) | (~X & Z)); + } + + private int G( int X, int Y, int Z) + { + return ((X & Z) | (Y & ~Z)); + } + + private int H( int X, int Y, int Z) + { + return (X ^ Y ^ Z); + } + + private int I( int X, int Y, int Z) + { + return (Y ^ (X | ~Z)); + } + + private int rotateLeft( int i, int count) + { + //Taken from FIPS 180-1 + return ( (i << count) | (i >>> (32 - count)) ) ; + } + + /* Round 1. */ + private int FF( int a, int b, int c, int d, int k, int s, int i) + { + /* Let [abcd k s i] denote the operation */ + a += F(b,c,d) + k + i; + return b + rotateLeft(a, s); + } + /* Round 2. */ + private int GG( int a, int b, int c, int d, int k, int s, int i) + { + /* Let [abcd k s i] denote the operation */ + a += G(b,c,d) + k + i; + return b + rotateLeft(a, s); + } + /* Round 3. */ + private int HH( int a, int b, int c, int d, int k, int s, int i) + { + /* Let [abcd k s t] denote the operation */ + a += H(b,c,d) + k + i; + return b + rotateLeft(a, s); + } + + /* Round 4. */ + private int II( int a, int b, int c, int d, int k, int s, int i) + { + /* Let [abcd k s t] denote the operation */ + a += I(b,c,d) + k + i; + return b + rotateLeft(a, s); + } + + private int SWAP(int n) + { + //Copied from md5.c in FSF Gnu Privacy Guard 0.9.2 + return (( (0xff & n) << 24) | ((n & 0xff00) << 8) | ((n >>> 8) & 0xff00) | (n >>> 24)); + } + + private void munch() + { + int AA,BB,CC,DD, j; + int X[] = new int[16]; + + /* Copy block i into X. */ + for(j = 0; j < 16; j++) + X[j] = SWAP(W[j]); + + /* Save A as AA, B as BB, C as CC, and D as DD. */ + AA = A; + BB = B; + CC = C; + DD = D; + + /* The hex constants are from md5.c + in FSF Gnu Privacy Guard 0.9.2 */ + /* Round 1. */ + /* Let [abcd k s i] denote the operation + a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ + /* Do the following 16 operations. */ + A = FF(A,B,C,D, X[0], 7, 0xd76aa478); + D = FF(D,A,B,C, X[1], 12, 0xe8c7b756); + C = FF(C,D,A,B, X[2], 17, 0x242070db); + B = FF(B,C,D,A, X[3], 22, 0xc1bdceee); + + A = FF(A,B,C,D, X[4], 7, 0xf57c0faf); + D = FF(D,A,B,C, X[5], 12, 0x4787c62a); + C = FF(C,D,A,B, X[6], 17, 0xa8304613); + B = FF(B,C,D,A, X[7], 22, 0xfd469501); + + A = FF(A,B,C,D, X[8], 7, 0x698098d8); + D = FF(D,A,B,C, X[9], 12, 0x8b44f7af); + C = FF(C,D,A,B, X[10], 17, 0xffff5bb1); + B = FF(B,C,D,A, X[11], 22, 0x895cd7be); + + A = FF(A,B,C,D, X[12], 7, 0x6b901122); + D = FF(D,A,B,C, X[13], 12, 0xfd987193); + C = FF(C,D,A,B, X[14], 17, 0xa679438e); + B = FF(B,C,D,A, X[15], 22, 0x49b40821); + + /* Round 2. */ + /* Let [abcd k s i] denote the operation + a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */ + /* Do the following 16 operations. */ + A = GG(A,B,C,D, X[1], 5, 0xf61e2562); + D = GG(D,A,B,C, X[6], 9, 0xc040b340); + C = GG(C,D,A,B, X[11], 14, 0x265e5a51); + B = GG(B,C,D,A, X[0], 20, 0xe9b6c7aa); + + A = GG(A,B,C,D, X[5], 5, 0xd62f105d); + D = GG(D,A,B,C, X[10], 9, 0x02441453); + C = GG(C,D,A,B, X[15], 14, 0xd8a1e681); + B = GG(B,C,D,A, X[4], 20, 0xe7d3fbc8); + + A = GG(A,B,C,D, X[9], 5, 0x21e1cde6); + D = GG(D,A,B,C, X[14], 9, 0xc33707d6); + C = GG(C,D,A,B, X[3], 14, 0xf4d50d87); + B = GG(B,C,D,A, X[8], 20, 0x455a14ed); + + A = GG(A,B,C,D, X[13], 5, 0xa9e3e905); + D = GG(D,A,B,C, X[2], 9, 0xfcefa3f8); + C = GG(C,D,A,B, X[7], 14, 0x676f02d9); + B = GG(B,C,D,A, X[12], 20, 0x8d2a4c8a); + + /* Round 3. */ + /* Let [abcd k s t] denote the operation + a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */ + /* Do the following 16 operations. */ + A = HH(A,B,C,D, X[5], 4, 0xfffa3942); + D = HH(D,A,B,C, X[8], 11, 0x8771f681); + C = HH(C,D,A,B, X[11], 16, 0x6d9d6122); + B = HH(B,C,D,A, X[14], 23, 0xfde5380c); + + A = HH(A,B,C,D, X[1], 4, 0xa4beea44); + D = HH(D,A,B,C, X[4], 11, 0x4bdecfa9); + C = HH(C,D,A,B, X[7], 16, 0xf6bb4b60); + B = HH(B,C,D,A, X[10], 23, 0xbebfbc70); + + A = HH(A,B,C,D, X[13], 4, 0x289b7ec6); + D = HH(D,A,B,C, X[0], 11, 0xeaa127fa); + C = HH(C,D,A,B, X[3], 16, 0xd4ef3085); + B = HH(B,C,D,A, X[6], 23, 0x04881d05); + + A = HH(A,B,C,D, X[9], 4, 0xd9d4d039); + D = HH(D,A,B,C, X[12], 11, 0xe6db99e5); + C = HH(C,D,A,B, X[15], 16, 0x1fa27cf8); + B = HH(B,C,D,A, X[2], 23, 0xc4ac5665); + + /* Round 4. */ + /* Let [abcd k s t] denote the operation + a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */ + /* Do the following 16 operations. */ + A = II(A,B,C,D, X[0], 6, 0xf4292244); + D = II(D,A,B,C, X[7], 10, 0x432aff97); + C = II(C,D,A,B, X[14], 15, 0xab9423a7); + B = II(B,C,D,A, X[5], 21, 0xfc93a039); + + A = II(A,B,C,D, X[12], 6, 0x655b59c3); + D = II(D,A,B,C, X[3], 10, 0x8f0ccc92); + C = II(C,D,A,B, X[10], 15, 0xffeff47d); + B = II(B,C,D,A, X[1], 21, 0x85845dd1); + + A = II(A,B,C,D, X[8], 6, 0x6fa87e4f); + D = II(D,A,B,C, X[15], 10, 0xfe2ce6e0); + C = II(C,D,A,B, X[6], 15, 0xa3014314); + B = II(B,C,D,A, X[13], 21, 0x4e0811a1); + + A = II(A,B,C,D, X[4], 6, 0xf7537e82); + D = II(D,A,B,C, X[11], 10, 0xbd3af235); + C = II(C,D,A,B, X[2], 15, 0x2ad7d2bb); + B = II(B,C,D,A, X[9], 21, 0xeb86d391); + + /* Then perform the following additions. (That is increment each + of the four registers by the value it had before this block + was started.) */ + A = A + AA; + B = B + BB; + C = C + CC; + D = D + DD; + } +} diff --git a/libjava/classpath/gnu/java/security/provider/MD5withRSA.java b/libjava/classpath/gnu/java/security/provider/MD5withRSA.java new file mode 100644 index 00000000000..721d897ed24 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/MD5withRSA.java @@ -0,0 +1,54 @@ +/* MD5withRSA.java -- MD5 with RSA encryption signatures. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class MD5withRSA extends RSA +{ + + // Constructor. + // ------------------------------------------------------------------------- + + public MD5withRSA() throws NoSuchAlgorithmException + { + super(MessageDigest.getInstance("MD5"), DIGEST_ALGORITHM.getChild(5)); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/PKIXCertPathValidatorImpl.java b/libjava/classpath/gnu/java/security/provider/PKIXCertPathValidatorImpl.java new file mode 100644 index 00000000000..1268b169d9b --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/PKIXCertPathValidatorImpl.java @@ -0,0 +1,701 @@ +/* PKIXCertPathValidatorImpl.java -- PKIX certificate path validator. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.x509.GnuPKIExtension; +import gnu.java.security.x509.PolicyNodeImpl; +import gnu.java.security.x509.X509CRLSelectorImpl; +import gnu.java.security.x509.X509CertSelectorImpl; +import gnu.java.security.x509.ext.BasicConstraints; +import gnu.java.security.x509.ext.CertificatePolicies; +import gnu.java.security.x509.ext.Extension; +import gnu.java.security.x509.ext.KeyUsage; +import gnu.java.security.x509.ext.PolicyConstraint; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.PublicKey; +import java.security.cert.CRL; +import java.security.cert.CertPath; +import java.security.cert.CertPathParameters; +import java.security.cert.CertPathValidatorException; +import java.security.cert.CertPathValidatorResult; +import java.security.cert.CertPathValidatorSpi; +import java.security.cert.CertStore; +import java.security.cert.CertStoreException; +import java.security.cert.CertificateException; +import java.security.cert.PKIXCertPathChecker; +import java.security.cert.PKIXCertPathValidatorResult; +import java.security.cert.PKIXParameters; +import java.security.cert.TrustAnchor; +import java.security.cert.X509CRL; +import java.security.cert.X509Certificate; +import java.security.interfaces.DSAParams; +import java.security.interfaces.DSAPublicKey; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +/** + * An implementation of the Public Key Infrastructure's X.509 + * certificate path validation algorithm. + * + * <p>See <a href="http://www.ietf.org/rfc/rfc3280.txt">RFC 3280: + * Internet X.509 Public Key Infrastructure Certificate and + * Certificate Revocation List (CRL) Profile</a>. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class PKIXCertPathValidatorImpl extends CertPathValidatorSpi +{ + + // Constants. + // ------------------------------------------------------------------------- + + private static final boolean DEBUG = false; + private static void debug (String msg) + { + System.err.print (">> PKIXCertPathValidatorImpl: "); + System.err.println (msg); + } + + public static final String ANY_POLICY = "2.5.29.32.0"; + + // Constructor. + // ------------------------------------------------------------------------- + + public PKIXCertPathValidatorImpl() + { + super(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public CertPathValidatorResult engineValidate(CertPath path, + CertPathParameters params) + throws CertPathValidatorException, InvalidAlgorithmParameterException + { + if (!(params instanceof PKIXParameters)) + throw new InvalidAlgorithmParameterException("not a PKIXParameters object"); + + // First check if the certificate path is valid. + // + // This means that: + // + // (a) for all x in {1, ..., n-1}, the subject of certificate x is + // the issuer of certificate x+1; + // + // (b) for all x in {1, ..., n}, the certificate was valid at the + // time in question. + // + // Because this is the X.509 algorithm, we also check if all + // cerificates are of type X509Certificate. + + PolicyNodeImpl rootNode = new PolicyNodeImpl(); + Set initPolicies = ((PKIXParameters) params).getInitialPolicies(); + rootNode.setValidPolicy(ANY_POLICY); + rootNode.setCritical(false); + rootNode.setDepth(0); + if (initPolicies != null) + rootNode.addAllExpectedPolicies(initPolicies); + else + rootNode.addExpectedPolicy(ANY_POLICY); + List checks = ((PKIXParameters) params).getCertPathCheckers(); + List l = path.getCertificates(); + if (l == null || l.size() == 0) + throw new CertPathValidatorException(); + X509Certificate[] p = null; + try + { + p = (X509Certificate[]) l.toArray(new X509Certificate[l.size()]); + } + catch (ClassCastException cce) + { + throw new CertPathValidatorException("invalid certificate path"); + } + + String sigProvider = ((PKIXParameters) params).getSigProvider(); + PublicKey prevKey = null; + Date now = ((PKIXParameters) params).getDate(); + if (now == null) + now = new Date(); + LinkedList policyConstraints = new LinkedList(); + for (int i = p.length - 1; i >= 0; i--) + { + try + { + p[i].checkValidity(now); + } + catch (CertificateException ce) + { + throw new CertPathValidatorException(ce.toString()); + } + Set uce = getCritExts(p[i]); + for (Iterator check = checks.iterator(); check.hasNext(); ) + { + try + { + ((PKIXCertPathChecker) check.next()).check(p[i], uce); + } + catch (Exception x) + { + } + } + + PolicyConstraint constr = null; + if (p[i] instanceof GnuPKIExtension) + { + Extension pcx = + ((GnuPKIExtension) p[i]).getExtension (PolicyConstraint.ID); + if (pcx != null) + constr = (PolicyConstraint) pcx.getValue(); + } + else + { + byte[] pcx = p[i].getExtensionValue (PolicyConstraint.ID.toString()); + if (pcx != null) + { + try + { + constr = new PolicyConstraint (pcx); + } + catch (Exception x) + { + } + } + } + if (constr != null && constr.getRequireExplicitPolicy() >= 0) + { + policyConstraints.add (new int[] + { p.length-i, constr.getRequireExplicitPolicy() }); + } + + updatePolicyTree(p[i], rootNode, p.length-i, (PKIXParameters) params, + checkExplicitPolicy (p.length-i, policyConstraints)); + + // The rest of the tests involve this cert's relationship with the + // next in the path. If this cert is the end entity, we can stop. + if (i == 0) + break; + + basicSanity(p, i); + PublicKey pubKey = null; + try + { + pubKey = p[i].getPublicKey(); + if (pubKey instanceof DSAPublicKey) + { + DSAParams dsa = ((DSAPublicKey) pubKey).getParams(); + // If the DSA public key is missing its parameters, use those + // from the previous cert's key. + if (dsa == null || dsa.getP() == null || dsa.getG() == null + || dsa.getQ() == null) + { + if (prevKey == null) + throw new InvalidKeyException("DSA keys not chainable"); + if (!(prevKey instanceof DSAPublicKey)) + throw new InvalidKeyException("DSA keys not chainable"); + dsa = ((DSAPublicKey) prevKey).getParams(); + pubKey = new GnuDSAPublicKey(((DSAPublicKey) pubKey).getY(), + dsa.getP(), dsa.getQ(), dsa.getG()); + } + } + if (sigProvider == null) + p[i-1].verify(pubKey); + else + p[i-1].verify(pubKey, sigProvider); + prevKey = pubKey; + } + catch (Exception e) + { + throw new CertPathValidatorException(e.toString()); + } + if (!p[i].getSubjectDN().equals(p[i-1].getIssuerDN())) + throw new CertPathValidatorException("issuer DN mismatch"); + boolean[] issuerUid = p[i-1].getIssuerUniqueID(); + boolean[] subjectUid = p[i].getSubjectUniqueID(); + if (issuerUid != null && subjectUid != null) + if (!Arrays.equals(issuerUid, subjectUid)) + throw new CertPathValidatorException("UID mismatch"); + + // Check the certificate against the revocation lists. + if (((PKIXParameters) params).isRevocationEnabled()) + { + X509CRLSelectorImpl selector = new X509CRLSelectorImpl(); + try + { + selector.addIssuerName(p[i].getSubjectDN()); + } + catch (IOException ioe) + { + throw new CertPathValidatorException("error selecting CRLs"); + } + List certStores = ((PKIXParameters) params).getCertStores(); + List crls = new LinkedList(); + for (Iterator it = certStores.iterator(); it.hasNext(); ) + { + CertStore cs = (CertStore) it.next(); + try + { + Collection c = cs.getCRLs(selector); + crls.addAll(c); + } + catch (CertStoreException cse) + { + } + } + if (crls.isEmpty()) + throw new CertPathValidatorException("no CRLs for issuer"); + boolean certOk = false; + for (Iterator it = crls.iterator(); it.hasNext(); ) + { + CRL crl = (CRL) it.next(); + if (!(crl instanceof X509CRL)) + continue; + X509CRL xcrl = (X509CRL) crl; + if (!checkCRL(xcrl, p, now, p[i], pubKey, certStores)) + continue; + if (xcrl.isRevoked(p[i-1])) + throw new CertPathValidatorException("certificate is revoked"); + else + certOk = true; + } + if (!certOk) + throw new CertPathValidatorException("certificate's validity could not be determined"); + } + } + rootNode.setReadOnly(); + + // Now ensure that the first certificate in the chain was issued + // by a trust anchor. + Exception cause = null; + Set anchors = ((PKIXParameters) params).getTrustAnchors(); + for (Iterator i = anchors.iterator(); i.hasNext(); ) + { + TrustAnchor anchor = (TrustAnchor) i.next(); + X509Certificate anchorCert = null; + PublicKey anchorKey = null; + if (anchor.getTrustedCert() != null) + { + anchorCert = anchor.getTrustedCert(); + anchorKey = anchorCert.getPublicKey(); + } + else + anchorKey = anchor.getCAPublicKey(); + if (anchorKey == null) + continue; + try + { + if (anchorCert == null) + anchorCert.checkValidity(now); + p[p.length-1].verify(anchorKey); + if (anchorCert != null && anchorCert.getBasicConstraints() >= 0 + && anchorCert.getBasicConstraints() < p.length) + continue; + + if (((PKIXParameters) params).isRevocationEnabled()) + { + X509CRLSelectorImpl selector = new X509CRLSelectorImpl(); + if (anchorCert != null) + try + { + selector.addIssuerName(anchorCert.getSubjectDN()); + } + catch (IOException ioe) + { + } + else + selector.addIssuerName(anchor.getCAName()); + List certStores = ((PKIXParameters) params).getCertStores(); + List crls = new LinkedList(); + for (Iterator it = certStores.iterator(); it.hasNext(); ) + { + CertStore cs = (CertStore) it.next(); + try + { + Collection c = cs.getCRLs(selector); + crls.addAll(c); + } + catch (CertStoreException cse) + { + } + } + if (crls.isEmpty()) + continue; + for (Iterator it = crls.iterator(); it.hasNext(); ) + { + CRL crl = (CRL) it.next(); + if (!(crl instanceof X509CRL)) + continue; + X509CRL xcrl = (X509CRL) crl; + try + { + xcrl.verify(anchorKey); + } + catch (Exception x) + { + continue; + } + Date nextUpdate = xcrl.getNextUpdate(); + if (nextUpdate != null && nextUpdate.compareTo(now) < 0) + continue; + if (xcrl.isRevoked(p[p.length-1])) + throw new CertPathValidatorException("certificate is revoked"); + } + } + + // The chain is valid; return the result. + return new PKIXCertPathValidatorResult(anchor, rootNode, + p[0].getPublicKey()); + } + catch (Exception ignored) + { + cause = ignored; + continue; + } + } + + // The path is not valid. + CertPathValidatorException cpve = + new CertPathValidatorException("path validation failed"); + if (cause != null) + cpve.initCause (cause); + throw cpve; + } + + // Own methods. + // ------------------------------------------------------------------------- + + /** + * Check if a given CRL is acceptable for checking the revocation status + * of certificates in the path being checked. + * + * <p>The CRL is accepted iff:</p> + * + * <ol> + * <li>The <i>nextUpdate</i> field (if present) is in the future.</li> + * <li>The CRL does not contain any unsupported critical extensions.</li> + * <li>The CRL is signed by one of the certificates in the path, or,</li> + * <li>The CRL is signed by the given public key and was issued by the + * public key's subject, or,</li> + * <li>The CRL is signed by a certificate in the given cert stores, and + * that cert is signed by one of the certificates in the path.</li> + * </ol> + * + * @param crl The CRL being checked. + * @param path The path this CRL is being checked against. + * @param now The value to use as 'now'. + * @param pubKeySubject The subject of the public key. + * @param pubKey The public key to check. + * @return True if the CRL is acceptable. + */ + private static boolean checkCRL(X509CRL crl, X509Certificate[] path, Date now, + X509Certificate pubKeyCert, PublicKey pubKey, + List certStores) + { + Date nextUpdate = crl.getNextUpdate(); + if (nextUpdate != null && nextUpdate.compareTo(now) < 0) + return false; + if (crl.hasUnsupportedCriticalExtension()) + return false; + for (int i = 0; i < path.length; i++) + { + if (!path[i].getSubjectDN().equals(crl.getIssuerDN())) + continue; + boolean[] keyUsage = path[i].getKeyUsage(); + if (keyUsage != null) + { + if (!keyUsage[KeyUsage.CRL_SIGN]) + continue; + } + try + { + crl.verify(path[i].getPublicKey()); + return true; + } + catch (Exception x) + { + } + } + if (crl.getIssuerDN().equals(pubKeyCert.getSubjectDN())) + { + try + { + boolean[] keyUsage = pubKeyCert.getKeyUsage(); + if (keyUsage != null) + { + if (!keyUsage[KeyUsage.CRL_SIGN]) + throw new Exception(); + } + crl.verify(pubKey); + return true; + } + catch (Exception x) + { + } + } + try + { + X509CertSelectorImpl select = new X509CertSelectorImpl(); + select.addSubjectName(crl.getIssuerDN()); + List certs = new LinkedList(); + for (Iterator it = certStores.iterator(); it.hasNext(); ) + { + CertStore cs = (CertStore) it.next(); + try + { + certs.addAll(cs.getCertificates(select)); + } + catch (CertStoreException cse) + { + } + } + for (Iterator it = certs.iterator(); it.hasNext(); ) + { + X509Certificate c = (X509Certificate) it.next(); + for (int i = 0; i < path.length; i++) + { + if (!c.getIssuerDN().equals(path[i].getSubjectDN())) + continue; + boolean[] keyUsage = c.getKeyUsage(); + if (keyUsage != null) + { + if (!keyUsage[KeyUsage.CRL_SIGN]) + continue; + } + try + { + c.verify(path[i].getPublicKey()); + crl.verify(c.getPublicKey()); + return true; + } + catch (Exception x) + { + } + } + if (c.getIssuerDN().equals(pubKeyCert.getSubjectDN())) + { + c.verify(pubKey); + crl.verify(c.getPublicKey()); + } + } + } + catch (Exception x) + { + } + return false; + } + + private static Set getCritExts(X509Certificate cert) + { + HashSet s = new HashSet(); + if (cert instanceof GnuPKIExtension) + { + Collection exts = ((GnuPKIExtension) cert).getExtensions(); + for (Iterator it = exts.iterator(); it.hasNext(); ) + { + Extension ext = (Extension) it.next(); + if (ext.isCritical() && !ext.isSupported()) + s.add(ext.getOid().toString()); + } + } + else + s.addAll(cert.getCriticalExtensionOIDs()); + return s; + } + + /** + * Perform a basic sanity check on the CA certificate at <code>index</code>. + */ + private static void basicSanity(X509Certificate[] path, int index) + throws CertPathValidatorException + { + X509Certificate cert = path[index]; + int pathLen = 0; + for (int i = index - 1; i > 0; i--) + { + if (!path[i].getIssuerDN().equals(path[i].getSubjectDN())) + pathLen++; + } + Extension e = null; + if (cert instanceof GnuPKIExtension) + { + e = ((GnuPKIExtension) cert).getExtension(BasicConstraints.ID); + } + else + { + try + { + e = new Extension(cert.getExtensionValue(BasicConstraints.ID.toString())); + } + catch (Exception x) + { + } + } + if (e == null) + throw new CertPathValidatorException("no basicConstraints"); + BasicConstraints bc = (BasicConstraints) e.getValue(); + if (!bc.isCA()) + throw new CertPathValidatorException("certificate cannot be used to verify signatures"); + if (bc.getPathLengthConstraint() >= 0 && bc.getPathLengthConstraint() < pathLen) + throw new CertPathValidatorException("path is too long"); + + boolean[] keyUsage = cert.getKeyUsage(); + if (keyUsage != null) + { + if (!keyUsage[KeyUsage.KEY_CERT_SIGN]) + throw new CertPathValidatorException("certificate cannot be used to sign certificates"); + } + } + + private static void updatePolicyTree(X509Certificate cert, PolicyNodeImpl root, + int depth, PKIXParameters params, + boolean explicitPolicy) + throws CertPathValidatorException + { + if (DEBUG) debug("updatePolicyTree depth == " + depth); + Set nodes = new HashSet(); + LinkedList stack = new LinkedList(); + Iterator current = null; + stack.addLast(Collections.singleton(root).iterator()); + do + { + current = (Iterator) stack.removeLast(); + while (current.hasNext()) + { + PolicyNodeImpl p = (PolicyNodeImpl) current.next(); + if (DEBUG) debug("visiting node == " + p); + if (p.getDepth() == depth - 1) + { + if (DEBUG) debug("added node"); + nodes.add(p); + } + else + { + if (DEBUG) debug("skipped node"); + stack.addLast(current); + current = p.getChildren(); + } + } + } + while (!stack.isEmpty()); + + Extension e = null; + CertificatePolicies policies = null; + List qualifierInfos = null; + if (cert instanceof GnuPKIExtension) + { + e = ((GnuPKIExtension) cert).getExtension(CertificatePolicies.ID); + if (e != null) + policies = (CertificatePolicies) e.getValue(); + } + + List cp = null; + if (policies != null) + cp = policies.getPolicies(); + else + cp = Collections.EMPTY_LIST; + boolean match = false; + if (DEBUG) debug("nodes are == " + nodes); + if (DEBUG) debug("cert policies are == " + cp); + for (Iterator it = nodes.iterator(); it.hasNext(); ) + { + PolicyNodeImpl parent = (PolicyNodeImpl) it.next(); + if (DEBUG) debug("adding policies to " + parent); + for (Iterator it2 = cp.iterator(); it2.hasNext(); ) + { + OID policy = (OID) it2.next(); + if (DEBUG) debug("trying to add policy == " + policy); + if (policy.toString().equals(ANY_POLICY) && + params.isAnyPolicyInhibited()) + continue; + PolicyNodeImpl child = new PolicyNodeImpl(); + child.setValidPolicy(policy.toString()); + child.addExpectedPolicy(policy.toString()); + if (parent.getExpectedPolicies().contains(policy.toString())) + { + parent.addChild(child); + match = true; + } + else if (parent.getExpectedPolicies().contains(ANY_POLICY)) + { + parent.addChild(child); + match = true; + } + else if (ANY_POLICY.equals (policy.toString())) + { + parent.addChild (child); + match = true; + } + if (match && policies != null) + { + List qualifiers = policies.getPolicyQualifierInfos (policy); + if (qualifiers != null) + child.addAllPolicyQualifiers (qualifiers); + } + } + } + if (!match && (params.isExplicitPolicyRequired() || explicitPolicy)) + throw new CertPathValidatorException("policy tree building failed"); + } + + private boolean checkExplicitPolicy (int depth, List explicitPolicies) + { + if (DEBUG) debug ("checkExplicitPolicy depth=" + depth); + for (Iterator it = explicitPolicies.iterator(); it.hasNext(); ) + { + int[] i = (int[]) it.next(); + int caDepth = i[0]; + int limit = i[1]; + if (DEBUG) debug (" caDepth=" + caDepth + " limit=" + limit); + if (depth - caDepth >= limit) + return true; + } + return false; + } +} diff --git a/libjava/classpath/gnu/java/security/provider/RSA.java b/libjava/classpath/gnu/java/security/provider/RSA.java new file mode 100644 index 00000000000..c3cfbbf79f1 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/RSA.java @@ -0,0 +1,311 @@ +/* RSA.java -- RSA PKCS#1 signatures. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.der.DERWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.SignatureException; +import java.security.SignatureSpi; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.util.ArrayList; + +public abstract class RSA extends SignatureSpi implements Cloneable +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + /** + * digestAlgorithm OBJECT IDENTIFIER ::= + * { iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) } + */ + protected static final OID DIGEST_ALGORITHM = new OID("1.2.840.113549.2"); + + protected final OID digestAlgorithm; + protected final MessageDigest md; + protected RSAPrivateKey signerKey; + protected RSAPublicKey verifierKey; + + // Constructor. + // ------------------------------------------------------------------------- + + protected RSA(MessageDigest md, OID digestAlgorithm) + { + super(); + this.md = md; + this.digestAlgorithm = digestAlgorithm; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public Object clone() throws CloneNotSupportedException + { + return super.clone(); + } + + protected Object engineGetParameter(String param) + { + throw new UnsupportedOperationException("deprecated"); + } + + protected void engineSetParameter(String param, Object value) + { + throw new UnsupportedOperationException("deprecated"); + } + + protected void engineInitSign(PrivateKey privateKey) + throws InvalidKeyException + { + if (!(privateKey instanceof RSAPrivateKey)) + throw new InvalidKeyException(); + verifierKey = null; + signerKey = (RSAPrivateKey) privateKey; + } + + protected void engineInitSign(PrivateKey privateKey, SecureRandom random) + throws InvalidKeyException + { + // This class does not need random bytes. + engineInitSign(privateKey); + } + + protected void engineInitVerify(PublicKey publicKey) + throws InvalidKeyException + { + if (!(publicKey instanceof RSAPublicKey)) + throw new InvalidKeyException(); + signerKey = null; + verifierKey = (RSAPublicKey) publicKey; + } + + protected void engineUpdate(byte b) throws SignatureException + { + if (signerKey == null && verifierKey == null) + throw new SignatureException("not initialized"); + md.update(b); + } + + protected void engineUpdate(byte[] buf, int off, int len) + throws SignatureException + { + if (signerKey == null && verifierKey == null) + throw new SignatureException("not initialized"); + md.update(buf, off, len); + } + + protected byte[] engineSign() throws SignatureException + { + if (signerKey == null) + throw new SignatureException("not initialized for signing"); + // + // The signature will be the RSA encrypted BER representation of + // the following: + // + // DigestInfo ::= SEQUENCE { + // digestAlgorithm DigestAlgorithmIdentifier, + // digest Digest } + // + // DigestAlgorithmIdentifier ::= AlgorithmIdentifier + // + // Digest ::= OCTET STRING + // + ArrayList digestAlg = new ArrayList(2); + digestAlg.add(new DERValue(DER.OBJECT_IDENTIFIER, digestAlgorithm)); + digestAlg.add(new DERValue(DER.NULL, null)); + ArrayList digestInfo = new ArrayList(2); + digestInfo.add(new DERValue(DER.SEQUENCE, digestAlg)); + digestInfo.add(new DERValue(DER.OCTET_STRING, md.digest())); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try + { + DERWriter.write(out, new DERValue(DER.SEQUENCE, digestInfo)); + } + catch (IOException ioe) + { + throw new SignatureException(ioe.toString()); + } + byte[] buf = out.toByteArray(); + md.reset(); + + // k = octect length of the modulus. + int k = signerKey.getModulus().bitLength(); + k = (k >>> 3) + ((k & 7) == 0 ? 0 : 1); + if (buf.length < k - 3) + { + throw new SignatureException("RSA modulus too small"); + } + byte[] d = new byte[k]; + + // Padding type 1: + // 00 | 01 | FF | ... | FF | 00 | D + d[1] = 0x01; + for (int i = 2; i < k - buf.length - 1; i++) + d[i] = (byte) 0xFF; + System.arraycopy(buf, 0, d, k - buf.length, buf.length); + + BigInteger eb = new BigInteger(d); + + byte[] ed = eb.modPow(signerKey.getPrivateExponent(), + signerKey.getModulus()).toByteArray(); + + // Ensure output is k octets long. + if (ed.length < k) + { + byte[] b = new byte[k]; + System.arraycopy(eb, 0, b, k - ed.length, ed.length); + ed = b; + } + else if (ed.length > k) + { + if (ed.length != k + 1) + { + throw new SignatureException("modPow result is larger than the modulus"); + } + // Maybe an extra 00 octect. + byte[] b = new byte[k]; + System.arraycopy(ed, 1, b, 0, k); + ed = b; + } + + return ed; + } + + protected int engineSign(byte[] out, int off, int len) + throws SignatureException + { + if (out == null || off < 0 || len < 0 || off+len > out.length) + throw new SignatureException("illegal output argument"); + byte[] result = engineSign(); + if (result.length > len) + throw new SignatureException("not enough space for signature"); + System.arraycopy(result, 0, out, off, result.length); + return result.length; + } + + protected boolean engineVerify(byte[] sig) throws SignatureException + { + if (verifierKey == null) + throw new SignatureException("not initialized for verifying"); + if (sig == null) + throw new SignatureException("no signature specified"); + int k = verifierKey.getModulus().bitLength(); + k = (k >>> 3) + ((k & 7) == 0 ? 0 : 1); + if (sig.length != k) + throw new SignatureException("signature is the wrong size (expecting " + + k + " bytes, got " + sig.length + ")"); + BigInteger ed = new BigInteger(1, sig); + byte[] eb = ed.modPow(verifierKey.getPublicExponent(), + verifierKey.getModulus()).toByteArray(); + + int i = 0; + if (eb[0] == 0x00) + { + for (i = 1; i < eb.length && eb[i] == 0x00; i++); + if (i == 1) + throw new SignatureException("wrong RSA padding"); + i--; + } + else if (eb[0] == 0x01) + { + for (i = 1; i < eb.length && eb[i] != 0x00; i++) + if (eb[i] != (byte) 0xFF) + throw new IllegalArgumentException("wrong RSA padding"); + } + else + throw new SignatureException("wrong RSA padding type"); + + byte[] d = new byte[eb.length-i-1]; + System.arraycopy(eb, i+1, d, 0, eb.length-i-1); + + DERReader der = new DERReader(d); + try + { + DERValue val = der.read(); + if (val.getTag() != DER.SEQUENCE) + throw new SignatureException("failed to parse DigestInfo"); + val = der.read(); + if (val.getTag() != DER.SEQUENCE) + throw new SignatureException("failed to parse DigestAlgorithmIdentifier"); + boolean sequenceIsBer = val.getLength() == 0; + val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new SignatureException("failed to parse object identifier"); + if (!val.getValue().equals(digestAlgorithm)) + throw new SignatureException("digest algorithms do not match"); + val = der.read(); + // We should never see parameters here, since they are never used. + if (val.getTag() != DER.NULL) + throw new SignatureException("cannot handle digest parameters"); + if (sequenceIsBer) + der.skip(1); // end-of-sequence byte. + val = der.read(); + if (val.getTag() != DER.OCTET_STRING) + throw new SignatureException("failed to parse Digest"); + return MessageDigest.isEqual(md.digest(), (byte[]) val.getValue()); + } + catch (IOException ioe) + { + throw new SignatureException(ioe.toString()); + } + } + + protected boolean engineVerify(byte[] sig, int off, int len) + throws SignatureException + { + if (sig == null || off < 0 || len < 0 || off+len > sig.length) + throw new SignatureException("illegal parameter"); + byte[] buf = new byte[len]; + System.arraycopy(sig, off, buf, 0, len); + return engineVerify(buf); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/RSAKeyFactory.java b/libjava/classpath/gnu/java/security/provider/RSAKeyFactory.java new file mode 100644 index 00000000000..d13cbe510a1 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/RSAKeyFactory.java @@ -0,0 +1,181 @@ +/* RSAKeyFactory.java -- RSA key factory. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.KeyFactorySpi; +import java.security.PrivateKey; +import java.security.PublicKey; + +import java.security.interfaces.RSAPrivateCrtKey; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; + +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.security.spec.RSAPrivateKeySpec; +import java.security.spec.RSAPublicKeySpec; +import java.security.spec.X509EncodedKeySpec; + +public class RSAKeyFactory extends KeyFactorySpi +{ + + // Default constructor. + // ------------------------------------------------------------------------- + + // Instance methods. + // ------------------------------------------------------------------------- + + protected PrivateKey engineGeneratePrivate(KeySpec spec) + throws InvalidKeySpecException + { + if (spec instanceof RSAPrivateCrtKeySpec) + { + return new GnuRSAPrivateKey((RSAPrivateCrtKeySpec) spec); + } + if (spec instanceof RSAPrivateKeySpec) + { + return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec( + ((RSAPrivateKeySpec) spec).getModulus(), null, + ((RSAPrivateKeySpec) spec).getPrivateExponent(), null, + null, null, null, null)); + } + if (spec instanceof PKCS8EncodedKeySpec) + { + EncodedKeyFactory ekf = new EncodedKeyFactory(); + PrivateKey pk = ekf.engineGeneratePrivate(spec); + if (pk instanceof RSAPrivateKey) + return pk; + } + throw new InvalidKeySpecException(); + } + + protected PublicKey engineGeneratePublic(KeySpec spec) + throws InvalidKeySpecException + { + if (spec instanceof RSAPublicKeySpec) + { + return new GnuRSAPublicKey((RSAPublicKeySpec) spec); + } + if (spec instanceof X509EncodedKeySpec) + { + EncodedKeyFactory ekf = new EncodedKeyFactory(); + PublicKey pk = ekf.engineGeneratePublic(spec); + if (pk instanceof RSAPublicKey) + return pk; + } + throw new InvalidKeySpecException(); + } + + protected KeySpec engineGetKeySpec(Key key, Class keySpec) + throws InvalidKeySpecException + { + if (keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class) + && (key instanceof RSAPrivateCrtKey)) + { + return new RSAPrivateCrtKeySpec( + ((RSAPrivateCrtKey) key).getModulus(), + ((RSAPrivateCrtKey) key).getPublicExponent(), + ((RSAPrivateCrtKey) key).getPrivateExponent(), + ((RSAPrivateCrtKey) key).getPrimeP(), + ((RSAPrivateCrtKey) key).getPrimeQ(), + ((RSAPrivateCrtKey) key).getPrimeExponentP(), + ((RSAPrivateCrtKey) key).getPrimeExponentQ(), + ((RSAPrivateCrtKey) key).getCrtCoefficient()); + } + if (keySpec.isAssignableFrom(RSAPrivateKeySpec.class) + && (key instanceof RSAPrivateKey)) + { + return new RSAPrivateKeySpec( + ((RSAPrivateCrtKey) key).getModulus(), + ((RSAPrivateCrtKey) key).getPrivateExponent()); + } + if (keySpec.isAssignableFrom(RSAPublicKeySpec.class) + && (key instanceof RSAPublicKey)) + { + return new RSAPublicKeySpec( + ((RSAPrivateCrtKey) key).getModulus(), + ((RSAPrivateCrtKey) key).getPublicExponent()); + } + if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class) + && key.getFormat().equalsIgnoreCase("PKCS#8")) + { + return new PKCS8EncodedKeySpec(key.getEncoded()); + } + if (keySpec.isAssignableFrom(X509EncodedKeySpec.class) + && key.getFormat().equalsIgnoreCase("X.509")) + { + return new X509EncodedKeySpec(key.getEncoded()); + } + throw new InvalidKeySpecException(); + } + + protected Key engineTranslateKey(Key key) throws InvalidKeyException + { + if (key instanceof RSAPrivateCrtKey) + { + return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec( + ((RSAPrivateCrtKey) key).getModulus(), + ((RSAPrivateCrtKey) key).getPublicExponent(), + ((RSAPrivateCrtKey) key).getPrivateExponent(), + ((RSAPrivateCrtKey) key).getPrimeP(), + ((RSAPrivateCrtKey) key).getPrimeQ(), + ((RSAPrivateCrtKey) key).getPrimeExponentP(), + ((RSAPrivateCrtKey) key).getPrimeExponentQ(), + ((RSAPrivateCrtKey) key).getCrtCoefficient())); + } + if (key instanceof RSAPrivateKey) + { + return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec( + ((RSAPrivateKey) key).getModulus(), null, + ((RSAPrivateKey) key).getPrivateExponent(), null, + null, null, null, null)); + } + if (key instanceof RSAPublicKey) + { + return new GnuRSAPublicKey(new RSAPublicKeySpec( + ((RSAPrivateCrtKey) key).getModulus(), + ((RSAPrivateCrtKey) key).getPublicExponent())); + } + throw new InvalidKeyException(); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/SHA.java b/libjava/classpath/gnu/java/security/provider/SHA.java new file mode 100644 index 00000000000..e3b09bc5603 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/SHA.java @@ -0,0 +1,242 @@ +/* SHA.java -- Class implementing the SHA-1 algorithm as specified in [1]. + Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.security.MessageDigest; + +/** + This class implements the SHA-1 algorithm as described in [1]. + + [1] Federal Information Processing Standards Publication 180-1. + Specifications for the Secure Hash Standard. April 17, 1995. + + @see java.security.MessageDigest +*/ +public class SHA extends MessageDigest implements Cloneable +{ + public SHA () + { + super("SHA"); + engineReset (); + } + + public int engineGetDigestLength() + { + return 20; + } + + public void engineUpdate (byte b) + { + int i = ((int)bytecount) & 0x3f; //wgs + int shift = (3 - i % 4) << 3; + int idx = i / 4; + + i = (int)b; + W[idx] = (W[idx] & ~(0xff << shift)) | ((i & 0xff) << shift); + + // if we've filled up a block, then process it + if (((++bytecount) & 0x3f) == 0) + munch (); + } + + // This could be optimized. + public void engineUpdate (byte bytes[], int off, int len) + { + if (len < 0) + throw new ArrayIndexOutOfBoundsException (); + + int end = off + len; + while (off < end) + engineUpdate (bytes[off++]); + } + + public void engineReset () + { + bytecount = 0; + // magic numbers from [1] p. 10. + H0 = 0x67452301; + H1 = 0xefcdab89; + H2 = 0x98badcfe; + H3 = 0x10325476; + H4 = 0xc3d2e1f0; + } + + public byte[] engineDigest () + { + long bitcount = bytecount << 3; + engineUpdate ((byte)0x80); // 10000000 in binary; the start of the padding + + // add the rest of the padding to fill this block out, but leave 8 + // bytes to put in the original bytecount + while ((bytecount & 0x3f) != 56) + engineUpdate ((byte)0); + + // add the length of the original, unpadded block to the end of + // the padding + W[14] = (int)(bitcount >>> 32); + W[15] = (int)bitcount; + bytecount += 8; + + // digest the fully padded block + munch (); + + byte[] result + = new byte[] {(byte)(H0 >>> 24), (byte)(H0 >>> 16), + (byte)(H0 >>> 8), (byte)H0, + (byte)(H1 >>> 24), (byte)(H1 >>> 16), + (byte)(H1 >>> 8), (byte)H1, + (byte)(H2 >>> 24), (byte)(H2 >>> 16), + (byte)(H2 >>> 8), (byte)H2, + (byte)(H3 >>> 24), (byte)(H3 >>> 16), + (byte)(H3 >>> 8), (byte)H3, + (byte)(H4 >>> 24), (byte)(H4 >>> 16), + (byte)(H4 >>> 8), (byte)H4}; + + engineReset (); + return result; + } + + // Process a single block. This is pretty much copied verbatim from + // [1] pp. 9, 10. + private void munch () + { + for (int t = 16; t < 80; ++ t) + { + int Wt = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]; + W[t] = Wt << 1 | Wt >>> 31; + } + + int A = H0; + int B = H1; + int C = H2; + int D = H3; + int E = H4; + + for (int t = 0; t < 20; ++ t) + { + int TEMP = (A << 5 | A >>> 27) // S^5(A) + + ((B & C) | (~B & D)) // f_t(B,C,D) + + E + W[t] + + 0x5a827999; // K_t + + E = D; + D = C; + C = B << 30 | B >>> 2; // S^30(B) + B = A; + A = TEMP; + } + + for (int t = 20; t < 40; ++ t) + { + int TEMP = (A << 5 | A >>> 27) // S^5(A) + + (B ^ C ^ D) // f_t(B,C,D) + + E + W[t] + + 0x6ed9eba1; // K_t + + E = D; + D = C; + C = B << 30 | B >>> 2; // S^30(B) + B = A; + A = TEMP; + } + + for (int t = 40; t < 60; ++ t) + { + int TEMP = (A << 5 | A >>> 27) // S^5(A) + + (B & C | B & D | C & D) // f_t(B,C,D) + + E + W[t] + + 0x8f1bbcdc; // K_t + + E = D; + D = C; + C = B << 30 | B >>> 2; // S^30(B) + B = A; + A = TEMP; + } + + for (int t = 60; t < 80; ++ t) + { + int TEMP = (A << 5 | A >>> 27) // S^5(A) + + (B ^ C ^ D) // f_t(B,C,D) + + E + W[t] + + 0xca62c1d6; // K_t + + E = D; + D = C; + C = B << 30 | B >>> 2; // S^30(B) + B = A; + A = TEMP; + } + + H0 += A; + H1 += B; + H2 += C; + H3 += D; + H4 += E; + + // Reset W by clearing it. + for (int t = 0; t < 80; ++ t) + W[t] = 0; + } + + public Object clone () + { + return new SHA (this); + } + + private SHA (SHA copy) + { + this (); + bytecount = copy.bytecount; + H0 = copy.H0; + H1 = copy.H1; + H2 = copy.H2; + H3 = copy.H3; + H4 = copy.H4; + System.arraycopy (copy.W, 0, W, 0, 80); + } + + private final int W[] = new int[80]; + private long bytecount; + private int H0; + private int H1; + private int H2; + private int H3; + private int H4; +} diff --git a/libjava/classpath/gnu/java/security/provider/SHA1PRNG.java b/libjava/classpath/gnu/java/security/provider/SHA1PRNG.java new file mode 100644 index 00000000000..e4058e3079b --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/SHA1PRNG.java @@ -0,0 +1,137 @@ +/* SHA1PRNG.java --- Secure Random SPI SHA1PRNG + Copyright (C) 1999, 2001, 2003, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import java.io.Serializable; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandomSpi; +import java.util.Random; + +public class SHA1PRNG extends SecureRandomSpi implements Serializable +{ + MessageDigest digest; + byte seed[]; + byte data[]; + int seedpos; + int datapos; + private boolean seeded = false; // set to true when we seed this + /** + * The size of seed. + */ + private static final int SEED_SIZE = 20; + /** + * The size of data. + */ + private static final int DATA_SIZE = 40; + + /** + * Create a new SHA-1 pseudo-random number generator. + */ + public SHA1PRNG() + { + try { + digest = MessageDigest.getInstance("SHA"); + } catch ( NoSuchAlgorithmException nsae) { +// System.out.println("Failed to find SHA Message Digest: " + nsae); +// nsae.printStackTrace(); + throw new InternalError ("no SHA implementation found"); + } + + seed = new byte[SEED_SIZE]; + seedpos = 0; + data = new byte[DATA_SIZE]; + datapos = SEED_SIZE; // try to force hashing a first block + } + + public void engineSetSeed(byte[] seed) + { + for(int i = 0; i < seed.length; i++) + this.seed[seedpos++ % SEED_SIZE] ^= seed[i]; + seedpos %= SEED_SIZE; + + } + + public void engineNextBytes(byte[] bytes) + { + ensureIsSeeded (); + int loc = 0; + while (loc < bytes.length) + { + int copy = Math.min (bytes.length - loc, SEED_SIZE - datapos); + + if (copy > 0) + { + System.arraycopy (data, datapos, bytes, loc, copy); + datapos += copy; + loc += copy; + } + else + { + // No data ready for copying, so refill our buffer. + System.arraycopy( seed, 0, data, SEED_SIZE, SEED_SIZE); + byte[] digestdata = digest.digest( data ); + System.arraycopy( digestdata, 0, data, 0, SEED_SIZE); + datapos = 0; + } + } + } + + public byte[] engineGenerateSeed(int numBytes) + { + byte tmp[] = new byte[numBytes]; + + engineNextBytes( tmp ); + return tmp; + } + + private void ensureIsSeeded() + { + if (!seeded) + { + new Random(0L).nextBytes(seed); + + byte[] digestdata = digest.digest(data); + System.arraycopy(digestdata, 0, data, 0, SEED_SIZE); + + seeded = true; + } + } + +} diff --git a/libjava/classpath/gnu/java/security/provider/SHA1withRSA.java b/libjava/classpath/gnu/java/security/provider/SHA1withRSA.java new file mode 100644 index 00000000000..0e63fdeeb52 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/SHA1withRSA.java @@ -0,0 +1,61 @@ +/* SHA1withRSA.java -- SHA-1 with RSA encryption signatures. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.security.OID; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class SHA1withRSA extends RSA +{ + + // Constant. + // ------------------------------------------------------------------------- + + private static final OID SHA1 = new OID("1.3.14.3.2.26"); + + // Constructor. + // ------------------------------------------------------------------------- + + public SHA1withRSA() throws NoSuchAlgorithmException + { + super(MessageDigest.getInstance("SHA-160"), SHA1); + } +} diff --git a/libjava/classpath/gnu/java/security/provider/X509CertificateFactory.java b/libjava/classpath/gnu/java/security/provider/X509CertificateFactory.java new file mode 100644 index 00000000000..1a415eabb05 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/X509CertificateFactory.java @@ -0,0 +1,305 @@ +/* X509CertificateFactory.java -- generates X.509 certificates. + Copyright (C) 2003 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.provider; + +import gnu.java.io.Base64InputStream; +import gnu.java.security.x509.X509CRL; +import gnu.java.security.x509.X509CertPath; +import gnu.java.security.x509.X509Certificate; + +import java.io.BufferedInputStream; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.security.cert.CRL; +import java.security.cert.CRLException; +import java.security.cert.CertPath; +import java.security.cert.Certificate; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactorySpi; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class X509CertificateFactory extends CertificateFactorySpi +{ + + // Constants. + // ------------------------------------------------------------------------ + + public static final String BEGIN_CERTIFICATE = "-----BEGIN CERTIFICATE-----"; + public static final String END_CERTIFICATE = "-----END CERTIFICATE-----"; + public static final String BEGIN_X509_CRL = "-----BEGIN X509 CRL-----"; + public static final String END_X509_CRL = "-----END X509 CRL-----"; + + // Constructors. + // ------------------------------------------------------------------------ + + public X509CertificateFactory() + { + super(); + } + + // Instance methods. + // ------------------------------------------------------------------------ + + public Certificate engineGenerateCertificate(InputStream inStream) + throws CertificateException + { + try + { + return generateCert(inStream); + } + catch (IOException ioe) + { + CertificateException ce = new CertificateException(ioe.getMessage()); + ce.initCause (ioe); + throw ce; + } + } + + public Collection engineGenerateCertificates(InputStream inStream) + throws CertificateException + { + LinkedList certs = new LinkedList(); + while (true) + { + try + { + certs.add(generateCert(inStream)); + } + catch (EOFException eof) + { + break; + } + catch (IOException ioe) + { + CertificateException ce = new CertificateException(ioe.getMessage()); + ce.initCause (ioe); + throw ce; + } + } + return certs; + } + + public CRL engineGenerateCRL(InputStream inStream) throws CRLException + { + try + { + return generateCRL(inStream); + } + catch (IOException ioe) + { + CRLException crle = new CRLException(ioe.getMessage()); + crle.initCause (ioe); + throw crle; + } + } + + public Collection engineGenerateCRLs(InputStream inStream) + throws CRLException + { + LinkedList crls = new LinkedList(); + while (true) + { + try + { + crls.add(generateCRL(inStream)); + } + catch (EOFException eof) + { + break; + } + catch (IOException ioe) + { + CRLException crle = new CRLException(ioe.getMessage()); + crle.initCause (ioe); + throw crle; + } + } + return crls; + } + + public CertPath engineGenerateCertPath(List certs) + { + return new X509CertPath(certs); + } + + public CertPath engineGenerateCertPath(InputStream in) + throws CertificateEncodingException + { + return new X509CertPath(in); + } + + public CertPath engineGenerateCertPath(InputStream in, String encoding) + throws CertificateEncodingException + { + return new X509CertPath(in, encoding); + } + + public Iterator engineGetCertPathEncodings() + { + return X509CertPath.ENCODINGS.iterator(); + } + + // Own methods. + // ------------------------------------------------------------------------ + + private X509Certificate generateCert(InputStream inStream) + throws IOException, CertificateException + { + if (inStream == null) + throw new CertificateException("missing input stream"); + if (!inStream.markSupported()) + inStream = new BufferedInputStream(inStream, 8192); + inStream.mark(20); + int i = inStream.read(); + if (i == -1) + throw new EOFException(); + + // If the input is in binary DER format, the first byte MUST be + // 0x30, which stands for the ASN.1 [UNIVERSAL 16], which is the + // UNIVERSAL SEQUENCE, with the CONSTRUCTED bit (0x20) set. + // + // So if we do not see 0x30 here we will assume it is in Base-64. + if (i != 0x30) + { + inStream.reset(); + StringBuffer line = new StringBuffer(80); + do + { + line.setLength(0); + do + { + i = inStream.read(); + if (i == -1) + throw new EOFException(); + if (i != '\n' && i != '\r') + line.append((char) i); + } + while (i != '\n' && i != '\r'); + } + while (!line.toString().equals(BEGIN_CERTIFICATE)); + X509Certificate ret = new X509Certificate( + new BufferedInputStream(new Base64InputStream(inStream), 8192)); + line.setLength(0); + line.append('-'); // Base64InputStream will eat this. + do + { + i = inStream.read(); + if (i == -1) + throw new EOFException(); + if (i != '\n' && i != '\r') + line.append((char) i); + } + while (i != '\n' && i != '\r'); + // XXX ??? + if (!line.toString().equals(END_CERTIFICATE)) + throw new CertificateException("no end-of-certificate marker"); + return ret; + } + else + { + inStream.reset(); + return new X509Certificate(inStream); + } + } + + private X509CRL generateCRL(InputStream inStream) + throws IOException, CRLException + { + if (inStream == null) + throw new CRLException("missing input stream"); + if (!inStream.markSupported()) + inStream = new BufferedInputStream(inStream, 8192); + inStream.mark(20); + int i = inStream.read(); + if (i == -1) + throw new EOFException(); + + // If the input is in binary DER format, the first byte MUST be + // 0x30, which stands for the ASN.1 [UNIVERSAL 16], which is the + // UNIVERSAL SEQUENCE, with the CONSTRUCTED bit (0x20) set. + // + // So if we do not see 0x30 here we will assume it is in Base-64. + if (i != 0x30) + { + inStream.reset(); + StringBuffer line = new StringBuffer(80); + do + { + line.setLength(0); + do + { + i = inStream.read(); + if (i == -1) + throw new EOFException(); + if (i != '\n' && i != '\r') + line.append((char) i); + } + while (i != '\n' && i != '\r'); + } + while (!line.toString().startsWith(BEGIN_X509_CRL)); + X509CRL ret = new X509CRL( + new BufferedInputStream(new Base64InputStream(inStream), 8192)); + line.setLength(0); + line.append('-'); // Base64InputStream will eat this. + do + { + i = inStream.read(); + if (i == -1) + throw new EOFException(); + if (i != '\n' && i != '\r') + line.append((char) i); + } + while (i != '\n' && i != '\r'); + // XXX ??? + if (!line.toString().startsWith(END_X509_CRL)) + throw new CRLException("no end-of-CRL marker"); + return ret; + } + else + { + inStream.reset(); + return new X509CRL(inStream); + } + } +} diff --git a/libjava/classpath/gnu/java/security/provider/package.html b/libjava/classpath/gnu/java/security/provider/package.html new file mode 100644 index 00000000000..641a22aff77 --- /dev/null +++ b/libjava/classpath/gnu/java/security/provider/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.provider package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.provider</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/util/Prime.java b/libjava/classpath/gnu/java/security/util/Prime.java new file mode 100644 index 00000000000..e493ce67503 --- /dev/null +++ b/libjava/classpath/gnu/java/security/util/Prime.java @@ -0,0 +1,164 @@ +/* Prime.java --- Prime number generation utilities + Copyright (C) 1999, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.util; +import java.math.BigInteger; +import java.util.Random; +//import java.security.SecureRandom; + +public final class Prime +{ + + /* + See IEEE P1363 A.15.4 (10/05/98 Draft) + */ + public static BigInteger generateRandomPrime( int pmin, int pmax, BigInteger f ) + { + BigInteger d; + + //Step 1 - generate prime + BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() ); + if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmin ) ) <= 0 ) + { + p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin ).subtract( p ) ); + } + + //Step 2 - test for even + if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0) + p = p.add( BigInteger.valueOf( 1 ) ); + + for(;;) + { + //Step 3 + if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0) + { + //Step 3.1 + p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) ); + p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) ); + p = p.subtract( BigInteger.valueOf( 1 ) ); + + //Step 3.2 + // put step 2 code here so looping code is cleaner + //Step 2 - test for even + if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0) + p = p.add( BigInteger.valueOf( 1 ) ); + continue; + } + + //Step 4 - compute GCD + d = p.subtract( BigInteger.valueOf(1) ); + d = d.gcd( f ); + + //Step 5 - test d + if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0) + { + //Step 5.1 - test primality + if( p.isProbablePrime( 1 ) == true ) + { + //Step 5.2; + return p; + } + } + //Step 6 + p = p.add( BigInteger.valueOf( 2 ) ); + + //Step 7 + } + } + + + /* + See IEEE P1363 A.15.5 (10/05/98 Draft) + */ + public static BigInteger generateRandomPrime( BigInteger r, BigInteger a, int pmin, int pmax, BigInteger f ) + { + BigInteger d, w; + + //Step 1 - generate prime + BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() ); + + steptwo:{ //Step 2 + w = p.mod( r.multiply( BigInteger.valueOf(2) )); + + //Step 3 + p = p.add( r.multiply( BigInteger.valueOf(2) ) ); + p = p.subtract( w ); + p = p.add(a); + + //Step 4 - test for even + if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0) + p = p.add( r ); + + for(;;) + { + //Step 5 + if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0) + { + //Step 5.1 + p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) ); + p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) ); + p = p.subtract( BigInteger.valueOf( 1 ) ); + + //Step 5.2 - goto to Step 2 + break steptwo; + } + + //Step 6 + d = p.subtract( BigInteger.valueOf(1) ); + d = d.gcd( f ); + + //Step 7 - test d + if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0) + { + //Step 7.1 - test primality + if( p.isProbablePrime( 1 ) == true ) + { + //Step 7.2; + return p; + } + } + //Step 8 + p = p.add( r.multiply( BigInteger.valueOf(2) ) ); + + //Step 9 + } + } + //Should never reach here but makes the compiler happy + return BigInteger.valueOf(0); + } +} diff --git a/libjava/classpath/gnu/java/security/util/package.html b/libjava/classpath/gnu/java/security/util/package.html new file mode 100644 index 00000000000..36dd33b7991 --- /dev/null +++ b/libjava/classpath/gnu/java/security/util/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.util package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.util</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/x509/GnuPKIExtension.java b/libjava/classpath/gnu/java/security/x509/GnuPKIExtension.java new file mode 100644 index 00000000000..8e74b8b24ac --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/GnuPKIExtension.java @@ -0,0 +1,59 @@ +/* GnuPKIExtension.java -- interface for GNU PKI extensions. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import gnu.java.security.OID; +import gnu.java.security.x509.ext.Extension; + +import java.security.cert.X509Extension; +import java.util.Collection; + +public interface GnuPKIExtension extends X509Extension +{ + + /** + * Returns the extension object for the given object identifier. + * + * @param oid The OID of the extension to get. + * @return The extension, or null if there is no such extension. + */ + Extension getExtension(OID oid); + + Collection getExtensions(); +} diff --git a/libjava/classpath/gnu/java/security/x509/PolicyNodeImpl.java b/libjava/classpath/gnu/java/security/x509/PolicyNodeImpl.java new file mode 100644 index 00000000000..72cb4a9ea91 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/PolicyNodeImpl.java @@ -0,0 +1,214 @@ +/* PolicyNodeImpl.java -- An implementation of a policy tree node. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import java.security.cert.PolicyNode; +import java.security.cert.PolicyQualifierInfo; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public final class PolicyNodeImpl implements PolicyNode +{ + + // Fields. + // ------------------------------------------------------------------------- + + private String policy; + private final Set expectedPolicies; + private final Set qualifiers; + private final Set children; + private PolicyNodeImpl parent; + private int depth; + private boolean critical; + private boolean readOnly; + + // Constructors. + // ------------------------------------------------------------------------- + + public PolicyNodeImpl() + { + expectedPolicies = new HashSet(); + qualifiers = new HashSet(); + children = new HashSet(); + readOnly = false; + critical = false; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public void addChild(PolicyNodeImpl node) + { + if (readOnly) + throw new IllegalStateException("read only"); + if (node.getParent() != null) + throw new IllegalStateException("already a child node"); + node.parent = this; + node.setDepth(depth + 1); + children.add(node); + } + + public Iterator getChildren() + { + return Collections.unmodifiableSet(children).iterator(); + } + + public int getDepth() + { + return depth; + } + + public void setDepth(int depth) + { + if (readOnly) + throw new IllegalStateException("read only"); + this.depth = depth; + } + + public void addAllExpectedPolicies(Set policies) + { + if (readOnly) + throw new IllegalStateException("read only"); + expectedPolicies.addAll(policies); + } + + public void addExpectedPolicy(String policy) + { + if (readOnly) + throw new IllegalStateException("read only"); + expectedPolicies.add(policy); + } + + public Set getExpectedPolicies() + { + return Collections.unmodifiableSet(expectedPolicies); + } + + public PolicyNode getParent() + { + return parent; + } + + public void addAllPolicyQualifiers (Collection qualifiers) + { + for (Iterator it = qualifiers.iterator(); it.hasNext(); ) + { + if (!(it.next() instanceof PolicyQualifierInfo)) + throw new IllegalArgumentException ("can only add PolicyQualifierInfos"); + } + qualifiers.addAll (qualifiers); + } + + public void addPolicyQualifier (PolicyQualifierInfo qualifier) + { + if (readOnly) + throw new IllegalStateException("read only"); + qualifiers.add(qualifier); + } + + public Set getPolicyQualifiers() + { + return Collections.unmodifiableSet(qualifiers); + } + + public String getValidPolicy() + { + return policy; + } + + public void setValidPolicy(String policy) + { + if (readOnly) + throw new IllegalStateException("read only"); + this.policy = policy; + } + + public boolean isCritical() + { + return critical; + } + + public void setCritical(boolean critical) + { + if (readOnly) + throw new IllegalStateException("read only"); + this.critical = critical; + } + + public void setReadOnly() + { + if (readOnly) + return; + readOnly = true; + for (Iterator it = getChildren(); it.hasNext(); ) + ((PolicyNodeImpl) it.next()).setReadOnly(); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < depth; i++) + buf.append(" "); + buf.append("("); + buf.append(PolicyNodeImpl.class.getName()); + buf.append(" (oid "); + buf.append(policy); + buf.append(") (depth "); + buf.append(depth); + buf.append(") (qualifiers "); + buf.append(qualifiers); + buf.append(") (critical "); + buf.append(critical); + buf.append(") (expectedPolicies "); + buf.append(expectedPolicies); + buf.append(") (children ("); + final String nl = System.getProperty("line.separator"); + for (Iterator it = getChildren(); it.hasNext(); ) + { + buf.append(nl); + buf.append(it.next().toString()); + } + buf.append(")))"); + return buf.toString(); + } +} diff --git a/libjava/classpath/gnu/java/security/x509/Util.java b/libjava/classpath/gnu/java/security/x509/Util.java new file mode 100644 index 00000000000..d27392026f8 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/Util.java @@ -0,0 +1,202 @@ +/* Util.java -- Miscellaneous utility methods. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +/** + * A collection of useful class methods. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public final class Util +{ + + // Constants. + // ------------------------------------------------------------------------- + + public static final String HEX = "0123456789abcdef"; + + // Class methods. + // ------------------------------------------------------------------------- + + /** + * Convert a byte array to a hexadecimal string, as though it were a + * big-endian arbitrarily-sized integer. + * + * @param buf The bytes to format. + * @param off The offset to start at. + * @param len The number of bytes to format. + * @return A hexadecimal representation of the specified bytes. + */ + public static String toHexString(byte[] buf, int off, int len) + { + StringBuffer str = new StringBuffer(); + for (int i = 0; i < len; i++) + { + str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F)); + str.append(HEX.charAt(buf[i+off] & 0x0F)); + } + return str.toString(); + } + + /** + * See {@link #toHexString(byte[],int,int)}. + */ + public static String toHexString(byte[] buf) + { + return Util.toHexString(buf, 0, buf.length); + } + + /** + * Convert a byte array to a hexadecimal string, separating octets + * with the given character. + * + * @param buf The bytes to format. + * @param off The offset to start at. + * @param len The number of bytes to format. + * @param sep The character to insert between octets. + * @return A hexadecimal representation of the specified bytes. + */ + public static String toHexString(byte[] buf, int off, int len, char sep) + { + StringBuffer str = new StringBuffer(); + for (int i = 0; i < len; i++) + { + str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F)); + str.append(HEX.charAt(buf[i+off] & 0x0F)); + if (i < len - 1) + str.append(sep); + } + return str.toString(); + } + + /** + * See {@link #toHexString(byte[],int,int,char)}. + */ + public static String toHexString(byte[] buf, char sep) + { + return Util.toHexString(buf, 0, buf.length, sep); + } + + /** + * Create a representation of the given byte array similar to the + * output of `hexdump -C', which is + * + * <p><pre>OFFSET SIXTEEN-BYTES-IN-HEX PRINTABLE-BYTES</pre> + * + * <p>The printable bytes show up as-is if they are printable and + * not a newline character, otherwise showing as '.'. + * + * @param buf The bytes to format. + * @param off The offset to start at. + * @param len The number of bytes to encode. + * @return The formatted string. + */ + public static String hexDump(byte[] buf, int off, int len, String prefix) + { + String nl = System.getProperty("line.separator"); + StringBuffer str = new StringBuffer(); + int i = 0; + while (i < len) + { + str.append(prefix); + str.append(Util.formatInt(i+off, 16, 8)); + str.append(" "); + String s = Util.toHexString(buf, i+off, Math.min(16, len-i), ' '); + str.append(s); + for (int j = 56 - (56 - s.length()); j < 56; j++) + str.append(" "); + for (int j = 0; j < Math.min(16, len - i); j++) + { + if ((buf[i+off+j] & 0xFF) < 0x20 || (buf[i+off+j] & 0xFF) > 0x7E) + str.append('.'); + else + str.append((char) (buf[i+off+j] & 0xFF)); + } + str.append(nl); + i += 16; + } + return str.toString(); + } + + /** + * See {@link #hexDump(byte[],int,int)}. + */ + public static String hexDump(byte[] buf, String prefix) + { + return hexDump(buf, 0, buf.length, prefix); + } + + /** + * Format an integer into the specified radix, zero-filled. + * + * @param i The integer to format. + * @param radix The radix to encode to. + * @param len The target length of the string. The string is + * zero-padded to this length, but may be longer. + * @return The formatted integer. + */ + public static String formatInt(int i, int radix, int len) + { + String s = Integer.toString(i, radix); + StringBuffer buf = new StringBuffer(); + for (int j = 0; j < len - s.length(); j++) + buf.append("0"); + buf.append(s); + return buf.toString(); + } + + /** + * Convert a hexadecimal string into its byte representation. + * + * @param hex The hexadecimal string. + * @return The converted bytes. + */ + public static byte[] toByteArray(String hex) + { + hex = hex.toLowerCase(); + byte[] buf = new byte[hex.length() / 2]; + int j = 0; + for (int i = 0; i < buf.length; i++) + { + buf[i] = (byte) ((Character.digit(hex.charAt(j++), 16) << 4) | + Character.digit(hex.charAt(j++), 16)); + } + return buf; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/X500DistinguishedName.java b/libjava/classpath/gnu/java/security/x509/X500DistinguishedName.java new file mode 100644 index 00000000000..daf746f5dc5 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X500DistinguishedName.java @@ -0,0 +1,548 @@ +/* X500DistinguishedName.java -- X.500 distinguished name. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class X500DistinguishedName implements Principal +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID CN = new OID("2.5.4.3"); + public static final OID C = new OID("2.5.4.6"); + public static final OID L = new OID("2.5.4.7"); + public static final OID ST = new OID("2.5.4.8"); + public static final OID STREET = new OID("2.5.4.9"); + public static final OID O = new OID("2.5.4.10"); + public static final OID OU = new OID("2.5.4.11"); + public static final OID T = new OID("2.5.4.12"); + public static final OID DNQ = new OID("2.5.4.46"); + public static final OID NAME = new OID("2.5.4.41"); + public static final OID GIVENNAME = new OID("2.5.4.42"); + public static final OID INITIALS = new OID("2.5.4.43"); + public static final OID GENERATION = new OID("2.5.4.44"); + public static final OID EMAIL = new OID("1.2.840.113549.1.9.1"); + public static final OID DC = new OID("0.9.2342.19200300.100.1.25"); + public static final OID UID = new OID("0.9.2342.19200300.100.1.1"); + + private List components; + private Map currentRdn; + private boolean fixed; + private String stringRep; + private byte[] encoded; + + // Constructors. + // ------------------------------------------------------------------------- + + public X500DistinguishedName() + { + components = new LinkedList(); + currentRdn = new LinkedHashMap(); + components.add(currentRdn); + } + + public X500DistinguishedName(String name) + { + this(); + try + { + parseString(name); + } + catch (IOException ioe) + { + throw new IllegalArgumentException(ioe.toString()); + } + } + + public X500DistinguishedName(byte[] encoded) throws IOException + { + this(); + parseDer(new DERReader(encoded)); + } + + public X500DistinguishedName(InputStream encoded) throws IOException + { + this(); + parseDer(new DERReader(encoded)); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public String getName() + { + return toString(); + } + + public void newRelativeDistinguishedName() + { + if (fixed || currentRdn.isEmpty()) return; + currentRdn = new LinkedHashMap(); + components.add(currentRdn); + } + + public int size() + { + return components.size(); + } + + public int countComponents() + { + int count = 0; + for (Iterator it = components.iterator(); it.hasNext(); ) + { + count += ((Map) it.next()).size(); + } + return count; + } + + public boolean containsComponent(OID oid, String value) + { + for (Iterator it = components.iterator(); it.hasNext(); ) + { + Map rdn = (Map) it.next(); + String s = (String) rdn.get(oid); + if (s == null) + continue; + if (compressWS(value).equalsIgnoreCase(compressWS(s))) + return true; + } + return false; + } + + public String getComponent(OID oid) + { + for (Iterator it = components.iterator(); it.hasNext(); ) + { + Map rdn = (Map) it.next(); + if (rdn.containsKey(oid)) + return (String) rdn.get(oid); + } + return null; + } + + public String getComponent(OID oid, int rdn) + { + if (rdn >= size()) + return null; + return (String) ((Map) components.get(rdn)).get(oid); + } + + public void putComponent(OID oid, String value) + { + currentRdn.put(oid, value); + } + + public void putComponent(String name, String value) + { + name = name.trim().toLowerCase(); + if (name.equals("cn")) + putComponent(CN, value); + else if (name.equals("c")) + putComponent(C, value); + else if (name.equals("l")) + putComponent(L, value); + else if (name.equals("street")) + putComponent(STREET, value); + else if (name.equals("st")) + putComponent(ST, value); + else if (name.equals("t")) + putComponent(T, value); + else if (name.equals("dnq")) + putComponent(DNQ, value); + else if (name.equals("name")) + putComponent(NAME, value); + else if (name.equals("givenname")) + putComponent(GIVENNAME, value); + else if (name.equals("initials")) + putComponent(INITIALS, value); + else if (name.equals("generation")) + putComponent(GENERATION, value); + else if (name.equals("email")) + putComponent(EMAIL, value); + else if (name.equals("dc")) + putComponent(DC, value); + else if (name.equals("uid")) + putComponent(UID, value); + else + putComponent(new OID(name), value); + } + + public void setUnmodifiable() + { + if (fixed) return; + fixed = true; + List newComps = new ArrayList(components.size()); + for (Iterator it = components.iterator(); it.hasNext(); ) + { + Map rdn = (Map) it.next(); + rdn = Collections.unmodifiableMap(rdn); + newComps.add(rdn); + } + components = Collections.unmodifiableList(newComps); + currentRdn = Collections.EMPTY_MAP; + } + + public int hashCode() + { + int sum = 0; + for (Iterator it = components.iterator(); it.hasNext(); ) + { + Map m = (Map) it.next(); + for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) + { + Map.Entry e = (Map.Entry) it2.next(); + sum += e.getKey().hashCode(); + sum += e.getValue().hashCode(); + } + } + return sum; + } + + public boolean equals(Object o) + { + if (!(o instanceof X500DistinguishedName)) + return false; + if (size() != ((X500DistinguishedName) o).size()) + return false; + for (int i = 0; i < size(); i++) + { + Map m = (Map) components.get(i); + for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) + { + Map.Entry e = (Map.Entry) it2.next(); + OID oid = (OID) e.getKey(); + String v1 = (String) e.getValue(); + String v2 = ((X500DistinguishedName) o).getComponent(oid, i); + if (!compressWS(v1).equalsIgnoreCase(compressWS(v2))) + return false; + } + } + return true; + } + + public String toString() + { + if (fixed && stringRep != null) + return stringRep; + StringBuffer str = new StringBuffer(); + for (Iterator it = components.iterator(); it.hasNext(); ) + { + Map m = (Map) it.next(); + for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it2.next(); + OID oid = (OID) entry.getKey(); + String value = (String) entry.getValue(); + if (oid.equals(CN)) + str.append("CN"); + else if (oid.equals(C)) + str.append("C"); + else if (oid.equals(L)) + str.append("L"); + else if (oid.equals(ST)) + str.append("ST"); + else if (oid.equals(STREET)) + str.append("STREET"); + else if (oid.equals(O)) + str.append("O"); + else if (oid.equals(OU)) + str.append("OU"); + else if (oid.equals(T)) + str.append("T"); + else if (oid.equals(DNQ)) + str.append("DNQ"); + else if (oid.equals(NAME)) + str.append("NAME"); + else + str.append(oid.toString()); + str.append('='); + str.append(value); + if (it2.hasNext()) + str.append("+"); + } + if (it.hasNext()) + str.append(','); + } + return (stringRep = str.toString()); + } + + public byte[] getDer() + { + if (fixed && encoded != null) + return (byte[]) encoded.clone(); + ArrayList name = new ArrayList(components.size()); + for (Iterator it = components.iterator(); it.hasNext(); ) + { + Map m = (Map) it.next(); + if (m.isEmpty()) + continue; + Set rdn = new HashSet(); + for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) + { + Map.Entry e = (Map.Entry) it.next(); + ArrayList atav = new ArrayList(2); + atav.add(new DERValue(DER.OBJECT_IDENTIFIER, e.getKey())); + atav.add(new DERValue(DER.UTF8_STRING, e.getValue())); + rdn.add(new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, atav)); + } + name.add(new DERValue(DER.SET|DER.CONSTRUCTED, rdn)); + } + DERValue val = new DERValue(DER.SEQUENCE|DER.CONSTRUCTED, name); + return (byte[]) (encoded = val.getEncoded()).clone(); + } + + // Own methods. + // ------------------------------------------------------------------------- + + private int sep; + + private void parseString(String str) throws IOException + { + Reader in = new StringReader(str); + while (true) + { + String key = readAttributeType(in); + if (key == null) + break; + String value = readAttributeValue(in); + putComponent(key, value); + if (sep == ',') + newRelativeDistinguishedName(); + } + setUnmodifiable(); + } + + private String readAttributeType(Reader in) throws IOException + { + StringBuffer buf = new StringBuffer(); + int ch; + while ((ch = in.read()) != '=') + { + if (ch == -1) + { + if (buf.length() > 0) + throw new EOFException(); + return null; + } + if (ch > 127) + throw new IOException("Invalid char: " + (char) ch); + if (Character.isLetterOrDigit((char) ch) || ch == '-' || ch == '.') + buf.append((char) ch); + else + throw new IOException("Invalid char: " + (char) ch); + } + return buf.toString(); + } + + private String readAttributeValue(Reader in) throws IOException + { + StringBuffer buf = new StringBuffer(); + int ch = in.read(); + if (ch == '#') + { + while (true) + { + ch = in.read(); + if (('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F') + || Character.isDigit((char) ch)) + buf.append((char) ch); + else if (ch == '+' || ch == ',') + { + sep = ch; + String hex = buf.toString(); + return new String(Util.toByteArray(hex)); + } + else + throw new IOException("illegal character: " + (char) ch); + } + } + else if (ch == '"') + { + while (true) + { + ch = in.read(); + if (ch == '"') + break; + else if (ch == '\\') + { + ch = in.read(); + if (ch == -1) + throw new EOFException(); + if (('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F') + || Character.isDigit((char) ch)) + { + int i = Character.digit((char) ch, 16) << 4; + ch = in.read(); + if (!(('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F') + || Character.isDigit((char) ch))) + throw new IOException("illegal hex char"); + i |= Character.digit((char) ch, 16); + buf.append((char) i); + } + else + buf.append((char) ch); + } + else + buf.append((char) ch); + } + sep = in.read(); + if (sep != '+' || sep != ',') + throw new IOException("illegal character: " + (char) ch); + return buf.toString(); + } + else + { + while (true) + { + switch (ch) + { + case '+': + case ',': + sep = ch; + return buf.toString(); + case '\\': + ch = in.read(); + if (ch == -1) + throw new EOFException(); + if (('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F') + || Character.isDigit((char) ch)) + { + int i = Character.digit((char) ch, 16) << 4; + ch = in.read(); + if (!(('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F') + || Character.isDigit((char) ch))) + throw new IOException("illegal hex char"); + i |= Character.digit((char) ch, 16); + buf.append((char) i); + } + else + buf.append((char) ch); + break; + case '=': + case '<': + case '>': + case '#': + case ';': + throw new IOException("illegal character: " + (char) ch); + case -1: + throw new EOFException(); + default: + buf.append((char) ch); + } + } + } + } + + private void parseDer(DERReader der) throws IOException + { + DERValue name = der.read(); + if (!name.isConstructed()) + throw new IOException("malformed Name"); + encoded = name.getEncoded(); + int len = 0; + while (len < name.getLength()) + { + DERValue rdn = der.read(); + if (!rdn.isConstructed()) + throw new IOException("badly formed RDNSequence"); + int len2 = 0; + while (len2 < rdn.getLength()) + { + DERValue atav = der.read(); + if (!atav.isConstructed()) + throw new IOException("badly formed AttributeTypeAndValue"); + DERValue val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("badly formed AttributeTypeAndValue"); + OID oid = (OID) val.getValue(); + val = der.read(); + if (!(val.getValue() instanceof String)) + throw new IOException("badly formed AttributeTypeAndValue"); + String value = (String) val.getValue(); + putComponent(oid, value); + len2 += atav.getEncodedLength(); + } + len += rdn.getEncodedLength(); + if (len < name.getLength()) + newRelativeDistinguishedName(); + } + setUnmodifiable(); + } + + private static String compressWS(String str) + { + StringBuffer buf = new StringBuffer(); + char lastChar = 0; + for (int i = 0; i < str.length(); i++) + { + char c = str.charAt(i); + if (Character.isWhitespace(c)) + { + if (!Character.isWhitespace(lastChar)) + buf.append(' '); + } + else + buf.append(c); + lastChar = c; + } + return buf.toString().trim(); + } +} diff --git a/libjava/classpath/gnu/java/security/x509/X509CRL.java b/libjava/classpath/gnu/java/security/x509/X509CRL.java new file mode 100644 index 00000000000..5b2d3b14130 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X509CRL.java @@ -0,0 +1,476 @@ +/* X509CRL.java -- X.509 certificate revocation list. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.ext.Extension; + +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Principal; +import java.security.PublicKey; +import java.security.Signature; +import java.security.SignatureException; +import java.security.cert.CRLException; +import java.security.cert.Certificate; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import javax.security.auth.x500.X500Principal; + +/** + * X.509 certificate revocation lists. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class X509CRL extends java.security.cert.X509CRL + implements GnuPKIExtension +{ + + // Constants and fields. + // ------------------------------------------------------------------------ + + private static final boolean DEBUG = false; + private static void debug(String msg) + { + if (DEBUG) + { + System.err.print(">> X509CRL: "); + System.err.println(msg); + } + } + + private static final OID ID_DSA = new OID("1.2.840.10040.4.1"); + private static final OID ID_DSA_WITH_SHA1 = new OID("1.2.840.10040.4.3"); + private static final OID ID_RSA = new OID("1.2.840.113549.1.1.1"); + private static final OID ID_RSA_WITH_MD2 = new OID("1.2.840.113549.1.1.2"); + private static final OID ID_RSA_WITH_MD5 = new OID("1.2.840.113549.1.1.4"); + private static final OID ID_RSA_WITH_SHA1 = new OID("1.2.840.113549.1.1.5"); + + private byte[] encoded; + + private byte[] tbsCRLBytes; + private int version; + private OID algId; + private byte[] algParams; + private Date thisUpdate; + private Date nextUpdate; + private X500DistinguishedName issuerDN; + private HashMap revokedCerts; + private HashMap extensions; + + private OID sigAlg; + private byte[] sigAlgParams; + private byte[] rawSig; + private byte[] signature; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new X.509 CRL. + * + * @param encoded The DER encoded CRL. + * @throws CRLException If the input bytes are incorrect. + * @throws IOException If the input bytes cannot be read. + */ + public X509CRL(InputStream encoded) throws CRLException, IOException + { + super(); + revokedCerts = new HashMap(); + extensions = new HashMap(); + try + { + parse(encoded); + } + catch (IOException ioe) + { + ioe.printStackTrace(); + throw ioe; + } + catch (Exception x) + { + x.printStackTrace(); + throw new CRLException(x.toString()); + } + } + + // X509CRL methods. + // ------------------------------------------------------------------------ + + public boolean equals(Object o) + { + if (!(o instanceof X509CRL)) + return false; + return ((X509CRL) o).getRevokedCertificates().equals(revokedCerts.values()); + } + + public int hashCode() + { + return revokedCerts.hashCode(); + } + + public byte[] getEncoded() throws CRLException + { + return (byte[]) encoded.clone(); + } + + public void verify(PublicKey key) + throws CRLException, NoSuchAlgorithmException, InvalidKeyException, + NoSuchProviderException, SignatureException + { + Signature sig = Signature.getInstance(sigAlg.toString()); + doVerify(sig, key); + } + + public void verify(PublicKey key, String provider) + throws CRLException, NoSuchAlgorithmException, InvalidKeyException, + NoSuchProviderException, SignatureException + { + Signature sig = Signature.getInstance(sigAlg.toString(), provider); + doVerify(sig, key); + } + + public int getVersion() + { + return version; + } + + public Principal getIssuerDN() + { + return issuerDN; + } + + public X500Principal getIssuerX500Principal() + { + return new X500Principal(issuerDN.getDer()); + } + + public Date getThisUpdate() + { + return (Date) thisUpdate.clone(); + } + + public Date getNextUpdate() + { + if (nextUpdate != null) + return (Date) nextUpdate.clone(); + return null; + } + + public java.security.cert.X509CRLEntry getRevokedCertificate(BigInteger serialNo) + { + return (java.security.cert.X509CRLEntry) revokedCerts.get(serialNo); + } + + public Set getRevokedCertificates() + { + return Collections.unmodifiableSet(new HashSet(revokedCerts.values())); + } + + public byte[] getTBSCertList() throws CRLException + { + return (byte[]) tbsCRLBytes.clone(); + } + + public byte[] getSignature() + { + return (byte[]) rawSig.clone(); + } + + public String getSigAlgName() + { + if (sigAlg.equals(ID_DSA_WITH_SHA1)) + return "SHA1withDSA"; + if (sigAlg.equals(ID_RSA_WITH_MD2)) + return "MD2withRSA"; + if (sigAlg.equals(ID_RSA_WITH_MD5)) + return "MD5withRSA"; + if (sigAlg.equals(ID_RSA_WITH_SHA1)) + return "SHA1withRSA"; + return "unknown"; + } + + public String getSigAlgOID() + { + return sigAlg.toString(); + } + + public byte[] getSigAlgParams() + { + if (sigAlgParams != null) + return (byte[]) sigAlgParams.clone(); + return null; + } + + // X509Extension methods. + // ------------------------------------------------------------------------ + + public boolean hasUnsupportedCriticalExtension() + { + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (e.isCritical() && !e.isSupported()) + return true; + } + return false; + } + + public Set getCriticalExtensionOIDs() + { + HashSet s = new HashSet(); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (e.isCritical()) + s.add(e.getOid().toString()); + } + return Collections.unmodifiableSet(s); + } + + public Set getNonCriticalExtensionOIDs() + { + HashSet s = new HashSet(); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (!e.isCritical()) + s.add(e.getOid().toString()); + } + return Collections.unmodifiableSet(s); + } + + public byte[] getExtensionValue(String oid) + { + Extension e = getExtension(new OID(oid)); + if (e != null) + { + return e.getValue().getEncoded(); + } + return null; + } + + // GnuPKIExtension method. + // ------------------------------------------------------------------------- + + public Extension getExtension(OID oid) + { + return (Extension) extensions.get(oid); + } + + public Collection getExtensions() + { + return extensions.values(); + } + + // CRL methods. + // ------------------------------------------------------------------------- + + public String toString() + { + return X509CRL.class.getName(); + } + + public boolean isRevoked(Certificate cert) + { + if (!(cert instanceof java.security.cert.X509Certificate)) + throw new IllegalArgumentException("not a X.509 certificate"); + BigInteger certSerial = + ((java.security.cert.X509Certificate) cert).getSerialNumber(); + X509CRLEntry ent = (X509CRLEntry) revokedCerts.get(certSerial); + if (ent == null) + return false; + return ent.getRevocationDate().compareTo(new Date()) < 0; + } + + // Own methods. + // ------------------------------------------------------------------------ + + private void doVerify(Signature sig, PublicKey key) + throws CRLException, InvalidKeyException, SignatureException + { + sig.initVerify(key); + sig.update(tbsCRLBytes); + if (!sig.verify(signature)) + throw new CRLException("signature not verified"); + } + + private void parse(InputStream in) throws Exception + { + // CertificateList ::= SEQUENCE { + DERReader der = new DERReader(in); + DERValue val = der.read(); + debug("start CertificateList len == " + val.getLength()); + if (!val.isConstructed()) + throw new IOException("malformed CertificateList"); + encoded = val.getEncoded(); + + // tbsCertList ::= SEQUENCE { -- TBSCertList + val = der.read(); + if (!val.isConstructed()) + throw new IOException("malformed TBSCertList"); + debug("start tbsCertList len == " + val.getLength()); + tbsCRLBytes = val.getEncoded(); + + // version Version OPTIONAL, + // -- If present must be v2 + val = der.read(); + if (val.getValue() instanceof BigInteger) + { + version = ((BigInteger) val.getValue()).intValue() + 1; + val = der.read(); + } + else + version = 1; + debug("read version == " + version); + + // signature AlgorithmIdentifier, + debug("start AlgorithmIdentifier len == " + val.getLength()); + if (!val.isConstructed()) + throw new IOException("malformed AlgorithmIdentifier"); + DERValue algIdVal = der.read(); + algId = (OID) algIdVal.getValue(); + debug("read object identifier == " + algId); + if (val.getLength() > algIdVal.getEncodedLength()) + { + val = der.read(); + debug("read parameters len == " + val.getEncodedLength()); + algParams = val.getEncoded(); + if (val.isConstructed()) + in.skip(val.getLength()); + } + + // issuer Name, + val = der.read(); + issuerDN = new X500DistinguishedName(val.getEncoded()); + der.skip(val.getLength()); + debug("read issuer == " + issuerDN); + + // thisUpdate Time, + thisUpdate = (Date) der.read().getValue(); + debug("read thisUpdate == " + thisUpdate); + + // nextUpdate Time OPTIONAL, + val = der.read(); + if (val.getValue() instanceof Date) + { + nextUpdate = (Date) val.getValue(); + debug("read nextUpdate == " + nextUpdate); + val = der.read(); + } + + // revokedCertificates SEQUENCE OF SEQUENCE { + // -- X509CRLEntry objects... + // } OPTIONAL, + if (val.getTag() != 0) + { + int len = 0; + while (len < val.getLength()) + { + X509CRLEntry entry = new X509CRLEntry(version, der); + revokedCerts.put(entry.getSerialNumber(), entry); + len += entry.getEncoded().length; + } + val = der.read(); + } + + // crlExtensions [0] EXPLICIT Extensions OPTIONAL + // -- if present MUST be v2 + if (val.getTagClass() != DER.UNIVERSAL && val.getTag() == 0) + { + if (version < 2) + throw new IOException("extra data in CRL"); + DERValue exts = der.read(); + if (!exts.isConstructed()) + throw new IOException("malformed Extensions"); + debug("start Extensions len == " + exts.getLength()); + int len = 0; + while (len < exts.getLength()) + { + DERValue ext = der.read(); + if (!ext.isConstructed()) + throw new IOException("malformed Extension"); + Extension e = new Extension(ext.getEncoded()); + extensions.put(e.getOid(), e); + der.skip(ext.getLength()); + len += ext.getEncodedLength(); + debug("current count == " + len); + } + val = der.read(); + } + + debug("read tag == " + val.getTag()); + if (!val.isConstructed()) + throw new IOException("malformed AlgorithmIdentifier"); + debug("start AlgorithmIdentifier len == " + val.getLength()); + DERValue sigAlgVal = der.read(); + debug("read tag == " + sigAlgVal.getTag()); + if (sigAlgVal.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("malformed AlgorithmIdentifier"); + sigAlg = (OID) sigAlgVal.getValue(); + debug("signature id == " + sigAlg); + debug("sigAlgVal length == " + sigAlgVal.getEncodedLength()); + if (val.getLength() > sigAlgVal.getEncodedLength()) + { + val = der.read(); + debug("sig params tag = " + val.getTag() + " len == " + val.getEncodedLength()); + sigAlgParams = (byte[]) val.getEncoded(); + if (val.isConstructed()) + in.skip(val.getLength()); + } + val = der.read(); + debug("read tag = " + val.getTag()); + rawSig = val.getEncoded(); + signature = ((BitString) val.getValue()).toByteArray(); + } +} diff --git a/libjava/classpath/gnu/java/security/x509/X509CRLEntry.java b/libjava/classpath/gnu/java/security/x509/X509CRLEntry.java new file mode 100644 index 00000000000..a3bcfdea823 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X509CRLEntry.java @@ -0,0 +1,278 @@ +/* X509CRLEntry.java -- an entry in a X.509 CRL. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import gnu.java.security.OID; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.ext.Extension; + +import java.io.IOException; +import java.math.BigInteger; +import java.security.cert.CRLException; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * A single entry in a X.509 certificate revocation list. + * + * @see X509CRL + * @author Casey Marshall + */ +class X509CRLEntry extends java.security.cert.X509CRLEntry + implements GnuPKIExtension +{ + + // Constants and fields. + // ------------------------------------------------------------------------ + + private static final boolean DEBUG = false; + private static void debug(String msg) + { + if (DEBUG) + { + System.err.print(">> X509CRLEntry: "); + System.err.println(msg); + } + } + + /** The DER encoded form of this CRL entry. */ + private byte[] encoded; + + /** The revoked certificate's serial number. */ + private BigInteger serialNo; + + /** The date the certificate was revoked. */ + private Date revocationDate; + + /** The CRL entry extensions. */ + private HashMap extensions; + + // Constructor. + // ------------------------------------------------------------------------ + + /** + * Create a new X.509 certificate revocation list entry from the given + * input stream and CRL version number. + * + * @param version The CRL version. + * @param encoded The stream of DER bytes. + * @throws CRLException If the ASN.1 structure is invalid. + * @throws IOException If the bytes cannot be read. + */ + X509CRLEntry(int version, DERReader encoded) + throws CRLException, IOException + { + super(); + extensions = new HashMap(); + try + { + parse(version, encoded); + } + catch (IOException ioe) + { + throw ioe; + } + catch (Exception x) + { + throw new CRLException(x.toString()); + } + } + + // X509CRLEntry methods. + // ------------------------------------------------------------------------ + + public boolean equals(Object o) + { + if (!(o instanceof X509CRLEntry)) + return false; + return ((X509CRLEntry) o).getSerialNumber().equals(serialNo) && + ((X509CRLEntry) o).getRevocationDate().equals(revocationDate); + } + + public int hashCode() + { + return serialNo.hashCode(); + } + + public byte[] getEncoded() throws CRLException + { + return (byte[]) encoded.clone(); + } + + public BigInteger getSerialNumber() + { + return serialNo; + } + + public Date getRevocationDate() + { + return (Date) revocationDate.clone(); + } + + public boolean hasExtensions() + { + return ! extensions.isEmpty(); + } + + public String toString() + { + return "X509CRLEntry serial=" + serialNo + " revocation date=" + + revocationDate + " ext=" + extensions; + } + + // X509Extension methods. + // ------------------------------------------------------------------------- + + public boolean hasUnsupportedCriticalExtension() + { + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (e.isCritical() && !e.isSupported()) + return true; + } + return false; + } + + public Set getCriticalExtensionOIDs() + { + HashSet s = new HashSet(); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (e.isCritical()) + s.add(e.getOid().toString()); + } + return Collections.unmodifiableSet(s); + } + + public Set getNonCriticalExtensionOIDs() + { + HashSet s = new HashSet(); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (!e.isCritical()) + s.add(e.getOid().toString()); + } + return Collections.unmodifiableSet(s); + } + + public byte[] getExtensionValue(String oid) + { + Extension e = getExtension(new OID(oid)); + if (e != null) + { + return e.getValue().getEncoded(); + } + return null; + } + + // GnuPKIExtension method. + // ------------------------------------------------------------------------- + + public Extension getExtension(OID oid) + { + return (Extension) extensions.get(oid); + } + + public Collection getExtensions() + { + return extensions.values(); + } + + // Own methods. + // ------------------------------------------------------------------------- + + private void parse(int version, DERReader der) throws Exception + { + // RevokedCertificate ::= SEQUENCE { + DERValue entry = der.read(); + debug("start CRL entry len == " + entry.getLength()); + if (!entry.isConstructed()) + throw new IOException("malformed revokedCertificate"); + encoded = entry.getEncoded(); + int len = 0; + + debug("encoded entry:\n" + Util.hexDump(encoded, ">>>> ")); + + // userCertificate CertificateSerialNumber, + DERValue val = der.read(); + serialNo = (BigInteger) val.getValue(); + len += val.getEncodedLength(); + debug("userCertificate == " + serialNo + " current count == " + len); + + // revocationDate Time, + val = der.read(); + revocationDate = (Date) val.getValue(); + len += val.getEncodedLength(); + debug("revocationDate == " + revocationDate + " current count == " + len); + + // crlEntryExtensions Extensions OPTIONAL + // -- if present MUST be v2 + if (len < entry.getLength()) + { + if (version < 2) + throw new IOException("extra data in CRL entry"); + DERValue exts = der.read(); + if (!exts.isConstructed()) + throw new IOException("malformed Extensions"); + debug("start Extensions len == " + exts.getLength()); + len = 0; + while (len < exts.getLength()) + { + val = der.read(); + if (!val.isConstructed()) + throw new IOException("malformed Extension"); + debug("start Extension len == " + val.getLength()); + Extension e = new Extension(val.getEncoded()); + extensions.put(e.getOid(), e); + der.skip(val.getLength()); + len += val.getEncodedLength(); + debug("current count == " + len); + } + } + } +} diff --git a/libjava/classpath/gnu/java/security/x509/X509CRLSelectorImpl.java b/libjava/classpath/gnu/java/security/x509/X509CRLSelectorImpl.java new file mode 100644 index 00000000000..0ada5501689 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X509CRLSelectorImpl.java @@ -0,0 +1,138 @@ +/* X509CRLSelectorImpl.java -- implementation of an X509CRLSelector. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import java.io.IOException; + +import java.security.Principal; +import java.security.cert.CRL; +import java.security.cert.CRLSelector; +import java.security.cert.X509CRL; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import javax.security.auth.x500.X500Principal; + +/** + * Sun's implementation of X509CRLSelector sucks. This one tries to work + * better. + */ +public class X509CRLSelectorImpl implements CRLSelector +{ + + // Fields. + // ------------------------------------------------------------------------- + + private Set issuerNames; + + // Constructor. + // ------------------------------------------------------------------------- + + public X509CRLSelectorImpl() + { + issuerNames = new HashSet(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public void addIssuerName(byte[] issuerName) throws IOException + { + issuerNames.add(new X500DistinguishedName(issuerName)); + } + + public void addIssuerName(String issuerName) + { + issuerNames.add(new X500DistinguishedName(issuerName)); + } + + public void addIssuerName(Principal issuerName) throws IOException + { + if (issuerName instanceof X500DistinguishedName) + issuerNames.add(issuerName); + else if (issuerName instanceof X500Principal) + issuerNames.add(new X500DistinguishedName(((X500Principal) issuerName).getEncoded())); + else + issuerNames.add(new X500DistinguishedName(issuerName.getName())); + } + + public Collection getIssuerNames() + { + return Collections.unmodifiableSet(issuerNames); + } + + public Object clone() + { + X509CRLSelectorImpl copy = new X509CRLSelectorImpl(); + copy.issuerNames.addAll(issuerNames); + return copy; + } + + public boolean match(CRL crl) + { + if (!(crl instanceof X509CRL)) + return false; + try + { + Principal p = ((X509CRL) crl).getIssuerDN(); + X500DistinguishedName thisName = null; + if (p instanceof X500DistinguishedName) + thisName = (X500DistinguishedName) p; + else if (p instanceof X500Principal) + thisName = new X500DistinguishedName(((X500Principal) p).getEncoded()); + else + thisName = new X500DistinguishedName(p.getName()); + for (Iterator it = issuerNames.iterator(); it.hasNext(); ) + { + X500DistinguishedName name = (X500DistinguishedName) it.next(); + if (thisName.equals(name)) + return true; + } + } + catch (Exception x) + { + } + return false; + } +} + diff --git a/libjava/classpath/gnu/java/security/x509/X509CertPath.java b/libjava/classpath/gnu/java/security/x509/X509CertPath.java new file mode 100644 index 00000000000..e8ed6bf35d1 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X509CertPath.java @@ -0,0 +1,303 @@ +/* X509CertPath.java -- an X.509 certificate path. + Copyright (C) 2004 Free Software Fonudation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DEREncodingException; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.security.cert.CertPath; +import java.security.cert.Certificate; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * A certificate path (or certificate chain) of X509Certificates. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class X509CertPath extends CertPath +{ + + // Fields. + // ------------------------------------------------------------------------- + + public static final List ENCODINGS = Collections.unmodifiableList( + Arrays.asList(new String[] { "PkiPath", "PKCS7" })); + + private static final OID PKCS7_SIGNED_DATA = new OID("1.2.840.113549.1.7.2"); + private static final OID PKCS7_DATA = new OID("1.2.840.113549.1.7.1"); + + /** The certificate path. */ + private List path; + + /** The cached PKCS #7 encoded bytes. */ + private byte[] pkcs_encoded; + + /** The cached PkiPath encoded bytes. */ + private byte[] pki_encoded; + + // Constructor. + // ------------------------------------------------------------------------- + + public X509CertPath(List path) + { + super("X.509"); + this.path = Collections.unmodifiableList(path); + } + + public X509CertPath(InputStream in) throws CertificateEncodingException + { + this(in, (String) ENCODINGS.get(0)); + } + + public X509CertPath(InputStream in, String encoding) + throws CertificateEncodingException + { + super("X.509"); + try + { + parse(in, encoding); + } + catch (IOException ioe) + { + throw new CertificateEncodingException(); + } + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public List getCertificates() + { + return path; // already unmodifiable + } + + public byte[] getEncoded() throws CertificateEncodingException + { + return getEncoded((String) ENCODINGS.get(0)); + } + + public byte[] getEncoded(String encoding) throws CertificateEncodingException + { + if (encoding.equalsIgnoreCase("PkiPath")) + { + if (pki_encoded == null) + { + try + { + pki_encoded = encodePki(); + } + catch (IOException ioe) + { + throw new CertificateEncodingException(); + } + } + return (byte[]) pki_encoded.clone(); + } + else if (encoding.equalsIgnoreCase("PKCS7")) + { + if (pkcs_encoded == null) + { + try + { + pkcs_encoded = encodePKCS(); + } + catch (IOException ioe) + { + throw new CertificateEncodingException(); + } + } + return (byte[]) pkcs_encoded.clone(); + } + else + throw new CertificateEncodingException("unknown encoding: " + encoding); + } + + public Iterator getEncodings() + { + return ENCODINGS.iterator(); // already unmodifiable + } + + // Own methods. + // ------------------------------------------------------------------------- + + private void parse(InputStream in, String encoding) + throws CertificateEncodingException, IOException + { + DERReader der = new DERReader(in); + DERValue path = null; + if (encoding.equalsIgnoreCase("PkiPath")) + { + // PKI encoding is just a SEQUENCE of X.509 certificates. + path = der.read(); + if (!path.isConstructed()) + throw new DEREncodingException("malformed PkiPath"); + } + else if (encoding.equalsIgnoreCase("PKCS7")) + { + // PKCS #7 encoding means that the certificates are contained in a + // SignedData PKCS #7 type. + // + // ContentInfo ::= SEQUENCE { + // contentType ::= ContentType, + // content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL } + // + // ContentType ::= OBJECT IDENTIFIER + // + // SignedData ::= SEQUENCE { + // version Version, + // digestAlgorithms DigestAlgorithmIdentifiers, + // contentInfo ContentInfo, + // certificates [0] IMPLICIT ExtendedCertificatesAndCertificates + // OPTIONAL, + // crls [1] IMPLICIT CertificateRevocationLists OPTIONAL, + // signerInfos SignerInfos } + // + // Version ::= INTEGER + // + DERValue value = der.read(); + if (!value.isConstructed()) + throw new DEREncodingException("malformed ContentInfo"); + value = der.read(); + if (!(value.getValue() instanceof OID) || + ((OID) value.getValue()).equals(PKCS7_SIGNED_DATA)) + throw new DEREncodingException("not a SignedData"); + value = der.read(); + if (!value.isConstructed() || value.getTag() != 0) + throw new DEREncodingException("malformed content"); + value = der.read(); + if (value.getTag() != DER.INTEGER) + throw new DEREncodingException("malformed Version"); + value = der.read(); + if (!value.isConstructed() || value.getTag() != DER.SET) + throw new DEREncodingException("malformed DigestAlgorithmIdentifiers"); + der.skip(value.getLength()); + value = der.read(); + if (!value.isConstructed()) + throw new DEREncodingException("malformed ContentInfo"); + der.skip(value.getLength()); + path = der.read(); + if (!path.isConstructed() || path.getTag() != 0) + throw new DEREncodingException("no certificates"); + } + else + throw new CertificateEncodingException("unknown encoding: " + encoding); + + LinkedList certs = new LinkedList(); + int len = 0; + while (len < path.getLength()) + { + DERValue cert = der.read(); + try + { + certs.add(new X509Certificate(new ByteArrayInputStream(cert.getEncoded()))); + } + catch (CertificateException ce) + { + throw new CertificateEncodingException(ce.getMessage()); + } + len += cert.getEncodedLength(); + der.skip(cert.getLength()); + } + + this.path = Collections.unmodifiableList(certs); + } + + private byte[] encodePki() + throws CertificateEncodingException, IOException + { + synchronized (path) + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (Iterator i = path.iterator(); i.hasNext(); ) + { + out.write(((Certificate) i.next()).getEncoded()); + } + byte[] b = out.toByteArray(); + DERValue val = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, + b.length, b, null); + return val.getEncoded(); + } + } + + private byte[] encodePKCS() + throws CertificateEncodingException, IOException + { + synchronized (path) + { + ArrayList signedData = new ArrayList(5); + signedData.add(new DERValue(DER.INTEGER, BigInteger.ONE)); + signedData.add(new DERValue(DER.CONSTRUCTED | DER.SET, + Collections.EMPTY_SET)); + signedData.add(new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, + Collections.singletonList( + new DERValue(DER.OBJECT_IDENTIFIER, PKCS7_DATA)))); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (Iterator i = path.iterator(); i.hasNext(); ) + { + out.write(((Certificate) i.next()).getEncoded()); + } + byte[] b = out.toByteArray(); + signedData.add(new DERValue(DER.CONSTRUCTED | DER.CONTEXT, + b.length, b, null)); + DERValue sdValue = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, + signedData); + + ArrayList contentInfo = new ArrayList(2); + contentInfo.add(new DERValue(DER.OBJECT_IDENTIFIER, PKCS7_SIGNED_DATA)); + contentInfo.add(new DERValue(DER.CONSTRUCTED | DER.CONTEXT, sdValue)); + return new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, + contentInfo).getEncoded(); + } + } +} diff --git a/libjava/classpath/gnu/java/security/x509/X509CertSelectorImpl.java b/libjava/classpath/gnu/java/security/x509/X509CertSelectorImpl.java new file mode 100644 index 00000000000..36187ad8e64 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X509CertSelectorImpl.java @@ -0,0 +1,197 @@ +/* X509CertSelectorImpl.java -- implementation of an X509CertSelector. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import java.io.IOException; +import java.security.Principal; +import java.security.cert.CertSelector; +import java.security.cert.Certificate; +import java.security.cert.X509Certificate; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import javax.security.auth.x500.X500Principal; + +/** + * Sun's implementation of X509CertSelector sucks. This one tries to work + * better. + */ +public class X509CertSelectorImpl implements CertSelector +{ + + // Fields. + // ------------------------------------------------------------------------- + + private Set issuerNames; + private Set subjectNames; + + // Constructor. + // ------------------------------------------------------------------------- + + public X509CertSelectorImpl() + { + issuerNames = new HashSet(); + subjectNames = new HashSet(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public void addIssuerName(byte[] issuerName) throws IOException + { + issuerNames.add(new X500DistinguishedName(issuerName)); + } + + public void addIssuerName(String issuerName) + { + issuerNames.add(new X500DistinguishedName(issuerName)); + } + + public void addIssuerName(Principal issuerName) throws IOException + { + if (issuerName instanceof X500DistinguishedName) + issuerNames.add(issuerName); + else if (issuerName instanceof X500Principal) + issuerNames.add(new X500DistinguishedName(((X500Principal) issuerName).getEncoded())); + else + issuerNames.add(new X500DistinguishedName(issuerName.getName())); + } + + public Collection getIssuerNames() + { + return Collections.unmodifiableSet(issuerNames); + } + + public void addSubjectName(byte[] subjectName) throws IOException + { + subjectNames.add(new X500DistinguishedName(subjectName)); + } + + public void addSubjectName(String subjectName) throws IOException + { + subjectNames.add(new X500DistinguishedName(subjectName)); + } + + public void addSubjectName(Principal subjectName) throws IOException + { + if (subjectName instanceof X500DistinguishedName) + subjectNames.add(subjectName); + else if (subjectName instanceof X500Principal) + subjectNames.add(new X500DistinguishedName(((X500Principal) subjectName).getEncoded())); + else + subjectNames.add(new X500DistinguishedName(subjectName.getName())); + } + + public Collection getSubjectNames() + { + return Collections.unmodifiableSet(subjectNames); + } + + public Object clone() + { + X509CertSelectorImpl copy = new X509CertSelectorImpl(); + copy.issuerNames.addAll(issuerNames); + copy.subjectNames.addAll(subjectNames); + return copy; + } + + public boolean match(Certificate cert) + { + if (!(cert instanceof X509Certificate)) + return false; + boolean matchIssuer = false; + boolean matchSubject = false; + try + { + Principal p = ((X509Certificate) cert).getIssuerDN(); + X500DistinguishedName thisName = null; + if (p instanceof X500DistinguishedName) + thisName = (X500DistinguishedName) p; + else if (p instanceof X500Principal) + thisName = new X500DistinguishedName(((X500Principal) p).getEncoded()); + else + thisName = new X500DistinguishedName(p.getName()); + if (issuerNames.isEmpty()) + matchIssuer = true; + else + { + for (Iterator it = issuerNames.iterator(); it.hasNext(); ) + { + X500DistinguishedName name = (X500DistinguishedName) it.next(); + if (thisName.equals(name)) + { + matchIssuer = true; + break; + } + } + } + + p = ((X509Certificate) cert).getSubjectDN(); + thisName = null; + if (p instanceof X500DistinguishedName) + thisName = (X500DistinguishedName) p; + else if (p instanceof X500Principal) + thisName = new X500DistinguishedName(((X500Principal) p).getEncoded()); + else + thisName = new X500DistinguishedName(p.getName()); + if (subjectNames.isEmpty()) + matchSubject = true; + else + { + for (Iterator it = subjectNames.iterator(); it.hasNext(); ) + { + X500DistinguishedName name = (X500DistinguishedName) it.next(); + if (thisName.equals(name)) + { + matchSubject = true; + break; + } + } + } + } + catch (Exception x) + { + } + return matchIssuer && matchSubject; + } +} + diff --git a/libjava/classpath/gnu/java/security/x509/X509Certificate.java b/libjava/classpath/gnu/java/security/x509/X509Certificate.java new file mode 100644 index 00000000000..14ac43a25e6 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/X509Certificate.java @@ -0,0 +1,745 @@ +/* X509Certificate.java -- X.509 certificate. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509; + +import gnu.classpath.debug.Component; +import gnu.classpath.debug.SystemLogger; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.ext.BasicConstraints; +import gnu.java.security.x509.ext.ExtendedKeyUsage; +import gnu.java.security.x509.ext.Extension; +import gnu.java.security.x509.ext.IssuerAlternativeNames; +import gnu.java.security.x509.ext.KeyUsage; +import gnu.java.security.x509.ext.SubjectAlternativeNames; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.Serializable; +import java.io.StringWriter; +import java.math.BigInteger; +import java.security.AlgorithmParameters; +import java.security.InvalidKeyException; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Principal; +import java.security.PublicKey; +import java.security.Signature; +import java.security.SignatureException; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.CertificateParsingException; +import java.security.interfaces.DSAParams; +import java.security.interfaces.DSAPublicKey; +import java.security.spec.DSAParameterSpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.security.auth.x500.X500Principal; + +/** + * An implementation of X.509 certificates. + * + * @author Casey Marshall (rsdio@metastatic.org) + */ +public class X509Certificate extends java.security.cert.X509Certificate + implements Serializable, GnuPKIExtension +{ + + // Constants and fields. + // ------------------------------------------------------------------------ + + private static final Logger logger = SystemLogger.SYSTEM; + + protected static final OID ID_DSA = new OID ("1.2.840.10040.4.1"); + protected static final OID ID_DSA_WITH_SHA1 = new OID ("1.2.840.10040.4.3"); + protected static final OID ID_RSA = new OID ("1.2.840.113549.1.1.1"); + protected static final OID ID_RSA_WITH_MD2 = new OID ("1.2.840.113549.1.1.2"); + protected static final OID ID_RSA_WITH_MD5 = new OID ("1.2.840.113549.1.1.4"); + protected static final OID ID_RSA_WITH_SHA1 = new OID ("1.2.840.113549.1.1.5"); + protected static final OID ID_ECDSA_WITH_SHA1 = new OID ("1.2.840.10045.4.1"); + + // This object SHOULD be serialized with an instance of + // java.security.cert.Certificate.CertificateRep, thus all fields are + // transient. + + // The encoded certificate. + protected transient byte[] encoded; + + // TBSCertificate part. + protected transient byte[] tbsCertBytes; + protected transient int version; + protected transient BigInteger serialNo; + protected transient OID algId; + protected transient byte[] algVal; + protected transient X500DistinguishedName issuer; + protected transient Date notBefore; + protected transient Date notAfter; + protected transient X500DistinguishedName subject; + protected transient PublicKey subjectKey; + protected transient BitString issuerUniqueId; + protected transient BitString subjectUniqueId; + protected transient Map extensions; + + // Signature. + protected transient OID sigAlgId; + protected transient byte[] sigAlgVal; + protected transient byte[] signature; + + // Constructors. + // ------------------------------------------------------------------------ + + /** + * Create a new X.509 certificate from the encoded data. The input + * data are expected to be the ASN.1 DER encoding of the certificate. + * + * @param encoded The encoded certificate data. + * @throws IOException If the certificate cannot be read, possibly + * from a formatting error. + * @throws CertificateException If the data read is not an X.509 + * certificate. + */ + public X509Certificate(InputStream encoded) + throws CertificateException, IOException + { + super(); + extensions = new HashMap(); + try + { + parse(encoded); + } + catch (IOException ioe) + { + logger.log (Component.X509, "", ioe); + throw ioe; + } + catch (Exception e) + { + logger.log (Component.X509, "", e); + CertificateException ce = new CertificateException(e.getMessage()); + ce.initCause (e); + throw ce; + } + } + + protected X509Certificate() + { + extensions = new HashMap(); + } + + // X509Certificate methods. + // ------------------------------------------------------------------------ + + public void checkValidity() + throws CertificateExpiredException, CertificateNotYetValidException + { + checkValidity(new Date()); + } + + public void checkValidity(Date date) + throws CertificateExpiredException, CertificateNotYetValidException + { + if (date.compareTo(notBefore) < 0) + { + throw new CertificateNotYetValidException(); + } + if (date.compareTo(notAfter) > 0) + { + throw new CertificateExpiredException(); + } + } + + public int getVersion() + { + return version; + } + + public BigInteger getSerialNumber() + { + return serialNo; + } + + public Principal getIssuerDN() + { + return issuer; + } + + public X500Principal getIssuerX500Principal() + { + return new X500Principal(issuer.getDer()); + } + + public Principal getSubjectDN() + { + return subject; + } + + public X500Principal getSubjectX500Principal() + { + return new X500Principal(subject.getDer()); + } + + public Date getNotBefore() + { + return (Date) notBefore.clone(); + } + + public Date getNotAfter() + { + return (Date) notAfter.clone(); + } + + public byte[] getTBSCertificate() throws CertificateEncodingException + { + return (byte[]) tbsCertBytes.clone(); + } + + public byte[] getSignature() + { + return (byte[]) signature.clone(); + } + + public String getSigAlgName() + { + if (sigAlgId.equals(ID_DSA_WITH_SHA1)) + { + return "SHA1withDSA"; + } + if (sigAlgId.equals(ID_RSA_WITH_MD2)) + { + return "MD2withRSA"; + } + if (sigAlgId.equals(ID_RSA_WITH_MD5)) + { + return "MD5withRSA"; + } + if (sigAlgId.equals(ID_RSA_WITH_SHA1)) + { + return "SHA1withRSA"; + } + return "unknown"; + } + + public String getSigAlgOID() + { + return sigAlgId.toString(); + } + + public byte[] getSigAlgParams() + { + return (byte[]) sigAlgVal.clone(); + } + + public boolean[] getIssuerUniqueID() + { + if (issuerUniqueId != null) + { + return issuerUniqueId.toBooleanArray(); + } + return null; + } + + public boolean[] getSubjectUniqueID() + { + if (subjectUniqueId != null) + { + return subjectUniqueId.toBooleanArray(); + } + return null; + } + + public boolean[] getKeyUsage() + { + Extension e = getExtension(KeyUsage.ID); + if (e != null) + { + KeyUsage ku = (KeyUsage) e.getValue(); + boolean[] result = new boolean[9]; + boolean[] b = ku.getKeyUsage().toBooleanArray(); + System.arraycopy(b, 0, result, 0, b.length); + return result; + } + return null; + } + + public List getExtendedKeyUsage() throws CertificateParsingException + { + Extension e = getExtension(ExtendedKeyUsage.ID); + if (e != null) + { + List a = ((ExtendedKeyUsage) e.getValue()).getPurposeIds(); + List b = new ArrayList(a.size()); + for (Iterator it = a.iterator(); it.hasNext(); ) + { + b.add(it.next().toString()); + } + return Collections.unmodifiableList(b); + } + return null; + } + + public int getBasicConstraints() + { + Extension e = getExtension(BasicConstraints.ID); + if (e != null) + { + return ((BasicConstraints) e.getValue()).getPathLengthConstraint(); + } + return -1; + } + + public Collection getSubjectAlternativeNames() + throws CertificateParsingException + { + Extension e = getExtension(SubjectAlternativeNames.ID); + if (e != null) + { + return ((SubjectAlternativeNames) e.getValue()).getNames(); + } + return null; + } + + public Collection getIssuerAlternativeNames() + throws CertificateParsingException + { + Extension e = getExtension(IssuerAlternativeNames.ID); + if (e != null) + { + return ((IssuerAlternativeNames) e.getValue()).getNames(); + } + return null; + } + +// X509Extension methods. + // ------------------------------------------------------------------------ + + public boolean hasUnsupportedCriticalExtension() + { + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (e.isCritical() && !e.isSupported()) + return true; + } + return false; + } + + public Set getCriticalExtensionOIDs() + { + HashSet s = new HashSet(); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (e.isCritical()) + s.add(e.getOid().toString()); + } + return Collections.unmodifiableSet(s); + } + + public Set getNonCriticalExtensionOIDs() + { + HashSet s = new HashSet(); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + Extension e = (Extension) it.next(); + if (!e.isCritical()) + s.add(e.getOid().toString()); + } + return Collections.unmodifiableSet(s); + } + + public byte[] getExtensionValue(String oid) + { + Extension e = getExtension(new OID(oid)); + if (e != null) + { + return e.getValue().getEncoded(); + } + return null; + } + + // GnuPKIExtension method. + // ------------------------------------------------------------------------- + + public Extension getExtension(OID oid) + { + return (Extension) extensions.get(oid); + } + + public Collection getExtensions() + { + return extensions.values(); + } + + // Certificate methods. + // ------------------------------------------------------------------------- + + public byte[] getEncoded() throws CertificateEncodingException + { + return (byte[]) encoded.clone(); + } + + public void verify(PublicKey key) + throws CertificateException, NoSuchAlgorithmException, + InvalidKeyException, NoSuchProviderException, SignatureException + { + Signature sig = Signature.getInstance(sigAlgId.toString()); + doVerify(sig, key); + } + + public void verify(PublicKey key, String provider) + throws CertificateException, NoSuchAlgorithmException, + InvalidKeyException, NoSuchProviderException, SignatureException + { + Signature sig = Signature.getInstance(sigAlgId.toString(), provider); + doVerify(sig, key); + } + + public String toString() + { + StringWriter str = new StringWriter(); + PrintWriter out = new PrintWriter(str); + out.println(X509Certificate.class.getName() + " {"); + out.println(" TBSCertificate {"); + out.println(" version = " + version + ";"); + out.println(" serialNo = " + serialNo + ";"); + out.println(" signature = {"); + out.println(" algorithm = " + getSigAlgName() + ";"); + out.print(" parameters ="); + if (sigAlgVal != null) + { + out.println(); + out.print(Util.hexDump(sigAlgVal, " ")); + } + else + { + out.println(" null;"); + } + out.println(" }"); + out.println(" issuer = " + issuer.getName() + ";"); + out.println(" validity = {"); + out.println(" notBefore = " + notBefore + ";"); + out.println(" notAfter = " + notAfter + ";"); + out.println(" }"); + out.println(" subject = " + subject.getName() + ";"); + out.println(" subjectPublicKeyInfo = {"); + out.println(" algorithm = " + subjectKey.getAlgorithm()); + out.println(" key ="); + out.print(Util.hexDump(subjectKey.getEncoded(), " ")); + out.println(" };"); + out.println(" issuerUniqueId = " + issuerUniqueId + ";"); + out.println(" subjectUniqueId = " + subjectUniqueId + ";"); + out.println(" extensions = {"); + for (Iterator it = extensions.values().iterator(); it.hasNext(); ) + { + out.println(" " + it.next()); + } + out.println(" }"); + out.println(" }"); + out.println(" signatureAlgorithm = " + getSigAlgName() + ";"); + out.println(" signatureValue ="); + out.print(Util.hexDump(signature, " ")); + out.println("}"); + return str.toString(); + } + + public PublicKey getPublicKey() + { + return subjectKey; + } + + public boolean equals(Object other) + { + if (!(other instanceof X509Certificate)) + return false; + try + { + if (other instanceof X509Certificate) + return Arrays.equals(encoded, ((X509Certificate) other).encoded); + byte[] enc = ((X509Certificate) other).getEncoded(); + if (enc == null) + return false; + return Arrays.equals(encoded, enc); + } + catch (CertificateEncodingException cee) + { + return false; + } + } + + // Own methods. + // ------------------------------------------------------------------------ + + /** + * Verify this certificate's signature. + */ + private void doVerify(Signature sig, PublicKey key) + throws CertificateException, InvalidKeyException, SignatureException + { + logger.log (Component.X509, "verifying sig={0} key={1}", + new Object[] { sig, key }); + sig.initVerify(key); + sig.update(tbsCertBytes); + if (!sig.verify(signature)) + { + throw new CertificateException("signature not validated"); + } + } + + /** + * Parse a DER stream into an X.509 certificate. + * + * @param encoded The encoded bytes. + */ + private void parse(InputStream encoded) throws Exception + { + DERReader der = new DERReader(encoded); + + // Certificate ::= SEQUENCE { + DERValue cert = der.read(); + logger.log (Component.X509, "start Certificate len == {0}", + new Integer (cert.getLength())); + + this.encoded = cert.getEncoded(); + if (!cert.isConstructed()) + { + throw new IOException("malformed Certificate"); + } + + // TBSCertificate ::= SEQUENCE { + DERValue tbsCert = der.read(); + if (tbsCert.getValue() != DER.CONSTRUCTED_VALUE) + { + throw new IOException("malformed TBSCertificate"); + } + tbsCertBytes = tbsCert.getEncoded(); + logger.log (Component.X509, "start TBSCertificate len == {0}", + new Integer (tbsCert.getLength())); + + // Version ::= INTEGER [0] { v1(0), v2(1), v3(2) } + DERValue val = der.read(); + if (val.getTagClass() == DER.CONTEXT && val.getTag() == 0) + { + version = ((BigInteger) der.read().getValue()).intValue() + 1; + val = der.read(); + } + else + { + version = 1; + } + logger.log (Component.X509, "read version == {0}", + new Integer (version)); + + // SerialNumber ::= INTEGER + serialNo = (BigInteger) val.getValue(); + logger.log (Component.X509, "read serial number == {0}", serialNo); + + // AlgorithmIdentifier ::= SEQUENCE { + val = der.read(); + if (!val.isConstructed()) + { + throw new IOException("malformed AlgorithmIdentifier"); + } + int certAlgLen = val.getLength(); + logger.log (Component.X509, "start AlgorithmIdentifier len == {0}", + new Integer (certAlgLen)); + val = der.read(); + + // algorithm OBJECT IDENTIFIER, + algId = (OID) val.getValue(); + logger.log (Component.X509, "read algorithm ID == {0}", algId); + + // parameters ANY DEFINED BY algorithm OPTIONAL } + if (certAlgLen > val.getEncodedLength()) + { + val = der.read(); + if (val == null) + { + algVal = null; + } + else + { + algVal = val.getEncoded(); + + if (val.isConstructed()) + encoded.skip(val.getLength()); + } + logger.log (Component.X509, "read algorithm parameters == {0}", algVal); + } + + // issuer Name, + val = der.read(); + issuer = new X500DistinguishedName(val.getEncoded()); + der.skip(val.getLength()); + logger.log (Component.X509, "read issuer == {0}", issuer); + + // Validity ::= SEQUENCE { + // notBefore Time, + // notAfter Time } + if (!der.read().isConstructed()) + { + throw new IOException("malformed Validity"); + } + notBefore = (Date) der.read().getValue(); + logger.log (Component.X509, "read notBefore == {0}", notBefore); + notAfter = (Date) der.read().getValue(); + logger.log (Component.X509, "read notAfter == {0}", notAfter); + + // subject Name, + val = der.read(); + subject = new X500DistinguishedName(val.getEncoded()); + der.skip(val.getLength()); + logger.log (Component.X509, "read subject == {0}", subject); + + // SubjectPublicKeyInfo ::= SEQUENCE { + // algorithm AlgorithmIdentifier, + // subjectPublicKey BIT STRING } + DERValue spki = der.read(); + if (!spki.isConstructed()) + { + throw new IOException("malformed SubjectPublicKeyInfo"); + } + KeyFactory spkFac = KeyFactory.getInstance("X.509"); + subjectKey = spkFac.generatePublic(new X509EncodedKeySpec(spki.getEncoded())); + der.skip(spki.getLength()); + logger.log (Component.X509, "read subjectPublicKey == {0}", subjectKey); + + if (version > 1) + { + val = der.read(); + } + if (version >= 2 && val.getTagClass() != DER.UNIVERSAL && val.getTag() == 1) + { + byte[] b = (byte[]) val.getValue(); + issuerUniqueId = new BitString(b, 1, b.length-1, b[0] & 0xFF); + logger.log (Component.X509, "read issuerUniqueId == {0}", issuerUniqueId); + val = der.read(); + } + if (version >= 2 && val.getTagClass() != DER.UNIVERSAL && val.getTag() == 2) + { + byte[] b = (byte[]) val.getValue(); + subjectUniqueId = new BitString(b, 1, b.length-1, b[0] & 0xFF); + logger.log (Component.X509, "read subjectUniqueId == {0}", subjectUniqueId); + val = der.read(); + } + if (version >= 3 && val.getTagClass() != DER.UNIVERSAL && val.getTag() == 3) + { + val = der.read(); + logger.log (Component.X509, "start Extensions len == {0}", + new Integer (val.getLength())); + int len = 0; + while (len < val.getLength()) + { + DERValue ext = der.read(); + logger.log (Component.X509, "start extension len == {0}", + new Integer (ext.getLength())); + Extension e = new Extension(ext.getEncoded()); + extensions.put(e.getOid(), e); + der.skip(ext.getLength()); + len += ext.getEncodedLength(); + logger.log (Component.X509, "read extension {0} == {1}", + new Object[] { e.getOid (), e }); + logger.log (Component.X509, "count == {0}", new Integer (len)); + } + + val = der.read (); + } + + logger.log (Component.X509, "read value {0}", val); + if (!val.isConstructed()) + { + throw new CertificateException ("malformed AlgorithmIdentifier"); + } + int sigAlgLen = val.getLength(); + logger.log (Component.X509, "start AlgorithmIdentifier len == {0}", + new Integer (sigAlgLen)); + val = der.read(); + sigAlgId = (OID) val.getValue(); + logger.log (Component.X509, "read algorithm id == {0}", sigAlgId); + if (sigAlgLen > val.getEncodedLength()) + { + val = der.read(); + if (val.getValue() == null) + { + if (subjectKey instanceof DSAPublicKey) + { + AlgorithmParameters params = + AlgorithmParameters.getInstance("DSA"); + DSAParams dsap = ((DSAPublicKey) subjectKey).getParams(); + DSAParameterSpec spec = + new DSAParameterSpec(dsap.getP(), dsap.getQ(), dsap.getG()); + params.init(spec); + sigAlgVal = params.getEncoded(); + } + } + else + { + sigAlgVal = (byte[]) val.getEncoded(); + } + if (val.isConstructed()) + { + encoded.skip(val.getLength()); + } + logger.log (Component.X509, "read parameters == {0}", sigAlgVal); + } + signature = ((BitString) der.read().getValue()).toByteArray(); + logger.log (Component.X509, "read signature ==\n{0}", Util.hexDump(signature, ">>>> ")); + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/AuthorityKeyIdentifier.java b/libjava/classpath/gnu/java/security/x509/ext/AuthorityKeyIdentifier.java new file mode 100644 index 00000000000..a94b76f093e --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/AuthorityKeyIdentifier.java @@ -0,0 +1,133 @@ +/* AuthorityKeyIdentifier.java -- Authority key identifier extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.Util; + +import java.io.IOException; +import java.math.BigInteger; + +public class AuthorityKeyIdentifier extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.35"); + + private final byte[] keyIdentifier; + private final GeneralNames authorityCertIssuer; + private final BigInteger authorityCertSerialNumber; + + // Contstructor. + // ------------------------------------------------------------------------- + + public AuthorityKeyIdentifier(final byte[] encoded) throws IOException + { + super(encoded); + DERReader der = new DERReader(encoded); + + // AuthorityKeyIdentifier ::= SEQUENCE { + DERValue val = der.read(); + if (!val.isConstructed()) + throw new IOException("malformed AuthorityKeyIdentifier"); + if (val.getLength() > 0) + val = der.read(); + + // keyIdentifier [0] KeyIdentifier OPTIONAL, + // KeyIdentifier ::= OCTET STRING + if (val.getTagClass() == DER.APPLICATION && val.getTag() == 0) + { + keyIdentifier = (byte[]) val.getValue(); + val = der.read(); + } + else + keyIdentifier = null; + + // authorityCertIssuer [1] GeneralNames OPTIONAL, + if (val.getTagClass() == DER.APPLICATION && val.getTag() == 1) + { + byte[] b = val.getEncoded(); + b[0] = (byte) (DER.CONSTRUCTED|DER.SEQUENCE); + authorityCertIssuer = new GeneralNames(b); + der.skip(val.getLength()); + val = der.read(); + } + else + authorityCertIssuer = null; + + // authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL } + if (val.getTagClass() == DER.APPLICATION && val.getTag() == 2) + { + authorityCertSerialNumber = new BigInteger((byte[]) val.getValue()); + } + else + authorityCertSerialNumber = null; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public byte[] getKeyIdentifier() + { + return keyIdentifier != null ? (byte[]) keyIdentifier.clone() : null; + } + + public GeneralNames getAuthorityCertIssuer() + { + return authorityCertIssuer; + } + + public BigInteger getAuthorityCertSerialNumber() + { + return authorityCertSerialNumber; + } + + public String toString() + { + return AuthorityKeyIdentifier.class.getName() + " [ keyId=" + + (keyIdentifier != null ? Util.toHexString (keyIdentifier, ':') : "nil") + + " authorityCertIssuer=" + authorityCertIssuer + + " authorityCertSerialNumbe=" + authorityCertSerialNumber + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/BasicConstraints.java b/libjava/classpath/gnu/java/security/x509/ext/BasicConstraints.java new file mode 100644 index 00000000000..00f7a6ed220 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/BasicConstraints.java @@ -0,0 +1,129 @@ +/* BasicConstraints.java -- the basic constraints extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +public class BasicConstraints extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.19"); + + private final boolean ca; + private final int pathLenConstraint; + + // Constructor. + // ------------------------------------------------------------------------- + + public BasicConstraints(final byte[] encoded) throws IOException + { + super(encoded); + DERReader der = new DERReader(encoded); + DERValue bc = der.read(); + if (!bc.isConstructed()) + throw new IOException("malformed BasicConstraints"); + DERValue val = bc; + if (bc.getLength() > 0) + val = der.read(); + if (val.getTag() == DER.BOOLEAN) + { + ca = ((Boolean) val.getValue()).booleanValue(); + if (val.getEncodedLength() < bc.getLength()) + val = der.read(); + } + else + ca = false; + if (val.getTag() == DER.INTEGER) + { + pathLenConstraint = ((BigInteger) val.getValue()).intValue(); + } + else + pathLenConstraint = -1; + } + + public BasicConstraints (final boolean ca, final int pathLenConstraint) + { + this.ca = ca; + this.pathLenConstraint = pathLenConstraint; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public boolean isCA() + { + return ca; + } + + public int getPathLengthConstraint() + { + return pathLenConstraint; + } + + public byte[] getEncoded() + { + if (encoded == null) + { + List bc = new ArrayList (2); + bc.add (new DERValue (DER.BOOLEAN, new Boolean (ca))); + if (pathLenConstraint >= 0) + bc.add (new DERValue (DER.INTEGER, + BigInteger.valueOf ((long) pathLenConstraint))); + encoded = new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, bc).getEncoded(); + } + return (byte[]) encoded.clone(); + } + + public String toString() + { + return BasicConstraints.class.getName() + " [ isCA=" + ca + + " pathLen=" + pathLenConstraint + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/CRLNumber.java b/libjava/classpath/gnu/java/security/x509/ext/CRLNumber.java new file mode 100644 index 00000000000..36b1c7b5f1d --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/CRLNumber.java @@ -0,0 +1,97 @@ +/* CRLNumber.java -- CRL number extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.math.BigInteger; + +public class CRLNumber extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.20"); + + private final BigInteger number; + + // Constructor. + // ------------------------------------------------------------------------- + + public CRLNumber(final byte[] encoded) throws IOException + { + super(encoded); + DERValue val = DERReader.read(encoded); + if (val.getTag() != DER.INTEGER) + throw new IOException("malformed CRLNumber"); + number = (BigInteger) val.getValue(); + } + + public CRLNumber (final BigInteger number) + { + this.number = number; + } + + // Instance method. + // ------------------------------------------------------------------------- + + public BigInteger getNumber() + { + return number; + } + + public byte[] getEncoded() + { + if (encoded == null) + { + encoded = new DERValue (DER.INTEGER, number).getEncoded(); + } + return (byte[]) encoded.clone(); + } + + public String toString() + { + return CRLNumber.class.getName() + " [ " + number + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/CertificatePolicies.java b/libjava/classpath/gnu/java/security/x509/ext/CertificatePolicies.java new file mode 100644 index 00000000000..50bc6d367d7 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/CertificatePolicies.java @@ -0,0 +1,189 @@ +/* CertificatePolicies.java -- certificate policy extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.security.cert.PolicyQualifierInfo; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class CertificatePolicies extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.32"); + + private final List policies; + private final Map policyQualifierInfos; + + // Constructor. + // ------------------------------------------------------------------------- + + public CertificatePolicies(final byte[] encoded) throws IOException + { + super(encoded); + DERReader der = new DERReader(encoded); + DERValue pol = der.read(); + if (!pol.isConstructed()) + throw new IOException("malformed CertificatePolicies"); + + int len = 0; + LinkedList policyList = new LinkedList(); + HashMap qualifierMap = new HashMap(); + while (len < pol.getLength()) + { + DERValue policyInfo = der.read(); + if (!policyInfo.isConstructed()) + throw new IOException("malformed PolicyInformation"); + DERValue val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("malformed CertPolicyId"); + OID policyId = (OID) val.getValue(); + policyList.add(policyId); + if (val.getEncodedLength() < policyInfo.getLength()) + { + DERValue qual = der.read(); + int len2 = 0; + LinkedList quals = new LinkedList(); + while (len2 < qual.getLength()) + { + val = der.read(); + quals.add(new PolicyQualifierInfo(val.getEncoded())); + der.skip(val.getLength()); + len2 += val.getEncodedLength(); + } + qualifierMap.put(policyId, quals); + } + len += policyInfo.getEncodedLength(); + } + + policies = Collections.unmodifiableList(policyList); + policyQualifierInfos = Collections.unmodifiableMap(qualifierMap); + } + + public CertificatePolicies (final List policies, + final Map policyQualifierInfos) + { + for (Iterator it = policies.iterator(); it.hasNext(); ) + if (!(it.next() instanceof OID)) + throw new IllegalArgumentException ("policies must be OIDs"); + for (Iterator it = policyQualifierInfos.entrySet().iterator(); it.hasNext();) + { + Map.Entry e = (Map.Entry) it.next(); + if (!(e.getKey() instanceof OID) || !policies.contains (e.getKey())) + throw new IllegalArgumentException + ("policyQualifierInfos keys must be OIDs"); + if (!(e.getValue() instanceof List)) + throw new IllegalArgumentException + ("policyQualifierInfos values must be Lists of PolicyQualifierInfos"); + for (Iterator it2 = ((List) e.getValue()).iterator(); it.hasNext(); ) + if (!(it2.next() instanceof PolicyQualifierInfo)) + throw new IllegalArgumentException + ("policyQualifierInfos values must be Lists of PolicyQualifierInfos"); + } + this.policies = Collections.unmodifiableList (new ArrayList (policies)); + this.policyQualifierInfos = Collections.unmodifiableMap + (new HashMap (policyQualifierInfos)); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public List getPolicies() + { + return policies; + } + + public List getPolicyQualifierInfos(OID oid) + { + return (List) policyQualifierInfos.get(oid); + } + + public byte[] getEncoded() + { + if (encoded == null) + { + List pol = new ArrayList (policies.size()); + for (Iterator it = policies.iterator(); it.hasNext(); ) + { + OID policy = (OID) it.next(); + List qualifiers = getPolicyQualifierInfos (policy); + List l = new ArrayList (qualifiers == null ? 1 : 2); + l.add (new DERValue (DER.OBJECT_IDENTIFIER, policy)); + if (qualifiers != null) + { + List ll = new ArrayList (qualifiers.size()); + for (Iterator it2 = qualifiers.iterator(); it.hasNext(); ) + { + PolicyQualifierInfo info = (PolicyQualifierInfo) it2.next(); + try + { + ll.add (DERReader.read (info.getEncoded())); + } + catch (IOException ioe) + { + } + } + l.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ll)); + } + pol.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, l)); + } + encoded = new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, pol).getEncoded(); + } + return (byte[]) encoded.clone(); + } + + public String toString() + { + return CertificatePolicies.class.getName() + " [ policies=" + policies + + " policyQualifierInfos=" + policyQualifierInfos + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/ExtendedKeyUsage.java b/libjava/classpath/gnu/java/security/x509/ext/ExtendedKeyUsage.java new file mode 100644 index 00000000000..37b08acf43e --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/ExtendedKeyUsage.java @@ -0,0 +1,95 @@ +/* ExtendedKeyUsage.java -- the extended key usage extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ExtendedKeyUsage extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.37"); + + private final List purposeIds; + + // Constructor. + // ------------------------------------------------------------------------- + + public ExtendedKeyUsage(final byte[] encoded) throws IOException + { + super(encoded); + DERReader der = new DERReader(encoded); + DERValue usageList = der.read(); + if (!usageList.isConstructed()) + throw new IOException("malformed ExtKeyUsageSyntax"); + int len = 0; + purposeIds = new LinkedList(); + while (len < usageList.getLength()) + { + DERValue val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("malformed KeyPurposeId"); + purposeIds.add(val.getValue()); + len += val.getEncodedLength(); + } + } + + // Instance method. + // ------------------------------------------------------------------------- + + public List getPurposeIds() + { + return Collections.unmodifiableList(purposeIds); + } + + public String toString() + { + return ExtendedKeyUsage.class.getName() + " [ " + purposeIds + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/Extension.java b/libjava/classpath/gnu/java/security/x509/ext/Extension.java new file mode 100644 index 00000000000..5ca9ac3a91d --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/Extension.java @@ -0,0 +1,289 @@ +/* Extension.java -- an X.509 certificate or CRL extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.Util; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Extension +{ + + // Fields. + // ------------------------------------------------------------------------- + + private static final boolean DEBUG = false; + private static void debug(String msg) + { + System.err.print(">> Extension: "); + System.err.println(msg); + } + + /** + * This extension's object identifier. + */ + protected final OID oid; + + /** + * The criticality flag. + */ + protected final boolean critical; + + /** + * Whether or not this extension is locally supported. + */ + protected boolean isSupported; + + /** + * The extension value. + */ + protected final Value value; + + /** + * The DER encoded form. + */ + protected byte[] encoded; + + // Constructors. + // ------------------------------------------------------------------------- + + public Extension(byte[] encoded) throws IOException + { + this.encoded = (byte[]) encoded.clone(); + DERReader der = new DERReader(encoded); + + // Extension ::= SEQUENCE { + DERValue val = der.read(); + if (DEBUG) debug("read val tag == " + val.getTag() + " len == " + val.getLength()); + if (!val.isConstructed()) + throw new IOException("malformed Extension"); + + // extnID OBJECT IDENTIFIER, + val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("expecting OBJECT IDENTIFIER"); + oid = (OID) val.getValue(); + if (DEBUG) debug("read oid == " + oid); + + // critical BOOLEAN DEFAULT FALSE, + val = der.read(); + if (val.getTag() == DER.BOOLEAN) + { + critical = ((Boolean) val.getValue()).booleanValue(); + val = der.read(); + } + else + critical = false; + if (DEBUG) debug("is critical == " + critical); + + // extnValue OCTET STRING } + if (val.getTag() != DER.OCTET_STRING) + throw new IOException("expecting OCTET STRING"); + byte[] encval = (byte[]) val.getValue(); + isSupported = true; + if (oid.equals(AuthorityKeyIdentifier.ID)) + { + value = new AuthorityKeyIdentifier(encval); + } + else if (oid.equals(SubjectKeyIdentifier.ID)) + { + value = new SubjectKeyIdentifier(encval); + } + else if (oid.equals(KeyUsage.ID)) + { + value = new KeyUsage(encval); + } + else if (oid.equals(PrivateKeyUsagePeriod.ID)) + { + value = new PrivateKeyUsagePeriod(encval); + } + else if (oid.equals(CertificatePolicies.ID)) + { + value = new CertificatePolicies(encval); + } + else if (oid.equals (PolicyConstraint.ID)) + { + value = new PolicyConstraint (encval); + } + else if (oid.equals(PolicyMappings.ID)) + { + value = new PolicyMappings(encval); + } + else if (oid.equals(SubjectAlternativeNames.ID)) + { + value = new SubjectAlternativeNames(encval); + } + else if (oid.equals(IssuerAlternativeNames.ID)) + { + value = new IssuerAlternativeNames(encval); + } + else if (oid.equals(BasicConstraints.ID)) + { + value = new BasicConstraints(encval); + } + else if (oid.equals(ExtendedKeyUsage.ID)) + { + value = new ExtendedKeyUsage(encval); + } + else if (oid.equals(CRLNumber.ID)) + { + value = new CRLNumber(encval); + } + else if (oid.equals(ReasonCode.ID)) + { + value = new ReasonCode(encval); + } + else + { + value = new Value(encval); + isSupported = false; + } + if (DEBUG) debug("read value == " + value); + } + + public Extension (final OID oid, final Value value, final boolean critical) + { + this.oid = oid; + this.value = value; + this.critical = critical; + isSupported = true; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public OID getOid() + { + return oid; + } + + public boolean isCritical() + { + return critical; + } + + public boolean isSupported() + { + return isSupported; + } + + public Value getValue() + { + return value; + } + + public byte[] getEncoded() + { + if (encoded == null) + encode(); + return (byte[]) encoded.clone(); + } + + public String toString() + { + return Extension.class.getName() + " [ id=" + oid + " critical=" + + critical + " value=" + value + " ]"; + } + + public DERValue getDerValue() + { + List ext = new ArrayList (3); + ext.add (new DERValue (DER.OBJECT_IDENTIFIER, oid)); + ext.add (new DERValue (DER.BOOLEAN, new Boolean (critical))); + ext.add (new DERValue (DER.OCTET_STRING, value.getEncoded())); + return new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ext); + } + + // Own methods. + // ------------------------------------------------------------------------- + + private void encode() + { + encoded = getDerValue().getEncoded(); + } + + // Inner class. + // ------------------------------------------------------------------------- + + public static class Value + { + + // Fields. + // ----------------------------------------------------------------------- + + protected byte[] encoded; + + // Constructor. + // ----------------------------------------------------------------------- + + public Value(byte[] encoded) + { + this.encoded = (byte[]) encoded.clone(); + } + + protected Value() { } + + // Instance methods. + // ----------------------------------------------------------------------- + + public byte[] getEncoded() + { + return (byte[]) encoded; + } + + public boolean equals(Object o) + { + if (!(o instanceof Value)) + return false; + return Arrays.equals(encoded, ((Value) o).encoded); + } + + public String toString() + { + return Util.toHexString(encoded, ':'); + } + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/GeneralNames.java b/libjava/classpath/gnu/java/security/x509/ext/GeneralNames.java new file mode 100644 index 00000000000..e92aedaefd0 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/GeneralNames.java @@ -0,0 +1,155 @@ +/* GeneralNames.java -- the GeneralNames object + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.X500DistinguishedName; + +import java.io.IOException; +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class GeneralNames +{ + + // Instance methods. + // ------------------------------------------------------------------------- + + public static final int OTHER_NAME = 0; + public static final int RFC822_NAME = 1; + public static final int DNS_NAME = 2; + public static final int X400_ADDRESS = 3; + public static final int DIRECTORY_NAME = 4; + public static final int EDI_PARTY_NAME = 5; + public static final int URI = 6; + public static final int IP_ADDRESS = 7; + public static final int REGISTERED_ID = 8; + + private List names; + + // Constructor. + // ------------------------------------------------------------------------- + + public GeneralNames(final byte[] encoded) throws IOException + { + names = new LinkedList(); + DERReader der = new DERReader(encoded); + DERValue nameList = der.read(); + if (!nameList.isConstructed()) + throw new IOException("malformed GeneralNames"); + int len = 0; + while (len < nameList.getLength()) + { + DERValue name = der.read(); + List namePair = new ArrayList(2); + if (name.getTagClass() != DER.APPLICATION) + throw new IOException("malformed GeneralName"); + namePair.add(new Integer(name.getTag())); + DERValue val = null; + switch (name.getTag()) + { + case RFC822_NAME: + case DNS_NAME: + case X400_ADDRESS: + case URI: + namePair.add(new String((byte[]) name.getValue())); + break; + + case OTHER_NAME: + case EDI_PARTY_NAME: + namePair.add(name.getValue()); + break; + + case DIRECTORY_NAME: + byte[] b = name.getEncoded(); + b[0] = (byte) (DER.CONSTRUCTED|DER.SEQUENCE); + namePair.add(new X500DistinguishedName(b).toString()); + break; + + case IP_ADDRESS: + namePair.add(InetAddress.getByAddress((byte[]) name.getValue()) + .getHostAddress()); + break; + + case REGISTERED_ID: + byte[] bb = name.getEncoded(); + bb[0] = (byte) DER.OBJECT_IDENTIFIER; + namePair.add(new OID(bb).toString()); + break; + + default: + throw new IOException("unknown tag " + name.getTag()); + } + names.add(namePair); + len += name.getEncodedLength(); + } + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public List getNames() + { + List l = new ArrayList(names.size()); + for (Iterator it = names.iterator(); it.hasNext(); ) + { + List ll = (List) it.next(); + List pair = new ArrayList(2); + pair.add(ll.get(0)); + if (ll.get(1) instanceof byte[]) + pair.add(((byte[]) ll.get(1)).clone()); + else + pair.add(ll.get(1)); + l.add(Collections.unmodifiableList(pair)); + } + return Collections.unmodifiableList(l); + } + + public String toString() + { + return GeneralNames.class.getName() + " [ " + names + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/IssuerAlternativeNames.java b/libjava/classpath/gnu/java/security/x509/ext/IssuerAlternativeNames.java new file mode 100644 index 00000000000..8b017dc62d8 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/IssuerAlternativeNames.java @@ -0,0 +1,77 @@ +/* IssuerAlternatuveNames.java -- issuer alternative names extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; + +import java.io.IOException; +import java.util.List; + +public class IssuerAlternativeNames extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.18"); + + private final GeneralNames names; + + // Constructor. + // ------------------------------------------------------------------------- + + public IssuerAlternativeNames(final byte[] encoded) throws IOException + { + super(encoded); + names = new GeneralNames(encoded); + } + + // Instance method. + // ------------------------------------------------------------------------- + + public List getNames() + { + return names.getNames(); + } + + public String toString() + { + return IssuerAlternativeNames.class.getName() + " [ " + names + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/KeyUsage.java b/libjava/classpath/gnu/java/security/x509/ext/KeyUsage.java new file mode 100644 index 00000000000..dcd98181e12 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/KeyUsage.java @@ -0,0 +1,92 @@ +/* KeyUsage.java -- the key usage extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.BitString; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; + +public class KeyUsage extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.15"); + public static final int DIGITAL_SIGNATURE = 0; + public static final int NON_REPUDIATION = 1; + public static final int KEY_ENCIPHERMENT = 2; + public static final int DATA_ENCIPHERMENT = 3; + public static final int KEY_AGREEMENT = 4; + public static final int KEY_CERT_SIGN = 5; + public static final int CRL_SIGN = 6; + public static final int ENCIPHER_ONLY = 7; + public static final int DECIPHER_ONLY = 8; + + private final BitString keyUsage; + + // Constructor. + // ------------------------------------------------------------------------- + + public KeyUsage(final byte[] encoded) throws IOException + { + super(encoded); + DERValue val = DERReader.read(encoded); + if (val.getTag() != DER.BIT_STRING) + throw new IOException("malformed KeyUsage"); + keyUsage = (BitString) val.getValue(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public BitString getKeyUsage() + { + return keyUsage; + } + + public String toString() + { + return KeyUsage.class.getName() + " [ " + keyUsage + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/PolicyConstraint.java b/libjava/classpath/gnu/java/security/x509/ext/PolicyConstraint.java new file mode 100644 index 00000000000..20cf552a0fe --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/PolicyConstraint.java @@ -0,0 +1,107 @@ +/* PolicyConstraint.java -- policyConstraint extension + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.math.BigInteger; + +public class PolicyConstraint extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID ("2.5.29.36"); + + private final int requireExplicitPolicy; + private final int inhibitPolicyMapping; + + // Constructors. + // ------------------------------------------------------------------------- + + public PolicyConstraint (final byte[] encoded) throws IOException + { + super (encoded); + int rpc = -1, ipm = -1; + DERReader der = new DERReader(encoded); + DERValue pc = der.read(); + if (!pc.isConstructed()) + throw new IOException("malformed PolicyConstraints"); + DERValue val; + int len = pc.getLength(); + while (len > 0) + { + val = der.read(); + if (val.getTag() == 0) + rpc = new BigInteger ((byte[]) val.getValue()).intValue(); + else if (val.getTag() == 1) + ipm = new BigInteger ((byte[]) val.getValue()).intValue(); + else + throw new IOException ("invalid policy constraint"); + len -= val.getEncodedLength(); + } + + requireExplicitPolicy = rpc; + inhibitPolicyMapping = ipm; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public int getRequireExplicitPolicy() + { + return requireExplicitPolicy; + } + + public int getInhibitPolicyMapping() + { + return inhibitPolicyMapping; + } + + public String toString() + { + return PolicyConstraint.class.getName() + " [ requireExplicitPolicy=" + + requireExplicitPolicy + " inhibitPolicyMapping=" + inhibitPolicyMapping + + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/PolicyMappings.java b/libjava/classpath/gnu/java/security/x509/ext/PolicyMappings.java new file mode 100644 index 00000000000..0493ed89dd4 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/PolicyMappings.java @@ -0,0 +1,104 @@ +/* PolicyMappings.java -- policy mappings extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class PolicyMappings extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.33"); + + private final Map mappings; + + // Constructor. + // ------------------------------------------------------------------------- + + public PolicyMappings(final byte[] encoded) throws IOException + { + super(encoded); + DERReader der = new DERReader(encoded); + DERValue maps = der.read(); + if (!maps.isConstructed()) + throw new IOException("malformed PolicyMappings"); + int len = 0; + HashMap _mappings = new HashMap(); + while (len < maps.getLength()) + { + DERValue map = der.read(); + if (!map.isConstructed()) + throw new IOException("malformed PolicyMapping"); + DERValue val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("malformed PolicyMapping"); + OID issuerPolicy = (OID) val.getValue(); + val = der.read(); + if (val.getTag() != DER.OBJECT_IDENTIFIER) + throw new IOException("malformed PolicyMapping"); + OID subjectPolicy = (OID) val.getValue(); + _mappings.put(issuerPolicy, subjectPolicy); + len += map.getEncodedLength(); + } + mappings = Collections.unmodifiableMap(_mappings); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public OID getSubjectDomainPolicy(OID issuerDomainPolicy) + { + return (OID) mappings.get(issuerDomainPolicy); + } + + public String toString() + { + return PolicyMappings.class.getName() + " [ " + mappings + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/PrivateKeyUsagePeriod.java b/libjava/classpath/gnu/java/security/x509/ext/PrivateKeyUsagePeriod.java new file mode 100644 index 00000000000..3b531c05517 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/PrivateKeyUsagePeriod.java @@ -0,0 +1,105 @@ +/* PrivateKeyUsagePeriod.java -- private key usage period extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.util.Date; + +public class PrivateKeyUsagePeriod extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.16"); + + private final Date notBefore; + private final Date notAfter; + + // Constructor. + // ------------------------------------------------------------------------- + + public PrivateKeyUsagePeriod(final byte[] encoded) throws IOException + { + super(encoded); + DERReader der = new DERReader(encoded); + DERValue val = der.read(); + if (!val.isConstructed()) + throw new IOException("malformed PrivateKeyUsagePeriod"); + if (val.getLength() > 0) + val = der.read(); + if (val.getTagClass() == DER.APPLICATION || val.getTag() == 0) + { + notBefore = (Date) val.getValueAs (DER.GENERALIZED_TIME); + val = der.read(); + } + else + notBefore = null; + if (val.getTagClass() == DER.APPLICATION || val.getTag() == 1) + { + notAfter = (Date) val.getValueAs (DER.GENERALIZED_TIME); + } + else + notAfter = null; + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public Date getNotBefore() + { + return notBefore != null ? (Date) notBefore.clone() : null; + } + + public Date getNotAfter() + { + return notAfter != null ? (Date) notAfter.clone() : null; + } + + public String toString() + { + return PrivateKeyUsagePeriod.class.getName() + " [ notBefore=" + notBefore + + " notAfter=" + notAfter + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/ReasonCode.java b/libjava/classpath/gnu/java/security/x509/ext/ReasonCode.java new file mode 100644 index 00000000000..a6d59e43ae8 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/ReasonCode.java @@ -0,0 +1,85 @@ +/* ReasonCode.java -- a reason code for a certificate revocation. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; + +import java.io.IOException; +import java.math.BigInteger; + +public class ReasonCode extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.21"); + + public final int reason; + + // Constructor. + // ------------------------------------------------------------------------- + + public ReasonCode(final byte[] encoded) throws IOException + { + super(encoded); + DERValue val = DERReader.read(encoded); + if (val.getTag() != DER.ENUMERATED) + throw new IOException("malformed CRLReason"); + reason = ((BigInteger) val.getValue()).intValue(); + if (reason < 0 || reason == 7 || reason > 10) + throw new IOException("illegal reason: " + reason); + } + + // Instance method. + // ------------------------------------------------------------------------- + + public int getReasonCode() + { + return reason; + } + + public String toString() + { + return ReasonCode.class.getName() + " [ " + reason + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/SubjectAlternativeNames.java b/libjava/classpath/gnu/java/security/x509/ext/SubjectAlternativeNames.java new file mode 100644 index 00000000000..f88e854965f --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/SubjectAlternativeNames.java @@ -0,0 +1,77 @@ +/* SubjectAlternatuveNames.java -- subject alternative names extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; + +import java.io.IOException; +import java.util.List; + +public class SubjectAlternativeNames extends Extension.Value +{ + + // Constants and fields. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.17"); + + private final GeneralNames names; + + // Constructor. + // ------------------------------------------------------------------------- + + public SubjectAlternativeNames(final byte[] encoded) throws IOException + { + super(encoded); + names = new GeneralNames(encoded); + } + + // Instance method. + // ------------------------------------------------------------------------- + + public List getNames() + { + return names.getNames(); + } + + public String toString() + { + return SubjectAlternativeNames.class.getName() + " [ " + names + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/SubjectKeyIdentifier.java b/libjava/classpath/gnu/java/security/x509/ext/SubjectKeyIdentifier.java new file mode 100644 index 00000000000..fc65abe211c --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/SubjectKeyIdentifier.java @@ -0,0 +1,84 @@ +/* SubjectKeyIdentifier.java -- subject key identifier extension. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.security.x509.ext; + +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERReader; +import gnu.java.security.der.DERValue; +import gnu.java.security.x509.Util; + +import java.io.IOException; + +public class SubjectKeyIdentifier extends Extension.Value +{ + + // Constant. + // ------------------------------------------------------------------------- + + public static final OID ID = new OID("2.5.29.14"); + + private final byte[] keyIdentifier; + + // Constructor. + // ------------------------------------------------------------------------- + + public SubjectKeyIdentifier(final byte[] encoded) throws IOException + { + super(encoded); + DERValue val = DERReader.read(encoded); + if (val.getTag() != DER.OCTET_STRING) + throw new IOException("malformed SubjectKeyIdentifier"); + keyIdentifier = (byte[]) val.getValue(); + } + + // Instance methods. + // ------------------------------------------------------------------------- + + public byte[] getKeyIdentifier() + { + return (byte[]) keyIdentifier.clone(); + } + + public String toString() + { + return SubjectKeyIdentifier.class.getName() + " [ " + + Util.toHexString (keyIdentifier, ':') + " ]"; + } +} diff --git a/libjava/classpath/gnu/java/security/x509/ext/package.html b/libjava/classpath/gnu/java/security/x509/ext/package.html new file mode 100644 index 00000000000..cc44e55c97e --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/ext/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.x509.ext package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.x509.ext</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/security/x509/package.html b/libjava/classpath/gnu/java/security/x509/package.html new file mode 100644 index 00000000000..8b0ba008451 --- /dev/null +++ b/libjava/classpath/gnu/java/security/x509/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.security.x509 package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.security.x509</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/text/AttributedFormatBuffer.java b/libjava/classpath/gnu/java/text/AttributedFormatBuffer.java new file mode 100644 index 00000000000..8cfc8f56ddb --- /dev/null +++ b/libjava/classpath/gnu/java/text/AttributedFormatBuffer.java @@ -0,0 +1,247 @@ +/* AttributedFormatBuffer.java -- Implements an attributed FormatBuffer. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.text; + +import java.text.AttributedCharacterIterator; +import java.util.ArrayList; +import java.util.HashMap; + +/** + * This class is an implementation of a FormatBuffer with attributes. + * + * @author Guilhem Lavaux <guilhem@kaffe.org> + * @date April 10, 2004 + */ +public class AttributedFormatBuffer implements FormatBuffer +{ + private StringBuffer buffer; + private ArrayList ranges; + private ArrayList attributes; + private int[] a_ranges; + private HashMap[] a_attributes; + private int startingRange; + AttributedCharacterIterator.Attribute defaultAttr; + + /** + * This constructor accepts a StringBuffer. If the buffer contains + * already some characters they will not be attributed. + */ + public AttributedFormatBuffer(StringBuffer buffer) + { + this.buffer = buffer; + this.ranges = new ArrayList(); + this.attributes = new ArrayList(); + this.defaultAttr = null; + if (buffer.length() != 0) + { + this.startingRange = buffer.length(); + addAttribute(buffer.length(), null); + } + else + this.startingRange = -1; + } + + public AttributedFormatBuffer(int prebuffer) + { + this(new StringBuffer(prebuffer)); + } + + public AttributedFormatBuffer() + { + this(10); + } + + /** + * This method is a helper function for formatters. Given a set of ranges + * and attributes it adds exactly one attribute for the range of characters + * comprised between the last entry in 'ranges' and the specified new range. + * + * @param new_range A new range to insert in the list. + * @param new_attribute A new attribute to insert in the list. + */ + private final void addAttribute(int new_range, AttributedCharacterIterator.Attribute attr) + { + HashMap map; + + if (attr != null) + { + map = new HashMap(); + map.put(attr, attr); + attributes.add(map); + } + else + attributes.add(null); + + ranges.add(new Integer(new_range)); + } + + public void append(String s) + { + if (startingRange < 0) + startingRange = 0; + buffer.append(s); + } + + public void append(String s, AttributedCharacterIterator.Attribute attr) + { + setDefaultAttribute(attr); + startingRange = buffer.length(); + append(s); + setDefaultAttribute(null); + } + + public void append(String s, int[] ranges, HashMap[] attrs) + { + int curPos = buffer.length(); + + setDefaultAttribute(null); + if (ranges != null) + { + for (int i = 0; i < ranges.length; i++) + { + this.ranges.add(new Integer(ranges[i] + curPos)); + this.attributes.add(attrs[i]); + } + } + startingRange = buffer.length(); + buffer.append(s); + } + + public void append(char c) + { + if (startingRange < 0) + startingRange = buffer.length(); + buffer.append(c); + } + + public void append(char c, AttributedCharacterIterator.Attribute attr) + { + setDefaultAttribute(attr); + buffer.append(c); + setDefaultAttribute(null); + } + + public void setDefaultAttribute(AttributedCharacterIterator.Attribute attr) + { + if (attr == defaultAttr) + return; + + int currentPos = buffer.length(); + + if (startingRange != currentPos && startingRange >= 0) + { + addAttribute(currentPos, defaultAttr); + } + defaultAttr = attr; + startingRange = currentPos; + } + + public AttributedCharacterIterator.Attribute getDefaultAttribute() + { + return defaultAttr; + } + + public void cutTail(int length) + { + buffer.setLength(buffer.length()-length); + } + + public int length() + { + return buffer.length(); + } + + public void clear() + { + buffer.setLength(0); + ranges.clear(); + attributes.clear(); + defaultAttr = null; + startingRange = -1; + } + + /** + * This method synchronizes the state of the attribute array. + * After calling it you may call {@link #getDefaultAttribute()}. + */ + public void sync() + { + if (startingRange < 0 || startingRange == buffer.length()) + return; + + addAttribute(buffer.length(), defaultAttr); + + a_ranges = new int[ranges.size()]; + for (int i = 0; i < a_ranges.length; i++) + a_ranges[i] = ((Integer)(ranges.get (i))).intValue(); + + a_attributes = new HashMap[attributes.size()]; + System.arraycopy(attributes.toArray(), 0, a_attributes, 0, a_attributes.length); + } + + /** + * This method returns the internal StringBuffer describing + * the attributed string. + * + * @return An instance of StringBuffer which contains the string. + */ + public StringBuffer getBuffer() + { + return buffer; + } + + /** + * This method returns the ranges for the attributes. + * + * @return An array of int describing the ranges. + */ + public int[] getRanges() + { + return a_ranges; + } + + /** + * This method returns the array containing the map on the + * attributes. + * + * @return An array of {@link java.util.Map} containing the attributes. + */ + public HashMap[] getAttributes() + { + return a_attributes; + } +} diff --git a/libjava/classpath/gnu/java/text/BaseBreakIterator.java b/libjava/classpath/gnu/java/text/BaseBreakIterator.java new file mode 100644 index 00000000000..c28a2089ad7 --- /dev/null +++ b/libjava/classpath/gnu/java/text/BaseBreakIterator.java @@ -0,0 +1,121 @@ +/* BaseBreakIterator.java -- Base class for default BreakIterators + Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.text; + +import java.text.BreakIterator; +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; + +/** + * @author Tom Tromey <tromey@cygnus.com> + * @date March 22, 1999 + */ + +public abstract class BaseBreakIterator extends BreakIterator +{ + public BaseBreakIterator () + { + // It isn't documented, but break iterators are created in a + // working state; their methods won't throw exceptions before + // setText(). + iter = new StringCharacterIterator(""); + } + + public int current () + { + return iter.getIndex(); + } + + public int first () + { + iter.first(); + return iter.getBeginIndex(); + } + + public int following (int pos) + { + int save = iter.getIndex(); + iter.setIndex(pos); + int r = next (); + iter.setIndex(save); + return r; + } + + public CharacterIterator getText () + { + return iter; + } + + public int last () + { + iter.last(); + // Go past the last character. + iter.next(); + return iter.getEndIndex(); + } + + public int next (int n) + { + int r = iter.getIndex (); + if (n > 0) + { + while (n > 0 && r != DONE) + { + r = next (); + --n; + } + } + else if (n < 0) + { + while (n < 0 && r != DONE) + { + r = previous (); + ++n; + } + } + return r; + } + + public void setText (CharacterIterator newText) + { + iter = newText; + } + + protected CharacterIterator iter; +} diff --git a/libjava/classpath/gnu/java/text/CharacterBreakIterator.java b/libjava/classpath/gnu/java/text/CharacterBreakIterator.java new file mode 100644 index 00000000000..5274543a9e5 --- /dev/null +++ b/libjava/classpath/gnu/java/text/CharacterBreakIterator.java @@ -0,0 +1,213 @@ +/* CharacterBreakIterator.java - Default character BreakIterator. + Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.text; + +import java.text.CharacterIterator; + +/** + * @author Tom Tromey <tromey@cygnus.com> + * @date March 19, 1999 + * Written using The Unicode Standard, Version 2.0. + */ + +public class CharacterBreakIterator extends BaseBreakIterator +{ + // Hangul Jamo constants from Unicode book. + private static final int LBase = 0x1100; + private static final int VBase = 0x1161; + private static final int TBase = 0x11a7; + private static final int LCount = 19; + private static final int VCount = 21; + private static final int TCount = 28; + + // Information about surrogates. + private static final int highSurrogateStart = 0xD800; + private static final int highSurrogateEnd = 0xDBFF; + private static final int lowSurrogateStart = 0xDC00; + private static final int lowSurrogateEnd = 0xDFFF; + + public Object clone () + { + return new CharacterBreakIterator (this); + } + + public CharacterBreakIterator () + { + } + + private CharacterBreakIterator (CharacterBreakIterator other) + { + iter = (CharacterIterator) other.iter.clone(); + } + + // Some methods to tell us different properties of characters. + private final boolean isL (char c) + { + return c >= LBase && c <= LBase + LCount; + } + private final boolean isV (char c) + { + return c >= VBase && c <= VBase + VCount; + } + private final boolean isT (char c) + { + return c >= TBase && c <= TBase + TCount; + } + private final boolean isLVT (char c) + { + return isL (c) || isV (c) || isT (c); + } + private final boolean isHighSurrogate (char c) + { + return c >= highSurrogateStart && c <= highSurrogateEnd; + } + private final boolean isLowSurrogate (char c) + { + return c >= lowSurrogateStart && c <= lowSurrogateEnd; + } + + public int next () + { + int end = iter.getEndIndex(); + if (iter.getIndex() == end) + return DONE; + + char c; + for (char prev = CharacterIterator.DONE; iter.getIndex() < end; prev = c) + { + c = iter.next(); + if (c == CharacterIterator.DONE) + break; + int type = Character.getType(c); + + // Break after paragraph separators. + if (type == Character.PARAGRAPH_SEPARATOR) + break; + + // Now we need some lookahead. + char ahead = iter.next(); + iter.previous(); + if (ahead == CharacterIterator.DONE) + break; + int aheadType = Character.getType(ahead); + + if (aheadType != Character.NON_SPACING_MARK + && ! isLowSurrogate (ahead) + && ! isLVT (ahead)) + break; + if (! isLVT (c) && isLVT (ahead)) + break; + if (isL (c) && ! isLVT (ahead) + && aheadType != Character.NON_SPACING_MARK) + break; + if (isV (c) && ! isV (ahead) && !isT (ahead) + && aheadType != Character.NON_SPACING_MARK) + break; + if (isT (c) && ! isT (ahead) + && aheadType != Character.NON_SPACING_MARK) + break; + + if (! isHighSurrogate (c) && isLowSurrogate (ahead)) + break; + if (isHighSurrogate (c) && ! isLowSurrogate (ahead)) + break; + if (! isHighSurrogate (prev) && isLowSurrogate (c)) + break; + } + + return iter.getIndex(); + } + + public int previous () + { + if (iter.getIndex() == iter.getBeginIndex()) + return DONE; + + while (iter.getIndex() >= iter.getBeginIndex()) + { + char c = iter.previous(); + if (c == CharacterIterator.DONE) + break; + int type = Character.getType(c); + + if (type != Character.NON_SPACING_MARK + && ! isLowSurrogate (c) + && ! isLVT (c)) + break; + + // Now we need some lookahead. + char ahead = iter.previous(); + if (ahead == CharacterIterator.DONE) + { + iter.next(); + break; + } + char ahead2 = iter.previous(); + iter.next(); + iter.next(); + if (ahead2 == CharacterIterator.DONE) + break; + int aheadType = Character.getType(ahead); + + if (aheadType == Character.PARAGRAPH_SEPARATOR) + break; + + if (isLVT (c) && ! isLVT (ahead)) + break; + if (! isLVT (c) && type != Character.NON_SPACING_MARK + && isL (ahead)) + break; + if (! isV (c) && ! isT (c) && type != Character.NON_SPACING_MARK + && isV (ahead)) + break; + if (! isT (c) && type != Character.NON_SPACING_MARK + && isT (ahead)) + break; + + if (isLowSurrogate (c) && ! isHighSurrogate (ahead)) + break; + if (! isLowSurrogate (c) && isHighSurrogate (ahead)) + break; + if (isLowSurrogate (ahead) && ! isHighSurrogate (ahead2)) + break; + } + + return iter.getIndex(); + } +} diff --git a/libjava/classpath/gnu/java/text/FormatBuffer.java b/libjava/classpath/gnu/java/text/FormatBuffer.java new file mode 100644 index 00000000000..e6b68208792 --- /dev/null +++ b/libjava/classpath/gnu/java/text/FormatBuffer.java @@ -0,0 +1,136 @@ +/* FormatBuffer.java -- General interface to build attributed strings. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.text; + +import java.text.AttributedCharacterIterator; +import java.util.HashMap; + +/** + * This interface describes a modifiable buffer which contains attributed + * characters. The implementation may or may not implements attributes. It + * aims to greatly simplify and clarify the implementation of java.text + * formatters. The buffer may be appended or have its tail cut. It may also + * be completely cleant up. + * + * @author Guilhem Lavaux <guilhem@kaffe.org> + * @date April 10, 2004 + */ +public interface FormatBuffer +{ + /** + * This method appends a simple string to the buffer. This part of + * the buffer will be attributed using the default attribute. + * + * @param s The string to append to the buffer. + */ + public void append(String s); + + /** + * This method appends a simple string to the buffer. This part of + * the buffer will have the specified attribute (and only this one). + * The default attribute may be changed after calling this method. + * + * @param s The string to append to the buffer. + * @param attr Attribute to use for the string in the buffer. + */ + public void append(String s, AttributedCharacterIterator.Attribute attr); + + /** + * This method appends a simple string to the buffer. This part of + * the buffer will be attributed using the specified ranges and attributes. + * To have an example on how to specify ranges see {@link gnu.java.text.FormatCharacterIterator}. + * + * @param s The string to append to the buffer. + * @param ranges The ranges describing how the attributes should be applied + * to the string. + * @param attrs The attributes of the string in the buffer. + */ + public void append(String s, int[] ranges, HashMap[] attrs); + + /** + * This method appends a simple char to the buffer. This part of + * the buffer will be attributed using the default attribute. + * + * @param c The character to append to the buffer. + */ + public void append(char c); + + /** + * This method appends a simple character to the buffer. This part of + * the buffer will have the specified attribute (and only this one). + * The default attribute may be changed after calling this method. + * + * @param c The character to append to the buffer. + * @param attr Attribute to use for the character in the buffer. + */ + public void append(char c, AttributedCharacterIterator.Attribute attr); + + /** + * This method changes the current default attribute for the next string + * or character which will be appended to the buffer. + * + * @param attr The attribute which will be used by default. + */ + public void setDefaultAttribute(AttributedCharacterIterator.Attribute attr); + + /** + * This method returns the current default attribute for the buffer. + * + * @return The default attribute for the buffer. + */ + public AttributedCharacterIterator.Attribute getDefaultAttribute(); + + /** + * This method cuts the last characters of the buffer. The number of + * characters to cut is given by "length". + * + * @param length Number of characters to cut at the end of the buffer. + */ + public void cutTail(int length); + + /** + * This method resets completely the buffer. + */ + public void clear(); + + /** + * This method returns the number of character in the buffer. + * + * @return The number of character in the buffer. + */ + public int length(); +} diff --git a/libjava/classpath/gnu/java/text/FormatCharacterIterator.java b/libjava/classpath/gnu/java/text/FormatCharacterIterator.java new file mode 100644 index 00000000000..60773aa1eb7 --- /dev/null +++ b/libjava/classpath/gnu/java/text/FormatCharacterIterator.java @@ -0,0 +1,533 @@ +/* FormatCharacter.java -- Implementation of AttributedCharacterIterator for + formatters. + Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.text; + +import java.text.AttributedCharacterIterator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.Vector; + +/** + * This class should not be put public and it is only intended to the + * classes of the java.text package. Its aim is to build a segmented + * character iterator by appending strings and adding attributes to + * portions of strings. The code intends to do some optimization + * concerning memory consumption and attribute access but at the + * end it is only an AttributedCharacterIterator. + * + * @author Guilhem Lavaux <guilhem@kaffe.org> + * @date November 22, 2003 + */ +public class FormatCharacterIterator implements AttributedCharacterIterator +{ + private String formattedString; + private int charIndex; + private int attributeIndex; + private int[] ranges; + private HashMap[] attributes; + private static final boolean DEBUG = false; + + /** + * This constructor builds an empty iterated strings. The attributes + * are empty and so is the string. However you may append strings + * and attributes to this iterator. + */ + public FormatCharacterIterator() + { + formattedString = ""; + ranges = new int[0]; + attributes = new HashMap[0]; + } + + /** + * This constructor take a string <code>s</code>, a set of ranges + * and the corresponding attributes. This is used to build an iterator. + * The array <code>ranges</code> should be formatted as follow: + * each element of <code>ranges</code> specifies the index in the string + * until which the corresponding map of attributes at the same position + * is applied. For example, if you have: + * <pre> + * s = "hello"; + * ranges = new int[] { 2, 6 }; + * attributes = new HashMap[2]; + * </pre> + * <code>"he"</code> will have the attributes <code>attributes[0]</code>, + * <code>"llo"</code> the <code>attributes[1]</code>. + */ + public FormatCharacterIterator (String s, int[] ranges, HashMap[] attributes) + { + formattedString = s; + this.ranges = ranges; + this.attributes = attributes; + } + + /* + * The following methods are inherited from AttributedCharacterIterator, + * and thus are already documented. + */ + + public Set getAllAttributeKeys() + { + if (attributes != null && attributes[attributeIndex] != null) + return attributes[attributeIndex].keySet(); + else + return new HashSet(); + } + + public Map getAttributes() + { + if (attributes != null && attributes[attributeIndex] != null) + return attributes[attributeIndex]; + else + return new HashMap(); + } + + public Object getAttribute (AttributedCharacterIterator.Attribute attrib) + { + if (attributes != null && attributes[attributeIndex] != null) + return attributes[attributeIndex].get (attrib); + else + return null; + } + + public int getRunLimit(Set reqAttrs) + { + if (attributes == null) + return formattedString.length(); + + int currentAttrIndex = attributeIndex; + Set newKeys; + + do + { + currentAttrIndex++; + if (currentAttrIndex == attributes.length) + return formattedString.length(); + if (attributes[currentAttrIndex] == null) + break; + newKeys = attributes[currentAttrIndex].keySet(); + } + while (newKeys.containsAll (reqAttrs)); + + return ranges[currentAttrIndex-1]; + } + + public int getRunLimit (AttributedCharacterIterator.Attribute attribute) + { + Set s = new HashSet(); + + s.add (attribute); + return getRunLimit (s); + } + + public int getRunLimit() + { + if (attributes == null) + return formattedString.length(); + if (attributes[attributeIndex] == null) + { + for (int i=attributeIndex+1;i<attributes.length;i++) + if (attributes[i] != null) + return ranges[i-1]; + return formattedString.length(); + } + + return getRunLimit (attributes[attributeIndex].keySet()); + } + + public int getRunStart (Set reqAttrs) + { + if (attributes == null) + return formattedString.length(); + + int currentAttrIndex = attributeIndex; + Set newKeys = null; + + do + { + if (currentAttrIndex == 0) + return 0; + + currentAttrIndex--; + if (attributes[currentAttrIndex] == null) + break; + newKeys = attributes[currentAttrIndex].keySet(); + } + while (newKeys.containsAll (reqAttrs)); + + return (currentAttrIndex > 0) ? ranges[currentAttrIndex-1] : 0; + } + + public int getRunStart() + { + if (attributes == null) + return 0; + + if (attributes[attributeIndex] == null) + { + for (int i=attributeIndex;i>0;i--) + if (attributes[i] != null) + return ranges[attributeIndex-1]; + return 0; + } + + return getRunStart (attributes[attributeIndex].keySet()); + } + + public int getRunStart (AttributedCharacterIterator.Attribute attribute) + { + Set s = new HashSet(); + + s.add (attribute); + return getRunStart (s); + } + + public Object clone() + { + return new FormatCharacterIterator (formattedString, ranges, attributes); + } + + /* + * The following methods are inherited from CharacterIterator and thus + * are already documented. + */ + + public char current() + { + return formattedString.charAt (charIndex); + } + + public char first() + { + charIndex = 0; + attributeIndex = 0; + return formattedString.charAt (0); + } + + public int getBeginIndex() + { + return 0; + } + + public int getEndIndex() + { + return formattedString.length(); + } + + public int getIndex() + { + return charIndex; + } + + public char last() + { + charIndex = formattedString.length()-1; + if (attributes != null) + attributeIndex = attributes.length-1; + return formattedString.charAt (charIndex); + } + + public char next() + { + charIndex++; + if (charIndex >= formattedString.length()) + { + charIndex = getEndIndex(); + return DONE; + } + if (attributes != null) + { + if (charIndex >= ranges[attributeIndex]) + attributeIndex++; + } + return formattedString.charAt (charIndex); + } + + public char previous() + { + charIndex--; + if (charIndex < 0) + { + charIndex = 0; + return DONE; + } + + if (attributes != null) + { + if (charIndex < ranges[attributeIndex]) + attributeIndex--; + } + return formattedString.charAt (charIndex); + } + + public char setIndex (int position) + { + if (position < 0 || position > formattedString.length()) + throw new IllegalArgumentException ("position is out of range"); + + charIndex = position; + if (attributes != null) + { + for (attributeIndex=0;attributeIndex<attributes.length; + attributeIndex++) + if (ranges[attributeIndex] > charIndex) + break; + attributeIndex--; + } + if (charIndex == formattedString.length()) + return DONE; + else + return formattedString.charAt (charIndex); + } + + /** + * This method merge the specified attributes and ranges with the + * internal tables. This method is in charge of the optimization + * of tables. Two following sets of attributes are never the same. + * + * @see #FormatCharacterIterator() + * + * @param attributes the new array attributes to apply to the string. + */ + public void mergeAttributes (HashMap[] attributes, int[] ranges) + { + Vector new_ranges = new Vector(); + Vector new_attributes = new Vector(); + int i = 0, j = 0; + + debug("merging " + attributes.length + " attrs"); + + while (i < this.ranges.length && j < ranges.length) + { + if (this.attributes[i] != null) + { + new_attributes.add (this.attributes[i]); + if (attributes[j] != null) + this.attributes[i].putAll (attributes[j]); + } + else + { + new_attributes.add (attributes[j]); + } + if (this.ranges[i] == ranges[j]) + { + new_ranges.add (new Integer (ranges[j])); + i++; + j++; + } + else if (this.ranges[i] < ranges[j]) + { + new_ranges.add (new Integer (this.ranges[i])); + i++; + } + else + { + new_ranges.add (new Integer (ranges[j])); + j++; + } + } + + if (i != this.ranges.length) + { + for (;i<this.ranges.length;i++) + { + new_attributes.add (this.attributes[i]); + new_ranges.add (new Integer (this.ranges[i])); + } + } + if (j != ranges.length) + { + for (;j<ranges.length;j++) + { + new_attributes.add (attributes[j]); + new_ranges.add (new Integer (ranges[j])); + } + } + + this.attributes = new HashMap[new_attributes.size()]; + this.ranges = new int[new_ranges.size()]; + System.arraycopy (new_attributes.toArray(), 0, this.attributes, + 0, this.attributes.length); + + for (i=0;i<new_ranges.size();i++) + { + this.ranges[i] = ((Integer)new_ranges.elementAt (i)).intValue(); + } + + dumpTable(); + } + + /** + * This method appends to the internal attributed string the attributed + * string contained in the specified iterator. + * + * @param iterator the iterator which contains the attributed string to + * append to this iterator. + */ + public void append (AttributedCharacterIterator iterator) + { + char c = iterator.first(); + Vector more_ranges = new Vector(); + Vector more_attributes = new Vector(); + + do + { + formattedString = formattedString + String.valueOf (c); + // TODO: Reduce the size of the output array. + more_attributes.add (iterator.getAttributes()); + more_ranges.add (new Integer (formattedString.length())); + // END TOOD + c = iterator.next(); + } + while (c != DONE); + + HashMap[] new_attributes = new HashMap[attributes.length + + more_attributes.size()]; + int[] new_ranges = new int[ranges.length + more_ranges.size()]; + + System.arraycopy (attributes, 0, new_attributes, 0, attributes.length); + System.arraycopy (more_attributes.toArray(), 0, new_attributes, + attributes.length, more_attributes.size()); + + System.arraycopy (ranges, 0, new_ranges, 0, ranges.length); + Object[] new_ranges_array = more_ranges.toArray(); + for (int i = 0; i < more_ranges.size();i++) + new_ranges[i+ranges.length] = ((Integer) new_ranges_array[i]).intValue(); + + attributes = new_attributes; + ranges = new_ranges; + } + + /** + * This method appends an attributed string which attributes are specified + * directly in the calling parameters. + * + * @param text The string to append. + * @param local_attributes The attributes to put on this string in the + * iterator. If it is <code>null</code> the string will simply have no + * attributes. + */ + public void append (String text, HashMap local_attributes) + { + int[] new_ranges = new int[ranges.length+1]; + HashMap[] new_attributes = new HashMap[attributes.length+1]; + + formattedString += text; + System.arraycopy (attributes, 0, new_attributes, 0, attributes.length); + System.arraycopy (ranges, 0, new_ranges, 0, ranges.length); + new_ranges[ranges.length] = formattedString.length(); + new_attributes[attributes.length] = local_attributes; + + ranges = new_ranges; + attributes = new_attributes; + } + + /** + * This method appends a string without attributes. It is completely + * equivalent to call {@link #append(String,HashMap)} with local_attributes + * equal to <code>null</code>. + * + * @param text The string to append to the iterator. + */ + public void append (String text) + { + append (text, null); + } + + /** + * This method adds a set of attributes to a range of character. The + * bounds are always inclusive. In the case many attributes have to + * be added it is advised to directly use {@link #mergeAttributes([Ljava.util.HashMap;[I} + * + * @param attributes Attributes to merge into the iterator. + * @param range_start Lower bound of the range of characters which will receive the + * attribute. + * @param range_end Upper bound of the range of characters which will receive the + * attribute. + * + * @throws IllegalArgumentException if ranges are out of bounds. + */ + public void addAttributes(HashMap attributes, int range_start, int range_end) + { + if (range_start == 0) + mergeAttributes(new HashMap[] { attributes }, new int[] { range_end }); + else + mergeAttributes(new HashMap[] { null, attributes }, new int[] { range_start, range_end }); + } + + private void debug(String s) + { + if (DEBUG) + System.out.println(s); + } + + private void dumpTable() + { + int start_range = 0; + + if (!DEBUG) + return; + + System.out.println("Dumping internal table:"); + for (int i = 0; i < ranges.length; i++) + { + System.out.print("\t" + start_range + " => " + ranges[i] + ":"); + if (attributes[i] == null) + System.out.println("null"); + else + { + Set keyset = attributes[i].keySet(); + if (keyset != null) + { + Iterator keys = keyset.iterator(); + + while (keys.hasNext()) + System.out.print(" " + keys.next()); + } + else + System.out.println("keySet null"); + System.out.println(); + } + } + System.out.println(); + System.out.flush(); + } +} diff --git a/libjava/classpath/gnu/java/text/LineBreakIterator.java b/libjava/classpath/gnu/java/text/LineBreakIterator.java new file mode 100644 index 00000000000..ad07479fb78 --- /dev/null +++ b/libjava/classpath/gnu/java/text/LineBreakIterator.java @@ -0,0 +1,194 @@ +/* LineBreakIterator.java - Default word BreakIterator. + Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.text; + +import java.text.CharacterIterator; + +/** + * @author Tom Tromey <tromey@cygnus.com> + * @date March 22, 1999 + * Written using The Unicode Standard, Version 2.0. + */ + +public class LineBreakIterator extends BaseBreakIterator +{ + public Object clone () + { + return new LineBreakIterator (this); + } + + public LineBreakIterator () + { + } + + private LineBreakIterator (LineBreakIterator other) + { + iter = (CharacterIterator) other.iter.clone(); + } + + // Some methods to tell us different properties of characters. + private final boolean isNb (char c) + { + return (c == 0x00a0 // NO-BREAK SPACE + || c == 0x2011 // NON-BREAKING HYPHEN + || c == 0xfeff); // ZERO WITH NO-BREAK SPACE + } + private final boolean isClose (int type) + { + return (type == Character.END_PUNCTUATION + // Unicode book says "comma, period, ...", which I take to + // mean "Po" class. + || type == Character.OTHER_PUNCTUATION); + } + private final boolean isIdeo (char c) + { + return (c >= 0x3040 && c <= 0x309f // Hiragana + || c >= 0x30a0 && c <= 0x30ff // Katakana + || c >= 0x4e00 && c <= 0x9fff // Han + || c >= 0x3100 && c <= 0x312f); // Bopomofo + } + + public int next () + { + int end = iter.getEndIndex(); + if (iter.getIndex() == end) + return DONE; + + while (iter.getIndex() < end) + { + char c = iter.current(); + int type = Character.getType(c); + + char n = iter.next(); + + if (n == CharacterIterator.DONE + || type == Character.PARAGRAPH_SEPARATOR + || type == Character.LINE_SEPARATOR) + break; + + // Handle two cases where we must scan for non-spacing marks. + int start = iter.getIndex(); + if (type == Character.SPACE_SEPARATOR + || type == Character.START_PUNCTUATION + || isIdeo (c)) + { + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.NON_SPACING_MARK) + n = iter.next(); + if (n == CharacterIterator.DONE) + break; + + if (type == Character.SPACE_SEPARATOR) + { + int nt = Character.getType(n); + if (nt != Character.NON_SPACING_MARK + && nt != Character.SPACE_SEPARATOR + && ! isNb (n)) + break; + } + else if (type == Character.START_PUNCTUATION) + { + if (isIdeo (n)) + { + // Open punctuation followed by non spacing marks + // and then ideograph does not have a break in + // it. So skip all this. + start = iter.getIndex(); + } + } + else + { + // Ideograph preceded this character. + if (isClose (Character.getType(n))) + break; + } + } + iter.setIndex(start); + } + + return iter.getIndex(); + } + + public int previous () + { + int start = iter.getBeginIndex(); + if (iter.getIndex() == start) + return DONE; + + while (iter.getIndex() >= start) + { + char c = iter.previous(); + if (c == CharacterIterator.DONE) + break; + int type = Character.getType(c); + + char n = iter.previous(); + if (n == CharacterIterator.DONE) + break; + iter.next(); + + int nt = Character.getType(n); + // Break after paragraph separators. + if (nt == Character.PARAGRAPH_SEPARATOR + || nt == Character.LINE_SEPARATOR) + break; + + // Skip non-spacing marks. + int init = iter.getIndex(); + while (n != CharacterIterator.DONE && nt == Character.NON_SPACING_MARK) + { + n = iter.previous(); + nt = Character.getType(n); + } + + if (nt == Character.SPACE_SEPARATOR + && type != Character.SPACE_SEPARATOR + && type != Character.NON_SPACING_MARK + && ! isNb (c)) + break; + if (! isClose (type) && isIdeo (n)) + break; + if (isIdeo (c) && nt != Character.START_PUNCTUATION) + break; + iter.setIndex(init); + } + + return iter.getIndex(); + } +} diff --git a/libjava/classpath/gnu/java/text/SentenceBreakIterator.java b/libjava/classpath/gnu/java/text/SentenceBreakIterator.java new file mode 100644 index 00000000000..f91d269bb08 --- /dev/null +++ b/libjava/classpath/gnu/java/text/SentenceBreakIterator.java @@ -0,0 +1,247 @@ +/* SentenceBreakIterator.java - Default sentence BreakIterator. + Copyright (C) 1999, 2001, 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.text; + +import java.text.CharacterIterator; + +/** + * @author Tom Tromey <tromey@cygnus.com> + * @date March 23, 1999 + * Written using The Unicode Standard, Version 2.0. + */ + +public class SentenceBreakIterator extends BaseBreakIterator +{ + public Object clone () + { + return new SentenceBreakIterator (this); + } + + public SentenceBreakIterator () + { + } + + private SentenceBreakIterator (SentenceBreakIterator other) + { + iter = (CharacterIterator) other.iter.clone(); + } + + public int next () + { + int end = iter.getEndIndex(); + if (iter.getIndex() == end) + return DONE; + + while (iter.getIndex() < end) + { + char c = iter.current(); + if (c == CharacterIterator.DONE) + break; + int type = Character.getType(c); + + char n = iter.next(); + if (n == CharacterIterator.DONE) + break; + + // Always break after paragraph separator. + if (type == Character.PARAGRAPH_SEPARATOR) + break; + + if (c == '!' || c == '?') + { + // Skip close punctuation. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.END_PUNCTUATION) + n = iter.next(); + // Skip (java) space, line and paragraph separators. + while (n != CharacterIterator.DONE && Character.isWhitespace(n)) + n = iter.next(); + + // There's always a break somewhere after `!' or `?'. + break; + } + + if (c == '.') + { + int save = iter.getIndex(); + // Skip close punctuation. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.END_PUNCTUATION) + n = iter.next(); + // Skip (java) space, line and paragraph separators. + // We keep count because we need at least one for this period to + // represent a terminator. + int spcount = 0; + while (n != CharacterIterator.DONE && Character.isWhitespace(n)) + { + n = iter.next(); + ++spcount; + } + if (spcount > 0) + { + int save2 = iter.getIndex(); + // Skip over open puncutation. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.START_PUNCTUATION) + n = iter.next(); + // Next character must not be lower case. + if (n == CharacterIterator.DONE + || ! Character.isLowerCase(n)) + { + iter.setIndex(save2); + break; + } + } + iter.setIndex(save); + } + } + + return iter.getIndex(); + } + + private final int previous_internal () + { + int start = iter.getBeginIndex(); + if (iter.getIndex() == start) + return DONE; + + while (iter.getIndex() >= start) + { + char c = iter.previous(); + if (c == CharacterIterator.DONE) + break; + + char n = iter.previous(); + if (n == CharacterIterator.DONE) + break; + iter.next(); + int nt = Character.getType(n); + + if (! Character.isLowerCase(c) + && (nt == Character.START_PUNCTUATION + || Character.isWhitespace(n))) + { + int save = iter.getIndex(); + int save_nt = nt; + char save_n = n; + // Skip open punctuation. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.START_PUNCTUATION) + n = iter.previous(); + if (n == CharacterIterator.DONE) + break; + if (Character.isWhitespace(n)) + { + // Must have at least one (java) space after the `.'. + int save2 = iter.getIndex(); + while (n != CharacterIterator.DONE + && Character.isWhitespace(n)) + n = iter.previous(); + // Skip close punctuation. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.END_PUNCTUATION) + n = iter.previous(); + if (n == CharacterIterator.DONE || n == '.') + { + // Communicate location of actual end. + period = iter.getIndex(); + iter.setIndex(save2); + break; + } + } + iter.setIndex(save); + nt = save_nt; + n = save_n; + } + + if (nt == Character.PARAGRAPH_SEPARATOR) + { + // Communicate location of actual end. + period = iter.getIndex(); + break; + } + else if (Character.isWhitespace(n) + || nt == Character.END_PUNCTUATION) + { + int save = iter.getIndex(); + // Skip (java) space, line and paragraph separators. + while (n != CharacterIterator.DONE + && Character.isWhitespace(n)) + n = iter.previous(); + // Skip close punctuation. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.END_PUNCTUATION) + n = iter.previous(); + int here = iter.getIndex(); + iter.setIndex(save); + if (n == CharacterIterator.DONE || n == '!' || n == '?') + { + // Communicate location of actual end. + period = here; + break; + } + } + else if (n == '!' || n == '?') + { + // Communicate location of actual end. + period = iter.getIndex(); + break; + } + } + + return iter.getIndex(); + } + + public int previous () + { + // We want to skip over the first sentence end to the second one. + // However, at the end of the string we want the first end. + int here = iter.getIndex(); + period = here; + int first = previous_internal (); + if (here == iter.getEndIndex() || first == DONE) + return first; + iter.setIndex(period); + return previous_internal (); + } + + // This is used for communication between previous and + // previous_internal. + private int period; +} diff --git a/libjava/classpath/gnu/java/text/StringFormatBuffer.java b/libjava/classpath/gnu/java/text/StringFormatBuffer.java new file mode 100644 index 00000000000..5772186b4c5 --- /dev/null +++ b/libjava/classpath/gnu/java/text/StringFormatBuffer.java @@ -0,0 +1,121 @@ +/* StringFormatBuffer.java -- Implements FormatBuffer using StringBuffer. + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.java.text; + +import java.text.AttributedCharacterIterator; +import java.util.HashMap; + +/** + * This class is an implementation of a FormatBuffer without attributes. + * + * @author Guilhem Lavaux <guilhem@kaffe.org> + * @date April 10, 2004 + */ +public class StringFormatBuffer implements FormatBuffer +{ + private StringBuffer buffer; + private AttributedCharacterIterator.Attribute defaultAttr; + + public StringFormatBuffer(int prebuffer) + { + buffer = new StringBuffer(prebuffer); + } + + public StringFormatBuffer(StringBuffer buffer) + { + this.buffer = buffer; + } + + public void append(String s) + { + buffer.append(s); + } + + public void append(String s, AttributedCharacterIterator.Attribute attr) + { + buffer.append(s); + } + + public void append(String s, int[] ranges, HashMap[] attrs) + { + buffer.append(s); + } + + public void append(char c) + { + buffer.append(c); + } + + public void append(char c, AttributedCharacterIterator.Attribute attr) + { + buffer.append(c); + } + + public void setDefaultAttribute(AttributedCharacterIterator.Attribute attr) + { + defaultAttr = attr; + } + + public AttributedCharacterIterator.Attribute getDefaultAttribute() + { + return defaultAttr; + } + + public void cutTail(int length) + { + buffer.setLength(buffer.length()-length); + } + + public int length() + { + return buffer.length(); + } + + public void clear() + { + buffer.setLength(0); + } + + /** + * This method returns the internal {@link java.lang.StringBuffer} which + * contains the string of character. + */ + public StringBuffer getBuffer() + { + return buffer; + } +} diff --git a/libjava/classpath/gnu/java/text/WordBreakIterator.java b/libjava/classpath/gnu/java/text/WordBreakIterator.java new file mode 100644 index 00000000000..f140369f8c6 --- /dev/null +++ b/libjava/classpath/gnu/java/text/WordBreakIterator.java @@ -0,0 +1,250 @@ +/* WordBreakIterator.java - Default word BreakIterator. + Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.text; + +import java.text.CharacterIterator; + +/** + * @author Tom Tromey <tromey@cygnus.com> + * @date March 22, 1999 + * Written using The Unicode Standard, Version 2.0. + */ + +public class WordBreakIterator extends BaseBreakIterator +{ + public Object clone () + { + return new WordBreakIterator (this); + } + + public WordBreakIterator () + { + } + + private WordBreakIterator (WordBreakIterator other) + { + iter = (CharacterIterator) other.iter.clone(); + } + + // Some methods to tell us different properties of characters. + private final boolean isHira (char c) + { + return c >= 0x3040 && c <= 0x309f; + } + private final boolean isKata (char c) + { + return c >= 0x30a0 && c <= 0x30ff; + } + private final boolean isHan (char c) + { + return c >= 0x4e00 && c <= 0x9fff; + } + + public int next () + { + int end = iter.getEndIndex(); + if (iter.getIndex() == end) + return DONE; + + while (iter.getIndex() < end) + { + char c = iter.current(); + if (c == CharacterIterator.DONE) + break; + int type = Character.getType(c); + + char n = iter.next(); + if (n == CharacterIterator.DONE) + break; + + // Break after paragraph separators. + if (type == Character.PARAGRAPH_SEPARATOR + || type == Character.LINE_SEPARATOR) + break; + + // Break between letters and non-letters. + // FIXME: we treat apostrophe as part of a word. This + // is an English-ism. + boolean is_letter = Character.isLetter(c); + if (c != '\'' && ! is_letter && type != Character.NON_SPACING_MARK + && Character.isLetter(n)) + break; + + // Always break after certain symbols, such as punctuation. + // This heuristic is derived from hints in the JCL book and is + // not part of Unicode. It seems to be right, however. + // FIXME: we treat apostrophe as part of a word. This + // is an English-ism. + if (c != '\'' + && (type == Character.DASH_PUNCTUATION + || type == Character.START_PUNCTUATION + || type == Character.END_PUNCTUATION + || type == Character.CONNECTOR_PUNCTUATION + || type == Character.OTHER_PUNCTUATION + || type == Character.MATH_SYMBOL + || type == Character.CURRENCY_SYMBOL + || type == Character.MODIFIER_SYMBOL + || type == Character.OTHER_SYMBOL + || type == Character.FORMAT + || type == Character.CONTROL)) + break; + + boolean is_hira = isHira (c); + boolean is_kata = isKata (c); + boolean is_han = isHan (c); + + // Special case Japanese. + if (! is_hira && ! is_kata && ! is_han + && type != Character.NON_SPACING_MARK + && (isHira (n) || isKata (n) || isHan (n))) + break; + + if (is_hira || is_kata || is_han || is_letter) + { + // Now we need to do some lookahead. We might need to do + // quite a bit of lookahead, so we save our position and + // restore it later. + int save = iter.getIndex(); + // Skip string of non spacing marks. + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.NON_SPACING_MARK) + n = iter.next(); + if (n == CharacterIterator.DONE) + break; + if ((is_hira && ! isHira (n)) + || (is_kata && ! isHira (n) && ! isKata (n)) + || (is_han && ! isHira (n) && ! isHan (n)) + // FIXME: we treat apostrophe as part of a word. This + // is an English-ism. + || (is_letter && ! Character.isLetter(n) && n != '\'')) + break; + iter.setIndex(save); + } + } + + return iter.getIndex(); + } + + public int previous () + { + int start = iter.getBeginIndex(); + if (iter.getIndex() == start) + return DONE; + + while (iter.getIndex() >= start) + { + char c = iter.previous(); + if (c == CharacterIterator.DONE) + break; + + boolean is_hira = isHira (c); + boolean is_kata = isKata (c); + boolean is_han = isHan (c); + boolean is_letter = Character.isLetter(c); + + char n = iter.previous(); + if (n == CharacterIterator.DONE) + break; + iter.next(); + int type = Character.getType(n); + // Break after paragraph separators. + if (type == Character.PARAGRAPH_SEPARATOR + || type == Character.LINE_SEPARATOR) + break; + + // Break between letters and non-letters. + // FIXME: we treat apostrophe as part of a word. This + // is an English-ism. + if (n != '\'' && ! Character.isLetter(n) + && type != Character.NON_SPACING_MARK + && is_letter) + break; + + // Always break after certain symbols, such as punctuation. + // This heuristic is derived from hints in the JCL book and is + // not part of Unicode. It seems to be right, however. + // FIXME: we treat apostrophe as part of a word. This + // is an English-ism. + if (n != '\'' + && (type == Character.DASH_PUNCTUATION + || type == Character.START_PUNCTUATION + || type == Character.END_PUNCTUATION + || type == Character.CONNECTOR_PUNCTUATION + || type == Character.OTHER_PUNCTUATION + || type == Character.MATH_SYMBOL + || type == Character.CURRENCY_SYMBOL + || type == Character.MODIFIER_SYMBOL + || type == Character.OTHER_SYMBOL + || type == Character.FORMAT + || type == Character.CONTROL)) + break; + + // Special case Japanese. + if ((is_hira || is_kata || is_han) + && ! isHira (n) && ! isKata (n) && ! isHan (n) + && type != Character.NON_SPACING_MARK) + break; + + // We might have to skip over non spacing marks to see what's + // on the other side. + if (! is_hira || (! is_letter && c != '\'')) + { + int save = iter.getIndex(); + while (n != CharacterIterator.DONE + && Character.getType(n) == Character.NON_SPACING_MARK) + n = iter.previous(); + iter.setIndex(save); + // This is a strange case: a bunch of non-spacing marks at + // the beginning. We treat the current location as a word + // break. + if (n == CharacterIterator.DONE) + break; + if ((isHira (n) && ! is_hira) + || (isKata (n) && ! is_hira && ! is_kata) + || (isHan (n) && ! is_hira && ! is_han) + // FIXME: we treat apostrophe as part of a word. This + // is an English-ism. + || (! is_letter && c != '\'' && Character.isLetter(n))) + break; + } + } + + return iter.getIndex(); + } +} diff --git a/libjava/classpath/gnu/java/text/package.html b/libjava/classpath/gnu/java/text/package.html new file mode 100644 index 00000000000..a1025a8e934 --- /dev/null +++ b/libjava/classpath/gnu/java/text/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.text package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.text</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/util/DoubleEnumeration.java b/libjava/classpath/gnu/java/util/DoubleEnumeration.java new file mode 100644 index 00000000000..1fc37f8e0f8 --- /dev/null +++ b/libjava/classpath/gnu/java/util/DoubleEnumeration.java @@ -0,0 +1,138 @@ +/* gnu.java.util.DoubleEnumeration + Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util; + +import java.util.Enumeration; +import java.util.NoSuchElementException; + + +/** + * This is a helper class that combines two Enumerations. + * It returns the elements of the first Enumeration until it has + * no more elements and then returns the elements of the second + * Enumeration.<br> + * + * In the default case: + * <pre> + * doubleEnum = new DoubleEnumeration(enum1, enum2); + * while (doubleEnum.hasMoreElements()) { + * Object o = doubleEnum.nextElement(); + * do_something(o); + * } + * </pre> + * it calls hasMoreElements of the Enumerations as few times as + * possible. + * The references to the Enumerations are cleared as soon as they have no + * more elements to help garbage collecting. + * + * @author Jochen Hoenicke + * @author Mark Wielaard (mark@klomp.org) + */ +public class DoubleEnumeration implements Enumeration +{ + /** + * This is true as long as one of the enumerations has more + * elements. + * Only valid when hasChecked is true. + * Set in <code>hasMoreElements()</code> + */ + private boolean hasMore; + /** + * This is true, if it is sure that hasMore indicates wether there are + * more elements. + * Set to true in <code>hasMoreElements()</code>. + * Set to false in <code>getNextElement()</code>. + */ + private boolean hasChecked; + /** + * The first enumeration. + */ + private Enumeration e1; + /** + * The second enumeration. + */ + private Enumeration e2; + + /** + * Creates a new Enumeration combining the given two enumerations. + * The enumerations mustn't be accessed by other classes. + */ + public DoubleEnumeration(Enumeration e1, Enumeration e2) + { + this.e1 = e1; + this.e2 = e2; + hasChecked = false; + } + + /** + * Returns true, if at least one of the two enumerations has more + * elements. + */ + public boolean hasMoreElements() + { + if (hasChecked) + return hasMore; + + hasMore = (e1 != null && e1.hasMoreElements()); + + if (!hasMore) { + e1 = e2; + e2 = null; + hasMore = (e1 != null && e1.hasMoreElements()); + } + + hasChecked = true; + return hasMore; + } + + /** + * Returns the next element. This returns the next element of the + * first enumeration, if it has more elements, otherwise the next + * element of the second enumeration. If both enumeration don't have + * any elements it throws a <code>NoSuchElementException</code>. + */ + public Object nextElement() + { + if (!hasMoreElements()) + throw new NoSuchElementException(); + else { + hasChecked = false; + return e1.nextElement(); + } + } +} diff --git a/libjava/classpath/gnu/java/util/EmptyEnumeration.java b/libjava/classpath/gnu/java/util/EmptyEnumeration.java new file mode 100644 index 00000000000..46a82d6cb53 --- /dev/null +++ b/libjava/classpath/gnu/java/util/EmptyEnumeration.java @@ -0,0 +1,96 @@ +/* EmptyEnumeration.java -- a constant empty enumeration + Copyright (C) 2001, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util; + +import java.io.Serializable; +import java.util.Enumeration; +import java.util.NoSuchElementException; + +/** + * This is a helper class that produces an empty Enumerations. There is only + * one instance of this class that can be used whenever one needs a + * non-null but empty enumeration. Using this class prevents multiple + * small objects and inner classes. <code>getInstance()</code> returns + * the only instance of this class. It can be shared by multiple objects and + * threads. + * + * @author Mark Wielaard (mark@klomp.org) + */ +public final class EmptyEnumeration implements Enumeration, Serializable +{ + /** The only instance of this class */ + private static final EmptyEnumeration instance = new EmptyEnumeration(); + + /** + * Private constructor that creates a new empty Enumeration. + */ + private EmptyEnumeration() + { + } + + /** + * Returns the only instance of this class. + * It can be shared by multiple objects and threads. + * + * @return the common empty enumeration + */ + public static EmptyEnumeration getInstance() + { + return instance; + } + + /** + * Returns false, since there are no elements. + * + * @return false + */ + public boolean hasMoreElements() + { + return false; + } + + /** + * Always throws <code>NoSuchElementException</code>, since it is empty. + * + * @throws NoSuchElementException this is empty + */ + public Object nextElement() + { + throw new NoSuchElementException(); + } +} diff --git a/libjava/classpath/gnu/java/util/package.html b/libjava/classpath/gnu/java/util/package.html new file mode 100644 index 00000000000..d90fa59d4dc --- /dev/null +++ b/libjava/classpath/gnu/java/util/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.util package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.util</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/java/util/prefs/FileBasedFactory.java b/libjava/classpath/gnu/java/util/prefs/FileBasedFactory.java new file mode 100644 index 00000000000..70f3558fc6f --- /dev/null +++ b/libjava/classpath/gnu/java/util/prefs/FileBasedFactory.java @@ -0,0 +1,57 @@ +/* FileBasedFactory - Default Classpath implementation of a PreferencesFactory + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util.prefs; + +import java.util.prefs.*; + +/** + * Default Classpath implementation of a PreferencesFactory. + * Returns system and user root Preferences nodes that are read from files. + * + * @author Mark Wielaard (mark@klomp.org) + */ +public class FileBasedFactory implements PreferencesFactory { + + public Preferences systemRoot() { + return null; + } + + public Preferences userRoot() { + return null; + } +} diff --git a/libjava/classpath/gnu/java/util/prefs/MemoryBasedFactory.java b/libjava/classpath/gnu/java/util/prefs/MemoryBasedFactory.java new file mode 100644 index 00000000000..abaf0016f5b --- /dev/null +++ b/libjava/classpath/gnu/java/util/prefs/MemoryBasedFactory.java @@ -0,0 +1,64 @@ +/* MemoryBasedFactory - Memory based PreferencesFactory usefull for testing + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util.prefs; + +import java.util.prefs.*; + +/** + * Memory based PreferencesFactory usefull for testing. + * Returns completely empty Preferences for system and user roots. + * All changes are only backed by the current instances in memory. + * + * @author Mark Wielaard (mark@klomp.org) + */ +public class MemoryBasedFactory implements PreferencesFactory { + + // Static fields containing the preferences root nodes + private static final Preferences systemPreferences + = new MemoryBasedPreferences(null, "", false); + private static final Preferences userPreferences + = new MemoryBasedPreferences(null, "", true); + + public Preferences systemRoot() { + return systemPreferences; + } + + public Preferences userRoot() { + return userPreferences; + } +} diff --git a/libjava/classpath/gnu/java/util/prefs/MemoryBasedPreferences.java b/libjava/classpath/gnu/java/util/prefs/MemoryBasedPreferences.java new file mode 100644 index 00000000000..b2f321c9c7e --- /dev/null +++ b/libjava/classpath/gnu/java/util/prefs/MemoryBasedPreferences.java @@ -0,0 +1,144 @@ +/* MemoryBasedPreferences - A Preference node which holds all entries in memory + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util.prefs; + +import java.util.HashMap; + +import java.util.prefs.*; + +/** + * A Preference node which holds all entries in memory + * + * @author Mark Wielaard (mark@klomp.org) + */ +public class MemoryBasedPreferences extends AbstractPreferences { + + /** True if this is a preference node in the user tree, false otherwise. */ + private final boolean isUser; + + /** Contains all the preference entries of this node. */ + private HashMap entries = new HashMap(); + + /** + * Creates a new preferences node with the given name and parent. + * When isUser is true it will be user node otherwise it will be a system + * node. It will always set the <code>newNode</code> field to true + * since there is no real backing store, so all nodes are new. + */ + public MemoryBasedPreferences(MemoryBasedPreferences parent, + String name, + boolean isUser) { + super(parent, name); + this.isUser = isUser; + + // Since we do not have a real backing store all nodes are new + newNode = true; + } + + /** + * Returns true if this node was created as a user node. + */ + public boolean isUserNode() { + return isUser; + } + + /** + * Returns an empty array since all children names are always already + * chached. + */ + protected String[] childrenNamesSpi() throws BackingStoreException { + return new String[0]; + } + + /** + * Returns a new node with the given name with as parent this node and + * with the <code>isUser</code> flag set to the same value as this node. + */ + protected AbstractPreferences childSpi(String childName) { + return new MemoryBasedPreferences(this, childName, isUser); + } + + /** + * Returns a (possibly empty) array of keys of the preferences entries of + * this node. + */ + protected String[] keysSpi() throws BackingStoreException { + return (String[]) entries.keySet().toArray(new String[entries.size()]); + } + + /** + * Returns the associated value from this nodes preferences entries or + * null when the key has not been set. + */ + protected String getSpi(String key) { + return (String) entries.get(key); + } + + /** + * Sets the value for the given key. + */ + protected void putSpi(String key, String value) { + entries.put(key, value); + } + + /** + * Removes the entry with the given key. + */ + protected void removeSpi(String key) { + entries.remove(key); + } + + /** + * Does nothing since we do not have any backing store. + */ + protected void flushSpi() { + } + + /** + * Does nothing since we do not have any backing store. + */ + protected void syncSpi() { + } + + /** + * Just removes the entries map of this node. + */ + protected void removeNodeSpi() { + entries = null; + } +} diff --git a/libjava/classpath/gnu/java/util/prefs/NodeReader.java b/libjava/classpath/gnu/java/util/prefs/NodeReader.java new file mode 100644 index 00000000000..4cd52e5dbf9 --- /dev/null +++ b/libjava/classpath/gnu/java/util/prefs/NodeReader.java @@ -0,0 +1,223 @@ +/* NodeReader - Reads and imports preferences nodes from files + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util.prefs; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.prefs.InvalidPreferencesFormatException; +import java.util.prefs.Preferences; +import java.util.prefs.PreferencesFactory; + +/** + * Reads and imports preferences nodes from files. + * + * @author Mark Wielaard (mark@klomp.org) + */ +public class NodeReader { + + private final BufferedReader br; + private String line = ""; + + private final PreferencesFactory factory; + + public NodeReader(Reader r, PreferencesFactory factory) { + if(r instanceof BufferedReader) { + br = (BufferedReader) r; + } else { + br = new BufferedReader(r); + } + this.factory = factory; + } + + public NodeReader(InputStream is, PreferencesFactory factory) { + this(new InputStreamReader(is), factory); + } + + public void importPreferences() + throws InvalidPreferencesFormatException, IOException + { + readPreferences(); + } + + private void readPreferences() + throws InvalidPreferencesFormatException, IOException + { + // Begin starting tag + skipTill("<preferences"); + + readRoot(); + + // Ending tag + skipTill("</preferences>"); + } + + private void readRoot() + throws InvalidPreferencesFormatException, IOException + { + // Begin starting tag + skipTill("<root"); + + // type attribute + skipTill("type=\""); + String type = readTill("\""); + Preferences root; + if ("user".equals(type)) { + root = factory.userRoot(); + } else if ("system".equals(type)) { + root = factory.systemRoot(); + } else { + throw new InvalidPreferencesFormatException("Unknown type: " + + type); + } + + // Read root map and subnodes + readMap(root); + readNodes(root); + + // Ending tag + skipTill("</root>"); + } + + private void readNodes(Preferences node) + throws InvalidPreferencesFormatException, IOException + { + while ("node".equals(nextTag())) { + skipTill("<node"); + skipTill("name=\""); + String name = readTill("\""); + Preferences subnode = node.node(name); + System.out.println("Found subnode: " + subnode.absolutePath()); + readMap(subnode); + readNodes(subnode); + skipTill("</node>"); + } + + } + + private void readMap(Preferences node) + throws InvalidPreferencesFormatException, IOException + { + // Begin map tag + skipTill("<map"); + + // Empty map? + if (line.startsWith("/>")) { + line = line.substring(2); + return; + } + + // Map entries + readEntries(node); + + // Ending tag + skipTill("</map>"); + } + + private void readEntries(Preferences node) + throws InvalidPreferencesFormatException, IOException + { + while ("entry".equals(nextTag())) { + skipTill("<entry"); + skipTill("key=\""); + String key = readTill("\""); + skipTill("value=\""); + String value = readTill("\""); + System.out.println("Key: " + key + " Value: " + value); + node.put(key, value); + } + } + + private void skipTill(String s) + throws InvalidPreferencesFormatException, IOException + { + while(true) { + if (line == null) + throw new InvalidPreferencesFormatException(s + " not found"); + + int index = line.indexOf(s); + if (index == -1) { + line = br.readLine(); + } else { + line = line.substring(index+s.length()); + return; + } + } + } + + private String readTill(String s) + throws InvalidPreferencesFormatException + { + int index = line.indexOf(s); + if (index == -1) + throw new InvalidPreferencesFormatException(s + " not found"); + + String read = line.substring(0, index); + line = line.substring(index+s.length()); + + return read; + } + + private String nextTag() + throws InvalidPreferencesFormatException, IOException + { + while(true) { + if (line == null) + throw new InvalidPreferencesFormatException("unexpected EOF"); + + int start = line.indexOf("<"); + if (start == -1) { + line = br.readLine(); + } else { + // Find end of tag + int end = start+1; + while (end != line.length() + && " \t\r\n".indexOf(line.charAt(end)) == -1) { + end++; + } + // Line now starts at the found tag + String tag = line.substring(start+1,end); + line = line.substring(start); + return tag; + } + } + } + +} diff --git a/libjava/classpath/gnu/java/util/prefs/NodeWriter.java b/libjava/classpath/gnu/java/util/prefs/NodeWriter.java new file mode 100644 index 00000000000..1eed9e66e2d --- /dev/null +++ b/libjava/classpath/gnu/java/util/prefs/NodeWriter.java @@ -0,0 +1,315 @@ +/* NodeWriter - Writes and exports preferences nodes to files + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.java.util.prefs; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; + +import java.util.StringTokenizer; + +import java.util.prefs.*; + +/** + * Writes and exports preferences nodes to files + * + * @author Mark Wielaard (mark@klomp.org) + */ +public class NodeWriter { + + /** The Preferences node to write. */ + private final Preferences prefs; + + /** The bufferedWriter to write the node to. */ + private final BufferedWriter bw; + + /** + * True if the complete sub tree should be written, + * false if only the node should be written. + */ + private boolean subtree; + + /** + * Creates a new NodeWriter for the given preferences node and writer. + */ + public NodeWriter(Preferences prefs, Writer w) { + this.prefs = prefs; + if (w instanceof BufferedWriter) { + this.bw = (BufferedWriter) w; + } else { + this.bw = new BufferedWriter(w); + } + } + + /** + * Creates a new NodeWriter for the given preferences node and + * outputstream. Creates a new OutputStreamWriter. + */ + public NodeWriter(Preferences prefs, OutputStream os) { + this(prefs, new OutputStreamWriter(os)); + } + + /** + * Writes the preference node plus the complete subtree. + */ + public void writePrefsTree() throws BackingStoreException, IOException { + subtree = true; + writeHeader(); + writePreferences(); + bw.flush(); + } + + /** + * Writes only the preference node. + */ + public void writePrefs() throws BackingStoreException, IOException { + subtree = false; + writeHeader(); + writePreferences(); + bw.flush(); + } + + /** + * Writes the standard header. + */ + private void writeHeader() throws BackingStoreException, IOException { + bw.write("<?xml version=\"1.0\"?>"); + bw.newLine(); + bw.newLine(); + bw.write("<!-- GNU Classpath java.util.prefs Preferences "); + + if (prefs.isUserNode()) { + bw.write("user"); + } else { + bw.write("system"); + } + + // root node? + if (prefs.parent() == null) { + bw.write(" root"); + } + + if (subtree) { + bw.write(" tree"); + } else { + bw.write(" node"); + } + + // no root? + if (prefs.parent() != null) { + bw.newLine(); + bw.write(" '"); + bw.write(prefs.absolutePath()); + bw.write('\''); + bw.newLine(); + } + bw.write(" -->"); + bw.newLine(); + bw.newLine(); + } + + /** + * Write the preferences tag and the root. + */ + private void writePreferences() throws BackingStoreException, IOException { + bw.write("<preferences>"); + bw.newLine(); + writeRoot(); + bw.write("</preferences>"); + bw.newLine(); + } + + private void writeRoot() throws BackingStoreException, IOException { + bw.write(" <root type=\""); + if (prefs.isUserNode()) { + bw.write("user"); + } else { + bw.write("system"); + } + bw.write("\"/>"); + + writeRootMap(); + writeNode(); + + bw.write(" </root>"); + bw.newLine(); + } + + private void writeRootMap() throws BackingStoreException, IOException { + // Is it a root node? + if(prefs.parent() == null && prefs.keys().length > 0) { + bw.newLine(); + writeMap(prefs, 2); + } else { + bw.write("<map/>"); + bw.newLine(); + } + } + + /** + * Writes all the parents of the preferences node without any entries. + * Returns the number of parents written, which has to be used as + * argument to <code>writeCloseParents()</code> after writing the node + * itself. + */ + private int writeParents() throws IOException { + int parents; + String path = prefs.absolutePath(); + int lastslash = path.lastIndexOf("/"); + if (lastslash > 0) { + path = path.substring(1, lastslash); + StringTokenizer st = new StringTokenizer(path); + parents = st.countTokens(); + + System.out.println("path: " + path); + System.out.println("parents: " + parents); + + for (int i=0; i<parents; i++) { + String name = st.nextToken(); + indent(i+2); + bw.write("<node name=\"" + name + "\">"); + bw.write("<map/>"); + bw.write("</node>"); + bw.newLine(); + } + } else { + parents = 0; + } + + return parents; + } + + private void writeCloseParents(int parents) throws IOException { + while(parents > 0) { + indent(parents+1); + bw.write("</node>"); + bw.newLine(); + parents--; + } + } + + private void writeNode() throws BackingStoreException, IOException { + int parents = writeParents(); + // root? + int indent; + if (prefs.parent() == null) { + indent = parents+1; + } else { + indent = parents+2; + } + writeNode(prefs, indent); + writeCloseParents(parents); + } + + private void writeNode(Preferences node, int indent) + throws BackingStoreException, IOException + { + // not root? + if (node.parent() != null) { + indent(indent); + bw.write("<node name=\"" + node.name() + "\">"); + if (node.keys().length > 0) { + bw.newLine(); + } + writeMap(node, indent+1); + } + + if (subtree) { + String[] children = node.childrenNames(); + for (int i=0; i<children.length; i++) { + Preferences child = node.node(children[i]); + writeNode(child, indent+1); + } + } + + // not root? + if (node.parent() != null) { + indent(indent); + bw.write("</node>"); + bw.newLine(); + } + } + + private void writeMap(Preferences node, int indent) + throws BackingStoreException, IOException + { + // construct String used for indentation + StringBuffer indentBuffer = new StringBuffer(2*indent); + for (int i=0; i < indent; i++) + indentBuffer.append(" "); + String indentString = indentBuffer.toString(); + + if (node.keys().length > 0) { + bw.write(indentString); + bw.write("<map>"); + bw.newLine(); + writeEntries(node, indentString + " "); + bw.write(indentString); + bw.write("</map>"); + } else { + bw.write("<map/>"); + } + bw.newLine(); + } + + private void writeEntries(Preferences node, String indent) + throws BackingStoreException, IOException + { + String[] keys = node.keys(); + for(int i = 0; i < keys.length; i++) { + String value = node.get(keys[i], null); + if (value == null) { + throw new BackingStoreException("null value for key '" + + keys[i] + "'"); + } + + bw.write(indent); + bw.write("<entry key=\"" + keys[i] + "\"" + + " value=\"" + value + "\"/>"); + bw.newLine(); + } + } + + private void indent(int x) throws IOException { + for (int i=0; i<x; i++) { + bw.write(" "); + } + } +} diff --git a/libjava/classpath/gnu/java/util/prefs/package.html b/libjava/classpath/gnu/java/util/prefs/package.html new file mode 100644 index 00000000000..ee5d67f725c --- /dev/null +++ b/libjava/classpath/gnu/java/util/prefs/package.html @@ -0,0 +1,46 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in gnu.java.util.prefs package. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.java.util.prefs</title></head> + +<body> +<p></p> + +</body> +</html> diff --git a/libjava/classpath/gnu/javax/rmi/CORBA/DelegateFactory.java b/libjava/classpath/gnu/javax/rmi/CORBA/DelegateFactory.java new file mode 100644 index 00000000000..d71546d7561 --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/CORBA/DelegateFactory.java @@ -0,0 +1,75 @@ +/* DelegateFactory.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi.CORBA; + +import java.util.HashMap; + +public class DelegateFactory +{ + private static HashMap cache = new HashMap(4); + + public static synchronized Object getInstance(String type) + throws GetDelegateInstanceException + { + Object r = cache.get(type); + if (r != null) + return r; + String dcname = System.getProperty("javax.rmi.CORBA." + type + "Class"); + if (dcname == null) + { + //throw new DelegateException + // ("no javax.rmi.CORBA.XXXClass property sepcified."); + dcname = "gnu.javax.rmi.CORBA." + type + "DelegateImpl"; + } + try + { + Class dclass = Class.forName(dcname, + true, + Thread.currentThread().getContextClassLoader()); + r = dclass.newInstance(); + cache.put(type, r); + return r; + } + catch(Exception e) + { + throw new GetDelegateInstanceException + ("Exception when trying to get delegate instance:" + dcname, e); + } + } +} diff --git a/libjava/classpath/gnu/javax/rmi/CORBA/GetDelegateInstanceException.java b/libjava/classpath/gnu/javax/rmi/CORBA/GetDelegateInstanceException.java new file mode 100644 index 00000000000..7d98bb57bf5 --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/CORBA/GetDelegateInstanceException.java @@ -0,0 +1,55 @@ +/* GetDelegateInstanceException.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi.CORBA; + +public class GetDelegateInstanceException + extends Exception +{ + private Throwable next; + + public GetDelegateInstanceException(String msg) + { + super(msg); + } + + public GetDelegateInstanceException(String msg, Throwable next) + { + super(msg, next); + } +} diff --git a/libjava/classpath/gnu/javax/rmi/CORBA/PortableRemoteObjectDelegateImpl.java b/libjava/classpath/gnu/javax/rmi/CORBA/PortableRemoteObjectDelegateImpl.java new file mode 100644 index 00000000000..6a7e6b74622 --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/CORBA/PortableRemoteObjectDelegateImpl.java @@ -0,0 +1,136 @@ +/* PortableRemoteObjectDelegateImpl.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi.CORBA; + +import gnu.javax.rmi.PortableServer; + +import java.rmi.NoSuchObjectException; +import java.rmi.Remote; +import java.rmi.RemoteException; + +import javax.rmi.CORBA.PortableRemoteObjectDelegate; + +public class PortableRemoteObjectDelegateImpl + implements PortableRemoteObjectDelegate +{ + + public PortableRemoteObjectDelegateImpl() + { + } + + public void connect(Remote remote, Remote remote1) + throws RemoteException + { + throw new Error("Not implemented for PortableRemoteObjectDelegateImpl"); + } + + public void exportObject(Remote obj) + throws RemoteException + { + PortableServer.exportObject(obj); + } + + public Object narrow(Object narrowFrom, Class narrowTo) + throws ClassCastException + { + if (narrowTo == null) + throw new ClassCastException("Can't narrow to null class"); + if (narrowFrom == null) + return null; + + Class fromClass = narrowFrom.getClass(); + Object result = null; + + try + { + if (narrowTo.isAssignableFrom(fromClass)) + result = narrowFrom; + else + { + System.out.println("We still haven't implement this case: narrow " + + narrowFrom + " of type " + fromClass + " to " + + narrowTo); + Class[] cs = fromClass.getInterfaces(); + for (int i = 0; i < cs.length; i++) + System.out.println(cs[i]); + Exception e1 = new Exception(); + try + { + throw e1; + } + catch(Exception ee) + { + ee.printStackTrace(); + } + System.exit(2); + //throw new Error("We still haven't implement this case: narrow " + // + narrowFrom + " of type " + fromClass + " to " + // + narrowTo); + /* + ObjectImpl objimpl = (ObjectImpl)narrowFrom; + if(objimpl._is_a(PortableServer.getTypeName(narrowTo))) + result = PortableServer.getStubFromObjectImpl(objimpl, narrowTo); + */ + } + } + catch(Exception e) + { + result = null; + } + + if (result == null) + throw new ClassCastException("Can't narrow from " + + fromClass + " to " + narrowTo); + + return result; + } + + public Remote toStub(Remote obj) + throws NoSuchObjectException + { + return PortableServer.toStub(obj); + } + + public void unexportObject(Remote obj) + throws NoSuchObjectException + { + PortableServer.unexportObject(obj); + } + +} diff --git a/libjava/classpath/gnu/javax/rmi/CORBA/StubDelegateImpl.java b/libjava/classpath/gnu/javax/rmi/CORBA/StubDelegateImpl.java new file mode 100644 index 00000000000..998d59e3379 --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/CORBA/StubDelegateImpl.java @@ -0,0 +1,104 @@ +/* StubDelegateImpl.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi.CORBA; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.rmi.RemoteException; +import javax.rmi.CORBA.Stub; +import javax.rmi.CORBA.StubDelegate; + +public class StubDelegateImpl + implements StubDelegate +{ + + private int hashCode; + + public StubDelegateImpl(){ + hashCode = 0; + } + // XXX javax.rmi.ORB -> org.omg.CORBA.ORB + public void connect(Stub self, javax.rmi.ORB orb) + throws RemoteException + { + throw new Error("Not implemented for StubDelegate"); + } + + public boolean equals(Stub self, Object obj) + { + if(self == null || obj == null) + return self == obj; + if(!(obj instanceof Stub)) + return false; + return self.hashCode() == ((Stub)obj).hashCode(); + } + + public int hashCode(Stub self) + { + //FIX ME + return hashCode; + } + + public String toString(Stub self) + { + try + { + return self._orb().object_to_string(self); + } + // XXX javax.rmi.BAD_OPERATION -> org.omg.CORBA.BAD_OPERATION + catch(javax.rmi.BAD_OPERATION bad_operation) + { + return null; + } + } + + public void readObject(Stub self, ObjectInputStream s) + throws IOException, ClassNotFoundException + { + throw new Error("Not implemented for StubDelegate"); + } + + public void writeObject(Stub self, ObjectOutputStream s) + throws IOException + { + throw new Error("Not implemented for StubDelegate"); + } + +} diff --git a/libjava/classpath/gnu/javax/rmi/CORBA/UtilDelegateImpl.java b/libjava/classpath/gnu/javax/rmi/CORBA/UtilDelegateImpl.java new file mode 100644 index 00000000000..7bed2aa22dc --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/CORBA/UtilDelegateImpl.java @@ -0,0 +1,152 @@ +/* UtilDelegateImpl.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi.CORBA; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.server.RMIClassLoader; +import java.net.MalformedURLException; +import java.io.*; +//import org.omg.CORBA.ORB; +//import org.omg.CORBA.SystemException; +//import org.omg.CORBA.portable.InputStream; +//import org.omg.CORBA.portable.OutputStream; +import javax.rmi.CORBA.*; + +public class UtilDelegateImpl + implements UtilDelegate +{ + // XXX javax.rmi.ORB -> org.omg.CORBA.ORB + public Object copyObject(Object obj, javax.rmi.ORB orb) + throws RemoteException + { + throw new Error("Not implemented for UtilDelegate"); + } + + // XXX javax.rmi.ORB -> org.omg.CORBA.ORB + public Object[] copyObjects(Object obj[], javax.rmi.ORB orb) + throws RemoteException + { + throw new Error("Not implemented for UtilDelegate"); + } + + public ValueHandler createValueHandler() + { + throw new Error("Not implemented for UtilDelegate"); + } + + public String getCodebase(Class clz) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public Tie getTie(Remote target) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public boolean isLocal(Stub stub) + throws RemoteException + { + throw new Error("Not implemented for UtilDelegate"); + } + + public Class loadClass(String className, String remoteCodebase, + ClassLoader loader) + throws ClassNotFoundException + { + try{ + if (remoteCodebase == null) + return RMIClassLoader.loadClass(className); + else + return RMIClassLoader.loadClass(remoteCodebase, className); + } + catch (MalformedURLException e1) + { + throw new ClassNotFoundException(className, e1); + } + catch(ClassNotFoundException e2) + { + if(loader != null) + return loader.loadClass(className); + else + return null; + } + } + + public RemoteException mapSystemException(SystemException ex) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public Object readAny(InputStream in) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public void registerTarget(Tie tie, Remote target) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public void unexportObject(Remote target) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public RemoteException wrapException(Throwable orig) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public void writeAbstractObject(OutputStream out, Object obj) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public void writeAny(OutputStream out, Object obj) + { + throw new Error("Not implemented for UtilDelegate"); + } + + public void writeRemoteObject(OutputStream out, Object obj) + { + throw new Error("Not implemented for UtilDelegate"); + } +} diff --git a/libjava/classpath/gnu/javax/rmi/CORBA/ValueHandlerImpl.java b/libjava/classpath/gnu/javax/rmi/CORBA/ValueHandlerImpl.java new file mode 100644 index 00000000000..906381fe478 --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/CORBA/ValueHandlerImpl.java @@ -0,0 +1,82 @@ +/* ValueHandlerImpl.java -- + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi.CORBA; + +import java.io.*; +//import org.omg.CORBA.portable.InputStream; +//import org.omg.CORBA.portable.OutputStream; +//import org.omg.SendingContext.RunTime; +import javax.rmi.CORBA.ValueHandler; + +public class ValueHandlerImpl + implements ValueHandler +{ + + public String getRMIRepositoryID(Class clz) + { + throw new Error("Not implemented for ValueHandler"); + } + + // XXX - Runtime -> RunTime + public Runtime getRunTimeCodeBase() + { + throw new Error("Not implemented for ValueHandler"); + } + + public boolean isCustomMarshaled(Class clz) + { + throw new Error("Not implemented for ValueHandler"); + } + + // XXX - Runtime -> RunTime + public Serializable readValue(InputStream in, int offset, Class clz, String repositoryID, Runtime sender) + { + throw new Error("Not implemented for ValueHandler"); + } + + public Serializable writeReplace(Serializable value) + { + throw new Error("Not implemented for ValueHandler"); + } + + public void writeValue(OutputStream out, Serializable value) + { + throw new Error("Not implemented for ValueHandler"); + } +} diff --git a/libjava/classpath/gnu/javax/rmi/PortableServer.java b/libjava/classpath/gnu/javax/rmi/PortableServer.java new file mode 100644 index 00000000000..4a841387dd7 --- /dev/null +++ b/libjava/classpath/gnu/javax/rmi/PortableServer.java @@ -0,0 +1,141 @@ +/* PortableServer.java -- + Copyright (C) 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.rmi; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.NoSuchObjectException; +import java.rmi.server.UnicastRemoteObject; +import java.rmi.server.RemoteStub; +import java.util.Hashtable; + +import javax.rmi.CORBA.*; + +/** + * The relationship of PortableRemoteObjectImpl with PortableServer + * is like that of UnicastRemoteObject with UnicastServer + */ +public class PortableServer +{ + static private Hashtable tieCache = new Hashtable(); + static private Object NO_TIE = new Object(); + + public static final synchronized void exportObject(Remote obj) + throws RemoteException + { + if(Util.getTie(obj) != null) + return; + + Tie tie = getTieFromRemote(obj); + if (tie != null) + Util.registerTarget(tie, obj); + else + UnicastRemoteObject.exportObject(obj); + } + + public static final void unexportObject(Remote obj) + { + if (Util.getTie(obj) != null) + Util.unexportObject(obj); + if (tieCache.get(obj) != null) //?? + tieCache.remove(obj); + } + + public static final Remote toStub(Remote obj) + throws NoSuchObjectException + { + if (obj instanceof Stub || obj instanceof RemoteStub) + return obj; + + Tie tie = Util.getTie(obj); + Remote stub; + if (tie != null) + stub = getStubFromTie(tie); + else + throw new NoSuchObjectException("Can't toStub an unexported object"); + return stub; + } + + static synchronized Tie getTieFromRemote(Remote obj) + { + Object tie = tieCache.get(obj); + if (tie == null) + { + tie = getTieFromClass(obj.getClass()); + if(tie == null) + tieCache.put(obj, NO_TIE); + else + tieCache.put(obj, tie); + } + else + if(tie != NO_TIE) + { + try + { + tie = obj.getClass().newInstance(); + } + catch(Exception _) + { + tie = null; + } + } + else //NO_TIE + tie = null; + + return (Tie)tie; + } + + static synchronized Tie getTieFromClass(Class clz) + { + //FIX ME + return null; + } + + public static Remote getStubFromTie(Tie tie) + { + //FIX ME + return null; + } + + public static Remote getStubFromObjectImpl(ObjectImpl objimpl, Class toClass) + { + //FIX ME + return null; + } +} diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkBorders.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkBorders.java new file mode 100644 index 00000000000..ebba6a49b2a --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkBorders.java @@ -0,0 +1,83 @@ +/* GtkBorders.java + Copyright (c) 1999 by Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.swing.plaf.gtk; +import java.awt.*; +import javax.swing.*; +import javax.swing.border.*; +import javax.swing.plaf.*; + +/** + * Optional class, can be used to define nifty borders. + * + * @author Brian Jones + * @see javax.swing.LookAndFeel + */ +public class GtkBorders +{ + public static class ButtonBorder extends AbstractBorder + implements UIResource + { + private Border raised; // use by default + private Border lowered; // use this one when pressed + + // creat the border + public ButtonBorder() + { + raised = BorderFactory.createRaisedBevelBorder(); + lowered = BorderFactory.createLoweredBevelBorder(); + } + + // define the insets (in terms of one of the others) + public Insets getBorderInsets(Component c) + { + return raised.getBorderInsets(c); + } + + public void paintBorder(Component c, Graphics g, int x, int y, + int width, int height) + { + AbstractButton b = (AbstractButton)c; + ButtonModel model = b.getModel(); + + if (model.isPressed() && model.isArmed()) + lowered.paintBorder(c, g, x, y, width, height); + else + raised.paintBorder(c, g, x, y, width, height); + } + } +} diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkCheckBoxUI.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkCheckBoxUI.java new file mode 100644 index 00000000000..0395af61bfb --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkCheckBoxUI.java @@ -0,0 +1,69 @@ +/* GtkCheckBoxUI.java + Copyright (c) 1999 by Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.swing.plaf.gtk; + +import java.awt.*; +import javax.swing.*; +import javax.swing.plaf.*; +import javax.swing.plaf.basic.*; + +/** + * + * @author Brian Jones + * @see javax.swing.LookAndFeel + */ +public class GtkCheckBoxUI extends GtkRadioButtonUI +{ + public GtkCheckBoxUI() + { + super(); + } + + public static ComponentUI createUI(JComponent c) + { + return new GtkCheckBoxUI(); + } + + public String getPropertyPrefix() + { + // FIXME + System.err.println(super.getPropertyPrefix()); + return super.getPropertyPrefix(); + } +} + diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkIconFactory.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkIconFactory.java new file mode 100644 index 00000000000..28fd36e897c --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkIconFactory.java @@ -0,0 +1,99 @@ +/* GtkIconFactory.java + Copyright (c) 1999 by Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.swing.plaf.gtk; +import java.awt.*; +import javax.swing.*; +import javax.swing.plaf.*; +import java.io.Serializable; + +/** + * + * @author Brian Jones + * @see javax.swing.LookAndFeel + */ +public class GtkIconFactory implements Serializable +{ + private static Icon radioButtonIcon; + private static Icon checkBoxIcon; + + public static Icon getRadioButtonIcon() + { + if (radioButtonIcon == null) + radioButtonIcon = new RadioButtonIcon(); + return radioButtonIcon; + } + + private static class RadioButtonIcon + implements Icon, UIResource, Serializable + { + private static final int size = 15; + + public int getIconWidth() { return size; } + public int getIconHeight() { return size; } + + public void paintIcon(Component c, Graphics g, int x, int y) + { + System.out.println("radiobuttonicon: paintIcon()"); + // get the button and model containing the state we are + // supposed to show + AbstractButton b = (AbstractButton)c; + ButtonModel model = b.getModel(); + + // If the button is being pressed (& armed), change the + // background color + // Note: could also do something different if the button is + // disabled + + if (model.isPressed() && model.isArmed()) + { + System.out.println("radiobuttonicon: pressed & armed"); + g.setColor(UIManager.getColor("RadioButton.pressed")); + g.fillOval(x,y,size-1, size-1); + } + // draw an outer circle + g.setColor(UIManager.getColor("RadioButton.foreground")); + g.drawOval(x,y,size-1, size-1); + + // fill a small circle inside if the button is selected + if (model.isSelected()) { + g.fillOval(x+4, y+4, size-8, size-8); + System.out.println("radiobuttonicon: is selected"); + } + } + } +} diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkLookAndFeel.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkLookAndFeel.java new file mode 100644 index 00000000000..ed99e6d216b --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkLookAndFeel.java @@ -0,0 +1,241 @@ +/* GtkLookAndFeel.java + Copyright (c) 1999 by Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.swing.plaf.gtk; +import java.awt.*; +import javax.swing.*; +import javax.swing.border.*; +import javax.swing.plaf.*; +import javax.swing.plaf.basic.*; + +/** + * + * @author Brian Jones + * @see javax.swing.LookAndFeel + */ +public class GtkLookAndFeel extends BasicLookAndFeel +{ + private UIDefaults uiDefaults; + + /** + */ + public GtkLookAndFeel() + { + super(); + } + + /** + * A short string to identify this look and feel, for example in a + * drop down list to choose between several look and feels. + */ + public String getName() { return "GIMP Toolkit"; } + + /** + * A much longer description of the look and feel. + */ + public String getDescription() + { + return new String("The GIMP Toolkit Look and Feel for Java, " + + "written by Brian Jones (cbj@gnu.org), " + + "(c) 1999 by Free Software Foundation, Inc. " + + "http://www.classpath.org"); + } + + /** + * Return a unique string identifying this look and feel as different + * from and not a subclass of any other look and feel. Usually, a + * subclass will return the same <code>String</code> here as the + * original look and feel if only a few changes are being made rather + * than something completely new and different. + */ + public String getID() + { + return "Gtk"; + } + + public boolean isNativeLookAndFeel() + { + return false; + } + + public boolean isSupportedLookAndFeel() + { + return true; + } + + protected void initClassDefaults(UIDefaults table) + { + super.initClassDefaults(table); + + String gtkPkgName = "gnu.javax.swing.plaf.gtk."; + + + Object[] defaults = { + "SliderUI", gtkPkgName + "GtkSliderUI" + }; + /* + "CheckBoxUI", gtkPkgName + "GtkCheckBoxUI", + "ButtonUI", gtkPkgName + "GtkButtonUI" + "ColorChooserUI", "MetalColorChooserUI", + "MenuBarUI", "MetalMenuBarUI", + "MenuUI", "MetalMenuUI", + "MenuItemUI", "MetalMenuItemUI", + "CheckBoxMenuItemUI", "MetalCheckBoxMenuItemUI", + "RadioButtonMenuItemUI", "MetalRadioButtonMenuItemUI", + "RadioButtonUI", "MetalRadioButtonUI", + "ToggleButtonUI", "MetalToggleButtonUI", + "PopupMenuUI", "MetalPopupMenuUI", + "ProgressBarUI", "MetalProgressBarUI", + "ScrollBarUI", "MetalScrollBarUI", + "ScrollPaneUI", "MetalScrollPaneUI", + "SplitPaneUI", "MetalSplitPaneUI", + "SeparatorUI", "MetalSeparatorUI", + "ToolBarSeparatorUI", "MetalToolBarSeparatorUI", + "PopupMenuSeparatorUI", "MetalPopupMenuSeparatorUI", + "TabbedPaneUI", "MetalTabbedPaneUI", + "TextAreaUI", "MetalTextAreaUI", + "TextFieldUI", "MetalTextFieldUI", + "PasswordFieldUI", "MetalPasswordFieldUI", + "TextPaneUI", "MetalTextPaneUI", + "EditorPaneUI", "MetalEditorPaneUI", + "TreeUI", "MetalTreeUI", + "LabelUI", "MetalLabelUI", + "ListUI", "MetalListUI", + "ToolBarUI", "MetalToolBarUI", + "ToolTipUI", "MetalToolTipUI", + "ComboBoxUI", "MetalComboBoxUI", + "TableUI", "MetalTableUI", + "TableHeaderUI", "MetalTableHeaderUI", + "InternalFrameUI", "GtkInternalFrameUI", + "StandardDialogUI", "GtkStandardDialogUI", + "DesktopPaneUI", "GtkDesktopPaneUI", + "DesktopIconUI", "GtkDesktopIconUI", + "DirectoryPaneUI", "GtkDirectoryPaneUI", + "FileChooserUI", "GtkFileChooserUI", + "OptionPaneUI", "GtkOptionPaneUI" } + */ + table.putDefaults(defaults); + + } + + protected void initSystemColorDefaults(UIDefaults table) + { + String[] colors = { + "desktop", "#000000", + "activeCaption", "#163555", + "activeCaptionText", "#FFFFFF", + "activeCaptionBorder", "#000000", + "inactiveCaption", "#375676", + "inactiveCaptionText", "#999999", + "inactiveCaptionBorder", "#000000", + "window", "#FFFFFF", + "windowBorder", "#969696", + "windowText", "#000000", + "menu", "#d6d6d6", + "menuText", "#000000", + "text", "#FFFFFF", + "textText", "#000000", + "textHighlight", "#00009c", + "textHighlightText", "#FFFFFF", + "textInactiveText", "#999999", + "control", "#d6d6d6", + "controlText", "#000000", + "controlHighlight", "#eaeaea", + "controlLtHighlight", "#eaeaea", + "controlShadow", "#c3c3c3", + "controlDkShadow", "#888888", + "scrollbar", "#c3c3c3", + "info", "#d6d6d6", + "infoText", "#000000" + }; + + loadSystemColors(table, colors, false); + } + + protected void initComponentDefaults(UIDefaults table) + { + super.initComponentDefaults(table); + + // define common resources + // fonts + FontUIResource sansSerifPlain10 = + new FontUIResource("SansSerif", Font.PLAIN, 10); + FontUIResource serifPlain10 = + new FontUIResource("Serif", Font.PLAIN, 10); + // insets + // borders + // colors + ColorUIResource controlDkShadow = new ColorUIResource(table.getColor("controlDkShadow")); + ColorUIResource controlShadow = new ColorUIResource(table.getColor("controlShadow")); + ColorUIResource control = new ColorUIResource(table.getColor("control")); + ColorUIResource scrollbar = new ColorUIResource(table.getColor("scrollbar")); + ColorUIResource controlHighlight = new ColorUIResource(table.getColor("controlHighlight")); + if (scrollbar == null) + System.out.println("scrollbar is null"); + + ColorUIResource white = new ColorUIResource(Color.white); + ColorUIResource black = new ColorUIResource(Color.black); + ColorUIResource blue = new ColorUIResource(Color.blue); + + // icons + Object errorIcon = LookAndFeel.makeIcon(getClass(), "icons/error.gif"); + // any other resources like dimensions and integer values + + // define defaults + Object[] defaults = + { + "Button.font", sansSerifPlain10, + "CheckBox.font", sansSerifPlain10, + "RadioButton.pressed", black, + "Slider.focus", blue, + "Slider.foreground", control, + "Slider.highlight", controlHighlight, + "Slider.shadow", controlShadow, + "Slider.background", controlDkShadow + +// "Slider.background", "#888888", +// "Slider.focus", "#c3c3c3", +// "Slider.foreground", "#d6d6d6", +// "Slider.highlight", "#ffffff", +// "Slider.shadow", "#000000" + + + }; + + table.putDefaults(defaults); + } +} diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkRadioButtonUI.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkRadioButtonUI.java new file mode 100644 index 00000000000..19d53387910 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkRadioButtonUI.java @@ -0,0 +1,69 @@ +/* GtkRadioButtonUI.java + Copyright (c) 1999 by Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.swing.plaf.gtk; + +import java.awt.*; +import javax.swing.*; +import javax.swing.plaf.*; +import javax.swing.plaf.basic.*; + +/** + * + * @author Brian Jones + * @see javax.swing.LookAndFeel + */ +public class GtkRadioButtonUI extends BasicRadioButtonUI +{ + public GtkRadioButtonUI() + { + super(); + } + + public static ComponentUI createUI(JComponent c) + { + return new GtkRadioButtonUI(); + } + + public String getPropertyPrefix() + { + // FIXME + System.err.println(super.getPropertyPrefix()); + return super.getPropertyPrefix(); + } +} + diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkSliderUI.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkSliderUI.java new file mode 100644 index 00000000000..c576b3d4b50 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/GtkSliderUI.java @@ -0,0 +1,230 @@ +/* GtkSliderUI.java + Copyright (c) 1999 by Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.swing.plaf.gtk; +import java.awt.*; +import javax.swing.*; +import javax.swing.plaf.*; +import javax.swing.plaf.basic.*; + +/** + * Gtk-like slider + * + * @author Brian Jones + * @see javax.swing.LookAndFeel + */ +public class GtkSliderUI extends BasicSliderUI +{ + private static Color thumbFgColor; + private static Color thumbBgColor; + private static Color thumbHighlight; + private static Color thumbFocus; + + private static Color bgColor; + private static Color fgColor; + private static Color focusColor; + private static Color highlight; + private static Color shadow; + + private static final Dimension PREF_HORIZ = new Dimension(250, 15); + private static final Dimension PREF_VERT = new Dimension(15, 250); + private static final Dimension MIN_HORIZ = new Dimension(25, 15); + private static final Dimension MIN_VERT = new Dimension(15, 25); + + public GtkSliderUI() + { + super(null); + bgColor = UIManager.getColor("Slider.background"); + fgColor = UIManager.getColor("Slider.foreground"); + focusColor = UIManager.getColor("Slider.focus"); + highlight = UIManager.getColor("Slider.highlight"); + shadow = UIManager.getColor("Slider.shadow"); + + System.out.println("bgColor: " + bgColor); + System.out.println("fgColor: " + fgColor); + System.out.println("focusColor: " + focusColor); + System.out.println("highlight: " + highlight); + System.out.println("shadow: " + shadow); + } + + public static ComponentUI createUI(JComponent c) + { + return new GtkSliderUI(); + } + + // methods not overridden here, using Basic defaults + // installUI() + // uninstall() + + public Dimension getPreferredHorizontalSize() + { + /* + Dimension thumbSize = getThumbSize(); + Dimenstion labelSize = getLabelSize(); + // getTickLength() + int width = thumbSize.width + + getWidthOfWidestLabel + */ + return PREF_HORIZ; + } + + public Dimension getPreferredVerticalSize() + { + return PREF_VERT; + } + + public Dimension getMinimumHorizontalSize() + { + return MIN_HORIZ; + } + + public Dimension getMinimumVerticalSize() + { + return MIN_VERT; + } + + /** + * Returns thumb size based on slider orientation + */ + protected Dimension getThumbSize() + { + Dimension size = new Dimension(); + + if (slider.getOrientation() == JSlider.VERTICAL) { + size.width = 15; + size.height = 33; + } + else { + size.width = 33; + size.height = 15; + } + return size; + } + + /** + * Reserved width or height for ticks, as appropriate to the slider + * orientation. + */ + protected int getTickLength() + { + return 10; + } + + public void paintFocus(Graphics g) + { + super.paintFocus(g); + System.err.println("focus " + focusRect); + } + + /** + * Must account for Unicode when drawing text. + */ + public void paintLabels(Graphics g) + { + super.paintLabels(g); + System.err.println("label " + labelRect); + } + + /** + * A drawRect() generated slider has ghosting when moving left on + * a horizontal slider and the bottom is not painted when moving + * right. + */ + public void paintThumb(Graphics g) + { + int x = thumbRect.x; + int y = thumbRect.y; + int h = thumbRect.height; + int w = thumbRect.width; + +// "Slider.background", "#888888", +// "Slider.focus", "#c3c3c3", +// "Slider.foreground", "#d6d6d6", +// "Slider.highlight", "#ffffff", +// "Slider.shadow", "#000000" + + g.setColor(fgColor); + g.fillRect(x,y,w,h); + g.setColor(bgColor); + + if (slider.getOrientation() == JSlider.HORIZONTAL) { + g.drawRect(x, y, w, h); + g.setColor(highlight); + g.drawLine(x+1, y+h-1, x+w, y+h-1); + g.setColor(focusColor); + g.drawLine(x+2, y+h-2, x+w, y+h-2); + g.setColor(Color.black); + g.drawLine(x+1, y+h-2, x+1, y+h-2); + g.drawRect(x+1, y+1, w-1, 12); + } + else + g.drawRect(x, y, w, h); + + System.err.println("thumb " + thumbRect); + } + + // public void paintTicks(Graphics g) + + public void paintTrack(Graphics g) + { +// super.paintTrack(g); + int x = trackRect.x; + int y = trackRect.y; + int h = trackRect.height; + int w = trackRect.width; + + System.err.println("track " + trackRect); + + g.setColor(Color.black); + g.fillRect(x,y,w,h); + +// if (slider.getOrientation() == JSlider.HORIZONTAL) +// g.drawLine(x, y+h-1, x+w-1, y+h-1); +// else +// g.drawLine(x+w-1, y, x+w-1, y+h-1); + +// System.err.println("track " + trackRect); +// System.err.println("content " + contentRect); + } + + // the four methods below allow you to control tick painting without + // worrying about what paintTicks does, look for in other UI delegates + // protected void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) + // protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) + // protected void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x) + // protected void paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) +} diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/README b/libjava/classpath/gnu/javax/swing/plaf/gtk/README new file mode 100644 index 00000000000..2b3f001ddc2 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/README @@ -0,0 +1,37 @@ +This is a start at a GTK look and feel for Java. +It is usable already, but it mainly just defaults back to Basic Look and +Feel for everything. + +Sliders are currently broken. I haven't figured out why yet. + +A bunch of system colors defined in GtkLookandFeel though I have a feeling +I'll be defining more ColorUIResources shortly. + +Based on Gnome File Manager colors... or my Window Manager setup + +desktop #000000 (gtk) +activeCaption #163555 (window manager) +activeCaptionText #FFFFFF (window manager) +activeCaptionBorder #000000 (unsure of this) +inactiveCaption #375676 (window manager) +inactiveCaptionText #999999 (window manager) +inactiveCaptionBorder #000000 (unsure of this) +window #FFFFFF (gtk) +windowBorder #969696 (gtk) +windowText #000000 (gtk) +menu #d6d6d6 (gtk) +menuText #000000 (gtk) +text #FFFFFF (gtk) +textText #000000 (gtk) +textHighlight #00009c (gtk) +textHighlightText #FFFFFF (gtk) +textInactiveText #999999 (unsure of this) +control #d6d6d6 (gtk) +controlText #000000 (gtk) +controlHighlight #eaeaea (gtk) +controlLtHighlight #eaeaea (unsure of this) +controlShadow #c3c3c3 (gtk) +controlDkShadow #888888 (unsure of this) +scrollbar #c3c3c3 (gtk) +info #d6d6d6 (gtk) +infoText #000000 (gtk) diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/SliderTest.java b/libjava/classpath/gnu/javax/swing/plaf/gtk/SliderTest.java new file mode 100644 index 00000000000..a838444924d --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/SliderTest.java @@ -0,0 +1,82 @@ +import javax.swing.*; +import javax.swing.event.*; +import java.awt.*; +import java.awt.event.*; +import gnu.javax.swing.plaf.gtk.*; + +public class SliderTest extends JFrame +{ + public SliderTest() + { + super("JSlider Test"); + Container c = getContentPane(); + c.setLayout(new BorderLayout()); + this.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { System.exit(0); } + }); + + JSlider s = new JSlider(); + s.createStandardLabels(10); + s.setMinorTickSpacing(10); + s.setMajorTickSpacing(20); + s.setPaintTicks(true); + s.setPaintTrack(true); + s.setPaintLabels(true); + s.setRequestFocusEnabled(true); + + // turning off double buffering in repaint manager + // in order to use debug graphics + RepaintManager repaintManager = RepaintManager.currentManager(s); + repaintManager.setDoubleBufferingEnabled(false); + + s.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION | DebugGraphics.FLASH_OPTION); + DebugGraphics.setFlashColor(Color.red); // color of flash + DebugGraphics.setFlashTime(4); // time delay of drawing operation flashing + DebugGraphics.setFlashCount(3); // number of time to draw + + this.setSize(250, 100); + c.add(new JLabel("Default Slider"), "North"); + c.add(s, "Center"); + + try { + UIManager.setLookAndFeel("gnu.javax.swing.plaf.gtk.GtkLookAndFeel"); + SwingUtilities.updateComponentTreeUI(this); + } catch (Exception e) { + e.printStackTrace(); + System.exit(0); + } + + center(); + } + + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + + public void center() + { + // Centering the frame + Toolkit t = this.getToolkit(); + Dimension framesize = this.getSize(); + Dimension screensize = t.getScreenSize(); + + // Calculate point for frame (main) + Point pframe = new Point(); + pframe.x = (screensize.width - framesize.width) / 2; + pframe.y = (screensize.height - framesize.height) / 2; + + // Set the location of each to be centered + this.setLocation(pframe); + } + + public static void main(String [] argv) + { + SliderTest t = new SliderTest(); + t.show(); + } + +} + + + + diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Error.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Error.png Binary files differnew file mode 100644 index 00000000000..9d6f1227892 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Error.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Inform.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Inform.png Binary files differnew file mode 100644 index 00000000000..89931d54d61 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Inform.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/JavaCup.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/JavaCup.png Binary files differnew file mode 100644 index 00000000000..d2cff625862 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/JavaCup.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/JavaCupLarge.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/JavaCupLarge.png Binary files differnew file mode 100644 index 00000000000..6a4f7923a2e --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/JavaCupLarge.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Question.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Question.png Binary files differnew file mode 100644 index 00000000000..89931d54d61 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Question.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/README b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/README new file mode 100644 index 00000000000..755f3182043 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/README @@ -0,0 +1,20 @@ +Images required by Basic Look and Feel + +Information based on images from Swing 1.1.1b2 +JavaCup.gif - a picture of the steaming cup of Java - 16x16 - 3 colors + +TreeOpen.gif - a picture of an open file folder - 16x13 - 8 colors +TreeClosed.gif - a picture of a closed file folder - 16x13 - 7 colors +TreeLeaf.gif - a picture of a small circle with points in four + directions - 16x13 - 2 colors + +Information on images used by Gtk Look and Feel, really need to work the +number of colors down I think. + +JavaCup.gif - a picture of Classpath's mascot - 16x16 - 58 colors + +TreeOpen.gif - taken from Gnome File Manager - 16x12 - 34 colors +TreeClosed.gif - taken from Gnome File Manager 14x12 - 28 colors +TreeLeaf.gif - a blank gif - 16x12 - 1 color + +Error.gif - think this will be needed later (taken from gnome) diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeClosed.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeClosed.png Binary files differnew file mode 100644 index 00000000000..e2edbf78e4f --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeClosed.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeLeaf-normal.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeLeaf-normal.png Binary files differnew file mode 100644 index 00000000000..fb8dbc98241 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeLeaf-normal.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeLeaf.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeLeaf.png Binary files differnew file mode 100644 index 00000000000..cdf058fe66c --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeLeaf.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeOpen.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeOpen.png Binary files differnew file mode 100644 index 00000000000..fba82587a22 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/TreeOpen.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Warn.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Warn.png Binary files differnew file mode 100644 index 00000000000..9d6f1227892 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/Warn.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/file-folders.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/file-folders.png Binary files differnew file mode 100644 index 00000000000..e1a03d0c916 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/file-folders.png diff --git a/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/slider.png b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/slider.png Binary files differnew file mode 100644 index 00000000000..ac429b15875 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/plaf/gtk/icons/slider.png diff --git a/libjava/classpath/gnu/javax/swing/text/html/package.html b/libjava/classpath/gnu/javax/swing/text/html/package.html new file mode 100644 index 00000000000..c7e7744282c --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/package.html @@ -0,0 +1,50 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in javax.swing.text.html package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - javax.swing.text.html</title></head> + +<body> +<p> Provides supporting classes for web browsers, + web robots, web page content analysers, web editors and + other applications applications working with Hypertext + Markup Language (HTML). +</p> + +</body> +</html> diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/HTML_401F.java b/libjava/classpath/gnu/javax/swing/text/html/parser/HTML_401F.java new file mode 100644 index 00000000000..1ed42a2ab98 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/HTML_401F.java @@ -0,0 +1,3729 @@ +/* HTML_401F.java -- HTML 4.01 FRAMESET DTD java conception. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser; + +import gnu.javax.swing.text.html.parser.models.PCDATAonly_model; +import gnu.javax.swing.text.html.parser.models.TableRowContentModel; +import gnu.javax.swing.text.html.parser.models.noTagModel; + +import java.io.IOException; +import java.io.Serializable; + +import javax.swing.text.html.parser.*; +import javax.swing.text.html.parser.ContentModel; +import javax.swing.text.html.parser.DTDConstants; + +/** + * This class represents the java implementation of the HTML 4.01 + * ( -//W3C//DTD HTML 4.01 Frameset//EN ) Frameset version. The + * Frameset version includes as recommended, as obsoleted features and + * also the frameset support. This the default DTD to parse HTML + * documents in this implementation, containing 315 pre-defined general + * entities and 92 elements. + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class HTML_401F + extends gnuDTD + implements DTDConstants, Serializable +{ + private static final long serialVersionUID = 1; + + /** + * The standard name of this DTD, + * '-//W3C//DTD HTML 4.01 Frameset//EN' + */ + public static final String DTD_NAME = "-//W3C//DTD HTML 4.01 Frameset//EN"; + + /** + * The integer representing length in pixels. + */ + static final int PIXELS = NUMBER; + + static final String[] NONE = new String[0]; + + /* Define the HTML tags. */ + static final String PCDATA = "#pcdata"; + static final String A = "a"; + static final String ABBR = "abbr"; + static final String ACRONYM = "acronym"; + static final String ADDRESS = "address"; + static final String APPLET = "applet"; + static final String AREA = "area"; + static final String B = "b"; + static final String BASE = "base"; + static final String BASEFONT = "basefont"; + static final String BDO = "bdo"; + static final String BIG = "big"; + static final String BLOCKQUOTE = "blockquote"; + static final String BODY = "body"; + static final String BR = "br"; + static final String BUTTON = "button"; + static final String CAPTION = "caption"; + static final String CENTER = "center"; + static final String CITE = "cite"; + static final String CODE = "code"; + static final String COL = "col"; + static final String COLGROUP = "colgroup"; + static final String DEFAULTS = "default"; + static final String DD = "dd"; + static final String DEL = "del"; + static final String DFN = "dfn"; + static final String DIR = "dir"; + static final String DIV = "div"; + static final String DL = "dl"; + static final String DT = "dt"; + static final String EM = "em"; + static final String FIELDSET = "fieldset"; + static final String FONT = "font"; + static final String FORM = "form"; + static final String FRAME = "frame"; + static final String FRAMESET = "frameset"; + static final String H1 = "h1"; + static final String H2 = "h2"; + static final String H3 = "h3"; + static final String H4 = "h4"; + static final String H5 = "h5"; + static final String H6 = "h6"; + static final String HEAD = "head"; + static final String HR = "hr"; + static final String HTML = "html"; + static final String I = "i"; + static final String IFRAME = "iframe"; + static final String IMG = "img"; + static final String INPUT = "input"; + static final String INS = "ins"; + static final String ISINDEX = "isindex"; + static final String KBD = "kbd"; + static final String LABEL = "label"; + static final String LEGEND = "legend"; + static final String LI = "li"; + static final String LINK = "link"; + static final String MAP = "map"; + static final String MENU = "menu"; + static final String META = "meta"; + static final String NOFRAMES = "noframes"; + static final String NOSCRIPT = "noscript"; + static final String NONES = "none"; + static final String sNAME = "name"; + static final String OBJECT = "object"; + static final String OL = "ol"; + static final String OPTGROUP = "optgroup"; + static final String OPTION = "option"; + static final String P = "p"; + static final String PARAM = "param"; + static final String PRE = "pre"; + static final String Q = "q"; + static final String S = "s"; + static final String SAMP = "samp"; + static final String SCRIPT = "script"; + static final String SELECT = "select"; + static final String SMALL = "small"; + static final String SPAN = "span"; + static final String STRIKE = "strike"; + static final String STRONG = "strong"; + static final String STYLE = "style"; + static final String SUB = "sub"; + static final String SUP = "sup"; + static final String TABLE = "table"; + static final String TBODY = "tbody"; + static final String TD = "td"; + static final String TEXTAREA = "textarea"; + static final String TFOOT = "tfoot"; + static final String TH = "th"; + static final String THEAD = "thead"; + static final String TITLE = "title"; + static final String TR = "tr"; + static final String TT = "tt"; + static final String U = "u"; + static final String UL = "ul"; + static final String VAR = "var"; + + /* Define the attribute constants. */ + static final String C_0 = "0"; + static final String C_1 = "1"; + static final String CHECKBOX = "checkbox"; + static final String DATA = "data"; + static final String FILE = "file"; + static final String GET = "get"; + static final String HIDDEN = "hidden"; + static final String IMAGE = "image"; + static final String PASSWORD = "password"; + static final String POST = "post"; + static final String RADIO = "radio"; + static final String REF = "ref"; + static final String RESET = "reset"; + static final String SUBMIT = "submit"; + static final String TEXT = "text"; + static final String ABOVE = "above"; + static final String ACCEPT = "accept"; + static final String ACCEPTCHARSET = "accept-charset"; + static final String ACCESSKEY = "accesskey"; + static final String ACTION = "action"; + static final String ALIGN = "align"; + static final String ALINK = "alink"; + static final String ALL = "all"; + static final String ALT = "alt"; + static final String APPLICATION_X_WWW_FORM_URLENCODED + = "application/x-www-form-urlencoded"; + static final String ARCHIVE = "archive"; + static final String AUTO = "auto"; + static final String AXIS = "axis"; + static final String BACKGROUND = "background"; + static final String BASELINE = "baseline"; + static final String BELOW = "below"; + static final String BGCOLOR = "bgcolor"; + static final String BORDER = "border"; + static final String BOTTOM = "bottom"; + static final String BOX = "box"; + static final String CELLPADDING = "cellpadding"; + static final String CELLSPACING = "cellspacing"; + static final String CHAR = "char"; + static final String CHAROFF = "charoff"; + static final String CHARSET = "charset"; + static final String CHECKED = "checked"; + static final String CIRCLE = "circle"; + static final String CLASS = "class"; + static final String CLASSID = "classid"; + static final String CLEAR = "clear"; + static final String CODEBASE = "codebase"; + static final String CODETYPE = "codetype"; + static final String COLOR = "color"; + static final String COLS = "cols"; + static final String COLSPAN = "colspan"; + static final String COMPACT = "compact"; + static final String CONTENT = "content"; + static final String COORDS = "coords"; + static final String DATAPAGESIZE = "datapagesize"; + static final String DATETIME = "datetime"; + static final String DECLARE = "declare"; + static final String DEFER = "defer"; + static final String DISABLED = "disabled"; + static final String DISC = "disc"; + static final String ENCTYPE = "enctype"; + static final String EVENT = "event"; + static final String FACE = "face"; + static final String FOR = "for"; + static final String FRAMEBORDER = "frameborder"; + static final String GROUPS = "groups"; + static final String HEADERS = "headers"; + static final String HEIGHT = "height"; + static final String HREF = "href"; + static final String HREFLANG = "hreflang"; + static final String HSIDES = "hsides"; + static final String HSPACE = "hspace"; + static final String HTTPEQUIV = "http-equiv"; + static final String sID = "id"; + static final String ISMAP = "ismap"; + static final String JUSTIFY = "justify"; + static final String LANG = "lang"; + static final String LANGUAGE = "language"; + static final String LEFT = "left"; + static final String LHS = "lhs"; + static final String LONGDESC = "longdesc"; + static final String LTR = "ltr"; + static final String MARGINHEIGHT = "marginheight"; + static final String MARGINWIDTH = "marginwidth"; + static final String MAXLENGTH = "maxlength"; + static final String MEDIA = "media"; + static final String METHOD = "method"; + static final String MIDDLE = "middle"; + static final String MULTIPLE = "multiple"; + static final String NO = "no"; + static final String NOHREF = "nohref"; + static final String NORESIZE = "noresize"; + static final String NOSHADE = "noshade"; + static final String NOWRAP = "nowrap"; + static final String ONBLUR = "onblur"; + static final String ONCHANGE = "onchange"; + static final String ONCLICK = "onclick"; + static final String ONDBLCLICK = "ondblclick"; + static final String ONFOCUS = "onfocus"; + static final String ONKEYDOWN = "onkeydown"; + static final String ONKEYPRESS = "onkeypress"; + static final String ONKEYUP = "onkeyup"; + static final String ONLOAD = "onload"; + static final String ONMOUSEDOWN = "onmousedown"; + static final String ONMOUSEMOVE = "onmousemove"; + static final String ONMOUSEOUT = "onmouseout"; + static final String ONMOUSEOVER = "onmouseover"; + static final String ONMOUSEUP = "onmouseup"; + static final String ONRESET = "onreset"; + static final String ONSELECT = "onselect"; + static final String ONSUBMIT = "onsubmit"; + static final String ONUNLOAD = "onunload"; + static final String POLY = "poly"; + static final String PROFILE = "profile"; + static final String PROMPT = "prompt"; + static final String READONLY = "readonly"; + static final String RECT = "rect"; + static final String REL = "rel"; + static final String REV = "rev"; + static final String RHS = "rhs"; + static final String RIGHT = "right"; + static final String ROW = "row"; + static final String ROWGROUP = "rowgroup"; + static final String ROWS = "rows"; + static final String ROWSPAN = "rowspan"; + static final String RTL = "rtl"; + static final String RULES = "rules"; + static final String SCHEME = "scheme"; + static final String SCOPE = "scope"; + static final String SCROLLING = "scrolling"; + static final String SELECTED = "selected"; + static final String SHAPE = "shape"; + static final String SIZE = "size"; + static final String SQUARE = "square"; + static final String SRC = "src"; + static final String STANDBY = "standby"; + static final String START = "start"; + static final String SUMMARY = "summary"; + static final String TABINDEX = "tabindex"; + static final String TARGET = "target"; + static final String TOP = "top"; + static final String TYPE = "type"; + static final String USEMAP = "usemap"; + static final String VALIGN = "valign"; + static final String VALUE = "value"; + static final String VALUETYPE = "valuetype"; + static final String VERSION = "version"; + static final String VLINK = "vlink"; + static final String VOID = "void"; + static final String VSIDES = "vsides"; + static final String VSPACE = "vspace"; + static final String WIDTH = "width"; + static final String YES = "yes"; + + static final String[] BLOCK = + new String[] { + ADDRESS, BLOCKQUOTE, CENTER, DIR, + DIV, DL, FIELDSET, FORM, + H1, H2, H3, H4, H5, H6, + HR, ISINDEX, MENU, NOFRAMES, NOSCRIPT, + OL, P, PRE, TABLE, UL + }; + + /** + * Creates this DTD, filling in the entities and attributes data + * as defined in -//W3C//DTD HTML 4.01 Frameset//EN. + */ + protected HTML_401F() + { + super(DTD_NAME); + defineEntities(); + defineElements(); + } + + /** + * Either takes the document (by name) from DTD table, or + * creates a new instance and registers it in the tabe. + * The document is registerd under name "-//W3C//DTD HTML 4.01 Frameset//EN". + * @return The new or existing DTD for parsing HTML 4.01 Frameset. + */ + public static DTD getInstance() + { + try + { + DTD dtd = getDTD(DTD_NAME); + if (dtd == null || dtd.getClass().equals(DTD.class)) + { + dtd = new HTML_401F(); + putDTDHash(DTD_NAME, dtd); + } + return dtd; + } + catch (IOException ex) + { + throw new Error("This should never happen. Report the bug.", ex); + } + } + + /** + * Define all elements of this DTD. + */ + protected void defineElements() + { + /* Define the elements. */ + defElement(PCDATA, 0, false, false, null, NONE, NONE, + new AttributeList[ 0 ]); + + defElement(A, 0, false, false, null, + new String[] { + A + } + , + new String[] { + PCDATA, ABBR, ACRONYM, APPLET, + B, BASEFONT, BDO, BIG, BR, + BUTTON, CITE, CODE, DFN, EM, + FONT, I, IFRAME, IMG, INPUT, + KBD, LABEL, MAP, OBJECT, Q, + S, SAMP, SCRIPT, SELECT, SMALL, + SPAN, STRIKE, STRONG, SUB, SUP, + TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(CHARSET, null, null, 0, IMPLIED), + attr(TYPE, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(HREF, null, null, 0, IMPLIED), + attr(HREFLANG, null, null, 0, IMPLIED), + attr(TARGET, null, null, 0, IMPLIED), + attr(REL, null, null, 0, IMPLIED), + attr(REV, null, null, 0, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(SHAPE, RECT, new String[] { RECT, CIRCLE, POLY, DEFAULTS }, + 0, DEFAULT), + attr(COORDS, null, null, 0, IMPLIED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED) + } + ); + defElement(ABBR, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(ACRONYM, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(ADDRESS, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + P + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(APPLET, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL, PARAM + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(CODEBASE, null, null, 0, IMPLIED), + attr(ARCHIVE, null, null, 0, IMPLIED), + attr(CODE, null, null, 0, IMPLIED), + attr(OBJECT, null, null, 0, IMPLIED), + attr(ALT, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, REQUIRED), + attr(HEIGHT, null, null, 0, REQUIRED), + attr(ALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED), + attr(HSPACE, null, null, 0, IMPLIED), + attr(VSPACE, null, null, 0, IMPLIED) + } + ); + defElement(AREA, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(SHAPE, RECT, new String[] { RECT, CIRCLE, POLY, DEFAULTS }, + 0, DEFAULT), + attr(COORDS, null, null, 0, IMPLIED), + attr(HREF, null, null, 0, IMPLIED), + attr(TARGET, null, null, 0, IMPLIED), + attr(NOHREF, null, new String[] { NOHREF }, 0, IMPLIED), + attr(ALT, null, null, 0, REQUIRED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED) + } + ); + defElement(B, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(BASE, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(HREF, null, null, 0, IMPLIED), + attr(TARGET, null, null, 0, IMPLIED) + } + ); + defElement(BASEFONT, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(SIZE, null, null, 0, REQUIRED), + attr(COLOR, null, null, 0, IMPLIED), + attr(FACE, null, null, 0, IMPLIED) + } + ); + defElement(BDO, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, REQUIRED) + } + ); + defElement(BIG, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(BLOCKQUOTE, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(CITE, null, null, 0, IMPLIED) + } + ); + defElement(BODY, 0, true, true, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DEL, DIR, + DIV, DL, FIELDSET, FORM, H1, + H2, H3, H4, H5, H6, + HR, INS, ISINDEX, MENU, NOFRAMES, + NOSCRIPT, OL, P, PRE, TABLE, + UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ONLOAD, null, null, 0, IMPLIED), + attr(ONUNLOAD, null, null, 0, IMPLIED), + attr(BACKGROUND, null, null, 0, IMPLIED), + attr(BGCOLOR, null, null, 0, IMPLIED), + attr(TEXT, null, null, 0, IMPLIED), + attr(LINK, null, null, 0, IMPLIED), + attr(VLINK, null, null, 0, IMPLIED), + attr(ALINK, null, null, 0, IMPLIED) + } + ); + defElement(BR, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(CLEAR, "NONE", new String[] { LEFT, ALL, RIGHT, NONES }, + 0, DEFAULT) + } + ); + defElement(BUTTON, 0, false, false, null, + new String[] { + A, BUTTON, IFRAME, INPUT, + LABEL, SELECT, TEXTAREA, FIELDSET, FORM, + ISINDEX + } + , + new String[] { + PCDATA, ABBR, ACRONYM, APPLET, + B, BASEFONT, BDO, BIG, BR, + CITE, CODE, DFN, EM, FONT, + I, IMG, KBD, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SMALL, + SPAN, STRIKE, STRONG, SUB, SUP, + TT, U, VAR, ADDRESS, BLOCKQUOTE, + CENTER, DIR, DIV, DL, H1, + H2, H3, H4, H5, H6, + HR, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(VALUE, null, null, 0, IMPLIED), + attr(TYPE, SUBMIT, new String[] { BUTTON, SUBMIT, RESET }, 0, DEFAULT), + attr(DISABLED, null, new String[] { DISABLED }, 0, IMPLIED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED) + } + ); + defElement(CAPTION, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { TOP, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED) + } + ); + defElement(CENTER, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(CITE, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(CODE, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(COL, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(SPAN, C_1, null, NUMBER, DEFAULT), + attr(WIDTH, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED) + } + ); + defElement(COLGROUP, 0, false, true, null, + NONE + , + new String[] { + COL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(SPAN, C_1, null, NUMBER, DEFAULT), + attr(WIDTH, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED) + } + ); + defElement(DD, 0, false, true, new ContentModel(0, + new noTagModel( new String[] { DD, DT } ), null ), + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(DEL, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(CITE, null, null, 0, IMPLIED), + attr(DATETIME, null, null, 0, IMPLIED) + } + ); + defElement(DFN, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(DIR, 0, false, false, createListModel(), + new String[] { + ADDRESS, BLOCKQUOTE, CENTER, DIR, + DIV, DL, FIELDSET, FORM, H1, + H2, H3, H4, H5, H6, + HR, ISINDEX, MENU, NOFRAMES, NOSCRIPT, + OL, P, PRE, TABLE, UL + } + , + new String[] { + LI, UL, OL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(COMPACT, null, new String[] { COMPACT }, 0, IMPLIED) + } + ); + defElement(DIV, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(DL, 0, false, false, createDefListModel(), + NONE + , + new String[] { + DD, DT + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(COMPACT, null, new String[] { COMPACT }, 0, IMPLIED) + } + ); + defElement(DT, 0, false, true, + new ContentModel(0, + new noTagModel( new String[] { DT, DD } ), null), + BLOCK + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(EM, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(FIELDSET, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL, LEGEND + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(FONT, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(SIZE, null, null, 0, IMPLIED), + attr(COLOR, null, null, 0, IMPLIED), + attr(FACE, null, null, 0, IMPLIED) + } + ); + defElement(FORM, 0, false, false, null, + new String[] { + FORM + } + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, H1, H2, H3, + H4, H5, H6, HR, ISINDEX, + MENU, NOFRAMES, NOSCRIPT, OL, P, + PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ACTION, null, null, 0, REQUIRED), + attr(METHOD, GET, new String[] { GET, POST }, 0, DEFAULT), + attr(ENCTYPE, APPLICATION_X_WWW_FORM_URLENCODED, null, 0, DEFAULT), + attr(ACCEPT, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(ONSUBMIT, null, null, 0, IMPLIED), + attr(ONRESET, null, null, 0, IMPLIED), + attr(TARGET, null, null, 0, IMPLIED), + attr(ACCEPTCHARSET, null, null, 0, IMPLIED) + } + ); + defElement(FRAME, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LONGDESC, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(SRC, null, null, 0, IMPLIED), + attr(FRAMEBORDER, C_1, new String[] { C_1, C_0 }, 0, DEFAULT), + attr(MARGINWIDTH, null, null, PIXELS, IMPLIED), + attr(MARGINHEIGHT, null, null, PIXELS, IMPLIED), + attr(NORESIZE, null, new String[] { NORESIZE }, 0, IMPLIED), + attr(SCROLLING, AUTO, new String[] { YES, NO, AUTO }, 0, DEFAULT) + } + ); + defElement(FRAMESET, 0, false, false, null, + NONE + , + new String[] { + NOFRAMES, FRAME, FRAMESET + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(ROWS, null, null, 0, IMPLIED), + attr(COLS, null, null, 0, IMPLIED), + attr(ONLOAD, null, null, 0, IMPLIED), + attr(ONUNLOAD, null, null, 0, IMPLIED) + } + ); + defElement(H1, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(H2, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(H3, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(H4, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(H5, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(H6, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(HEAD, 0, true, true, null, + new String[] { + BODY + } + , + new String[] { + TITLE, ISINDEX, BASE, + SCRIPT, STYLE, META, LINK, OBJECT + } + , + new AttributeList[] { + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(PROFILE, null, null, 0, IMPLIED) + } + ); + + defElement(HR, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT }, 0, IMPLIED), + attr(NOSHADE, null, new String[] { NOSHADE }, 0, IMPLIED), + attr(SIZE, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED) + } + ); + defElement(HTML, 0, true, true, createHtmlContentModel(), + NONE + , + new String[] { + HEAD, BODY + } + , + new AttributeList[] { + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(VERSION, DTD_NAME, null, 0, FIXED) + } + ); + defElement(I, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(IFRAME, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LONGDESC, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(SRC, null, null, 0, IMPLIED), + attr(FRAMEBORDER, C_1, new String[] { C_1, C_0 }, 0, DEFAULT), + attr(MARGINWIDTH, null, null, PIXELS, IMPLIED), + attr(MARGINHEIGHT, null, null, PIXELS, IMPLIED), + attr(SCROLLING, AUTO, new String[] { YES, NO, AUTO }, 0, DEFAULT), + attr(ALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED), + attr(HEIGHT, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED) + } + ); + defElement(IMG, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(SRC, null, null, 0, REQUIRED), + attr(ALT, null, null, 0, REQUIRED), + attr(LONGDESC, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(HEIGHT, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED), + attr(USEMAP, null, null, 0, IMPLIED), + attr(ISMAP, null, new String[] { ISMAP }, 0, IMPLIED), + attr(ALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED), + attr(BORDER, null, null, PIXELS, IMPLIED), + attr(HSPACE, null, null, 0, IMPLIED), + attr(VSPACE, null, null, 0, IMPLIED) + } + ); + defElement(INPUT, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(TYPE, TEXT, new String[] { TEXT, PASSWORD, CHECKBOX, RADIO, + SUBMIT, RESET, FILE, HIDDEN, IMAGE, BUTTON }, 0, DEFAULT), + attr(sNAME, null, null, 0, IMPLIED), + attr(VALUE, null, null, 0, IMPLIED), + attr(CHECKED, null, new String[] { CHECKED }, 0, IMPLIED), + attr(DISABLED, null, new String[] { DISABLED }, 0, IMPLIED), + attr(READONLY, null, new String[] { READONLY }, 0, IMPLIED), + attr(SIZE, null, null, 0, IMPLIED), + attr(MAXLENGTH, null, null, 0, IMPLIED), + attr(SRC, null, null, 0, IMPLIED), + attr(ALT, null, null, 0, IMPLIED), + attr(USEMAP, null, null, 0, IMPLIED), + attr(ISMAP, null, new String[] { ISMAP }, 0, IMPLIED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED), + attr(ONSELECT, null, null, 0, IMPLIED), + attr(ONCHANGE, null, null, 0, IMPLIED), + attr(ACCEPT, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED) + } + ); + defElement(INS, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(CITE, null, null, 0, IMPLIED), + attr(DATETIME, null, null, 0, IMPLIED) + } + ); + defElement(ISINDEX, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(PROMPT, null, null, 0, IMPLIED) + } + ); + defElement(KBD, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(LABEL, 0, false, false, null, + new String[] { + LABEL + } + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, MAP, OBJECT, Q, + S, SAMP, SCRIPT, SELECT, SMALL, + SPAN, STRIKE, STRONG, SUB, SUP, + TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(FOR, null, null, 0, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED) + } + ); + defElement(LEGEND, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { TOP, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED) + } + ); + // LI has a special content model that will be resolved into + // by transformer. + defElement(LI, 0, false, true, + new ContentModel(0, + new noTagModel(LI), null), + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(TYPE, null, null, 0, IMPLIED), + attr(VALUE, null, null, NUMBER, IMPLIED) + } + ); + defElement(LINK, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(CHARSET, null, null, 0, IMPLIED), + attr(HREF, null, null, 0, IMPLIED), + attr(HREFLANG, null, null, 0, IMPLIED), + attr(TYPE, null, null, 0, IMPLIED), + attr(REL, null, null, 0, IMPLIED), + attr(REV, null, null, 0, IMPLIED), + attr(MEDIA, null, null, 0, IMPLIED), + attr(TARGET, null, null, 0, IMPLIED) + } + ); + defElement(MAP, 0, false, false, null, + NONE + , + new String[] { + ADDRESS, BLOCKQUOTE, CENTER, DIR, + DIV, DL, FIELDSET, FORM, H1, + H2, H3, H4, H5, H6, + HR, ISINDEX, MENU, NOFRAMES, NOSCRIPT, + OL, P, PRE, TABLE, UL, + AREA + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, REQUIRED) + } + ); + defElement(MENU, 0, false, false, createListModel(), + new String[] { + ADDRESS, BLOCKQUOTE, CENTER, DIR, + DIV, DL, FIELDSET, FORM, H1, + H2, H3, H4, H5, H6, + HR, ISINDEX, MENU, NOFRAMES, NOSCRIPT, + OL, P, PRE, TABLE, UL + } + , + new String[] { + LI, UL, OL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(COMPACT, null, new String[] { COMPACT }, 0, IMPLIED) + } + ); + defElement(META, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(HTTPEQUIV, null, null, 0, IMPLIED), + attr(sNAME, null, null, NAME, IMPLIED), + attr(CONTENT, null, null, 0, REQUIRED), + attr(SCHEME, null, null, 0, IMPLIED) + } + ); + defElement(NOFRAMES, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(NOSCRIPT, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(OBJECT, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL, PARAM + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(DECLARE, null, new String[] { DECLARE }, 0, IMPLIED), + attr(CLASSID, null, null, 0, IMPLIED), + attr(CODEBASE, null, null, 0, IMPLIED), + attr(DATA, null, null, 0, IMPLIED), + attr(TYPE, null, null, 0, IMPLIED), + attr(CODETYPE, null, null, 0, IMPLIED), + attr(ARCHIVE, null, null, 0, IMPLIED), + attr(STANDBY, null, null, 0, IMPLIED), + attr(HEIGHT, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED), + attr(USEMAP, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, LEFT, RIGHT }, + 0, IMPLIED), + attr(BORDER, null, null, PIXELS, IMPLIED), + attr(HSPACE, null, null, 0, IMPLIED), + attr(VSPACE, null, null, 0, IMPLIED) + } + ); + defElement(OL, 0, false, false, createListModel(), + NONE + , + new String[] { + // See note on the createListModel method + LI, UL, OL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(TYPE, null, null, 0, IMPLIED), + attr(COMPACT, null, new String[] { COMPACT }, 0, IMPLIED), + attr(START, null, null, 0, IMPLIED) + } + ); + defElement(OPTGROUP, 0, false, false, null, + NONE + , + new String[] { + OPTION + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(DISABLED, null, new String[] { DISABLED }, 0, IMPLIED), + attr(LABEL, null, null, 0, REQUIRED) + } + ); + defElement(OPTION, 0, false, true, new ContentModel(0, + new PCDATAonly_model(), null), + NONE, + new String[] { + PCDATA + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(SELECTED, null, new String[] { SELECTED }, 0, IMPLIED), + attr(DISABLED, null, new String[] { DISABLED }, 0, IMPLIED), + attr(LABEL, null, null, 0, IMPLIED), + attr(VALUE, null, null, 0, IMPLIED) + } + ); + defElement(P, 0, false, true, new ContentModel( 0, + new noTagModel(P), null), + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY }, + 0, IMPLIED) + } + ); + defElement(PARAM, EMPTY, false, true, null, + NONE + , + NONE + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(sNAME, null, null, 0, REQUIRED), + attr(VALUE, null, null, 0, IMPLIED), + attr(VALUETYPE, DATA, new String[] { DATA, REF, OBJECT }, 0, DEFAULT), + attr(TYPE, null, null, 0, IMPLIED) + } + ); + defElement(PRE, 0, false, false, null, + new String[] { + APPLET, BASEFONT, BIG, FONT, + IMG, OBJECT, SMALL, SUB, SUP + } + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + B, BDO, BR, BUTTON, CITE, + CODE, DFN, EM, I, IFRAME, + INPUT, KBD, LABEL, MAP, Q, + S, SAMP, SCRIPT, SELECT, SPAN, + STRIKE, STRONG, TEXTAREA, TT, U, + VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(WIDTH, null, null, NUMBER, IMPLIED) + } + ); + defElement(Q, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(CITE, null, null, 0, IMPLIED) + } + ); + defElement(S, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(SAMP, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(SCRIPT, CDATA, false, false, null, + NONE + , + NONE + , + new AttributeList[] { + attr(CHARSET, null, null, 0, IMPLIED), + attr(TYPE, null, null, 0, REQUIRED), + attr(LANGUAGE, null, null, 0, IMPLIED), + attr(SRC, null, null, 0, IMPLIED), + attr(DEFER, null, new String[] { DEFER }, 0, IMPLIED), + attr(EVENT, null, null, 0, IMPLIED), + attr(FOR, null, null, 0, IMPLIED) + } + ); + defElement(SELECT, 0, false, false, null, + NONE + , + new String[] { + OPTGROUP, OPTION + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(SIZE, null, null, NUMBER, IMPLIED), + attr(MULTIPLE, null, new String[] { MULTIPLE }, 0, IMPLIED), + attr(DISABLED, null, new String[] { DISABLED }, 0, IMPLIED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED), + attr(ONCHANGE, null, null, 0, IMPLIED) + } + ); + defElement(SMALL, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(SPAN, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(STRIKE, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(STRONG, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(STYLE, CDATA, false, false, null, + NONE + , + NONE + , + new AttributeList[] { + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(TYPE, null, null, 0, REQUIRED), + attr(MEDIA, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED) + } + ); + defElement(SUB, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(SUP, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(TABLE, 0, false, false, createTableContentModel(), + NONE + , + new String[] { + CAPTION, COL, COLGROUP, TBODY, + TFOOT, THEAD + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(SUMMARY, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED), + attr(BORDER, null, null, PIXELS, IMPLIED), + attr(FRAME, null, new String[] { VOID, ABOVE, BELOW, HSIDES, LHS, RHS, + VSIDES, BOX, BORDER }, 0, IMPLIED), + attr(RULES, null, new String[] { NONES, GROUPS, ROWS, COLS, ALL }, + 0, IMPLIED), + attr(CELLSPACING, null, null, 0, IMPLIED), + attr(CELLPADDING, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT }, 0, IMPLIED), + attr(BGCOLOR, null, null, 0, IMPLIED), + attr(DATAPAGESIZE, null, null, 0, IMPLIED) + } + ); + defElement(TBODY, 0, true, true, model(TR,'+'), + NONE + , + new String[] { + TR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED) + } + ); + + defElement(TD, 0, false, true, + new ContentModel(0, + new noTagModel(new String[] {"TD", "TH", "TR" } ), null), + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ABBR, null, null, 0, IMPLIED), + attr(AXIS, null, null, 0, IMPLIED), + attr(HEADERS, null, null, 0, IMPLIED), + attr(SCOPE, null, new String[] { ROW, COL, ROWGROUP, COLGROUP }, + 0, IMPLIED), + attr(ROWSPAN, C_1, null, NUMBER, DEFAULT), + attr(COLSPAN, C_1, null, NUMBER, DEFAULT), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED), + attr(NOWRAP, null, new String[] { NOWRAP }, 0, IMPLIED), + attr(BGCOLOR, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED), + attr(HEIGHT, null, null, 0, IMPLIED) + } + ); + defElement(TEXTAREA, 0, false, false, null, + NONE + , + new String[] { + PCDATA + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(sNAME, null, null, 0, IMPLIED), + attr(ROWS, null, null, NUMBER, REQUIRED), + attr(COLS, null, null, NUMBER, REQUIRED), + attr(DISABLED, null, new String[] { DISABLED }, 0, IMPLIED), + attr(READONLY, null, new String[] { READONLY }, 0, IMPLIED), + attr(TABINDEX, null, null, NUMBER, IMPLIED), + attr(ACCESSKEY, null, null, 0, IMPLIED), + attr(ONFOCUS, null, null, 0, IMPLIED), + attr(ONBLUR, null, null, 0, IMPLIED), + attr(ONSELECT, null, null, 0, IMPLIED), + attr(ONCHANGE, null, null, 0, IMPLIED) + } + ); + defElement(TFOOT, 0, false, true, model(TR,'+'), + NONE + , + new String[] { + TR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED) + } + ); + defElement(TH, 0, false, true, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR, + ADDRESS, BLOCKQUOTE, CENTER, DIR, DIV, + DL, FIELDSET, FORM, H1, H2, + H3, H4, H5, H6, HR, + ISINDEX, MENU, NOFRAMES, NOSCRIPT, OL, + P, PRE, TABLE, UL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ABBR, null, null, 0, IMPLIED), + attr(AXIS, null, null, 0, IMPLIED), + attr(HEADERS, null, null, 0, IMPLIED), + attr(SCOPE, null, new String[] { ROW, COL, ROWGROUP, COLGROUP }, + 0, IMPLIED), + attr(ROWSPAN, C_1, null, NUMBER, DEFAULT), + attr(COLSPAN, C_1, null, NUMBER, DEFAULT), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED), + attr(NOWRAP, null, new String[] { NOWRAP }, 0, IMPLIED), + attr(BGCOLOR, null, null, 0, IMPLIED), + attr(WIDTH, null, null, 0, IMPLIED), + attr(HEIGHT, null, null, 0, IMPLIED) + } + ); + defElement(THEAD, 0, false, true, model(TR,'+'), + NONE + , + new String[] { + TR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED) + } + ); + defElement(TITLE, 0, false, false, null, + new String[] { + OBJECT, SCRIPT, LINK, META, + STYLE + } + , + new String[] { + PCDATA + } + , + new AttributeList[] { + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED) + } + ); + defElement(TR, 0, false, true, + new ContentModel(0, new TableRowContentModel(this), null), + NONE + , + new String[] { + TD, TH + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(ALIGN, null, new String[] { LEFT, CENTER, RIGHT, JUSTIFY, CHAR }, + 0, IMPLIED), + attr(CHAR, null, null, 0, IMPLIED), + attr(CHAROFF, null, null, 0, IMPLIED), + attr(VALIGN, null, new String[] { TOP, MIDDLE, BOTTOM, BASELINE }, + 0, IMPLIED), + attr(BGCOLOR, null, null, 0, IMPLIED) + } + ); + defElement(TT, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(U, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + defElement(UL, 0, false, false, createListModel(), + NONE + , + new String[] { + // See note on the createListModel method + LI, UL, OL + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED), + attr(TYPE, null, new String[] { DISC, SQUARE, CIRCLE }, 0, IMPLIED), + attr(COMPACT, null, new String[] { COMPACT }, 0, IMPLIED) + } + ); + defElement(VAR, 0, false, false, null, + NONE + , + new String[] { + PCDATA, A, ABBR, ACRONYM, + APPLET, B, BASEFONT, BDO, BIG, + BR, BUTTON, CITE, CODE, DFN, + EM, FONT, I, IFRAME, IMG, + INPUT, KBD, LABEL, MAP, OBJECT, + Q, S, SAMP, SCRIPT, SELECT, + SMALL, SPAN, STRIKE, STRONG, SUB, + SUP, TEXTAREA, TT, U, VAR + } + , + new AttributeList[] { + attr(sID, null, null, ID, IMPLIED), + attr(CLASS, null, null, 0, IMPLIED), + attr(STYLE, null, null, 0, IMPLIED), + attr(TITLE, null, null, 0, IMPLIED), + attr(LANG, null, null, 0, IMPLIED), + attr(DIR, null, new String[] { LTR, RTL }, 0, IMPLIED), + attr(ONCLICK, null, null, 0, IMPLIED), + attr(ONDBLCLICK, null, null, 0, IMPLIED), + attr(ONMOUSEDOWN, null, null, 0, IMPLIED), + attr(ONMOUSEUP, null, null, 0, IMPLIED), + attr(ONMOUSEOVER, null, null, 0, IMPLIED), + attr(ONMOUSEMOVE, null, null, 0, IMPLIED), + attr(ONMOUSEOUT, null, null, 0, IMPLIED), + attr(ONKEYPRESS, null, null, 0, IMPLIED), + attr(ONKEYDOWN, null, null, 0, IMPLIED), + attr(ONKEYUP, null, null, 0, IMPLIED) + } + ); + + } + + /** + * Define all entities in this DTD. + */ + protected void defineEntities() + { + /* Define general entities */ + defineEntity("AElig", 198); + defineEntity("Aacute", 193); + defineEntity("Acirc", 194); + defineEntity("Agrave", 192); + defineEntity("Alpha", 913); + defineEntity("Aring", 197); + defineEntity("Atilde", 195); + defineEntity("Auml", 196); + defineEntity("Beta", 914); + defineEntity("Ccedil", 199); + defineEntity("Chi", 935); + defineEntity("Dagger", 8225); + defineEntity("Delta", 916); + defineEntity("ETH", 208); + defineEntity("Eacute", 201); + defineEntity("Ecirc", 202); + defineEntity("Egrave", 200); + defineEntity("Epsilon", 917); + defineEntity("Eta", 919); + defineEntity("Euml", 203); + defineEntity("Gamma", 915); + defineEntity("Iacute", 205); + defineEntity("Icirc", 206); + defineEntity("Igrave", 204); + defineEntity("Iota", 921); + defineEntity("Iuml", 207); + defineEntity("Kappa", 922); + defineEntity("Lambda", 923); + defineEntity("Mu", 924); + defineEntity("Ntilde", 209); + defineEntity("Nu", 925); + defineEntity("OElig", 338); + defineEntity("Oacute", 211); + defineEntity("Ocirc", 212); + defineEntity("Ograve", 210); + defineEntity("Omega", 937); + defineEntity("Omicron", 927); + defineEntity("Oslash", 216); + defineEntity("Otilde", 213); + defineEntity("Ouml", 214); + defineEntity("Phi", 934); + defineEntity("Pi", 928); + defineEntity("Prime", 8243); + defineEntity("Psi", 936); + defineEntity("Rho", 929); + defineEntity("Scaron", 352); + defineEntity("Sigma", 931); + defineEntity("THORN", 222); + defineEntity("Tau", 932); + defineEntity("Theta", 920); + defineEntity("Uacute", 218); + defineEntity("Ucirc", 219); + defineEntity("Ugrave", 217); + defineEntity("Upsilon", 933); + defineEntity("Uuml", 220); + defineEntity("Xi", 926); + defineEntity("Yacute", 221); + defineEntity("Yuml", 376); + defineEntity("Zeta", 918); + defineEntity("aacute", 225); + defineEntity("acirc", 226); + defineEntity("acute", 180); + defineEntity("aelig", 230); + defineEntity("agrave", 224); + defineEntity("alefsym", 8501); + defineEntity("alpha", 945); + defineEntity("amp", 38); + defineEntity("and", 8743); + defineEntity("ang", 8736); + defineEntity("aring", 229); + defineEntity("asymp", 8776); + defineEntity("atilde", 227); + defineEntity("auml", 228); + defineEntity("bdquo", 8222); + defineEntity("beta", 946); + defineEntity("brvbar", 166); + defineEntity("bull", 8226); + defineEntity("cap", 8745); + defineEntity("ccedil", 231); + defineEntity("cedil", 184); + defineEntity("cent", 162); + defineEntity("chi", 967); + defineEntity("circ", 710); + defineEntity("clubs", 9827); + defineEntity("cong", 8773); + defineEntity("copy", 169); + defineEntity("crarr", 8629); + defineEntity("cup", 8746); + defineEntity("curren", 164); + defineEntity("dArr", 8659); + defineEntity("dagger", 8224); + defineEntity("darr", 8595); + defineEntity("deg", 176); + defineEntity("delta", 948); + defineEntity("diams", 9830); + defineEntity("divide", 247); + defineEntity("eacute", 233); + defineEntity("ecirc", 234); + defineEntity("egrave", 232); + defineEntity("empty", 8709); + defineEntity("emsp", 8195); + defineEntity("ensp", 8194); + defineEntity("epsilon", 949); + defineEntity("equiv", 8801); + defineEntity("eta", 951); + defineEntity("eth", 240); + defineEntity("euml", 235); + defineEntity("euro", 8364); + defineEntity("exist", 8707); + defineEntity("fnof", 402); + defineEntity("forall", 8704); + defineEntity("frac12", 189); + defineEntity("frac14", 188); + defineEntity("frac34", 190); + defineEntity("frasl", 8260); + defineEntity("gamma", 947); + defineEntity("ge", 8805); + defineEntity("gt", 62); + defineEntity("hArr", 8660); + defineEntity("harr", 8596); + defineEntity("hearts", 9829); + defineEntity("hellip", 8230); + defineEntity("iacute", 237); + defineEntity("icirc", 238); + defineEntity("iexcl", 161); + defineEntity("igrave", 236); + defineEntity("image", 8465); + defineEntity("infin", 8734); + defineEntity("int", 8747); + defineEntity("iota", 953); + defineEntity("iquest", 191); + defineEntity("isin", 8712); + defineEntity("iuml", 239); + defineEntity("kappa", 954); + defineEntity("lArr", 8656); + defineEntity("lambda", 955); + defineEntity("lang", 9001); + defineEntity("laquo", 171); + defineEntity("larr", 8592); + defineEntity("lceil", 8968); + defineEntity("ldquo", 8220); + defineEntity("le", 8804); + defineEntity("lfloor", 8970); + defineEntity("lowast", 8727); + defineEntity("loz", 9674); + defineEntity("lrm", 8206); + defineEntity("lsaquo", 8249); + defineEntity("lsquo", 8216); + defineEntity("lt", 60); + defineEntity("macr", 175); + defineEntity("mdash", 8212); + defineEntity("micro", 181); + defineEntity("middot", 183); + defineEntity("minus", 8722); + defineEntity("mu", 956); + defineEntity("nabla", 8711); + defineEntity("nbsp", 160); + defineEntity("ndash", 8211); + defineEntity("ne", 8800); + defineEntity("ni", 8715); + defineEntity("not", 172); + defineEntity("notin", 8713); + defineEntity("nsub", 8836); + defineEntity("ntilde", 241); + defineEntity("nu", 957); + defineEntity("oacute", 243); + defineEntity("ocirc", 244); + defineEntity("oelig", 339); + defineEntity("ograve", 242); + defineEntity("oline", 8254); + defineEntity("omega", 969); + defineEntity("omicron", 959); + defineEntity("oplus", 8853); + defineEntity("or", 8744); + defineEntity("ordf", 170); + defineEntity("ordm", 186); + defineEntity("oslash", 248); + defineEntity("otilde", 245); + defineEntity("otimes", 8855); + defineEntity("ouml", 246); + defineEntity("para", 182); + defineEntity("part", 8706); + defineEntity("permil", 8240); + defineEntity("perp", 8869); + defineEntity("phi", 966); + defineEntity("pi", 960); + defineEntity("piv", 982); + defineEntity("plusmn", 177); + defineEntity("pound", 163); + defineEntity("prime", 8242); + defineEntity("prod", 8719); + defineEntity("prop", 8733); + defineEntity("psi", 968); + defineEntity("quot", 34); + defineEntity("rArr", 8658); + defineEntity("radic", 8730); + defineEntity("rang", 9002); + defineEntity("raquo", 187); + defineEntity("rarr", 8594); + defineEntity("rceil", 8969); + defineEntity("rdquo", 8221); + defineEntity("real", 8476); + defineEntity("reg", 174); + defineEntity("rfloor", 8971); + defineEntity("rho", 961); + defineEntity("rlm", 8207); + defineEntity("rsaquo", 8250); + defineEntity("rsquo", 8217); + defineEntity("sbquo", 8218); + defineEntity("scaron", 353); + defineEntity("sdot", 8901); + defineEntity("sect", 167); + defineEntity("shy", 173); + defineEntity("sigma", 963); + defineEntity("sigmaf", 962); + defineEntity("sim", 8764); + defineEntity("spades", 9824); + defineEntity("sub", 8834); + defineEntity("sube", 8838); + defineEntity("sum", 8721); + defineEntity("sup", 8835); + defineEntity("sup1", 185); + defineEntity("sup2", 178); + defineEntity("sup3", 179); + defineEntity("supe", 8839); + defineEntity("szlig", 223); + defineEntity("tau", 964); + defineEntity("there4", 8756); + defineEntity("theta", 952); + defineEntity("thetasym", 977); + defineEntity("thinsp", 8201); + defineEntity("thorn", 254); + defineEntity("tilde", 732); + defineEntity("times", 215); + defineEntity("trade", 8482); + defineEntity("uArr", 8657); + defineEntity("uacute", 250); + defineEntity("uarr", 8593); + defineEntity("ucirc", 251); + defineEntity("ugrave", 249); + defineEntity("uml", 168); + defineEntity("upsih", 978); + defineEntity("upsilon", 965); + defineEntity("uuml", 252); + defineEntity("weierp", 8472); + defineEntity("xi", 958); + defineEntity("yacute", 253); + defineEntity("yen", 165); + defineEntity("yuml", 255); + defineEntity("zeta", 950); + defineEntity("zwj", 8205); + defineEntity("zwnj", 8204); + } + + /** + * Crate a content model, consisting of the single + * element, specified by name. + */ + private ContentModel model(String element) + { + return new ContentModel(getElement(element)); + } + + /** + * Crate a chain from the two content models, + * the last containing the given element and + * the specified unary operation. + */ + private ContentModel model(String element, int unary) + { + ContentModel ct = model(element); + ct.type = unary; + return new ContentModel(0, ct); + } + + /** + * Create the model HEAD, BODY + * @return + */ + protected ContentModel createHtmlContentModel() + { + ContentModel head = model(HEAD); + ContentModel body = model(BODY); + head.next = body; + head.type = ','; + return head; + } + + /** + * Create the model + * ( CAPTION ? , ( COL * | COLGROUP * ) , THEAD ? , TFOOT ? , TBODY + ) + */ + protected ContentModel createTableContentModel() + { + ContentModel col_colgroup = new ContentModel + ('|', model(COL,'*'), model(COLGROUP,'*') ); + + col_colgroup = new ContentModel('*', col_colgroup); + col_colgroup = new ContentModel(',', col_colgroup); + + ContentModel caption = model(CAPTION,'?'); + ContentModel thead = model(THEAD, '?'); + ContentModel tfoot = model(TFOOT, '?'); + ContentModel tbody = model(TBODY, '+'); + + caption.next = col_colgroup; + col_colgroup.next = thead; + thead.next = tfoot; + tfoot.next = tbody; + + caption.type = col_colgroup.type = thead.type = tfoot.type = + tbody.type = ','; + + return caption; + } + + /** + * Creates a model for <DL> tag: + * <code> DT+ | DL+ </code>. + * @return + */ + protected ContentModel createDefListModel() + { + ContentModel dt = model(DT, '+'); + ContentModel dd = model(DD, '+'); + + dt.next = dd; + dt.type = dd.type = '|'; + return dt; + } + + /** + * This model is used for UL, OL, MENU and DIR. + * HTML 4.01 specifies LI only, but the nested + * list seems rendered correctly only if + * it is not enclosed into <LI>-</LI> of the + * parent list. + */ + protected ContentModel createListModel() + { + ContentModel li = model(LI, '+'); + ContentModel ul = model(UL, '+'); + ContentModel ol = model(OL, '+'); + + li.next = ul; + ul.next = ol; + li.type = ul.type = ol.type = '|'; + return li; + } + +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/gnuDTD.java b/libjava/classpath/gnu/javax/swing/text/html/parser/gnuDTD.java new file mode 100644 index 00000000000..1f4b3ec6adc --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/gnuDTD.java @@ -0,0 +1,422 @@ +/* gnuDTD.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser; + +import java.io.PrintStream; +import java.io.Serializable; + +import java.util.BitSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import javax.swing.text.html.parser.AttributeList; +import javax.swing.text.html.parser.ContentModel; +import javax.swing.text.html.parser.Element; +import javax.swing.text.html.parser.Entity; + +/** + * <p> + * The class is derived from {@link gnu.javax.swing.text.html.parser.DTD } + * making structure creation methods public. This is required when + * creating the DTD by SGML parser that must have access to the structure. + * + * SGML DTD representation. Provides basis for describing a syntax of the + * HTML documents. The fields of this class are NOT initialized in + * constructor. You need to do this separately before passing this data + * structure to the parser constructor.</p> + * + * <p>This implementation also provides you the derived class + * <code>gnu.javax.swing.text.html.parser.DTD.HTML_4_0_1</code>, where + * all fields are initialized to the values, representing HTML 4.01 + * ("-//W3C//DTD HTML 4.01 Frameset//EN") DTD. You can use it if you do not care + * about the portability between different implementations of the core + * class libraries. </p> + * <p>Use {@link javax.swing.HTML.HTMLEditorKit.Parser#parse } + * for parsing in accordance with "-//W3C//DTD HTML 4.01 Frameset//EN" + * without specifying DTD separately.</p> + * + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class gnuDTD + extends javax.swing.text.html.parser.DTD + implements javax.swing.text.html.parser.DTDConstants, Serializable +{ + /* The undocumented element types, used to specify types, not defined + in DTDConstants. */ + + /** + * The URI element type (not defined in DTDConstants). + */ + public static final int URI = 512; + + /** + * The Length element type + */ + public static final int Length = 513; + + /** + * The Char element type + */ + public static final int Char = 514; + + /** + * The Color element type + */ + public static final int Color = 515; + + /** + * Creates a new instance of gnuDTD. + * @param name the name of the DTD. + */ + public gnuDTD(String name) + { + super(name); + } + + /** + * Creates and returns new attribute (not an attribute list). + * @param name the name of this attribute + * @param type the type of this attribute (FIXED, IMPLIED or + * REQUIRED from <code>DTDConstants</code>). + * @param modifier the modifier of this attribute + * @param default_value the default value of this attribute or null if + * it is not specified. + * @param allowed_values the allowed values of this attribute. The multiple + * possible values in this parameter are supposed to be separated by + * '|', same as in SGML DTD <code><!ATTLIST </code>tag. This parameter + * can be null if no list of allowed values is specified. + * @param atts the previous attribute of this element. This is + * placed to the field + * {@link javax.swing.text.html.parser.AttributeList#next }, + * creating a linked list. + * @return + */ + public AttributeList defAttributeList(String name, int type, int modifier, + String default_value, + String allowed_values, + AttributeList atts + ) + { + return super.defAttributeList(name, type, modifier, default_value, + allowed_values, atts + ); + } + + /** + * Define the attributes for the element with the given name. + * If the element is not exist, it is created. This method is + * needed if the element attributes are defined befor the + * element itself. + * @param forElement + * @param attributes + */ + public void defAttrsFor(String forElement, AttributeList attributes) + { + super.defineAttributes(forElement, attributes); + } + + /** + * Creates a new content model. + * @param type specifies the BNF operation for this content model. + * The valid operations are documented in the + * {@link javax.swing.text.html.parser.ContentModel#type }. + * @param content the content of this content model + * @param next if the content model is specified by BNF-like + * expression, contains the rest of this expression. + * @return The newly created content model. + */ + public ContentModel defContentModel(int type, Object content, + ContentModel next + ) + { + return super.defContentModel(type, content, next); + } + + /** + * Defines a new element and adds it to the element table. + * If the element alredy exists, + * overrides it settings with the specified values. + * @param name the name of the new element + * @param type the type of the element + * @param headless true if the element needs no starting tag + * @param tailless true if the element needs no closing tag + * @param content the element content. + * @param exclusions the elements that must be excluded from the + * content of this element, in all levels of the hierarchy. + * @param inclusions the elements that can be included as the + * content of this element. + * @param attributes the element attributes. + * @return the created or updated element. + */ + public Element defElement(String name, int type, boolean headless, + boolean tailless, ContentModel content, + String[] exclusions, String[] inclusions, + AttributeList attributes + ) + { + return super.defElement(name, type, headless, tailless, content, + exclusions, inclusions, attributes + ); + } + + /** + * Defines a new element and adds it to the element table. + * If the element alredy exists, + * overrides it settings with the specified values. + * @param name the name of the new element + * @param type the type of the element + * @param headless true if the element needs no starting tag + * @param tailless true if the element needs no closing tag + * @param content the element content. + * @param exclusions the elements that must be excluded from the + * content of this element, in all levels of the hierarchy. + * @param inclusions the elements that can be included as the + * content of this element. + * @param attributes the element attributes. + * @return the created or updated element. + */ + public Element defElement(String name, int type, boolean headless, + boolean tailless, ContentModel content, + Collection exclusions, Collection inclusions, + AttributeList attributes + ) + { + return super.defElement(name, type, headless, tailless, content, + toStringArray(exclusions), + toStringArray(inclusions), attributes + ); + } + + /** + * Defines a new element and adds it to the element table. + * If the element alredy exists, + * overrides it settings with the specified values. + * @param name the name of the new element + * @param type the type of the element + * @param headless true if the element needs no starting tag + * @param tailless true if the element needs no closing tag + * @param content the element content. + * @param exclusions the elements that must be excluded from the + * content of this element, in all levels of the hierarchy. + * @param inclusions the elements that can be included as the + * content of this element. + * @param attributes the element attributes (an array and not a + * linked list). The attributes are chained into the linked list + * inside this method. + * @return the created or updated element. + */ + public Element defElement(String name, int type, boolean headless, + boolean tailless, ContentModel content, + String[] exclusions, String[] inclusions, + AttributeList[] attributes + ) + { + AttributeList list; + + if (attributes == null || attributes.length == 0) + list = null; + else + { + if (attributes.length > 1) + for (int i = 1; i < attributes.length; i++) + { + attributes [ i - 1 ].next = attributes [ i ]; + } + list = attributes [ 0 ]; + } + + Element e = + super.defElement(name, type, headless, tailless, content, exclusions, + inclusions, list + ); + return e; + } + + /** + * Creates, adds into the internal table and returns the + * character entity like <code>&lt;</code> + * (means '<code><</code>' ); + * This method inactivates the recursive refenrences to the same + * entity. + * @param name The entity name (without heading & and closing ;) + * @param type The entity type + * @param character The entity value (single character) + * @return The created entity + */ + public Entity defEntity(String name, int type, String data) + { + int r; + String eref = "%" + name + ";"; + do + { + r = data.indexOf(eref); + if (r > 0) + { + data = data.substring(0, r) + data.substring(r + 1); + } + } + while (r > 0); + + return super.defEntity(name, type, data); + } + + /** + * Summarises the document content into the given PrintStream. + */ + public void dump(PrintStream p) + { + Iterator iter = entityHash.entrySet().iterator(); + while (iter.hasNext()) + { + Map.Entry item = (Map.Entry) iter.next(); + Entity e = (Entity) item.getValue(); + if (e.isGeneral()) + p.println("Entity " + e.getName() + ": " + e.getString()); + } + + iter = elementHash.entrySet().iterator(); + while (iter.hasNext()) + { + Map.Entry item = (Map.Entry) iter.next(); + Element e = (Element) item.getValue(); + p.println("Element " + e.getName()); + + System.out.println(" includes:"); + dump(e.inclusions); + System.out.println(" excludes:"); + dump(e.exclusions); + System.out.println(" attributes:"); + + AttributeList atts = e.atts; + while (atts != null) + { + p.print(" " + atts.name + " = " + atts.value); + if (atts.values == null || atts.values.size() == 0) + p.println(); + else + { + Iterator viter = atts.values.iterator(); + System.out.print(" ( "); + while (viter.hasNext()) + { + System.out.print(viter.next()); + if (viter.hasNext()) + System.out.print(" | "); + } + System.out.println(" ) "); + } + atts = atts.next; + } + } + } + + /** + * Prints the content of the given attribute set to the System.out. + * @param b + */ + public void dump(BitSet b) + { + if (b != null) + { + for (int i = 0; i < b.size(); i++) + { + if (b.get(i)) + System.out.println(" " + elements.get(i)); + } + } + else + System.out.println(" NULL set"); + } + + /** + * Creates the attribute. + * @param name The attribute name. + * @param type The attribute type. + * @param modifier The attribute modifier. + * @param defaultValue Default value (or null) + * @param allowed_values Allowed values (or null) + * @return The newly created AttributeList. The <code>next</code> + * field is initialized to null. + */ + protected AttributeList attr(String name, String default_value, + String[] allowed_values, int type, int modifier + ) + { + Vector allowed = null; + + if (allowed_values != null) + { + allowed = new Vector(allowed_values.length); + for (int i = 0; i < allowed_values.length; i++) + { + allowed.add(allowed_values [ i ]); + } + } + ; + + AttributeList attr = + new AttributeList(name, type, modifier, default_value, allowed, null); + + return attr; + } + + /** + * Define the general entity, holding a single character. + * @param name The entity name (for example, 'amp'). + * The defined entity <b>is</b> stored into the entity table. + * @param character The entity character (for example, '&'). + */ + protected void defineEntity(String name, int character) + { + super.defEntity(name, GENERAL, character); + } + + private String[] toStringArray(Collection c) + { + String[] s = new String[ c.size() ]; + Iterator iter = c.iterator(); + for (int i = 0; i < s.length; i++) + { + s [ i ] = iter.next().toString(); + } + return s; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/htmlAttributeSet.java b/libjava/classpath/gnu/javax/swing/text/html/parser/htmlAttributeSet.java new file mode 100644 index 00000000000..a4e6f73c523 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/htmlAttributeSet.java @@ -0,0 +1,133 @@ +/* htmlAttributeSet.java -- A set to store HTML attributes + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser; + +import java.util.Enumeration; + +import javax.swing.text.AttributeSet; +import javax.swing.text.SimpleAttributeSet; +import javax.swing.text.html.HTML; + +/** + * A set, adapted to store HTML attributes. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class htmlAttributeSet + extends SimpleAttributeSet +{ + public static final htmlAttributeSet EMPTY_HTML_ATTRIBUTE_SET = + new htmlAttributeSet(); + AttributeSet parent; + + /** + * Looks in this set and, if not found, later looks in the parent set. + * Calls toString(), allowing to pass as HTML.Attribute, as String + * to this method. + * @param key A key to search for a value. + * @return The value, if one is defined. + */ + public Object getAttribute(Object _key) + { + Object key = _key.toString().toLowerCase(); + + Object v = super.getAttribute(key); + if (v != null) + return v; + else if (parent != null) + return parent.getAttribute(key); + else + return null; + } + + /** + * The name set must return HTML.Attribute and not a string, + * where applicable. + */ + public Enumeration getAttributeNames() + { + // Replace the string keys by HTML.attribute, where applicable + final Enumeration enumeration = super.getAttributeNames(); + + return new Enumeration() + { + public boolean hasMoreElements() + { + return enumeration.hasMoreElements(); + } + + public Object nextElement() + { + Object key = enumeration.nextElement(); + HTML.Attribute hKey = HTML.getAttributeKey((String) key); + if (hKey != null) + return hKey; + else + return key; + } + }; + } + + /** + * Set the parent set, containing the default values. + * @param a_parent + */ + public void setResolveParent(AttributeSet a_parent) + { + parent = a_parent; + } + + /** + * Get the parent set, containing the default values. + * @return + */ + public AttributeSet getResolveParent() + { + return parent; + } + + /** + * Add the attribute to this attribute set. + * @param key Attribute key (will be case insensitive) + * @param value Attribute value + */ + public void addAttribute(Object key, Object value) + { + super.addAttribute(key.toString().toLowerCase(), value); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/htmlValidator.java b/libjava/classpath/gnu/javax/swing/text/html/parser/htmlValidator.java new file mode 100644 index 00000000000..1f9eee03a91 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/htmlValidator.java @@ -0,0 +1,610 @@ +/* tagStack.java -- The HTML tag stack. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser; + +import gnu.javax.swing.text.html.parser.models.node; +import gnu.javax.swing.text.html.parser.models.transformer; + +import java.util.BitSet; +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.ListIterator; + +import javax.swing.text.SimpleAttributeSet; +import javax.swing.text.html.HTML; +import javax.swing.text.html.parser.*; + +/** + * <p>The HTML content validator, is responsible for opening and + * closing elements with optional start/end tags, detecting + * the wrongly placed html tags and reporting errors. The working instance + * is the inner class inside the {@link javax.swing.text.html.parser.Parser } + * </p> + * <p>This class could potentially + * provide basis for automated closing and insertion of the html tags, + * correcting the found html errors. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public abstract class htmlValidator +{ + /** + * The tag reference, holding additional information that the tag + * has been forcibly closed. + */ + protected class hTag + { + protected final Element element; + protected final HTML.Tag tag; + protected final TagElement tgElement; + protected boolean forcibly_closed; + protected node validationTrace; + + protected hTag(TagElement an_element) + { + element = an_element.getElement(); + tag = an_element.getHTMLTag(); + tgElement = an_element; + + if (element.content != null) + validationTrace = transformer.transform(element.content, dtd); + } + + /** + * This is called when the tag must be forcibly closed because + * it would make the newly appearing tag invalid. + * The parser is not notified about such event (just the error + * is reported). For such tags, the closing message does not + * appear when later reaching the end of stream. The exception is + * the <head> tag: the parser is notified about its silent closing + * when <body> or other html content appears. + */ + protected void forciblyCloseDueContext() + { + forcibly_closed = true; + } + + /** + * This is called when the tag must be forcibly closed after + * reaching the end of stream. The parser is notified as if + * closing the tag explicitly. + */ + protected void forciblyCloseDueEndOfStream() + { + forcibly_closed = true; + handleSupposedEndTag(element); + } + } + + /** + * The DTD, providing information about the valid document structure. + */ + protected final DTD dtd; + + /** + * The stack, holding the current tag context. + */ + protected final LinkedList stack = new LinkedList(); + + /** + * Creates a new tag stack, using the given DTD. + * @param a_dtd A DTD, providing the information about the valid + * tag content. + */ + public htmlValidator(DTD a_dtd) + { + dtd = a_dtd; + } + + /** + * Close all opened tags (called at the end of parsing). + */ + public void closeAll() + { + hTag h; + while (!stack.isEmpty()) + { + h = (hTag) stack.getLast(); + if (!h.forcibly_closed && !h.element.omitEnd()) + s_error("Unclosed <" + h.tag + ">, closing at the end of stream"); + + handleSupposedEndTag(h.element); + + closeTag(h.tgElement); + } + } + + /** + * Remove the given tag from the stack or (if found) from the list + * of the forcibly closed tags. + */ + public void closeTag(TagElement tElement) + { + HTML.Tag tag = tElement.getHTMLTag(); + hTag x; + hTag close; + + if (!stack.isEmpty()) + { + ListIterator iter = stack.listIterator(stack.size()); + + while (iter.hasPrevious()) + { + x = (hTag) iter.previous(); + if (tag.equals(x.tag)) + { + if (x.forcibly_closed && !x.element.omitEnd()) + s_error("The tag <" + x.tag + + "> has already been forcibly closed" + ); + + + // If the tag has a content model defined, forcibly close all + // tags that were opened after the tag being currently closed. + closing: + if (x.element.content != null) + { + iter = stack.listIterator(stack.size()); + while (iter.hasPrevious()) + { + close = (hTag) iter.previous(); + if (close == x) + break closing; + handleSupposedEndTag(close.element); + iter.remove(); + } + } + + stack.remove(x); + return; + } + } + } + s_error("Closing unopened <" + tag + ">"); + } + + /** + * Add the given HTML tag to the stack of the opened tags. Forcibly closes + * all tags in the stack that does not allow this tag in they content (error + * is reported). + * @param element + */ + public void openTag(TagElement tElement, htmlAttributeSet parameters) + { + // If this is a fictional call, the message from the parser + // has recursively returned - ignore. + if (tElement.fictional()) + return; + + validateParameters(tElement, parameters); + + // If the stack is empty, start from HTML + if (stack.isEmpty() && tElement.getHTMLTag() != HTML.Tag.HTML) + { + Element html = dtd.getElement(HTML.Tag.HTML.toString()); + openFictionalTag(html); + } + + Object v = tagIsValidForContext(tElement); + if (v != Boolean.TRUE) + { + // The tag is not valid for context, the content + // model suggest to open another tag. + if (v instanceof Element) + { + int n = 0; + while (v instanceof Element && (n++ < 100)) + { + Element fe = (Element) v; + + // notify the content model that we add the proposed tag + getCurrentContentModel().show(fe); + openFictionalTag(fe); + + Object vv = tagIsValidForContext(tElement); + if (vv instanceof Element) // One level of nesting is supported. + { + openFictionalTag((Element) vv); + + Object vx = tagIsValidForContext(tElement); + if (vx instanceof Element) + openFictionalTag((Element) vx); + } + else if (vv == Boolean.FALSE) + { + // The tag is still not valid for the current + // content after opening a fictional element. + if (fe.omitEnd()) + { + // close the previously opened fictional tag. + closeLast(); + vv = tagIsValidForContext(tElement); + if (vv instanceof Element) + + // another tag was suggested by the content model + openFictionalTag((Element) vv); + } + } + v = tagIsValidForContext(tElement); + } + } + else // If the current element has the optional end tag, close it. + { + if (!stack.isEmpty()) + { + closing: + do + { + hTag last = (hTag) stack.getLast(); + if (last.element.omitEnd()) + { + closeLast(); + v = tagIsValidForContext(tElement); + if (v instanceof Element) // another tag was suggested by the content model + { + openFictionalTag((Element) v); + break closing; + } + } + else + break closing; + } + while (v == Boolean.FALSE && !stack.isEmpty()); + } + } + } + + stack.add(new hTag(tElement)); + } + + /** + * Clear the stack. + */ + public void restart() + { + stack.clear(); + } + + /** + * Check if this tag is valid for the current context. + * Return Boolean.True if it is OK, Boolean.False + * if it is surely not OK or the Element that the + * content model recommends to insert making the situation + * ok. If Boolean.True is returned, the content model current + * position is moved forward. Otherwise this position remains + * the same. + * @param tElement + * @return + */ + public Object tagIsValidForContext(TagElement tElement) + { + // Check the current content model, if one is available. + node cv = getCurrentContentModel(); + + if (cv != null) + return cv.show(tElement.getElement()); + + // Check exclusions and inclusions. + ListIterator iter = stack.listIterator(stack.size()); + hTag t; + final int idx = tElement.getElement().index; + + // Check only known tags. + if (idx >= 0) + { + BitSet inclusions = new BitSet(); + while (iter.hasPrevious()) + { + t = (hTag) iter.previous(); + if (!t.forcibly_closed) + { + if (t.element.exclusions != null && + t.element.exclusions.get(idx) + ) + return Boolean.FALSE; + + if (t.element.inclusions != null) + inclusions.or(t.element.inclusions); + } + } + if (!inclusions.get(idx)) + return Boolean.FALSE; + } + return Boolean.TRUE; + } + + /** + * Validate tag without storing in into the tag stack. This is called + * for the empty tags and results the subsequent calls to the openTag + * and closeTag. + */ + public void validateTag(TagElement tElement, htmlAttributeSet parameters) + { + openTag(tElement, parameters); + closeTag(tElement); + } + + /** + * Check for mandatory elements, subsequent to the last tag: + * @param tElement The element that will be inserted next. + */ + protected void checkContentModel(TagElement tElement, boolean first) + { + if (stack.isEmpty()) + return; + + hTag last = (hTag) stack.getLast(); + if (last.validationTrace == null) + return; + + Object r = last.validationTrace.show(tElement.getElement()); + if (r == Boolean.FALSE) + s_error("The <" + last.element + "> does not match the content model " + + last.validationTrace + ); + else if (r instanceof Element) // The content model recommends insertion of this element + { + if (!first) + closeTag(last.tgElement); + handleSupposedStartTag((Element) r); + openTag(new TagElement((Element) r), null); + } + } + + /** + * The method is called when the tag must be closed because + * it does not allow the subsequent elements inside its context + * or the end of stream has been reached. The parser is only + * informed if the element being closed does not require the + * end tag (the "omitEnd" flag is set). + * The closing message must be passed to the parser mechanism + * before passing message about the opening the next tag. + * + * @param element The tag being fictionally (forcibly) closed. + */ + protected abstract void handleSupposedEndTag(Element element); + + /** + * The method is called when the validator decides to open the + * tag on its own initiative. This may happen if the content model + * includes the element with the optional (supposed) start tag. + * + * @param element The tag being opened. + */ + protected abstract void handleSupposedStartTag(Element element); + + /** + * Handles the error message. This method must be overridden to pass + * the message where required. + * @param msg The message text. + */ + protected abstract void s_error(String msg); + + /** + * Validate the parameters, report the error if the given parameter is + * not in the parameter set, valid for the given attribute. The information + * about the valid parameter set is taken from the Element, enclosed + * inside the tag. The method does not validate the default parameters. + * @param tag The tag + * @param parameters The parameters of this tag. + */ + protected void validateParameters(TagElement tag, htmlAttributeSet parameters) + { + if (parameters == null || + parameters == htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET || + parameters == SimpleAttributeSet.EMPTY + ) + return; + + Enumeration enumeration = parameters.getAttributeNames(); + + while (enumeration.hasMoreElements()) + { + validateAttribute(tag, parameters, enumeration); + } + + // Check for missing required values. + AttributeList a = tag.getElement().getAttributes(); + + while (a != null) + { + if (a.getModifier() == DTDConstants.REQUIRED) + if (parameters.getAttribute(a.getName()) == null) + { + s_error("Missing required attribute '" + a.getName() + "' for <" + + tag.getHTMLTag() + ">" + ); + } + a = a.next; + } + } + + private node getCurrentContentModel() + { + if (!stack.isEmpty()) + { + hTag last = (hTag) stack.getLast(); + return last.validationTrace; + } + else + return null; + } + + private void closeLast() + { + handleSupposedEndTag(((hTag) stack.getLast()).element); + stack.removeLast(); + } + + private void openFictionalTag(Element e) + { + handleSupposedStartTag(e); + stack.add(new hTag(new TagElement(e, true))); + if (!e.omitStart()) + s_error("<" + e + "> is expected (supposing it)"); + } + + private void validateAttribute(TagElement tag, htmlAttributeSet parameters, + Enumeration enumeration + ) + { + Object foundAttribute; + AttributeList dtdAttribute; + foundAttribute = enumeration.nextElement(); + dtdAttribute = tag.getElement().getAttribute(foundAttribute.toString()); + if (dtdAttribute == null) + { + StringBuffer valid = + new StringBuffer("The tag <" + tag.getHTMLTag() + + "> cannot contain the attribute '" + foundAttribute + + "'. The valid attributes for this tag are: " + ); + + AttributeList a = tag.getElement().getAttributes(); + + while (a != null) + { + valid.append(a.name.toUpperCase()); + valid.append(' '); + a = a.next; + } + s_error(valid.toString()); + } + + else + { + String value = parameters.getAttribute(foundAttribute).toString(); + + if (dtdAttribute.type == DTDConstants.NUMBER) + validateNumberAttribute(tag, foundAttribute, value); + + if (dtdAttribute.type == DTDConstants.NAME || + dtdAttribute.type == DTDConstants.ID + ) + validateNameOrIdAttribute(tag, foundAttribute, value); + + if (dtdAttribute.values != null) + validateAttributeWithValueList(tag, foundAttribute, dtdAttribute, + value + ); + } + } + + private void validateAttributeWithValueList(TagElement tag, + Object foundAttribute, + AttributeList dtdAttribute, + String value + ) + { + if (!dtdAttribute.values.contains(value.toLowerCase()) && + !dtdAttribute.values.contains(value.toUpperCase()) + ) + { + StringBuffer valid; + if (dtdAttribute.values.size() == 1) + valid = + new StringBuffer("The attribute '" + foundAttribute + + "' of the tag <" + tag.getHTMLTag() + + "> cannot have the value '" + value + + "'. The only valid value is " + ); + else + valid = + new StringBuffer("The attribute '" + foundAttribute + + "' of the tag <" + tag.getHTMLTag() + + "> cannot have the value '" + value + "'. The " + + dtdAttribute.values.size() + + " valid values are: " + ); + + Enumeration vv = dtdAttribute.values.elements(); + while (vv.hasMoreElements()) + { + valid.append('"'); + valid.append(vv.nextElement()); + valid.append("\" "); + } + s_error(valid.toString()); + } + } + + private void validateNameOrIdAttribute(TagElement tag, Object foundAttribute, + String value + ) + { + boolean ok = true; + + if (!Character.isLetter(value.charAt(0))) + ok = false; + + char c; + for (int i = 0; i < value.length(); i++) + { + c = value.charAt(i); + if (!( + Character.isLetter(c) || Character.isDigit(c) || + "".indexOf(c) >= 0 + ) + ) + ok = false; + } + if (!ok) + s_error("The '" + foundAttribute + "' attribute of the tag <" + + tag.getHTMLTag() + "> must start from letter and consist of " + + "letters, digits, hypens, colons, underscores and periods. " + + "It cannot be '" + value + "'" + ); + } + + private void validateNumberAttribute(TagElement tag, Object foundAttribute, + String value + ) + { + try + { + Integer.parseInt(value); + } + catch (NumberFormatException ex) + { + s_error("The '" + foundAttribute + "' attribute of the tag <" + + tag.getHTMLTag() + "> must be a valid number and not '" + + value + "'" + ); + } + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/PCDATAonly_model.java b/libjava/classpath/gnu/javax/swing/text/html/parser/models/PCDATAonly_model.java new file mode 100644 index 00000000000..5a19a1bc13c --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/PCDATAonly_model.java @@ -0,0 +1,62 @@ +/* PCDATAonly_model.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.models; + +import java.io.Serializable; + +/** + * The model, allowing only PCDATA in it (like for element OPTION). + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class PCDATAonly_model + extends node + implements Serializable +{ + private static final long serialVersionUID = 1; + + public PCDATAonly_model() + { + super((char) 0, (char) 0, null); + } + + public Object show(Object x) + { + return x.toString().equalsIgnoreCase("#pcdata") ? Boolean.TRUE : Boolean.FALSE; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/TableRowContentModel.java b/libjava/classpath/gnu/javax/swing/text/html/parser/models/TableRowContentModel.java new file mode 100644 index 00000000000..14514d58458 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/TableRowContentModel.java @@ -0,0 +1,77 @@ +/* TableRowContentModel.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.models; + +import java.io.Serializable; + +import javax.swing.text.html.parser.DTD; +import javax.swing.text.html.parser.Element; + +/** + * Table row content model. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class TableRowContentModel + extends node + implements Serializable +{ + private static final long serialVersionUID = 1; + final Element TD; + + public TableRowContentModel(DTD dtd) + { + super((char) 0, (char) 0, null); + TD = dtd.getElement("TD"); + } + + public Object show(Object x) + { + // Always accept TD and TH + String s = x.toString(); + if (s.equalsIgnoreCase("TD") || s.equalsIgnoreCase("TH")) + return Boolean.TRUE; + + // Suggest closing in response to TR: + if (s.equalsIgnoreCase("TR")) + return Boolean.FALSE; + + // Recommend TD for other cases: + return TD; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/list.java b/libjava/classpath/gnu/javax/swing/text/html/parser/models/list.java new file mode 100644 index 00000000000..b77ef7fd850 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/list.java @@ -0,0 +1,382 @@ +/* list.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.models; + +import java.io.Serializable; + +/** + * Part of the internal representation of the content model. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class list + extends node + implements Serializable +{ + private static final long serialVersionUID = 1; + + /** + * Setting to true means that the list nodes must always be connected + * by the same operation. This is far safer and clearer, but not + * required by default standard. + */ + public static boolean CLEAR; + + /** + * A list of nodes. + */ + public final node[] nodes; + + /** + * Creates a new model list that is a member of some enclosing list. + * @param binary_operator An operator with that this list is connected + * with other members of the enclosing list. + * @param unary_operator The unary operator for this list. + * @param a_nodes The nodes inside this list. + */ + public list(char binary_operator, char unary_operator, node[] a_nodes) + { + super(binary_operator, unary_operator, a_nodes); + nodes = a_nodes; + } + + /** + * Creates a new model list. Assigns the previous field. + * @param a_nodes The nodes for this list. + * @throws an error if the node elements are connected by the + * different operations. This is not supported, use grouping. + */ + public list(node[] a_nodes) + throws Error + { + this(',', (char) 0, a_nodes); + + int operation = nodes [ 0 ].binary; + + for (int i = 0; i < nodes.length; i++) + { + if (CLEAR && nodes [ i ].binary != operation) + throw new Error("List members can only be connected by " + + "the same operation, use grouping" + ); + + if (i > 0) + nodes [ i ].previous = nodes [ i - 1 ]; + } + } + + /** + * Returns true if all members in the list are closed. + */ + public boolean isClosed() + { + if (super.isClosed()) + return true; + for (int i = 0; i < nodes.length; i++) + { + if (!nodes [ i ].isClosed()) + return false; + } + return true; + } + + /** + * Find the token that could match as the next token in + * the token list. + * + * @return Such token object or null if none is found. + */ + public Object findFreeNode() + { + Object fn; + for (int j = 0; j < nodes.length; j++) + { + if (!nodes [ j ].isClosed()) + { + fn = nodes [ j ].findFreeNode(); + if (fn != null) + return fn; + } + } + return null; + } + + /** + * Tries to match this list agains the given token sequence. + * @param tokens the sequence of the tokens to match. + * @return true if the valid match is found. + */ + public boolean matches(Object[] tokens) + { + reset(); + + Object x; + boolean m; + boolean matched = false; + + for (int i = 0; i < tokens.length; i++) + { + matched = false; + x = tokens [ i ]; + + nodescan: + for (int j = 0; j < nodes.length; j++) + { + if (!nodes [ j ].isClosed()) + { + m = nodes [ j ].performMatch(x); + + if (m) + { + matched = true; + break nodescan; + } + } + } + if (!matched) + return false; + } + + boolean valid = true; + + for (int i = 0; i < nodes.length; i++) + { + if (!nodes [ i ].valid()) + valid = false; + } + + return valid; + } + + /** + * The list never closes, despite it is trated as closed + * if all members in the list are closed. + * @return false. + */ + public boolean mustClose() + { + return false; + } + + /** + * Perform a match operation for the single token + * against this list. + * @param token a token to match. + * @return true if the match is found. + */ + public boolean performMatch(Object token) + { + boolean ok = false; + Matching: + for (int i = 0; i < nodes.length; i++) + { + ok = nodes [ i ].performMatch(token); + + if (ok) + break Matching; + } + + if (ok) + matches(); + + return ok; + } + + /** + * Prepeares the list for the next matching operation. + */ + public void reset() + { + super.reset(); + for (int i = 0; i < nodes.length; i++) + nodes [ i ].reset(); + } + + /** + * Check if the provided token can match as a next token in the + * list. In the case of match, the list state changes, moving + * current position after the matched token. However if this method + * returns a suggested new token to insert before the provided one, + * the state of the list does not change. + * @return Boolean.TRUE if the match is found, + * Boolean.FALSE if the match is not possible and no token can be + * inserted to make the match valid. Otherwise, returns the + * token object that can be inserted before the last token in the + * list, probably (not for sure) making the match valid. + * If the object is an instance of Element or TagElement, + * it is first ensured that the object flag "omit start" is set. + */ + public Object show(Object x) + { + boolean m; + boolean matched = false; + + nodescan: + for (int j = 0; j < nodes.length; j++) + { + if (!nodes [ j ].isClosed()) + { + m = nodes [ j ].performMatch(x); + + if (m) + { + matched = true; + break nodescan; + } + else + { + // For comma operation, only first not closed + // node must be tested for a match. + // unless it allows matching zero times. + if (binary == ',' && + !(nodes [ j ].unary == '?' || nodes [ j ].unary == '*') + ) + break nodescan; + } + } + } + + if (!matched) + { + // Find and return that would be matched. + Object freeNode = findFreeNode(); + if (freeNode == null) + return Boolean.FALSE; + else + return freeNode; + } + + for (int i = 0; i < nodes.length; i++) + if (!nodes [ i ].validPreliminary()) + { + return Boolean.FALSE; + } + + return Boolean.TRUE; + } + + /** + * Returns a string representation of the list. + * @return String representation, similar to BNF expression. + */ + public String toString() + { + StringBuffer b = new StringBuffer(); + b.append(" ( "); + for (int i = 0; i < nodes.length; i++) + { + if (i > 0) + b.append(" " + (char) nodes [ i ].binary + " "); + b.append(nodes [ i ]); + } + + b.append(" )"); + if (unary != 0) + b.append((char) unary); + else + b.append(' '); + return b.toString(); + } + + /** + * Returns true if all memebers in the list are valid. + */ + public boolean valid() + { + for (int i = 0; i < nodes.length; i++) + { + if (!nodes [ i ].valid()) + return false; + } + return true; + } + + /** + * Returns true if all memebers in the list are either valid + * or unvisited. The unvisited members can become valid after + * more tokens will be shown. + */ + public boolean validPreliminary() + { + if (silenceAllowed()) + { + boolean everVisited = false; + for (int i = 0; i < nodes.length; i++) + { + if (nodes [ i ].visits > 0) + { + everVisited = true; + break; + } + } + if (!everVisited) + return true; + } + + for (int i = 0; i < nodes.length; i++) + { + if (!nodes [ i ].validPreliminary()) + return false; + } + return true; + } + + /** + * Closes all members in the list. + */ + protected void close() + { + super.close(); + for (int i = 0; i < nodes.length; i++) + { + nodes [ i ].close(); + } + } + + /** + * Compare given token with the token of this node. + * If the token represents a <code>list</code>, the call may be + * delegeted to the child subnodes. + * @param a_token A token to compare. + * @return True if the token matches the token of this node. + */ + protected boolean compare(Object a_token) + { + return performMatch(a_token); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/noTagModel.java b/libjava/classpath/gnu/javax/swing/text/html/parser/models/noTagModel.java new file mode 100644 index 00000000000..8aac14d8e16 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/noTagModel.java @@ -0,0 +1,75 @@ +/* noTagModel.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.models; + +import java.io.Serializable; + +/** + * Disallows a single given tag at the current content level only. + * <p>@author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)</p> + */ +public class noTagModel + extends node + implements Serializable +{ + private static final long serialVersionUID = 1; + final String[] no; + + public noTagModel(String[] noTag) + { + super((char) 0, (char) 0, null); + no = noTag; + } + + public noTagModel(String noTag) + { + super((char) 0, (char) 0, null); + no = new String[] { noTag }; + } + + public Object show(Object x) + { + for (int i = 0; i < no.length; i++) + { + if (x.toString().equalsIgnoreCase(no [ i ])) + return Boolean.FALSE; + } + return Boolean.TRUE; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/node.java b/libjava/classpath/gnu/javax/swing/text/html/parser/models/node.java new file mode 100644 index 00000000000..c615ddff350 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/node.java @@ -0,0 +1,337 @@ +/* node.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.models; + +import java.io.Serializable; + +/** + * Part of the internal representation of the content model. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class node + implements Serializable +{ + private static final long serialVersionUID = 1; + + /** + * The token to match (can be instance of list). + */ + public Object token; + + /** + * True for the node that cannot be visited again. + */ + public boolean _closed; + + /** + * The binary operation for this node. + */ + public char binary; + + /** + * The unary opeation for this node. + */ + public char unary; + + /** + * The number of times the node already was visited. + */ + public int visits; + + /** + * The previous node in content model (used for closing nodes). + */ + public node previous; + + /** + * Creates a new node. + * @param binary_operator The operator, connecting all nodes in the list. + * The nodes, connected by the different operators, must be arranged into + * the different lists. + * @param unary_operator The unary operator for this node or zero if + * no such was specified. + * @param token The token to match. This can be either a string or + * the new instance of the list. + * @param a_previous The previous node in the list, null for the first + * node. This is used for propagating the closing operation for the + * comma delimited list. + */ + public node(char binary_operator, char unary_operator, Object a_token) + { + if (a_token != null) + if (a_token.getClass().equals(node.class)) + throw new Error("Creating node in node is redundant and ineffective."); + + binary = binary_operator; + unary = unary_operator; + token = a_token; + } + + /** + * Checks if this node is in the closed state. + * @return True if the node is closed. + */ + public boolean isClosed() + { + return _closed; + } + + /** + * Check if closing this node means closing the previous node. + */ + public boolean closePrevious() + { + return binary == ','; + } + + /** + * Return the token object if it could match as a next token in + * a token list of null if it could not. + * @return + */ + public Object findFreeNode() + { + boolean ok; + if (isClosed() || silenceAllowed()) + return null; + + // Try if the node would stay valid after a one more visit. + visits++; + ok = valid(); + visits--; + + if (ok) + { + if (token instanceof node) + return ((node) token).findFreeNode(); + else + return token; + } + else + return null; + } + + /** + * Check if the current situation is such that the node must be closed + * now. + */ + public boolean mustClose() + { + switch (unary) + { + case 0 : + return true; + + case '*' : + return false; + + case '+' : + return false; + + case '?' : + return visits <= 1; + + default : + throw new Error("Invalid unary operation " + unary + " ( '" + + (char) unary + "' )" + ); + } + } + + /** + * Do the match operation with the given token. This sets various + * flags. + * @param token The token to match. + * @return true if the the token matches node, false if it does not match + * or if the node is closed. + */ + public boolean performMatch(Object a_token) + { + if (isClosed()) + return false; + + boolean matches = compare(a_token); + if (matches) + matches(); + + return matches; + } + + /** + * Prepares the node for matching against a new list of tokens. + */ + public void reset() + { + _closed = false; + visits = 0; + } + + /** + * Check if the provided token can match this node. + * In the case of match, the node state changes, moving + * current position after the matched token. However if this method + * returns a suggested new token to insert before the provided one, + * the state of the list does not change. + * @return Boolean.TRUE if the match is found, + * Boolean.FALSE if the match is not possible and no token can be + * inserted to make the match valid. Otherwise, returns the + * token object that can be inserted before the last token in the + * list, probably (not for sure) making the match valid. + */ + public Object show(Object x) + { + if (compare(x)) + return performMatch(x) ? Boolean.TRUE : Boolean.FALSE; + + Object recommended = findFreeNode(); + return recommended != null ? recommended : Boolean.FALSE; + } + + /** + * Check if it would be a valid case if this node is visited zero times. + * Nodes with unary operator * or ? need not be matched to make a + * model valid. + */ + public boolean silenceAllowed() + { + return unary == '?' || unary == '*'; + } + + /** + * Returns a string representation of the list. + * @return String representation, similar to BNF expression. + */ + public String toString() + { + StringBuffer b = new StringBuffer(); + + b.append(token); + if (unary != 0) + b.append((char) unary); + else + b.append('\''); + + return b.toString(); + } + + /** + * Check if the node state is valid. + */ + public boolean valid() + { + switch (unary) + { + case 0 : + if (binary == '|') + return true; + else + return visits == 1; + + case '*' : + return true; + + case '+' : + return visits > 0; + + case '?' : + return visits <= 1; + + default : + throw new Error("Invalid unary operation " + unary + " ( '" + + (char) unary + "' )" + ); + } + } + + public boolean validPreliminary() + { + return visits == 0 || valid(); + } + + /** + * Closes this node and, if closePrevious() returs true, calls close() for + * the previous node. + */ + protected void close() + { + _closed = true; + if (previous != null && closePrevious()) + previous.close(); + } + + /** + * Compare the provided token object with the token object of this node. + */ + protected boolean compare(Object a_token) + { + if (token instanceof Object[]) + throw new Error("Invalid token object, probably the 'list' " + + "should be used. " + ); + + if (token instanceof node[]) + throw new Error("Do not use 'node' for the array of nodes, use 'list'. "); + + if (token instanceof node) + { + return ((node) token).performMatch(a_token); + } + + boolean rt = false; + + if (token == a_token) + rt = true; + if (token.equals(a_token)) + rt = true; + if (token.toString().equalsIgnoreCase(a_token.toString())) + rt = true; + + return rt; + } + + /** + * Fire the changes that must happen then the token matches this node. + */ + protected void matches() + { + visits++; + if (mustClose()) + close(); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/package.html b/libjava/classpath/gnu/javax/swing/text/html/parser/models/package.html new file mode 100644 index 00000000000..18e61aeded7 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/package.html @@ -0,0 +1,53 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in javax.swing.text.html.parser package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.javax.swing.text.html.parser.models</title></head> + +<body> +<p>This package contains classes for working with content models. In this implementation, the +standardized content model is pre-processed by <code>transformer</code> into an instance of +<code>node</code>. Node holds a single element of the content model with the optional unary operation. +The derived class <code>list</code> holds multiple nodes connected by the same binary operation. +As the members of this <code>list</code> can also be lists itself, these structures support +the most of required operations. Several cases when the model cannot be expressed using +BNF syntax are handled providing specialised classes that are also derived from <code>node</code>. +</p> +@author Audrius Meskauskas, Lithuania +</body> +</html> diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/models/transformer.java b/libjava/classpath/gnu/javax/swing/text/html/parser/models/transformer.java new file mode 100644 index 00000000000..22ae3c3fa52 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/models/transformer.java @@ -0,0 +1,201 @@ +/* transformer.java -- Content model transforms. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.models; + +import java.io.Serializable; + +import javax.swing.text.html.parser.ContentModel; +import javax.swing.text.html.parser.DTD; + +/** + * Transforms the standard ContentModel tree into the internal representation, + * used in this implementation. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class transformer + implements Serializable +{ + private static final long serialVersionUID = 1; + + /** + * All binary operators. + */ + protected static String binary = "&|,"; + + /** + * All unary operators. + */ + protected static String unary = "+*?"; + + /** + * Measure length of the linked list of the content models. + * @param c The heading element of the linked list. + * @return the length of the list (0 for null 1 if c!=null and c.next==null, + * etc. + */ + public static int measureChainLength(ContentModel c) + { + if (c == null) + return 0; + else + return measureChainLength(c.next) + 1; + } + + /** + * Transform into internal representation without usind dtd. + * This should be used only for testing. + */ + public static node transform(ContentModel c) + { + return transform(c, null); + } + + /** + * Transform into internal representation. + * @param c a model to transform + * @return a transformed model + * @throws Error if the model structure contains errors. + */ + public static node transform(ContentModel c, DTD dtd) + { + // Handle the special cases first. + if (c.content instanceof node) + return (node) c.content; + + // Do the typical transform. + node n; + + /* Case with the single token */ + if (c.next == null) + { + n = optionalTransform(c, dtd); + } + else /* Case with the chain of the multiple tokens. */ + { + node[] l = new node[ measureChainLength(c) ]; + ContentModel m = c; + for (int i = 0; i < l.length; i++) + { + if (m.content instanceof ContentModel) + { + ContentModel nested = (ContentModel) m.content; + if (nested.next == null && + !(nested.content instanceof ContentModel) + ) + { + l [ i ] = + new node((char) m.type, (char) nested.type, nested.content); + } + else + { + l [ i ] = transform(nested, dtd); + } + } + else + l [ i ] = new node((char) 0, (char) 0, m.content); + addtype(l [ i ], (char) m.type); + m = m.next; + } + + if (isBinary(c.type)) + for (int i = 0; i < l.length; i++) + { + l [ i ].binary = (char) c.type; + } + + n = new list(l); + } + + addtype(n, (char) c.type); + + return n; + } + + /** + * True for binary operator + * @param c a character to test + * @return true for [ ,&| ], false otherwise. + */ + private static boolean isBinary(int c) + { + return binary.indexOf((char) c) >= 0; + } + + /** + * True for unary operator. + * @param c a character to test + * @return true for [ +?* ], false otherwise. + */ + private static boolean isUnary(int c) + { + return unary.indexOf((char) c) >= 0; + } + + /** + * Assign an operation type for the given node. + * @param n A node to set the operation to. + * @param type Either binary or unary operation, is assigned to the + * corresponding field of the node. + * @throws error if the operation type is not + * representing a valid unary or binary operation. + */ + private static void addtype(node n, char type) + { + if (isBinary(type)) + n.binary = type; + + else if (isUnary(type)) + n.unary = type; + + else if (type != 0) + throw new Error("Invalid operation '" + (char) type + "'"); + } + + private static node optionalTransform(ContentModel c, DTD dtd) + { + node n; + if (c.content instanceof ContentModel) + n = transform((ContentModel) c.content, dtd); + else + + /* A single token with the specified operation */ + n = new node((char) 0, (char) 0, c.content); + return n; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/package.html b/libjava/classpath/gnu/javax/swing/text/html/parser/package.html new file mode 100644 index 00000000000..cd050f9c2cf --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/package.html @@ -0,0 +1,51 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in javax.swing.text.html.parser package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - javax.swing.text.html.parser</title></head> + +<body> +<p>Provides the error tolerant, DTD-driven HTML 4.01 parser. +The parser that is used in web robots, html content analysers, +web browsers, web editors and other related applications. +It should compativle with the older HTML versions, supporting +obsoleted HTML featues. This package also includes some +supporting classes.</p> +@author Audrius Meskauskas, Lithuania +</body> +</html> diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/Parser.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/Parser.java new file mode 100644 index 00000000000..cef94942e16 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/Parser.java @@ -0,0 +1,1443 @@ +/* Parser.java -- HTML parser. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support; + +import gnu.javax.swing.text.html.parser.htmlAttributeSet; +import gnu.javax.swing.text.html.parser.htmlValidator; +import gnu.javax.swing.text.html.parser.support.low.Constants; +import gnu.javax.swing.text.html.parser.support.low.ParseException; +import gnu.javax.swing.text.html.parser.support.low.ReaderTokenizer; +import gnu.javax.swing.text.html.parser.support.low.Token; +import gnu.javax.swing.text.html.parser.support.low.node; +import gnu.javax.swing.text.html.parser.support.low.pattern; + +import java.io.IOException; +import java.io.Reader; + +import java.util.Comparator; +import java.util.Set; +import java.util.TreeSet; +import java.util.Vector; + +import javax.swing.text.ChangedCharSetException; +import javax.swing.text.html.HTML; +import javax.swing.text.html.parser.AttributeList; +import javax.swing.text.html.parser.DTD; +import javax.swing.text.html.parser.DTDConstants; +import javax.swing.text.html.parser.Element; +import javax.swing.text.html.parser.Entity; +import javax.swing.text.html.parser.TagElement; + +/** + * <p>A simple error-tolerant HTML parser that uses a DTD document + * to access data on the possible tokens, arguments and syntax.</p> + * <p> The parser reads an HTML content from a Reader and calls various + * notifying methods (which should be overridden in a subclass) + * when tags or data are encountered.</p> + * <p>Some HTML elements need no opening or closing tags. The + * task of this parser is to invoke the tag handling methods also when + * the tags are not explicitly specified and must be supposed using + * information, stored in the DTD. + * For example, parsing the document + * <p><table><tr><td>a<td>b<td>c</tr> <br> + * will invoke exactly the handling methods exactly in the same order + * (and with the same parameters) as if parsing the document: <br> + * <em><html><head></head><body><table>< + * tbody></em><tr><td>a<em></td></em><td>b<em> + * </td></em><td>c<em></td></tr></em>< + * <em>/tbody></table></body></html></em></p> + * (supposed tags are given in italics). The parser also supports + * obsolete elements of HTML syntax.<p> + * </p> + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Parser + extends ReaderTokenizer + implements DTDConstants +{ + /** + * The current html tag. + */ + public Token hTag = new Token(); + + /** + * The document template description that will be used to parse the documents. + */ + protected DTD dtd; + + /** + * The value of this field determines whether or not the Parser will be + * strict in enforcing SGML compatibility. The default value is false, + * stating that the parser should do everything to parse and get at least + * some information even from the incorrectly written HTML input. + */ + protected boolean strict; + + /** + * This fields has positive values in preformatted tags. + */ + protected int preformatted = 0; + + /** + * The set of the document tags. This field is used for supporting + * markFirstTime(). + */ + private Set documentTags = + new TreeSet(new Comparator() + { + public int compare(Object a, Object b) + { + return ((String) a).compareToIgnoreCase((String) b); + } + } + ); + + /** + * The buffer to collect the incremental output like text or coment. + */ + private StringBuffer buffer = new StringBuffer(); + + /** + * The buffer to store the document title. + */ + private StringBuffer title = new StringBuffer(); + + /** + * The current token. + */ + private Token t; + + /** + * True means that the 'title' tag of this document has + * already been handled. + */ + private boolean titleHandled; + + /** + * True means that the 'title' tag is currently open and all + * text is also added to the title buffer. + */ + private boolean titleOpen; + + /** + * The attributes of the current HTML element. + * Package-private to avoid an accessor method. + */ + htmlAttributeSet attributes = + htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET; + + /** + * The validator, controlling the forcible closing of the tags that + * (in accordance to dtd) are not allowed in the current context. + */ + private htmlValidator validator; + + /** + * Provides the default values for parameters in the case when these + * values are defined in the DTD. + */ + private parameterDefaulter defaulter; + + /** + * The text pre-processor for handling line ends and tabs. + */ + private textPreProcessor textProcessor = new textPreProcessor(); + + /** + * Creates a new Parser that uses the given + * {@link javax.swing.text.html.parser.DTD }. The only standard way + * to get an instance of DTD is to construct it manually, filling in + * all required fields. + * @param a_dtd The DTD to use. The parser behaviour after passing null + * as an argument is not documented and may vary between implementations. + */ + public Parser(DTD a_dtd) + { + if (a_dtd == null) + dtd = gnu.javax.swing.text.html.parser.HTML_401F.getInstance(); + else + dtd = a_dtd; + + defaulter = new parameterDefaulter(dtd); + + validator = + new htmlValidator(dtd) + { + /** + * Handles the error message. This method must be overridden to pass + * the message where required. + * @param msg The message text. + */ + protected void s_error(String msg) + { + error(msg); + } + + /** + * The method is called when the tag validator decides to close the + * tag on its own initiative. After reaching the end of stream, + * The tag validator closes all unclosed elements that are required + * to have the end (closing) tag. + * + * @param element The tag being fictionally (forcibly) closed. + */ + protected void handleSupposedEndTag(Element tElement) + { + // The tag is cloned as the original tElement is the + // element from the starting tag - may be accidently used + // somewhere else. + TagElement tag = makeTag(tElement, true); + _handleEndTag_remaining(tag); + } + + /** + * The method is called when the the tag validator decides to open + * the new tag on its own initiative. The tags, opened in this + * way, are HTML, HEAD and BODY. The attribute set is temporary + * assigned to the empty one, the previous value is + * restored before return. + * + * @param element The tag being fictionally (forcibly) closed. + */ + protected void handleSupposedStartTag(Element tElement) + { + TagElement tag = makeTag(tElement, true); + htmlAttributeSet were = attributes; + attributes = htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET; + _handleStartTag(tag); + attributes = were; + } + }; + } + + /** + * Get the attributes of the current tag. + * @return The attribute set, representing the attributes of the current tag. + */ + public htmlAttributeSet getAttributes() + { + return attributes; + } + + /** + * Invokes the error handler. The default method in this implementation + * delegates the call to handleError, also providing the current line. + */ + public void error(String msg) + { + error(msg, getTokenAhead()); + } + + public void error(String msg, Token atToken) + { + if (atToken != null) + handleError(atToken.where.beginLine, + msg + ": line " + atToken.where.beginLine + + ", absolute pos " + atToken.where.startPosition + ); + else + handleError(0, msg); + } + + /** + * Invokes the error handler. The default method in this implementation + * delegates the call to error (parm1+": '"+parm2+"'"). + */ + public void error(String msg, String invalid) + { + error(msg + ": '" + invalid + "'"); + } + + /** + * Invokes the error handler. The default method in this implementation + * delegates the call to error (parm1+" "+ parm2+" "+ parm3). + */ + public void error(String parm1, String parm2, String parm3) + { + error(parm1 + " " + parm2 + " " + parm3); + } + + /** + * Invokes the error handler. The default method in this implementation + * delegates the call to error (parm1+" "+ parm2+" "+ parm3+" "+ parm4). + */ + public void error(String parm1, String parm2, String parm3, String parm4) + { + error(parm1 + " " + parm2 + " " + parm3 + " " + parm4); + } + + public void flushAttributes() + { + } + + /** + * Parse the HTML text, calling various methods in response to the + * occurence of the corresponding HTML constructions. + * @param reader The reader to read the source HTML from. + * @throws IOException If the reader throws one. + */ + public synchronized void parse(Reader reader) + throws IOException + { + reset(reader); + restart(); + try + { + parseDocument(); + validator.closeAll(); + } + catch (ParseException ex) + { + if (ex != null) + { + error("Unable to continue parsing the document", ex.getMessage()); + + Throwable cause = ex.getCause(); + if (cause instanceof IOException) + throw (IOException) cause; + } + } + } + + /** + * Parses DTD markup declaration. Currently returns null without action. + * @return null. + * @throws IOException + */ + public String parseDTDMarkup() + throws IOException + { + return null; + } + + /** + * Parse SGML insertion ( <! ... > ). When the + * the SGML insertion is found, this method is called, passing + * SGML in the string buffer as a parameter. The default method + * returns false without action and can be overridden to + * implement user - defined SGML support. + * <p> + * If you need more information about SGML insertions in HTML documents, + * the author suggests to read SGML tutorial on + * {@link http://www.w3.org/TR/WD-html40-970708/intro/sgmltut.html}. + * We also recommend Goldfarb C.F (1991) <i>The SGML Handbook</i>, + * Oxford University Press, 688 p, ISBN: 0198537379. + * </p> + * @param strBuff + * @return true if this is a valid DTD markup declaration. + * @throws IOException + */ + public boolean parseMarkupDeclarations(StringBuffer strBuff) + throws IOException + { + return false; + } + + /** + * Get the first line of the last parsed token. + */ + protected int getCurrentLine() + { + return hTag.where.beginLine; + } + + /** + * Read parseable character data, add to buffer. + * @param clearBuffer If true, buffer if filled by CDATA section, + * otherwise the section is appended to the existing content of the + * buffer. + * + * @throws ParseException + */ + protected void CDATA(boolean clearBuffer) + throws ParseException + { + Token start = hTag = getTokenAhead(); + + if (clearBuffer) + buffer.setLength(0); + + // Handle expected EOF. + if (start.kind == EOF) + return; + + read: + while (true) + { + t = getTokenAhead(); + if (t.kind == EOF) + { + error("unexpected eof", t); + break read; + } + else if (t.kind == BEGIN) + break read; + else if (t.kind == Constants.ENTITY) + { + resolveAndAppendEntity(t); + getNextToken(); + } + else + { + append(t); + getNextToken(); + } + } + hTag = new Token(start, getTokenAhead(0)); + if (buffer.length() != 0) + _handleText(); + } + + /** + * Process Comment. This method skips till --> without + * taking SGML constructs into consideration. The supported SGML + * constructs are handled separately. + */ + protected void Comment() + throws ParseException + { + buffer.setLength(0); + + Token start = hTag = mustBe(BEGIN); + optional(WS); + mustBe(EXCLAMATION); + optional(WS); + mustBe(DOUBLE_DASH); + + Token t; + Token last; + + comment: + while (true) + { + t = getTokenAhead(); + if (t.kind == EOF) + { + handleEOFInComment(); + last = t; + break comment; + } + else if (COMMENT_END.matches(this)) + { + mustBe(DOUBLE_DASH); + optional(WS); + last = mustBe(END); + break comment; + } + else if (COMMENT_TRIPLEDASH_END.matches(this)) + { + mustBe(DOUBLE_DASH); + t = mustBe(NUMTOKEN); + if (t.getImage().equals("-")) + { + append(t); + last = mustBe(END); + break comment; + } + else + { + buffer.append("--"); + append(t); + t = getTokenAhead(); + } + } + else + /* The lllll-- can match as NUMTOKEN */ + if ((t.getImage().endsWith("--")) && + ( + getTokenAhead(1).kind == END || + (getTokenAhead(1).kind == WS && getTokenAhead(2).kind == END) + ) + ) + { + buffer.append(t.getImage().substring(0, t.getImage().length() - 2)); + + /* Skip the closing > that we have already checked. */ + last = mustBe(t.kind); + break comment; + } + else + append(t); + mustBe(t.kind); + } + hTag = new Token(start, last); + handleComment(); + } + + /** + * Read a script. The text, returned without any changes, + * is terminated only by the closing tag SCRIPT. + */ + protected void Script() + throws ParseException + { + Token name; + + Token start = hTag = mustBe(BEGIN); + optional(WS); + + name = mustBe(SCRIPT); + + optional(WS); + + restOfTag(false, name, start); + + buffer.setLength(0); + + script: + while (!SCRIPT_CLOSE.matches(this)) + { + append(getNextToken()); + } + + consume(SCRIPT_CLOSE); + + _handleText(); + + endTag(false); + _handleEndTag(makeTagElement(name.getImage(), false)); + } + + /** + * Process SGML insertion that is not a comment. + */ + protected void Sgml() + throws ParseException + { + if (COMMENT_OPEN.matches(this)) + Comment(); + else // skip till ">" + { + Token start = hTag = mustBe(BEGIN); + optional(WS); + mustBe(EXCLAMATION); + + buffer.setLength(0); + read: + while (true) + { + t = getNextToken(); + if (t.kind == Constants.ENTITY) + { + resolveAndAppendEntity(t); + } + else if (t.kind == EOF) + { + error("unexpected eof", t); + break read; + } + else if (t.kind == END) + break read; + else + append(t); + } + + try + { + parseMarkupDeclarations(buffer); + } + catch (IOException ex) + { + error("Unable to parse SGML insertion: '" + buffer + "'", + new Token(start, t) + ); + } + } + } + + /** + * Read a style definition. The text, returned without any changes, + * is terminated only by the closing tag STYLE. + */ + protected void Style() + throws ParseException + { + Token name; + + Token start = hTag = mustBe(BEGIN); + optional(WS); + + name = mustBe(STYLE); + + optional(WS); + + restOfTag(false, name, start); + + buffer.setLength(0); + + style: + while (!STYLE_CLOSE.matches(this)) + { + append(getNextToken()); + } + + consume(STYLE_CLOSE); + + _handleText(); + + endTag(false); + _handleEndTag(makeTagElement(name.getImage(), false)); + } + + /** + * Read a html tag. + */ + protected void Tag() + throws ParseException + { + mark(true); + + boolean closing = false; + Token name; + Token start = hTag = mustBe(BEGIN); + + optional(WS); + name = getNextToken(); + optional(WS); + + if (name.kind == SLASH) + { + closing = true; + name = getNextToken(); + } + + restOfTag(closing, name, start); + } + + /** + * A hook, for operations, preceeding call to handleText. + * Handle text in a string buffer. + * In non - preformatted mode, all line breaks immediately following the + * start tag and immediately before an end tag is discarded, + * \r, \n and \t are replaced by spaces, multiple space are replaced + * by the single one and the result is moved into array, + * passing it to handleText(). + */ + protected void _handleText() + { + char[] text; + + if (preformatted > 0) + text = textProcessor.preprocessPreformatted(buffer); + else + text = textProcessor.preprocess(buffer); + + if (text != null && text.length > 0) + { + TagElement pcdata = new TagElement(dtd.getElement("#pcdata")); + attributes = htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET; + _handleEmptyTag(pcdata); + + handleText(text); + if (titleOpen) + title.append(text); + } + } + + /** + * Add the image of this token to the buffer. + * @param t A token to append. + */ + protected final void append(Token t) + { + if (t.kind != EOF) + t.appendTo(buffer); + } + + /** + * Consume pattern that must match. + * @param p A pattern to consume. + */ + protected final void consume(pattern p) + { + node n; + for (int i = 0; i < p.nodes.length; i++) + { + n = p.nodes [ i ]; + if (n.optional) + optional(n.kind); + else + mustBe(n.kind); + } + } + + /** + * The method is called when the HTML end (closing) tag is found or if + * the parser concludes that the one should be present in the + * current position. The method is called immediatly + * before calling the handleEndTag(). + * @param omitted True if the tag is no actually present in the document, + * but is supposed by the parser (like </html> at the end of the + * document). + */ + protected void endTag(boolean omitted) + { + } + + /** + * Handle HTML comment. The default method returns without action. + * @param comment + */ + protected void handleComment(char[] comment) + { + } + + /** + * This is additionally called in when the HTML content terminates + * without closing the HTML comment. This can only happen if the + * HTML document contains errors (for example, the closing --;gt is + * missing. + */ + protected void handleEOFInComment() + { + error("Unclosed comment"); + } + + /** + * Handle the tag with no content, like <br>. The method is + * called for the elements that, in accordance with the current DTD, + * has an empty content. + * @param The tag being handled. + * @throws javax.swing.text.ChangedCharSetException + */ + protected void handleEmptyTag(TagElement tag) + throws javax.swing.text.ChangedCharSetException + { + } + + /** + * The method is called when the HTML closing tag ((like </table>) + * is found or if the parser concludes that the one should be present + * in the current position. + * @param The tag + */ + protected void handleEndTag(TagElement tag) + { + } + + /* Handle error that has occured in the given line. */ + protected void handleError(int line, String message) + { + } + + /** + * The method is called when the HTML opening tag ((like <table>) + * is found or if the parser concludes that the one should be present + * in the current position. + * @param The tag + */ + protected void handleStartTag(TagElement tag) + { + } + + /** + * Handle the text section. + * <p> For non-preformatted section, the parser replaces + * \t, \r and \n by spaces and then multiple spaces + * by a single space. Additionaly, all whitespace around + * tags is discarded. + * </p> + * <p> For pre-formatted text (inside TEXAREA and PRE), the parser preserves + * all tabs and spaces, but removes <b>one</b> bounding \r, \n or \r\n, + * if it is present. Additionally, it replaces each occurence of \r or \r\n + * by a single \n.</p> + * + * @param text A section text. + */ + protected void handleText(char[] text) + { + } + + /** + * Handle HTML <title> tag. This method is invoked when + * both title starting and closing tags are already behind. + * The passed argument contains the concatenation of all + * title text sections. + * @param The title text. + */ + protected void handleTitle(char[] title) + { + } + + /** + * Constructs the tag from the given element. In this implementation, + * this is defined, but never called. + * @return the tag + */ + protected TagElement makeTag(Element element) + { + return makeTag(element, false); + } + + /** + * Constructs the tag from the given element. + * @param the tag base {@link javax.swing.text.html.parser.Element} + * @param isSupposed true if the tag is not actually present in the + * html input, but the parser supposes that it should to occur in + * the current location. + * @return the tag + */ + protected TagElement makeTag(Element element, boolean isSupposed) + { + return new TagElement(element, isSupposed); + } + + /** + * This is called when the tag, representing the given element, + * occurs first time in the document. + * @param element + */ + protected void markFirstTime(Element element) + { + } + + /** + * Consume the token that was checked before and hence MUST be present. + * @param kind The kind of token to consume. + */ + protected Token mustBe(int kind) + { + if (getTokenAhead().kind == kind) + return getNextToken(); + else + { + String ei = ""; + if (kind < 1000) + ei = " ('" + (char) kind + "') "; + throw new AssertionError("The token of kind " + kind + ei + + " MUST be here," + ); + } + } + + /** + * Handle attribute without value. The default method uses + * the only allowed attribute value from DTD. + * If the attribute is unknown or allows several values, + * the HTML.NULL_ATTRIBUTE_VALUE is used. The attribute with + * this value is added to the attribute set. + * @param element The name of element. + * @param attribute The name of attribute without value. + */ + protected void noValueAttribute(String element, String attribute) + { + Object value = HTML.NULL_ATTRIBUTE_VALUE; + + Element e = (Element) dtd.elementHash.get(element.toLowerCase()); + if (e != null) + { + AttributeList attr = e.getAttribute(attribute); + if (attr != null) + { + Vector values = attr.values; + if (values != null && values.size() == 1) + value = values.get(0); + } + } + attributes.addAttribute(attribute, value); + } + + /** + * Consume the optional token, if present. + * @param kind The kind of token to consume. + */ + protected Token optional(int kind) + { + if (getTokenAhead().kind == kind) + return getNextToken(); + else + return null; + } + + /** Parse the html document. */ + protected void parseDocument() + throws ParseException + { + while (getTokenAhead().kind != EOF) + { + advanced = false; + if (TAG.matches(this)) + Tag(); + else if (COMMENT_OPEN.matches(this)) + Comment(); + else if (STYLE_OPEN.matches(this)) + Style(); + else if (SCRIPT_OPEN.matches(this)) + Script(); + else if (SGML.matches(this)) + Sgml(); + else + CDATA(true); + + // Surely HTML error, treat as a text. + if (!advanced) + { + Token wrong = getNextToken(); + error("unexpected '" + wrong.getImage() + "'", wrong); + buffer.setLength(0); + buffer.append(wrong.getImage()); + _handleText(); + } + } + } + + /** + * Read the element attributes, adding them into attribute set. + * @param element The element name (needed to access attribute + * information in dtd). + */ + protected void readAttributes(String element) + { + Token name; + Token value; + Token next; + String attrValue; + + attributes = new htmlAttributeSet(); + + optional(WS); + + attributeReading: + while (getTokenAhead().kind == NUMTOKEN) + { + name = getNextToken(); + optional(WS); + + next = getTokenAhead(); + if (next.kind == EQ) + { + mustBe(EQ); + optional(WS); + + next = getNextToken(); + + switch (next.kind) + { + case QUOT : + + // read "quoted" attribute. + buffer.setLength(0); + readTillTokenE(QUOT); + attrValue = buffer.toString(); + break; + + case AP : + + // read 'quoted' attribute. + buffer.setLength(0); + readTillTokenE(AP); + attrValue = buffer.toString(); + break; + + // read unquoted attribute. + case NUMTOKEN : + value = next; + optional(WS); + + // Check maybe the opening quote is missing. + next = getTokenAhead(); + if (bQUOTING.get(next.kind)) + { + hTag = next; + error("The value without opening quote is closed with '" + + next.getImage() + "'" + ); + } + attrValue = value.getImage(); + break; + + default : + break attributeReading; + } + attributes.addAttribute(name.getImage(), attrValue); + optional(WS); + } + else // The '=' is missing: attribute without value. + { + noValueAttribute(element, name.getImage()); + } + } + } + + /** + * Return string, corresponding the given named entity. + * The name is passed with the preceeding &, but without + * the ending semicolon. + */ + protected String resolveNamedEntity(final String a_tag) + { + // Discard & + if (!a_tag.startsWith("&")) + throw new AssertionError("Named entity " + a_tag + + " must start witn '&'." + ); + + String tag = a_tag.substring(1); + + try + { + Entity entity = dtd.getEntity(tag); + if (entity != null) + return entity.getString(); + + entity = dtd.getEntity(tag.toLowerCase()); + + if (entity != null) + { + error("The name of this entity should be in lowercase", a_tag); + return entity.getString(); + } + } + catch (IndexOutOfBoundsException ibx) + { + /* The error will be reported. */ + } + + error("Unknown named entity", a_tag); + return a_tag; + } + + /** + * Return char, corresponding the given numeric entity. + * The name is passed with the preceeding &#, but without + * the ending semicolon. + */ + protected char resolveNumericEntity(final String a_tag) + { + // Discard &# + if (!a_tag.startsWith("&#")) + throw new AssertionError("Numeric entity " + a_tag + + " must start witn '&#'." + ); + + String tag = a_tag.substring(2); + + try + { + // Determine the encoding type: + char cx = tag.charAt(0); + if (cx == 'x' || cx == 'X') // Hexadecimal &#Xnnn; + + return (char) Integer.parseInt(tag.substring(1), 16); + + return (char) Integer.parseInt(tag); + } + + /* The error will be reported. */ + catch (NumberFormatException nex) + { + } + catch (IndexOutOfBoundsException ix) + { + } + + error("Invalid numeric entity", a_tag); + return '?'; + } + + /** + * Reset all fields into the intial default state, preparing the + * parset for parsing the next document. + */ + protected void restart() + { + documentTags.clear(); + titleHandled = false; + titleOpen = false; + buffer.setLength(0); + title.setLength(0); + validator.restart(); + } + + /** + * The method is called when the HTML opening tag ((like <table>) + * is found or if the parser concludes that the one should be present + * in the current position. The method is called immediately before + * calling the handleStartTag. + * @param The tag + */ + protected void startTag(TagElement tag) + throws ChangedCharSetException + { + } + + /** + * Handle a complete element, when the tag content is already present in the + * buffer and both starting and heading tags behind. This is called + * in the case when the tag text must not be parsed for the nested + * elements (elements STYLE and SCRIPT). + */ + private void _handleCompleteElement(TagElement tag) + { + _handleStartTag(tag); + + // Suppress inclusion of the SCRIPT ans STYLE texts into the title. + HTML.Tag h = tag.getHTMLTag(); + if (h == HTML.Tag.SCRIPT || h == HTML.Tag.STYLE) + { + boolean tmp = titleOpen; + titleOpen = false; + _handleText(); + titleOpen = tmp; + } + else + _handleText(); + + _handleEndTag(tag); + } + + /** + * A hooks for operations, preceeding call to handleEmptyTag(). + * Handle the tag with no content, like <br>. As no any + * nested tags are expected, the tag validator is not involved. + * @param The tag being handled. + */ + private void _handleEmptyTag(TagElement tag) + { + try + { + validator.validateTag(tag, attributes); + handleEmptyTag(tag); + } + catch (ChangedCharSetException ex) + { + error("Changed charset exception:", ex.getMessage()); + } + } + + /** + * A hooks for operations, preceeding call to handleEndTag(). + * The method is called when the HTML closing tag + * is found. Calls handleTitle after closing the 'title' tag. + * @param The tag + */ + private void _handleEndTag(TagElement tag) + { + validator.closeTag(tag); + _handleEndTag_remaining(tag); + } + + /** + * Actions that are also required if the closing action was + * initiated by the tag validator. + * Package-private to avoid an accessor method. + */ + void _handleEndTag_remaining(TagElement tag) + { + HTML.Tag h = tag.getHTMLTag(); + + handleEndTag(tag); + endTag(tag.fictional()); + + if (h.isPreformatted()) + preformatted--; + if (preformatted < 0) + preformatted = 0; + + if (h == HTML.Tag.TITLE) + { + titleOpen = false; + titleHandled = true; + + char[] a = new char[ title.length() ]; + title.getChars(0, a.length, a, 0); + handleTitle(a); + } + } + + /** + * A hooks for operations, preceeding call to handleStartTag(). + * The method is called when the HTML opening tag ((like <table>) + * is found. + * Package-private to avoid an accessor method. + * @param The tag + */ + void _handleStartTag(TagElement tag) + { + validator.openTag(tag, attributes); + startingTag(tag); + handleStartTag(tag); + + HTML.Tag h = tag.getHTMLTag(); + + if (h.isPreformatted()) + preformatted++; + + if (h == HTML.Tag.TITLE) + { + if (titleHandled) + error("Repetetive <TITLE> tag"); + titleOpen = true; + titleHandled = false; + } + } + + /** + * Resume parsing after heavy errors in HTML tag structure. + * @throws ParseException + */ + private void forciblyCloseTheTag() + throws ParseException + { + int closeAt = 0; + buffer.setLength(0); + + ahead: + for (int i = 1; i < 100; i++) + { + t = getTokenAhead(i - 1); + if (t.kind == EOF || t.kind == BEGIN) + break ahead; + if (t.kind == END) + { + /* Closing '>' found. */ + closeAt = i; + break ahead; + } + } + if (closeAt > 0) + { + buffer.append("Ignoring '"); + for (int i = 1; i <= closeAt; i++) + { + t = getNextToken(); + append(t); + } + buffer.append('\''); + error(buffer.toString()); + } + } + + /** + * Handle comment in string buffer. You can avoid allocating a char + * array each time by processing your comment directly here. + */ + private void handleComment() + { + char[] a = new char[ buffer.length() ]; + buffer.getChars(0, a.length, a, 0); + handleComment(a); + } + + private TagElement makeTagElement(String name, boolean isSupposed) + { + Element e = (Element) dtd.elementHash.get(name.toLowerCase()); + if (e == null) + { + error("Unknown tag <" + name + ">"); + e = dtd.getElement(name); + e.name = name.toUpperCase(); + e.index = -1; + } + + if (!documentTags.contains(e.name)) + { + markFirstTime(e); + documentTags.add(e.name); + } + + return makeTag(e, isSupposed); + } + + /** + * Read till the given token, resolving entities. Consume the given + * token without adding it to buffer. + * @param till The token to read till + * @throws ParseException + */ + private void readTillTokenE(int till) + throws ParseException + { + buffer.setLength(0); + read: + while (true) + { + t = getNextToken(); + if (t.kind == Constants.ENTITY) + { + resolveAndAppendEntity(t); + } + else if (t.kind == EOF) + { + error("unexpected eof", t); + break read; + } + else if (t.kind == till) + break read; + else if (t.kind == WS) + { + // Processing whitespace in accordance with CDATA rules: + String s = t.getImage(); + char c; + for (int i = 0; i < s.length(); i++) + { + c = s.charAt(i); + if (c == '\r') + buffer.append(' '); // CR replaced by space + else if (c == '\n') + ; // LF ignored + else if (c == '\t') + buffer.append(' '); // Tab replaced by space + else + buffer.append(c); + } + } + else + append(t); + } + } + + /** + * Resolve the entity and append it to the end of buffer. + * @param entity + */ + private void resolveAndAppendEntity(Token entity) + { + switch (entity.category) + { + case ENTITY_NAMED : + buffer.append(resolveNamedEntity(entity.getImage())); + break; + + case ENTITY_NUMERIC : + buffer.append(resolveNumericEntity(entity.getImage())); + break; + + default : + throw new AssertionError("Invalid entity category " + + entity.category + ); + } + } + + /** + * Handle the remaining of HTML tags. This is a common end for + * TAG, SCRIPT and STYLE. + * @param closing True for closing tags ( </TAG> ). + * @param name Name of element + * @param start Token where element has started + * @throws ParseException + */ + private void restOfTag(boolean closing, Token name, Token start) + throws ParseException + { + boolean end = false; + Token next; + + optional(WS); + + readAttributes(name.getImage()); + + optional(WS); + + next = getTokenAhead(); + if (next.kind == END) + { + mustBe(END); + end = true; + } + + hTag = new Token(start, next); + + attributes.setResolveParent(defaulter.getDefaultParameters(name.getImage())); + + if (!end) + { + // The tag body contains errors. If additionally the tag + // name is not valid, this construction is treated as text. + if (dtd.elementHash.get(name.getImage().toLowerCase()) == null && + backupMode + ) + { + error("Errors in tag body and unknown tag name. " + + "Treating the tag as a text." + ); + reset(); + + hTag = mustBe(BEGIN); + buffer.setLength(0); + buffer.append(hTag.getImage()); + CDATA(false); + return; + } + else + { + error("Forcibly closing invalid parameter list"); + forciblyCloseTheTag(); + } + } + + if (closing) + { + endTag(false); + _handleEndTag(makeTagElement(name.getImage(), false)); + } + else + { + TagElement te = makeTagElement(name.getImage(), false); + if (te.getElement().type == DTDConstants.EMPTY) + _handleEmptyTag(te); + else + _handleStartTag(te); + } + } + + /** + * This should fire additional actions in response to the + * ChangedCharSetException. The current implementation + * does nothing. + * @param tag + */ + private void startingTag(TagElement tag) + { + try + { + startTag(tag); + } + catch (ChangedCharSetException cax) + { + error("Invalid change of charset"); + } + } + + private void ws_error() + { + error("Whitespace here is not permitted"); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/gnuStringIntMapper.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/gnuStringIntMapper.java new file mode 100644 index 00000000000..9cdf810dd42 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/gnuStringIntMapper.java @@ -0,0 +1,112 @@ +/* gnuStringIntMapper.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +/** + * A helper class, mapping between the strings and they unique integer + * identifiers. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public abstract class gnuStringIntMapper +{ + /** + * Maps argument integer values from DTDConstants into they string + * names. Initialized on demand. + */ + private Map is_Map; + + /** + * Maps argument string names into they integer values from DTDConstants. + * Initialized on demand. + */ + private Map si_Map; + + /** + * Get string from id or null if no such id is present in the mapper. + */ + public final String get(int id) + { + if (is_Map == null) + createTheMap(); + + return (String) is_Map.get(new Integer(id)); + } + + /** Get id from string or 0 if no such string is present in the mapper. */ + public final int get(String id) + { + if (si_Map == null) + createTheMap(); + + Integer i = (Integer) si_Map.get(id); + + return i != null ? i.intValue() : 0; + } + + /** + * Create the mapping table for this mapper by adding the required + * String/int pairs. The method is invoked + * only once for each instance, after the first invocation of the any + * form of the <code>get</code> method. Use <code>add</code> to + * create a map for a concrete instance. + */ + protected abstract void create(); + + /** + * Add an id/string pair to this mapper. This is called from + * the method <code>create</code> only. + */ + protected void add(String name, int id) + { + Integer i = new Integer(id); + si_Map.put(name, i); + is_Map.put(i, name); + } + + private void createTheMap() + { + is_Map = new HashMap(); + si_Map = new TreeMap(); + create(); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Buffer.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Buffer.java new file mode 100644 index 00000000000..a39330af846 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Buffer.java @@ -0,0 +1,238 @@ +/* Buffer.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +/** + * A string buffer that additionally holds line and absolute postion + * information. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Buffer +{ + public static int INITIAL_SIZE = 2048; + + /** + * True if the \n symbol has been seen. + */ + public boolean n_seen; + + /** + * True if the \r symbol has been seen. + */ + public boolean r_seen; + char[] chr = new char[ INITIAL_SIZE ]; + int[] line = new int[ INITIAL_SIZE ]; + int[] position = new int[ INITIAL_SIZE ]; + + /** + * Current line. + */ + int current_line = 0; + + /** + * Point to the next free position. + */ + int length; + + public Buffer() + { + } + + public Buffer(String content) + { + for (int i = 0; i < content.length(); i++) + { + append(content.charAt(i), i); + } + } + + /** + * Get the characters into array. + * @param srcBegin From, inclusive + * @param srcEnd To, exclusive. + * @param dst Into + * @param dstBegin Offset. + */ + public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) + { + System.arraycopy(chr, srcBegin, dst, dstBegin, (srcEnd - srcBegin)); + } + + /** + * Return the sequence, used to separate lines in the document. + * @return one of \n, \r or \r\n. + */ + public String getEndOfLineSequence() + { + if (r_seen && n_seen) + return "\r\n"; + else if (r_seen) + return "\r"; + else + + // This also is returned for single-line document. + return "\n"; + } + + /** + * Truncate. + * @param n The length to truncate till. + */ + public void setLength(int n) + { + length = n; + } + + /** + * Get location information for the given region. + * @param from Region start, inclusive. + * @param to Region end, exclusive. + * @return The location, covering the region. + */ + public Location getLocation(int from, int to) + { + Location l = new Location(); + l.beginLine = line [ from ]; + l.endLine = line [ to - 1 ]; + + l.startPosition = position [ from ]; + l.endPosition = position [ to - 1 ] + 1; + + return l; + } + + /** + * Add the character. + * @param c The character. + * @param pos The character position in the stream (the line number + * is handled internally in the buffer). + */ + public void append(char c, int pos) + { + if (length >= chr.length) + expand(); + chr [ length ] = c; + position [ length ] = pos; + + if (c == '\n') + { + if (!r_seen) + current_line++; + n_seen = true; + } + else if (c == '\r') + { + current_line++; + r_seen = true; + } + + line [ length ] = current_line; + + length++; + } + + /** + * Return char at the given positon. + */ + public char charAt(int i) + { + return chr [ i ]; + } + + /** + * Delete the range + * @param from Start position, inclusive. + * @param to End position, exclusive. + */ + public void delete(int from, int to) + { + int len = to - from; + if (len < 1) + throw new AssertionError("Deleting " + from + " till " + to); + + int tail = length - to; + + System.arraycopy(chr, to, chr, from, tail); + System.arraycopy(position, to, position, from, tail); + System.arraycopy(line, to, line, from, tail); + length = length - len; + } + + /** + * Double the buffer size. + */ + public void expand() + { + int nSize = 2 * chr.length; + + char[] nchr = new char[ nSize ]; + int[] nposition = new int[ nSize ]; + int[] nline = new int[ nSize ]; + + System.arraycopy(chr, 0, nchr, 0, chr.length); + System.arraycopy(position, 0, nposition, 0, position.length); + System.arraycopy(line, 0, nline, 0, line.length); + + chr = nchr; + position = nposition; + line = nline; + } + + /** + * Return length of the occupied part of the buffer. + */ + public int length() + { + return length; + } + + /** + * Prepare for parsing the new document. + */ + public void reset() + { + setLength(0); + r_seen = n_seen = false; + } + + public String toString() + { + return new String(chr, 0, length); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Constants.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Constants.java new file mode 100644 index 00000000000..283d32385ef --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Constants.java @@ -0,0 +1,422 @@ +/* Constants.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +import java.util.BitSet; + +/** + * The parser constants and operations, directly related to the parser + * constants. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Constants +{ + /* Single character tokens are reflected into they ASCII codes. */ + + /** + * Start of HTML token. + */ + public static final int BEGIN = '<'; + + /** + * End of HTML token. + */ + public static final int END = '>'; + + /** + * Exclamation (indicates SGML or comment). + */ + public static final int EXCLAMATION = '!'; + + /** + * Slash (indicates closing tag). + */ + public static final int SLASH = '/'; + + /** + * Equals sign. + */ + public static final int EQ = '='; + + /** + * Quoting sign. + */ + public static final int AP = '\''; + + /** + * Quoting sign. + */ + public static final int QUOT = '"'; + + /* The numbers of other tokens start outside the ascii space. */ + /* String tokens */ + + /** + * Double dash (--) + */ + public static final int DOUBLE_DASH = 1000; + + /** + * The STYLE tag (needs special handling). + */ + public static final int STYLE = 1001; + + /** + * The SCRIPT tag (needs special handling). + */ + public static final int SCRIPT = 1002; + + /* Pattern tokens */ + + /** + * HTML whitespace. + */ + public static final int WS = 1003; + + /** + * Named or numeric entity, + */ + public static final int ENTITY = 1004; + + /** + * Sequence of valid name characters (can start from digit). + */ + public static final int NUMTOKEN = 1005; + + /* Complex tokens */ + + /** + * Comment opening sequence. + */ + public static final pattern COMMENT_OPEN = + new pattern(new node[] + { + new node(BEGIN), new node(WS, true), new node(EXCLAMATION), + new node(WS, true), new node(DOUBLE_DASH), + } + ); + + /** + * Comment closing sequence + */ + public static final pattern COMMENT_END = + new pattern(new node[] + { + new node(DOUBLE_DASH), new node(WS, true), new node(END) + } + ); + + /** + * Special case ---> (also is treated as end of comment). + */ + public static final pattern COMMENT_TRIPLEDASH_END = + new pattern(new node[] + { + new node(DOUBLE_DASH), new node(NUMTOKEN), new node(END) + } + ); + + /** + * STYLE element heading pattern. + */ + public static final pattern STYLE_OPEN = + new pattern(new node[] { new node(BEGIN), new node(WS, true), new node(STYLE) }); + + /** + * SCRIPT element heading pattern. + */ + public static final pattern SCRIPT_OPEN = + new pattern(new node[] { new node(BEGIN), new node(WS, true), new node(SCRIPT) }); + + /** + * SGML element heading pattern. + */ + public static final pattern SGML = + new pattern(new node[] + { + new node(BEGIN), new node(WS, true), new node(EXCLAMATION) + } + ); + + /** + * SCRIPT element closing pattern. + */ + public static final pattern SCRIPT_CLOSE = + new pattern(new node[] + { + new node(BEGIN), new node(WS, true), new node(SLASH), + new node(WS, true), new node(SCRIPT), new node(WS, true), + new node(END) + } + ); + + /** + * STYLE element closing pattern. + */ + public static final pattern STYLE_CLOSE = + new pattern(new node[] + { + new node(BEGIN), new node(WS, true), new node(SLASH), + new node(WS, true), new node(STYLE), new node(WS, true), + new node(END) + } + ); + + /** + * Ordinary HTML tag heading pattern. + */ + public static final pattern TAG = + new pattern(new node[] + { + new node(BEGIN), new node(WS, true), new node(SLASH, true), + new node(WS, true), new node(NUMTOKEN) + } + ); + + /* Special tokens */ + + /** + * All other tokens. + */ + public static final int OTHER = 1999; + + /** + * The UNICODE "end of text" control code + */ + static final char ETX = 3; + + /** + * End of file. + */ + public static final int EOF = ETX; + + /* Character categories */ + + /** + * All single char tokens. + */ + public static final BitSet bSINGLE_CHAR_TOKEN = new BitSet(); + + /** + * Non letters and non numbers, allowed in HTML names. + */ + public static final BitSet bSPECIAL = new BitSet(); + + /** + * All letters, used in HTML names. + */ + public static final BitSet bLETTER = new BitSet(); + + /** + * Digits. + */ + public static final BitSet bDIGIT = new BitSet(); + + /** + * Both line breaks. + */ + public static final BitSet bLINEBREAK = new BitSet(); + + /** + * All whitespace. + */ + public static final BitSet bWHITESPACE = new BitSet(); + + /** + * Both quoting characters. + */ + public static final BitSet bQUOTING = new BitSet(); + + /** + * Valid name characters. + */ + public static final BitSet bNAME = new BitSet(); + + /* Entity subcategories */ + + /** + * Named entity. + */ + public static final int ENTITY_NAMED = 1; + + /** + * Numeric entity. + */ + public static final int ENTITY_NUMERIC = 2; + + static + { + bQUOTING.set(AP); + bQUOTING.set(QUOT); + + bSINGLE_CHAR_TOKEN.set(BEGIN); + bSINGLE_CHAR_TOKEN.set(END); + bSINGLE_CHAR_TOKEN.set(EXCLAMATION); + bSINGLE_CHAR_TOKEN.set(SLASH); + bSINGLE_CHAR_TOKEN.set(EQ); + bSINGLE_CHAR_TOKEN.set(EOF); + + bSINGLE_CHAR_TOKEN.or(bQUOTING); + + bLINEBREAK.set('\r'); + bLINEBREAK.set('\n'); + + bWHITESPACE.set(' '); + bWHITESPACE.set('\t'); + bWHITESPACE.set(0xC); + bWHITESPACE.or(bLINEBREAK); + + for (char i = '0'; i <= '9'; i++) + { + bDIGIT.set(i); + } + + for (char i = 'a'; i <= 'z'; i++) + { + bLETTER.set(i); + } + + for (char i = 'A'; i <= 'Z'; i++) + { + bLETTER.set(i); + } + + bSPECIAL.set('-'); + bSPECIAL.set('_'); + bSPECIAL.set(':'); + bSPECIAL.set('.'); + + bNAME.or(bLETTER); + bNAME.or(bDIGIT); + bNAME.or(bSPECIAL); + } + + /** + * Verifies if one of the tokens matches the end of string + * buffer. The last character in the string buffer is the + * "future character", some tokens needs to verify it the + * token does not continue "towards the future". If the token + * matches, it matches till "pre-last" character in the buffer. + * @param b + * @return + */ + public Token endMatches(Buffer b) + { + if (b.length() < 2) + return null; + + int p = b.length() - 2; + + if (b.length() > 2 && b.charAt(p) == '-' && b.charAt(p - 1) == '-') + return new Token(DOUBLE_DASH, "--", b.getLocation(p - 1, p + 1)); + + char last = b.charAt(p); + + if (bSINGLE_CHAR_TOKEN.get(last)) + return new Token(last, last, b.getLocation(p, p + 1)); + + char future = b.charAt(p + 1); + + // Check for numtokens, script and style: + if (bNAME.get(last) && !bNAME.get(future)) + { + // Scan the history up: + int u = p - 1; + while (u >= 0 && bNAME.get(b.charAt(u))) + u--; + u++; + + char[] token = new char[ p - u + 1 ]; + + // Found a numtoken + b.getChars(u, p + 1, token, 0); + + // Verify for the built-in tokens: + String e = new String(token); + + // found the entity reference + if (u > 0 && b.charAt(u - 1) == '&') + { + // The subsequent semicolon may be the part of the token + // as well. The semicolon must be ignored. This must be + // handled elsewhere. + return new Token(ENTITY, ENTITY_NAMED, "&" + e, + b.getLocation(u - 1, p + 1) + ); + } + + // found the numeric entity reference + if (u > 1 && b.charAt(u - 1) == '#' && b.charAt(u - 2) == '&') + { + // The subsequent semicolon may be the part of the token + // as well. The semicolon must be ignored. This must be + // handled elsewhere. + return new Token(ENTITY, ENTITY_NUMERIC, "&#" + e, + b.getLocation(u - 2, p + 2) + ); + } + + Location le = b.getLocation(u, p + 1); + + if (e.equalsIgnoreCase("SCRIPT")) + return new Token(SCRIPT, e, le); + else if (e.equalsIgnoreCase("STYLE")) + return new Token(STYLE, e, le); + else + return new Token(NUMTOKEN, e, le); + } + + // Check for whitespace + if (bWHITESPACE.get(last) && !bWHITESPACE.get(future)) + { + // Scan the history up: + int u = p - 1; + while (u >= 0 && bWHITESPACE.get(b.charAt(u))) + u--; + u++; + + char[] token = new char[ p - u + 1 ]; + b.getChars(u, p + 1, token, 0); + + return new Token(WS, new String(token), b.getLocation(u, p + 1)); + } + + return null; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Location.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Location.java new file mode 100644 index 00000000000..8a1cde1c86c --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Location.java @@ -0,0 +1,83 @@ +/* Location.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +/** + * Defines a region in the text: its bounding positions and the line number. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Location +{ + /** + * The line number, where the token starts. + */ + public int beginLine; + + /** + * The line, where the token ends. + */ + public int endLine; + + /** + * The absolute token end position in the input stream, + * exclusive. + */ + public int endPosition; + + /** + * The absolute token start position in the input stream, + * inclusive. + */ + public int startPosition; + + public Location() + { + } + + /** + * Special case, used to mark EOF. + * @param p The total stream length. + */ + public Location(int p) + { + startPosition = p; + endPosition = p + 1; + beginLine = endLine = -1; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/ParseException.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/ParseException.java new file mode 100644 index 00000000000..e71c0c1f651 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/ParseException.java @@ -0,0 +1,51 @@ +/* ParseException.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +/** + * This can be thrown from various parsing methods. + */ +public class ParseException + extends RuntimeException +{ + public ParseException(String s, Throwable cause) + { + super(s, cause); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Queue.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Queue.java new file mode 100644 index 00000000000..31cf4bb4d59 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Queue.java @@ -0,0 +1,142 @@ +/* Queue.java -- a token queue. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +import java.util.Arrays; + +/** + * A token queue. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Queue +{ + Token[] m = new Token[ 64 ]; + int a = 0; + int b = 0; + + /** + * True for the empty queue. + */ + public boolean isEmpty() + { + return size() == 0; + } + + /** + * Add this trace to the end of the queue. + */ + public void add(Token u) + { + if (a < m.length) + { + m [ a ] = u; + a++; + } + else // The end of array has been reached. + { + if (b > 0) // If some elements were deleted from the start of the queue, shift. + { + int d = b; + System.arraycopy(m, b, m, 0, a - b); + b = b - d; + a = a - d; + m [ a ] = u; + a++; + } + else // Enlarge the queue, doubling the size. + { + int n = m.length * 2; + Token[] nm = new Token[ 2 * n ]; + System.arraycopy(m, 0, nm, 0, m.length); + Arrays.fill(m, null); + + nm [ a ] = u; + m = nm; + a++; + } + } + } + + /** + * Clear the queue. + */ + public void clear() + { + a = b = 0; + Arrays.fill(m, null); + } + + /** + * Read the value ahead. 0 is the value that will be returned with + * the following next. This method does not remove values from the + * queue. To test if there is enough tokens in the queue, size() must + * be checked before calling this method. + */ + public Token get(int ahead) + { + int p = b + ahead; + if (p < a) + return m [ p ]; + else + throw new ArrayIndexOutOfBoundsException("Not enough tokens"); + } + + /** + * Read the oldest value from the queue and remove this value from + * the queue. + */ + public Token next() + { + if (a == b) + throw new ArrayIndexOutOfBoundsException("queue empty"); + + Token r = m [ b ]; + m [ b ] = null; + b++; + return r; + } + + /** + * Size of the queue. + */ + public int size() + { + return a - b; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/ReaderTokenizer.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/ReaderTokenizer.java new file mode 100644 index 00000000000..0ffc6c8b754 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/ReaderTokenizer.java @@ -0,0 +1,374 @@ +/* ReaderTokenizer.java -- splits the input char sequence int tokens. + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +import java.io.IOException; +import java.io.Reader; + +/** + * Reader splits the input char sequence into tokens. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class ReaderTokenizer + extends Constants +{ + /** + * This is set to true each time the getNextToken is called. + * Used in preventing loops when all patterns refuse to accept + * the invalid input. + */ + protected boolean advanced; + + /** + * If true, the returned tokens are also placed in the backup + * queue. + */ + protected boolean backupMode; + + /** + * The buffer to read document into. + */ + Buffer buffer = new Buffer(); + + /** + * The queue for supporting mark(). + */ + Queue backup = new Queue(); + + /** + * The queue of found tokens. + */ + Queue queue = new Queue(); + + /** + * The reader to read the document from. + */ + Reader reader; + + /** + * Array of char tokens + */ + char[] charTokens; + + /** + * Array of string tokens. + */ + String[] stringTokens; + + /** + * The current reader position. + */ + int readerPosition = -1; + + /** + * Creates a new ReaderTokenizer. The reset(...) method must be + * subsequently called to set the reader. + */ + public ReaderTokenizer() + { + } + + /** + * Return the sequence, used to separate lines in the document. + * @return one of \n, \r or \r\n. + */ + public String getEndOfLineSequence() + { + return buffer.getEndOfLineSequence(); + } + + /** + * Get the next token. + * @return + */ + public Token getNextToken() + { + Token rt; + advanced = true; + try + { + if (queue.isEmpty()) + read(1); + + if (!queue.isEmpty()) + rt = queue.next(); + else + rt = new Token(EOF, new Location(readerPosition)); + } + catch (IOException ex) + { + throw new ParseException("IO Exception", ex); + } + if (backupMode) + backup.add(rt); + return rt; + } + + /** + * Get a token, lying the given number of tokens + * ahead. getToken(0) will return the same token, + * what would be returned by getNextToken(). + * getToken(..) does change the current position + * in the input stream. If the end of stream is + * reached, the EOF token is always returned. + */ + public Token getTokenAhead(int ahead) + { + try + { + read(ahead - queue.size() + 1); + return queue.size() >= ahead ? queue.get(ahead) : eofToken(); + } + catch (IOException ex) + { + throw new ParseException("IO Exception", ex); + } + } + + /** + * Get a token, bein immediatley ahead. + * If the end of stream is + * reached, the EOF token is always returned. + * The method is equivalent calling getTokenAhead(0). + */ + public Token getTokenAhead() + { + try + { + if (queue.isEmpty()) + read(1); + if (!queue.isEmpty()) + return queue.get(0); + else + return eofToken(); + } + catch (IOException ex) + { + throw new ParseException("IO Exception", ex); + } + } + + /** + * Invokes the error handler. + */ + public void error(String msg, Token at) + { + System.out.println(msg); + } + + /** + * Turns the backup mode on or off. + * It is possible to return where the mark(true) was last called + * by calling reset(). + * @param mode True if it is required to save tokens, making + * returning to the current point possible. + */ + public void mark(boolean mode) + { + backup.clear(); + backupMode = mode; + } + + /** + * Prepare for new parsing from the given stream. + * @param a_reader A reader to parse from. + */ + public void reset(Reader a_reader) + { + reader = a_reader; + readerPosition = -1; + buffer.reset(); + queue.clear(); + } + + /** + * Reset the internal cursor to the position where the mark() + * was last time called. Switches the backup mode off. + */ + public void reset() + { + if (!backupMode) + throw new AssertionError("Call mark(true) before using reset()!"); + backupMode = false; + + // That is now in the queue, will be appended to the end of backup. + while (!queue.isEmpty()) + backup.add(queue.next()); + + Queue t = queue; + queue = backup; + backup = t; + backup.clear(); + } + + /** + * Read the given number of the tokens. Add the needed number of EOF + * tokens if there are no more data in the stream. + * @param amount The number of additional tokens to read. + */ + void read(int numberOfTokens) + throws IOException + { + if (numberOfTokens <= 0) + return; + + reading: + for (int i = 0; i < numberOfTokens; i++) + readToken(); + } + + /** + * Read next token from the reader, add it to the queue + */ + void readToken() + throws IOException + { + Token t; + int ch; + + enlarging: + while (true) + { + t = tokenMatches(); + if (t != null) + break enlarging; + else + { + ch = reader.read(); + readerPosition++; + if (ch == ETX) + ch = ' '; + if (ch < 0) + { + if (buffer.length() == 0) + { + queue.add(eofToken()); + return; + } + else + { + if (buffer.charAt(buffer.length() - 1) != ETX) + buffer.append(ETX, readerPosition++); + else + { + // Discard terminating ETX + buffer.setLength(buffer.length() - 1); + if (buffer.length() > 0) + { + t = new Token(OTHER, buffer.toString(), + buffer.getLocation(0, buffer.length()) + ); + queue.add(t); + buffer.setLength(0); + } + return; + } + } + } + else + buffer.append((char) ch, readerPosition); + } + } + } + + /** + * Check if the end of buffer matches one of the tokens. If it does, + * return this token and remove the token sequence from the end of + * buffer. + * @return The matching token. + */ + Token tokenMatches() + { + Token rt = endMatches(buffer); + if (rt != null) // Remove the matched image + { + // Consume future character if it was an entity and the future + // character is semicolon. + if (rt.kind == ENTITY) + { + if (buffer.charAt(buffer.length() - 1) == ';') + buffer.setLength(buffer.length() - rt.getImage().length() - 1); + else + { + error("Missing closing semicolon for entity '" + rt.getImage() + + "'", rt + ); + consumeBuffer(rt); + } + } + else + { + consumeBuffer(rt); + } + } + + // If the buffer is not empty, some sequence does not match any tokens. + // Add it to the queue as "OTHER". + if (rt != null) + { + if (buffer.length() > 1) + { + String rest = buffer.toString(); + rest = rest.substring(0, rest.length() - 1); + + Token other = + new Token(OTHER, rest, buffer.getLocation(0, buffer.length)); + queue.add(other); + consumeBuffer(other); + } + queue.add(rt); + } + return rt; + } + + private void consumeBuffer(Token rt) + { + buffer.delete(buffer.length() - rt.getImage().length() - 1, + buffer.length() - 1 + ); + } + + /** + * Create EOF token. + */ + private Token eofToken() + { + return new Token(EOF, "#", new Location(readerPosition)); + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Token.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Token.java new file mode 100644 index 00000000000..d91adf47ab8 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/Token.java @@ -0,0 +1,169 @@ +/* Token.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +/** + * A token. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class Token +{ + /** + * The place of this token in the document. + */ + public Location where; + + /** + * The additional category of token. + */ + public int category; + + /** + * An integer that describes the kind of this token. + */ + public int kind; + + /** + * The string image of the token, null if the char image must be used. + */ + private String stringImage; + + /** + * The char image of the token. + */ + private char charImage; + + /** + * Creates a new token with fields, initialized to the default values. + */ + public Token() + { + } + + /** + * Creates a new token of the given kind. + */ + public Token(int _kind, Location _where) + { + kind = _kind; + where = _where; + } + + /** + * Creates a new token of the given kind and given single char image. + */ + public Token(int _kind, char _image, Location _where) + { + kind = _kind; + charImage = _image; + where = _where; + } + + /** + * Creates a new token of the given kind and given string image. + */ + public Token(int _kind, String _image, Location _where) + { + kind = _kind; + stringImage = _image; + where = _where; + } + + /** + * Creates a new token of the given kind, category and given string image. + */ + public Token(int _kind, int _category, String _image, Location _where) + { + kind = _kind; + category = _category; + stringImage = _image; + where = _where; + } + + /** + * Creates a new token, where location fields are set as for token, + * spanning over two provided tokens and any tokens between them. + * The image field is initialized to null, the kind field is set to -1. + */ + public Token(Token fromInclusive, Token toInclusive) + { + where = new Location(); + where.beginLine = fromInclusive.where.beginLine; + where.startPosition = fromInclusive.where.startPosition; + + where.endLine = toInclusive.where.endLine; + where.endPosition = toInclusive.where.endPosition; + } + + public String getImage() + { + if (kind == 3) + return "#"; + if (stringImage == null) + { + if (charImage == 0) + return null; + stringImage = new String(new char[] { charImage }); + } + return stringImage; + } + + /** + * Append the token image to the given string buffer. + * This may be more effective that buffer.append(this.getImage()). + * @param buffer A buffer to append. + */ + public void appendTo(StringBuffer buffer) + { + if (charImage == 0) + buffer.append(getImage()); + else + buffer.append(charImage); + } + + /** + * Returns the string image or, if null, the bounding positions. + */ + public String toString() + { + return getImage() != null ? kind + "'" + getImage() + : "<line " + where.beginLine + ", abs pos " + where.startPosition + + ".." + where.endPosition + ">"; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/node.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/node.java new file mode 100644 index 00000000000..b54ed86a381 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/node.java @@ -0,0 +1,78 @@ +/* node.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + +/** + * A text level content model node. The only required unary operations + * here are "appears" and "optionally appears" ('?'). + * <p>@author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)</p> + */ +public class node +{ + /** + * True for node that is optional for the given position. + */ + public boolean optional; + + /** + * The kind of the token to match. + */ + public int kind; + + /** + * Creates the new node for matching a given kind of the token. + * @param kind The kind of the token to match. + * @param modifier The modifier (*?+). + */ + public node(int kind, boolean _optional) + { + this.kind = kind; + optional = _optional; + } + + /** + * Creates the node, indicating that token must match exactluy one time. + * @param kind The kind of token to match. + */ + public node(int kind) + { + this.kind = kind; + optional = false; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/package.html b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/package.html new file mode 100644 index 00000000000..17358301530 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/package.html @@ -0,0 +1,47 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in javax.swing.text.html.parser package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.javax.swing.text.html.parser.support.low</title></head> + +<body> +<p>This package contains classes that are directly used to process +the text input: adapted stream tokenizer, specialized buffer and text-level content models .</p> +@author Audrius Meskauskas, Lithuania +</body> +</html> diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/pattern.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/pattern.java new file mode 100644 index 00000000000..0fe03fdbe87 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/low/pattern.java @@ -0,0 +1,105 @@ +/* pattern.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support.low; + + +/** + * The simple pattern, consisting from the sequence of tokens that + * may have the unary modifier '?'. Choices and grouping + * are not required here. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class pattern +{ + /** + * The nodes of this pattern. + */ + public final node[] nodes; + + /** + * Create a pattern, containing the given list of nodes. + * @param a_nodes + */ + public pattern(node[] a_nodes) + { + nodes = a_nodes; + } + + /** + * Checks if the pattern can match the tokens in this + * tokenizer. Does not change the state of tokenizer. + * @param stream The tokenizer to read data from + * @return True if the pattern sequence matches the + * beginning of the tokenizer content. + */ + public boolean matches(ReaderTokenizer stream) + { + try + { + int pt = 0; + int pn = 0; + Token t; + node n; + + while (pn < nodes.length) + { + n = nodes [ pn ]; + t = stream.getTokenAhead(pt); + + if (t.kind == n.kind) + { + pn++; + pt++; + } + else + { + if (!n.optional) + return false; + else + pn++; + } + } + return true; + } + catch (Exception ex) + { + throw new ParseException("Exception", ex); + } + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/package.html b/libjava/classpath/gnu/javax/swing/text/html/parser/support/package.html new file mode 100644 index 00000000000..97c6439b3fe --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/package.html @@ -0,0 +1,47 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- package.html - describes classes in javax.swing.text.html.parser package. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. --> + +<html> +<head><title>GNU Classpath - gnu.javax.swing.text.html.parser.support</title></head> + +<body> +<p>This package provides various specialised classes, needed by HTML parser. +</p> +@author Audrius Meskauskas, Lithuania +</body> +</html> diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/parameterDefaulter.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/parameterDefaulter.java new file mode 100644 index 00000000000..654acbbb58c --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/parameterDefaulter.java @@ -0,0 +1,106 @@ +/* parameterDefaulter.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support; + +import gnu.javax.swing.text.html.parser.htmlAttributeSet; + +import java.util.Hashtable; + +import javax.swing.text.html.parser.AttributeList; +import javax.swing.text.html.parser.DTD; +import javax.swing.text.html.parser.Element; + +/** + * Returns an attribute set, containing default + * parameters for the given element. Caches sets of default + * parameters. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class parameterDefaulter +{ + public final DTD dtd; + Hashtable sets = new Hashtable(); + + /** + * Create a parameterDefaulter that looks for the default attribute + * values in the given DTD. + * @param a_dtd + */ + public parameterDefaulter(DTD a_dtd) + { + dtd = a_dtd; + } + + /** + * Get the default parameter set for the given element. + * @param element The element name (case insensitive). + * @return the default attrbute set. + */ + public htmlAttributeSet getDefaultParameters(String element) + { + String key = element.toLowerCase(); + htmlAttributeSet atts = (htmlAttributeSet) sets.get(key); + + if (atts == null) + { + htmlAttributeSet set = new htmlAttributeSet(); + Element e = (Element) dtd.elementHash.get(element.toLowerCase()); + + if (e != null) + { + AttributeList a = e.getAttributes(); + + while (a != null) + { + if (a.value != null) + set.addAttribute(a.name, a.value); + a = a.next; + } + } + + if (set.getAttributeCount() > 0) + sets.put(key, set); + else + sets.put(key, htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET); + + atts = set; + } + return atts; + } +} diff --git a/libjava/classpath/gnu/javax/swing/text/html/parser/support/textPreProcessor.java b/libjava/classpath/gnu/javax/swing/text/html/parser/support/textPreProcessor.java new file mode 100644 index 00000000000..cc1610585a6 --- /dev/null +++ b/libjava/classpath/gnu/javax/swing/text/html/parser/support/textPreProcessor.java @@ -0,0 +1,193 @@ +/* textPreProcessor.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.swing.text.html.parser.support; + +import gnu.javax.swing.text.html.parser.support.low.Constants; + +/** + * Pre - processes text in text parts of the html document. + * Not thread - safe. + * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) + */ +public class textPreProcessor +{ + /** + * Pre - process non-preformatted text. + * \t, \r and \n mutate into spaces, then multiple spaces mutate + * into single one, all whitespace around tags is consumed. + * The content of the passed buffer is destroyed. + * @param text A text to pre-process. + */ + public char[] preprocess(StringBuffer a_text) + { + if (a_text.length() == 0) + return null; + + char[] text = toCharArray(a_text); + + int a = 0; + int b = text.length - 1; + + try + { + while (Constants.bWHITESPACE.get(text [ a ])) + a++; + while (Constants.bWHITESPACE.get(text [ b ])) + b--; + } + catch (ArrayIndexOutOfBoundsException sx) + { + // A text fragment, consisting from line breaks only. + return null; + } + + a_text.setLength(0); + + boolean spacesWere = false; + boolean spaceNow; + char c; + + chars: + for (int i = a; i <= b; i++) + { + c = text [ i ]; + spaceNow = Constants.bWHITESPACE.get(c); + if (spacesWere && spaceNow) + continue chars; + if (spaceNow) + a_text.append(' '); + else + a_text.append(c); + spacesWere = spaceNow; + } + + if (a_text.length() == text.length) + { + a_text.getChars(0, a_text.length(), text, 0); + return text; + } + else + return toCharArray(a_text); + } + + /** + * Pre - process pre-formatted text. + * Heading/closing spaces and tabs preserved. + * ONE bounding \r, \n or \r\n is removed. + * \r or \r\n mutate into \n. Tabs are + * preserved. + * The content of the passed buffer is destroyed. + * @param text + * @return + */ + public char[] preprocessPreformatted(StringBuffer a_text) + { + if (a_text.length() == 0) + return null; + + char[] text = toCharArray(a_text); + + int a = 0; + int n = text.length - 1; + int b = n; + + if (text [ 0 ] == '\n') + a++; + else + { + if (text [ 0 ] == '\r') + { + a++; + if (text.length > 1 && text [ 1 ] == '\n') + a++; + } + } + + if (text [ n ] == '\r') + b--; + else + { + if (text [ n ] == '\n') + { + b--; + if (n > 0 && text [ n - 1 ] == '\r') + b--; + } + } + + a_text.setLength(0); + + if (a > b) + return null; + + char c; + + for (int i = a; i <= b; i++) + { + c = text [ i ]; + if (c == '\r') + { + if (i == b || text [ i + 1 ] != '\n') + a_text.append('\n'); + } + else + a_text.append(c); + } + + if (a_text.length() == text.length) + { + a_text.getChars(0, a_text.length(), text, 0); + return text; + } + else + return toCharArray(a_text); + } + + /** + * Return array of chars, present in the given buffer. + * @param a_text The buffer + * @return + */ + private static char[] toCharArray(StringBuffer a_text) + { + char[] text = new char[ a_text.length() ]; + a_text.getChars(0, text.length, text, 0); + return text; + } +} diff --git a/libjava/classpath/gnu/regexp/CharIndexed.java b/libjava/classpath/gnu/regexp/CharIndexed.java new file mode 100644 index 00000000000..a0d7106aefa --- /dev/null +++ b/libjava/classpath/gnu/regexp/CharIndexed.java @@ -0,0 +1,84 @@ +/* gnu/regexp/CharIndexed.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +/** + * Defines the interface used internally so that different types of source + * text can be accessed in the same way. Built-in concrete classes provide + * support for String, StringBuffer, InputStream and char[] types. + * A class that is CharIndexed supports the notion of a cursor within a + * block of text. The cursor must be able to be advanced via the move() + * method. The charAt() method returns the character at the cursor position + * plus a given offset. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + */ +public interface CharIndexed { + /** + * Defines a constant (0xFFFF was somewhat arbitrarily chosen) + * that can be returned by the charAt() function indicating that + * the specified index is out of range. + */ + char OUT_OF_BOUNDS = '\uFFFF'; + + /** + * Returns the character at the given offset past the current cursor + * position in the input. The index of the current position is zero. + * It is possible for this method to be called with a negative index. + * This happens when using the '^' operator in multiline matching mode + * or the '\b' or '\<' word boundary operators. In any case, the lower + * bound is currently fixed at -2 (for '^' with a two-character newline). + * + * @param index the offset position in the character field to examine + * @return the character at the specified index, or the OUT_OF_BOUNDS + * character defined by this interface. + */ + char charAt(int index); + + /** + * Shifts the input buffer by a given number of positions. Returns + * true if the new cursor position is valid. + */ + boolean move(int index); + + /** + * Returns true if the most recent move() operation placed the cursor + * position at a valid position in the input. + */ + boolean isValid(); +} diff --git a/libjava/classpath/gnu/regexp/CharIndexedCharArray.java b/libjava/classpath/gnu/regexp/CharIndexedCharArray.java new file mode 100644 index 00000000000..63d858c8709 --- /dev/null +++ b/libjava/classpath/gnu/regexp/CharIndexedCharArray.java @@ -0,0 +1,62 @@ +/* gnu/regexp/CharIndexedCharArray.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.Serializable; + +class CharIndexedCharArray implements CharIndexed, Serializable { + private char[] s; + private int anchor; + + CharIndexedCharArray(char[] str, int index) { + s = str; + anchor = index; + } + + public char charAt(int index) { + int pos = anchor + index; + return ((pos < s.length) && (pos >= 0)) ? s[pos] : OUT_OF_BOUNDS; + } + + public boolean isValid() { + return (anchor < s.length); + } + + public boolean move(int index) { + return ((anchor += index) < s.length); + } +} diff --git a/libjava/classpath/gnu/regexp/CharIndexedInputStream.java b/libjava/classpath/gnu/regexp/CharIndexedInputStream.java new file mode 100644 index 00000000000..145fe11b135 --- /dev/null +++ b/libjava/classpath/gnu/regexp/CharIndexedInputStream.java @@ -0,0 +1,149 @@ +/* gnu/regexp/CharIndexedInputStream.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +// TODO: move(x) shouldn't rely on calling next() x times + +class CharIndexedInputStream implements CharIndexed { + private static final int BUFFER_INCREMENT = 1024; + private static final int UNKNOWN = Integer.MAX_VALUE; // value for end + + private BufferedInputStream br; + + // so that we don't try to reset() right away + private int index = -1; + + private int bufsize = BUFFER_INCREMENT; + + private int end = UNKNOWN; + + private char cached = OUT_OF_BOUNDS; + + // Big enough for a \r\n pair + // lookBehind[0] = most recent + // lookBehind[1] = second most recent + private char[] lookBehind = new char[] { OUT_OF_BOUNDS, OUT_OF_BOUNDS }; + + CharIndexedInputStream(InputStream str, int index) { + if (str instanceof BufferedInputStream) br = (BufferedInputStream) str; + else br = new BufferedInputStream(str,BUFFER_INCREMENT); + next(); + if (index > 0) move(index); + } + + private boolean next() { + if (end == 1) return false; + end--; // closer to end + + try { + if (index != -1) { + br.reset(); + } + int i = br.read(); + br.mark(bufsize); + if (i == -1) { + end = 1; + cached = OUT_OF_BOUNDS; + return false; + } + cached = (char) i; + index = 1; + } catch (IOException e) { + e.printStackTrace(); + cached = OUT_OF_BOUNDS; + return false; + } + return true; + } + + public char charAt(int index) { + if (index == 0) { + return cached; + } else if (index >= end) { + return OUT_OF_BOUNDS; + } else if (index == -1) { + return lookBehind[0]; + } else if (index == -2) { + return lookBehind[1]; + } else if (index < -2) { + return OUT_OF_BOUNDS; + } else if (index >= bufsize) { + // Allocate more space in the buffer. + try { + while (bufsize <= index) bufsize += BUFFER_INCREMENT; + br.reset(); + br.mark(bufsize); + br.skip(index-1); + } catch (IOException e) { } + } else if (this.index != index) { + try { + br.reset(); + br.skip(index-1); + } catch (IOException e) { } + } + char ch = OUT_OF_BOUNDS; + + try { + int i = br.read(); + this.index = index+1; // this.index is index of next pos relative to charAt(0) + if (i == -1) { + // set flag that next should fail next time? + end = index; + return ch; + } + ch = (char) i; + } catch (IOException ie) { } + + return ch; + } + + public boolean move(int index) { + // move read position [index] clicks from 'charAt(0)' + boolean retval = true; + while (retval && (index-- > 0)) retval = next(); + return retval; + } + + public boolean isValid() { + return (cached != OUT_OF_BOUNDS); + } +} + diff --git a/libjava/classpath/gnu/regexp/CharIndexedString.java b/libjava/classpath/gnu/regexp/CharIndexedString.java new file mode 100644 index 00000000000..05be07ac68c --- /dev/null +++ b/libjava/classpath/gnu/regexp/CharIndexedString.java @@ -0,0 +1,64 @@ +/* gnu/regexp/CharIndexedString.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.Serializable; + +class CharIndexedString implements CharIndexed, Serializable { + private String s; + private int anchor; + private int len; + + CharIndexedString(String str, int index) { + s = str; + len = s.length(); + anchor = index; + } + + public char charAt(int index) { + int pos = anchor + index; + return ((pos < len) && (pos >= 0)) ? s.charAt(pos) : OUT_OF_BOUNDS; + } + + public boolean isValid() { + return (anchor < len); + } + + public boolean move(int index) { + return ((anchor += index) < len); + } +} diff --git a/libjava/classpath/gnu/regexp/CharIndexedStringBuffer.java b/libjava/classpath/gnu/regexp/CharIndexedStringBuffer.java new file mode 100644 index 00000000000..1b88e398571 --- /dev/null +++ b/libjava/classpath/gnu/regexp/CharIndexedStringBuffer.java @@ -0,0 +1,62 @@ +/* gnu/regexp/CharIndexedStringBuffer.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.Serializable; + +class CharIndexedStringBuffer implements CharIndexed, Serializable { + private StringBuffer s; + private int anchor; + + CharIndexedStringBuffer(StringBuffer str, int index) { + s = str; + anchor = index; + } + + public char charAt(int index) { + int pos = anchor + index; + return ((pos < s.length()) && (pos >= 0)) ? s.charAt(pos) : OUT_OF_BOUNDS; + } + + public boolean isValid() { + return (anchor < s.length()); + } + + public boolean move(int index) { + return ((anchor += index) < s.length()); + } +} diff --git a/libjava/classpath/gnu/regexp/RE.java b/libjava/classpath/gnu/regexp/RE.java new file mode 100644 index 00000000000..16427c7566d --- /dev/null +++ b/libjava/classpath/gnu/regexp/RE.java @@ -0,0 +1,1396 @@ +/* gnu/regexp/RE.java + Copyright (C) 1998-2001, 2004, 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.InputStream; +import java.io.Serializable; +import java.util.Locale; +import java.util.PropertyResourceBundle; +import java.util.ResourceBundle; +import java.util.Vector; + +/** + * RE provides the user interface for compiling and matching regular + * expressions. + * <P> + * A regular expression object (class RE) is compiled by constructing it + * from a String, StringBuffer or character array, with optional + * compilation flags (below) + * and an optional syntax specification (see RESyntax; if not specified, + * <code>RESyntax.RE_SYNTAX_PERL5</code> is used). + * <P> + * Once compiled, a regular expression object is reusable as well as + * threadsafe: multiple threads can use the RE instance simultaneously + * to match against different input text. + * <P> + * Various methods attempt to match input text against a compiled + * regular expression. These methods are: + * <LI><code>isMatch</code>: returns true if the input text in its + * entirety matches the regular expression pattern. + * <LI><code>getMatch</code>: returns the first match found in the + * input text, or null if no match is found. + * <LI><code>getAllMatches</code>: returns an array of all + * non-overlapping matches found in the input text. If no matches are + * found, the array is zero-length. + * <LI><code>substitute</code>: substitute the first occurence of the + * pattern in the input text with a replacement string (which may + * include metacharacters $0-$9, see REMatch.substituteInto). + * <LI><code>substituteAll</code>: same as above, but repeat for each + * match before returning. + * <LI><code>getMatchEnumeration</code>: returns an REMatchEnumeration + * object that allows iteration over the matches (see + * REMatchEnumeration for some reasons why you may want to do this + * instead of using <code>getAllMatches</code>. + * <P> + * + * These methods all have similar argument lists. The input can be a + * String, a character array, a StringBuffer, or an + * InputStream of some sort. Note that when using an + * InputStream, the stream read position cannot be guaranteed after + * attempting a match (this is not a bug, but a consequence of the way + * regular expressions work). Using an REMatchEnumeration can + * eliminate most positioning problems. + * + * <P> + * + * The optional index argument specifies the offset from the beginning + * of the text at which the search should start (see the descriptions + * of some of the execution flags for how this can affect positional + * pattern operators). For an InputStream, this means an + * offset from the current read position, so subsequent calls with the + * same index argument on an InputStream will not + * necessarily access the same position on the stream, whereas + * repeated searches at a given index in a fixed string will return + * consistent results. + * + * <P> + * You can optionally affect the execution environment by using a + * combination of execution flags (constants listed below). + * + * <P> + * All operations on a regular expression are performed in a + * thread-safe manner. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + * @version 1.1.5-dev, to be released + */ + +public class RE extends REToken { + + private static final class IntPair implements Serializable { + public int first, second; + } + + private static final class CharUnit implements Serializable { + public char ch; + public boolean bk; + } + + // This String will be returned by getVersion() + private static final String VERSION = "1.1.5-dev"; + + // The localized strings are kept in a separate file + private static ResourceBundle messages = PropertyResourceBundle.getBundle("gnu/regexp/MessagesBundle", Locale.getDefault()); + + // These are, respectively, the first and last tokens in our linked list + // If there is only one token, firstToken == lastToken + private REToken firstToken, lastToken; + + // This is the number of subexpressions in this regular expression, + // with a minimum value of zero. Returned by getNumSubs() + private int numSubs; + + /** Minimum length, in characters, of any possible match. */ + private int minimumLength; + + /** + * Compilation flag. Do not differentiate case. Subsequent + * searches using this RE will be case insensitive. + */ + public static final int REG_ICASE = 2; + + /** + * Compilation flag. The match-any-character operator (dot) + * will match a newline character. When set this overrides the syntax + * bit RE_DOT_NEWLINE (see RESyntax for details). This is equivalent to + * the "/s" operator in Perl. + */ + public static final int REG_DOT_NEWLINE = 4; + + /** + * Compilation flag. Use multiline mode. In this mode, the ^ and $ + * anchors will match based on newlines within the input. This is + * equivalent to the "/m" operator in Perl. + */ + public static final int REG_MULTILINE = 8; + + /** + * Execution flag. + * The match-beginning operator (^) will not match at the beginning + * of the input string. Useful for matching on a substring when you + * know the context of the input is such that position zero of the + * input to the match test is not actually position zero of the text. + * <P> + * This example demonstrates the results of various ways of matching on + * a substring. + * <P> + * <CODE> + * String s = "food bar fool";<BR> + * RE exp = new RE("^foo.");<BR> + * REMatch m0 = exp.getMatch(s);<BR> + * REMatch m1 = exp.getMatch(s.substring(8));<BR> + * REMatch m2 = exp.getMatch(s.substring(8),0,RE.REG_NOTBOL); <BR> + * REMatch m3 = exp.getMatch(s,8); <BR> + * REMatch m4 = exp.getMatch(s,8,RE.REG_ANCHORINDEX); <BR> + * <P> + * // Results:<BR> + * // m0.toString(): "food"<BR> + * // m1.toString(): "fool"<BR> + * // m2.toString(): null<BR> + * // m3.toString(): null<BR> + * // m4.toString(): "fool"<BR> + * </CODE> + */ + public static final int REG_NOTBOL = 16; + + /** + * Execution flag. + * The match-end operator ($) does not match at the end + * of the input string. Useful for matching on substrings. + */ + public static final int REG_NOTEOL = 32; + + /** + * Execution flag. + * When a match method is invoked that starts matching at a non-zero + * index into the input, treat the input as if it begins at the index + * given. The effect of this flag is that the engine does not "see" + * any text in the input before the given index. This is useful so + * that the match-beginning operator (^) matches not at position 0 + * in the input string, but at the position the search started at + * (based on the index input given to the getMatch function). See + * the example under REG_NOTBOL. It also affects the use of the \< + * and \b operators. + */ + public static final int REG_ANCHORINDEX = 64; + + /** + * Execution flag. + * The substitute and substituteAll methods will not attempt to + * interpolate occurrences of $1-$9 in the replacement text with + * the corresponding subexpressions. For example, you may want to + * replace all matches of "one dollar" with "$1". + */ + public static final int REG_NO_INTERPOLATE = 128; + + /** Returns a string representing the version of the gnu.regexp package. */ + public static final String version() { + return VERSION; + } + + // Retrieves a message from the ResourceBundle + static final String getLocalizedMessage(String key) { + return messages.getString(key); + } + + /** + * Constructs a regular expression pattern buffer without any compilation + * flags set, and using the default syntax (RESyntax.RE_SYNTAX_PERL5). + * + * @param pattern A regular expression pattern, in the form of a String, + * StringBuffer or char[]. Other input types will be converted to + * strings using the toString() method. + * @exception REException The input pattern could not be parsed. + * @exception NullPointerException The pattern was null. + */ + public RE(Object pattern) throws REException { + this(pattern,0,RESyntax.RE_SYNTAX_PERL5,0,0); + } + + /** + * Constructs a regular expression pattern buffer using the specified + * compilation flags and the default syntax (RESyntax.RE_SYNTAX_PERL5). + * + * @param pattern A regular expression pattern, in the form of a String, + * StringBuffer, or char[]. Other input types will be converted to + * strings using the toString() method. + * @param cflags The logical OR of any combination of the compilation flags listed above. + * @exception REException The input pattern could not be parsed. + * @exception NullPointerException The pattern was null. + */ + public RE(Object pattern, int cflags) throws REException { + this(pattern,cflags,RESyntax.RE_SYNTAX_PERL5,0,0); + } + + /** + * Constructs a regular expression pattern buffer using the specified + * compilation flags and regular expression syntax. + * + * @param pattern A regular expression pattern, in the form of a String, + * StringBuffer, or char[]. Other input types will be converted to + * strings using the toString() method. + * @param cflags The logical OR of any combination of the compilation flags listed above. + * @param syntax The type of regular expression syntax to use. + * @exception REException The input pattern could not be parsed. + * @exception NullPointerException The pattern was null. + */ + public RE(Object pattern, int cflags, RESyntax syntax) throws REException { + this(pattern,cflags,syntax,0,0); + } + + // internal constructor used for alternation + private RE(REToken first, REToken last,int subs, int subIndex, int minLength) { + super(subIndex); + firstToken = first; + lastToken = last; + numSubs = subs; + minimumLength = minLength; + addToken(new RETokenEndSub(subIndex)); + } + + private RE(Object patternObj, int cflags, RESyntax syntax, int myIndex, int nextSub) throws REException { + super(myIndex); // Subexpression index of this token. + initialize(patternObj, cflags, syntax, myIndex, nextSub); + } + + // For use by subclasses + protected RE() { super(0); } + + // The meat of construction + protected void initialize(Object patternObj, int cflags, RESyntax syntax, int myIndex, int nextSub) throws REException { + char[] pattern; + if (patternObj instanceof String) { + pattern = ((String) patternObj).toCharArray(); + } else if (patternObj instanceof char[]) { + pattern = (char[]) patternObj; + } else if (patternObj instanceof StringBuffer) { + pattern = new char [((StringBuffer) patternObj).length()]; + ((StringBuffer) patternObj).getChars(0,pattern.length,pattern,0); + } else { + pattern = patternObj.toString().toCharArray(); + } + + int pLength = pattern.length; + + numSubs = 0; // Number of subexpressions in this token. + Vector branches = null; + + // linked list of tokens (sort of -- some closed loops can exist) + firstToken = lastToken = null; + + // Precalculate these so we don't pay for the math every time we + // need to access them. + boolean insens = ((cflags & REG_ICASE) > 0); + + // Parse pattern into tokens. Does anyone know if it's more efficient + // to use char[] than a String.charAt()? I'm assuming so. + + // index tracks the position in the char array + int index = 0; + + // this will be the current parse character (pattern[index]) + CharUnit unit = new CharUnit(); + + // This is used for {x,y} calculations + IntPair minMax = new IntPair(); + + // Buffer a token so we can create a TokenRepeated, etc. + REToken currentToken = null; + char ch; + boolean quot = false; + + while (index < pLength) { + // read the next character unit (including backslash escapes) + index = getCharUnit(pattern,index,unit,quot); + + if (unit.bk) + if (unit.ch == 'Q') { + quot = true; + continue; + } else if (unit.ch == 'E') { + quot = false; + continue; + } + if (quot) + unit.bk = false; + + // ALTERNATION OPERATOR + // \| or | (if RE_NO_BK_VBAR) or newline (if RE_NEWLINE_ALT) + // not available if RE_LIMITED_OPS is set + + // TODO: the '\n' literal here should be a test against REToken.newline, + // which unfortunately may be more than a single character. + if ( ( (unit.ch == '|' && (syntax.get(RESyntax.RE_NO_BK_VBAR) ^ (unit.bk || quot))) + || (syntax.get(RESyntax.RE_NEWLINE_ALT) && (unit.ch == '\n') && !(unit.bk || quot)) ) + && !syntax.get(RESyntax.RE_LIMITED_OPS)) { + // make everything up to here be a branch. create vector if nec. + addToken(currentToken); + RE theBranch = new RE(firstToken, lastToken, numSubs, subIndex, minimumLength); + minimumLength = 0; + if (branches == null) { + branches = new Vector(); + } + branches.addElement(theBranch); + firstToken = lastToken = currentToken = null; + } + + // INTERVAL OPERATOR: + // {x} | {x,} | {x,y} (RE_INTERVALS && RE_NO_BK_BRACES) + // \{x\} | \{x,\} | \{x,y\} (RE_INTERVALS && !RE_NO_BK_BRACES) + // + // OPEN QUESTION: + // what is proper interpretation of '{' at start of string? + + else if ((unit.ch == '{') && syntax.get(RESyntax.RE_INTERVALS) && (syntax.get(RESyntax.RE_NO_BK_BRACES) ^ (unit.bk || quot))) { + int newIndex = getMinMax(pattern,index,minMax,syntax); + if (newIndex > index) { + if (minMax.first > minMax.second) + throw new REException(getLocalizedMessage("interval.order"),REException.REG_BADRPT,newIndex); + if (currentToken == null) + throw new REException(getLocalizedMessage("repeat.no.token"),REException.REG_BADRPT,newIndex); + if (currentToken instanceof RETokenRepeated) + throw new REException(getLocalizedMessage("repeat.chained"),REException.REG_BADRPT,newIndex); + if (currentToken instanceof RETokenWordBoundary || currentToken instanceof RETokenWordBoundary) + throw new REException(getLocalizedMessage("repeat.assertion"),REException.REG_BADRPT,newIndex); + if ((currentToken.getMinimumLength() == 0) && (minMax.second == Integer.MAX_VALUE)) + throw new REException(getLocalizedMessage("repeat.empty.token"),REException.REG_BADRPT,newIndex); + index = newIndex; + currentToken = setRepeated(currentToken,minMax.first,minMax.second,index); + } + else { + addToken(currentToken); + currentToken = new RETokenChar(subIndex,unit.ch,insens); + } + } + + // LIST OPERATOR: + // [...] | [^...] + + else if ((unit.ch == '[') && !(unit.bk || quot)) { + Vector options = new Vector(); + boolean negative = false; + char lastChar = 0; + if (index == pLength) throw new REException(getLocalizedMessage("unmatched.bracket"),REException.REG_EBRACK,index); + + // Check for initial caret, negation + if ((ch = pattern[index]) == '^') { + negative = true; + if (++index == pLength) throw new REException(getLocalizedMessage("class.no.end"),REException.REG_EBRACK,index); + ch = pattern[index]; + } + + // Check for leading right bracket literal + if (ch == ']') { + lastChar = ch; + if (++index == pLength) throw new REException(getLocalizedMessage("class.no.end"),REException.REG_EBRACK,index); + } + + while ((ch = pattern[index++]) != ']') { + if ((ch == '-') && (lastChar != 0)) { + if (index == pLength) throw new REException(getLocalizedMessage("class.no.end"),REException.REG_EBRACK,index); + if ((ch = pattern[index]) == ']') { + options.addElement(new RETokenChar(subIndex,lastChar,insens)); + lastChar = '-'; + } else { + options.addElement(new RETokenRange(subIndex,lastChar,ch,insens)); + lastChar = 0; + index++; + } + } else if ((ch == '\\') && syntax.get(RESyntax.RE_BACKSLASH_ESCAPE_IN_LISTS)) { + if (index == pLength) throw new REException(getLocalizedMessage("class.no.end"),REException.REG_EBRACK,index); + int posixID = -1; + boolean negate = false; + char asciiEsc = 0; + if (("dswDSW".indexOf(pattern[index]) != -1) && syntax.get(RESyntax.RE_CHAR_CLASS_ESC_IN_LISTS)) { + switch (pattern[index]) { + case 'D': + negate = true; + case 'd': + posixID = RETokenPOSIX.DIGIT; + break; + case 'S': + negate = true; + case 's': + posixID = RETokenPOSIX.SPACE; + break; + case 'W': + negate = true; + case 'w': + posixID = RETokenPOSIX.ALNUM; + break; + } + } + else if ("nrt".indexOf(pattern[index]) != -1) { + switch (pattern[index]) { + case 'n': + asciiEsc = '\n'; + break; + case 't': + asciiEsc = '\t'; + break; + case 'r': + asciiEsc = '\r'; + break; + } + } + if (lastChar != 0) options.addElement(new RETokenChar(subIndex,lastChar,insens)); + + if (posixID != -1) { + options.addElement(new RETokenPOSIX(subIndex,posixID,insens,negate)); + } else if (asciiEsc != 0) { + lastChar = asciiEsc; + } else { + lastChar = pattern[index]; + } + ++index; + } else if ((ch == '[') && (syntax.get(RESyntax.RE_CHAR_CLASSES)) && (index < pLength) && (pattern[index] == ':')) { + StringBuffer posixSet = new StringBuffer(); + index = getPosixSet(pattern,index+1,posixSet); + int posixId = RETokenPOSIX.intValue(posixSet.toString()); + if (posixId != -1) + options.addElement(new RETokenPOSIX(subIndex,posixId,insens,false)); + } else { + if (lastChar != 0) options.addElement(new RETokenChar(subIndex,lastChar,insens)); + lastChar = ch; + } + if (index == pLength) throw new REException(getLocalizedMessage("class.no.end"),REException.REG_EBRACK,index); + } // while in list + // Out of list, index is one past ']' + + if (lastChar != 0) options.addElement(new RETokenChar(subIndex,lastChar,insens)); + + // Create a new RETokenOneOf + addToken(currentToken); + options.trimToSize(); + currentToken = new RETokenOneOf(subIndex,options,negative); + } + + // SUBEXPRESSIONS + // (...) | \(...\) depending on RE_NO_BK_PARENS + + else if ((unit.ch == '(') && (syntax.get(RESyntax.RE_NO_BK_PARENS) ^ (unit.bk || quot))) { + boolean pure = false; + boolean comment = false; + boolean lookAhead = false; + boolean negativelh = false; + if ((index+1 < pLength) && (pattern[index] == '?')) { + switch (pattern[index+1]) { + case '!': + if (syntax.get(RESyntax.RE_LOOKAHEAD)) { + pure = true; + negativelh = true; + lookAhead = true; + index += 2; + } + break; + case '=': + if (syntax.get(RESyntax.RE_LOOKAHEAD)) { + pure = true; + lookAhead = true; + index += 2; + } + break; + case ':': + if (syntax.get(RESyntax.RE_PURE_GROUPING)) { + pure = true; + index += 2; + } + break; + case '#': + if (syntax.get(RESyntax.RE_COMMENTS)) { + comment = true; + } + break; + default: + throw new REException(getLocalizedMessage("repeat.no.token"), REException.REG_BADRPT, index); + } + } + + if (index >= pLength) { + throw new REException(getLocalizedMessage("unmatched.paren"), REException.REG_ESUBREG,index); + } + + // find end of subexpression + int endIndex = index; + int nextIndex = index; + int nested = 0; + + while ( ((nextIndex = getCharUnit(pattern,endIndex,unit,false)) > 0) + && !(nested == 0 && (unit.ch == ')') && (syntax.get(RESyntax.RE_NO_BK_PARENS) ^ (unit.bk || quot))) ) + if ((endIndex = nextIndex) >= pLength) + throw new REException(getLocalizedMessage("subexpr.no.end"),REException.REG_ESUBREG,nextIndex); + else if (unit.ch == '(' && (syntax.get(RESyntax.RE_NO_BK_PARENS) ^ (unit.bk || quot))) + nested++; + else if (unit.ch == ')' && (syntax.get(RESyntax.RE_NO_BK_PARENS) ^ (unit.bk || quot))) + nested--; + + // endIndex is now position at a ')','\)' + // nextIndex is end of string or position after ')' or '\)' + + if (comment) index = nextIndex; + else { // not a comment + // create RE subexpression as token. + addToken(currentToken); + if (!pure) { + numSubs++; + } + + int useIndex = (pure || lookAhead) ? 0 : nextSub + numSubs; + currentToken = new RE(String.valueOf(pattern,index,endIndex-index).toCharArray(),cflags,syntax,useIndex,nextSub + numSubs); + numSubs += ((RE) currentToken).getNumSubs(); + + if (lookAhead) { + currentToken = new RETokenLookAhead(currentToken,negativelh); + } + + index = nextIndex; + } // not a comment + } // subexpression + + // UNMATCHED RIGHT PAREN + // ) or \) throw exception if + // !syntax.get(RESyntax.RE_UNMATCHED_RIGHT_PAREN_ORD) + else if (!syntax.get(RESyntax.RE_UNMATCHED_RIGHT_PAREN_ORD) && ((unit.ch == ')') && (syntax.get(RESyntax.RE_NO_BK_PARENS) ^ (unit.bk || quot)))) { + throw new REException(getLocalizedMessage("unmatched.paren"),REException.REG_EPAREN,index); + } + + // START OF LINE OPERATOR + // ^ + + else if ((unit.ch == '^') && !(unit.bk || quot)) { + addToken(currentToken); + currentToken = null; + addToken(new RETokenStart(subIndex,((cflags & REG_MULTILINE) > 0) ? syntax.getLineSeparator() : null)); + } + + // END OF LINE OPERATOR + // $ + + else if ((unit.ch == '$') && !(unit.bk || quot)) { + addToken(currentToken); + currentToken = null; + addToken(new RETokenEnd(subIndex,((cflags & REG_MULTILINE) > 0) ? syntax.getLineSeparator() : null)); + } + + // MATCH-ANY-CHARACTER OPERATOR (except possibly newline and null) + // . + + else if ((unit.ch == '.') && !(unit.bk || quot)) { + addToken(currentToken); + currentToken = new RETokenAny(subIndex,syntax.get(RESyntax.RE_DOT_NEWLINE) || ((cflags & REG_DOT_NEWLINE) > 0),syntax.get(RESyntax.RE_DOT_NOT_NULL)); + } + + // ZERO-OR-MORE REPEAT OPERATOR + // * + + else if ((unit.ch == '*') && !(unit.bk || quot)) { + if (currentToken == null) + throw new REException(getLocalizedMessage("repeat.no.token"),REException.REG_BADRPT,index); + if (currentToken instanceof RETokenRepeated) + throw new REException(getLocalizedMessage("repeat.chained"),REException.REG_BADRPT,index); + if (currentToken instanceof RETokenWordBoundary || currentToken instanceof RETokenWordBoundary) + throw new REException(getLocalizedMessage("repeat.assertion"),REException.REG_BADRPT,index); + if (currentToken.getMinimumLength() == 0) + throw new REException(getLocalizedMessage("repeat.empty.token"),REException.REG_BADRPT,index); + currentToken = setRepeated(currentToken,0,Integer.MAX_VALUE,index); + } + + // ONE-OR-MORE REPEAT OPERATOR / POSSESSIVE MATCHING OPERATOR + // + | \+ depending on RE_BK_PLUS_QM + // not available if RE_LIMITED_OPS is set + + else if ((unit.ch == '+') && !syntax.get(RESyntax.RE_LIMITED_OPS) && (!syntax.get(RESyntax.RE_BK_PLUS_QM) ^ (unit.bk || quot))) { + if (currentToken == null) + throw new REException(getLocalizedMessage("repeat.no.token"),REException.REG_BADRPT,index); + + // Check for possessive matching on RETokenRepeated + if (currentToken instanceof RETokenRepeated) { + RETokenRepeated tokenRep = (RETokenRepeated)currentToken; + if (syntax.get(RESyntax.RE_POSSESSIVE_OPS) && !tokenRep.isPossessive() && !tokenRep.isStingy()) + tokenRep.makePossessive(); + else + throw new REException(getLocalizedMessage("repeat.chained"),REException.REG_BADRPT,index); + + } + else if (currentToken instanceof RETokenWordBoundary || currentToken instanceof RETokenWordBoundary) + throw new REException(getLocalizedMessage("repeat.assertion"),REException.REG_BADRPT,index); + else if (currentToken.getMinimumLength() == 0) + throw new REException(getLocalizedMessage("repeat.empty.token"),REException.REG_BADRPT,index); + else + currentToken = setRepeated(currentToken,1,Integer.MAX_VALUE,index); + } + + // ZERO-OR-ONE REPEAT OPERATOR / STINGY MATCHING OPERATOR + // ? | \? depending on RE_BK_PLUS_QM + // not available if RE_LIMITED_OPS is set + // stingy matching if RE_STINGY_OPS is set and it follows a quantifier + + else if ((unit.ch == '?') && !syntax.get(RESyntax.RE_LIMITED_OPS) && (!syntax.get(RESyntax.RE_BK_PLUS_QM) ^ (unit.bk || quot))) { + if (currentToken == null) throw new REException(getLocalizedMessage("repeat.no.token"),REException.REG_BADRPT,index); + + // Check for stingy matching on RETokenRepeated + if (currentToken instanceof RETokenRepeated) { + RETokenRepeated tokenRep = (RETokenRepeated)currentToken; + if (syntax.get(RESyntax.RE_STINGY_OPS) && !tokenRep.isStingy() && !tokenRep.isPossessive()) + tokenRep.makeStingy(); + else + throw new REException(getLocalizedMessage("repeat.chained"),REException.REG_BADRPT,index); + } + else if (currentToken instanceof RETokenWordBoundary || currentToken instanceof RETokenWordBoundary) + throw new REException(getLocalizedMessage("repeat.assertion"),REException.REG_BADRPT,index); + else + currentToken = setRepeated(currentToken,0,1,index); + } + + // BACKREFERENCE OPERATOR + // \1 \2 ... \9 + // not available if RE_NO_BK_REFS is set + + else if (unit.bk && Character.isDigit(unit.ch) && !syntax.get(RESyntax.RE_NO_BK_REFS)) { + addToken(currentToken); + currentToken = new RETokenBackRef(subIndex,Character.digit(unit.ch,10),insens); + } + + // START OF STRING OPERATOR + // \A if RE_STRING_ANCHORS is set + + else if (unit.bk && (unit.ch == 'A') && syntax.get(RESyntax.RE_STRING_ANCHORS)) { + addToken(currentToken); + currentToken = new RETokenStart(subIndex,null); + } + + // WORD BREAK OPERATOR + // \b if ???? + + else if (unit.bk && (unit.ch == 'b') && syntax.get(RESyntax.RE_STRING_ANCHORS)) { + addToken(currentToken); + currentToken = new RETokenWordBoundary(subIndex, RETokenWordBoundary.BEGIN | RETokenWordBoundary.END, false); + } + + // WORD BEGIN OPERATOR + // \< if ???? + else if (unit.bk && (unit.ch == '<')) { + addToken(currentToken); + currentToken = new RETokenWordBoundary(subIndex, RETokenWordBoundary.BEGIN, false); + } + + // WORD END OPERATOR + // \> if ???? + else if (unit.bk && (unit.ch == '>')) { + addToken(currentToken); + currentToken = new RETokenWordBoundary(subIndex, RETokenWordBoundary.END, false); + } + + // NON-WORD BREAK OPERATOR + // \B if ???? + + else if (unit.bk && (unit.ch == 'B') && syntax.get(RESyntax.RE_STRING_ANCHORS)) { + addToken(currentToken); + currentToken = new RETokenWordBoundary(subIndex, RETokenWordBoundary.BEGIN | RETokenWordBoundary.END, true); + } + + + // DIGIT OPERATOR + // \d if RE_CHAR_CLASS_ESCAPES is set + + else if (unit.bk && (unit.ch == 'd') && syntax.get(RESyntax.RE_CHAR_CLASS_ESCAPES)) { + addToken(currentToken); + currentToken = new RETokenPOSIX(subIndex,RETokenPOSIX.DIGIT,insens,false); + } + + // NON-DIGIT OPERATOR + // \D + + else if (unit.bk && (unit.ch == 'D') && syntax.get(RESyntax.RE_CHAR_CLASS_ESCAPES)) { + addToken(currentToken); + currentToken = new RETokenPOSIX(subIndex,RETokenPOSIX.DIGIT,insens,true); + } + + // NEWLINE ESCAPE + // \n + + else if (unit.bk && (unit.ch == 'n')) { + addToken(currentToken); + currentToken = new RETokenChar(subIndex,'\n',false); + } + + // RETURN ESCAPE + // \r + + else if (unit.bk && (unit.ch == 'r')) { + addToken(currentToken); + currentToken = new RETokenChar(subIndex,'\r',false); + } + + // WHITESPACE OPERATOR + // \s if RE_CHAR_CLASS_ESCAPES is set + + else if (unit.bk && (unit.ch == 's') && syntax.get(RESyntax.RE_CHAR_CLASS_ESCAPES)) { + addToken(currentToken); + currentToken = new RETokenPOSIX(subIndex,RETokenPOSIX.SPACE,insens,false); + } + + // NON-WHITESPACE OPERATOR + // \S + + else if (unit.bk && (unit.ch == 'S') && syntax.get(RESyntax.RE_CHAR_CLASS_ESCAPES)) { + addToken(currentToken); + currentToken = new RETokenPOSIX(subIndex,RETokenPOSIX.SPACE,insens,true); + } + + // TAB ESCAPE + // \t + + else if (unit.bk && (unit.ch == 't')) { + addToken(currentToken); + currentToken = new RETokenChar(subIndex,'\t',false); + } + + // ALPHANUMERIC OPERATOR + // \w + + else if (unit.bk && (unit.ch == 'w') && syntax.get(RESyntax.RE_CHAR_CLASS_ESCAPES)) { + addToken(currentToken); + currentToken = new RETokenPOSIX(subIndex,RETokenPOSIX.ALNUM,insens,false); + } + + // NON-ALPHANUMERIC OPERATOR + // \W + + else if (unit.bk && (unit.ch == 'W') && syntax.get(RESyntax.RE_CHAR_CLASS_ESCAPES)) { + addToken(currentToken); + currentToken = new RETokenPOSIX(subIndex,RETokenPOSIX.ALNUM,insens,true); + } + + // END OF STRING OPERATOR + // \Z + + else if (unit.bk && (unit.ch == 'Z') && syntax.get(RESyntax.RE_STRING_ANCHORS)) { + addToken(currentToken); + currentToken = new RETokenEnd(subIndex,null); + } + + // NON-SPECIAL CHARACTER (or escape to make literal) + // c | \* for example + + else { // not a special character + addToken(currentToken); + currentToken = new RETokenChar(subIndex,unit.ch,insens); + } + } // end while + + // Add final buffered token and an EndSub marker + addToken(currentToken); + + if (branches != null) { + branches.addElement(new RE(firstToken,lastToken,numSubs,subIndex,minimumLength)); + branches.trimToSize(); // compact the Vector + minimumLength = 0; + firstToken = lastToken = null; + addToken(new RETokenOneOf(subIndex,branches,false)); + } + else addToken(new RETokenEndSub(subIndex)); + + } + + private static int getCharUnit(char[] input, int index, CharUnit unit, boolean quot) throws REException { + unit.ch = input[index++]; + unit.bk = (unit.ch == '\\' + && (!quot || index >= input.length || input[index] == 'E')); + if (unit.bk) + if (index < input.length) + unit.ch = input[index++]; + else throw new REException(getLocalizedMessage("ends.with.backslash"),REException.REG_ESCAPE,index); + return index; + } + + /** + * Checks if the regular expression matches the input in its entirety. + * + * @param input The input text. + */ + public boolean isMatch(Object input) { + return isMatch(input,0,0); + } + + /** + * Checks if the input string, starting from index, is an exact match of + * this regular expression. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + */ + public boolean isMatch(Object input,int index) { + return isMatch(input,index,0); + } + + + /** + * Checks if the input, starting from index and using the specified + * execution flags, is an exact match of this regular expression. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + */ + public boolean isMatch(Object input,int index,int eflags) { + return isMatchImpl(makeCharIndexed(input,index),index,eflags); + } + + private boolean isMatchImpl(CharIndexed input, int index, int eflags) { + if (firstToken == null) // Trivial case + return (input.charAt(0) == CharIndexed.OUT_OF_BOUNDS); + REMatch m = new REMatch(numSubs, index, eflags); + if (firstToken.match(input, m)) { + while (m != null) { + if (input.charAt(m.index) == CharIndexed.OUT_OF_BOUNDS) { + return true; + } + m = m.next; + } + } + return false; + } + + /** + * Returns the maximum number of subexpressions in this regular expression. + * If the expression contains branches, the value returned will be the + * maximum subexpressions in any of the branches. + */ + public int getNumSubs() { + return numSubs; + } + + // Overrides REToken.setUncle + void setUncle(REToken uncle) { + if (lastToken != null) { + lastToken.setUncle(uncle); + } else super.setUncle(uncle); // to deal with empty subexpressions + } + + // Overrides REToken.chain + + boolean chain(REToken next) { + super.chain(next); + setUncle(next); + return true; + } + + /** + * Returns the minimum number of characters that could possibly + * constitute a match of this regular expression. + */ + public int getMinimumLength() { + return minimumLength; + } + + /** + * Returns an array of all matches found in the input. + * + * If the regular expression allows the empty string to match, it will + * substitute matches at all positions except the end of the input. + * + * @param input The input text. + * @return a non-null (but possibly zero-length) array of matches + */ + public REMatch[] getAllMatches(Object input) { + return getAllMatches(input,0,0); + } + + /** + * Returns an array of all matches found in the input, + * beginning at the specified index position. + * + * If the regular expression allows the empty string to match, it will + * substitute matches at all positions except the end of the input. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @return a non-null (but possibly zero-length) array of matches + */ + public REMatch[] getAllMatches(Object input, int index) { + return getAllMatches(input,index,0); + } + + /** + * Returns an array of all matches found in the input string, + * beginning at the specified index position and using the specified + * execution flags. + * + * If the regular expression allows the empty string to match, it will + * substitute matches at all positions except the end of the input. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + * @return a non-null (but possibly zero-length) array of matches + */ + public REMatch[] getAllMatches(Object input, int index, int eflags) { + return getAllMatchesImpl(makeCharIndexed(input,index),index,eflags); + } + + // this has been changed since 1.03 to be non-overlapping matches + private REMatch[] getAllMatchesImpl(CharIndexed input, int index, int eflags) { + Vector all = new Vector(); + REMatch m = null; + while ((m = getMatchImpl(input,index,eflags,null)) != null) { + all.addElement(m); + index = m.getEndIndex(); + if (m.end[0] == 0) { // handle pathological case of zero-length match + index++; + input.move(1); + } else { + input.move(m.end[0]); + } + if (!input.isValid()) break; + } + REMatch[] mset = new REMatch[all.size()]; + all.copyInto(mset); + return mset; + } + + /* Implements abstract method REToken.match() */ + boolean match(CharIndexed input, REMatch mymatch) { + if (firstToken == null) return next(input, mymatch); + + // Note the start of this subexpression + mymatch.start[subIndex] = mymatch.index; + + return firstToken.match(input, mymatch); + } + + /** + * Returns the first match found in the input. If no match is found, + * null is returned. + * + * @param input The input text. + * @return An REMatch instance referencing the match, or null if none. + */ + public REMatch getMatch(Object input) { + return getMatch(input,0,0); + } + + /** + * Returns the first match found in the input, beginning + * the search at the specified index. If no match is found, + * returns null. + * + * @param input The input text. + * @param index The offset within the text to begin looking for a match. + * @return An REMatch instance referencing the match, or null if none. + */ + public REMatch getMatch(Object input, int index) { + return getMatch(input,index,0); + } + + /** + * Returns the first match found in the input, beginning + * the search at the specified index, and using the specified + * execution flags. If no match is found, returns null. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + * @return An REMatch instance referencing the match, or null if none. + */ + public REMatch getMatch(Object input, int index, int eflags) { + return getMatch(input,index,eflags,null); + } + + /** + * Returns the first match found in the input, beginning the search + * at the specified index, and using the specified execution flags. + * If no match is found, returns null. If a StringBuffer is + * provided and is non-null, the contents of the input text from the + * index to the beginning of the match (or to the end of the input, + * if there is no match) are appended to the StringBuffer. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + * @param buffer The StringBuffer to save pre-match text in. + * @return An REMatch instance referencing the match, or null if none. */ + public REMatch getMatch(Object input, int index, int eflags, StringBuffer buffer) { + return getMatchImpl(makeCharIndexed(input,index),index,eflags,buffer); + } + + REMatch getMatchImpl(CharIndexed input, int anchor, int eflags, StringBuffer buffer) { + // Create a new REMatch to hold results + REMatch mymatch = new REMatch(numSubs, anchor, eflags); + do { + // Optimization: check if anchor + minimumLength > length + if (minimumLength == 0 || input.charAt(minimumLength-1) != CharIndexed.OUT_OF_BOUNDS) { + if (match(input, mymatch)) { + // Find longest match of them all to observe leftmost longest + REMatch longest = mymatch; + while ((mymatch = mymatch.next) != null) { + if (mymatch.index > longest.index) { + longest = mymatch; + } + } + + longest.end[0] = longest.index; + longest.finish(input); + return longest; + } + } + mymatch.clear(++anchor); + // Append character to buffer if needed + if (buffer != null && input.charAt(0) != CharIndexed.OUT_OF_BOUNDS) { + buffer.append(input.charAt(0)); + } + } while (input.move(1)); + + // Special handling at end of input for e.g. "$" + if (minimumLength == 0) { + if (match(input, mymatch)) { + mymatch.finish(input); + return mymatch; + } + } + + return null; + } + + /** + * Returns an REMatchEnumeration that can be used to iterate over the + * matches found in the input text. + * + * @param input The input text. + * @return A non-null REMatchEnumeration instance. + */ + public REMatchEnumeration getMatchEnumeration(Object input) { + return getMatchEnumeration(input,0,0); + } + + + /** + * Returns an REMatchEnumeration that can be used to iterate over the + * matches found in the input text. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @return A non-null REMatchEnumeration instance, with its input cursor + * set to the index position specified. + */ + public REMatchEnumeration getMatchEnumeration(Object input, int index) { + return getMatchEnumeration(input,index,0); + } + + /** + * Returns an REMatchEnumeration that can be used to iterate over the + * matches found in the input text. + * + * @param input The input text. + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + * @return A non-null REMatchEnumeration instance, with its input cursor + * set to the index position specified. + */ + public REMatchEnumeration getMatchEnumeration(Object input, int index, int eflags) { + return new REMatchEnumeration(this,makeCharIndexed(input,index),index,eflags); + } + + + /** + * Substitutes the replacement text for the first match found in the input. + * + * @param input The input text. + * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). + * @return A String interpolating the substituted text. + * @see REMatch#substituteInto + */ + public String substitute(Object input,String replace) { + return substitute(input,replace,0,0); + } + + /** + * Substitutes the replacement text for the first match found in the input + * beginning at the specified index position. Specifying an index + * effectively causes the regular expression engine to throw away the + * specified number of characters. + * + * @param input The input text. + * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). + * @param index The offset index at which the search should be begin. + * @return A String containing the substring of the input, starting + * at the index position, and interpolating the substituted text. + * @see REMatch#substituteInto + */ + public String substitute(Object input,String replace,int index) { + return substitute(input,replace,index,0); + } + + /** + * Substitutes the replacement text for the first match found in the input + * string, beginning at the specified index position and using the + * specified execution flags. + * + * @param input The input text. + * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + * @return A String containing the substring of the input, starting + * at the index position, and interpolating the substituted text. + * @see REMatch#substituteInto + */ + public String substitute(Object input,String replace,int index,int eflags) { + return substituteImpl(makeCharIndexed(input,index),replace,index,eflags); + } + + private String substituteImpl(CharIndexed input,String replace,int index,int eflags) { + StringBuffer buffer = new StringBuffer(); + REMatch m = getMatchImpl(input,index,eflags,buffer); + if (m==null) return buffer.toString(); + buffer.append( ((eflags & REG_NO_INTERPOLATE) > 0) ? + replace : m.substituteInto(replace) ); + if (input.move(m.end[0])) { + do { + buffer.append(input.charAt(0)); + } while (input.move(1)); + } + return buffer.toString(); + } + + /** + * Substitutes the replacement text for each non-overlapping match found + * in the input text. + * + * @param input The input text. + * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). + * @return A String interpolating the substituted text. + * @see REMatch#substituteInto + */ + public String substituteAll(Object input,String replace) { + return substituteAll(input,replace,0,0); + } + + /** + * Substitutes the replacement text for each non-overlapping match found + * in the input text, starting at the specified index. + * + * If the regular expression allows the empty string to match, it will + * substitute matches at all positions except the end of the input. + * + * @param input The input text. + * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). + * @param index The offset index at which the search should be begin. + * @return A String containing the substring of the input, starting + * at the index position, and interpolating the substituted text. + * @see REMatch#substituteInto + */ + public String substituteAll(Object input,String replace,int index) { + return substituteAll(input,replace,index,0); + } + + /** + * Substitutes the replacement text for each non-overlapping match found + * in the input text, starting at the specified index and using the + * specified execution flags. + * + * @param input The input text. + * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). + * @param index The offset index at which the search should be begin. + * @param eflags The logical OR of any execution flags above. + * @return A String containing the substring of the input, starting + * at the index position, and interpolating the substituted text. + * @see REMatch#substituteInto + */ + public String substituteAll(Object input,String replace,int index,int eflags) { + return substituteAllImpl(makeCharIndexed(input,index),replace,index,eflags); + } + + private String substituteAllImpl(CharIndexed input,String replace,int index,int eflags) { + StringBuffer buffer = new StringBuffer(); + REMatch m; + while ((m = getMatchImpl(input,index,eflags,buffer)) != null) { + buffer.append( ((eflags & REG_NO_INTERPOLATE) > 0) ? + replace : m.substituteInto(replace) ); + index = m.getEndIndex(); + if (m.end[0] == 0) { + char ch = input.charAt(0); + if (ch != CharIndexed.OUT_OF_BOUNDS) + buffer.append(ch); + input.move(1); + } else { + input.move(m.end[0]); + } + + if (!input.isValid()) break; + } + return buffer.toString(); + } + + /* Helper function for constructor */ + private void addToken(REToken next) { + if (next == null) return; + minimumLength += next.getMinimumLength(); + if (firstToken == null) { + lastToken = firstToken = next; + } else { + // if chain returns false, it "rejected" the token due to + // an optimization, and next was combined with lastToken + if (lastToken.chain(next)) { + lastToken = next; + } + } + } + + private static REToken setRepeated(REToken current, int min, int max, int index) throws REException { + if (current == null) throw new REException(getLocalizedMessage("repeat.no.token"),REException.REG_BADRPT,index); + return new RETokenRepeated(current.subIndex,current,min,max); + } + + private static int getPosixSet(char[] pattern,int index,StringBuffer buf) { + // Precondition: pattern[index-1] == ':' + // we will return pos of closing ']'. + int i; + for (i=index; i<(pattern.length-1); i++) { + if ((pattern[i] == ':') && (pattern[i+1] == ']')) + return i+2; + buf.append(pattern[i]); + } + return index; // didn't match up + } + + private int getMinMax(char[] input,int index,IntPair minMax,RESyntax syntax) throws REException { + // Precondition: input[index-1] == '{', minMax != null + + boolean mustMatch = !syntax.get(RESyntax.RE_NO_BK_BRACES); + int startIndex = index; + if (index == input.length) { + if (mustMatch) + throw new REException(getLocalizedMessage("unmatched.brace"),REException.REG_EBRACE,index); + else + return startIndex; + } + + int min,max=0; + CharUnit unit = new CharUnit(); + StringBuffer buf = new StringBuffer(); + + // Read string of digits + do { + index = getCharUnit(input,index,unit,false); + if (Character.isDigit(unit.ch)) + buf.append(unit.ch); + } while ((index != input.length) && Character.isDigit(unit.ch)); + + // Check for {} tomfoolery + if (buf.length() == 0) { + if (mustMatch) + throw new REException(getLocalizedMessage("interval.error"),REException.REG_EBRACE,index); + else + return startIndex; + } + + min = Integer.parseInt(buf.toString()); + + if ((unit.ch == '}') && (syntax.get(RESyntax.RE_NO_BK_BRACES) ^ unit.bk)) + max = min; + else if (index == input.length) + if (mustMatch) + throw new REException(getLocalizedMessage("interval.no.end"),REException.REG_EBRACE,index); + else + return startIndex; + else if ((unit.ch == ',') && !unit.bk) { + buf = new StringBuffer(); + // Read string of digits + while (((index = getCharUnit(input,index,unit,false)) != input.length) && Character.isDigit(unit.ch)) + buf.append(unit.ch); + + if (!((unit.ch == '}') && (syntax.get(RESyntax.RE_NO_BK_BRACES) ^ unit.bk))) + if (mustMatch) + throw new REException(getLocalizedMessage("interval.error"),REException.REG_EBRACE,index); + else + return startIndex; + + // This is the case of {x,} + if (buf.length() == 0) max = Integer.MAX_VALUE; + else max = Integer.parseInt(buf.toString()); + } else + if (mustMatch) + throw new REException(getLocalizedMessage("interval.error"),REException.REG_EBRACE,index); + else + return startIndex; + + // We know min and max now, and they are valid. + + minMax.first = min; + minMax.second = max; + + // return the index following the '}' + return index; + } + + /** + * Return a human readable form of the compiled regular expression, + * useful for debugging. + */ + public String toString() { + StringBuffer sb = new StringBuffer(); + dump(sb); + return sb.toString(); + } + + void dump(StringBuffer os) { + os.append('('); + if (subIndex == 0) + os.append("?:"); + if (firstToken != null) + firstToken.dumpAll(os); + os.append(')'); + } + + // Cast input appropriately or throw exception + private static CharIndexed makeCharIndexed(Object input, int index) { + // We could let a String fall through to final input, but since + // it's the most likely input type, we check it first. + if (input instanceof String) + return new CharIndexedString((String) input,index); + else if (input instanceof char[]) + return new CharIndexedCharArray((char[]) input,index); + else if (input instanceof StringBuffer) + return new CharIndexedStringBuffer((StringBuffer) input,index); + else if (input instanceof InputStream) + return new CharIndexedInputStream((InputStream) input,index); + else if (input instanceof CharIndexed) + return (CharIndexed) input; // do we lose index info? + else + return new CharIndexedString(input.toString(), index); + } +} diff --git a/libjava/classpath/gnu/regexp/REException.java b/libjava/classpath/gnu/regexp/REException.java new file mode 100644 index 00000000000..73f86fa8add --- /dev/null +++ b/libjava/classpath/gnu/regexp/REException.java @@ -0,0 +1,182 @@ +/* gnu/regexp/REException.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +import java.text.MessageFormat; + +/** + * This is the regular expression exception class. An exception of this type + * defines the three attributes: + * <OL> + * <LI> A descriptive message of the error. + * <LI> An integral type code equivalent to one of the statically + * defined symbols listed below. + * <LI> The approximate position in the input string where the error + * occurred. + * </OL> + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + */ + +public class REException extends Exception { + private int type; + private int pos; + + // Error conditions from GNU regcomp(3) manual + + /** + * Error flag. + * Invalid use of repetition operators such as using + * `*' as the first character. + */ + public static final int REG_BADRPT = 1; + + /** + * Error flag. + * Invalid use of back reference operator. + */ + public static final int REG_BADBR = 2; + + /** + * Error flag. + * Un-matched brace interval operators. + */ + public static final int REG_EBRACE = 3; + + /** + * Error flag. + * Un-matched bracket list operators. + */ + public static final int REG_EBRACK = 4; + + /** + * Error flag. + * Invalid use of the range operator, eg. the ending + * point of the range occurs prior to the starting + * point. + */ + public static final int REG_ERANGE = 5; + + /** + * Error flag. + * Unknown character class name. <B>Not implemented</B>. + */ + public static final int REG_ECTYPE = 6; + + /** + * Error flag. + * Un-matched parenthesis group operators. + */ + public static final int REG_EPAREN = 7; + + /** + * Error flag. + * Invalid back reference to a subexpression. + */ + public static final int REG_ESUBREG = 8; + + /** + * Error flag. + * Non specific error. <B>Not implemented</B>. + */ + public static final int REG_EEND = 9; + + /** + * Error flag. + * Invalid escape sequence. <B>Not implemented</B>. + */ + public static final int REG_ESCAPE = 10; + + /** + * Error flag. + * Invalid use of pattern operators such as group or list. + */ + public static final int REG_BADPAT = 11; + + /** + * Error flag. + * Compiled regular expression requires a pattern + * buffer larger than 64Kb. <B>Not implemented</B>. + */ + public static final int REG_ESIZE = 12; + + /** + * Error flag. + * The regex routines ran out of memory. <B>Not implemented</B>. + */ + public static final int REG_ESPACE = 13; + + REException(String msg, int type, int position) { + super(msg); + this.type = type; + this.pos = position; + } + + /** + * Returns the type of the exception, one of the constants listed above. + */ + + public int getType() { + return type; + } + + /** + * Returns the position, relative to the string or character array being + * compiled, where the error occurred. This position is generally the point + * where the error was detected, not necessarily the starting index of + * a bad subexpression. + */ + public int getPosition() { + return pos; + } + + /** + * Reports the descriptive message associated with this exception + * as well as its index position in the string or character array + * being compiled. + */ + public String getMessage() { + Object[] args = {new Integer(pos)}; + StringBuffer sb = new StringBuffer(); + String prefix = RE.getLocalizedMessage("error.prefix"); + sb.append(MessageFormat.format(prefix, args)); + sb.append('\n'); + sb.append(super.getMessage()); + return sb.toString(); + } +} diff --git a/libjava/classpath/gnu/regexp/REFilterInputStream.java b/libjava/classpath/gnu/regexp/REFilterInputStream.java new file mode 100644 index 00000000000..485de56605d --- /dev/null +++ b/libjava/classpath/gnu/regexp/REFilterInputStream.java @@ -0,0 +1,140 @@ +/* gnu/regexp/REFilterInputStream.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; +import java.io.FilterInputStream; +import java.io.InputStream; + +/** + * Replaces instances of a given RE found within an InputStream + * with replacement text. The replacements are interpolated into the + * stream when a match is found. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + * @deprecated This class cannot properly handle all character + * encodings. For proper handling, use the REFilterReader + * class instead. + */ + +public class REFilterInputStream extends FilterInputStream { + + private RE expr; + private String replace; + private String buffer; + private int bufpos; + private int offset; + private CharIndexedInputStream stream; + + /** + * Creates an REFilterInputStream. When reading from this stream, + * occurrences of patterns matching the supplied regular expression + * will be replaced with the supplied replacement text (the + * metacharacters $0 through $9 may be used to refer to the full + * match or subexpression matches). + * + * @param stream The InputStream to be filtered. + * @param expr The regular expression to search for. + * @param replace The text pattern to replace matches with. + */ + public REFilterInputStream(InputStream stream, RE expr, String replace) { + super(stream); + this.stream = new CharIndexedInputStream(stream,0); + this.expr = expr; + this.replace = replace; + } + + /** + * Reads the next byte from the stream per the general contract of + * InputStream.read(). Returns -1 on error or end of stream. + */ + public int read() { + // If we have buffered replace data, use it. + if ((buffer != null) && (bufpos < buffer.length())) { + return (int) buffer.charAt(bufpos++); + } + + // check if input is at a valid position + if (!stream.isValid()) return -1; + + REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0); + if (expr.match(stream, mymatch)) { + mymatch.end[0] = mymatch.index; + mymatch.finish(stream); + stream.move(mymatch.toString().length()); + offset += mymatch.toString().length(); + buffer = mymatch.substituteInto(replace); + bufpos = 1; + + // This is prone to infinite loops if replace string turns out empty. + if (buffer.length() > 0) { + return buffer.charAt(0); + } + } + char ch = stream.charAt(0); + if (ch == CharIndexed.OUT_OF_BOUNDS) return -1; + stream.move(1); + offset++; + return ch; + } + + /** + * Returns false. REFilterInputStream does not support mark() and + * reset() methods. + */ + public boolean markSupported() { + return false; + } + + /** Reads from the stream into the provided array. */ + public int read(byte[] b, int off, int len) { + int i; + int ok = 0; + while (len-- > 0) { + i = read(); + if (i == -1) return (ok == 0) ? -1 : ok; + b[off++] = (byte) i; + ok++; + } + return ok; + } + + /** Reads from the stream into the provided array. */ + public int read(byte[] b) { + return read(b,0,b.length); + } +} diff --git a/libjava/classpath/gnu/regexp/REMatch.java b/libjava/classpath/gnu/regexp/REMatch.java new file mode 100644 index 00000000000..cf25bb331c5 --- /dev/null +++ b/libjava/classpath/gnu/regexp/REMatch.java @@ -0,0 +1,263 @@ +/* gnu/regexp/REMatch.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; +import java.io.Serializable; + +/** + * An instance of this class represents a match + * completed by a gnu.regexp matching function. It can be used + * to obtain relevant information about the location of a match + * or submatch. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + */ +public final class REMatch implements Serializable, Cloneable { + private String matchedText; + + // These variables are package scope for fast access within the engine + int eflags; // execution flags this match was made using + + // Offset in source text where match was tried. This is zero-based; + // the actual position in the source text is given by (offset + anchor). + int offset; + + // Anchor position refers to the index into the source input + // at which the matching operation began. + // This is also useful for the ANCHORINDEX option. + int anchor; + + // Package scope; used by RE. + int index; // used while matching to mark current match position in input + int[] start; // start positions (relative to offset) for each (sub)exp. + int[] end; // end positions for the same + REMatch next; // other possibility (to avoid having to use arrays) + + public Object clone() { + try { + REMatch copy = (REMatch) super.clone(); + copy.next = null; + + copy.start = (int[]) start.clone(); + copy.end = (int[]) end.clone(); + + return copy; + } catch (CloneNotSupportedException e) { + throw new Error(); // doesn't happen + } + } + + void assignFrom(REMatch other) { + start = other.start; + end = other.end; + index = other.index; + // need to deep clone? + next = other.next; + } + + REMatch(int subs, int anchor, int eflags) { + start = new int[subs+1]; + end = new int[subs+1]; + this.anchor = anchor; + this.eflags = eflags; + clear(anchor); + } + + void finish(CharIndexed text) { + start[0] = 0; + StringBuffer sb = new StringBuffer(); + int i; + for (i = 0; i < end[0]; i++) + sb.append(text.charAt(i)); + matchedText = sb.toString(); + for (i = 0; i < start.length; i++) { + // If any subexpressions didn't terminate, they don't count + // TODO check if this code ever gets hit + if ((start[i] == -1) ^ (end[i] == -1)) { + start[i] = -1; + end[i] = -1; + } + } + next = null; // cut off alternates + } + + /** Clears the current match and moves the offset to the new index. */ + void clear(int index) { + offset = index; + this.index = 0; + for (int i = 0; i < start.length; i++) { + start[i] = end[i] = -1; + } + next = null; // cut off alternates + } + + /** + * Returns the string matching the pattern. This makes it convenient + * to write code like the following: + * <P> + * <code> + * REMatch myMatch = myExpression.getMatch(myString);<br> + * if (myMatch != null) System.out.println("Regexp found: "+myMatch); + * </code> + */ + public String toString() { + return matchedText; + } + + /** + * Returns the index within the input text where the match in its entirety + * began. + */ + public int getStartIndex() { + return offset + start[0]; + } + + /** + * Returns the index within the input string where the match in + * its entirety ends. The return value is the next position after + * the end of the string; therefore, a match created by the + * following call: + * + * <P> + * <code>REMatch myMatch = myExpression.getMatch(myString);</code> + * <P> + * can be viewed (given that myMatch is not null) by creating + * <P> + * <code>String theMatch = myString.substring(myMatch.getStartIndex(), + * myMatch.getEndIndex());</code> + * <P> + * But you can save yourself that work, since the <code>toString()</code> + * method (above) does exactly that for you. + */ + public int getEndIndex() { + return offset + end[0]; + } + + /** + * Returns the string matching the given subexpression. The subexpressions + * are indexed starting with one, not zero. That is, the subexpression + * identified by the first set of parentheses in a regular expression + * could be retrieved from an REMatch by calling match.toString(1). + * + * @param sub Index of the subexpression. + */ + public String toString(int sub) { + if ((sub >= start.length) || (start[sub] == -1)) return ""; + return (matchedText.substring(start[sub],end[sub])); + } + + /** + * Returns the index within the input string used to generate this match + * where subexpression number <i>sub</i> begins, or <code>-1</code> if + * the subexpression does not exist. The initial position is zero. + * + * @param sub Subexpression index + * @deprecated Use getStartIndex(int) instead. + */ + public int getSubStartIndex(int sub) { + if (sub >= start.length) return -1; + int x = start[sub]; + return (x == -1) ? x : offset + x; + } + + /** + * Returns the index within the input string used to generate this match + * where subexpression number <i>sub</i> begins, or <code>-1</code> if + * the subexpression does not exist. The initial position is zero. + * + * @param sub Subexpression index + * @since gnu.regexp 1.1.0 + */ + public int getStartIndex(int sub) { + if (sub >= start.length) return -1; + int x = start[sub]; + return (x == -1) ? x : offset + x; + } + + /** + * Returns the index within the input string used to generate this match + * where subexpression number <i>sub</i> ends, or <code>-1</code> if + * the subexpression does not exist. The initial position is zero. + * + * @param sub Subexpression index + * @deprecated Use getEndIndex(int) instead + */ + public int getSubEndIndex(int sub) { + if (sub >= start.length) return -1; + int x = end[sub]; + return (x == -1) ? x : offset + x; + } + + /** + * Returns the index within the input string used to generate this match + * where subexpression number <i>sub</i> ends, or <code>-1</code> if + * the subexpression does not exist. The initial position is zero. + * + * @param sub Subexpression index + */ + public int getEndIndex(int sub) { + if (sub >= start.length) return -1; + int x = end[sub]; + return (x == -1) ? x : offset + x; + } + + /** + * Substitute the results of this match to create a new string. + * This is patterned after PERL, so the tokens to watch out for are + * <code>$0</code> through <code>$9</code>. <code>$0</code> matches + * the full substring matched; <code>$<i>n</i></code> matches + * subexpression number <i>n</i>. + * + * @param input A string consisting of literals and <code>$<i>n</i></code> tokens. + */ + public String substituteInto(String input) { + // a la Perl, $0 is whole thing, $1 - $9 are subexpressions + StringBuffer output = new StringBuffer(); + int pos; + for (pos = 0; pos < input.length()-1; pos++) { + if ((input.charAt(pos) == '$') && (Character.isDigit(input.charAt(pos+1)))) { + int val = Character.digit(input.charAt(++pos),10); + if (val < start.length) { + output.append(toString(val)); + } + } else output.append(input.charAt(pos)); + } + if (pos < input.length()) output.append(input.charAt(pos)); + return output.toString(); + } +} diff --git a/libjava/classpath/gnu/regexp/REMatchEnumeration.java b/libjava/classpath/gnu/regexp/REMatchEnumeration.java new file mode 100644 index 00000000000..f164a69af4f --- /dev/null +++ b/libjava/classpath/gnu/regexp/REMatchEnumeration.java @@ -0,0 +1,135 @@ +/* gnu/regexp/REMatchEnumeration.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.Serializable; +import java.util.Enumeration; +import java.util.NoSuchElementException; + +/** + * An REMatchEnumeration enumerates regular expression matches over a + * given input text. You obtain a reference to an enumeration using + * the <code>getMatchEnumeration()</code> methods on an instance of + * RE. + * + * <P> + * + * REMatchEnumeration does lazy computation; that is, it will not + * search for a match until it needs to. If you'd rather just get all + * the matches at once in a big array, use the + * <code>getAllMatches()</code> methods on RE. However, using an + * enumeration can help speed performance when the entire text does + * not need to be searched immediately. + * + * <P> + * + * The enumerated type is especially useful when searching on a Reader + * or InputStream, because the InputStream read position cannot be + * guaranteed after calling <code>getMatch()</code> (see the + * description of that method for an explanation of why). Enumeration + * also saves a lot of overhead required when calling + * <code>getMatch()</code> multiple times. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + */ +public class REMatchEnumeration implements Enumeration, Serializable { + private static final int YES = 1; + private static final int MAYBE = 0; + private static final int NO = -1; + + private int more; + private REMatch match; + private RE expr; + private CharIndexed input; + private int eflags; + private int index; + + // Package scope constructor is used by RE.getMatchEnumeration() + REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) { + more = MAYBE; + this.expr = expr; + this.input = input; + this.index = index; + this.eflags = eflags; + } + + /** Returns true if there are more matches in the input text. */ + public boolean hasMoreElements() { + return hasMoreMatches(null); + } + + /** Returns true if there are more matches in the input text. */ + public boolean hasMoreMatches() { + return hasMoreMatches(null); + } + + /** Returns true if there are more matches in the input text. + * Saves the text leading up to the match (or to the end of the input) + * in the specified buffer. + */ + public boolean hasMoreMatches(StringBuffer buffer) { + if (more == MAYBE) { + match = expr.getMatchImpl(input,index,eflags,buffer); + if (match != null) { + input.move((match.end[0] > 0) ? match.end[0] : 1); + + index = (match.end[0] > 0) ? match.end[0] + match.offset : index + 1; + more = YES; + } else more = NO; + } + return (more == YES); + } + + /** Returns the next match in the input text. */ + public Object nextElement() throws NoSuchElementException { + return nextMatch(); + } + + /** + * Returns the next match in the input text. This method is provided + * for convenience to avoid having to explicitly cast the return value + * to class REMatch. + */ + public REMatch nextMatch() throws NoSuchElementException { + if (hasMoreElements()) { + more = (input.isValid()) ? MAYBE : NO; + return match; + } + throw new NoSuchElementException(); + } +} + diff --git a/libjava/classpath/gnu/regexp/RESyntax.java b/libjava/classpath/gnu/regexp/RESyntax.java new file mode 100644 index 00000000000..7272b03481b --- /dev/null +++ b/libjava/classpath/gnu/regexp/RESyntax.java @@ -0,0 +1,527 @@ +/* gnu/regexp/RESyntax.java + Copyright (C) 1998-2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; +import java.io.Serializable; +import java.util.BitSet; + +/** + * An RESyntax specifies the way a regular expression will be compiled. + * This class provides a number of predefined useful constants for + * emulating popular regular expression syntaxes. Additionally the + * user may construct his or her own syntax, using any combination of the + * syntax bit constants. The syntax is an optional argument to any of the + * matching methods on class RE. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + */ + +public final class RESyntax implements Serializable { + static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator"); + + private static final String SYNTAX_IS_FINAL = RE.getLocalizedMessage("syntax.final"); + + private BitSet bits; + + // true for the constant defined syntaxes + private boolean isFinal = false; + + private String lineSeparator = DEFAULT_LINE_SEPARATOR; + + // Values for constants are bit indexes + + /** + * Syntax bit. Backslash is an escape character in lists. + */ + public static final int RE_BACKSLASH_ESCAPE_IN_LISTS = 0; + + /** + * Syntax bit. Use \? instead of ? and \+ instead of +. + */ + public static final int RE_BK_PLUS_QM = 1; + + /** + * Syntax bit. POSIX character classes ([:...:]) in lists are allowed. + */ + public static final int RE_CHAR_CLASSES = 2; + + /** + * Syntax bit. ^ and $ are special everywhere. + * <B>Not implemented.</B> + */ + public static final int RE_CONTEXT_INDEP_ANCHORS = 3; + + /** + * Syntax bit. Repetition operators are only special in valid positions. + * <B>Not implemented.</B> + */ + public static final int RE_CONTEXT_INDEP_OPS = 4; + + /** + * Syntax bit. Repetition and alternation operators are invalid + * at start and end of pattern and other places. + * <B>Not implemented</B>. + */ + public static final int RE_CONTEXT_INVALID_OPS = 5; + + /** + * Syntax bit. Match-any-character operator (.) matches a newline. + */ + public static final int RE_DOT_NEWLINE = 6; + + /** + * Syntax bit. Match-any-character operator (.) does not match a null. + */ + public static final int RE_DOT_NOT_NULL = 7; + + /** + * Syntax bit. Intervals ({x}, {x,}, {x,y}) are allowed. + */ + public static final int RE_INTERVALS = 8; + + /** + * Syntax bit. No alternation (|), match one-or-more (+), or + * match zero-or-one (?) operators. + */ + public static final int RE_LIMITED_OPS = 9; + + /** + * Syntax bit. Newline is an alternation operator. + */ + public static final int RE_NEWLINE_ALT = 10; // impl. + + /** + * Syntax bit. Intervals use { } instead of \{ \} + */ + public static final int RE_NO_BK_BRACES = 11; + + /** + * Syntax bit. Grouping uses ( ) instead of \( \). + */ + public static final int RE_NO_BK_PARENS = 12; + + /** + * Syntax bit. Backreferences not allowed. + */ + public static final int RE_NO_BK_REFS = 13; + + /** + * Syntax bit. Alternation uses | instead of \| + */ + public static final int RE_NO_BK_VBAR = 14; + + /** + * Syntax bit. <B>Not implemented</B>. + */ + public static final int RE_NO_EMPTY_RANGES = 15; + + /** + * Syntax bit. An unmatched right parenthesis (')' or '\)', depending + * on RE_NO_BK_PARENS) will throw an exception when compiling. + */ + public static final int RE_UNMATCHED_RIGHT_PAREN_ORD = 16; + + /** + * Syntax bit. <B>Not implemented.</B> + */ + public static final int RE_HAT_LISTS_NOT_NEWLINE = 17; + + /** + * Syntax bit. Stingy matching is allowed (+?, *?, ??, {x,y}?). + */ + public static final int RE_STINGY_OPS = 18; + + /** + * Syntax bit. Allow character class escapes (\d, \D, \s, \S, \w, \W). + */ + public static final int RE_CHAR_CLASS_ESCAPES = 19; + + /** + * Syntax bit. Allow use of (?:xxx) grouping (subexpression is not saved). + */ + public static final int RE_PURE_GROUPING = 20; + + /** + * Syntax bit. Allow use of (?=xxx) and (?!xxx) apply the subexpression + * to the text following the current position without consuming that text. + */ + public static final int RE_LOOKAHEAD = 21; + + /** + * Syntax bit. Allow beginning- and end-of-string anchors (\A, \Z). + */ + public static final int RE_STRING_ANCHORS = 22; + + /** + * Syntax bit. Allow embedded comments, (?#comment), as in Perl5. + */ + public static final int RE_COMMENTS = 23; + + /** + * Syntax bit. Allow character class escapes within lists, as in Perl5. + */ + public static final int RE_CHAR_CLASS_ESC_IN_LISTS = 24; + + /** + * Syntax bit. Possessive matching is allowed (++, *+, ?+, {x,y}+). + */ + public static final int RE_POSSESSIVE_OPS = 25; + + private static final int BIT_TOTAL = 26; + + /** + * Predefined syntax. + * Emulates regular expression support in the awk utility. + */ + public static final RESyntax RE_SYNTAX_AWK; + + /** + * Predefined syntax. + * Emulates regular expression support in the ed utility. + */ + public static final RESyntax RE_SYNTAX_ED; + + /** + * Predefined syntax. + * Emulates regular expression support in the egrep utility. + */ + public static final RESyntax RE_SYNTAX_EGREP; + + /** + * Predefined syntax. + * Emulates regular expression support in the GNU Emacs editor. + */ + public static final RESyntax RE_SYNTAX_EMACS; + + /** + * Predefined syntax. + * Emulates regular expression support in the grep utility. + */ + public static final RESyntax RE_SYNTAX_GREP; + + /** + * Predefined syntax. + * Emulates regular expression support in the POSIX awk specification. + */ + public static final RESyntax RE_SYNTAX_POSIX_AWK; + + /** + * Predefined syntax. + * Emulates POSIX basic regular expression support. + */ + public static final RESyntax RE_SYNTAX_POSIX_BASIC; + + /** + * Predefined syntax. + * Emulates regular expression support in the POSIX egrep specification. + */ + public static final RESyntax RE_SYNTAX_POSIX_EGREP; + + /** + * Predefined syntax. + * Emulates POSIX extended regular expression support. + */ + public static final RESyntax RE_SYNTAX_POSIX_EXTENDED; + + /** + * Predefined syntax. + * Emulates POSIX basic minimal regular expressions. + */ + public static final RESyntax RE_SYNTAX_POSIX_MINIMAL_BASIC; + + /** + * Predefined syntax. + * Emulates POSIX extended minimal regular expressions. + */ + public static final RESyntax RE_SYNTAX_POSIX_MINIMAL_EXTENDED; + + /** + * Predefined syntax. + * Emulates regular expression support in the sed utility. + */ + public static final RESyntax RE_SYNTAX_SED; + + /** + * Predefined syntax. + * Emulates regular expression support in Larry Wall's perl, version 4, + */ + public static final RESyntax RE_SYNTAX_PERL4; + + /** + * Predefined syntax. + * Emulates regular expression support in Larry Wall's perl, version 4, + * using single line mode (/s modifier). + */ + public static final RESyntax RE_SYNTAX_PERL4_S; // single line mode (/s) + + /** + * Predefined syntax. + * Emulates regular expression support in Larry Wall's perl, version 5. + */ + public static final RESyntax RE_SYNTAX_PERL5; + + /** + * Predefined syntax. + * Emulates regular expression support in Larry Wall's perl, version 5, + * using single line mode (/s modifier). + */ + public static final RESyntax RE_SYNTAX_PERL5_S; + + /** + * Predefined syntax. + * Emulates regular expression support in Java 1.4's java.util.regex + * package. + */ + public static final RESyntax RE_SYNTAX_JAVA_1_4; + + static { + // Define syntaxes + + RE_SYNTAX_EMACS = new RESyntax().makeFinal(); + + RESyntax RE_SYNTAX_POSIX_COMMON = new RESyntax() + .set(RE_CHAR_CLASSES) + .set(RE_DOT_NEWLINE) + .set(RE_DOT_NOT_NULL) + .set(RE_INTERVALS) + .set(RE_NO_EMPTY_RANGES) + .makeFinal(); + + RE_SYNTAX_POSIX_BASIC = new RESyntax(RE_SYNTAX_POSIX_COMMON) + .set(RE_BK_PLUS_QM) + .makeFinal(); + + RE_SYNTAX_POSIX_EXTENDED = new RESyntax(RE_SYNTAX_POSIX_COMMON) + .set(RE_CONTEXT_INDEP_ANCHORS) + .set(RE_CONTEXT_INDEP_OPS) + .set(RE_NO_BK_BRACES) + .set(RE_NO_BK_PARENS) + .set(RE_NO_BK_VBAR) + .set(RE_UNMATCHED_RIGHT_PAREN_ORD) + .makeFinal(); + + RE_SYNTAX_AWK = new RESyntax() + .set(RE_BACKSLASH_ESCAPE_IN_LISTS) + .set(RE_DOT_NOT_NULL) + .set(RE_NO_BK_PARENS) + .set(RE_NO_BK_REFS) + .set(RE_NO_BK_VBAR) + .set(RE_NO_EMPTY_RANGES) + .set(RE_UNMATCHED_RIGHT_PAREN_ORD) + .makeFinal(); + + RE_SYNTAX_POSIX_AWK = new RESyntax(RE_SYNTAX_POSIX_EXTENDED) + .set(RE_BACKSLASH_ESCAPE_IN_LISTS) + .makeFinal(); + + RE_SYNTAX_GREP = new RESyntax() + .set(RE_BK_PLUS_QM) + .set(RE_CHAR_CLASSES) + .set(RE_HAT_LISTS_NOT_NEWLINE) + .set(RE_INTERVALS) + .set(RE_NEWLINE_ALT) + .makeFinal(); + + RE_SYNTAX_EGREP = new RESyntax() + .set(RE_CHAR_CLASSES) + .set(RE_CONTEXT_INDEP_ANCHORS) + .set(RE_CONTEXT_INDEP_OPS) + .set(RE_HAT_LISTS_NOT_NEWLINE) + .set(RE_NEWLINE_ALT) + .set(RE_NO_BK_PARENS) + .set(RE_NO_BK_VBAR) + .makeFinal(); + + RE_SYNTAX_POSIX_EGREP = new RESyntax(RE_SYNTAX_EGREP) + .set(RE_INTERVALS) + .set(RE_NO_BK_BRACES) + .makeFinal(); + + /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */ + + RE_SYNTAX_ED = new RESyntax(RE_SYNTAX_POSIX_BASIC) + .makeFinal(); + + RE_SYNTAX_SED = new RESyntax(RE_SYNTAX_POSIX_BASIC) + .makeFinal(); + + RE_SYNTAX_POSIX_MINIMAL_BASIC = new RESyntax(RE_SYNTAX_POSIX_COMMON) + .set(RE_LIMITED_OPS) + .makeFinal(); + + /* Differs from RE_SYNTAX_POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS + replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */ + + RE_SYNTAX_POSIX_MINIMAL_EXTENDED = new RESyntax(RE_SYNTAX_POSIX_COMMON) + .set(RE_CONTEXT_INDEP_ANCHORS) + .set(RE_CONTEXT_INVALID_OPS) + .set(RE_NO_BK_BRACES) + .set(RE_NO_BK_PARENS) + .set(RE_NO_BK_REFS) + .set(RE_NO_BK_VBAR) + .set(RE_UNMATCHED_RIGHT_PAREN_ORD) + .makeFinal(); + + /* There is no official Perl spec, but here's a "best guess" */ + + RE_SYNTAX_PERL4 = new RESyntax() + .set(RE_BACKSLASH_ESCAPE_IN_LISTS) + .set(RE_CONTEXT_INDEP_ANCHORS) + .set(RE_CONTEXT_INDEP_OPS) // except for '{', apparently + .set(RE_INTERVALS) + .set(RE_NO_BK_BRACES) + .set(RE_NO_BK_PARENS) + .set(RE_NO_BK_VBAR) + .set(RE_NO_EMPTY_RANGES) + .set(RE_CHAR_CLASS_ESCAPES) // \d,\D,\w,\W,\s,\S + .makeFinal(); + + RE_SYNTAX_PERL4_S = new RESyntax(RE_SYNTAX_PERL4) + .set(RE_DOT_NEWLINE) + .makeFinal(); + + RE_SYNTAX_PERL5 = new RESyntax(RE_SYNTAX_PERL4) + .set(RE_PURE_GROUPING) // (?:) + .set(RE_STINGY_OPS) // *?,??,+?,{}? + .set(RE_LOOKAHEAD) // (?=)(?!) + .set(RE_STRING_ANCHORS) // \A,\Z + .set(RE_CHAR_CLASS_ESC_IN_LISTS)// \d,\D,\w,\W,\s,\S within [] + .set(RE_COMMENTS) // (?#) + .makeFinal(); + + RE_SYNTAX_PERL5_S = new RESyntax(RE_SYNTAX_PERL5) + .set(RE_DOT_NEWLINE) + .makeFinal(); + + RE_SYNTAX_JAVA_1_4 = new RESyntax(RE_SYNTAX_PERL5) + // XXX + .set(RE_POSSESSIVE_OPS) // *+,?+,++,{}+ + .makeFinal(); + } + + /** + * Construct a new syntax object with all bits turned off. + * This is equivalent to RE_SYNTAX_EMACS. + */ + public RESyntax() { + bits = new BitSet(BIT_TOTAL); + } + + /** + * Called internally when constructing predefined syntaxes + * so their interpretation cannot vary. Conceivably useful + * for your syntaxes as well. Causes IllegalAccessError to + * be thrown if any attempt to modify the syntax is made. + * + * @return this object for convenient chaining + */ + public RESyntax makeFinal() { + isFinal = true; + return this; + } + + /** + * Construct a new syntax object with all bits set the same + * as the other syntax. + */ + public RESyntax(RESyntax other) { + bits = (BitSet) other.bits.clone(); + } + + /** + * Check if a given bit is set in this syntax. + */ + public boolean get(int index) { + return bits.get(index); + } + + /** + * Set a given bit in this syntax. + * + * @param index the constant (RESyntax.RE_xxx) bit to set. + * @return a reference to this object for easy chaining. + */ + public RESyntax set(int index) { + if (isFinal) throw new IllegalAccessError(SYNTAX_IS_FINAL); + bits.set(index); + return this; + } + + /** + * Clear a given bit in this syntax. + * + * @param index the constant (RESyntax.RE_xxx) bit to clear. + * @return a reference to this object for easy chaining. + */ + public RESyntax clear(int index) { + if (isFinal) throw new IllegalAccessError(SYNTAX_IS_FINAL); + bits.clear(index); + return this; + } + + /** + * Changes the line separator string for regular expressions + * created using this RESyntax. The default separator is the + * value returned by the system property "line.separator", which + * should be correct when reading platform-specific files from a + * filesystem. However, many programs may collect input from + * sources where the line separator is differently specified (for + * example, in the applet environment, the text box widget + * interprets line breaks as single-character newlines, + * regardless of the host platform. + * + * Note that setting the line separator to a character or + * characters that have specific meaning within the current syntax + * can cause unexpected chronosynclastic infundibula. + * + * @return this object for convenient chaining + */ + public RESyntax setLineSeparator(String aSeparator) { + if (isFinal) throw new IllegalAccessError(SYNTAX_IS_FINAL); + lineSeparator = aSeparator; + return this; + } + + /** + * Returns the currently active line separator string. The default + * is the platform-dependent system property "line.separator". + */ + public String getLineSeparator() { + return lineSeparator; + } +} diff --git a/libjava/classpath/gnu/regexp/REToken.java b/libjava/classpath/gnu/regexp/REToken.java new file mode 100644 index 00000000000..4eae9ec473c --- /dev/null +++ b/libjava/classpath/gnu/regexp/REToken.java @@ -0,0 +1,86 @@ +/* gnu/regexp/REToken.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.io.Serializable; + +abstract class REToken implements Serializable { + + protected REToken next = null; + protected REToken uncle = null; + protected int subIndex; + + protected REToken(int subIndex) { + this.subIndex = subIndex; + } + + int getMinimumLength() { + return 0; + } + + void setUncle(REToken anUncle) { + uncle = anUncle; + } + + /** Returns true if the match succeeded, false if it failed. */ + abstract boolean match(CharIndexed input, REMatch mymatch); + + /** Returns true if the rest of the tokens match, false if they fail. */ + protected boolean next(CharIndexed input, REMatch mymatch) { + if (next == null) { + if (uncle == null) { + return true; + } else { + return uncle.match(input, mymatch); + } + } else { + return next.match(input, mymatch); + } + } + + boolean chain(REToken token) { + next = token; + return true; // Token was accepted + } + + abstract void dump(StringBuffer os); + + void dumpAll(StringBuffer os) { + dump(os); + if (next != null) next.dumpAll(os); + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenAny.java b/libjava/classpath/gnu/regexp/RETokenAny.java new file mode 100644 index 00000000000..ac032dcb3bf --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenAny.java @@ -0,0 +1,73 @@ +/* gnu/regexp/RETokenAny.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +final class RETokenAny extends REToken { + /** True if '.' can match a newline (RE_DOT_NEWLINE) */ + private boolean newline; + + /** True if '.' can't match a null (RE_DOT_NOT_NULL) */ + private boolean matchNull; + + RETokenAny(int subIndex, boolean newline, boolean matchNull) { + super(subIndex); + this.newline = newline; + this.matchNull = matchNull; + } + + int getMinimumLength() { + return 1; + } + + boolean match(CharIndexed input, REMatch mymatch) { + char ch = input.charAt(mymatch.index); + if ((ch == CharIndexed.OUT_OF_BOUNDS) + || (!newline && (ch == '\n')) + || (matchNull && (ch == 0))) { + return false; + } + ++mymatch.index; + return next(input, mymatch); + } + + void dump(StringBuffer os) { + os.append('.'); + } +} + diff --git a/libjava/classpath/gnu/regexp/RETokenBackRef.java b/libjava/classpath/gnu/regexp/RETokenBackRef.java new file mode 100644 index 00000000000..674822abd70 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenBackRef.java @@ -0,0 +1,72 @@ +/* gnu/regexp/RETokenBackRef.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +final class RETokenBackRef extends REToken { + private int num; + private boolean insens; + + RETokenBackRef(int subIndex, int num, boolean insens) { + super(subIndex); + this.num = num; + this.insens = insens; + } + + // should implement getMinimumLength() -- any ideas? + + boolean match(CharIndexed input, REMatch mymatch) { + int b,e; + b = mymatch.start[num]; + e = mymatch.end[num]; + if ((b==-1)||(e==-1)) return false; // this shouldn't happen, but... + for (int i=b; i<e; i++) { + if (input.charAt(mymatch.index+i-b) != input.charAt(i)) { + return false; + } + } + mymatch.index += e-b; + return next(input, mymatch); + } + + void dump(StringBuffer os) { + os.append('\\').append(num); + } +} + + diff --git a/libjava/classpath/gnu/regexp/RETokenChar.java b/libjava/classpath/gnu/regexp/RETokenChar.java new file mode 100644 index 00000000000..a15449b2d96 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenChar.java @@ -0,0 +1,91 @@ +/* gnu/regexp/RETokenChar.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +final class RETokenChar extends REToken { + private char[] ch; + private boolean insens; + + RETokenChar(int subIndex, char c, boolean ins) { + super(subIndex); + ch = new char [1]; + ch[0] = (insens = ins) ? Character.toLowerCase(c) : c; + } + + int getMinimumLength() { + return ch.length; + } + + boolean match(CharIndexed input, REMatch mymatch) { + int z = ch.length; + char c; + for (int i=0; i<z; i++) { + c = input.charAt(mymatch.index+i); + if (( (insens) ? Character.toLowerCase(c) : c ) != ch[i]) { + return false; + } + } + mymatch.index += z; + + return next(input, mymatch); + } + + // Overrides REToken.chain() to optimize for strings + boolean chain(REToken next) { + if (next instanceof RETokenChar) { + RETokenChar cnext = (RETokenChar) next; + // assume for now that next can only be one character + int newsize = ch.length + cnext.ch.length; + + char[] chTemp = new char [newsize]; + + System.arraycopy(ch,0,chTemp,0,ch.length); + System.arraycopy(cnext.ch,0,chTemp,ch.length,cnext.ch.length); + + ch = chTemp; + return false; + } else return super.chain(next); + } + + void dump(StringBuffer os) { + os.append(ch); + } +} + + diff --git a/libjava/classpath/gnu/regexp/RETokenEnd.java b/libjava/classpath/gnu/regexp/RETokenEnd.java new file mode 100644 index 00000000000..70483b746a9 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenEnd.java @@ -0,0 +1,75 @@ +/* gnu/regexp/RETokenEnd.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +final class RETokenEnd extends REToken { + /** + * Indicates whether this token should match on a line break. + */ + private String newline; + + RETokenEnd(int subIndex,String newline) { + super(subIndex); + this.newline = newline; + } + + boolean match(CharIndexed input, REMatch mymatch) { + char ch = input.charAt(mymatch.index); + if (ch == CharIndexed.OUT_OF_BOUNDS) + return ((mymatch.eflags & RE.REG_NOTEOL)>0) ? + false : next(input, mymatch); + if (newline != null) { + char z; + int i = 0; // position in newline + do { + z = newline.charAt(i); + if (ch != z) return false; + ++i; + ch = input.charAt(mymatch.index + i); + } while (i < newline.length()); + + return next(input, mymatch); + } + return false; + } + + void dump(StringBuffer os) { + os.append('$'); + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenEndSub.java b/libjava/classpath/gnu/regexp/RETokenEndSub.java new file mode 100644 index 00000000000..f3bb4f2e131 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenEndSub.java @@ -0,0 +1,53 @@ +/* gnu/regexp/RETokenEndSub.java + Copyright (C) 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +final class RETokenEndSub extends REToken { + RETokenEndSub(int subIndex) { + super(subIndex); + } + + boolean match(CharIndexed input, REMatch mymatch) { + mymatch.end[subIndex] = mymatch.index; + return next(input, mymatch); + } + + void dump(StringBuffer os) { + // handled by RE + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenLookAhead.java b/libjava/classpath/gnu/regexp/RETokenLookAhead.java new file mode 100644 index 00000000000..33eaec9fac1 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenLookAhead.java @@ -0,0 +1,87 @@ +/* gnu/regexp/RETokenLookAhead.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +/** + * @since gnu.regexp 1.1.3 + * @author Shashank Bapat + */ +final class RETokenLookAhead extends REToken +{ + REToken re; + boolean negative; + + RETokenLookAhead(REToken re, boolean negative) throws REException { + super(0); + this.re = re; + this.negative = negative; + } + + boolean match(CharIndexed input, REMatch mymatch) + { + REMatch trymatch = (REMatch)mymatch.clone(); + REMatch trymatch1 = (REMatch)mymatch.clone(); + REMatch newMatch = null; + if (re.match(input, trymatch)) { + if (negative) return false; + if (next(input, trymatch1)) + newMatch = trymatch1; + } + + if (newMatch != null) { + if (negative) return false; + //else + mymatch.assignFrom(newMatch); + return true; + } + else { // no match + if (negative) + return next(input, mymatch); + //else + return false; + } + } + + void dump(StringBuffer os) { + os.append("(?"); + os.append(negative ? '!' : '='); + re.dumpAll(os); + os.append(')'); + } +} + diff --git a/libjava/classpath/gnu/regexp/RETokenOneOf.java b/libjava/classpath/gnu/regexp/RETokenOneOf.java new file mode 100644 index 00000000000..3f6e89e2103 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenOneOf.java @@ -0,0 +1,130 @@ +/* gnu/regexp/RETokenOneOf.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; +import java.util.Vector; + +final class RETokenOneOf extends REToken { + private Vector options; + private boolean negative; + + // This constructor is used for convenience when we know the set beforehand, + // e.g. \d --> new RETokenOneOf("0123456789",false, ..) + // \D --> new RETokenOneOf("0123456789",true, ..) + + RETokenOneOf(int subIndex, String optionsStr, boolean negative, boolean insens) { + super(subIndex); + options = new Vector(); + this.negative = negative; + for (int i = 0; i < optionsStr.length(); i++) + options.addElement(new RETokenChar(subIndex,optionsStr.charAt(i),insens)); + } + + RETokenOneOf(int subIndex, Vector options, boolean negative) { + super(subIndex); + this.options = options; + this.negative = negative; + } + + int getMinimumLength() { + int min = Integer.MAX_VALUE; + int x; + for (int i=0; i < options.size(); i++) { + if ((x = ((REToken) options.elementAt(i)).getMinimumLength()) < min) + min = x; + } + return min; + } + + boolean match(CharIndexed input, REMatch mymatch) { + if (negative && (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS)) + return false; + + REMatch newMatch = null; + REMatch last = null; + REToken tk; + boolean isMatch; + for (int i=0; i < options.size(); i++) { + tk = (REToken) options.elementAt(i); + REMatch tryMatch = (REMatch) mymatch.clone(); + if (tk.match(input, tryMatch)) { // match was successful + if (negative) return false; + + if (next(input, tryMatch)) { + // Add tryMatch to list of possibilities. + if (last == null) { + newMatch = tryMatch; + last = tryMatch; + } else { + last.next = tryMatch; + last = tryMatch; + } + } // next succeeds + } // is a match + } // try next option + + if (newMatch != null) { + if (negative) { + return false; + } else { + // set contents of mymatch equal to newMatch + + // try each one that matched + mymatch.assignFrom(newMatch); + return true; + } + } else { + if (negative) { + ++mymatch.index; + return next(input, mymatch); + } else { + return false; + } + } + + // index+1 works for [^abc] lists, not for generic lookahead (--> index) + } + + void dump(StringBuffer os) { + os.append(negative ? "[^" : "(?:"); + for (int i = 0; i < options.size(); i++) { + if (!negative && (i > 0)) os.append('|'); + ((REToken) options.elementAt(i)).dumpAll(os); + } + os.append(negative ? ']' : ')'); + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenPOSIX.java b/libjava/classpath/gnu/regexp/RETokenPOSIX.java new file mode 100644 index 00000000000..bbb8066bca8 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenPOSIX.java @@ -0,0 +1,144 @@ +/* gnu/regexp/RETokenPOSIX.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +final class RETokenPOSIX extends REToken { + int type; + boolean insens; + boolean negated; + + static final int ALNUM = 0; + static final int ALPHA = 1; + static final int BLANK = 2; + static final int CNTRL = 3; + static final int DIGIT = 4; + static final int GRAPH = 5; + static final int LOWER = 6; + static final int PRINT = 7; + static final int PUNCT = 8; + static final int SPACE = 9; + static final int UPPER = 10; + static final int XDIGIT = 11; + + // Array indices correspond to constants defined above. + static final String[] s_nameTable = { + "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower", + "print", "punct", "space", "upper", "xdigit" + }; + + // The RE constructor uses this to look up the constant for a string + static int intValue(String key) { + for (int i = 0; i < s_nameTable.length; i++) { + if (s_nameTable[i].equals(key)) return i; + } + return -1; + } + + RETokenPOSIX(int subIndex, int type, boolean insens, boolean negated) { + super(subIndex); + this.type = type; + this.insens = insens; + this.negated = negated; + } + + int getMinimumLength() { + return 1; + } + + boolean match(CharIndexed input, REMatch mymatch) { + char ch = input.charAt(mymatch.index); + if (ch == CharIndexed.OUT_OF_BOUNDS) + return false; + + boolean retval = false; + switch (type) { + case ALNUM: + // Note that there is some debate over whether '_' should be included + retval = Character.isLetterOrDigit(ch) || (ch == '_'); + break; + case ALPHA: + retval = Character.isLetter(ch); + break; + case BLANK: + retval = ((ch == ' ') || (ch == '\t')); + break; + case CNTRL: + retval = Character.isISOControl(ch); + break; + case DIGIT: + retval = Character.isDigit(ch); + break; + case GRAPH: + retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch))); + break; + case LOWER: + retval = ((insens && Character.isLetter(ch)) || Character.isLowerCase(ch)); + break; + case PRINT: + retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch))) + || (ch == ' '); + break; + case PUNCT: + // This feels sloppy, especially for non-U.S. locales. + retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch)!=-1); + break; + case SPACE: + retval = Character.isWhitespace(ch); + break; + case UPPER: + retval = ((insens && Character.isLetter(ch)) || Character.isUpperCase(ch)); + break; + case XDIGIT: + retval = (Character.isDigit(ch) || ("abcdefABCDEF".indexOf(ch)!=-1)); + break; + } + + if (negated) retval = !retval; + if (retval) { + ++mymatch.index; + return next(input, mymatch); + } + else return false; + } + + void dump(StringBuffer os) { + if (negated) os.append('^'); + os.append("[:" + s_nameTable[type] + ":]"); + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenRange.java b/libjava/classpath/gnu/regexp/RETokenRange.java new file mode 100644 index 00000000000..dadaf2d8072 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenRange.java @@ -0,0 +1,69 @@ +/* gnu/regexp/RETokenRange.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +final class RETokenRange extends REToken { + private char lo, hi; + private boolean insens; + + RETokenRange(int subIndex, char lo, char hi, boolean ins) { + super(subIndex); + this.lo = (insens = ins) ? Character.toLowerCase(lo) : lo; + this.hi = ins ? Character.toLowerCase(hi) : hi; + } + + int getMinimumLength() { + return 1; + } + + boolean match(CharIndexed input, REMatch mymatch) { + char c = input.charAt(mymatch.index); + if (c == CharIndexed.OUT_OF_BOUNDS) return false; + if (insens) c = Character.toLowerCase(c); + if ((c >= lo) && (c <= hi)) { + ++mymatch.index; + return next(input, mymatch); + } + return false; + } + + void dump(StringBuffer os) { + os.append(lo).append('-').append(hi); + } +} + diff --git a/libjava/classpath/gnu/regexp/RETokenRepeated.java b/libjava/classpath/gnu/regexp/RETokenRepeated.java new file mode 100644 index 00000000000..3165a6f1654 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenRepeated.java @@ -0,0 +1,240 @@ +/* gnu/regexp/RETokenRepeated.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +import java.util.Vector; + +final class RETokenRepeated extends REToken { + private REToken token; + private int min,max; + private boolean stingy; + private boolean possessive; + + RETokenRepeated(int subIndex, REToken token, int min, int max) { + super(subIndex); + this.token = token; + this.min = min; + this.max = max; + } + + /** Sets the minimal matching mode to true. */ + void makeStingy() { + stingy = true; + } + + /** Queries if this token has minimal matching enabled. */ + boolean isStingy() { + return stingy; + } + + /** Sets possessive matching mode to true. */ + void makePossessive() { + possessive = true; + } + + /** Queries if this token has possessive matching enabled. */ + boolean isPossessive() { + return possessive; + } + + /** + * The minimum length of a repeated token is the minimum length + * of the token multiplied by the minimum number of times it must + * match. + */ + int getMinimumLength() { + return (min * token.getMinimumLength()); + } + + // We do need to save every possible point, but the number of clone() + // invocations here is really a killer for performance on non-stingy + // repeat operators. I'm open to suggestions... + + // Hypothetical question: can you have a RE that matches 1 times, + // 3 times, 5 times, but not 2 times or 4 times? Does having + // the subexpression back-reference operator allow that? + + boolean match(CharIndexed input, REMatch mymatch) { + // number of times we've matched so far + int numRepeats = 0; + + // Possible positions for the next repeat to match at + REMatch newMatch = mymatch; + REMatch last = null; + REMatch current; + + // Add the '0-repeats' index + // positions.elementAt(z) == position [] in input after <<z>> matches + Vector positions = new Vector(); + positions.addElement(newMatch); + + // Declare variables used in loop + REMatch doables; + REMatch doablesLast; + REMatch recurrent; + + do { + // Check for stingy match for each possibility. + if (stingy && (numRepeats >= min)) { + REMatch result = matchRest(input, newMatch); + if (result != null) { + mymatch.assignFrom(result); + return true; + } + } + + doables = null; + doablesLast = null; + + // try next repeat at all possible positions + for (current = newMatch; current != null; current = current.next) { + recurrent = (REMatch) current.clone(); + if (token.match(input, recurrent)) { + // add all items in current to doables array + if (doables == null) { + doables = recurrent; + doablesLast = recurrent; + } else { + // Order these from longest to shortest + // Start by assuming longest (more repeats) + doablesLast.next = recurrent; + } + // Find new doablesLast + while (doablesLast.next != null) { + doablesLast = doablesLast.next; + } + } + } + // if none of the possibilities worked out, break out of do/while + if (doables == null) break; + + // reassign where the next repeat can match + newMatch = doables; + + // increment how many repeats we've successfully found + ++numRepeats; + + positions.addElement(newMatch); + } while (numRepeats < max); + + // If there aren't enough repeats, then fail + if (numRepeats < min) return false; + + // We're greedy, but ease off until a true match is found + int posIndex = positions.size(); + + // At this point we've either got too many or just the right amount. + // See if this numRepeats works with the rest of the regexp. + REMatch allResults = null; + REMatch allResultsLast = null; + + REMatch results = null; + while (--posIndex >= min) { + newMatch = (REMatch) positions.elementAt(posIndex); + results = matchRest(input, newMatch); + if (results != null) { + if (allResults == null) { + allResults = results; + allResultsLast = results; + } else { + // Order these from longest to shortest + // Start by assuming longest (more repeats) + allResultsLast.next = results; + } + // Find new doablesLast + while (allResultsLast.next != null) { + allResultsLast = allResultsLast.next; + } + } + // else did not match rest of the tokens, try again on smaller sample + // or break out when performing possessive matching + if (possessive) break; + } + if (allResults != null) { + mymatch.assignFrom(allResults); // does this get all? + return true; + } + // If we fall out, no matches. + return false; + } + + private REMatch matchRest(CharIndexed input, final REMatch newMatch) { + REMatch current, single; + REMatch doneIndex = null; + REMatch doneIndexLast = null; + // Test all possible matches for this number of repeats + for (current = newMatch; current != null; current = current.next) { + // clone() separates a single match from the chain + single = (REMatch) current.clone(); + if (next(input, single)) { + // chain results to doneIndex + if (doneIndex == null) { + doneIndex = single; + doneIndexLast = single; + } else { + doneIndexLast.next = single; + } + // Find new doneIndexLast + while (doneIndexLast.next != null) { + doneIndexLast = doneIndexLast.next; + } + } + } + return doneIndex; + } + + void dump(StringBuffer os) { + os.append("(?:"); + token.dumpAll(os); + os.append(')'); + if ((max == Integer.MAX_VALUE) && (min <= 1)) + os.append( (min == 0) ? '*' : '+' ); + else if ((min == 0) && (max == 1)) + os.append('?'); + else { + os.append('{').append(min); + if (max > min) { + os.append(','); + if (max != Integer.MAX_VALUE) os.append(max); + } + os.append('}'); + } + if (stingy) os.append('?'); + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenStart.java b/libjava/classpath/gnu/regexp/RETokenStart.java new file mode 100644 index 00000000000..8f7198237e1 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenStart.java @@ -0,0 +1,87 @@ +/* gnu/regexp/RETokenStart.java + Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +class RETokenStart extends REToken { + private String newline; // matches after a newline + + RETokenStart(int subIndex, String newline) { + super(subIndex); + this.newline = newline; + } + + boolean match(CharIndexed input, REMatch mymatch) { + // charAt(index-n) may be unknown on a Reader/InputStream. FIXME + // Match after a newline if in multiline mode + + if (newline != null) { + int len = newline.length(); + if (mymatch.offset >= len) { + boolean found = true; + char z; + int i = 0; // position in REToken.newline + char ch = input.charAt(mymatch.index - len); + do { + z = newline.charAt(i); + if (ch != z) { + found = false; + break; + } + ++i; + ch = input.charAt(mymatch.index - len + i); + } while (i < len); + + if (found) return next(input, mymatch); + } + } + + // Don't match at all if REG_NOTBOL is set. + if ((mymatch.eflags & RE.REG_NOTBOL) > 0) return false; + + if ((mymatch.eflags & RE.REG_ANCHORINDEX) > 0) + return (mymatch.anchor == mymatch.offset) ? + next(input, mymatch) : false; + else + return ((mymatch.index == 0) && (mymatch.offset == 0)) ? + next(input, mymatch) : false; + } + + void dump(StringBuffer os) { + os.append('^'); + } +} diff --git a/libjava/classpath/gnu/regexp/RETokenWordBoundary.java b/libjava/classpath/gnu/regexp/RETokenWordBoundary.java new file mode 100644 index 00000000000..6804151e261 --- /dev/null +++ b/libjava/classpath/gnu/regexp/RETokenWordBoundary.java @@ -0,0 +1,104 @@ +/* gnu/regexp/RETokenWordBoundary.java + Copyright (C) 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.regexp; + +/** + * Represents a combination lookahead/lookbehind for POSIX [:alnum:]. + */ +final class RETokenWordBoundary extends REToken { + private boolean negated; + private int where; + static final int BEGIN = 1; + static final int END = 2; + + RETokenWordBoundary(int subIndex, int where, boolean negated) { + super(subIndex); + this.where = where; + this.negated = negated; + } + + boolean match(CharIndexed input, REMatch mymatch) { + // Word boundary means input[index-1] was a word character + // and input[index] is not, or input[index] is a word character + // and input[index-1] was not + // In the string "one two three", these positions match: + // |o|n|e| |t|w|o| |t|h|r|e|e| + // ^ ^ ^ ^ ^ ^ + boolean after = false; // is current character a letter or digit? + boolean before = false; // is previous character a letter or digit? + char ch; + + // TODO: Also check REG_ANCHORINDEX vs. anchor + if (((mymatch.eflags & RE.REG_ANCHORINDEX) != RE.REG_ANCHORINDEX) + || (mymatch.offset + mymatch.index > mymatch.anchor)) { + if ((ch = input.charAt(mymatch.index - 1)) != CharIndexed.OUT_OF_BOUNDS) { + before = Character.isLetterOrDigit(ch) || (ch == '_'); + } + } + + if ((ch = input.charAt(mymatch.index)) != CharIndexed.OUT_OF_BOUNDS) { + after = Character.isLetterOrDigit(ch) || (ch == '_'); + } + + // if (before) and (!after), we're at end (\>) + // if (after) and (!before), we're at beginning (\<) + boolean doNext = false; + + if ((where & BEGIN) == BEGIN) { + doNext = after && !before; + } + if ((where & END) == END) { + doNext ^= before && !after; + } + + if (negated) doNext = !doNext; + + return (doNext ? next(input, mymatch) : false); + } + + void dump(StringBuffer os) { + if (where == (BEGIN | END)) { + os.append( negated ? "\\B" : "\\b" ); + } else if (where == BEGIN) { + os.append("\\<"); + } else { + os.append("\\>"); + } + } +} diff --git a/libjava/classpath/gnu/regexp/UncheckedRE.java b/libjava/classpath/gnu/regexp/UncheckedRE.java new file mode 100644 index 00000000000..e6712113828 --- /dev/null +++ b/libjava/classpath/gnu/regexp/UncheckedRE.java @@ -0,0 +1,109 @@ +/* gnu/regexp/UncheckedRE.java + Copyright (C) 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.regexp; + +/** + * UncheckedRE is a subclass of RE that allows programmers an easier means + * of programmatically precompiling regular expressions. It is constructed + * and used in exactly the same manner as an instance of the RE class; the + * only difference is that its constructors do not throw REException. + * Instead, if a syntax error is encountered during construction, a + * RuntimeException will be thrown. + * <P> + * Note that this makes UncheckedRE dangerous if constructed with + * dynamic data. Do not use UncheckedRE unless you are completely sure + * that all input being passed to it contains valid, well-formed + * regular expressions for the syntax specified. + * + * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> + * @see gnu.regexp.RE + * @since gnu.regexp 1.1.4 + */ + +public final class UncheckedRE extends RE { + /** + * Constructs a regular expression pattern buffer without any compilation + * flags set, and using the default syntax (RESyntax.RE_SYNTAX_PERL5). + * + * @param pattern A regular expression pattern, in the form of a String, + * StringBuffer or char[]. Other input types will be converted to + * strings using the toString() method. + * @exception RuntimeException The input pattern could not be parsed. + * @exception NullPointerException The pattern was null. + */ + public UncheckedRE(Object pattern) { + this(pattern,0,RESyntax.RE_SYNTAX_PERL5); + } + + /** + * Constructs a regular expression pattern buffer using the specified + * compilation flags and the default syntax (RESyntax.RE_SYNTAX_PERL5). + * + * @param pattern A regular expression pattern, in the form of a String, + * StringBuffer, or char[]. Other input types will be converted to + * strings using the toString() method. + * @param cflags The logical OR of any combination of the compilation flags in the RE class. + * @exception RuntimeException The input pattern could not be parsed. + * @exception NullPointerException The pattern was null. + */ + public UncheckedRE(Object pattern, int cflags) { + this(pattern,cflags,RESyntax.RE_SYNTAX_PERL5); + } + + /** + * Constructs a regular expression pattern buffer using the specified + * compilation flags and regular expression syntax. + * + * @param pattern A regular expression pattern, in the form of a String, + * StringBuffer, or char[]. Other input types will be converted to + * strings using the toString() method. + * @param cflags The logical OR of any combination of the compilation flags in the RE class. + * @param syntax The type of regular expression syntax to use. + * @exception RuntimeException The input pattern could not be parsed. + * @exception NullPointerException The pattern was null. + */ + public UncheckedRE(Object pattern, int cflags, RESyntax syntax) { + try { + initialize(pattern,cflags,syntax,0,0); + } catch (REException e) { + throw new RuntimeException(e.getMessage()); + } + } +} + + diff --git a/libjava/classpath/gnu/test/.cvsignore b/libjava/classpath/gnu/test/.cvsignore new file mode 100644 index 00000000000..70845e08eb0 --- /dev/null +++ b/libjava/classpath/gnu/test/.cvsignore @@ -0,0 +1 @@ +Makefile.in diff --git a/libjava/classpath/gnu/test/Fail.java b/libjava/classpath/gnu/test/Fail.java new file mode 100644 index 00000000000..f1c4a0cd2de --- /dev/null +++ b/libjava/classpath/gnu/test/Fail.java @@ -0,0 +1,57 @@ +/* Fail.java -- Result code returned when test failed but was expected to + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test failed but was expected to pass. + */ +public class Fail extends Result +{ + /** + * Constructs a Fail result code with additional information. + */ + public Fail(String msg) { + super("FAIL", msg); + } + /** + * Constructs a Fail result code. + */ + public Fail() { + this(""); + } +} diff --git a/libjava/classpath/gnu/test/Makefile.am b/libjava/classpath/gnu/test/Makefile.am new file mode 100644 index 00000000000..49bc66916f5 --- /dev/null +++ b/libjava/classpath/gnu/test/Makefile.am @@ -0,0 +1,5 @@ +## Input file for automake to generate the Makefile.in used by configure + +##gnutestdir = $(datadir)/gnu/test/ + +##gnutest_JAVA = Fail.java Test.java Untested.java Pass.java Unresolved.java XFail.java Result.java Unsupported.java XPass.java diff --git a/libjava/classpath/gnu/test/Pass.java b/libjava/classpath/gnu/test/Pass.java new file mode 100644 index 00000000000..abec54ef602 --- /dev/null +++ b/libjava/classpath/gnu/test/Pass.java @@ -0,0 +1,57 @@ +/* Pass.java -- Result code returned when test passed and was excepted to + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test passed and was excepted to pass. + */ +public class Pass extends Result +{ + /** + * Constructs a Pass result code with additional information. + */ + public Pass(String msg) { + super("PASS", msg); + } + /** + * Constructs a Pass result code. + */ + public Pass() { + this(""); + } +} diff --git a/libjava/classpath/gnu/test/Result.java b/libjava/classpath/gnu/test/Result.java new file mode 100644 index 00000000000..908d4b48595 --- /dev/null +++ b/libjava/classpath/gnu/test/Result.java @@ -0,0 +1,85 @@ +/* Result.java -- Abstract base class for all Result types. + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Class which all usable Result objects extend. + */ +public abstract class Result +{ + String name, msg; + + /** + * Create a result of a given type, with a given message. + * + * @param name name of type + * @param msg message + */ + public Result(String name, String msg) { + this.name = name; + this.msg = msg; + } + + /** + * Create a result of a given type. + * + * @param name name of type + */ + public Result(String name) { + this(name, ""); + } + + /** + * Returns the name of the type. + */ + public String getName() { + return name; + } + + /** + * Returns the message associated with this instance of Result, or + * the empty string if no message exists. + */ + public String getMsg() { + return (msg != null) ? msg : ""; + } + + public String toString() { + return getName() + ":" + getMsg(); + } +} diff --git a/libjava/classpath/gnu/test/Test.java b/libjava/classpath/gnu/test/Test.java new file mode 100644 index 00000000000..6b2e18df1a0 --- /dev/null +++ b/libjava/classpath/gnu/test/Test.java @@ -0,0 +1,57 @@ +/* Test.java -- Interface representing a single test. + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Interface which all GNU Classpath tests must implement. + * The method <code>test</code> is invoked once for each test. + */ +public interface Test +{ + /** + * Returns the name of the test. + */ + public String getName(); + + /** + * Performs a test. + * + * @return result from running the test + */ + public Result test(); +} diff --git a/libjava/classpath/gnu/test/Unresolved.java b/libjava/classpath/gnu/test/Unresolved.java new file mode 100644 index 00000000000..280d8b69945 --- /dev/null +++ b/libjava/classpath/gnu/test/Unresolved.java @@ -0,0 +1,57 @@ +/* Unresolved.java - Result code returned when test gives indeterminate results. + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test produced indeterminate results. + */ +public class Unresolved extends Result +{ + /** + * Constructs an Unresolved result code with additional information. + */ + public Unresolved(String msg) { + super("UNRESOLVED", msg); + } + /** + * Constructs an Unresolved result code. + */ + public Unresolved() { + this(""); + } +} diff --git a/libjava/classpath/gnu/test/Unsupported.java b/libjava/classpath/gnu/test/Unsupported.java new file mode 100644 index 00000000000..c6dabb09af7 --- /dev/null +++ b/libjava/classpath/gnu/test/Unsupported.java @@ -0,0 +1,59 @@ +/* Unsupported.java -- Result code returned when test does not have the + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test does not have the required support to run. For example, + * Unsupported could be returned when a needed resource + * (ie. multicasting) is not available. + */ +public class Unsupported extends Result +{ + /** + * Constructs an Unsupported result code with additional information. + */ + public Unsupported(String msg) { + super("UNSUPPORTED", msg); + } + /** + * Constructs an Unsupported result code. + */ + public Unsupported() { + this(""); + } +} diff --git a/libjava/classpath/gnu/test/Untested.java b/libjava/classpath/gnu/test/Untested.java new file mode 100644 index 00000000000..5627fa8ca96 --- /dev/null +++ b/libjava/classpath/gnu/test/Untested.java @@ -0,0 +1,57 @@ +/* Untested.java -- Result code returned when test was not run -- placeholder. + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test was not run -- a placeholder. + */ +public class Untested extends Result +{ + /** + * Constructs an Untested result code with additional information. + */ + public Untested(String msg) { + super("UNTESTED", msg); + } + /** + * Constructs an Untested result code. + */ + public Untested() { + this(""); + } +} diff --git a/libjava/classpath/gnu/test/XFail.java b/libjava/classpath/gnu/test/XFail.java new file mode 100644 index 00000000000..793ff117f28 --- /dev/null +++ b/libjava/classpath/gnu/test/XFail.java @@ -0,0 +1,57 @@ +/* XFail.java - Result code returned when test failed and was expected to fail. + Copyright (c) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test failed and was expected to fail. + */ +public class XFail extends Result +{ + /** + * Constructs an XFail result code with additional information. + */ + public XFail(String msg) { + super("XFAIL", msg); + } + /** + * Constructs an XFail result code. + */ + public XFail() { + this(""); + } +} diff --git a/libjava/classpath/gnu/test/XPass.java b/libjava/classpath/gnu/test/XPass.java new file mode 100644 index 00000000000..b41dd1c3788 --- /dev/null +++ b/libjava/classpath/gnu/test/XPass.java @@ -0,0 +1,56 @@ +/* XPass.java - Result code returned when test passed but was expected to fail. + Copyright (c) 1998 Free Software Foundation, Inc. +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.test; + +/** + * Test passed but was expected to fail. + */ +public class XPass extends Result +{ + /** + * Constructs an XPass result code with additional information. + */ + public XPass(String msg) { + super("XPASS", msg); + } + /** + * Constructs an XPass result code. + */ + public XPass() { + this(""); + } +} diff --git a/libjava/classpath/gnu/xml/aelfred2/ContentHandler2.java b/libjava/classpath/gnu/xml/aelfred2/ContentHandler2.java new file mode 100644 index 00000000000..15161383592 --- /dev/null +++ b/libjava/classpath/gnu/xml/aelfred2/ContentHandler2.java @@ -0,0 +1,65 @@ +/* ContentHandler2.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.aelfred2; + +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; + +/** + * Extension to the SAX ContentHandler interface to report parsing events + * and parameters required by DOM Level 3 but not supported by SAX. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public interface ContentHandler2 + extends ContentHandler +{ + + /** + * Reports the XML declaration. + * @param version the value of the version attribute in the XML + * declaration + * @param encoding the encoding specified in the XML declaration, if any + * @param standalone the standalone attribute from the XML declaration + * @param inputEncoding the encoding of the XML input + */ + void xmlDecl(String version, String encoding, boolean standalone, + String inputEncoding) + throws SAXException; + +} diff --git a/libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java b/libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java new file mode 100644 index 00000000000..37e8cc9c120 --- /dev/null +++ b/libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java @@ -0,0 +1,231 @@ +/* JAXPFactory.java -- + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.aelfred2; + +import java.util.Enumeration; +import java.util.Hashtable; + +import org.xml.sax.Parser; +import org.xml.sax.XMLReader; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.helpers.XMLReaderAdapter; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + + +/** + * Configurable factory to create an Ælfred2 JAXP parser; required + * to bootstrap using JAXP. You should use SAX2 directly where possible, + * rather than through JAXP, since that gives you better control. + * This class would normally be configured as a platform default factory. + * + * @author David Brownell + */ +public final class JAXPFactory + extends SAXParserFactory +{ + + private Hashtable flags = new Hashtable(); + + /** + * Constructs a factory which normally returns a non-validating + * parser. + */ + public JAXPFactory() + { + } + + public SAXParser newSAXParser() + throws ParserConfigurationException, SAXException + { + JaxpParser jaxp = new JaxpParser(); + Enumeration e = flags.keys(); + XMLReader parser = jaxp.getXMLReader(); + + parser.setFeature(SAXDriver.FEATURE + "namespaces", + isNamespaceAware()); + parser.setFeature(SAXDriver.FEATURE + "validation", + isValidating()); + // that makes SAX2 feature flags trump JAXP + + while (e.hasMoreElements()) + { + String uri = (String) e.nextElement(); + Boolean value = (Boolean) flags.get(uri); + parser.setFeature(uri, value.booleanValue()); + } + + return jaxp; + } + + // yes, this "feature transfer" mechanism doesn't play well + + public void setFeature(String name, boolean value) + throws ParserConfigurationException, SAXNotRecognizedException, + SAXNotSupportedException + { + try + { + // force "early" detection of errors where possible + // (flags can't necessarily be set before parsing) + new JaxpParser().getXMLReader().setFeature(name, value); + + flags.put(name, new Boolean(value)); + } + catch (SAXNotRecognizedException e) + { + throw new SAXNotRecognizedException(name); + } + catch (SAXNotSupportedException e) + { + throw new SAXNotSupportedException(name); + } + catch (Exception e) + { + throw new ParserConfigurationException(e.getClass().getName() + + ": " + + e.getMessage()); + } + } + + public boolean getFeature(String name) + throws ParserConfigurationException, SAXNotRecognizedException, + SAXNotSupportedException + { + Boolean value = (Boolean) flags.get(name); + + if (value != null) + { + return value.booleanValue(); + } + else + { + try + { + return new JaxpParser().getXMLReader().getFeature(name); + } + catch (SAXNotRecognizedException e) + { + throw new SAXNotRecognizedException(name); + } + catch (SAXNotSupportedException e) + { + throw new SAXNotSupportedException(name); + } + catch (SAXException e) + { + throw new ParserConfigurationException(e.getClass().getName() + + ": " + + e.getMessage()); + } + } + } + + private static class JaxpParser + extends SAXParser + { + + private XmlReader ae2 = new XmlReader(); + private XMLReaderAdapter parser = null; + + JaxpParser() + { + } + + public void setProperty(String id, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + ae2.setProperty(id, value); + } + + public Object getProperty(String id) + throws SAXNotRecognizedException, SAXNotSupportedException + { + return ae2.getProperty(id); + } + + public Parser getParser() + throws SAXException + { + if (parser == null) + { + parser = new XMLReaderAdapter(ae2); + } + return parser; + } + + public XMLReader getXMLReader () + throws SAXException + { + return ae2; + } + + public boolean isNamespaceAware() + { + try + { + return ae2.getFeature(SAXDriver.FEATURE + "namespaces"); + } + catch (Exception e) + { + throw new Error(); + } + } + + public boolean isValidating() + { + try + { + return ae2.getFeature(SAXDriver.FEATURE + "validation"); + } + catch (Exception e) + { + throw new Error(); + } + } + + // TODO isXIncludeAware() + + } + +} + diff --git a/libjava/classpath/gnu/xml/aelfred2/SAXDriver.java b/libjava/classpath/gnu/xml/aelfred2/SAXDriver.java new file mode 100644 index 00000000000..7e950ce042a --- /dev/null +++ b/libjava/classpath/gnu/xml/aelfred2/SAXDriver.java @@ -0,0 +1,1622 @@ +/* SAXDriver.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + +Portions derived from code which carried the following notice: + + Copyright (c) 1997, 1998 by Microstar Software Ltd. + + AElfred is free for both commercial and non-commercial use and + redistribution, provided that Microstar's copyright and disclaimer are + retained intact. You are free to modify AElfred for your own use and + to redistribute AElfred with your modifications, provided that the + modifications are clearly documented. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + merchantability or fitness for a particular purpose. Please use it AT + YOUR OWN RISK. +*/ + +package gnu.xml.aelfred2; + +import java.io.*; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Locale; +import java.util.Stack; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; + +import org.xml.sax.*; +import org.xml.sax.ext.*; +import org.xml.sax.helpers.NamespaceSupport; + + +/** + * An enhanced SAX2 version of Microstar's Ælfred XML parser. + * The enhancements primarily relate to significant improvements in + * conformance to the XML specification, and SAX2 support. Performance + * has been improved. See the package level documentation for more + * information. + * + * <table border="1" width='100%' cellpadding='3' cellspacing='0'> + * <tr bgcolor='#ccccff'> + * <th><font size='+1'>Name</font></th> + * <th><font size='+1'>Notes</font></th></tr> + * + * <tr><td colspan=2><center><em>Features ... URL prefix is + * <b>http://xml.org/sax/features/</b></em></center></td></tr> + * + * <tr><td>(URL)/external-general-entities</td> + * <td>Value defaults to <em>true</em></td></tr> + * <tr><td>(URL)/external-parameter-entities</td> + * <td>Value defaults to <em>true</em></td></tr> + * <tr><td>(URL)/is-standalone</td> + * <td>(PRELIMINARY) Returns true iff the document's parsing + * has started (some non-error event after <em>startDocument()</em> + * was reported) and the document's standalone flag is set.</td></tr> + * <tr><td>(URL)/namespace-prefixes</td> + * <td>Value defaults to <em>false</em> (but XML 1.0 names are + * always reported)</td></tr> + * <tr><td>(URL)/lexical-handler/parameter-entities</td> + * <td>Value is fixed at <em>true</em></td></tr> + * <tr><td>(URL)/namespaces</td> + * <td>Value defaults to <em>true</em></td></tr> + * <tr><td>(URL)/resolve-dtd-uris</td> + * <td>(PRELIMINARY) Value defaults to <em>true</em></td></tr> + * <tr><td>(URL)/string-interning</td> + * <td>Value is fixed at <em>true</em></td></tr> + * <tr><td>(URL)/use-attributes2</td> + * <td>(PRELIMINARY) Value is fixed at <em>true</em></td></tr> + * <tr><td>(URL)/use-entity-resolver2</td> + * <td>(PRELIMINARY) Value defaults to <em>true</em></td></tr> + * <tr><td>(URL)/validation</td> + * <td>Value is fixed at <em>false</em></td></tr> + * + * <tr><td colspan=2><center><em>Handler Properties ... URL prefix is + * <b>http://xml.org/sax/properties/</b></em></center></td></tr> + * + * <tr><td>(URL)/declaration-handler</td> + * <td>A declaration handler may be provided. </td></tr> + * <tr><td>(URL)/lexical-handler</td> + * <td>A lexical handler may be provided. </td></tr> + * </table> + * + * <p>This parser currently implements the SAX1 Parser API, but + * it may not continue to do so in the future. + * + * @author Written by David Megginson (version 1.2a from Microstar) + * @author Updated by David Brownell <dbrownell@users.sourceforge.net> + * @see org.xml.sax.Parser + */ +final public class SAXDriver + implements Locator, Attributes2, XMLReader, Parser, AttributeList +{ + + private final DefaultHandler2 base = new DefaultHandler2(); + private XmlParser parser; + + private EntityResolver entityResolver = base; + private EntityResolver2 resolver2 = null; + private ContentHandler contentHandler = base; + private DTDHandler dtdHandler = base; + private ErrorHandler errorHandler = base; + private DeclHandler declHandler = base; + private LexicalHandler lexicalHandler = base; + + private String elementName; + private Stack entityStack; + + // one vector (of object/struct): faster, smaller + private List attributesList; + + private boolean namespaces = true; + private boolean xmlNames = false; + private boolean extGE = true; + private boolean extPE = true; + private boolean resolveAll = true; + private boolean useResolver2 = true; + + // package private to allow (read-only) access in XmlParser + boolean stringInterning = true; + + private int attributeCount; + private boolean attributes; + private String[] nsTemp; + private NamespaceSupport prefixStack; + + // + // Constructor. + // + + /** + * Constructs a SAX Parser. + */ + public SAXDriver() + { + reset(); + } + + private void reset() + { + elementName = null; + entityStack = new Stack(); + attributesList = Collections.synchronizedList(new ArrayList()); + attributeCount = 0; + attributes = false; + nsTemp = new String[3]; + prefixStack = null; + } + + + // + // Implementation of org.xml.sax.Parser. + // + + /** + * <b>SAX1</b>: Sets the locale used for diagnostics; currently, + * only locales using the English language are supported. + * @param locale The locale for which diagnostics will be generated + */ + public void setLocale(Locale locale) + throws SAXException + { + if ("en".equals(locale.getLanguage())) + { + return; + } + throw new SAXException ("AElfred2 only supports English locales."); + } + + /** + * <b>SAX2</b>: Returns the object used when resolving external + * entities during parsing (both general and parameter entities). + */ + public EntityResolver getEntityResolver() + { + return (entityResolver == base) ? null : entityResolver; + } + + /** + * <b>SAX1, SAX2</b>: Set the entity resolver for this parser. + * @param handler The object to receive entity events. + */ + public void setEntityResolver(EntityResolver resolver) + { + if (resolver instanceof EntityResolver2) + { + resolver2 = (EntityResolver2) resolver; + } + else + { + resolver2 = null; + } + if (resolver == null) + { + resolver = base; + } + entityResolver = resolver; + } + + /** + * <b>SAX2</b>: Returns the object used to process declarations related + * to notations and unparsed entities. + */ + public DTDHandler getDTDHandler() + { + return (dtdHandler == base) ? null : dtdHandler; + } + + /** + * <b>SAX1, SAX2</b>: Set the DTD handler for this parser. + * @param handler The object to receive DTD events. + */ + public void setDTDHandler(DTDHandler handler) + { + if (handler == null) + { + handler = base; + } + this.dtdHandler = handler; + } + + + /** + * <b>SAX1</b>: Set the document handler for this parser. If a + * content handler was set, this document handler will supplant it. + * The parser is set to report all XML 1.0 names rather than to + * filter out "xmlns" attributes (the "namespace-prefixes" feature + * is set to true). + * + * @deprecated SAX2 programs should use the XMLReader interface + * and a ContentHandler. + * + * @param handler The object to receive document events. + */ + public void setDocumentHandler(DocumentHandler handler) + { + contentHandler = new Adapter(handler); + xmlNames = true; + } + + /** + * <b>SAX2</b>: Returns the object used to report the logical + * content of an XML document. + */ + public ContentHandler getContentHandler() + { + return (contentHandler == base) ? null : contentHandler; + } + + /** + * <b>SAX2</b>: Assigns the object used to report the logical + * content of an XML document. If a document handler was set, + * this content handler will supplant it (but XML 1.0 style name + * reporting may remain enabled). + */ + public void setContentHandler(ContentHandler handler) + { + if (handler == null) + { + handler = base; + } + contentHandler = handler; + } + + /** + * <b>SAX1, SAX2</b>: Set the error handler for this parser. + * @param handler The object to receive error events. + */ + public void setErrorHandler(ErrorHandler handler) + { + if (handler == null) + { + handler = base; + } + this.errorHandler = handler; + } + + /** + * <b>SAX2</b>: Returns the object used to receive callbacks for XML + * errors of all levels (fatal, nonfatal, warning); this is never null; + */ + public ErrorHandler getErrorHandler() + { + return (errorHandler == base) ? null : errorHandler; + } + + /** + * <b>SAX1, SAX2</b>: Auxiliary API to parse an XML document, used mostly + * when no URI is available. + * If you want anything useful to happen, you should set + * at least one type of handler. + * @param source The XML input source. Don't set 'encoding' unless + * you know for a fact that it's correct. + * @see #setEntityResolver + * @see #setDTDHandler + * @see #setContentHandler + * @see #setErrorHandler + * @exception SAXException The handlers may throw any SAXException, + * and the parser normally throws SAXParseException objects. + * @exception IOException IOExceptions are normally through through + * the parser if there are problems reading the source document. + */ + public void parse(InputSource source) + throws SAXException, IOException + { + synchronized (base) + { + parser = new XmlParser(); + if (namespaces) + { + prefixStack = new NamespaceSupport(); + } + else if (!xmlNames) + { + throw new IllegalStateException(); + } + parser.setHandler(this); + + try + { + Reader r = source.getCharacterStream(); + InputStream in = source.getByteStream(); + + parser.doParse(source.getSystemId(), + source.getPublicId(), + r, + in, + source.getEncoding()); + } + catch (SAXException e) + { + throw e; + } + catch (IOException e) + { + throw e; + } + catch (RuntimeException e) + { + throw e; + } + catch (Exception e) + { + throw new SAXParseException(e.getMessage(), this, e); + } + finally + { + contentHandler.endDocument(); + reset(); + } + } + } + + /** + * <b>SAX1, SAX2</b>: Preferred API to parse an XML document, using a + * system identifier (URI). + */ + public void parse(String systemId) + throws SAXException, IOException + { + parse(new InputSource(systemId)); + } + + // + // Implementation of SAX2 "XMLReader" interface + // + static final String FEATURE = "http://xml.org/sax/features/"; + static final String PROPERTY = "http://xml.org/sax/properties/"; + + /** + * <b>SAX2</b>: Tells the value of the specified feature flag. + * + * @exception SAXNotRecognizedException thrown if the feature flag + * is neither built in, nor yet assigned. + */ + public boolean getFeature(String featureId) + throws SAXNotRecognizedException, SAXNotSupportedException + { + if ((FEATURE + "validation").equals(featureId)) + { + return false; + } + + // external entities (both types) are optionally included + if ((FEATURE + "external-general-entities").equals(featureId)) + { + return extGE; + } + if ((FEATURE + "external-parameter-entities").equals(featureId)) + { + return extPE; + } + + // element/attribute names are as written in document; no mangling + if ((FEATURE + "namespace-prefixes").equals(featureId)) + { + return xmlNames; + } + + // report element/attribute namespaces? + if ((FEATURE + "namespaces").equals(featureId)) + { + return namespaces; + } + + // all PEs and GEs are reported + if ((FEATURE + "lexical-handler/parameter-entities").equals(featureId)) + { + return true; + } + + // default is true + if ((FEATURE + "string-interning").equals(featureId)) + { + return stringInterning; + } + + // EXTENSIONS 1.1 + + // always returns isSpecified info + if ((FEATURE + "use-attributes2").equals(featureId)) + { + return true; + } + + // meaningful between startDocument/endDocument + if ((FEATURE + "is-standalone").equals(featureId)) + { + if (parser == null) + { + throw new SAXNotSupportedException(featureId); + } + return parser.isStandalone(); + } + + // optionally don't absolutize URIs in declarations + if ((FEATURE + "resolve-dtd-uris").equals(featureId)) + { + return resolveAll; + } + + // optionally use resolver2 interface methods, if possible + if ((FEATURE + "use-entity-resolver2").equals(featureId)) + { + return useResolver2; + } + + throw new SAXNotRecognizedException(featureId); + } + + // package private + DeclHandler getDeclHandler() + { + return declHandler; + } + + // package private + boolean resolveURIs() + { + return resolveAll; + } + + /** + * <b>SAX2</b>: Returns the specified property. + * + * @exception SAXNotRecognizedException thrown if the property value + * is neither built in, nor yet stored. + */ + public Object getProperty(String propertyId) + throws SAXNotRecognizedException + { + if ((PROPERTY + "declaration-handler").equals(propertyId)) + { + return (declHandler == base) ? null : declHandler; + } + + if ((PROPERTY + "lexical-handler").equals(propertyId)) + { + return (lexicalHandler == base) ? null : lexicalHandler; + } + + // unknown properties + throw new SAXNotRecognizedException(propertyId); + } + + /** + * <b>SAX2</b>: Sets the state of feature flags in this parser. Some + * built-in feature flags are mutable. + */ + public void setFeature(String featureId, boolean value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + boolean state; + + // Features with a defined value, we just change it if we can. + state = getFeature (featureId); + + if (state == value) + { + return; + } + if (parser != null) + { + throw new SAXNotSupportedException("not while parsing"); + } + + if ((FEATURE + "namespace-prefixes").equals(featureId)) + { + // in this implementation, this only affects xmlns reporting + xmlNames = value; + // forcibly prevent illegal parser state + if (!xmlNames) + { + namespaces = true; + } + return; + } + + if ((FEATURE + "namespaces").equals(featureId)) + { + namespaces = value; + // forcibly prevent illegal parser state + if (!namespaces) + { + xmlNames = true; + } + return; + } + + if ((FEATURE + "external-general-entities").equals(featureId)) + { + extGE = value; + return; + } + if ((FEATURE + "external-parameter-entities").equals(featureId)) + { + extPE = value; + return; + } + if ((FEATURE + "resolve-dtd-uris").equals(featureId)) + { + resolveAll = value; + return; + } + + if ((FEATURE + "use-entity-resolver2").equals(featureId)) + { + useResolver2 = value; + return; + } + + throw new SAXNotRecognizedException(featureId); + } + + /** + * <b>SAX2</b>: Assigns the specified property. Like SAX1 handlers, + * these may be changed at any time. + */ + public void setProperty(String propertyId, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + // see if the property is recognized + getProperty(propertyId); + + // Properties with a defined value, we just change it if we can. + + if ((PROPERTY + "declaration-handler").equals(propertyId)) + { + if (value == null) + { + declHandler = base; + } + else if (!(value instanceof DeclHandler)) + { + throw new SAXNotSupportedException(propertyId); + } + else + { + declHandler = (DeclHandler) value; + } + return ; + } + + if ((PROPERTY + "lexical-handler").equals(propertyId)) + { + if (value == null) + { + lexicalHandler = base; + } + else if (!(value instanceof LexicalHandler)) + { + throw new SAXNotSupportedException(propertyId); + } + else + { + lexicalHandler = (LexicalHandler) value; + } + return; + } + + throw new SAXNotSupportedException(propertyId); + } + + // + // This is where the driver receives XmlParser callbacks and translates + // them into SAX callbacks. Some more callbacks have been added for + // SAX2 support. + // + + void startDocument() + throws SAXException + { + contentHandler.setDocumentLocator(this); + contentHandler.startDocument(); + attributesList.clear(); + } + + void xmlDecl(String version, + String encoding, + boolean standalone, + String inputEncoding) + throws SAXException + { + if (contentHandler instanceof ContentHandler2) + { + ((ContentHandler2) contentHandler).xmlDecl(version, + encoding, + standalone, + inputEncoding); + } + } + + void skippedEntity(String name) + throws SAXException + { + contentHandler.skippedEntity(name); + } + + InputSource getExternalSubset(String name, String baseURI) + throws SAXException, IOException + { + if (resolver2 == null || !useResolver2 || !extPE) + { + return null; + } + return resolver2.getExternalSubset(name, baseURI); + } + + InputSource resolveEntity(boolean isPE, String name, + InputSource in, String baseURI) + throws SAXException, IOException + { + InputSource source; + + // external entities might be skipped + if (isPE && !extPE) + { + return null; + } + if (!isPE && !extGE) + { + return null; + } + + // ... or not + lexicalHandler.startEntity(name); + if (resolver2 != null && useResolver2) + { + source = resolver2.resolveEntity(name, in.getPublicId(), + baseURI, in.getSystemId()); + if (source == null) + { + in.setSystemId(absolutize(baseURI, + in.getSystemId(), false)); + source = in; + } + } + else + { + in.setSystemId(absolutize(baseURI, in.getSystemId(), false)); + source = entityResolver.resolveEntity(in.getPublicId(), + in.getSystemId()); + if (source == null) + { + source = in; + } + } + startExternalEntity(name, source.getSystemId(), true); + return source; + } + + // absolutize a system ID relative to the specified base URI + // (temporarily) package-visible for external entity decls + String absolutize(String baseURI, String systemId, boolean nice) + throws MalformedURLException, SAXException + { + // FIXME normalize system IDs -- when? + // - Convert to UTF-8 + // - Map reserved and non-ASCII characters to %HH + + try + { + if (baseURI == null) + { + if (XmlParser.uriWarnings) + { + warn ("No base URI; hope this SYSTEM id is absolute: " + + systemId); + } + return new URL(systemId).toString(); + } + else + { + return new URL(new URL(baseURI), systemId).toString(); + } + } + catch (MalformedURLException e) + { + // Let unknown URI schemes pass through unless we need + // the JVM to map them to i/o streams for us... + if (!nice) + { + throw e; + } + + // sometimes sysids for notations or unparsed entities + // aren't really URIs... + warn("Can't absolutize SYSTEM id: " + e.getMessage()); + return systemId; + } + } + + void startExternalEntity(String name, String systemId, boolean stackOnly) + throws SAXException + { + // The following warning was deleted because the application has the + // option of not setting systemId. Sun's JAXP or Xerces seems to + // ignore this case. + /* + if (systemId == null) + warn ("URI was not reported to parser for entity " + name); + */ + if (!stackOnly) // spliced [dtd] needs startEntity + { + lexicalHandler.startEntity(name); + } + entityStack.push(systemId); + } + + void endExternalEntity(String name) + throws SAXException + { + if (!"[document]".equals(name)) + { + lexicalHandler.endEntity(name); + } + entityStack.pop(); + } + + void startInternalEntity(String name) + throws SAXException + { + lexicalHandler.startEntity(name); + } + + void endInternalEntity(String name) + throws SAXException + { + lexicalHandler.endEntity(name); + } + + void doctypeDecl(String name, String publicId, String systemId) + throws SAXException + { + lexicalHandler.startDTD(name, publicId, systemId); + + // ... the "name" is a declaration and should be given + // to the DeclHandler (but sax2 doesn't). + + // the IDs for the external subset are lexical details, + // as are the contents of the internal subset; but sax2 + // doesn't provide the internal subset "pre-parse" + } + + void notationDecl(String name, String publicId, String systemId, + String baseUri) + throws SAXException + { + try + { + dtdHandler.notationDecl(name, publicId, + (resolveAll && systemId != null) + ? absolutize(baseUri, systemId, true) + : systemId); + } + catch (IOException e) + { + // "can't happen" + throw new SAXParseException(e.getMessage(), this, e); + } + } + + void unparsedEntityDecl(String name, String publicId, String systemId, + String baseUri, String notation) + throws SAXException + { + try + { + dtdHandler.unparsedEntityDecl(name, publicId, + resolveAll + ? absolutize(baseUri, systemId, true) + : systemId, + notation); + } + catch (IOException e) + { + // "can't happen" + throw new SAXParseException(e.getMessage(), this, e); + } + } + + void endDoctype() + throws SAXException + { + lexicalHandler.endDTD(); + } + + private void declarePrefix(String prefix, String uri) + throws SAXException + { + int index = uri.indexOf(':'); + + // many versions of nwalsh docbook stylesheets + // have bogus URLs; so this can't be an error... + if (index < 1 && uri.length() != 0) + { + warn("relative URI for namespace: " + uri); + } + + // FIXME: char [0] must be ascii alpha; chars [1..index] + // must be ascii alphanumeric or in "+-." [RFC 2396] + + //Namespace Constraints + //name for xml prefix must be http://www.w3.org/XML/1998/namespace + boolean prefixEquality = prefix.equals("xml"); + boolean uriEquality = uri.equals("http://www.w3.org/XML/1998/namespace"); + if ((prefixEquality || uriEquality) && !(prefixEquality && uriEquality)) + { + fatal("xml is by definition bound to the namespace name " + + "http://www.w3.org/XML/1998/namespace"); + } + + //xmlns prefix declaration is illegal but xml prefix declaration is llegal... + if (prefixEquality && uriEquality) + { + return; + } + + //name for xmlns prefix must be http://www.w3.org/2000/xmlns/ + prefixEquality = prefix.equals("xmlns"); + uriEquality = uri.equals("http://www.w3.org/2000/xmlns/"); + if ((prefixEquality || uriEquality) && !(prefixEquality && uriEquality)) + { + fatal("http://www.w3.org/2000/xmlns/ is by definition bound" + + " to prefix xmlns"); + } + + //even if the uri is http://www.w3.org/2000/xmlns/ + // it is illegal to declare it + if (prefixEquality && uriEquality) + { + fatal ("declaring the xmlns prefix is illegal"); + } + + uri = uri.intern(); + prefixStack.declarePrefix(prefix, uri); + contentHandler.startPrefixMapping(prefix, uri); + } + + void attribute(String qname, String value, boolean isSpecified) + throws SAXException + { + if (!attributes) + { + attributes = true; + if (namespaces) + { + prefixStack.pushContext(); + } + } + + // process namespace decls immediately; + // then maybe forget this as an attribute + if (namespaces) + { + int index; + + // default NS declaration? + if (stringInterning) + { + if ("xmlns" == qname) + { + declarePrefix("", value); + if (!xmlNames) + { + return; + } + } + // NS prefix declaration? + else if ((index = qname.indexOf(':')) == 5 + && qname.startsWith("xmlns")) + { + String prefix = qname.substring(6); + + if (prefix.equals("")) + { + fatal("missing prefix " + + "in namespace declaration attribute"); + } + if (value.length() == 0) + { + verror("missing URI in namespace declaration attribute: " + + qname); + } + else + { + declarePrefix(prefix, value); + } + if (!xmlNames) + { + return; + } + } + } + else + { + if ("xmlns".equals(qname)) + { + declarePrefix("", value); + if (!xmlNames) + { + return; + } + } + // NS prefix declaration? + else if ((index = qname.indexOf(':')) == 5 + && qname.startsWith("xmlns")) + { + String prefix = qname.substring(6); + + if (value.length() == 0) + { + verror("missing URI in namespace decl attribute: " + + qname); + } + else + { + declarePrefix(prefix, value); + } + if (!xmlNames) + { + return; + } + } + } + } + // remember this attribute ... + attributeCount++; + + // attribute type comes from querying parser's DTD records + attributesList.add(new Attribute(qname, value, isSpecified)); + + } + + void startElement(String elname) + throws SAXException + { + ContentHandler handler = contentHandler; + + // + // NOTE: this implementation of namespace support adds something + // like six percent to parsing CPU time, in a large (~50 MB) + // document that doesn't use namespaces at all. (Measured by PC + // sampling, with a bug where endElement processing was omitted.) + // [Measurement referred to older implementation, older JVM ...] + // + // It ought to become notably faster in such cases. Most + // costs are the prefix stack calling Hashtable.get() (2%), + // String.hashCode() (1.5%) and about 1.3% each for pushing + // the context, and two chunks of name processing. + // + + if (!attributes) + { + if (namespaces) + { + prefixStack.pushContext(); + } + } + else if (namespaces) + { + + // now we can patch up namespace refs; we saw all the + // declarations, so now we'll do the Right Thing + Iterator itt = attributesList.iterator(); + while (itt.hasNext()) + { + Attribute attribute = (Attribute) itt.next(); + String qname = attribute.name; + int index; + + // default NS declaration? + if (stringInterning) + { + if ("xmlns" == qname) + { + continue; + } + } + else + { + if ("xmlns".equals(qname)) + { + continue; + } + } + //Illegal in the new Namespaces Draft + //should it be only in 1.1 docs?? + if (qname.equals (":")) + { + fatal("namespace names consisting of a single colon " + + "character are invalid"); + } + index = qname.indexOf(':'); + + // NS prefix declaration? + if (index == 5 && qname.startsWith("xmlns")) + { + continue; + } + + // it's not a NS decl; patch namespace info items + if (prefixStack.processName(qname, nsTemp, true) == null) + { + fatal("undeclared attribute prefix in: " + qname); + } + else + { + attribute.nameSpace = nsTemp[0]; + attribute.localName = nsTemp[1]; + } + } + } + + // save element name so attribute callbacks work + elementName = elname; + if (namespaces) + { + if (prefixStack.processName(elname, nsTemp, false) == null) + { + fatal("undeclared element prefix in: " + elname); + nsTemp[0] = nsTemp[1] = ""; + } + handler.startElement(nsTemp[0], nsTemp[1], elname, this); + } + else + { + handler.startElement("", "", elname, this); + } + // elementName = null; + + // elements with no attributes are pretty common! + if (attributes) + { + attributesList.clear(); + attributeCount = 0; + attributes = false; + } + } + + void endElement(String elname) + throws SAXException + { + ContentHandler handler = contentHandler; + + if (!namespaces) + { + handler.endElement("", "", elname); + return; + } + prefixStack.processName(elname, nsTemp, false); + handler.endElement(nsTemp[0], nsTemp[1], elname); + + Enumeration prefixes = prefixStack.getDeclaredPrefixes(); + + while (prefixes.hasMoreElements()) + { + handler.endPrefixMapping((String) prefixes.nextElement()); + } + prefixStack.popContext(); + } + + void startCDATA() + throws SAXException + { + lexicalHandler.startCDATA(); + } + + void charData(char[] ch, int start, int length) + throws SAXException + { + contentHandler.characters(ch, start, length); + } + + void endCDATA() + throws SAXException + { + lexicalHandler.endCDATA(); + } + + void ignorableWhitespace(char[] ch, int start, int length) + throws SAXException + { + contentHandler.ignorableWhitespace(ch, start, length); + } + + void processingInstruction(String target, String data) + throws SAXException + { + contentHandler.processingInstruction(target, data); + } + + void comment(char[] ch, int start, int length) + throws SAXException + { + if (lexicalHandler != base) + { + lexicalHandler.comment(ch, start, length); + } + } + + void fatal(String message) + throws SAXException + { + SAXParseException fatal; + + fatal = new SAXParseException(message, this); + errorHandler.fatalError(fatal); + + // Even if the application can continue ... we can't! + throw fatal; + } + + // We can safely report a few validity errors that + // make layered SAX2 DTD validation more conformant + void verror(String message) + throws SAXException + { + SAXParseException err; + + err = new SAXParseException(message, this); + errorHandler.error(err); + } + + void warn(String message) + throws SAXException + { + SAXParseException err; + + err = new SAXParseException(message, this); + errorHandler.warning(err); + } + + // + // Implementation of org.xml.sax.Attributes. + // + + /** + * <b>SAX1 AttributeList, SAX2 Attributes</b> method + * (don't invoke on parser); + */ + public int getLength() + { + return attributesList.size(); + } + + /** + * <b>SAX2 Attributes</b> method (don't invoke on parser); + */ + public String getURI(int index) + { + if (index < 0 || index >= attributesList.size()) + { + return null; + } + return ((Attribute) attributesList.get(index)).nameSpace; + } + + /** + * <b>SAX2 Attributes</b> method (don't invoke on parser); + */ + public String getLocalName(int index) + { + if (index < 0 || index >= attributesList.size()) + { + return null; + } + Attribute attr = (Attribute) attributesList.get(index); + // FIXME attr.localName is sometimes null, why? + if (namespaces && attr.localName == null) + { + // XXX fix this here for now + int ci = attr.name.indexOf(':'); + attr.localName = (ci == -1) ? attr.name : + attr.name.substring(ci + 1); + } + return (attr.localName == null) ? "" : attr.localName; + } + + /** + * <b>SAX2 Attributes</b> method (don't invoke on parser); + */ + public String getQName(int index) + { + if (index < 0 || index >= attributesList.size()) + { + return null; + } + Attribute attr = (Attribute) attributesList.get(index); + return (attr.name == null) ? "" : attr.name; + } + + /** + * <b>SAX1 AttributeList</b> method (don't invoke on parser); + */ + public String getName(int index) + { + return getQName(index); + } + + /** + * <b>SAX1 AttributeList, SAX2 Attributes</b> method + * (don't invoke on parser); + */ + public String getType(int index) + { + if (index < 0 || index >= attributesList.size()) + { + return null; + } + String type = parser.getAttributeType(elementName, getQName(index)); + if (type == null) + { + return "CDATA"; + } + // ... use DeclHandler.attributeDecl to see enumerations + if (type == "ENUMERATION") + { + return "NMTOKEN"; + } + return type; + } + + /** + * <b>SAX1 AttributeList, SAX2 Attributes</b> method + * (don't invoke on parser); + */ + public String getValue(int index) + { + if (index < 0 || index >= attributesList.size()) + { + return null; + } + return ((Attribute) attributesList.get(index)).value; + } + + /** + * <b>SAX2 Attributes</b> method (don't invoke on parser); + */ + public int getIndex(String uri, String local) + { + int length = getLength(); + + for (int i = 0; i < length; i++) + { + if (!getURI(i).equals(uri)) + { + continue; + } + if (getLocalName(i).equals(local)) + { + return i; + } + } + return -1; + } + + /** + * <b>SAX2 Attributes</b> method (don't invoke on parser); + */ + public int getIndex(String xmlName) + { + int length = getLength(); + + for (int i = 0; i < length; i++) + { + if (getQName(i).equals(xmlName)) + { + return i; + } + } + return -1; + } + + /** + * <b>SAX2 Attributes</b> method (don't invoke on parser); + */ + public String getType(String uri, String local) + { + int index = getIndex(uri, local); + + if (index < 0) + { + return null; + } + return getType(index); + } + + /** + * <b>SAX1 AttributeList, SAX2 Attributes</b> method + * (don't invoke on parser); + */ + public String getType(String xmlName) + { + int index = getIndex(xmlName); + + if (index < 0) + { + return null; + } + return getType(index); + } + + /** + * <b>SAX Attributes</b> method (don't invoke on parser); + */ + public String getValue(String uri, String local) + { + int index = getIndex(uri, local); + + if (index < 0) + { + return null; + } + return getValue(index); + } + + /** + * <b>SAX1 AttributeList, SAX2 Attributes</b> method + * (don't invoke on parser); + */ + public String getValue(String xmlName) + { + int index = getIndex(xmlName); + + if (index < 0) + { + return null; + } + return getValue(index); + } + + // + // Implementation of org.xml.sax.ext.Attributes2 + // + + /** @return false unless the attribute was declared in the DTD. + * @throws java.lang.ArrayIndexOutOfBoundsException + * When the supplied index does not identify an attribute. + */ + public boolean isDeclared(int index) + { + if (index < 0 || index >= attributeCount) + { + throw new ArrayIndexOutOfBoundsException(); + } + String type = parser.getAttributeType(elementName, getQName(index)); + return (type != null); + } + + /** @return false unless the attribute was declared in the DTD. + * @throws java.lang.IllegalArgumentException + * When the supplied names do not identify an attribute. + */ + public boolean isDeclared(String qName) + { + int index = getIndex(qName); + if (index < 0) + { + throw new IllegalArgumentException(); + } + String type = parser.getAttributeType(elementName, qName); + return (type != null); + } + + /** @return false unless the attribute was declared in the DTD. + * @throws java.lang.IllegalArgumentException + * When the supplied names do not identify an attribute. + */ + public boolean isDeclared(String uri, String localName) + { + int index = getIndex(uri, localName); + return isDeclared(index); + } + + /** + * <b>SAX-ext Attributes2</b> method (don't invoke on parser); + */ + public boolean isSpecified(int index) + { + return ((Attribute) attributesList.get(index)).specified; + } + + /** + * <b>SAX-ext Attributes2</b> method (don't invoke on parser); + */ + public boolean isSpecified(String uri, String local) + { + int index = getIndex (uri, local); + return isSpecified(index); + } + + /** + * <b>SAX-ext Attributes2</b> method (don't invoke on parser); + */ + public boolean isSpecified(String xmlName) + { + int index = getIndex (xmlName); + return isSpecified(index); + } + + // + // Implementation of org.xml.sax.Locator. + // + + /** + * <b>SAX Locator</b> method (don't invoke on parser); + */ + public String getPublicId() + { + return null; // FIXME track public IDs too + } + + /** + * <b>SAX Locator</b> method (don't invoke on parser); + */ + public String getSystemId() + { + if (entityStack.empty()) + { + return null; + } + else + { + return (String) entityStack.peek(); + } + } + + /** + * <b>SAX Locator</b> method (don't invoke on parser); + */ + public int getLineNumber() + { + return parser.getLineNumber(); + } + + /** + * <b>SAX Locator</b> method (don't invoke on parser); + */ + public int getColumnNumber() + { + return parser.getColumnNumber(); + } + + // adapter between SAX2 content handler and SAX1 document handler callbacks + private static class Adapter + implements ContentHandler + { + + private DocumentHandler docHandler; + + Adapter(DocumentHandler dh) + { + docHandler = dh; + } + + public void setDocumentLocator(Locator l) + { + docHandler.setDocumentLocator(l); + } + + public void startDocument() + throws SAXException + { + docHandler.startDocument(); + } + + public void processingInstruction(String target, String data) + throws SAXException + { + docHandler.processingInstruction(target, data); + } + + public void startPrefixMapping(String prefix, String uri) + { + /* ignored */ + } + + public void startElement(String namespace, + String local, + String name, + Attributes attrs) + throws SAXException + { + docHandler.startElement(name, (AttributeList) attrs); + } + + public void characters(char[] buf, int offset, int len) + throws SAXException + { + docHandler.characters(buf, offset, len); + } + + public void ignorableWhitespace(char[] buf, int offset, int len) + throws SAXException + { + docHandler.ignorableWhitespace(buf, offset, len); + } + + public void skippedEntity(String name) + { + /* ignored */ + } + + public void endElement(String u, String l, String name) + throws SAXException + { + docHandler.endElement(name); + } + + public void endPrefixMapping(String prefix) + { + /* ignored */ + } + + public void endDocument() + throws SAXException + { + docHandler.endDocument(); + } + } + + private static class Attribute + { + + String name; + String value; + String nameSpace; + String localName; + boolean specified; + + Attribute(String name, String value, boolean specified) + { + this.name = name; + this.value = value; + this.nameSpace = ""; + this.specified = specified; + } + + } + +} diff --git a/libjava/classpath/gnu/xml/aelfred2/XmlParser.java b/libjava/classpath/gnu/xml/aelfred2/XmlParser.java new file mode 100644 index 00000000000..f2abb889666 --- /dev/null +++ b/libjava/classpath/gnu/xml/aelfred2/XmlParser.java @@ -0,0 +1,5835 @@ +/* XmlParser.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + +Partly derived from code which carried the following notice: + + Copyright (c) 1997, 1998 by Microstar Software Ltd. + + AElfred is free for both commercial and non-commercial use and + redistribution, provided that Microstar's copyright and disclaimer are + retained intact. You are free to modify AElfred for your own use and + to redistribute AElfred with your modifications, provided that the + modifications are clearly documented. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + merchantability or fitness for a particular purpose. Please use it AT + YOUR OWN RISK. +*/ + +package gnu.xml.aelfred2; + +import gnu.java.security.action.GetPropertyAction; + +import java.io.BufferedInputStream; +import java.io.CharConversionException; +import java.io.EOFException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLConnection; +import java.security.AccessController; + +import java.util.Iterator; +import java.util.HashMap; +import java.util.LinkedList; + +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + + +/** + * Parse XML documents and return parse events through call-backs. + * Use the <code>SAXDriver</code> class as your entry point, as all + * internal parser interfaces are subject to change. + * + * @author Written by David Megginson <dmeggins@microstar.com> + * (version 1.2a with bugfixes) + * @author Updated by David Brownell <dbrownell@users.sourceforge.net> + * @see SAXDriver + */ +final class XmlParser +{ + + // avoid slow per-character readCh() + private final static boolean USE_CHEATS = true; + + //////////////////////////////////////////////////////////////////////// + // Constants. + //////////////////////////////////////////////////////////////////////// + + // + // Constants for element content type. + // + + /** + * Constant: an element has not been declared. + * @see #getElementContentType + */ + public final static int CONTENT_UNDECLARED = 0; + + /** + * Constant: the element has a content model of ANY. + * @see #getElementContentType + */ + public final static int CONTENT_ANY = 1; + + /** + * Constant: the element has declared content of EMPTY. + * @see #getElementContentType + */ + public final static int CONTENT_EMPTY = 2; + + /** + * Constant: the element has mixed content. + * @see #getElementContentType + */ + public final static int CONTENT_MIXED = 3; + + /** + * Constant: the element has element content. + * @see #getElementContentType + */ + public final static int CONTENT_ELEMENTS = 4; + + + // + // Constants for the entity type. + // + + /** + * Constant: the entity has not been declared. + * @see #getEntityType + */ + public final static int ENTITY_UNDECLARED = 0; + + /** + * Constant: the entity is internal. + * @see #getEntityType + */ + public final static int ENTITY_INTERNAL = 1; + + /** + * Constant: the entity is external, non-parsable data. + * @see #getEntityType + */ + public final static int ENTITY_NDATA = 2; + + /** + * Constant: the entity is external XML data. + * @see #getEntityType + */ + public final static int ENTITY_TEXT = 3; + + // + // Attribute type constants are interned literal strings. + // + + // + // Constants for supported encodings. "external" is just a flag. + // + private final static int ENCODING_EXTERNAL = 0; + private final static int ENCODING_UTF_8 = 1; + private final static int ENCODING_ISO_8859_1 = 2; + private final static int ENCODING_UCS_2_12 = 3; + private final static int ENCODING_UCS_2_21 = 4; + private final static int ENCODING_UCS_4_1234 = 5; + private final static int ENCODING_UCS_4_4321 = 6; + private final static int ENCODING_UCS_4_2143 = 7; + private final static int ENCODING_UCS_4_3412 = 8; + private final static int ENCODING_ASCII = 9; + + // + // Constants for attribute default value. + // + + /** + * Constant: the attribute is not declared. + * @see #getAttributeDefaultValueType + */ + public final static int ATTRIBUTE_DEFAULT_UNDECLARED = 30; + + /** + * Constant: the attribute has a literal default value specified. + * @see #getAttributeDefaultValueType + * @see #getAttributeDefaultValue + */ + public final static int ATTRIBUTE_DEFAULT_SPECIFIED = 31; + + /** + * Constant: the attribute was declared #IMPLIED. + * @see #getAttributeDefaultValueType + */ + public final static int ATTRIBUTE_DEFAULT_IMPLIED = 32; + + /** + * Constant: the attribute was declared #REQUIRED. + * @see #getAttributeDefaultValueType + */ + public final static int ATTRIBUTE_DEFAULT_REQUIRED = 33; + + /** + * Constant: the attribute was declared #FIXED. + * @see #getAttributeDefaultValueType + * @see #getAttributeDefaultValue + */ + public final static int ATTRIBUTE_DEFAULT_FIXED = 34; + + // + // Constants for input. + // + private final static int INPUT_NONE = 0; + private final static int INPUT_INTERNAL = 1; + private final static int INPUT_STREAM = 3; + private final static int INPUT_READER = 5; + + // + // Flags for reading literals. + // + // expand general entity refs (attribute values in dtd and content) + private final static int LIT_ENTITY_REF = 2; + // normalize this value (space chars) (attributes, public ids) + private final static int LIT_NORMALIZE = 4; + // literal is an attribute value + private final static int LIT_ATTRIBUTE = 8; + // don't expand parameter entities + private final static int LIT_DISABLE_PE = 16; + // don't expand [or parse] character refs + private final static int LIT_DISABLE_CREF = 32; + // don't parse general entity refs + private final static int LIT_DISABLE_EREF = 64; + // literal is a public ID value + private final static int LIT_PUBID = 256; + + // + // Flags affecting PE handling in DTDs (if expandPE is true). + // PEs expand with space padding, except inside literals. + // + private final static int CONTEXT_NORMAL = 0; + private final static int CONTEXT_LITERAL = 1; + + // Emit warnings for relative URIs with no base URI. + static boolean uriWarnings; + static + { + String key = "gnu.xml.aelfred2.XmlParser.uriWarnings"; + GetPropertyAction a = new GetPropertyAction(key); + uriWarnings = "true".equals(AccessController.doPrivileged(a)); + } + + // + // The current XML handler interface. + // + private SAXDriver handler; + + // + // I/O information. + // + private Reader reader; // current reader + private InputStream is; // current input stream + private int line; // current line number + private int column; // current column number + private int sourceType; // type of input source + private LinkedList inputStack; // stack of input soruces + private URLConnection externalEntity; // current external entity + private int encoding; // current character encoding + private int currentByteCount; // bytes read from current source + private InputSource scratch; // temporary + + // + // Buffers for decoded but unparsed character input. + // + private char[] readBuffer; + private int readBufferPos; + private int readBufferLength; + private int readBufferOverflow; // overflow from last data chunk. + + // + // Buffer for undecoded raw byte input. + // + private final static int READ_BUFFER_MAX = 16384; + private byte[] rawReadBuffer; + + + // + // Buffer for attribute values, char refs, DTD stuff. + // + private static int DATA_BUFFER_INITIAL = 4096; + private char[] dataBuffer; + private int dataBufferPos; + + // + // Buffer for parsed names. + // + private static int NAME_BUFFER_INITIAL = 1024; + private char[] nameBuffer; + private int nameBufferPos; + + // + // Save any standalone flag + // + private boolean docIsStandalone; + + // + // Hashtables for DTD information on elements, entities, and notations. + // Populated until we start ignoring decls (because of skipping a PE) + // + private HashMap elementInfo; + private HashMap entityInfo; + private HashMap notationInfo; + private boolean skippedPE; + + // + // Element type currently in force. + // + private String currentElement; + private int currentElementContent; + + // + // Stack of entity names, to detect recursion. + // + private LinkedList entityStack; + + // + // PE expansion is enabled in most chunks of the DTD, not all. + // When it's enabled, literals are treated differently. + // + private boolean inLiteral; + private boolean expandPE; + private boolean peIsError; + + // + // can't report entity expansion inside two constructs: + // - attribute expansions (internal entities only) + // - markup declarations (parameter entities only) + // + private boolean doReport; + + // + // Symbol table, for caching interned names. + // + // These show up wherever XML names or nmtokens are used: naming elements, + // attributes, PIs, notations, entities, and enumerated attribute values. + // + // NOTE: This hashtable doesn't grow. The default size is intended to be + // rather large for most documents. Example: one snapshot of the DocBook + // XML 4.1 DTD used only about 350 such names. As a rule, only pathological + // documents (ones that don't reuse names) should ever see much collision. + // + // Be sure that SYMBOL_TABLE_LENGTH always stays prime, for best hashing. + // "2039" keeps the hash table size at about two memory pages on typical + // 32 bit hardware. + // + private final static int SYMBOL_TABLE_LENGTH = 2039; + + private Object[][] symbolTable; + + // + // Hash table of attributes found in current start tag. + // + private String[] tagAttributes; + private int tagAttributePos; + + // + // Utility flag: have we noticed a CR while reading the last + // data chunk? If so, we will have to go back and normalise + // CR or CR/LF line ends. + // + private boolean sawCR; + + // + // Utility flag: are we in CDATA? If so, whitespace isn't ignorable. + // + private boolean inCDATA; + + // + // Xml version. + // + private static final int XML_10 = 0; + private static final int XML_11 = 1; + private int xmlVersion = XML_10; + + ////////////////////////////////////////////////////////////////////// + // Constructors. + //////////////////////////////////////////////////////////////////////// + + /** + * Construct a new parser with no associated handler. + * @see #setHandler + * @see #parse + */ + // package private + XmlParser() + { + } + + /** + * Set the handler that will receive parsing events. + * @param handler The handler to receive callback events. + * @see #parse + */ + // package private + void setHandler(SAXDriver handler) + { + this.handler = handler; + } + + /** + * Parse an XML document from the character stream, byte stream, or URI + * that you provide (in that order of preference). Any URI that you + * supply will become the base URI for resolving relative URI, and may + * be used to acquire a reader or byte stream. + * + * <p> Only one thread at a time may use this parser; since it is + * private to this package, post-parse cleanup is done by the caller, + * which MUST NOT REUSE the parser (just null it). + * + * @param systemId Absolute URI of the document; should never be null, + * but may be so iff a reader <em>or</em> a stream is provided. + * @param publicId The public identifier of the document, or null. + * @param reader A character stream; must be null if stream isn't. + * @param stream A byte input stream; must be null if reader isn't. + * @param encoding The suggested encoding, or null if unknown. + * @exception java.lang.Exception Basically SAXException or IOException + */ + // package private + void doParse(String systemId, String publicId, Reader reader, + InputStream stream, String encoding) + throws Exception + { + if (handler == null) + { + throw new IllegalStateException("no callback handler"); + } + + initializeVariables(); + + // predeclare the built-in entities here (replacement texts) + // we don't need to intern(), since we're guaranteed literals + // are always (globally) interned. + setInternalEntity("amp", "&"); + setInternalEntity("lt", "<"); + setInternalEntity("gt", ">"); + setInternalEntity("apos", "'"); + setInternalEntity("quot", """); + + try + { + // pushURL first to ensure locator is correct in startDocument + // ... it might report an IO or encoding exception. + handler.startDocument(); + pushURL(false, "[document]", + // default baseURI: null + new ExternalIdentifiers(publicId, systemId, null), + reader, stream, encoding, false); + + parseDocument(); + } + catch (EOFException e) + { + //empty input + error("empty document, with no root element."); + } + finally + { + if (reader != null) + { + try + { + reader.close(); + } + catch (IOException e) + { + /* ignore */ + } + } + if (stream != null) + { + try + { + stream.close(); + } + catch (IOException e) + { + /* ignore */ + } + } + if (is != null) + { + try + { + is.close(); + } + catch (IOException e) + { + /* ignore */ + } + } + scratch = null; + } + } + + ////////////////////////////////////////////////////////////////////// + // Error reporting. + ////////////////////////////////////////////////////////////////////// + + /** + * Report an error. + * @param message The error message. + * @param textFound The text that caused the error (or null). + * @see SAXDriver#error + * @see #line + */ + private void error(String message, String textFound, String textExpected) + throws SAXException + { + if (textFound != null) + { + message = message + " (found \"" + textFound + "\")"; + } + if (textExpected != null) + { + message = message + " (expected \"" + textExpected + "\")"; + } + handler.fatal(message); + + // "can't happen" + throw new SAXException(message); + } + + /** + * Report a serious error. + * @param message The error message. + * @param textFound The text that caused the error (or null). + */ + private void error(String message, char textFound, String textExpected) + throws SAXException + { + error(message, new Character(textFound).toString(), textExpected); + } + + /** + * Report typical case fatal errors. + */ + private void error(String message) + throws SAXException + { + handler.fatal(message); + } + + ////////////////////////////////////////////////////////////////////// + // Major syntactic productions. + ////////////////////////////////////////////////////////////////////// + + /** + * Parse an XML document. + * <pre> + * [1] document ::= prolog element Misc* + * </pre> + * <p>This is the top-level parsing function for a single XML + * document. As a minimum, a well-formed document must have + * a document element, and a valid document must have a prolog + * (one with doctype) as well. + */ + private void parseDocument() + throws Exception + { + try + { // added by MHK + boolean sawDTD = parseProlog(); + require('<'); + parseElement(!sawDTD); + } + catch (EOFException ee) + { // added by MHK + error("premature end of file", "[EOF]", null); + } + + try + { + parseMisc(); //skip all white, PIs, and comments + char c = readCh(); //if this doesn't throw an exception... + error("unexpected characters after document end", c, null); + } + catch (EOFException e) + { + return; + } + } + + static final char[] startDelimComment = { '<', '!', '-', '-' }; + static final char[] endDelimComment = { '-', '-' }; + + /** + * Skip a comment. + * <pre> + * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* "-->" + * </pre> + * <p> (The <code><!--</code> has already been read.) + */ + private void parseComment() + throws Exception + { + char c; + boolean saved = expandPE; + + expandPE = false; + parseUntil(endDelimComment); + require('>'); + expandPE = saved; + handler.comment(dataBuffer, 0, dataBufferPos); + dataBufferPos = 0; + } + + static final char[] startDelimPI = { '<', '?' }; + static final char[] endDelimPI = { '?', '>' }; + + /** + * Parse a processing instruction and do a call-back. + * <pre> + * [16] PI ::= '<?' PITarget + * (S (Char* - (Char* '?>' Char*)))? + * '?>' + * [17] PITarget ::= Name - ( ('X'|'x') ('M'|m') ('L'|l') ) + * </pre> + * <p> (The <code><?</code> has already been read.) + */ + private void parsePI() + throws SAXException, IOException + { + String name; + boolean saved = expandPE; + + expandPE = false; + name = readNmtoken(true); + //NE08 + if (name.indexOf(':') >= 0) + { + error("Illegal character(':') in processing instruction name ", + name, null); + } + if ("xml".equalsIgnoreCase(name)) + { + error("Illegal processing instruction target", name, null); + } + if (!tryRead(endDelimPI)) + { + requireWhitespace(); + parseUntil(endDelimPI); + } + expandPE = saved; + handler.processingInstruction(name, dataBufferToString()); + } + + static final char[] endDelimCDATA = { ']', ']', '>' }; + + private boolean isDirtyCurrentElement; + + /** + * Parse a CDATA section. + * <pre> + * [18] CDSect ::= CDStart CData CDEnd + * [19] CDStart ::= '<![CDATA[' + * [20] CData ::= (Char* - (Char* ']]>' Char*)) + * [21] CDEnd ::= ']]>' + * </pre> + * <p> (The '<![CDATA[' has already been read.) + */ + private void parseCDSect() + throws Exception + { + parseUntil(endDelimCDATA); + dataBufferFlush(); + } + + /** + * Parse the prolog of an XML document. + * <pre> + * [22] prolog ::= XMLDecl? Misc* (Doctypedecl Misc*)? + * </pre> + * <p>We do not look for the XML declaration here, because it was + * handled by pushURL (). + * @see pushURL + * @return true if a DTD was read. + */ + private boolean parseProlog() + throws Exception + { + parseMisc(); + + if (tryRead("<!DOCTYPE")) + { + parseDoctypedecl(); + parseMisc(); + return true; + } + return false; + } + + private void checkLegalVersion(String version) + throws SAXException + { + int len = version.length(); + for (int i = 0; i < len; i++) + { + char c = version.charAt(i); + if ('0' <= c && c <= '9') + { + continue; + } + if (c == '_' || c == '.' || c == ':' || c == '-') + { + continue; + } + if ('a' <= c && c <= 'z') + { + continue; + } + if ('A' <= c && c <= 'Z') + { + continue; + } + error ("illegal character in version", version, "1.0"); + } + } + + /** + * Parse the XML declaration. + * <pre> + * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' + * [24] VersionInfo ::= S 'version' Eq + * ("'" VersionNum "'" | '"' VersionNum '"' ) + * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')* + * [32] SDDecl ::= S 'standalone' Eq + * ( "'"" ('yes' | 'no') "'"" | '"' ("yes" | "no") '"' ) + * [80] EncodingDecl ::= S 'encoding' Eq + * ( "'" EncName "'" | "'" EncName "'" ) + * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* + * </pre> + * <p> (The <code><?xml</code> and whitespace have already been read.) + * @return the encoding in the declaration, uppercased; or null + * @see #parseTextDecl + * @see #setupDecoding + */ + private String parseXMLDecl(boolean ignoreEncoding) + throws SAXException, IOException + { + String version; + String encodingName = null; + String standalone = null; + int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF; + String inputEncoding = null; + + switch (this.encoding) + { + case ENCODING_EXTERNAL: + case ENCODING_UTF_8: + inputEncoding = "UTF-8"; + break; + case ENCODING_ISO_8859_1: + inputEncoding = "ISO-8859-1"; + break; + case ENCODING_UCS_2_12: + inputEncoding = "UTF-16BE"; + break; + case ENCODING_UCS_2_21: + inputEncoding = "UTF-16LE"; + break; + } + + // Read the version. + require("version"); + parseEq(); + checkLegalVersion(version = readLiteral(flags)); + if (!version.equals("1.0")) + { + if (version.equals("1.1")) + { + handler.warn("expected XML version 1.0, not: " + version); + xmlVersion = XML_11; + } + else + { + error("illegal XML version", version, "1.0 or 1.1"); + } + } + else + { + xmlVersion = XML_10; + } + // Try reading an encoding declaration. + boolean white = tryWhitespace(); + + if (tryRead("encoding")) + { + if (!white) + { + error("whitespace required before 'encoding='"); + } + parseEq(); + encodingName = readLiteral(flags); + if (!ignoreEncoding) + { + setupDecoding(encodingName); + } + } + + // Try reading a standalone declaration + if (encodingName != null) + { + white = tryWhitespace(); + } + if (tryRead("standalone")) + { + if (!white) + { + error("whitespace required before 'standalone='"); + } + parseEq(); + standalone = readLiteral(flags); + if ("yes".equals(standalone)) + { + docIsStandalone = true; + } + else if (!"no".equals(standalone)) + { + error("standalone flag must be 'yes' or 'no'"); + } + } + + skipWhitespace(); + require("?>"); + + if (inputEncoding == null) + { + inputEncoding = encodingName; + } + handler.xmlDecl(version, encodingName, docIsStandalone, + inputEncoding); + + return encodingName; + } + + /** + * Parse a text declaration. + * <pre> + * [79] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' + * [80] EncodingDecl ::= S 'encoding' Eq + * ( '"' EncName '"' | "'" EncName "'" ) + * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* + * </pre> + * <p> (The <code><?xml</code>' and whitespace have already been read.) + * @return the encoding in the declaration, uppercased; or null + * @see #parseXMLDecl + * @see #setupDecoding + */ + private String parseTextDecl(boolean ignoreEncoding) + throws SAXException, IOException + { + String encodingName = null; + int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF; + + // Read an optional version. + if (tryRead ("version")) + { + String version; + parseEq(); + checkLegalVersion(version = readLiteral(flags)); + + if (version.equals("1.1")) + { + if (xmlVersion == XML_10) + { + error("external subset has later version number.", "1.0", + version); + } + handler.warn("expected XML version 1.0, not: " + version); + xmlVersion = XML_11; + } + else if (!version.equals("1.0")) + { + error("illegal XML version", version, "1.0 or 1.1"); + } + requireWhitespace(); + } + + // Read the encoding. + require("encoding"); + parseEq(); + encodingName = readLiteral(flags); + if (!ignoreEncoding) + { + setupDecoding(encodingName); + } + skipWhitespace(); + require("?>"); + + return encodingName; + } + + /** + * Sets up internal state so that we can decode an entity using the + * specified encoding. This is used when we start to read an entity + * and we have been given knowledge of its encoding before we start to + * read any data (e.g. from a SAX input source or from a MIME type). + * + * <p> It is also used after autodetection, at which point only very + * limited adjustments to the encoding may be used (switching between + * related builtin decoders). + * + * @param encodingName The name of the encoding specified by the user. + * @exception IOException if the encoding isn't supported either + * internally to this parser, or by the hosting JVM. + * @see #parseXMLDecl + * @see #parseTextDecl + */ + private void setupDecoding(String encodingName) + throws SAXException, IOException + { + encodingName = encodingName.toUpperCase(); + + // ENCODING_EXTERNAL indicates an encoding that wasn't + // autodetected ... we can use builtin decoders, or + // ones from the JVM (InputStreamReader). + + // Otherwise we can only tweak what was autodetected, and + // only for single byte (ASCII derived) builtin encodings. + + // ASCII-derived encodings + if (encoding == ENCODING_UTF_8 || encoding == ENCODING_EXTERNAL) + { + if (encodingName.equals("ISO-8859-1") + || encodingName.equals("8859_1") + || encodingName.equals("ISO8859_1")) + { + encoding = ENCODING_ISO_8859_1; + return; + } + else if (encodingName.equals("US-ASCII") + || encodingName.equals("ASCII")) + { + encoding = ENCODING_ASCII; + return; + } + else if (encodingName.equals("UTF-8") + || encodingName.equals("UTF8")) + { + encoding = ENCODING_UTF_8; + return; + } + else if (encoding != ENCODING_EXTERNAL) + { + // used to start with a new reader ... + throw new UnsupportedEncodingException(encodingName); + } + // else fallthrough ... + // it's ASCII-ish and something other than a builtin + } + + // Unicode and such + if (encoding == ENCODING_UCS_2_12 || encoding == ENCODING_UCS_2_21) + { + if (!(encodingName.equals("ISO-10646-UCS-2") + || encodingName.equals("UTF-16") + || encodingName.equals("UTF-16BE") + || encodingName.equals("UTF-16LE"))) + { + error("unsupported Unicode encoding", encodingName, "UTF-16"); + } + return; + } + + // four byte encodings + if (encoding == ENCODING_UCS_4_1234 + || encoding == ENCODING_UCS_4_4321 + || encoding == ENCODING_UCS_4_2143 + || encoding == ENCODING_UCS_4_3412) + { + // Strictly: "UCS-4" == "UTF-32BE"; also, "UTF-32LE" exists + if (!encodingName.equals("ISO-10646-UCS-4")) + { + error("unsupported 32-bit encoding", encodingName, + "ISO-10646-UCS-4"); + } + return; + } + + // assert encoding == ENCODING_EXTERNAL + // if (encoding != ENCODING_EXTERNAL) + // throw new RuntimeException ("encoding = " + encoding); + + if (encodingName.equals("UTF-16BE")) + { + encoding = ENCODING_UCS_2_12; + return; + } + if (encodingName.equals("UTF-16LE")) + { + encoding = ENCODING_UCS_2_21; + return; + } + + // We couldn't use the builtin decoders at all. But we can try to + // create a reader, since we haven't messed up buffering. Tweak + // the encoding name if necessary. + + if (encodingName.equals("UTF-16") + || encodingName.equals("ISO-10646-UCS-2")) + { + encodingName = "Unicode"; + } + // Ignoring all the EBCDIC aliases here + + reader = new InputStreamReader(is, encodingName); + sourceType = INPUT_READER; + } + + /** + * Parse miscellaneous markup outside the document element and DOCTYPE + * declaration. + * <pre> + * [27] Misc ::= Comment | PI | S + * </pre> + */ + private void parseMisc() + throws Exception + { + while (true) + { + skipWhitespace(); + if (tryRead(startDelimPI)) + { + parsePI(); + } + else if (tryRead(startDelimComment)) + { + parseComment(); + } + else + { + return; + } + } + } + + /** + * Parse a document type declaration. + * <pre> + * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? + * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' + * </pre> + * <p> (The <code><!DOCTYPE</code> has already been read.) + */ + private void parseDoctypedecl() + throws Exception + { + String rootName; + ExternalIdentifiers ids; + + // Read the document type name. + requireWhitespace(); + rootName = readNmtoken(true); + + // Read the External subset's IDs + skipWhitespace(); + ids = readExternalIds(false, true); + + // report (a) declaration of name, (b) lexical info (ids) + handler.doctypeDecl(rootName, ids.publicId, ids.systemId); + + // Internal subset is parsed first, if present + skipWhitespace(); + if (tryRead('[')) + { + + // loop until the subset ends + while (true) + { + doReport = expandPE = true; + skipWhitespace(); + doReport = expandPE = false; + if (tryRead(']')) + { + break; // end of subset + } + else + { + // WFC, PEs in internal subset (only between decls) + peIsError = expandPE = true; + parseMarkupdecl(); + peIsError = expandPE = false; + } + } + } + skipWhitespace(); + require('>'); + + // Read the external subset, if any + InputSource subset; + + if (ids.systemId == null) + { + subset = handler.getExternalSubset(rootName, + handler.getSystemId()); + } + else + { + subset = null; + } + if (ids.systemId != null || subset != null) + { + pushString(null, ">"); + + // NOTE: [dtd] is so we say what SAX2 expects, + // though it's misleading (subset, not entire dtd) + if (ids.systemId != null) + { + pushURL(true, "[dtd]", ids, null, null, null, true); + } + else + { + handler.warn("modifying document by adding external subset"); + pushURL(true, "[dtd]", + new ExternalIdentifiers(subset.getPublicId(), + subset.getSystemId(), + null), + subset.getCharacterStream(), + subset.getByteStream(), + subset.getEncoding(), + false); + } + + // Loop until we end up back at '>' + while (true) + { + doReport = expandPE = true; + skipWhitespace(); + doReport = expandPE = false; + if (tryRead('>')) + { + break; + } + else + { + expandPE = true; + parseMarkupdecl(); + expandPE = false; + } + } + + // the ">" string isn't popped yet + if (inputStack.size() != 1) + { + error("external subset has unmatched '>'"); + } + } + + // done dtd + handler.endDoctype(); + expandPE = false; + doReport = true; + } + + /** + * Parse a markup declaration in the internal or external DTD subset. + * <pre> + * [29] markupdecl ::= elementdecl | Attlistdecl | EntityDecl + * | NotationDecl | PI | Comment + * [30] extSubsetDecl ::= (markupdecl | conditionalSect + * | PEReference | S) * + * </pre> + * <p> Reading toplevel PE references is handled as a lexical issue + * by the caller, as is whitespace. + */ + private void parseMarkupdecl() + throws Exception + { + char[] saved = null; + boolean savedPE = expandPE; + + // prevent "<%foo;" and ensures saved entity is right + require('<'); + unread('<'); + expandPE = false; + + if (tryRead("<!ELEMENT")) + { + saved = readBuffer; + expandPE = savedPE; + parseElementDecl(); + } + else if (tryRead("<!ATTLIST")) + { + saved = readBuffer; + expandPE = savedPE; + parseAttlistDecl(); + } + else if (tryRead("<!ENTITY")) + { + saved = readBuffer; + expandPE = savedPE; + parseEntityDecl(); + } + else if (tryRead("<!NOTATION")) + { + saved = readBuffer; + expandPE = savedPE; + parseNotationDecl(); + } + else if (tryRead(startDelimPI)) + { + saved = readBuffer; + expandPE = savedPE; + parsePI(); + } + else if (tryRead(startDelimComment)) + { + saved = readBuffer; + expandPE = savedPE; + parseComment(); + } + else if (tryRead("<![")) + { + saved = readBuffer; + expandPE = savedPE; + if (inputStack.size() > 0) + { + parseConditionalSect(saved); + } + else + { + error("conditional sections illegal in internal subset"); + } + } + else + { + error("expected markup declaration"); + } + + // VC: Proper Decl/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Declaration/PE nesting"); + } + } + + /** + * Parse an element, with its tags. + * <pre> + * [39] element ::= EmptyElementTag | STag content ETag + * [40] STag ::= '<' Name (S Attribute)* S? '>' + * [44] EmptyElementTag ::= '<' Name (S Attribute)* S? '/>' + * </pre> + * <p> (The '<' has already been read.) + * <p>NOTE: this method actually chains onto parseContent (), if necessary, + * and parseContent () will take care of calling parseETag (). + */ + private void parseElement(boolean maybeGetSubset) + throws Exception + { + String gi; + char c; + int oldElementContent = currentElementContent; + String oldElement = currentElement; + ElementDecl element; + + // This is the (global) counter for the + // array of specified attributes. + tagAttributePos = 0; + + // Read the element type name. + gi = readNmtoken(true); + + // If we saw no DTD, and this is the document root element, + // let the application modify the input stream by providing one. + if (maybeGetSubset) + { + InputSource subset = handler.getExternalSubset(gi, + handler.getSystemId()); + if (subset != null) + { + String publicId = subset.getPublicId(); + String systemId = subset.getSystemId(); + + handler.warn("modifying document by adding DTD"); + handler.doctypeDecl(gi, publicId, systemId); + pushString(null, ">"); + + // NOTE: [dtd] is so we say what SAX2 expects, + // though it's misleading (subset, not entire dtd) + pushURL(true, "[dtd]", + new ExternalIdentifiers(publicId, systemId, null), + subset.getCharacterStream(), + subset.getByteStream(), + subset.getEncoding(), + false); + + // Loop until we end up back at '>' + while (true) + { + doReport = expandPE = true; + skipWhitespace(); + doReport = expandPE = false; + if (tryRead('>')) + { + break; + } + else + { + expandPE = true; + parseMarkupdecl(); + expandPE = false; + } + } + + // the ">" string isn't popped yet + if (inputStack.size() != 1) + { + error("external subset has unmatched '>'"); + } + + handler.endDoctype(); + } + } + + // Determine the current content type. + currentElement = gi; + element = (ElementDecl) elementInfo.get(gi); + currentElementContent = getContentType(element, CONTENT_ANY); + + // Read the attributes, if any. + // After this loop, "c" is the closing delimiter. + boolean white = tryWhitespace(); + c = readCh(); + while (c != '/' && c != '>') + { + unread(c); + if (!white) + { + error("need whitespace between attributes"); + } + parseAttribute(gi); + white = tryWhitespace(); + c = readCh(); + } + + // Supply any defaulted attributes. + Iterator atts = declaredAttributes(element); + if (atts != null) + { + String aname; +loop: + while (atts.hasNext()) + { + aname = (String) atts.next(); + // See if it was specified. + for (int i = 0; i < tagAttributePos; i++) + { + if (tagAttributes[i] == aname) + { + continue loop; + } + } + // ... or has a default + String value = getAttributeDefaultValue(gi, aname); + + if (value == null) + { + continue; + } + handler.attribute(aname, value, false); + } + } + + // Figure out if this is a start tag + // or an empty element, and dispatch an + // event accordingly. + switch (c) + { + case '>': + handler.startElement(gi); + parseContent(); + break; + case '/': + require('>'); + handler.startElement(gi); + handler.endElement(gi); + break; + } + + // Restore the previous state. + currentElement = oldElement; + currentElementContent = oldElementContent; + } + + /** + * Parse an attribute assignment. + * <pre> + * [41] Attribute ::= Name Eq AttValue + * </pre> + * @param name The name of the attribute's element. + * @see SAXDriver#attribute + */ + private void parseAttribute(String name) + throws Exception + { + String aname; + String type; + String value; + int flags = LIT_ATTRIBUTE | LIT_ENTITY_REF; + + // Read the attribute name. + aname = readNmtoken(true); + type = getAttributeType(name, aname); + + // Parse '=' + parseEq(); + + // Read the value, normalizing whitespace + // unless it is CDATA. + if (handler.stringInterning) + { + if (type == "CDATA" || type == null) + { + value = readLiteral(flags); + } + else + { + value = readLiteral(flags | LIT_NORMALIZE); + } + } + else + { + if (type == null || type.equals("CDATA")) + { + value = readLiteral(flags); + } + else + { + value = readLiteral(flags | LIT_NORMALIZE); + } + } + + // WFC: no duplicate attributes + for (int i = 0; i < tagAttributePos; i++) + { + if (aname.equals(tagAttributes [i])) + { + error("duplicate attribute", aname, null); + } + } + + // Inform the handler about the + // attribute. + handler.attribute(aname, value, true); + dataBufferPos = 0; + + // Note that the attribute has been + // specified. + if (tagAttributePos == tagAttributes.length) + { + String newAttrib[] = new String[tagAttributes.length * 2]; + System.arraycopy(tagAttributes, 0, newAttrib, 0, tagAttributePos); + tagAttributes = newAttrib; + } + tagAttributes[tagAttributePos++] = aname; + } + + /** + * Parse an equals sign surrounded by optional whitespace. + * <pre> + * [25] Eq ::= S? '=' S? + * </pre> + */ + private void parseEq() + throws SAXException, IOException + { + skipWhitespace(); + require('='); + skipWhitespace(); + } + + /** + * Parse an end tag. + * <pre> + * [42] ETag ::= '</' Name S? '>' + * </pre> + * <p>NOTE: parseContent () chains to here, we already read the + * "</". + */ + private void parseETag() + throws Exception + { + require(currentElement); + skipWhitespace(); + require('>'); + handler.endElement(currentElement); + // not re-reporting any SAXException re bogus end tags, + // even though that diagnostic might be clearer ... + } + + /** + * Parse the content of an element. + * <pre> + * [43] content ::= (element | CharData | Reference + * | CDSect | PI | Comment)* + * [67] Reference ::= EntityRef | CharRef + * </pre> + * <p> NOTE: consumes ETtag. + */ + private void parseContent() + throws Exception + { + char c; + + while (true) + { + // consume characters (or ignorable whitspace) until delimiter + parseCharData(); + + // Handle delimiters + c = readCh(); + switch (c) + { + case '&': // Found "&" + c = readCh(); + if (c == '#') + { + parseCharRef(); + } + else + { + unread(c); + parseEntityRef(true); + } + isDirtyCurrentElement = true; + break; + + case '<': // Found "<" + dataBufferFlush(); + c = readCh(); + switch (c) + { + case '!': // Found "<!" + c = readCh(); + switch (c) + { + case '-': // Found "<!-" + require('-'); + isDirtyCurrentElement = false; + parseComment(); + break; + case '[': // Found "<![" + isDirtyCurrentElement = false; + require("CDATA["); + handler.startCDATA(); + inCDATA = true; + parseCDSect(); + inCDATA = false; + handler.endCDATA(); + break; + default: + error("expected comment or CDATA section", c, null); + break; + } + break; + + case '?': // Found "<?" + isDirtyCurrentElement = false; + parsePI(); + break; + + case '/': // Found "</" + isDirtyCurrentElement = false; + parseETag(); + return; + + default: // Found "<" followed by something else + isDirtyCurrentElement = false; + unread(c); + parseElement(false); + break; + } + } + } + } + + /** + * Parse an element type declaration. + * <pre> + * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' + * </pre> + * <p> NOTE: the '<!ELEMENT' has already been read. + */ + private void parseElementDecl() + throws Exception + { + String name; + + requireWhitespace(); + // Read the element type name. + name = readNmtoken(true); + + requireWhitespace(); + // Read the content model. + parseContentspec(name); + + skipWhitespace(); + require('>'); + } + + /** + * Content specification. + * <pre> + * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | elements + * </pre> + */ + private void parseContentspec(String name) + throws Exception + { + // FIXME: move elementDecl() into setElement(), pass EMTPY/ANY ... + if (tryRead("EMPTY")) + { + setElement(name, CONTENT_EMPTY, null, null); + if (!skippedPE) + { + handler.getDeclHandler().elementDecl(name, "EMPTY"); + } + return; + } + else if (tryRead("ANY")) + { + setElement(name, CONTENT_ANY, null, null); + if (!skippedPE) + { + handler.getDeclHandler().elementDecl(name, "ANY"); + } + return; + } + else + { + String model; + char[] saved; + + require('('); + saved = readBuffer; + dataBufferAppend('('); + skipWhitespace(); + if (tryRead("#PCDATA")) + { + dataBufferAppend("#PCDATA"); + parseMixed(saved); + model = dataBufferToString(); + setElement(name, CONTENT_MIXED, model, null); + } + else + { + parseElements(saved); + model = dataBufferToString(); + setElement(name, CONTENT_ELEMENTS, model, null); + } + if (!skippedPE) + { + handler.getDeclHandler().elementDecl(name, model); + } + } + } + + /** + * Parse an element-content model. + * <pre> + * [47] elements ::= (choice | seq) ('?' | '*' | '+')? + * [49] choice ::= '(' S? cp (S? '|' S? cp)+ S? ')' + * [50] seq ::= '(' S? cp (S? ',' S? cp)* S? ')' + * </pre> + * + * <p> NOTE: the opening '(' and S have already been read. + * + * @param saved Buffer for entity that should have the terminal ')' + */ + private void parseElements(char[] saved) + throws Exception + { + char c; + char sep; + + // Parse the first content particle + skipWhitespace(); + parseCp(); + + // Check for end or for a separator. + skipWhitespace(); + c = readCh(); + switch (c) + { + case ')': + // VC: Proper Group/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Group/PE nesting"); + } + + dataBufferAppend(')'); + c = readCh(); + switch (c) + { + case '*': + case '+': + case '?': + dataBufferAppend(c); + break; + default: + unread(c); + } + return; + case ',': // Register the separator. + case '|': + sep = c; + dataBufferAppend(c); + break; + default: + error("bad separator in content model", c, null); + return; + } + + // Parse the rest of the content model. + while (true) + { + skipWhitespace(); + parseCp(); + skipWhitespace(); + c = readCh(); + if (c == ')') + { + // VC: Proper Group/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Group/PE nesting"); + } + + dataBufferAppend(')'); + break; + } + else if (c != sep) + { + error("bad separator in content model", c, null); + return; + } + else + { + dataBufferAppend(c); + } + } + + // Check for the occurrence indicator. + c = readCh(); + switch (c) + { + case '?': + case '*': + case '+': + dataBufferAppend(c); + return; + default: + unread(c); + return; + } + } + + /** + * Parse a content particle. + * <pre> + * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? + * </pre> + */ + private void parseCp() + throws Exception + { + if (tryRead('(')) + { + dataBufferAppend('('); + parseElements(readBuffer); + } + else + { + dataBufferAppend(readNmtoken(true)); + char c = readCh(); + switch (c) + { + case '?': + case '*': + case '+': + dataBufferAppend(c); + break; + default: + unread(c); + break; + } + } + } + + /** + * Parse mixed content. + * <pre> + * [51] Mixed ::= '(' S? ( '#PCDATA' (S? '|' S? Name)*) S? ')*' + * | '(' S? ('#PCDATA') S? ')' + * </pre> + * + * @param saved Buffer for entity that should have the terminal ')' + */ + private void parseMixed(char[] saved) + throws Exception + { + // Check for PCDATA alone. + skipWhitespace(); + if (tryRead(')')) + { + // VC: Proper Group/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Group/PE nesting"); + } + + dataBufferAppend(")*"); + tryRead('*'); + return; + } + + // Parse mixed content. + skipWhitespace(); + while (!tryRead(")")) + { + require('|'); + dataBufferAppend('|'); + skipWhitespace(); + dataBufferAppend(readNmtoken(true)); + skipWhitespace(); + } + + // VC: Proper Group/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Group/PE nesting"); + } + + require('*'); + dataBufferAppend(")*"); + } + + /** + * Parse an attribute list declaration. + * <pre> + * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' + * </pre> + * <p>NOTE: the '<!ATTLIST' has already been read. + */ + private void parseAttlistDecl() + throws Exception + { + String elementName; + + requireWhitespace(); + elementName = readNmtoken(true); + boolean white = tryWhitespace(); + while (!tryRead('>')) + { + if (!white) + { + error("whitespace required before attribute definition"); + } + parseAttDef(elementName); + white = tryWhitespace(); + } + } + + /** + * Parse a single attribute definition. + * <pre> + * [53] AttDef ::= S Name S AttType S DefaultDecl + * </pre> + */ + private void parseAttDef(String elementName) + throws Exception + { + String name; + String type; + String enumer = null; + + // Read the attribute name. + name = readNmtoken(true); + + // Read the attribute type. + requireWhitespace(); + type = readAttType(); + + // Get the string of enumerated values if necessary. + if (handler.stringInterning) + { + if ("ENUMERATION" == type || "NOTATION" == type) + { + enumer = dataBufferToString(); + } + } + else + { + if ("ENUMERATION".equals(type) || "NOTATION".equals(type)) + { + enumer = dataBufferToString(); + } + } + + // Read the default value. + requireWhitespace(); + parseDefault(elementName, name, type, enumer); + } + + /** + * Parse the attribute type. + * <pre> + * [54] AttType ::= StringType | TokenizedType | EnumeratedType + * [55] StringType ::= 'CDATA' + * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' + * | 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' + * [57] EnumeratedType ::= NotationType | Enumeration + * </pre> + */ + private String readAttType() + throws Exception + { + if (tryRead('(')) + { + parseEnumeration(false); + return "ENUMERATION"; + } + else + { + String typeString = readNmtoken(true); + if (handler.stringInterning) + { + if ("NOTATION" == typeString) + { + parseNotationType(); + return typeString; + } + else if ("CDATA" == typeString + || "ID" == typeString + || "IDREF" == typeString + || "IDREFS" == typeString + || "ENTITY" == typeString + || "ENTITIES" == typeString + || "NMTOKEN" == typeString + || "NMTOKENS" == typeString) + { + return typeString; + } + } + else + { + if ("NOTATION".equals(typeString)) + { + parseNotationType(); + return typeString; + } + else if ("CDATA".equals(typeString) + || "ID".equals(typeString) + || "IDREF".equals(typeString) + || "IDREFS".equals(typeString) + || "ENTITY".equals(typeString) + || "ENTITIES".equals(typeString) + || "NMTOKEN".equals(typeString) + || "NMTOKENS".equals(typeString)) + { + return typeString; + } + } + error("illegal attribute type", typeString, null); + return null; + } + } + + /** + * Parse an enumeration. + * <pre> + * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' + * </pre> + * <p>NOTE: the '(' has already been read. + */ + private void parseEnumeration(boolean isNames) + throws Exception + { + dataBufferAppend('('); + + // Read the first token. + skipWhitespace(); + dataBufferAppend(readNmtoken(isNames)); + // Read the remaining tokens. + skipWhitespace(); + while (!tryRead(')')) + { + require('|'); + dataBufferAppend('|'); + skipWhitespace(); + dataBufferAppend(readNmtoken (isNames)); + skipWhitespace(); + } + dataBufferAppend(')'); + } + + /** + * Parse a notation type for an attribute. + * <pre> + * [58] NotationType ::= 'NOTATION' S '(' S? NameNtoks + * (S? '|' S? name)* S? ')' + * </pre> + * <p>NOTE: the 'NOTATION' has already been read + */ + private void parseNotationType() + throws Exception + { + requireWhitespace(); + require('('); + + parseEnumeration(true); + } + + /** + * Parse the default value for an attribute. + * <pre> + * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' + * | (('#FIXED' S)? AttValue) + * </pre> + */ + private void parseDefault(String elementName, String name, + String type, String enumer) + throws Exception + { + int valueType = ATTRIBUTE_DEFAULT_SPECIFIED; + String value = null; + int flags = LIT_ATTRIBUTE; + boolean saved = expandPE; + String defaultType = null; + + // LIT_ATTRIBUTE forces '<' checks now (ASAP) and turns whitespace + // chars to spaces (doesn't matter when that's done if it doesn't + // interfere with char refs expanding to whitespace). + + if (!skippedPE) + { + flags |= LIT_ENTITY_REF; + if (handler.stringInterning) + { + if ("CDATA" != type) + { + flags |= LIT_NORMALIZE; + } + } + else + { + if (!"CDATA".equals(type)) + { + flags |= LIT_NORMALIZE; + } + } + } + + expandPE = false; + if (tryRead('#')) + { + if (tryRead("FIXED")) + { + defaultType = "#FIXED"; + valueType = ATTRIBUTE_DEFAULT_FIXED; + requireWhitespace(); + value = readLiteral(flags); + } + else if (tryRead("REQUIRED")) + { + defaultType = "#REQUIRED"; + valueType = ATTRIBUTE_DEFAULT_REQUIRED; + } + else if (tryRead("IMPLIED")) + { + defaultType = "#IMPLIED"; + valueType = ATTRIBUTE_DEFAULT_IMPLIED; + } + else + { + error("illegal keyword for attribute default value"); + } + } + else + { + value = readLiteral(flags); + } + expandPE = saved; + setAttribute(elementName, name, type, enumer, value, valueType); + if (handler.stringInterning) + { + if ("ENUMERATION" == type) + { + type = enumer; + } + else if ("NOTATION" == type) + { + type = "NOTATION " + enumer; + } + } + else + { + if ("ENUMERATION".equals(type)) + { + type = enumer; + } + else if ("NOTATION".equals(type)) + { + type = "NOTATION " + enumer; + } + } + if (!skippedPE) + { + handler.getDeclHandler().attributeDecl(elementName, name, type, + defaultType, value); + } + } + + /** + * Parse a conditional section. + * <pre> + * [61] conditionalSect ::= includeSect || ignoreSect + * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' + * extSubsetDecl ']]>' + * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' + * ignoreSectContents* ']]>' + * [64] ignoreSectContents ::= Ignore + * ('<![' ignoreSectContents* ']]>' Ignore )* + * [65] Ignore ::= Char* - (Char* ( '<![' | ']]>') Char* ) + * </pre> + * <p> NOTE: the '>![' has already been read. + */ + private void parseConditionalSect(char[] saved) + throws Exception + { + skipWhitespace(); + if (tryRead("INCLUDE")) + { + skipWhitespace(); + require('['); + // VC: Proper Conditional Section/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Conditional Section/PE nesting"); + } + skipWhitespace(); + while (!tryRead("]]>")) + { + parseMarkupdecl(); + skipWhitespace(); + } + } + else if (tryRead("IGNORE")) + { + skipWhitespace(); + require('['); + // VC: Proper Conditional Section/PE Nesting + if (readBuffer != saved) + { + handler.verror("Illegal Conditional Section/PE nesting"); + } + int nesting = 1; + char c; + expandPE = false; + for (int nest = 1; nest > 0; ) + { + c = readCh(); + switch (c) + { + case '<': + if (tryRead("![")) + { + nest++; + } + case ']': + if (tryRead("]>")) + { + nest--; + } + } + } + expandPE = true; + } + else + { + error("conditional section must begin with INCLUDE or IGNORE"); + } + } + + private void parseCharRef() + throws SAXException, IOException + { + parseCharRef(true /* do flushDataBuffer by default */); + } + + /** + * Try to read a character reference without consuming data from buffer. + * <pre> + * [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';' + * </pre> + * <p>NOTE: the '&#' has already been read. + */ + private void tryReadCharRef() + throws SAXException, IOException + { + int value = 0; + char c; + + if (tryRead('x')) + { +loop1: + while (true) + { + c = readCh(); + if (c == ';') + { + break loop1; + } + else + { + int n = Character.digit(c, 16); + if (n == -1) + { + error("illegal character in character reference", c, null); + break loop1; + } + value *= 16; + value += n; + } + } + } + else + { +loop2: + while (true) + { + c = readCh(); + if (c == ';') + { + break loop2; + } + else + { + int n = Character.digit(c, 10); + if (n == -1) + { + error("illegal character in character reference", c, null); + break loop2; + } + value *= 10; + value += n; + } + } + } + + // check for character refs being legal XML + if ((value < 0x0020 + && ! (value == '\n' || value == '\t' || value == '\r')) + || (value >= 0xD800 && value <= 0xDFFF) + || value == 0xFFFE || value == 0xFFFF + || value > 0x0010ffff) + { + error("illegal XML character reference U+" + + Integer.toHexString(value)); + } + + // Check for surrogates: 00000000 0000xxxx yyyyyyyy zzzzzzzz + // (1101|10xx|xxyy|yyyy + 1101|11yy|zzzz|zzzz: + if (value > 0x0010ffff) + { + // too big for surrogate + error("character reference " + value + " is too large for UTF-16", + new Integer(value).toString(), null); + } + + } + + /** + * Read and interpret a character reference. + * <pre> + * [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';' + * </pre> + * <p>NOTE: the '&#' has already been read. + */ + private void parseCharRef(boolean doFlush) + throws SAXException, IOException + { + int value = 0; + char c; + + if (tryRead('x')) + { +loop1: + while (true) + { + c = readCh(); + if (c == ';') + { + break loop1; + } + else + { + int n = Character.digit(c, 16); + if (n == -1) + { + error("illegal character in character reference", c, null); + break loop1; + } + value *= 16; + value += n; + } + } + } + else + { +loop2: + while (true) + { + c = readCh(); + if (c == ';') + { + break loop2; + } + else + { + int n = Character.digit(c, 10); + if (n == -1) + { + error("illegal character in character reference", c, null); + break loop2; + } + value *= 10; + value += c - '0'; + } + } + } + + // check for character refs being legal XML + if ((value < 0x0020 + && ! (value == '\n' || value == '\t' || value == '\r')) + || (value >= 0xD800 && value <= 0xDFFF) + || value == 0xFFFE || value == 0xFFFF + || value > 0x0010ffff) + { + error("illegal XML character reference U+" + + Integer.toHexString(value)); + } + + // Check for surrogates: 00000000 0000xxxx yyyyyyyy zzzzzzzz + // (1101|10xx|xxyy|yyyy + 1101|11yy|zzzz|zzzz: + if (value <= 0x0000ffff) + { + // no surrogates needed + dataBufferAppend((char) value); + } + else if (value <= 0x0010ffff) + { + value -= 0x10000; + // > 16 bits, surrogate needed + dataBufferAppend((char) (0xd800 | (value >> 10))); + dataBufferAppend((char) (0xdc00 | (value & 0x0003ff))); + } + else + { + // too big for surrogate + error("character reference " + value + " is too large for UTF-16", + new Integer(value).toString(), null); + } + if (doFlush) + { + dataBufferFlush(); + } + } + + /** + * Parse and expand an entity reference. + * <pre> + * [68] EntityRef ::= '&' Name ';' + * </pre> + * <p>NOTE: the '&' has already been read. + * @param externalAllowed External entities are allowed here. + */ + private void parseEntityRef(boolean externalAllowed) + throws SAXException, IOException + { + String name; + + name = readNmtoken(true); + require(';'); + switch (getEntityType(name)) + { + case ENTITY_UNDECLARED: + // NOTE: XML REC describes amazingly convoluted handling for + // this case. Nothing as meaningful as being a WFness error + // unless the processor might _legitimately_ not have seen a + // declaration ... which is what this implements. + String message; + + message = "reference to undeclared general entity " + name; + if (skippedPE && !docIsStandalone) + { + handler.verror(message); + // we don't know this entity, and it might be external... + if (externalAllowed) + { + handler.skippedEntity(name); + } + } + else + { + error(message); + } + break; + case ENTITY_INTERNAL: + pushString(name, getEntityValue(name)); + + //workaround for possible input pop before marking + //the buffer reading position + char t = readCh(); + unread(t); + int bufferPosMark = readBufferPos; + + int end = readBufferPos + getEntityValue(name).length(); + for (int k = readBufferPos; k < end; k++) + { + t = readCh(); + if (t == '&') + { + t = readCh(); + if (t == '#') + { + //try to match a character ref + tryReadCharRef(); + + //everything has been read + if (readBufferPos >= end) + { + break; + } + k = readBufferPos; + continue; + } + else if (Character.isLetter(t)) + { + //looks like an entity ref + unread(t); + readNmtoken(true); + require(';'); + + //everything has been read + if (readBufferPos >= end) + { + break; + } + k = readBufferPos; + continue; + } + error(" malformed entity reference"); + } + + } + readBufferPos = bufferPosMark; + break; + case ENTITY_TEXT: + if (externalAllowed) + { + pushURL(false, name, getEntityIds(name), + null, null, null, true); + } + else + { + error("reference to external entity in attribute value.", + name, null); + } + break; + case ENTITY_NDATA: + if (externalAllowed) + { + error("unparsed entity reference in content", name, null); + } + else + { + error("reference to external entity in attribute value.", + name, null); + } + break; + default: + throw new RuntimeException(); + } + } + + /** + * Parse and expand a parameter entity reference. + * <pre> + * [69] PEReference ::= '%' Name ';' + * </pre> + * <p>NOTE: the '%' has already been read. + */ + private void parsePEReference() + throws SAXException, IOException + { + String name; + + name = "%" + readNmtoken(true); + require(';'); + switch (getEntityType(name)) + { + case ENTITY_UNDECLARED: + // VC: Entity Declared + handler.verror("reference to undeclared parameter entity " + name); + + // we should disable handling of all subsequent declarations + // unless this is a standalone document (info discarded) + break; + case ENTITY_INTERNAL: + if (inLiteral) + { + pushString(name, getEntityValue(name)); + } + else + { + pushString(name, ' ' + getEntityValue(name) + ' '); + } + break; + case ENTITY_TEXT: + if (!inLiteral) + { + pushString(null, " "); + } + pushURL(true, name, getEntityIds(name), null, null, null, true); + if (!inLiteral) + { + pushString(null, " "); + } + break; + } + } + + /** + * Parse an entity declaration. + * <pre> + * [70] EntityDecl ::= GEDecl | PEDecl + * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' + * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' + * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) + * [74] PEDef ::= EntityValue | ExternalID + * [75] ExternalID ::= 'SYSTEM' S SystemLiteral + * | 'PUBLIC' S PubidLiteral S SystemLiteral + * [76] NDataDecl ::= S 'NDATA' S Name + * </pre> + * <p>NOTE: the '<!ENTITY' has already been read. + */ + private void parseEntityDecl() + throws Exception + { + boolean peFlag = false; + int flags = 0; + + // Check for a parameter entity. + expandPE = false; + requireWhitespace(); + if (tryRead('%')) + { + peFlag = true; + requireWhitespace(); + } + expandPE = true; + + // Read the entity name, and prepend + // '%' if necessary. + String name = readNmtoken(true); + //NE08 + if (name.indexOf(':') >= 0) + { + error("Illegal character(':') in entity name ", name, null); + } + if (peFlag) + { + name = "%" + name; + } + + // Read the entity value. + requireWhitespace(); + char c = readCh(); + unread (c); + if (c == '"' || c == '\'') + { + // Internal entity ... replacement text has expanded refs + // to characters and PEs, but not to general entities + String value = readLiteral(flags); + setInternalEntity(name, value); + } + else + { + // Read the external IDs + ExternalIdentifiers ids = readExternalIds(false, false); + + // Check for NDATA declaration. + boolean white = tryWhitespace(); + if (!peFlag && tryRead("NDATA")) + { + if (!white) + { + error("whitespace required before NDATA"); + } + requireWhitespace(); + String notationName = readNmtoken(true); + if (!skippedPE) + { + setExternalEntity(name, ENTITY_NDATA, ids, notationName); + handler.unparsedEntityDecl(name, ids.publicId, ids.systemId, + ids.baseUri, notationName); + } + } + else if (!skippedPE) + { + setExternalEntity(name, ENTITY_TEXT, ids, null); + handler.getDeclHandler() + .externalEntityDecl(name, ids.publicId, + handler.resolveURIs() + // FIXME: ASSUMES not skipped + // "false" forces error on bad URI + ? handler.absolutize(ids.baseUri, + ids.systemId, + false) + : ids.systemId); + } + } + + // Finish the declaration. + skipWhitespace(); + require('>'); + } + + /** + * Parse a notation declaration. + * <pre> + * [82] NotationDecl ::= '<!NOTATION' S Name S + * (ExternalID | PublicID) S? '>' + * [83] PublicID ::= 'PUBLIC' S PubidLiteral + * </pre> + * <P>NOTE: the '<!NOTATION' has already been read. + */ + private void parseNotationDecl() + throws Exception + { + String nname; + ExternalIdentifiers ids; + + requireWhitespace(); + nname = readNmtoken(true); + //NE08 + if (nname.indexOf(':') >= 0) + { + error("Illegal character(':') in notation name ", nname, null); + } + requireWhitespace(); + + // Read the external identifiers. + ids = readExternalIds(true, false); + + // Register the notation. + setNotation(nname, ids); + + skipWhitespace(); + require('>'); + } + + /** + * Parse character data. + * <pre> + * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) + * </pre> + */ + private void parseCharData() + throws Exception + { + char c; + int state = 0; + boolean pureWhite = false; + + // assert (dataBufferPos == 0); + + // are we expecting pure whitespace? it might be dirty... + if ((currentElementContent == CONTENT_ELEMENTS) && !isDirtyCurrentElement) + { + pureWhite = true; + } + + // always report right out of readBuffer + // to minimize (pointless) buffer copies + while (true) + { + int lineAugment = 0; + int columnAugment = 0; + int i; + +loop: + for (i = readBufferPos; i < readBufferLength; i++) + { + switch (c = readBuffer[i]) + { + case '\n': + lineAugment++; + columnAugment = 0; + // pureWhite unmodified + break; + case '\r': // should not happen!! + case '\t': + case ' ': + // pureWhite unmodified + columnAugment++; + break; + case '&': + case '<': + columnAugment++; + // pureWhite unmodified + // CLEAN end of text sequence + state = 1; + break loop; + case ']': + // that's not a whitespace char, and + // can not terminate pure whitespace either + pureWhite = false; + if ((i + 2) < readBufferLength) + { + if (readBuffer [i + 1] == ']' + && readBuffer [i + 2] == '>') + { + // ERROR end of text sequence + state = 2; + break loop; + } + } + else + { + // FIXME missing two end-of-buffer cases + } + columnAugment++; + break; + default: + if ((c < 0x0020 || c > 0xFFFD) + || ((c >= 0x007f) && (c <= 0x009f) && (c != 0x0085) + && xmlVersion == XML_11)) + { + error("illegal XML character U+" + + Integer.toHexString(c)); + } + // that's not a whitespace char + pureWhite = false; + columnAugment++; + } + } + + // report text thus far + if (lineAugment > 0) + { + line += lineAugment; + column = columnAugment; + } + else + { + column += columnAugment; + } + + // report characters/whitspace + int length = i - readBufferPos; + + if (length != 0) + { + if (pureWhite) + { + handler.ignorableWhitespace(readBuffer, + readBufferPos, length); + } + else + { + handler.charData(readBuffer, readBufferPos, length); + } + readBufferPos = i; + } + + if (state != 0) + { + break; + } + + // fill next buffer from this entity, or + // pop stack and continue with previous entity + unread(readCh()); + } + if (!pureWhite) + { + isDirtyCurrentElement = true; + } + // finish, maybe with error + if (state != 1) // finish, no error + { + error("character data may not contain ']]>'"); + } + } + + ////////////////////////////////////////////////////////////////////// + // High-level reading and scanning methods. + ////////////////////////////////////////////////////////////////////// + + /** + * Require whitespace characters. + */ + private void requireWhitespace() + throws SAXException, IOException + { + char c = readCh(); + if (isWhitespace(c)) + { + skipWhitespace(); + } + else + { + error("whitespace required", c, null); + } + } + + /** + * Skip whitespace characters. + * <pre> + * [3] S ::= (#x20 | #x9 | #xd | #xa)+ + * </pre> + */ + private void skipWhitespace() + throws SAXException, IOException + { + // Start with a little cheat. Most of + // the time, the white space will fall + // within the current read buffer; if + // not, then fall through. + if (USE_CHEATS) + { + int lineAugment = 0; + int columnAugment = 0; + +loop: + for (int i = readBufferPos; i < readBufferLength; i++) + { + switch (readBuffer[i]) + { + case ' ': + case '\t': + case '\r': + columnAugment++; + break; + case '\n': + lineAugment++; + columnAugment = 0; + break; + case '%': + if (expandPE) + { + break loop; + } + // else fall through... + default: + readBufferPos = i; + if (lineAugment > 0) + { + line += lineAugment; + column = columnAugment; + } + else + { + column += columnAugment; + } + return; + } + } + } + + // OK, do it the slow way. + char c = readCh (); + while (isWhitespace(c)) + { + c = readCh(); + } + unread(c); + } + + /** + * Read a name or (when parsing an enumeration) name token. + * <pre> + * [5] Name ::= (Letter | '_' | ':') (NameChar)* + * [7] Nmtoken ::= (NameChar)+ + * </pre> + */ + private String readNmtoken(boolean isName) + throws SAXException, IOException + { + char c; + + if (USE_CHEATS) + { +loop: + for (int i = readBufferPos; i < readBufferLength; i++) + { + c = readBuffer[i]; + switch (c) + { + case '%': + if (expandPE) + { + break loop; + } + // else fall through... + + // What may legitimately come AFTER a name/nmtoken? + case '<': case '>': case '&': + case ',': case '|': case '*': case '+': case '?': + case ')': + case '=': + case '\'': case '"': + case '[': + case ' ': case '\t': case '\r': case '\n': + case ';': + case '/': + int start = readBufferPos; + if (i == start) + { + error("name expected", readBuffer[i], null); + } + readBufferPos = i; + return intern(readBuffer, start, i - start); + + default: + // FIXME ... per IBM's OASIS test submission, these: + // ? U+06dd + // Combining U+309B + //these switches are kind of ugly but at least we won't + //have to go over the whole lits for each char + if (isName && i == readBufferPos) + { + char c2 = (char) (c & 0x00f0); + switch (c & 0xff00) + { + //starting with 01 + case 0x0100: + switch (c2) + { + case 0x0030: + if (c == 0x0132 || c == 0x0133 || c == 0x013f) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x0040: + if (c == 0x0140 || c == 0x0149) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x00c0: + if (c == 0x01c4 || c == 0x01cc) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x00f0: + if (c == 0x01f1 || c == 0x01f3) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x00b0: + if (c == 0x01f1 || c == 0x01f3) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + default: + if (c == 0x017f) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + } + + break; + //starting with 11 + case 0x1100: + switch (c2) + { + case 0x0000: + if (c == 0x1104 || c == 0x1108 || + c == 0x110a || c == 0x110d) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x0030: + if (c == 0x113b || c == 0x113f) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x0040: + if (c == 0x1141 || c == 0x114d + || c == 0x114f ) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x0050: + if (c == 0x1151 || c == 0x1156) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x0060: + if (c == 0x1162 || c == 0x1164 + || c == 0x1166 || c == 0x116b + || c == 0x116f) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + case 0x00b0: + if (c == 0x11b6 || c == 0x11b9 + || c == 0x11bb || c == 0x116f) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + break; + default: + if (c == 0x1174 || c == 0x119f + || c == 0x11ac || c == 0x11c3 + || c == 0x11f1) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + } + break; + default: + if (c == 0x0e46 || c == 0x1011 + || c == 0x212f || c == 0x0587 + || c == 0x0230 ) + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + } + } + // punt on exact tests from Appendix A; approximate + // them using the Unicode ID start/part rules + if (i == readBufferPos && isName) + { + if (!Character.isUnicodeIdentifierStart(c) + && c != ':' && c != '_') + { + error("Not a name start character, U+" + + Integer.toHexString(c)); + } + } + else if (!Character.isUnicodeIdentifierPart(c) + && c != '-' && c != ':' && c != '_' && c != '.' + && !isExtender(c)) + { + error("Not a name character, U+" + + Integer.toHexString(c)); + } + } + } + } + + nameBufferPos = 0; + + // Read the first character. +loop: + while (true) + { + c = readCh(); + switch (c) + { + case '%': + case '<': case '>': case '&': + case ',': case '|': case '*': case '+': case '?': + case ')': + case '=': + case '\'': case '"': + case '[': + case ' ': case '\t': case '\n': case '\r': + case ';': + case '/': + unread(c); + if (nameBufferPos == 0) + { + error ("name expected"); + } + // punt on exact tests from Appendix A, but approximate them + if (isName + && !Character.isUnicodeIdentifierStart(nameBuffer[0]) + && ":_".indexOf(nameBuffer[0]) == -1) + { + error("Not a name start character, U+" + + Integer.toHexString(nameBuffer[0])); + } + String s = intern(nameBuffer, 0, nameBufferPos); + nameBufferPos = 0; + return s; + default: + // punt on exact tests from Appendix A, but approximate them + + if ((nameBufferPos != 0 || !isName) + && !Character.isUnicodeIdentifierPart(c) + && ":-_.".indexOf(c) == -1 + && !isExtender(c)) + { + error("Not a name character, U+" + + Integer.toHexString(c)); + } + if (nameBufferPos >= nameBuffer.length) + { + nameBuffer = + (char[]) extendArray(nameBuffer, + nameBuffer.length, nameBufferPos); + } + nameBuffer[nameBufferPos++] = c; + } + } + } + + private static boolean isExtender(char c) + { + // [88] Extender ::= ... + return c == 0x00b7 || c == 0x02d0 || c == 0x02d1 || c == 0x0387 + || c == 0x0640 || c == 0x0e46 || c == 0x0ec6 || c == 0x3005 + || (c >= 0x3031 && c <= 0x3035) + || (c >= 0x309d && c <= 0x309e) + || (c >= 0x30fc && c <= 0x30fe); + } + + /** + * Read a literal. With matching single or double quotes as + * delimiters (and not embedded!) this is used to parse: + * <pre> + * [9] EntityValue ::= ... ([^%&] | PEReference | Reference)* ... + * [10] AttValue ::= ... ([^<&] | Reference)* ... + * [11] SystemLiteral ::= ... (URLchar - "'")* ... + * [12] PubidLiteral ::= ... (PubidChar - "'")* ... + * </pre> + * as well as the quoted strings in XML and text declarations + * (for version, encoding, and standalone) which have their + * own constraints. + */ + private String readLiteral(int flags) + throws SAXException, IOException + { + char delim, c; + int startLine = line; + boolean saved = expandPE; + boolean savedReport = doReport; + + // Find the first delimiter. + delim = readCh(); + if (delim != '"' && delim != '\'') + { + error("expected '\"' or \"'\"", delim, null); + return null; + } + inLiteral = true; + if ((flags & LIT_DISABLE_PE) != 0) + { + expandPE = false; + } + doReport = false; + + // Each level of input source has its own buffer; remember + // ours, so we won't read the ending delimiter from any + // other input source, regardless of entity processing. + char[] ourBuf = readBuffer; + + // Read the literal. + try + { + c = readCh(); + boolean ampRead = false; +loop: + while (! (c == delim && readBuffer == ourBuf)) + { + switch (c) + { + // attributes and public ids are normalized + // in almost the same ways + case '\n': + case '\r': + if ((flags & (LIT_ATTRIBUTE | LIT_PUBID)) != 0) + { + c = ' '; + } + break; + case '\t': + if ((flags & LIT_ATTRIBUTE) != 0) + { + c = ' '; + } + break; + case '&': + c = readCh(); + // Char refs are expanded immediately, except for + // all the cases where it's deferred. + if (c == '#') + { + if ((flags & LIT_DISABLE_CREF) != 0) + { + dataBufferAppend('&'); + break; + } + parseCharRef(false /* Do not do flushDataBuffer */); + + // exotic WFness risk: this is an entity literal, + // dataBuffer [dataBufferPos - 1] == '&', and + // following chars are a _partial_ entity/char ref + + // It looks like an entity ref ... + } + else + { + unread(c); + // Expand it? + if ((flags & LIT_ENTITY_REF) > 0) + { + parseEntityRef(false); + if (String.valueOf(readBuffer).equals("&")) + { + ampRead = true; + } + //Is it just data? + } + else if ((flags & LIT_DISABLE_EREF) != 0) + { + dataBufferAppend('&'); + + // OK, it will be an entity ref -- expanded later. + } + else + { + String name = readNmtoken(true); + require(';'); + dataBufferAppend('&'); + dataBufferAppend(name); + dataBufferAppend(';'); + } + } + c = readCh(); + continue loop; + + case '<': + // and why? Perhaps so "&foo;" expands the same + // inside and outside an attribute? + if ((flags & LIT_ATTRIBUTE) != 0) + { + error("attribute values may not contain '<'"); + } + break; + + // We don't worry about case '%' and PE refs, readCh does. + + default: + break; + } + dataBufferAppend(c); + c = readCh(); + } + } + catch (EOFException e) + { + error("end of input while looking for delimiter (started on line " + + startLine + ')', null, new Character(delim).toString()); + } + inLiteral = false; + expandPE = saved; + doReport = savedReport; + + // Normalise whitespace if necessary. + if ((flags & LIT_NORMALIZE) > 0) + { + dataBufferNormalize(); + } + + // Return the value. + return dataBufferToString(); + } + + /** + * Try reading external identifiers. + * A system identifier is not required for notations. + * @param inNotation Are we parsing a notation decl? + * @param isSubset Parsing external subset decl (may be omitted)? + * @return A three-member String array containing the identifiers, + * or nulls. Order: public, system, baseURI. + */ + private ExternalIdentifiers readExternalIds(boolean inNotation, + boolean isSubset) + throws Exception + { + char c; + ExternalIdentifiers ids = new ExternalIdentifiers(); + int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF; + + if (tryRead("PUBLIC")) + { + requireWhitespace(); + ids.publicId = readLiteral(LIT_NORMALIZE | LIT_PUBID | flags); + if (inNotation) + { + skipWhitespace(); + c = readCh(); + unread(c); + if (c == '"' || c == '\'') + { + ids.systemId = readLiteral(flags); + } + } + else + { + requireWhitespace(); + ids.systemId = readLiteral(flags); + } + + for (int i = 0; i < ids.publicId.length(); i++) + { + c = ids.publicId.charAt(i); + if (c >= 'a' && c <= 'z') + { + continue; + } + if (c >= 'A' && c <= 'Z') + { + continue; + } + if (" \r\n0123456789-' ()+,./:=?;!*#@$_%".indexOf(c) != -1) + { + continue; + } + error("illegal PUBLIC id character U+" + + Integer.toHexString(c)); + } + } + else if (tryRead("SYSTEM")) + { + requireWhitespace(); + ids.systemId = readLiteral(flags); + } + else if (!isSubset) + { + error("missing SYSTEM or PUBLIC keyword"); + } + + if (ids.systemId != null) + { + if (ids.systemId.indexOf('#') != -1) + { + handler.verror("SYSTEM id has a URI fragment: " + ids.systemId); + } + ids.baseUri = handler.getSystemId(); + if (ids.baseUri == null && uriWarnings) + { + handler.warn("No base URI; hope URI is absolute: " + + ids.systemId); + } + } + + return ids; + } + + /** + * Test if a character is whitespace. + * <pre> + * [3] S ::= (#x20 | #x9 | #xd | #xa)+ + * </pre> + * @param c The character to test. + * @return true if the character is whitespace. + */ + private final boolean isWhitespace(char c) + { + if (c > 0x20) + { + return false; + } + if (c == 0x20 || c == 0x0a || c == 0x09 || c == 0x0d) + { + return true; + } + return false; // illegal ... + } + + ////////////////////////////////////////////////////////////////////// + // Utility routines. + ////////////////////////////////////////////////////////////////////// + + /** + * Add a character to the data buffer. + */ + private void dataBufferAppend(char c) + { + // Expand buffer if necessary. + if (dataBufferPos >= dataBuffer.length) + { + dataBuffer = (char[]) extendArray(dataBuffer, + dataBuffer.length, dataBufferPos); + } + dataBuffer[dataBufferPos++] = c; + } + + /** + * Add a string to the data buffer. + */ + private void dataBufferAppend(String s) + { + dataBufferAppend(s.toCharArray(), 0, s.length()); + } + + /** + * Append (part of) a character array to the data buffer. + */ + private void dataBufferAppend(char[] ch, int start, int length) + { + dataBuffer = (char[]) extendArray(dataBuffer, dataBuffer.length, + dataBufferPos + length); + + System.arraycopy(ch, start, dataBuffer, dataBufferPos, length); + dataBufferPos += length; + } + + /** + * Normalise space characters in the data buffer. + */ + private void dataBufferNormalize() + { + int i = 0; + int j = 0; + int end = dataBufferPos; + + // Skip spaces at the start. + while (j < end && dataBuffer[j] == ' ') + { + j++; + } + + // Skip whitespace at the end. + while (end > j && dataBuffer[end - 1] == ' ') + { + end --; + } + + // Start copying to the left. + while (j < end) + { + + char c = dataBuffer[j++]; + + // Normalise all other spaces to + // a single space. + if (c == ' ') + { + while (j < end && dataBuffer[j++] == ' ') + { + continue; + } + dataBuffer[i++] = ' '; + dataBuffer[i++] = dataBuffer[j - 1]; + } + else + { + dataBuffer[i++] = c; + } + } + + // The new length is <= the old one. + dataBufferPos = i; + } + + /** + * Convert the data buffer to a string. + */ + private String dataBufferToString() + { + String s = new String(dataBuffer, 0, dataBufferPos); + dataBufferPos = 0; + return s; + } + + /** + * Flush the contents of the data buffer to the handler, as + * appropriate, and reset the buffer for new input. + */ + private void dataBufferFlush() + throws SAXException + { + if (currentElementContent == CONTENT_ELEMENTS + && dataBufferPos > 0 + && !inCDATA) + { + // We can't just trust the buffer to be whitespace, there + // are (error) cases when it isn't + for (int i = 0; i < dataBufferPos; i++) + { + if (!isWhitespace(dataBuffer[i])) + { + handler.charData(dataBuffer, 0, dataBufferPos); + dataBufferPos = 0; + } + } + if (dataBufferPos > 0) + { + handler.ignorableWhitespace(dataBuffer, 0, dataBufferPos); + dataBufferPos = 0; + } + } + else if (dataBufferPos > 0) + { + handler.charData(dataBuffer, 0, dataBufferPos); + dataBufferPos = 0; + } + } + + /** + * Require a string to appear, or throw an exception. + * <p><em>Precondition:</em> Entity expansion is not required. + * <p><em>Precondition:</em> data buffer has no characters that + * will get sent to the application. + */ + private void require(String delim) + throws SAXException, IOException + { + int length = delim.length(); + char[] ch; + + if (length < dataBuffer.length) + { + ch = dataBuffer; + delim.getChars(0, length, ch, 0); + } + else + { + ch = delim.toCharArray(); + } + + if (USE_CHEATS && length <= (readBufferLength - readBufferPos)) + { + int offset = readBufferPos; + + for (int i = 0; i < length; i++, offset++) + { + if (ch[i] != readBuffer[offset]) + { + error ("required string", null, delim); + } + } + readBufferPos = offset; + + } + else + { + for (int i = 0; i < length; i++) + { + require(ch[i]); + } + } + } + + /** + * Require a character to appear, or throw an exception. + */ + private void require(char delim) + throws SAXException, IOException + { + char c = readCh(); + + if (c != delim) + { + error("required character", c, new Character(delim).toString()); + } + } + + /** + * Create an interned string from a character array. + * Ælfred uses this method to create an interned version + * of all names and name tokens, so that it can test equality + * with <code>==</code> instead of <code>String.equals ()</code>. + * + * <p>This is much more efficient than constructing a non-interned + * string first, and then interning it. + * + * @param ch an array of characters for building the string. + * @param start the starting position in the array. + * @param length the number of characters to place in the string. + * @return an interned string. + * @see #intern (String) + * @see java.lang.String#intern + */ + public String intern(char[] ch, int start, int length) + { + int index = 0; + int hash = 0; + Object[] bucket; + + // Generate a hash code. This is a widely used string hash, + // often attributed to Brian Kernighan. + for (int i = start; i < start + length; i++) + { + hash = 31 * hash + ch[i]; + } + hash = (hash & 0x7fffffff) % SYMBOL_TABLE_LENGTH; + + // Get the bucket -- consists of {array,String} pairs + if ((bucket = symbolTable[hash]) == null) + { + // first string in this bucket + bucket = new Object[8]; + + // Search for a matching tuple, and + // return the string if we find one. + } + else + { + while (index < bucket.length) + { + char[] chFound = (char[]) bucket[index]; + + // Stop when we hit an empty entry. + if (chFound == null) + { + break; + } + + // If they're the same length, check for a match. + if (chFound.length == length) + { + for (int i = 0; i < chFound.length; i++) + { + // continue search on failure + if (ch[start + i] != chFound[i]) + { + break; + } + else if (i == length - 1) + { + // That's it, we have a match! + return (String) bucket[index + 1]; + } + } + } + index += 2; + } + // Not found -- we'll have to add it. + + // Do we have to grow the bucket? + bucket = (Object[]) extendArray(bucket, bucket.length, index); + } + symbolTable[hash] = bucket; + + // OK, add it to the end of the bucket -- "local" interning. + // Intern "globally" to let applications share interning benefits. + // That is, "!=" and "==" work on our strings, not just equals(). + String s = new String(ch, start, length).intern(); + bucket[index] = s.toCharArray(); + bucket[index + 1] = s; + return s; + } + + /** + * Ensure the capacity of an array, allocating a new one if + * necessary. Usually extends only for name hash collisions. + */ + private Object extendArray(Object array, int currentSize, int requiredSize) + { + if (requiredSize < currentSize) + { + return array; + } + else + { + Object newArray = null; + int newSize = currentSize * 2; + + if (newSize <= requiredSize) + { + newSize = requiredSize + 1; + } + + if (array instanceof char[]) + { + newArray = new char[newSize]; + } + else if (array instanceof Object[]) + { + newArray = new Object[newSize]; + } + else + { + throw new RuntimeException(); + } + + System.arraycopy(array, 0, newArray, 0, currentSize); + return newArray; + } + } + + ////////////////////////////////////////////////////////////////////// + // XML query routines. + ////////////////////////////////////////////////////////////////////// + + boolean isStandalone() + { + return docIsStandalone; + } + + // + // Elements + // + + private int getContentType(ElementDecl element, int defaultType) + { + int retval; + + if (element == null) + { + return defaultType; + } + retval = element.contentType; + if (retval == CONTENT_UNDECLARED) + { + retval = defaultType; + } + return retval; + } + + /** + * Look up the content type of an element. + * @param name The element type name. + * @return An integer constant representing the content type. + * @see #CONTENT_UNDECLARED + * @see #CONTENT_ANY + * @see #CONTENT_EMPTY + * @see #CONTENT_MIXED + * @see #CONTENT_ELEMENTS + */ + public int getElementContentType(String name) + { + ElementDecl element = (ElementDecl) elementInfo.get(name); + return getContentType(element, CONTENT_UNDECLARED); + } + + /** + * Register an element. + * Array format: + * [0] element type name + * [1] content model (mixed, elements only) + * [2] attribute hash table + */ + private void setElement(String name, int contentType, + String contentModel, HashMap attributes) + throws SAXException + { + if (skippedPE) + { + return; + } + + ElementDecl element = (ElementDecl) elementInfo.get(name); + + // first <!ELEMENT ...> or <!ATTLIST ...> for this type? + if (element == null) + { + element = new ElementDecl(); + element.contentType = contentType; + element.contentModel = contentModel; + element.attributes = attributes; + elementInfo.put(name, element); + return; + } + + // <!ELEMENT ...> declaration? + if (contentType != CONTENT_UNDECLARED) + { + // ... following an associated <!ATTLIST ...> + if (element.contentType == CONTENT_UNDECLARED) + { + element.contentType = contentType; + element.contentModel = contentModel; + } + else + { + // VC: Unique Element Type Declaration + handler.verror("multiple declarations for element type: " + + name); + } + } + + // first <!ATTLIST ...>, before <!ELEMENT ...> ? + else if (attributes != null) + { + element.attributes = attributes; + } + } + + /** + * Look up the attribute hash table for an element. + * The hash table is the second item in the element array. + */ + private HashMap getElementAttributes(String name) + { + ElementDecl element = (ElementDecl) elementInfo.get(name); + return (element == null) ? null : element.attributes; + } + + // + // Attributes + // + + /** + * Get the declared attributes for an element type. + * @param elname The name of the element type. + * @return An iterator over all the attributes declared for + * a specific element type. The results will be valid only + * after the DTD (if any) has been parsed. + * @see #getAttributeType + * @see #getAttributeEnumeration + * @see #getAttributeDefaultValueType + * @see #getAttributeDefaultValue + * @see #getAttributeExpandedValue + */ + private Iterator declaredAttributes(ElementDecl element) + { + HashMap attlist; + + if (element == null) + { + return null; + } + if ((attlist = element.attributes) == null) + { + return null; + } + return attlist.keySet().iterator(); + } + + /** + * Get the declared attributes for an element type. + * @param elname The name of the element type. + * @return An iterator over all the attributes declared for + * a specific element type. The results will be valid only + * after the DTD (if any) has been parsed. + * @see #getAttributeType + * @see #getAttributeEnumeration + * @see #getAttributeDefaultValueType + * @see #getAttributeDefaultValue + * @see #getAttributeExpandedValue + */ + public Iterator declaredAttributes(String elname) + { + return declaredAttributes((ElementDecl) elementInfo.get(elname)); + } + + /** + * Retrieve the declared type of an attribute. + * @param name The name of the associated element. + * @param aname The name of the attribute. + * @return An interend string denoting the type, or null + * indicating an undeclared attribute. + */ + public String getAttributeType(String name, String aname) + { + AttributeDecl attribute = getAttribute(name, aname); + return (attribute == null) ? null : attribute.type; + } + + /** + * Retrieve the allowed values for an enumerated attribute type. + * @param name The name of the associated element. + * @param aname The name of the attribute. + * @return A string containing the token list. + */ + public String getAttributeEnumeration(String name, String aname) + { + AttributeDecl attribute = getAttribute(name, aname); + // assert: attribute.enumeration is "ENUMERATION" or "NOTATION" + return (attribute == null) ? null : attribute.enumeration; + } + + /** + * Retrieve the default value of a declared attribute. + * @param name The name of the associated element. + * @param aname The name of the attribute. + * @return The default value, or null if the attribute was + * #IMPLIED or simply undeclared and unspecified. + * @see #getAttributeExpandedValue + */ + public String getAttributeDefaultValue(String name, String aname) + { + AttributeDecl attribute = getAttribute(name, aname); + return (attribute == null) ? null : attribute.value; + } + + /* + +// FIXME: Leaving this in, until W3C finally resolves the confusion +// between parts of the XML 2nd REC about when entity declararations +// are guaranteed to be known. Current code matches what section 5.1 +// (conformance) describes, but some readings of the self-contradicting +// text in 4.1 (the "Entity Declared" WFC and VC) seem to expect that +// attribute expansion/normalization must be deferred in some cases +// (just TRY to identify them!). + + * Retrieve the expanded value of a declared attribute. + * <p>General entities (and char refs) will be expanded (once). + * @param name The name of the associated element. + * @param aname The name of the attribute. + * @return The expanded default value, or null if the attribute was + * #IMPLIED or simply undeclared + * @see #getAttributeDefaultValue + public String getAttributeExpandedValue (String name, String aname) + throws Exception + { + AttributeDecl attribute = getAttribute (name, aname); + + if (attribute == null) { + return null; + } else if (attribute.defaultValue == null && attribute.value != null) { + // we MUST use the same buf for both quotes else the literal + // can't be properly terminated + char buf [] = new char [1]; + int flags = LIT_ENTITY_REF | LIT_ATTRIBUTE; + String type = getAttributeType (name, aname); + + if (type != "CDATA" && type != null) + flags |= LIT_NORMALIZE; + buf [0] = '"'; + pushCharArray (null, buf, 0, 1); + pushString (null, attribute.value); + pushCharArray (null, buf, 0, 1); + attribute.defaultValue = readLiteral (flags); + } + return attribute.defaultValue; + } + */ + + /** + * Retrieve the default value mode of a declared attribute. + * @see #ATTRIBUTE_DEFAULT_SPECIFIED + * @see #ATTRIBUTE_DEFAULT_IMPLIED + * @see #ATTRIBUTE_DEFAULT_REQUIRED + * @see #ATTRIBUTE_DEFAULT_FIXED + */ + public int getAttributeDefaultValueType(String name, String aname) + { + AttributeDecl attribute = getAttribute(name, aname); + return (attribute == null) ? ATTRIBUTE_DEFAULT_UNDECLARED : + attribute.valueType; + } + + /** + * Register an attribute declaration for later retrieval. + * Format: + * - String type + * - String default value + * - int value type + * - enumeration + * - processed default value + */ + private void setAttribute(String elName, String name, String type, + String enumeration, String value, int valueType) + throws Exception + { + HashMap attlist; + + if (skippedPE) + { + return; + } + + // Create a new hashtable if necessary. + attlist = getElementAttributes(elName); + if (attlist == null) + { + attlist = new HashMap(); + } + + // ignore multiple attribute declarations! + if (attlist.get(name) != null) + { + // warn ... + return; + } + else + { + AttributeDecl attribute = new AttributeDecl(); + attribute.type = type; + attribute.value = value; + attribute.valueType = valueType; + attribute.enumeration = enumeration; + attlist.put(name, attribute); + + // save; but don't overwrite any existing <!ELEMENT ...> + setElement(elName, CONTENT_UNDECLARED, null, attlist); + } + } + + /** + * Retrieve the attribute declaration for the given element name and name. + */ + private AttributeDecl getAttribute(String elName, String name) + { + HashMap attlist = getElementAttributes(elName); + return (attlist == null) ? null : (AttributeDecl) attlist.get(name); + } + + // + // Entities + // + + /** + * Find the type of an entity. + * @returns An integer constant representing the entity type. + * @see #ENTITY_UNDECLARED + * @see #ENTITY_INTERNAL + * @see #ENTITY_NDATA + * @see #ENTITY_TEXT + */ + public int getEntityType(String ename) + { + EntityInfo entity = (EntityInfo) entityInfo.get(ename); + return (entity == null) ? ENTITY_UNDECLARED : entity.type; + } + + /** + * Return an external entity's identifiers. + * @param ename The name of the external entity. + * @return The entity's public identifier, system identifier, and base URI. + * Null if the entity was not declared as an external entity. + * @see #getEntityType + */ + public ExternalIdentifiers getEntityIds(String ename) + { + EntityInfo entity = (EntityInfo) entityInfo.get(ename); + return (entity == null) ? null : entity.ids; + } + + /** + * Return an internal entity's replacement text. + * @param ename The name of the internal entity. + * @return The entity's replacement text, or null if + * the entity was not declared as an internal entity. + * @see #getEntityType + */ + public String getEntityValue(String ename) + { + EntityInfo entity = (EntityInfo) entityInfo.get(ename); + return (entity == null) ? null : entity.value; + } + + /** + * Register an entity declaration for later retrieval. + */ + private void setInternalEntity(String eName, String value) + throws SAXException + { + if (skippedPE) + { + return; + } + + if (entityInfo.get(eName) == null) + { + EntityInfo entity = new EntityInfo(); + entity.type = ENTITY_INTERNAL; + entity.value = value; + entityInfo.put(eName, entity); + } + if (handler.stringInterning) + { + if ("lt" == eName || "gt" == eName || "quot" == eName + || "apos" == eName || "amp" == eName) + { + return; + } + } + else + { + if ("lt".equals(eName) || "gt".equals(eName) || "quot".equals(eName) + || "apos".equals(eName) || "amp".equals(eName)) + { + return; + } + } + handler.getDeclHandler().internalEntityDecl(eName, value); + } + + /** + * Register an external entity declaration for later retrieval. + */ + private void setExternalEntity(String eName, int eClass, + ExternalIdentifiers ids, String nName) + { + if (entityInfo.get(eName) == null) + { + EntityInfo entity = new EntityInfo(); + entity.type = eClass; + entity.ids = ids; + entity.notationName = nName; + entityInfo.put(eName, entity); + } + } + + // + // Notations. + // + + /** + * Report a notation declaration, checking for duplicates. + */ + private void setNotation(String nname, ExternalIdentifiers ids) + throws SAXException + { + if (skippedPE) + { + return; + } + + handler.notationDecl(nname, ids.publicId, ids.systemId, ids.baseUri); + if (notationInfo.get(nname) == null) + { + notationInfo.put(nname, nname); + } + else + { + // VC: Unique Notation Name + handler.verror("Duplicate notation name decl: " + nname); + } + } + + // + // Location. + // + + /** + * Return the current line number. + */ + public int getLineNumber() + { + return line; + } + + /** + * Return the current column number. + */ + public int getColumnNumber() + { + return column; + } + + ////////////////////////////////////////////////////////////////////// + // High-level I/O. + ////////////////////////////////////////////////////////////////////// + + /** + * Read a single character from the readBuffer. + * <p>The readDataChunk () method maintains the buffer. + * <p>If we hit the end of an entity, try to pop the stack and + * keep going. + * <p> (This approach doesn't really enforce XML's rules about + * entity boundaries, but this is not currently a validating + * parser). + * <p>This routine also attempts to keep track of the current + * position in external entities, but it's not entirely accurate. + * @return The next available input character. + * @see #unread (char) + * @see #readDataChunk + * @see #readBuffer + * @see #line + * @return The next character from the current input source. + */ + private char readCh() + throws SAXException, IOException + { + // As long as there's nothing in the + // read buffer, try reading more data + // (for an external entity) or popping + // the entity stack (for either). + while (readBufferPos >= readBufferLength) + { + switch (sourceType) + { + case INPUT_READER: + case INPUT_STREAM: + readDataChunk(); + while (readBufferLength < 1) + { + popInput(); + if (readBufferLength < 1) + { + readDataChunk(); + } + } + break; + + default: + + popInput(); + break; + } + } + + char c = readBuffer[readBufferPos++]; + + if (c == '\n') + { + line++; + column = 0; + } + else + { + if (c == '<') + { + /* the most common return to parseContent () ... NOP */ + } + else if (((c < 0x0020 && (c != '\t') && (c != '\r')) || c > 0xFFFD) + || ((c >= 0x007f) && (c <= 0x009f) && (c != 0x0085) + && xmlVersion == XML_11)) + { + error("illegal XML character U+" + Integer.toHexString(c)); + } + + // If we're in the DTD and in a context where PEs get expanded, + // do so ... 1/14/2000 errata identify those contexts. There + // are also spots in the internal subset where PE refs are fatal + // errors, hence yet another flag. + else if (c == '%' && expandPE) + { + if (peIsError) + { + error("PE reference within decl in internal subset."); + } + parsePEReference(); + return readCh(); + } + column++; + } + + return c; + } + + /** + * Push a single character back onto the current input stream. + * <p>This method usually pushes the character back onto + * the readBuffer. + * <p>I don't think that this would ever be called with + * readBufferPos = 0, because the methods always reads a character + * before unreading it, but just in case, I've added a boundary + * condition. + * @param c The character to push back. + * @see #readCh + * @see #unread (char[]) + * @see #readBuffer + */ + private void unread(char c) + throws SAXException + { + // Normal condition. + if (c == '\n') + { + line--; + column = -1; + } + if (readBufferPos > 0) + { + readBuffer[--readBufferPos] = c; + } + else + { + pushString(null, new Character(c).toString()); + } + } + + /** + * Push a char array back onto the current input stream. + * <p>NOTE: you must <em>never</em> push back characters that you + * haven't actually read: use pushString () instead. + * @see #readCh + * @see #unread (char) + * @see #readBuffer + * @see #pushString + */ + private void unread(char[] ch, int length) + throws SAXException + { + for (int i = 0; i < length; i++) + { + if (ch[i] == '\n') + { + line--; + column = -1; + } + } + if (length < readBufferPos) + { + readBufferPos -= length; + } + else + { + pushCharArray(null, ch, 0, length); + } + } + + /** + * Push, or skip, a new external input source. + * The source will be some kind of parsed entity, such as a PE + * (including the external DTD subset) or content for the body. + * + * @param url The java.net.URL object for the entity. + * @see SAXDriver#resolveEntity + * @see #pushString + * @see #sourceType + * @see #pushInput + * @see #detectEncoding + * @see #sourceType + * @see #readBuffer + */ + private void pushURL(boolean isPE, + String ename, + ExternalIdentifiers ids, + Reader reader, + InputStream stream, + String encoding, + boolean doResolve) + throws SAXException, IOException + { + boolean ignoreEncoding; + String systemId; + InputSource source; + + if (!isPE) + { + dataBufferFlush(); + } + + scratch.setPublicId(ids.publicId); + scratch.setSystemId(ids.systemId); + + // See if we should skip or substitute the entity. + // If we're not skipping, resolving reports startEntity() + // and updates the (handler's) stack of URIs. + if (doResolve) + { + // assert (stream == null && reader == null && encoding == null) + source = handler.resolveEntity(isPE, ename, scratch, ids.baseUri); + if (source == null) + { + handler.warn("skipping entity: " + ename); + handler.skippedEntity(ename); + if (isPE) + { + skippedPE = true; + } + return; + } + + // we might be using alternate IDs/encoding + systemId = source.getSystemId(); + // The following warning and setting systemId was deleted bcause + // the application has the option of not setting systemId + // provided that it has set the characte/byte stream. + /* + if (systemId == null) { + handler.warn ("missing system ID, using " + ids.systemId); + systemId = ids.systemId; + } + */ + } + else + { + // "[document]", or "[dtd]" via getExternalSubset() + scratch.setCharacterStream(reader); + scratch.setByteStream(stream); + scratch.setEncoding(encoding); + source = scratch; + systemId = ids.systemId; + if (handler.stringInterning) + { + handler.startExternalEntity(ename, systemId, + "[document]" == ename); + } + else + { + handler.startExternalEntity(ename, systemId, + "[document]".equals(ename)); + } + } + + // we may have been given I/O streams directly + if (source.getCharacterStream() != null) + { + if (source.getByteStream() != null) + error("InputSource has two streams!"); + reader = source.getCharacterStream(); + } + else if (source.getByteStream() != null) + { + encoding = source.getEncoding(); + if (encoding == null) + { + stream = source.getByteStream(); + } + else + { + try + { + reader = new InputStreamReader(source.getByteStream(), + encoding); + } + catch (IOException e) + { + stream = source.getByteStream(); + } + } + } + else if (systemId == null) + { + error("InputSource has no URI!"); + } + scratch.setCharacterStream(null); + scratch.setByteStream(null); + scratch.setEncoding(null); + + // Push the existing status. + pushInput(ename); + + // Create a new read buffer. + // (Note the four-character margin) + readBuffer = new char[READ_BUFFER_MAX + 4]; + readBufferPos = 0; + readBufferLength = 0; + readBufferOverflow = -1; + is = null; + line = 1; + column = 0; + currentByteCount = 0; + + // If there's an explicit character stream, just + // ignore encoding declarations. + if (reader != null) + { + sourceType = INPUT_READER; + this.reader = reader; + tryEncodingDecl(true); + return; + } + + // Else we handle the conversion, and need to ensure + // it's done right. + sourceType = INPUT_STREAM; + if (stream != null) + { + is = stream; + } + else + { + // We have to open our own stream to the URL. + URL url = new URL(systemId); + + externalEntity = url.openConnection(); + externalEntity.connect(); + is = externalEntity.getInputStream(); + } + + // If we get to here, there must be + // an InputStream available. + if (!is.markSupported()) + { + is = new BufferedInputStream(is); + } + + // Get any external encoding label. + if (encoding == null && externalEntity != null) + { + // External labels can be untrustworthy; filesystems in + // particular often have the wrong default for content + // that wasn't locally originated. Those we autodetect. + if (!"file".equals(externalEntity.getURL().getProtocol())) + { + int temp; + + // application/xml;charset=something;otherAttr=... + // ... with many variants on 'something' + encoding = externalEntity.getContentType(); + + // MHK code (fix for Saxon 5.5.1/007): + // protect against encoding==null + if (encoding == null) + { + temp = -1; + } + else + { + temp = encoding.indexOf("charset"); + } + + // RFC 2376 sez MIME text defaults to ASCII, but since the + // JDK will create a MIME type out of thin air, we always + // autodetect when there's no explicit charset attribute. + if (temp < 0) + { + encoding = null; // autodetect + } + else + { + // only this one attribute + if ((temp = encoding.indexOf(';')) > 0) + { + encoding = encoding.substring(0, temp); + } + + if ((temp = encoding.indexOf('=', temp + 7)) > 0) + { + encoding = encoding.substring(temp + 1); + + // attributes can have comment fields (RFC 822) + if ((temp = encoding.indexOf('(')) > 0) + { + encoding = encoding.substring(0, temp); + } + // ... and values may be quoted + if ((temp = encoding.indexOf('"')) > 0) + { + encoding = + encoding.substring(temp + 1, + encoding.indexOf('"', temp + 2)); + } + encoding = encoding.trim(); + } + else + { + handler.warn("ignoring illegal MIME attribute: " + + encoding); + encoding = null; + } + } + } + } + + // if we got an external encoding label, use it ... + if (encoding != null) + { + this.encoding = ENCODING_EXTERNAL; + setupDecoding(encoding); + ignoreEncoding = true; + + // ... else autodetect from first bytes. + } + else + { + detectEncoding(); + ignoreEncoding = false; + } + + // Read any XML or text declaration. + // If we autodetected, it may tell us the "real" encoding. + try + { + tryEncodingDecl(ignoreEncoding); + } + catch (UnsupportedEncodingException x) + { + encoding = x.getMessage(); + + // if we don't handle the declared encoding, + // try letting a JVM InputStreamReader do it + try + { + if (sourceType != INPUT_STREAM) + { + throw x; + } + + is.reset(); + readBufferPos = 0; + readBufferLength = 0; + readBufferOverflow = -1; + line = 1; + currentByteCount = column = 0; + + sourceType = INPUT_READER; + this.reader = new InputStreamReader(is, encoding); + is = null; + + tryEncodingDecl(true); + + } + catch (IOException e) + { + error("unsupported text encoding", + encoding, + null); + } + } + } + + /** + * Check for an encoding declaration. This is the second part of the + * XML encoding autodetection algorithm, relying on detectEncoding to + * get to the point that this part can read any encoding declaration + * in the document (using only US-ASCII characters). + * + * <p> Because this part starts to fill parser buffers with this data, + * it's tricky to setup a reader so that Java's built-in decoders can be + * used for the character encodings that aren't built in to this parser + * (such as EUC-JP, KOI8-R, Big5, etc). + * + * @return any encoding in the declaration, uppercased; or null + * @see detectEncoding + */ + private String tryEncodingDecl(boolean ignoreEncoding) + throws SAXException, IOException + { + // Read the XML/text declaration. + if (tryRead("<?xml")) + { + if (tryWhitespace()) + { + if (inputStack.size() > 0) + { + return parseTextDecl(ignoreEncoding); + } + else + { + return parseXMLDecl(ignoreEncoding); + } + } + else + { + // <?xml-stylesheet ...?> or similar + unread('l'); + unread('m'); + unread('x'); + unread('?'); + unread('<'); + } + } + return null; + } + + /** + * Attempt to detect the encoding of an entity. + * <p>The trick here (as suggested in the XML standard) is that + * any entity not in UTF-8, or in UCS-2 with a byte-order mark, + * <b>must</b> begin with an XML declaration or an encoding + * declaration; we simply have to look for "<?xml" in various + * encodings. + * <p>This method has no way to distinguish among 8-bit encodings. + * Instead, it sets up for UTF-8, then (possibly) revises its assumption + * later in setupDecoding (). Any ASCII-derived 8-bit encoding + * should work, but most will be rejected later by setupDecoding (). + * @see #tryEncoding (byte[], byte, byte, byte, byte) + * @see #tryEncoding (byte[], byte, byte) + * @see #setupDecoding + */ + private void detectEncoding() + throws SAXException, IOException + { + byte[] signature = new byte[4]; + + // Read the first four bytes for + // autodetection. + is.mark(4); + is.read(signature); + is.reset(); + + // + // FIRST: four byte encodings (who uses these?) + // + if (tryEncoding(signature, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x3c)) + { + // UCS-4 must begin with "<?xml" + // 0x00 0x00 0x00 0x3c: UCS-4, big-endian (1234) + // "UTF-32BE" + encoding = ENCODING_UCS_4_1234; + } + else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x00, + (byte) 0x00, (byte) 0x00)) + { + // 0x3c 0x00 0x00 0x00: UCS-4, little-endian (4321) + // "UTF-32LE" + encoding = ENCODING_UCS_4_4321; + } + else if (tryEncoding(signature, (byte) 0x00, (byte) 0x00, + (byte) 0x3c, (byte) 0x00)) + { + // 0x00 0x00 0x3c 0x00: UCS-4, unusual (2143) + encoding = ENCODING_UCS_4_2143; + } + else if (tryEncoding(signature, (byte) 0x00, (byte) 0x3c, + (byte) 0x00, (byte) 0x00)) + { + // 0x00 0x3c 0x00 0x00: UCS-4, unusual (3421) + encoding = ENCODING_UCS_4_3412; + + // 00 00 fe ff UCS_4_1234 (with BOM) + // ff fe 00 00 UCS_4_4321 (with BOM) + } + + // + // SECOND: two byte encodings + // note ... with 1/14/2000 errata the XML spec identifies some + // more "broken UTF-16" autodetection cases, with no XML decl, + // which we don't handle here (that's legal too). + // + else if (tryEncoding(signature, (byte) 0xfe, (byte) 0xff)) + { + // UCS-2 with a byte-order marker. (UTF-16) + // 0xfe 0xff: UCS-2, big-endian (12) + encoding = ENCODING_UCS_2_12; + is.read(); is.read(); + } + else if (tryEncoding(signature, (byte) 0xff, (byte) 0xfe)) + { + // UCS-2 with a byte-order marker. (UTF-16) + // 0xff 0xfe: UCS-2, little-endian (21) + encoding = ENCODING_UCS_2_21; + is.read(); is.read(); + } + else if (tryEncoding(signature, (byte) 0x00, (byte) 0x3c, + (byte) 0x00, (byte) 0x3f)) + { + // UTF-16BE (otherwise, malformed UTF-16) + // 0x00 0x3c 0x00 0x3f: UCS-2, big-endian, no byte-order mark + encoding = ENCODING_UCS_2_12; + error("no byte-order mark for UCS-2 entity"); + } + else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x00, + (byte) 0x3f, (byte) 0x00)) + { + // UTF-16LE (otherwise, malformed UTF-16) + // 0x3c 0x00 0x3f 0x00: UCS-2, little-endian, no byte-order mark + encoding = ENCODING_UCS_2_21; + error("no byte-order mark for UCS-2 entity"); + } + + // + // THIRD: ASCII-derived encodings, fixed and variable lengths + // + else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x3f, + (byte) 0x78, (byte) 0x6d)) + { + // ASCII derived + // 0x3c 0x3f 0x78 0x6d: UTF-8 or other 8-bit markup (read ENCODING) + encoding = ENCODING_UTF_8; + prefetchASCIIEncodingDecl(); + } + else if (signature[0] == (byte) 0xef + && signature[1] == (byte) 0xbb + && signature[2] == (byte) 0xbf) + { + // 0xef 0xbb 0xbf: UTF-8 BOM (not part of document text) + // this un-needed notion slipped into XML 2nd ed through a + // "non-normative" erratum; now required by MSFT and UDDI, + // and E22 made it normative. + encoding = ENCODING_UTF_8; + is.read(); is.read(); is.read(); + } + else + { + // 4c 6f a7 94 ... we don't understand EBCDIC flavors + // ... but we COULD at least kick in some fixed code page + + // (default) UTF-8 without encoding/XML declaration + encoding = ENCODING_UTF_8; + } + } + + /** + * Check for a four-byte signature. + * <p>Utility routine for detectEncoding (). + * <p>Always looks for some part of "<?XML" in a specific encoding. + * @param sig The first four bytes read. + * @param b1 The first byte of the signature + * @param b2 The second byte of the signature + * @param b3 The third byte of the signature + * @param b4 The fourth byte of the signature + * @see #detectEncoding + */ + private static boolean tryEncoding(byte[] sig, byte b1, byte b2, + byte b3, byte b4) + { + return (sig[0] == b1 && sig[1] == b2 + && sig[2] == b3 && sig[3] == b4); + } + + /** + * Check for a two-byte signature. + * <p>Looks for a UCS-2 byte-order mark. + * <p>Utility routine for detectEncoding (). + * @param sig The first four bytes read. + * @param b1 The first byte of the signature + * @param b2 The second byte of the signature + * @see #detectEncoding + */ + private static boolean tryEncoding(byte[] sig, byte b1, byte b2) + { + return ((sig[0] == b1) && (sig[1] == b2)); + } + + /** + * This method pushes a string back onto input. + * <p>It is useful either as the expansion of an internal entity, + * or for backtracking during the parse. + * <p>Call pushCharArray () to do the actual work. + * @param s The string to push back onto input. + * @see #pushCharArray + */ + private void pushString(String ename, String s) + throws SAXException + { + char[] ch = s.toCharArray(); + pushCharArray(ename, ch, 0, ch.length); + } + + /** + * Push a new internal input source. + * <p>This method is useful for expanding an internal entity, + * or for unreading a string of characters. It creates a new + * readBuffer containing the characters in the array, instead + * of characters converted from an input byte stream. + * @param ch The char array to push. + * @see #pushString + * @see #pushURL + * @see #readBuffer + * @see #sourceType + * @see #pushInput + */ + private void pushCharArray(String ename, char[] ch, int start, int length) + throws SAXException + { + // Push the existing status + pushInput(ename); + if (ename != null && doReport) + { + dataBufferFlush(); + handler.startInternalEntity(ename); + } + sourceType = INPUT_INTERNAL; + readBuffer = ch; + readBufferPos = start; + readBufferLength = length; + readBufferOverflow = -1; + } + + /** + * Save the current input source onto the stack. + * <p>This method saves all of the global variables associated with + * the current input source, so that they can be restored when a new + * input source has finished. It also tests for entity recursion. + * <p>The method saves the following global variables onto a stack + * using a fixed-length array: + * <ol> + * <li>sourceType + * <li>externalEntity + * <li>readBuffer + * <li>readBufferPos + * <li>readBufferLength + * <li>line + * <li>encoding + * </ol> + * @param ename The name of the entity (if any) causing the new input. + * @see #popInput + * @see #sourceType + * @see #externalEntity + * @see #readBuffer + * @see #readBufferPos + * @see #readBufferLength + * @see #line + * @see #encoding + */ + private void pushInput(String ename) + throws SAXException + { + // Check for entity recursion. + if (ename != null) + { + Iterator entities = entityStack.iterator(); + while (entities.hasNext()) + { + String e = (String) entities.next(); + if (e != null && e == ename) + { + error("recursive reference to entity", ename, null); + } + } + } + entityStack.addLast(ename); + + // Don't bother if there is no current input. + if (sourceType == INPUT_NONE) + { + return; + } + + // Set up a snapshot of the current + // input source. + Input input = new Input(); + + input.sourceType = sourceType; + input.externalEntity = externalEntity; + input.readBuffer = readBuffer; + input.readBufferPos = readBufferPos; + input.readBufferLength = readBufferLength; + input.line = line; + input.encoding = encoding; + input.readBufferOverflow = readBufferOverflow; + input.is = is; + input.currentByteCount = currentByteCount; + input.column = column; + input.reader = reader; + + // Push it onto the stack. + inputStack.addLast(input); + } + + /** + * Restore a previous input source. + * <p>This method restores all of the global variables associated with + * the current input source. + * @exception java.io.EOFException + * If there are no more entries on the input stack. + * @see #pushInput + * @see #sourceType + * @see #externalEntity + * @see #readBuffer + * @see #readBufferPos + * @see #readBufferLength + * @see #line + * @see #encoding + */ + private void popInput() + throws SAXException, IOException + { + String ename = (String) entityStack.removeLast(); + + if (ename != null && doReport) + { + dataBufferFlush(); + } + switch (sourceType) + { + case INPUT_STREAM: + handler.endExternalEntity(ename); + is.close(); + break; + case INPUT_READER: + handler.endExternalEntity(ename); + reader.close(); + break; + case INPUT_INTERNAL: + if (ename != null && doReport) + { + handler.endInternalEntity(ename); + } + break; + } + + // Throw an EOFException if there + // is nothing else to pop. + if (inputStack.isEmpty()) + { + throw new EOFException("no more input"); + } + + Input input = (Input) inputStack.removeLast(); + + sourceType = input.sourceType; + externalEntity = input.externalEntity; + readBuffer = input.readBuffer; + readBufferPos = input.readBufferPos; + readBufferLength = input.readBufferLength; + line = input.line; + encoding = input.encoding; + readBufferOverflow = input.readBufferOverflow; + is = input.is; + currentByteCount = input.currentByteCount; + column = input.column; + reader = input.reader; + } + + /** + * Return true if we can read the expected character. + * <p>Note that the character will be removed from the input stream + * on success, but will be put back on failure. Do not attempt to + * read the character again if the method succeeds. + * @param delim The character that should appear next. For a + * insensitive match, you must supply this in upper-case. + * @return true if the character was successfully read, or false if + * it was not. + * @see #tryRead (String) + */ + private boolean tryRead(char delim) + throws SAXException, IOException + { + char c; + + // Read the character + c = readCh(); + + // Test for a match, and push the character + // back if the match fails. + if (c == delim) + { + return true; + } + else + { + unread(c); + return false; + } + } + + /** + * Return true if we can read the expected string. + * <p>This is simply a convenience method. + * <p>Note that the string will be removed from the input stream + * on success, but will be put back on failure. Do not attempt to + * read the string again if the method succeeds. + * <p>This method will push back a character rather than an + * array whenever possible (probably the majority of cases). + * @param delim The string that should appear next. + * @return true if the string was successfully read, or false if + * it was not. + * @see #tryRead (char) + */ + private boolean tryRead(String delim) + throws SAXException, IOException + { + return tryRead(delim.toCharArray()); + } + + private boolean tryRead(char[] ch) + throws SAXException, IOException + { + char c; + + // Compare the input, character- + // by character. + + for (int i = 0; i < ch.length; i++) + { + c = readCh(); + if (c != ch[i]) + { + unread(c); + if (i != 0) + { + unread(ch, i); + } + return false; + } + } + return true; + } + + /** + * Return true if we can read some whitespace. + * <p>This is simply a convenience method. + * <p>This method will push back a character rather than an + * array whenever possible (probably the majority of cases). + * @return true if whitespace was found. + */ + private boolean tryWhitespace() + throws SAXException, IOException + { + char c; + c = readCh(); + if (isWhitespace(c)) + { + skipWhitespace(); + return true; + } + else + { + unread(c); + return false; + } + } + + /** + * Read all data until we find the specified string. + * This is useful for scanning CDATA sections and PIs. + * <p>This is inefficient right now, since it calls tryRead () + * for every character. + * @param delim The string delimiter + * @see #tryRead (String, boolean) + * @see #readCh + */ + private void parseUntil(String delim) + throws SAXException, IOException + { + parseUntil(delim.toCharArray()); + } + + private void parseUntil(char[] delim) + throws SAXException, IOException + { + char c; + int startLine = line; + + try + { + while (!tryRead(delim)) + { + c = readCh(); + dataBufferAppend(c); + } + } + catch (EOFException e) + { + error("end of input while looking for delimiter " + + "(started on line " + startLine + + ')', null, new String(delim)); + } + } + + ////////////////////////////////////////////////////////////////////// + // Low-level I/O. + ////////////////////////////////////////////////////////////////////// + + /** + * Prefetch US-ASCII XML/text decl from input stream into read buffer. + * Doesn't buffer more than absolutely needed, so that when an encoding + * decl says we need to create an InputStreamReader, we can discard our + * buffer and reset(). Caller knows the first chars of the decl exist + * in the input stream. + */ + private void prefetchASCIIEncodingDecl() + throws SAXException, IOException + { + int ch; + readBufferPos = readBufferLength = 0; + + is.mark(readBuffer.length); + while (true) + { + ch = is.read(); + readBuffer[readBufferLength++] = (char) ch; + switch (ch) + { + case (int) '>': + return; + case -1: + error("file ends before end of XML or encoding declaration.", + null, "?>"); + } + if (readBuffer.length == readBufferLength) + { + error("unfinished XML or encoding declaration"); + } + } + } + + /** + * Read a chunk of data from an external input source. + * <p>This is simply a front-end that fills the rawReadBuffer + * with bytes, then calls the appropriate encoding handler. + * @see #encoding + * @see #rawReadBuffer + * @see #readBuffer + * @see #filterCR + * @see #copyUtf8ReadBuffer + * @see #copyIso8859_1ReadBuffer + * @see #copyUcs_2ReadBuffer + * @see #copyUcs_4ReadBuffer + */ + private void readDataChunk() + throws SAXException, IOException + { + int count; + + // See if we have any overflow (filterCR sets for CR at end) + if (readBufferOverflow > -1) + { + readBuffer[0] = (char) readBufferOverflow; + readBufferOverflow = -1; + readBufferPos = 1; + sawCR = true; + } + else + { + readBufferPos = 0; + sawCR = false; + } + + // input from a character stream. + if (sourceType == INPUT_READER) + { + count = reader.read(readBuffer, + readBufferPos, READ_BUFFER_MAX - readBufferPos); + if (count < 0) + { + readBufferLength = readBufferPos; + } + else + { + readBufferLength = readBufferPos + count; + } + if (readBufferLength > 0) + { + filterCR(count >= 0); + } + sawCR = false; + return; + } + + // Read as many bytes as possible into the raw buffer. + count = is.read(rawReadBuffer, 0, READ_BUFFER_MAX); + + // Dispatch to an encoding-specific reader method to populate + // the readBuffer. In most parser speed profiles, these routines + // show up at the top of the CPU usage chart. + if (count > 0) + { + switch (encoding) + { + // one byte builtins + case ENCODING_ASCII: + copyIso8859_1ReadBuffer(count, (char) 0x0080); + break; + case ENCODING_UTF_8: + copyUtf8ReadBuffer(count); + break; + case ENCODING_ISO_8859_1: + copyIso8859_1ReadBuffer(count, (char) 0); + break; + + // two byte builtins + case ENCODING_UCS_2_12: + copyUcs2ReadBuffer(count, 8, 0); + break; + case ENCODING_UCS_2_21: + copyUcs2ReadBuffer(count, 0, 8); + break; + + // four byte builtins + case ENCODING_UCS_4_1234: + copyUcs4ReadBuffer(count, 24, 16, 8, 0); + break; + case ENCODING_UCS_4_4321: + copyUcs4ReadBuffer(count, 0, 8, 16, 24); + break; + case ENCODING_UCS_4_2143: + copyUcs4ReadBuffer(count, 16, 24, 0, 8); + break; + case ENCODING_UCS_4_3412: + copyUcs4ReadBuffer(count, 8, 0, 24, 16); + break; + } + } + else + { + readBufferLength = readBufferPos; + } + + readBufferPos = 0; + + // Filter out all carriage returns if we've seen any + // (including any saved from a previous read) + if (sawCR) + { + filterCR(count >= 0); + sawCR = false; + + // must actively report EOF, lest some CRs get lost. + if (readBufferLength == 0 && count >= 0) + { + readDataChunk(); + } + } + + if (count > 0) + { + currentByteCount += count; + } + } + + /** + * Filter carriage returns in the read buffer. + * CRLF becomes LF; CR becomes LF. + * @param moreData true iff more data might come from the same source + * @see #readDataChunk + * @see #readBuffer + * @see #readBufferOverflow + */ + private void filterCR(boolean moreData) + { + int i, j; + + readBufferOverflow = -1; + +loop: + for (i = j = readBufferPos; j < readBufferLength; i++, j++) + { + switch (readBuffer[j]) + { + case '\r': + if (j == readBufferLength - 1) + { + if (moreData) + { + readBufferOverflow = '\r'; + readBufferLength--; + } + else // CR at end of buffer + { + readBuffer[i++] = '\n'; + } + break loop; + } + else if (readBuffer[j + 1] == '\n') + { + j++; + } + readBuffer[i] = '\n'; + break; + + case '\n': + default: + readBuffer[i] = readBuffer[j]; + break; + } + } + readBufferLength = i; + } + + /** + * Convert a buffer of UTF-8-encoded bytes into UTF-16 characters. + * <p>When readDataChunk () calls this method, the raw bytes are in + * rawReadBuffer, and the final characters will appear in + * readBuffer. + * <p>Note that as of Unicode 3.1, good practice became a requirement, + * so that each Unicode character has exactly one UTF-8 representation. + * @param count The number of bytes to convert. + * @see #readDataChunk + * @see #rawReadBuffer + * @see #readBuffer + * @see #getNextUtf8Byte + */ + private void copyUtf8ReadBuffer(int count) + throws SAXException, IOException + { + int i = 0; + int j = readBufferPos; + int b1; + char c = 0; + + /* + // check once, so the runtime won't (if it's smart enough) + if (count < 0 || count > rawReadBuffer.length) + throw new ArrayIndexOutOfBoundsException (Integer.toString (count)); + */ + + while (i < count) + { + b1 = rawReadBuffer[i++]; + + // Determine whether we are dealing + // with a one-, two-, three-, or four- + // byte sequence. + if (b1 < 0) + { + if ((b1 & 0xe0) == 0xc0) + { + // 2-byte sequence: 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx + c = (char) (((b1 & 0x1f) << 6) + | getNextUtf8Byte(i++, count)); + if (c < 0x0080) + { + encodingError("Illegal two byte UTF-8 sequence", + c, 0); + } + + //Sec 2.11 + // [1] the two-character sequence #xD #xA + // [2] the two-character sequence #xD #x85 + if ((c == 0x0085 || c == 0x000a) && sawCR) + { + continue; + } + + // Sec 2.11 + // [3] the single character #x85 + + if (c == 0x0085 && xmlVersion == XML_11) + { + readBuffer[j++] = '\r'; + } + } + else if ((b1 & 0xf0) == 0xe0) + { + // 3-byte sequence: + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx + // most CJKV characters + c = (char) (((b1 & 0x0f) << 12) | + (getNextUtf8Byte(i++, count) << 6) | + getNextUtf8Byte(i++, count)); + //sec 2.11 + //[4] the single character #x2028 + if (c == 0x2028 && xmlVersion == XML_11) + { + readBuffer[j++] = '\r'; + sawCR = true; + continue; + } + if (c < 0x0800 || (c >= 0xd800 && c <= 0xdfff)) + { + encodingError("Illegal three byte UTF-8 sequence", + c, 0); + } + } + else if ((b1 & 0xf8) == 0xf0) + { + // 4-byte sequence: 11101110wwwwzzzzyy + 110111yyyyxxxxxx + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx + // (uuuuu = wwww + 1) + // "Surrogate Pairs" ... from the "Astral Planes" + // Unicode 3.1 assigned the first characters there + int iso646 = b1 & 07; + iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count); + iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count); + iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count); + + if (iso646 <= 0xffff) + { + encodingError("Illegal four byte UTF-8 sequence", + iso646, 0); + } + else + { + if (iso646 > 0x0010ffff) + { + encodingError("UTF-8 value out of range for Unicode", + iso646, 0); + } + iso646 -= 0x010000; + readBuffer[j++] = (char) (0xd800 | (iso646 >> 10)); + readBuffer[j++] = (char) (0xdc00 | (iso646 & 0x03ff)); + continue; + } + } + else + { + // The five and six byte encodings aren't supported; + // they exceed the Unicode (and XML) range. + encodingError("unsupported five or six byte UTF-8 sequence", + 0xff & b1, i); + // NOTREACHED + c = 0; + } + } + else + { + // 1-byte sequence: 000000000xxxxxxx = 0xxxxxxx + // (US-ASCII character, "common" case, one branch to here) + c = (char) b1; + } + readBuffer[j++] = c; + if (c == '\r') + { + sawCR = true; + } + } + // How many characters have we read? + readBufferLength = j; + } + + /** + * Return the next byte value in a UTF-8 sequence. + * If it is not possible to get a byte from the current + * entity, throw an exception. + * @param pos The current position in the rawReadBuffer. + * @param count The number of bytes in the rawReadBuffer + * @return The significant six bits of a non-initial byte in + * a UTF-8 sequence. + * @exception EOFException If the sequence is incomplete. + */ + private int getNextUtf8Byte(int pos, int count) + throws SAXException, IOException + { + int val; + + // Take a character from the buffer + // or from the actual input stream. + if (pos < count) + { + val = rawReadBuffer[pos]; + } + else + { + val = is.read(); + if (val == -1) + { + encodingError("unfinished multi-byte UTF-8 sequence at EOF", + -1, pos); + } + } + + // Check for the correct bits at the start. + if ((val & 0xc0) != 0x80) + { + encodingError("bad continuation of multi-byte UTF-8 sequence", + val, pos + 1); + } + + // Return the significant bits. + return (val & 0x3f); + } + + /** + * Convert a buffer of US-ASCII or ISO-8859-1-encoded bytes into + * UTF-16 characters. + * + * <p>When readDataChunk () calls this method, the raw bytes are in + * rawReadBuffer, and the final characters will appear in + * readBuffer. + * + * @param count The number of bytes to convert. + * @param mask For ASCII conversion, 0x7f; else, 0xff. + * @see #readDataChunk + * @see #rawReadBuffer + * @see #readBuffer + */ + private void copyIso8859_1ReadBuffer(int count, char mask) + throws IOException + { + int i, j; + for (i = 0, j = readBufferPos; i < count; i++, j++) + { + char c = (char) (rawReadBuffer[i] & 0xff); + if ((c & mask) != 0) + { + throw new CharConversionException("non-ASCII character U+" + + Integer.toHexString(c)); + } + if (c == 0x0085 && xmlVersion == XML_11) + { + c = '\r'; + } + readBuffer[j] = c; + if (c == '\r') + { + sawCR = true; + } + } + readBufferLength = j; + } + + /** + * Convert a buffer of UCS-2-encoded bytes into UTF-16 characters + * (as used in Java string manipulation). + * + * <p>When readDataChunk () calls this method, the raw bytes are in + * rawReadBuffer, and the final characters will appear in + * readBuffer. + * @param count The number of bytes to convert. + * @param shift1 The number of bits to shift byte 1. + * @param shift2 The number of bits to shift byte 2 + * @see #readDataChunk + * @see #rawReadBuffer + * @see #readBuffer + */ + private void copyUcs2ReadBuffer(int count, int shift1, int shift2) + throws SAXException + { + int j = readBufferPos; + + if (count > 0 && (count % 2) != 0) + { + encodingError("odd number of bytes in UCS-2 encoding", -1, count); + } + // The loops are faster with less internal brancing; hence two + if (shift1 == 0) + { // "UTF-16-LE" + for (int i = 0; i < count; i += 2) + { + char c = (char) (rawReadBuffer[i + 1] << 8); + c |= 0xff & rawReadBuffer[i]; + readBuffer[j++] = c; + if (c == '\r') + { + sawCR = true; + } + } + } + else + { // "UTF-16-BE" + for (int i = 0; i < count; i += 2) + { + char c = (char) (rawReadBuffer[i] << 8); + c |= 0xff & rawReadBuffer[i + 1]; + readBuffer[j++] = c; + if (c == '\r') + { + sawCR = true; + } + } + } + readBufferLength = j; + } + + /** + * Convert a buffer of UCS-4-encoded bytes into UTF-16 characters. + * + * <p>When readDataChunk () calls this method, the raw bytes are in + * rawReadBuffer, and the final characters will appear in + * readBuffer. + * <p>Java has Unicode chars, and this routine uses surrogate pairs + * for ISO-10646 values between 0x00010000 and 0x000fffff. An + * exception is thrown if the ISO-10646 character has no Unicode + * representation. + * + * @param count The number of bytes to convert. + * @param shift1 The number of bits to shift byte 1. + * @param shift2 The number of bits to shift byte 2 + * @param shift3 The number of bits to shift byte 2 + * @param shift4 The number of bits to shift byte 2 + * @see #readDataChunk + * @see #rawReadBuffer + * @see #readBuffer + */ + private void copyUcs4ReadBuffer(int count, int shift1, int shift2, + int shift3, int shift4) + throws SAXException + { + int j = readBufferPos; + + if (count > 0 && (count % 4) != 0) + { + encodingError("number of bytes in UCS-4 encoding " + + "not divisible by 4", + -1, count); + } + for (int i = 0; i < count; i += 4) + { + int value = (((rawReadBuffer [i] & 0xff) << shift1) | + ((rawReadBuffer [i + 1] & 0xff) << shift2) | + ((rawReadBuffer [i + 2] & 0xff) << shift3) | + ((rawReadBuffer [i + 3] & 0xff) << shift4)); + if (value < 0x0000ffff) + { + readBuffer [j++] = (char) value; + if (value == (int) '\r') + { + sawCR = true; + } + } + else if (value < 0x0010ffff) + { + value -= 0x010000; + readBuffer[j++] = (char) (0xd8 | ((value >> 10) & 0x03ff)); + readBuffer[j++] = (char) (0xdc | (value & 0x03ff)); + } + else + { + encodingError("UCS-4 value out of range for Unicode", + value, i); + } + } + readBufferLength = j; + } + + /** + * Report a character encoding error. + */ + private void encodingError(String message, int value, int offset) + throws SAXException + { + if (value != -1) + { + message = message + " (character code: 0x" + + Integer.toHexString(value) + ')'; + error(message); + } + } + + ////////////////////////////////////////////////////////////////////// + // Local Variables. + ////////////////////////////////////////////////////////////////////// + + /** + * Re-initialize the variables for each parse. + */ + private void initializeVariables() + { + // First line + line = 1; + column = 0; + + // Set up the buffers for data and names + dataBufferPos = 0; + dataBuffer = new char[DATA_BUFFER_INITIAL]; + nameBufferPos = 0; + nameBuffer = new char[NAME_BUFFER_INITIAL]; + + // Set up the DTD hash tables + elementInfo = new HashMap(); + entityInfo = new HashMap(); + notationInfo = new HashMap(); + skippedPE = false; + + // Set up the variables for the current + // element context. + currentElement = null; + currentElementContent = CONTENT_UNDECLARED; + + // Set up the input variables + sourceType = INPUT_NONE; + inputStack = new LinkedList(); + entityStack = new LinkedList(); + externalEntity = null; + tagAttributePos = 0; + tagAttributes = new String[100]; + rawReadBuffer = new byte[READ_BUFFER_MAX]; + readBufferOverflow = -1; + + scratch = new InputSource(); + + inLiteral = false; + expandPE = false; + peIsError = false; + + doReport = false; + + inCDATA = false; + + symbolTable = new Object[SYMBOL_TABLE_LENGTH][]; + } + + static class ExternalIdentifiers + { + + String publicId; + String systemId; + String baseUri; + + ExternalIdentifiers() + { + } + + ExternalIdentifiers(String publicId, String systemId, String baseUri) + { + this.publicId = publicId; + this.systemId = systemId; + this.baseUri = baseUri; + } + + } + + static class EntityInfo + { + + int type; + ExternalIdentifiers ids; + String value; + String notationName; + + } + + static class AttributeDecl + { + + String type; + String value; + int valueType; + String enumeration; + String defaultValue; + + } + + static class ElementDecl + { + + int contentType; + String contentModel; + HashMap attributes; + + } + + static class Input + { + + int sourceType; + URLConnection externalEntity; + char[] readBuffer; + int readBufferPos; + int readBufferLength; + int line; + int encoding; + int readBufferOverflow; + InputStream is; + int currentByteCount; + int column; + Reader reader; + + } + +} + diff --git a/libjava/classpath/gnu/xml/aelfred2/XmlReader.java b/libjava/classpath/gnu/xml/aelfred2/XmlReader.java new file mode 100644 index 00000000000..a3bd03a8644 --- /dev/null +++ b/libjava/classpath/gnu/xml/aelfred2/XmlReader.java @@ -0,0 +1,374 @@ +/* XmlReader.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.aelfred2; + +import java.io.IOException; +import java.util.Locale; + +import org.xml.sax.*; +import org.xml.sax.ext.*; + +import gnu.xml.pipeline.EventFilter; +import gnu.xml.pipeline.ValidationConsumer; + + +/** + * This SAX2 parser optionally layers a validator over the Ælfred2 + * SAX2 parser. While this will not evaluate every XML validity constraint, + * it does support all the validity constraints that are of any real utility + * outside the strict SGML-compatible world. See the documentation for the + * SAXDriver class for information about the SAX2 features and properties + * that are supported, and documentation for the ValidationConsumer for + * information about what validity constraints may not be supported. + * (Ælfred2 tests some of those, even in non-validating mode, to + * achieve better conformance.) + * + * <p> Note that due to its internal construction, you can't change most + * handlers until parse() returns. This diverges slightly from SAX, which + * expects later binding to be supported. Early binding involves less + * runtime overhead, which is an issue for event pipelines as used inside + * this parser. Rather than relying on the parser to handle late binding + * to your own handlers, do it yourself. + * + * @see SAXDriver + * @see gnu.xml.pipeline.ValidationConsumer + * + * @author David Brownell + */ +public final class XmlReader + implements XMLReader +{ + + static class FatalErrorHandler + extends DefaultHandler2 + { + + public void error(SAXParseException e) + throws SAXException + { + throw e; + } + + } + + private SAXDriver aelfred2 = new SAXDriver(); + private EventFilter filter = new EventFilter(); + private boolean isValidating; + private boolean active; + + /** + * Constructs a SAX Parser. + */ + public XmlReader() + { + } + + /** + * Constructs a SAX Parser, optionally treating validity errors + * as if they were fatal errors. + */ + public XmlReader(boolean invalidIsFatal) + { + if (invalidIsFatal) + { + setErrorHandler(new FatalErrorHandler()); + } + } + + /** + * <b>SAX2</b>: Returns the object used to report the logical + * content of an XML document. + */ + public ContentHandler getContentHandler() + { + return filter.getContentHandler(); + } + + /** + * <b>SAX2</b>: Assigns the object used to report the logical + * content of an XML document. + * @exception IllegalStateException if called mid-parse + */ + public void setContentHandler(ContentHandler handler) + { + if (active) + { + throw new IllegalStateException("already parsing"); + } + filter.setContentHandler(handler); + } + + /** + * <b>SAX2</b>: Returns the object used to process declarations related + * to notations and unparsed entities. + */ + public DTDHandler getDTDHandler() + { + return filter.getDTDHandler(); + } + + /** + * <b>SAX1</b> Assigns DTD handler + * @exception IllegalStateException if called mid-parse + */ + public void setDTDHandler(DTDHandler handler) + { + if (active) + { + throw new IllegalStateException("already parsing"); + } + filter.setDTDHandler(handler); + } + + /** + * <b>SAX2</b>: Returns the object used when resolving external + * entities during parsing (both general and parameter entities). + */ + public EntityResolver getEntityResolver() + { + return aelfred2.getEntityResolver(); + } + + /** + * <b>SAX1</b> Assigns parser's entity resolver + */ + public void setEntityResolver(EntityResolver handler) + { + aelfred2.setEntityResolver(handler); + } + + /** + * <b>SAX2</b>: Returns the object used to receive callbacks for XML + * errors of all levels (fatal, nonfatal, warning); this is never null; + */ + public ErrorHandler getErrorHandler() + { + return aelfred2.getErrorHandler(); + } + + /** + * <b>SAX1</b> Assigns error handler + * @exception IllegalStateException if called mid-parse + */ + public void setErrorHandler(ErrorHandler handler) + { + if (active) + { + throw new IllegalStateException("already parsing"); + } + aelfred2.setErrorHandler(handler); + } + + /** + * <b>SAX2</b>: Assigns the specified property. + * @exception IllegalStateException if called mid-parse + */ + public void setProperty(String propertyId, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + if (active) + { + throw new IllegalStateException("already parsing"); + } + if (getProperty(propertyId) != value) + { + filter.setProperty(propertyId, value); + } + } + + /** + * <b>SAX2</b>: Returns the specified property. + */ + public Object getProperty(String propertyId) + throws SAXNotRecognizedException + { + if ((SAXDriver.PROPERTY + "declaration-handler").equals(propertyId) + || (SAXDriver.PROPERTY + "lexical-handler").equals(propertyId)) + { + return filter.getProperty(propertyId); + } + throw new SAXNotRecognizedException(propertyId); + } + + private void forceValidating() + throws SAXNotRecognizedException, SAXNotSupportedException + { + aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes", + true); + aelfred2.setFeature(SAXDriver.FEATURE + "external-general-entities", + true); + aelfred2.setFeature(SAXDriver.FEATURE + "external-parameter-entities", + true); + } + + /** + * <b>SAX2</b>: Sets the state of features supported in this parser. + * Note that this parser requires reporting of namespace prefixes when + * validating. + */ + public void setFeature(String featureId, boolean state) + throws SAXNotRecognizedException, SAXNotSupportedException + { + boolean value = getFeature(featureId); + + if (state == value) + { + return; + } + + if ((SAXDriver.FEATURE + "validation").equals(featureId)) + { + if (active) + { + throw new SAXNotSupportedException("already parsing"); + } + if (state) + { + forceValidating(); + } + isValidating = state; + } + else + { + aelfred2.setFeature(featureId, state); + } + } + + /** + * <b>SAX2</b>: Tells whether this parser supports the specified feature. + * At this time, this directly parallels the underlying SAXDriver, + * except that validation is optionally supported. + * + * @see SAXDriver + */ + public boolean getFeature(String featureId) + throws SAXNotRecognizedException, SAXNotSupportedException + { + if ((SAXDriver.FEATURE + "validation").equals(featureId)) + { + return isValidating; + } + + return aelfred2.getFeature(featureId); + } + + /** + * <b>SAX1</b>: Sets the locale used for diagnostics; currently, + * only locales using the English language are supported. + * @param locale The locale for which diagnostics will be generated + */ + public void setLocale(Locale locale) + throws SAXException + { + aelfred2.setLocale(locale); + } + + /** + * <b>SAX1</b>: Preferred API to parse an XML document, using a + * system identifier (URI). + */ + public void parse(String systemId) + throws SAXException, IOException + { + parse(new InputSource(systemId)); + } + + /** + * <b>SAX1</b>: Underlying API to parse an XML document, used + * directly when no URI is available. When this is invoked, + * and the parser is set to validate, some features will be + * automatically reset to appropriate values: for reporting + * namespace prefixes, and incorporating external entities. + * + * @param source The XML input source. + * + * @exception IllegalStateException if called mid-parse + * @exception SAXException The handlers may throw any SAXException, + * and the parser normally throws SAXParseException objects. + * @exception IOException IOExceptions are normally through through + * the parser if there are problems reading the source document. + */ + public void parse(InputSource source) + throws SAXException, IOException + { + EventFilter next; + boolean nsdecls; + + synchronized (aelfred2) + { + if (active) + { + throw new IllegalStateException("already parsing"); + } + active = true; + } + + // set up the output pipeline + if (isValidating) + { + forceValidating(); + next = new ValidationConsumer(filter); + } + else + { + next = filter; + } + + // connect pipeline and error handler + // don't let _this_ call to bind() affect xmlns* attributes + nsdecls = aelfred2.getFeature(SAXDriver.FEATURE + "namespace-prefixes"); + EventFilter.bind(aelfred2, next); + if (!nsdecls) + { + aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes", + false); + } + + // parse, clean up + try + { + aelfred2.parse(source); + } + finally + { + active = false; + } + } + +} + diff --git a/libjava/classpath/gnu/xml/aelfred2/package.html b/libjava/classpath/gnu/xml/aelfred2/package.html new file mode 100644 index 00000000000..e2042584494 --- /dev/null +++ b/libjava/classpath/gnu/xml/aelfred2/package.html @@ -0,0 +1,506 @@ +<!DOCTYPE html PUBLIC + '-//W3C//DTD XHTML 1.0 Transitional//EN' + 'http://www.w3.org/TR/xhtml1/DTD/transitional.dtd'> + +<html><head> + <title>package overview</title> +<!-- +/* + * Copyright (C) 1999,2000,2001 The Free Software Foundation, Inc. + */ +--> +</head><body> + +<p> This package contains Ælfred2, which includes an +enhanced SAX2-compatible version of the Ælfred +non-validating XML parser, a modular (and hence optional) +DTD validating parser, and modular (and hence optional) +JAXP glue to those. +Use these like any other SAX2 parsers. </p> + +<ul> + <li><a href="#about">About Ælfred</a><ul> + <li><a href="#principles">Design Principles</a></li> + <li><a href="#name">About the Name Ælfred</a></li> + <li><a href="#encodings">Character Encodings</a></li> + <li><a href="#violations">Known Conformance Violations</a></li> + <li><a href="#copyright">Licensing</a></li> + </ul></li> + + <li><a href="#changes">Changes Since the Last Microstar Release</a><ul> + <li><a href="#sax2">SAX2 Support</a></li> + <li><a href="#validation">Validation</a></li> + <li><a href="#smaller">You Want Smaller?</a></li> + <li><a href="#bugfixes">Bugs Fixed</a></li> + </ul></li> + +</ul> + +<h2><a name="about">About Ælfred</a></h2> + +<p>Ælfred is a XML parser written in the java programming language. + +<h3><a name="principles">Design Principles</a></h3> + +<p>In most Java applets and applications, XML should not be the central +feature; instead, XML is the means to another end, such as loading +configuration information, reading meta-data, or parsing transactions.</p> + +<p> When an XML parser is only a single component of a much larger +program, it cannot be large, slow, or resource-intensive. With Java +applets, in particular, code size is a significant issue. The standard +modem is still not operating at 56 Kbaud, or sometimes even with data +compression. Assuming an uncompressed 28.8 Kbaud modem, only about +3 KBytes can be downloaded in one second; compression often doubles +that speed, but a V.90 modem may not provide another doubling. When +used with embedded processors, similar size concerns apply. </p> + +<p> Ælfred is designed for easy and efficient use over the Internet, +based on the following principles: </p> <ol> + +<li> Ælfred must be as small as possible, so that it doesn't add too + much to an applet's download time. </li> + +<li> Ælfred must use as few class files as possible, to minimize the + number of HTTP connections necessary. (The use of JAR files has made this + be less of a concern.) </li> + +<li> Ælfred must be compatible with most or all Java implementations + and platforms. (Write once, run anywhere.) </li> + +<li> Ælfred must use as little memory as possible, so that it does + not take away resources from the rest of your program. (It doesn't force + you to use DOM or a similar costly data structure API.)</li> + +<li> Ælfred must run as fast as possible, so that it does not slow down + the rest of your program. </li> + +<li> Ælfred must produce correct output for well-formed and valid + documents, but need not reject every document that is not valid or + not well-formed. (In Ælfred2, correctness was a bigger concern + than in the original version; and a validation option is available.) </li> + +<li> Ælfred must provide full internationalization from the first + release. (Ælfred2 now automatically handles all encodings + supported by the underlying JVM; previous versions handled only + UTF-8, UTF_16, ASCII, and ISO-8859-1.)</li> + +</ol> + +<p>As you can see from this list, Ælfred is designed for production +use, but neither validation nor perfect conformance was a requirement. +Good validating parsers exist, including one in this package, +and you should use them as appropriate. (See conformance reviews +available at <a href="http://www.xml.com/">http://www.xml.com</a>) +</p> + +<p> One of the main goals of Ælfred2 was to significantly improve +conformance, while not significantly affecting the other goals stated above. +Since the only use of this parser is with SAX, some classes could be +removed, and so the overall size of Ælfred was actually reduced. +Subsequent performance work produced a notable speedup (over twenty +percent on larger files). That is, the tradeoffs between speed, size, and +conformance were re-targeted towards conformance and support of newer APIs +(SAX2), with a a positive performance impact. </p> + +<p> The role anticipated for this version of Ælfred is as a +lightweight Free Software SAX parser that can be used in essentially every +Java program where the handful of conformance violations (noted below) +are acceptable. +That certainly includes applets, and +nowadays one must also mention embedded systems as being even more +size-critical. +At this writing, all parsers that are more conformant are +significantly larger, even when counting the optional +validation support in this version of Ælfred. </p> + + +<h3><a name="name">About the Name <em>Ælfred</em></a></h3> + +<p>Ælfred the Great (AElfred in ASCII) was King of Wessex, and +some say of King of England, at the time of his death in 899 AD. +Ælfred introduced a wide-spread literacy program in the hope that +his people would learn to read English, at least, if Latin was too +difficult for them. This Ælfred hopes to bring another sort of +literacy to Java, using XML, at least, if full SGML is too difficult.</p> + +<p>The initial Æ ligature ("AE)" is also a reminder that XML is +not limited to ASCII.</p> + + +<h3><a name="encodings">Character Encodings</a></h3> + +<p> The Ælfred parser currently builds in support for a handful +of input encodings. Of course these include UTF-8 and UTF-16, which +all XML parsers are required to support:</p> <ul> + + <li> UTF-8 ... the standard eight bit encoding, used unless + you provide an encoding declaration or a MIME charset tag.</li> + + <li> US-ASCII ... an extremely common seven bit encoding, + which happens to be a subset of UTF-8 and ISO-8859-1 as well + as many other encodings. XHTML web pages using US-ASCII + (without an encoding declaration) are probably more + widely interoperable than those in any other encoding. </li> + + <li> ISO-8859-1 ... includes accented characters used in + much of western Europe (but excluding the Euro currency + symbol).</li> + + <li> UTF-16 ... with several variants, this encodes each + sixteen bit Unicode character in sixteen bits of output. + Variants include UTF-16BE (big endian, no byte order mark), + UTF-16LE (little endian, no byte order mark), and + ISO-10646-UCS-2 (an older and less used encoding, using a + version of Unicode without surrogate pairs). This is + essentially the native encoding used by Java. </li> + + <li> ISO-10646-UCS-4 ... a seldom-used four byte encoding, + also known as UTF-32BE. Four byte order variants are supported, + including one known as UTF-32LE. Some operating systems + standardized on UCS-4 despite its significant size penalty, + in anticipation that Unicode (even with surrogate pairs) + would eventually become limiting. UCS-4 permits encoding + of non-Unicode characters, which Java can't represent (and + XML doesn't allow). + </li> + + </ul> + +<p> If you use any encoding other than UTF-8 or UTF-16 you should +make sure to label your data appropriately: </p> + +<blockquote> +<?xml version="1.0" encoding="<b>ISO-8859-15</b>"?> +</blockquote> + +<p> Encodings accessed through <code>java.io.InputStreamReader</code> +are now fully supported for both external labels (such as MIME types) +and internal types (as shown above). +There is one limitation in the support for internal labels: +the encodings must be derived from the US-ASCII encoding, +the EBCDIC family of encodings is not recognized. +Note that Java defines its +own encoding names, which don't always correspond to the standard +Internet encoding names defined by the IETF/IANA, and that Java +may even <em>require</em> use of nonstandard encoding names. +Please report +such problems; some of them can be worked around in this parser, +and many can be worked around by using external labels. +</p> + +<p>Note that if you are using the Euro symbol with an fixed length +eight bit encoding, you should probably be using the encoding label +<em>iso-8859-15</em> or, with a Microsoft OS, <em>cp-1252</em>. +Of course, UTF-8 and UTF-16 handle the Euro symbol directly. +</p> + + +<h3><a name="violations">Known Conformance Violations</a></h3> + +<p>Known conformance issues should be of negligible importance for +most applications, and include: </p><ul> + + <li> Rather than following the voluminous "Appendix B" rules about + what characters may appear in names (and name tokens), the Unicode + rules embedded in <em>java.lang.Character</em> are used. + This means mostly that some names are inappropriately accepted, + though a few are inappropriately rejected. (It's much simpler + to avoid that much special case code. Recent OASIS/NIST test + cases may have these rules be realistically testable.) </li> + + <li> Text containing "]]>" is not rejected unless it fully resides + in an internal buffer ... which is, thankfully, the typical case. This + text is illegal, but sometimes appears in illegal attempts to + nest CDATA sections. (Not catching that boundary condition + substantially simplifies parsing text.) </li> + + <li> Surrogate characters that aren't correctly paired are ignored + rather than rejected, unless they were encoded using UTF-8. (This + simplifies parsing text.) Unicode 3.1 assigned the first characters + to those character codes, in early 2001, so few documents (or tools) + use such characters in any case. </li> + + <li> Declarations following references to an undefined parameter + entity reference are not ignored. (Not maintaining and using state + about this validity error simplifies declaration handling; few + XML parsers address this constraint in any case.) </li> + + <li> Well formedness constraints for general entity references + are not enforced. (The code to handle the "content" production + is merged with the element parsing code, making it hard to reuse + for this additional situation.) </li> + +</ul> + +<p> When tested against the July 12, 1999 version of the OASIS +XML Conformance test suite, an earlier version passed 1057 of 1067 tests. +That contrasts with the original version, which passed 867. The +current parser is top-ranked in terms of conformance, as is its +validating sibling (which has some additional conformance violations +imposed on it by SAX2 API deficiencies as well as some of the more +curious SGML layering artifacts found in the XML specification). </p> + +<p> The XML 1.0 specification itself was not without problems, +and after some delays the W3C has come out with a revised +"second edition" specification. While that doesn't resolve all +the problems identified the XML specification, many of the most +egregious problems have been resolved. (You still need to drink +magic Kool-Aid before some DTD-related issues make sense.) +To the extent possible, this parser conforms to that second +edition specification, and does well against corrected versions +of the OASIS/NIST XML conformance test cases. See <a href= +"http://xmlconf.sourceforge.net">http://xmlconf.sourceforge.net</a> +for more information about SAX2/XML conformance testing. </p> + + +<h3><a name="copyright">Copyright and distribution terms</a></h3> + +<p> +The software in this package is distributed under the GNU General Public +License (with a special exception described below). +</p> + +<p> +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING. If you do not have the source code, it is available at: + + <a href="http://www.gnu.org/software/classpath/">http://www.gnu.org/software/classpath/</a> +</p> + +<pre> + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. + + Parts derived from code which carried the following notice: + + Copyright (c) 1997, 1998 by Microstar Software Ltd. + + AElfred is free for both commercial and non-commercial use and + redistribution, provided that Microstar's copyright and disclaimer are + retained intact. You are free to modify AElfred for your own use and + to redistribute AElfred with your modifications, provided that the + modifications are clearly documented. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + merchantability or fitness for a particular purpose. Please use it AT + YOUR OWN RISK. +</pre> + +<p> Some of this documentation was modified from the original +Ælfred README.txt file. All of it has been updated. </p> + +</p> + + +<h2><a name="changes">Changes Since the last Microstar Release</a></h2> + +<p> As noted above, Microstar has not updated this parser since +the summer of 1998, when it released version 1.2a on its web site. +This release is intended to benefit the developer community by +refocusing the API on SAX2, and improving conformance to the extent +that most developers should not need to use another XML parser. </p> + +<p> The code has been cleaned up (referring to the XML 1.0 spec in +all the production numbers in +comments, rather than some preliminary draft, for one example) and +has been sped up a bit as well. +JAXP support has been added, although developers are still +strongly encouraged to use the SAX2 APIs directly. </p> + + +<h3><a name="sax2">SAX2 Support</a></h3> + +<p> The original version of Ælfred did not support the +SAX2 APIs. </p> + +<p> This version supports the SAX2 APIs, exposing the standard +boolean feature descriptors. It supports the "DeclHandler" property +to provide access to all DTD declarations not already exposed +through the SAX1 API. The "LexicalHandler" property is supported, +exposing entity boundaries (including the unnamed external subset) and +things like comments and CDATA boundaries. SAX1 compatibility is +currently provided.</p> + + +<h3><a name="validation">Validation</a></h3> + +<p> In the 'pipeline' package in this same software distribution is an +<a href="../pipeline/ValidationConsumer.html">XML Validation component</a> +using any full SAX2 event stream (including all document type declarations) +to validate. There is now a <a href="XmlReader.html">XmlReader</a> class +which combines that class and this enhanced Ælfred parser, creating +an optionally validating SAX2 parser. </p> + +<p> As noted in the documentation for that validating component, certain +validity constraints can't reliably be tested by a layered validator. +These include all constraints relying on +layering violations (exposing XML at the level of tokens or below, +required since XML isn't a context-free grammar), some that +SAX2 doesn't support, and a few others. The resulting validating +parser is conformant enough for most applications that aren't doing +strange SGML tricks with DTDs. +Moreover, that validating filter can be used without +a parser ... any application component that emits SAX event streams +can DTD-validate its output on demand. </p> + +<h3><a name="smaller">You want Smaller?</a></h3> + +<p> You'll have noticed that the original version of Ælfred +had small size as a top goal. Ælfred2 normally includes a +DTD validation layer, but you can package without that. +Similarly, JAXP factory support is available but optional. +Then the main added cost due to this revision are for +supporting the SAX2 API itself; DTD validation is as +cleanly layered as allowed by SAX2.</p> + +<h3><a name="bugfixes">Bugs Fixed</a></h3> + +<p> Bugs fixed in Ælfred2 include: </p> + +<ol> + <li> Originally Ælfred didn't close file descriptors, which + led to file descriptor leakage on programs which ran for any + length of time. </li> + + <li> NOTATION declarations without system identifiers are + now handled correctly. </li> + + <li> DTD events are now reported for all invocations of a + given parser, not just the first one. </li> + + <li> More correct character handling: <ul> + + <li> Rejects out-of-range characters, both in text and in + character references. </li> + + <li> Correctly handles character references that expand to + surrogate pairs. </li> + + <li> Correctly handles UTF-8 encodings of surrogate pairs. </li> + + <li> Correctly handles Unicode 3.1 rules about illegal UTF-8 + encodings: there is only one legal encoding per character. </li> + + <li> PUBLIC identifiers are now rejected if they have illegal + characters. </li> + + <li> The parser is more correct about what characters are allowed + in names and name tokens. Uses Unicode rules (built in to Java) + rather than the voluminous XML rules, although some extensions + have been made to match XML rules more closely.</li> + + <li> Line ends are now normalized to newlines in all known + cases. </li> + + </ul></li> + + <li> Certain validity errors were previously treated as well + formedness violations. <ul> + + <li> Repeated declarations of an element type are no + longer fatal errors. </li> + + <li> Undeclared parameter entity references are no longer + fatal errors. </li> + + </ul></li> + + <li> Attribute handling is improved: <ul> + + <li> Whitespace must exist between attributes. </li> + + <li> Only one value for a given attribute is permitted. </li> + + <li> ATTLIST declarations don't need to declare attributes. </li> + + <li> Attribute values are normalized when required. </li> + + <li> Tabs in attribute values are normalized to spaces. </li> + + <li> Attribute values containing a literal "<" are rejected. </li> + + </ul></li> + + <li> More correct entity handling: <ul> + + <li> Whitespace must precede NDATA when declaring unparsed + entities.</li> + + <li> Parameter entity declarations may not have NDATA annotations. </li> + + <li> The XML specification has a bug in that it doesn't specify + that certain contexts exist within which parameter entity + expansion must not be performed. Lacking an offical erratum, + this parser now disables such expansion inside comments, + processing instructions, ignored sections, public identifiers, + and parts of entity declarations. </li> + + <li> Entity expansions that include quote characters no longer + confuse parsing of strings using such expansions. </li> + + <li> Whitespace in the values of internal entities is not mapped + to space characters. </li> + + <li> General Entity references in attribute defaults within the + DTD now cause fatal errors when the entity is not defined at the + time it is referenced. </li> + + <li> Malformed general entity references in entity declarations are + now detected. </li> + + </ul></li> + + <li> Neither conditional sections + nor parameter entity references within markup declarations + are permitted in the internal subset. </li> + + <li> Processing instructions whose target names are "XML" + (ignoring case) are now rejected. </li> + + <li> Comments may not include "--".</li> + + <li> Most "]]>" sequences in text are rejected. </li> + + <li> Correct syntax for standalone declarations is enforced. </li> + + <li> Setting a locale for diagnostics only produces an exception + if the language of that locale isn't English. </li> + + <li> Some more encoding names are recognized. These include the + Unicode 3.0 variants of UTF-16 (UTF-16BE, UTF-16LE) as well as + US-ASCII and a few commonly seen synonyms. </li> + + <li> Text (from character content, PIs, or comments) large enough + not to fit into internal buffers is now handled correctly even in + some cases which were originally handled incorrectly.</li> + + <li> Content is now reported for element types for which attributes + have been declared, but no content model is known. (Such documents + are invalid, but may still be well formed.) </li> + +</ol> + +<p> Other bugs may also have been fixed. </p> + +<p> For better overall validation support, some of the validity +constraints that can't be verified using the SAX2 event stream +are now reported directly by Ælfred2. </p> + +</body></html> + diff --git a/libjava/classpath/gnu/xml/dom/Consumer.java b/libjava/classpath/gnu/xml/dom/Consumer.java new file mode 100644 index 00000000000..f99e221e998 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/Consumer.java @@ -0,0 +1,368 @@ +/* Consumer.java -- + Copyright (C) 2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.xml.dom; + +import org.w3c.dom.DocumentType; +import org.w3c.dom.Node; +import org.w3c.dom.Text; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.ext.Attributes2; + +import gnu.xml.pipeline.DomConsumer; +import gnu.xml.pipeline.EventConsumer; + + +/** + * Event consumer which constructs DOM documents using the implementation + * in this package, using SAX2 events. This packages various backdoors + * into this DOM implementation, as needed to address DOM requirements + * that can't be met by strictly conforming implementations of DOM. + * + * <p> These requirements all relate to {@link DocumentType} nodes and + * features of that node type. These features are normally not used, + * because that interface only exposes a subset of the information found + * in DTDs. More, that subset does not include the most important typing + * information. For example, it excludes element content models and + * attribute typing. It does expose some entity management issues, + * although entity management doesn't relate to document typing. + * + * <p> Note that SAX2 does not expose the literal text of the DTD's + * internal subset, so it will not be present in DOM trees constructed + * using this API. (Though with a good SAX2 implementation, it could + * be partially recreated...) + * + * @author David Brownell + */ +public class Consumer extends DomConsumer +{ + /** + * Constructs an unconfigured event consumer, + * as a terminus in a SAX event pipeline. + */ + // used by PipelineFactory [terminus] + public Consumer () + throws SAXException + { + super (DomDocument.class); + setHandler (new Backdoor (this)); + } + + /** + * Constructs an unconfigured event consumer, + * as a stage in a SAX event pipeline. + */ + // used by PipelineFactory [filter] + public Consumer (EventConsumer next) + throws SAXException + { + super (DomDocument.class, next); + setHandler (new Backdoor (this)); + } + + /** + * Implements the backdoors needed by DOM. + * All methods in this class use implementation-specific APIs that are + * implied by the DOM specification (needed to implement testable + * behavior) but which are excluded from the DOM specification. + */ + public static class Backdoor extends DomConsumer.Handler + { + /** + * Constructor. + * @param consumer must have been initialized to use the + * {@link DomDocument} class (or a subclass) for + * constructing DOM trees + */ + protected Backdoor (DomConsumer consumer) + throws SAXException + { super (consumer); } + + // helper routine + private DomDoctype getDoctype () + throws SAXException + { + DomDocument doc = (DomDocument) getDocument (); + DocumentType dt = doc.getDoctype (); + + if (dt == null) + throw new SAXException ("doctype missing!"); + return (DomDoctype) dt; + } + + // SAX2 "lexical" event + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + DomDocument doc = (DomDocument) getDocument (); + + super.startDTD (name, publicId, systemId); + // DOM L2 doctype creation model is bizarre + DomDoctype dt = new DomDoctype (doc, name, publicId, systemId); + doc.appendChild (dt); + } + + // SAX2 "lexical" event + public void endDTD () + throws SAXException + { + super.endDTD (); + // DOM L2 has no way to make things readonly + getDoctype ().makeReadonly (); + } + + // SAX1 DTD event + public void notationDecl ( + String name, + String publicId, String systemId + ) throws SAXException + { + // DOM L2 can't create/save notation nodes + getDoctype ().declareNotation (name, publicId, systemId); + } + + // SAX1 DTD event + public void unparsedEntityDecl ( + String name, + String publicId, String systemId, + String notationName + ) throws SAXException + { + // DOM L2 can't create/save entity nodes + getDoctype ().declareEntity (name, publicId, systemId, + notationName); + } + + // SAX2 declaration event + public void internalEntityDecl (String name, String value) + throws SAXException + { + // DOM L2 can't create/save entity nodes + // NOTE: this doesn't save the value as a child of this + // node, though it could realistically do so. + getDoctype ().declareEntity (name, null, null, null); + } + + // SAX2 declaration event + public void externalEntityDecl ( + String name, + String publicId, + String systemId + ) throws SAXException + { + // DOM L2 can't create/save entity nodes + // NOTE: DOM allows for these to have children, if + // they don't have unbound namespace references. + getDoctype ().declareEntity (name, publicId, systemId, null); + } + + // SAX2 element + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + Node top; + + super.startElement (uri, localName, qName, atts); + + // might there be more work? + top = getTop (); + if (!top.hasAttributes () || !(atts instanceof Attributes2)) + return; + + // remember any attributes that got defaulted + DomNamedNodeMap map = (DomNamedNodeMap) top.getAttributes (); + Attributes2 attrs = (Attributes2) atts; + int length = atts.getLength (); + + //map.compact (); + for (int i = 0; i < length; i++) { + if (attrs.isSpecified (i)) + continue; + + // value was defaulted. + String temp = attrs.getQName (i); + DomAttr attr; + + if ("".equals (temp)) + attr = (DomAttr) map.getNamedItemNS (attrs.getURI (i), + atts.getLocalName (i)); + else + attr = (DomAttr) map.getNamedItem (temp); + + // DOM L2 can't write this flag, only read it + attr.setSpecified (false); + } + } + + public void endElement ( + String uri, + String localName, + String qName + ) throws SAXException + { + DomNode top = (DomNode) getTop (); + top.compact (); + super.endElement (uri, localName, qName); + } + + protected Text createText ( + boolean isCDATA, + char buf [], + int off, + int len + ) { + DomDocument doc = (DomDocument) getDocument (); + + if (isCDATA) + return doc.createCDATASection (buf, off, len); + else + return doc.createTextNode (buf, off, len); + } + + public void elementDecl(String name, String model) + throws SAXException + { + getDoctype().elementDecl(name, model); + } + + public void attributeDecl ( + String ename, + String aname, + String type, + String mode, + String value + ) throws SAXException + { + getDoctype().attributeDecl(ename, aname, type, mode, value); + /* + if (value == null && !"ID".equals (type)) + return; + + DomDoctype.ElementInfo info; + + info = getDoctype ().getElementInfo (ename); + if (value != null) + info.setAttrDefault (aname, value); + if ("ID".equals (type)) + info.setIdAttr (aname); + */ + + } + + // force duplicate name checking off while we're + // using parser output (don't duplicate the work) + public void startDocument () throws SAXException + { + super.startDocument (); + + DomDocument doc = (DomDocument) getDocument (); + doc.setStrictErrorChecking(false); + doc.setBuilding(true); + } + + /** + * Required by DOM Level 3 to report document parameters + */ + public void xmlDecl(String version, + String encoding, + boolean standalone, + String inputEncoding) + throws SAXException + { + super.xmlDecl(version, encoding, standalone, inputEncoding); + + DomDocument doc = (DomDocument) getDocument(); + doc.setXmlEncoding(encoding); + doc.setInputEncoding(inputEncoding); + } + + public void endDocument () + throws SAXException + { + DomDocument doc = (DomDocument) getDocument (); + doc.setStrictErrorChecking(true); + doc.setBuilding(false); + doc.compact (); + DomDoctype doctype = (DomDoctype) doc.getDoctype(); + if (doctype != null) + { + doctype.makeReadonly(); + } + super.endDocument (); + } + + // these three methods collaborate to populate entity + // refs, marking contents readonly on end-of-entity + + public boolean canPopulateEntityRefs () + { return true; } + + public void startEntity (String name) + throws SAXException + { + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + super.startEntity (name); + + DomNode top = (DomNode) getTop (); + + if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) + top.readonly = false; + } + + public void endEntity (String name) + throws SAXException + { + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + DomNode top = (DomNode) getTop (); + + if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) { + top.compact (); + top.makeReadonly (); + } + super.endEntity (name); + } + } +} diff --git a/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java b/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java new file mode 100644 index 00000000000..e3c69c43ecb --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java @@ -0,0 +1,84 @@ +/* DTDAttributeTypeInfo.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.TypeInfo; + +/** + * Attribute type information supplied by a DTD attribute declaration. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DTDAttributeTypeInfo + implements TypeInfo +{ + + final String elementName; + final String name; + final String type; + final String mode; + final String value; + + DTDAttributeTypeInfo(String elementName, String name, String type, + String mode, String value) + { + this.elementName = elementName; + this.name = name; + this.type = type; + this.mode = mode; + this.value = value; + } + + public final String getTypeName() + { + return type; + } + + public final String getTypeNamespace() + { + return "http://www.w3.org/TR/REC-xml"; + } + + public final boolean isDerivedFrom(String typeNamespace, String typeName, + int derivationMethod) + { + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java b/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java new file mode 100644 index 00000000000..c5553e20e62 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java @@ -0,0 +1,112 @@ +/* DTDElementTypeInfo.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashMap; +import java.util.Iterator; +import org.w3c.dom.TypeInfo; + +/** + * Element type information provided by a DTD element declaration. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DTDElementTypeInfo + implements TypeInfo +{ + + final String name; + String model; + HashMap attributes; + String idAttrName; + + DTDElementTypeInfo(String name, String model) + { + this.name = name; + this.model = model; + } + + public final String getTypeName() + { + return null; + } + + public final String getTypeNamespace() + { + return "http://www.w3.org/TR/REC-xml"; + } + + public final boolean isDerivedFrom(String typeNamespace, String typeName, + int derivationMethod) + { + return false; + } + + DTDAttributeTypeInfo getAttributeTypeInfo(String name) + { + if (attributes == null) + { + return null; + } + return (DTDAttributeTypeInfo) attributes.get(name); + } + + void setAttributeTypeInfo(String name, DTDAttributeTypeInfo info) + { + if (attributes == null) + { + attributes = new HashMap(); + } + attributes.put(name, info); + if ("ID".equals(info.type)) + { + idAttrName = name; + } + } + + Iterator attributes() + { + if (attributes == null) + { + return null; + } + return attributes.values().iterator(); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomAttr.java b/libjava/classpath/gnu/xml/dom/DomAttr.java new file mode 100644 index 00000000000..8673a796161 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomAttr.java @@ -0,0 +1,380 @@ +/* DomAttr.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.TypeInfo; +import org.w3c.dom.events.MutationEvent; + + +/** + * <p> "Attr" implementation. In DOM, attributes cost quite a lot of + * memory because their values are complex structures rather than just + * simple strings. To reduce your costs, avoid having more than one + * child of an attribute; stick to a single Text node child, and ignore + * even that by using the attribute's "nodeValue" property.</p> + * + * <p> As a bit of general advice, only look at attribute modification + * events through the DOMAttrModified event (sent to the associated + * element). Implementations are not guaranteed to report other events + * in the same order, so you're very likely to write nonportable code if + * you monitor events at the "children of Attr" level.</p> + * + * <p> At this writing, not all attribute modifications will cause the + * DOMAttrModified event to be triggered ... only the ones using the string + * methods (setNodeValue, setValue, and Element.setAttribute) to modify + * those values. That is, if you manipulate those children directly, + * elements won't get notified that attribute values have changed. + * The natural fix for that will report other modifications, but won't + * be able to expose "previous" attribute value; it'll need to be cached + * or something (at which point why bother using child nodes). </p> + * + * <p><em>You are strongly advised not to use "children" of any attribute + * nodes you work with.</em> </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomAttr + extends DomNsNode + implements Attr +{ + + private boolean specified; + private String value; // string value cache + + /** + * Constructs an Attr node associated with the specified document. + * The "specified" flag is initialized to true, since this DOM has + * no current "back door" mechanisms to manage default values so + * that every value must effectively be "specified". + * + * <p>This constructor should only be invoked by a Document as part of + * its createAttribute functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this node is associated + * @param namespaceURI Combined with the local part of the name, + * this is used to uniquely identify a type of attribute + * @param name Name of this attribute, which may include a prefix + */ + protected DomAttr(DomDocument owner, String namespaceURI, String name) + { + super(ATTRIBUTE_NODE, owner, namespaceURI, name); + specified = true; + length = 1; + + // XXX register self to get insertion/removal events + // and character data change events and when they happen, + // report self-mutation + } + + /** + * <b>DOM L1</b> + * Returns the attribute name (same as getNodeName) + */ + public final String getName() + { + return getNodeName(); + } + + /** + * <b>DOM L1</b> + * Returns true if a parser reported this was in the source text. + */ + public final boolean getSpecified() + { + return specified; + } + + /** + * Records whether this attribute was in the source text. + */ + public final void setSpecified(boolean value) + { + specified = value; + } + + /** + * <b>DOM L1</b> + * Returns the attribute value, with character and entity + * references substituted. + * <em>NOTE: entity refs as children aren't currently handled.</em> + */ + public String getNodeValue() + { + // If we have a simple node-value, use that + if (first == null) + { + return (value == null) ? "" : value; + } + // Otherwise collect child node-values + StringBuffer buf = new StringBuffer(); + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + switch (ctx.nodeType) + { + case Node.TEXT_NODE: + buf.append(ctx.getNodeValue()); + break; + case Node.ENTITY_REFERENCE_NODE: + // TODO + break; + } + } + return buf.toString(); + } + + /** + * <b>DOM L1</b> + * Assigns the value of the attribute; it will have one child, + * which is a text node with the specified value (same as + * setNodeValue). + */ + public final void setValue(String value) + { + setNodeValue(value); + } + + /** + * <b>DOM L1</b> + * Returns the value of the attribute as a non-null string; same + * as getNodeValue. + * <em>NOTE: entity refs as children aren't currently handled.</em> + */ + public final String getValue() + { + return getNodeValue(); + } + + /** + * <b>DOM L1</b> + * Assigns the attribute value; using this API, no entity or + * character references will exist. + * Causes a DOMAttrModified mutation event to be sent. + */ + public void setNodeValue(String value) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (value == null) + { + value = ""; + } + String oldValue = getNodeValue(); + while (last != null) + { + removeChild(last); + } + // don't create a new node just for this... + /* + Node text = owner.createTextNode(value); + appendChild(text); + */ + this.value = value; + length = 1; + specified = true; + + mutating(oldValue, value, MutationEvent.MODIFICATION); + } + + public final Node getFirstChild() + { + // Create a child text node if necessary + if (first == null) + { + length = 0; + Node text = owner.createTextNode((value == null) ? "" : value); + appendChild(text); + } + return first; + } + + public final Node getLastChild() + { + // Create a child text node if necessary + if (last == null) + { + length = 0; + Node text = owner.createTextNode((value == null) ? "" : value); + appendChild(text); + } + return last; + } + + public Node item(int index) + { + // Create a child text node if necessary + if (first == null) + { + length = 0; + Node text = owner.createTextNode((value == null) ? "" : value); + appendChild(text); + } + return super.item(index); + } + + /** + * <b>DOM L2</b> + * Returns the element with which this attribute is associated. + */ + public final Element getOwnerElement() + { + return (Element) parent; + } + + public final Node getNextSibling() + { + return null; + } + + public final Node getPreviousSibling() + { + return null; + } + + public Node getParentNode() + { + return null; + } + + /** + * Records the element with which this attribute is associated. + */ + public final void setOwnerElement(Element e) + { + if (parent != null) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR); + } + if (!(e instanceof DomElement)) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR); + } + parent = (DomElement) e; + depth = parent.depth + 1; + } + + /** + * The base URI of an Attr is always <code>null</code>. + */ + public final String getBaseURI() + { + return null; + } + + /** + * Shallow clone of the attribute, breaking all ties with any + * elements. + */ + public Object clone() + { + DomAttr retval = (DomAttr) super.clone(); + retval.specified = true; + return retval; + } + + private void mutating(String oldValue, String newValue, short why) + { + if (!reportMutations || parent == null) + { + return; + } + + // EVENT: DOMAttrModified, target = parent, + // prev/new values provided, also attr name + MutationEvent event; + + event = (MutationEvent) createEvent ("MutationEvents"); + event.initMutationEvent ("DOMAttrModified", + true /* bubbles */, false /* nocancel */, + null, oldValue, newValue, getNodeName (), why); + parent.dispatchEvent (event); + } + + // DOM Level 3 methods + + public TypeInfo getSchemaTypeInfo() + { + if (parent != null) + { + // DTD implementation + DomDoctype doctype = (DomDoctype) parent.owner.getDoctype(); + if (doctype != null) + { + return doctype.getAttributeTypeInfo(parent.getNodeName(), + getNodeName()); + } + // TODO XML Schema implementation + } + return null; + } + + public boolean isId() + { + if (parent != null) + { + DomDoctype doctype = (DomDoctype) parent.owner.getDoctype(); + if (doctype != null) + { + DTDAttributeTypeInfo info = + doctype.getAttributeTypeInfo(parent.getNodeName(), + getNodeName()); + if (info != null && "ID".equals(info.type)) + { + return true; + } + } + DomElement element = (DomElement) parent; + if (element.userIdAttrs != null && + element.userIdAttrs.contains(this)) + { + return true; + } + // TODO XML Schema implementation + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomCDATASection.java b/libjava/classpath/gnu/xml/dom/DomCDATASection.java new file mode 100644 index 00000000000..e34359e71bf --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomCDATASection.java @@ -0,0 +1,91 @@ +/* DomCDATASection.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.CDATASection; + +/** + * <p> "CDATASection" implementation. + * This is a non-core DOM class, supporting the "XML" feature. + * CDATA sections are just ways to represent text using different + * delimeters. </p> + * + * <p> <em>You are strongly advised not to use CDATASection nodes.</em> + * The advantage of having slightly prettier ways to print text that may + * have lots of embedded XML delimiters, such as "&" and "<", + * can be dwarfed by the cost of dealing with multiple kinds of text + * nodes in all your algorithms. </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomCDATASection + extends DomText + implements CDATASection +{ + + /** + * Constructs a CDATA section node associated with the specified + * document and holding the specified data. + * + * <p>This constructor should only be invoked by a Document as part of + * its createCDATASection functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + */ + protected DomCDATASection(DomDocument owner, String value) + { + super(CDATA_SECTION_NODE, owner, value); + } + + protected DomCDATASection(DomDocument owner, char buf [], int off, int len) + { + super(CDATA_SECTION_NODE, owner, buf, off, len); + } + + /** + * <b>DOM L1</b> + * Returns the string "#cdata-section". + */ + final public String getNodeName() + { + return "#cdata-section"; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomCharacterData.java b/libjava/classpath/gnu/xml/dom/DomCharacterData.java new file mode 100644 index 00000000000..e94dcc4ecf9 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomCharacterData.java @@ -0,0 +1,310 @@ +/* DomCharacterData.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.CharacterData; +import org.w3c.dom.DOMException; +import org.w3c.dom.events.MutationEvent; + + +/** + * <p> Abstract "CharacterData" implementation. This + * facilitates reusing code in classes implementing subtypes of that DOM + * interface (Text, Comment, CDATASection). </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class DomCharacterData + extends DomNode + implements CharacterData +{ + + private String text; + + // package private + DomCharacterData(short nodeType, DomDocument doc, String value) + { + super(nodeType, doc); + text = (value == null) ? "" : value; + } + + // package private + DomCharacterData(short nodeType, DomDocument doc, + char[] buf, int offset, int length) + { + super(nodeType, doc); + text = (buf == null) ? "" : new String(buf, offset, length); + } + + /** + * <b>DOM L1</b> + * Appends the specified data to the value of this node. + * Causes a DOMCharacterDataModified mutation event to be reported. + */ + public void appendData(String arg) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + String value = text + arg; + mutating(value); + text = value; + } + + /** + * <b>DOM L1</b> + * Modifies the value of this node. + * Causes a DOMCharacterDataModified mutation event to be reported. + */ + public void deleteData(int offset, int count) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + char[] raw = text.toCharArray(); + if (offset < 0 || count < 0 || offset > raw.length) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + if ((offset + count) > raw.length) + { + count = raw.length - offset; + } + if (count == 0) + { + return; + } + try + { + char[] buf = new char[raw.length - count]; + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(raw, offset + count, buf, offset, + raw.length - (offset + count)); + String value = new String(buf); + mutating(value); + text = value; + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * <b>DOM L1</b> + * Returns the value of this node. + */ + public String getNodeValue() + { + return text; + } + + /** + * <b>DOM L1</b> + * Returns the value of this node; same as getNodeValue. + */ + public final String getData() + { + return text; + } + + /** + * <b>DOM L1</b> + * Returns the length of the data. + */ + public int getLength() + { + return text.length(); + } + + /** + * <b>DOM L1</b> + * Modifies the value of this node. + */ + public void insertData(int offset, String arg) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + char[] raw = text.toCharArray(); + char[] tmp = arg.toCharArray (); + char[] buf = new char[raw.length + tmp.length]; + + try + { + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(tmp, 0, buf, offset, tmp.length); + System.arraycopy(raw, offset, buf, offset + tmp.length, + raw.length - offset); + String value = new String(buf); + mutating(value); + text = value; + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * <b>DOM L1</b> + * Modifies the value of this node. Causes DOMCharacterDataModified + * mutation events to be reported (at least one). + */ + public void replaceData(int offset, int count, String arg) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + char[] raw = text.toCharArray(); + + // deleteData + if (offset < 0 || count < 0 || offset > raw.length) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + if ((offset + count) > raw.length) + { + count = raw.length - offset; + } + try + { + char[] buf = new char[raw.length - count]; + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(raw, offset + count, buf, offset, + raw.length - (offset + count)); + + // insertData + char[] tmp = arg.toCharArray (); + char[] buf2 = new char[buf.length + tmp.length]; + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(tmp, 0, buf, offset, tmp.length); + System.arraycopy(raw, offset, buf, offset + tmp.length, + raw.length - offset); + String value = new String(buf); + mutating(value); + text = value; + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * <b>DOM L1</b> + * Assigns the value of this node. + * Causes a DOMCharacterDataModified mutation event to be reported. + */ + public void setNodeValue(String value) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (value == null) + { + value = ""; + } + mutating(value); + text = value; + } + + /** + * <b>DOM L1</b> + * Assigns the value of this node; same as setNodeValue. + */ + final public void setData(String data) + { + setNodeValue(data); + } + + /** + * <b>DOM L1</b> + * Returns the specified substring. + */ + public String substringData(int offset, int count) + { + try + { + return text.substring(offset, count); + } + catch (StringIndexOutOfBoundsException e) + { + if (offset >= 0 && count >= 0 && offset < text.length()) + { + return text.substring(offset); + } + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * The base URI for character data is <code>null</code>. + * @since DOM Level 3 Core + */ + public final String getBaseURI() + { + return null; + } + + private void mutating(String newValue) + { + if (!reportMutations) + { + return; + } + + // EVENT: DOMCharacterDataModified, target = this, + // prev/new values provided + MutationEvent event; + + event = (MutationEvent) createEvent("MutationEvents"); + event.initMutationEvent("DOMCharacterDataModified", + true /* bubbles */, false /* nocancel */, + null, text, newValue, null, (short) 0); + dispatchEvent(event); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomComment.java b/libjava/classpath/gnu/xml/dom/DomComment.java new file mode 100644 index 00000000000..71c8160485f --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomComment.java @@ -0,0 +1,81 @@ +/* DomComment.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Comment; + +/** + * <p> "Comment" implementation. + * Comments hold data intended for direct consumption by people; + * programs should only use ProcessingInstruction nodes. Note that + * since SAX makes comment reporting optional, XML systems that + * rely on comments (such as by using this class) will often lose + * those comments at some point in the processing pipeline. </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomComment + extends DomCharacterData + implements Comment +{ + + /** + * Constructs a comment node associated with the specified + * document and holding the specified data. + * + * <p>This constructor should only be invoked by a Document as part of + * its createComment functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + */ + protected DomComment(DomDocument owner, String value) + { + super(COMMENT_NODE, owner, value); + } + + /** + * <b>DOM L1</b> + * Returns the string "#comment". + */ + final public String getNodeName() + { + return "#comment"; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomDOMException.java b/libjava/classpath/gnu/xml/dom/DomDOMException.java new file mode 100644 index 00000000000..cf93fcf9749 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDOMException.java @@ -0,0 +1,175 @@ +/* DomDOMException.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; + +/** + * <p> DOMException implementation. The version that + * is provided by the W3C is abstract, so it can't be instantiated. + * + * <p> This also provides a bit more information about the error + * that is being reported, in terms of the relevant DOM structures + * and data. + * + * @author David Brownell + */ +public class DomDOMException + extends DOMException +{ + + /** @serial Data that caused an error to be reported */ + private String data; + + /** @serial Node associated with the error. */ + private Node node; + + /** @serial Data associated with the error. */ + private int value; + + /** + * Constructs an exception, with the diagnostic message + * corresponding to the specified code. + */ + public DomDOMException(short code) + { + super(code, diagnostic(code)); + } + + /** + * Constructs an exception, with the diagnostic message + * corresponding to the specified code and additional + * information as provided. + */ + public DomDOMException(short code, String data, Node node, int value) + { + super(code, diagnostic(code)); + this.data = data; + this.node = node; + this.value = value; + } + + /** Returns the node to which the diagnotic applies, or null. */ + final public Node getNode() + { + return node; + } + + /** Returns data to which the diagnotic applies, or null. */ + final public String getData() + { + return data; + } + + /** Returns data to which the diagnotic applies, or null. */ + final public int getValue() + { + return value; + } + + /** + * Returns a diagnostic message that may be slightly more useful + * than the generic one, where possible. + */ + public String getMessage() + { + String retval = super.getMessage(); + + if (data != null) + { + retval += "\nMore Information: " + data; + } + if (value != 0) + { + retval += "\nNumber: " + value; + } + if (node != null) + { + retval += "\nNode Name: " + node.getNodeName(); + } + return retval; + } + + // these strings should be localizable. + + private static String diagnostic(short code) + { + switch (code) + { + // DOM L1: + case INDEX_SIZE_ERR: + return "An index or size is out of range."; + case DOMSTRING_SIZE_ERR: + return "A string is too big."; + case HIERARCHY_REQUEST_ERR: + return "The node doesn't belong here."; + case WRONG_DOCUMENT_ERR: + return "The node belongs in another document."; + case INVALID_CHARACTER_ERR: + return "That character is not permitted."; + case NO_DATA_ALLOWED_ERR: + return "This node does not permit data."; + case NO_MODIFICATION_ALLOWED_ERR: + return "No changes are allowed."; + case NOT_FOUND_ERR: + return "The node was not found in that context."; + case NOT_SUPPORTED_ERR: + return "That object is not supported."; + case INUSE_ATTRIBUTE_ERR: + return "The attribute belongs to a different element."; + + // DOM L2: + case INVALID_STATE_ERR: + return "The object is not usable."; + case SYNTAX_ERR: + return "An illegal string was provided."; + case INVALID_MODIFICATION_ERR: + return "An object's type may not be changed."; + case NAMESPACE_ERR: + return "The operation violates XML Namespaces."; + case INVALID_ACCESS_ERR: + return "Parameter or operation isn't supported by this node."; + case TYPE_MISMATCH_ERR: + return "The type of the argument is incompatible with the expected type."; + } + return "Reserved exception number: " + code; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomDoctype.java b/libjava/classpath/gnu/xml/dom/DomDoctype.java new file mode 100644 index 00000000000..d35eedc7f75 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDoctype.java @@ -0,0 +1,455 @@ +/* DomDoctype.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashMap; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Entity; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Notation; + +/** + * <p> "DocumentType" implementation (with no extensions for supporting + * any document typing information). This is a non-core DOM class, + * supporting the "XML" feature. </p> + * + * <p> <em>Few XML applications will actually care about this partial + * DTD support</em>, since it doesn't expose any (!) of the data typing + * facilities which can motivate applications to use DTDs. It does not + * expose element content models, or information about attribute typing + * rules. Plus the information it exposes isn't very useful; as one example, + * DOM exposes information about unparsed ENTITY objects, which is only used + * with certain element attributes, but does not expose the information about + * those attributes which is needed to apply that data! </p> + * + * <p> Also, note that there are no nonportable ways to associate even the + * notation and entity information exposed by DOM with a DocumentType. While + * there is a DOM L2 method to construct a DocumentType, it only gives access + * to the textual content of the <!DOCTYPE ...> declaration. </p> + * + * <p> In short, <em>you are strongly advised not to rely on this incomplete + * DTD functionality</em> in your application code.</p> + * + * @see DomEntity + * @see DomEntityReference + * @see DomNotation + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomDoctype + extends DomExtern + implements DocumentType +{ + + private DomNamedNodeMap notations; + private DomNamedNodeMap entities; + private final DOMImplementation implementation; + private String subset; + + private HashMap elements = new HashMap(); + private boolean ids; + + /** + * Constructs a DocumentType node associated with the specified + * implementation, with the specified name. + * + * <p>This constructor should only be invoked by a DOMImplementation as + * part of its createDocumentType functionality, or through a subclass + * which is similarly used in a "Sub-DOM" style layer. + * + * <p> Note that at this time there is no standard SAX API granting + * access to the internal subset text, so that relying on that value + * is not currently portable. + * + * @param impl The implementation with which this object is associated + * @param name Name of this root element + * @param publicId If non-null, provides the external subset's + * PUBLIC identifier + * @param systemId If non-null, provides the external subset's + * SYSTEM identifier + * @param internalSubset Provides the literal value (unparsed, no + * entities expanded) of the DTD's internal subset. + */ + protected DomDoctype(DOMImplementation impl, + String name, + String publicId, + String systemId, + String internalSubset) + { + super(DOCUMENT_TYPE_NODE, null, name, publicId, systemId); + implementation = impl; + subset = internalSubset; + } + + /** + * JAXP builder constructor. + * @param doc the document + * @param name the name of the document element + * @param publicId the public ID of the document type declaration + * @param systemId the system ID of the document type declaration + */ + public DomDoctype(DomDocument doc, + String name, + String publicId, + String systemId) + { + super(DOCUMENT_TYPE_NODE, doc, name, publicId, systemId); + implementation = doc.getImplementation(); + } + + /** + * <b>DOM L1</b> + * Returns the root element's name (just like getNodeName). + */ + final public String getName() + { + return getNodeName(); + } + + /** + * <b>DOM L1</b> + * Returns information about any general entities declared + * in the DTD. + * + * <p><em>Note: DOM L1 doesn't throw a DOMException here, but + * then it doesn't have the strange construction rules of L2.</em> + * + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public NamedNodeMap getEntities() + { + if (entities == null) + { + entities = new DomNamedNodeMap(this, Node.ENTITY_NODE); + } + return entities; + } + + /** + * Records the declaration of a general entity in this DocumentType. + * + * @param name Name of the entity + * @param publicId If non-null, provides the entity's PUBLIC identifier + * @param systemId Provides the entity's SYSTEM identifier + * @param notation If non-null, provides the entity's notation + * (indicating an unparsed entity) + * @return The Entity that was declared, or null if the entity wasn't + * recorded (because it's a parameter entity or because an entity with + * this name was already declared). + * + * @exception DOMException NO_MODIFICATION_ALLOWED_ERR if the + * DocumentType is no longer writable. + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public Entity declareEntity(String name, + String publicId, + String systemId, + String notation) + { + DomEntity entity; + + if (name.charAt(0) == '%' || "[dtd]".equals(name)) + { + return null; + } + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + getEntities(); + + DomDocument.checkName(name, (owner != null) ? + "1.1".equals(owner.getXmlVersion()) : false); + if (entities.getNamedItem(name) != null) + { + return null; + } + + entity = new DomEntity(owner, name, publicId, systemId, notation); + entities.setNamedItem(entity); + return entity; + } + + /** + * <b>DOM L1</b> + * Returns information about any notations declared in the DTD. + * + * <p><em>Note: DOM L1 doesn't throw a DOMException here, but + * then it doesn't have the strange construction rules of L2.</em> + * + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public NamedNodeMap getNotations() + { + if (notations == null) + { + notations = new DomNamedNodeMap(this, Node.NOTATION_NODE); + } + return notations; + } + + /** + * Records the declaration of a notation in this DocumentType. + * + * @param name Name of the notation + * @param publicId If non-null, provides the notation's PUBLIC identifier + * @param systemId If non-null, provides the notation's SYSTEM identifier + * @return The notation that was declared. + * + * @exception DOMException NO_MODIFICATION_ALLOWED_ERR if the + * DocumentType is no longer writable. + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public Notation declareNotation(String name, + String publicId, + String systemId) + { + DomNotation notation; + + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + getNotations(); + + DomDocument.checkName(name, (owner != null) ? + "1.1".equals(owner.getXmlVersion()) : false); + + notation = new DomNotation(owner, name, publicId, systemId); + notations.setNamedItem(notation); + return notation; + } + + /** + * <b>DOM L2</b> + * Returns the internal subset of the document, as a string of unparsed + * XML declarations (and comments, PIs, whitespace); or returns null if + * there is no such subset. There is no vendor-independent expectation + * that this attribute be set, or that declarations found in it be + * reflected in the <em>entities</em> or <em>notations</em> attributes + * of this Document "Type" object. + * + * <p> Some application-specific XML profiles require that documents + * only use specific PUBLIC identifiers, without an internal subset + * to modify the interperetation of the declarations associated with + * that PUBLIC identifier through some standard. + */ + public String getInternalSubset() + { + return subset; + } + + /** + * The base URI of a DocumentType is always <code>null</code>. + * See the Infoset Mapping, appendix C. + */ + public final String getBaseURI() + { + return null; + } + + /** + * Sets the internal "readonly" flag so the node and its associated + * data (only lists of entities and notations, no type information + * at the moment) can't be changed. + */ + public void makeReadonly() + { + super.makeReadonly(); + if (entities != null) + { + entities.makeReadonly(); + } + if (notations != null) + { + notations.makeReadonly(); + } + } + + void setOwner(DomDocument doc) + { + if (entities != null) + { + for (DomNode ctx = entities.first; ctx != null; ctx = ctx.next) + { + ctx.setOwner(doc); + } + } + if (notations != null) + { + for (DomNode ctx = notations.first; ctx != null; ctx = ctx.next) + { + ctx.setOwner(doc); + } + } + super.setOwner(doc); + } + + /** + * <b>DOM L2</b> + * Consults the DOM implementation to determine if the requested + * feature is supported. + */ + final public boolean supports(String feature, String version) + { + return implementation.hasFeature(feature, version); + } + + /** + * Returns the implementation associated with this document type. + */ + final public DOMImplementation getImplementation() + { + return implementation; + } + + public void elementDecl(String name, String model) + { + DTDElementTypeInfo info = getElementTypeInfo(name); + if (info == null) + { + info = new DTDElementTypeInfo(name, model); + elements.put(name, info); + } + else + { + info.model = model; + } + } + + DTDElementTypeInfo getElementTypeInfo(String name) + { + return (DTDElementTypeInfo) elements.get(name); + } + + public void attributeDecl(String eName, String aName, String type, + String mode, String value) + { + DTDAttributeTypeInfo info = new DTDAttributeTypeInfo(eName, aName, type, + mode, value); + DTDElementTypeInfo elementInfo = getElementTypeInfo(eName); + if (elementInfo == null) + { + elementInfo = new DTDElementTypeInfo(eName, null); + elements.put(eName, elementInfo); + } + elementInfo.setAttributeTypeInfo(aName, info); + if ("ID".equals(type)) + { + ids = true; + } + } + + DTDAttributeTypeInfo getAttributeTypeInfo(String elementName, String name) + { + DTDElementTypeInfo elementInfo = + (DTDElementTypeInfo) elements.get(elementName); + return (elementInfo == null) ? null : + elementInfo.getAttributeTypeInfo(name); + } + + boolean hasIds() + { + return ids; + } + + public boolean isSameNode(Node arg) + { + if (equals(arg)) + { + return true; + } + if (!(arg instanceof DocumentType)) + { + return false; + } + DocumentType doctype = (DocumentType) arg; + if (!equal(getPublicId(), doctype.getPublicId())) + { + return false; + } + if (!equal(getSystemId(), doctype.getSystemId())) + { + return false; + } + if (!equal(getInternalSubset(), doctype.getInternalSubset())) + { + return false; + } + // TODO entities + // TODO notations + return true; + } + + /** + * Shallow clone of the doctype, except that associated + * entities and notations are (deep) cloned. + */ + public Object clone() + { + DomDoctype node = (DomDoctype) super.clone(); + + if (entities != null) + { + node.entities = new DomNamedNodeMap(node, Node.ENTITY_NODE); + for (DomNode ctx = entities.first; ctx != null; ctx = ctx.next) + { + node.entities.setNamedItem(ctx.cloneNode(true)); + } + } + if (notations != null) + { + node.notations = new DomNamedNodeMap(node, Node.NOTATION_NODE); + for (DomNode ctx = notations.first; ctx != null; ctx = ctx.next) + { + node.notations.setNamedItem(ctx.cloneNode(true)); + } + } + return node; + } + + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDocument.java b/libjava/classpath/gnu/xml/dom/DomDocument.java new file mode 100644 index 00000000000..dc476b582ac --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocument.java @@ -0,0 +1,1496 @@ +/* DomDocument.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.Iterator; +import javax.xml.XMLConstants; + +import org.w3c.dom.Attr; +import org.w3c.dom.CDATASection; +import org.w3c.dom.Comment; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.Entity; +import org.w3c.dom.EntityReference; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Notation; +import org.w3c.dom.ProcessingInstruction; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import org.w3c.dom.traversal.DocumentTraversal; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; +import org.w3c.dom.traversal.TreeWalker; +import org.w3c.dom.xpath.XPathEvaluator; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathExpression; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * <p> "Document" and "DocumentTraversal" implementation. + * + * <p> Note that when this checks names for legality, it uses an + * approximation of the XML rules, not the real ones. Specifically, + * it uses Unicode rules, with sufficient tweaks to pass a majority + * of basic XML conformance tests. (The huge XML character tables are + * hairy to implement.) + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomDocument + extends DomNode + implements Document, DocumentTraversal, XPathEvaluator +{ + + private final DOMImplementation implementation; + private boolean checkingCharacters = true; + boolean checkingWellformedness = true; + + boolean building; // if true, skip mutation events in the tree + + DomDocumentConfiguration config; + + String inputEncoding; + String encoding; + String version = "1.0"; + boolean standalone; + String systemId; + + /** + * Constructs a Document node, associating it with an instance + * of the DomImpl class. + * + * <p> Note that this constructor disables character checking. + * It is normally used when connecting a DOM to an XML parser, + * and duplicating such checks is undesirable. When used for + * purposes other than connecting to a parser, you should + * re-enable that checking. + * + * @see #setCheckingCharacters + */ + public DomDocument() + { + this(new DomImpl()); + } + + /** + * Constructs a Document node, associating it with the specified + * implementation. This should only be used in conjunction with + * a specialized implementation; it will normally be called by + * that implementation. + * + * @see DomImpl + * @see #setCheckingCharacters + */ + protected DomDocument(DOMImplementation impl) + { + super(DOCUMENT_NODE, null); + implementation = impl; + } + + /** + * Sets the <code>building</code> flag. + * Mutation events in the document are not reported. + */ + public void setBuilding(boolean flag) + { + building = flag; + } + + /** + * Sets whether to check for document well-formedness. + * If true, an exception will be raised if a second doctype or root + * element node is added to the document. + */ + public void setCheckWellformedness(boolean flag) + { + checkingWellformedness = flag; + } + + /** + * <b>DOM L1</b> + * Returns the constant "#document". + */ + final public String getNodeName() + { + return "#document"; + } + + /** + * <b>DOM L1</b> + * Returns the document's root element, or null. + */ + final public Element getDocumentElement() + { + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + if (ctx.nodeType == ELEMENT_NODE) + { + return (Element) ctx; + } + } + return null; + } + + /** + * <b>DOM L1</b> + * Returns the document's DocumentType, or null. + */ + final public DocumentType getDoctype() + { + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + if (ctx.nodeType == DOCUMENT_TYPE_NODE) + { + return (DocumentType) ctx; + } + } + return null; + } + + /** + * <b>DOM L1</b> + * Returns the document's DOMImplementation. + */ + final public DOMImplementation getImplementation() + { + return implementation; + } + + /** + * <b>DOM L1 (relocated in DOM L2)</b> + * Returns the element with the specified "ID" attribute, or null. + * + * <p>Returns null unless {@link Consumer} was used to populate internal + * DTD declaration information, using package-private APIs. If that + * internal DTD information is available, the document may be searched for + * the element with that ID. + */ + public Element getElementById(String id) + { + DomDoctype doctype = (DomDoctype) getDoctype(); + + if (doctype == null || !doctype.hasIds() + || id == null || id.length() == 0) + { + return null; + } + + // yes, this is linear in size of document. + // it'd be easy enough to maintain a hashtable. + Node current = getDocumentElement(); + Node temp; + + if (current == null) + { + return null; + } + while (current != this) + { + // done? + if (current.getNodeType() == ELEMENT_NODE) + { + DomElement element = (DomElement) current; + DTDElementTypeInfo info = + doctype.getElementTypeInfo(current.getNodeName()); + if (info != null && + id.equals(element.getAttribute(info.idAttrName))) + { + return element; + } + else if (element.userIdAttrs != null) + { + for (Iterator i = element.userIdAttrs.iterator(); + i.hasNext(); ) + { + Node idAttr = (Node) i.next(); + if (id.equals(idAttr.getNodeValue())) + { + return element; + } + } + } + } + + // descend? + if (current.hasChildNodes()) + { + current = current.getFirstChild(); + continue; + } + + // lateral? + temp = current.getNextSibling(); + if (temp != null) + { + current = temp; + continue; + } + + // back up ... + do + { + temp = current.getParentNode(); + if (temp == null) + { + return null; + } + current = temp; + temp = current.getNextSibling(); + } + while (temp == null); + current = temp; + } + return null; + } + + private void checkNewChild(Node newChild) + { + if (newChild.getNodeType() == ELEMENT_NODE + && getDocumentElement() != null) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "document element already present: " + + getDocumentElement(), newChild, 0); + } + if (newChild.getNodeType() == DOCUMENT_TYPE_NODE + && getDoctype() != null) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "document type already present: " + + getDoctype(), newChild, 0); + } + } + + /** + * <b>DOM L1</b> + * Appends the specified node to this node's list of children, + * enforcing the constraints that there be only one root element + * and one document type child. + */ + public Node appendChild(Node newChild) + { + if (checkingWellformedness) + { + checkNewChild(newChild); + } + return super.appendChild(newChild); + } + + /** + * <b>DOM L1</b> + * Inserts the specified node in this node's list of children, + * enforcing the constraints that there be only one root element + * and one document type child. + */ + public Node insertBefore(Node newChild, Node refChild) + { + if (checkingWellformedness) + { + checkNewChild(newChild); + } + return super.insertBefore(newChild, refChild); + } + + /** + * <b>DOM L1</b> + * Replaces the specified node in this node's list of children, + * enforcing the constraints that there be only one root element + * and one document type child. + */ + public Node replaceChild(Node newChild, Node refChild) + { + if (checkingWellformedness && + ((newChild.getNodeType() == ELEMENT_NODE && + refChild.getNodeType() != ELEMENT_NODE) || + (newChild.getNodeType() == DOCUMENT_TYPE_NODE && + refChild.getNodeType() != DOCUMENT_TYPE_NODE))) + { + checkNewChild(newChild); + } + return super.replaceChild(newChild, refChild); + } + + // NOTE: DOM can't really tell when the name of an entity, + // notation, or PI must follow the namespace rules (excluding + // colons) instead of the XML rules (which allow them without + // much restriction). That's an API issue. verifyXmlName + // aims to enforce the XML rules, not the namespace rules. + + /** + * Throws a DOM exception if the specified name is not a legal XML 1.0 + * Name. + * @deprecated This method is deprecated and may be removed in future + * versions of GNU JAXP + */ + public static void verifyXmlName(String name) + { + // XXX why is this public? + checkName(name, false); + } + + static void checkName(String name, boolean xml11) + { + if (name == null) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0); + } + int len = name.length(); + if (len == 0) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0); + } + + // dog: rewritten to use the rules for XML 1.0 and 1.1 + + // Name start character + char c = name.charAt(0); + if (xml11) + { + // XML 1.1 + if ((c < 0x0041 || c > 0x005a) && + (c < 0x0061 || c > 0x007a) && + c != ':' && c != '_' && + (c < 0x00c0 || c > 0x00d6) && + (c < 0x00d8 || c > 0x00f6) && + (c < 0x00f8 || c > 0x02ff) && + (c < 0x0370 || c > 0x037d) && + (c < 0x037f || c > 0x1fff) && + (c < 0x200c || c > 0x200d) && + (c < 0x2070 || c > 0x218f) && + (c < 0x2c00 || c > 0x2fef) && + (c < 0x3001 || c > 0xd7ff) && + (c < 0xf900 || c > 0xfdcf) && + (c < 0xfdf0 || c > 0xfffd) && + (c < 0x10000 || c > 0xeffff)) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + } + else + { + // XML 1.0 + int type = Character.getType(c); + switch (type) + { + case Character.LOWERCASE_LETTER: // Ll + case Character.UPPERCASE_LETTER: // Lu + case Character.OTHER_LETTER: // Lo + case Character.TITLECASE_LETTER: // Lt + case Character.LETTER_NUMBER: // Nl + if ((c > 0xf900 && c < 0xfffe) || + (c >= 0x20dd && c <= 0x20e0)) + { + // Compatibility area and Unicode 2.0 exclusions + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + break; + default: + if (c != ':' && c != '_' && (c < 0x02bb || c > 0x02c1) && + c != 0x0559 && c != 0x06e5 && c != 0x06e6) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + } + } + + // Subsequent characters + for (int i = 1; i < len; i++) + { + c = name.charAt(i); + if (xml11) + { + // XML 1.1 + if ((c < 0x0041 || c > 0x005a) && + (c < 0x0061 || c > 0x007a) && + (c < 0x0030 || c > 0x0039) && + c != ':' && c != '_' && c != '-' && c != '.' && + (c < 0x00c0 || c > 0x00d6) && + (c < 0x00d8 || c > 0x00f6) && + (c < 0x00f8 || c > 0x02ff) && + (c < 0x0370 || c > 0x037d) && + (c < 0x037f || c > 0x1fff) && + (c < 0x200c || c > 0x200d) && + (c < 0x2070 || c > 0x218f) && + (c < 0x2c00 || c > 0x2fef) && + (c < 0x3001 || c > 0xd7ff) && + (c < 0xf900 || c > 0xfdcf) && + (c < 0xfdf0 || c > 0xfffd) && + (c < 0x10000 || c > 0xeffff) && + c != 0x00b7 && + (c < 0x0300 || c > 0x036f) && + (c < 0x203f || c > 0x2040)) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name, + null, c); + } + } + else + { + // XML 1.0 + int type = Character.getType(c); + switch (type) + { + case Character.LOWERCASE_LETTER: // Ll + case Character.UPPERCASE_LETTER: // Lu + case Character.DECIMAL_DIGIT_NUMBER: // Nd + case Character.OTHER_LETTER: // Lo + case Character.TITLECASE_LETTER: // Lt + case Character.LETTER_NUMBER: // Nl + case Character.COMBINING_SPACING_MARK: // Mc + case Character.ENCLOSING_MARK: // Me + case Character.NON_SPACING_MARK: // Mn + case Character.MODIFIER_LETTER: // Lm + if ((c > 0xf900 && c < 0xfffe) || + (c >= 0x20dd && c <= 0x20e0)) + { + // Compatibility area and Unicode 2.0 exclusions + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + break; + default: + if (c != '-' && c != '.' && c != ':' && c != '_' && + c != 0x0387 && (c < 0x02bb || c > 0x02c1) && + c != 0x0559 && c != 0x06e5 && c != 0x06e6 && c != 0x00b7) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + } + } + } + + // FIXME characters with a font or compatibility decomposition (i.e. + // those with a "compatibility formatting tag" in field 5 of the + // database -- marked by field 5 beginning with a "<") are not allowed. + } + + // package private + static void checkNCName(String name, boolean xml11) + { + checkName(name, xml11); + int len = name.length(); + int index = name.indexOf(':'); + if (index != -1) + { + if (index == 0 || index == (len - 1) || + name.lastIndexOf(':') != index) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + name, null, 0); + } + } + } + + // package private + static void checkChar(String value, boolean xml11) + { + char[] chars = value.toCharArray(); + checkChar(chars, 0, chars.length, xml11); + } + + static void checkChar(char[] buf, int off, int len, boolean xml11) + { + for (int i = 0; i < len; i++) + { + char c = buf[i]; + + // assume surrogate pairing checks out OK, for simplicity + if ((c >= 0x0020 && c <= 0xd7ff) || + (c == 0x000a || c == 0x000d || c == 0x0009) || + (c >= 0xe000 && c <= 0xfffd) || + (c >= 0x10000 && c <= 0x10ffff)) + { + continue; + } + if (xml11) + { + if ((c >= 0x0001 && c <= 0x001f) || + (c >= 0x007f && c <= 0x0084) || + (c >= 0x0086 && c <= 0x009f)) + { + continue; + } + } + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + new String(buf, off, len), null, c); + } + } + + /** + * <b>DOM L1</b> + * Returns a newly created element with the specified name. + */ + public Element createElement(String name) + { + Element element; + + if (checkingCharacters) + { + checkName(name, "1.1".equals(version)); + } + if (name.startsWith("xml:")) + { + element = createElementNS(null, name); + } + else + { + DomElement domElement = new DomElement(this, null, name); + domElement.localName = null; + element = domElement; + } + defaultAttributes(element, name); + return element; + } + + /** + * <b>DOM L2</b> + * Returns a newly created element with the specified name + * and namespace information. + */ + public Element createElementNS(String namespaceURI, String name) + { + if (checkingCharacters) + { + checkNCName(name, "1.1".equals(version)); + } + + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + if (name.startsWith("xml:")) + { + if (namespaceURI != null + && !XMLConstants.XML_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace is always " + + XMLConstants.XML_NS_URI, this, 0); + } + namespaceURI = XMLConstants.XML_NS_URI; + } + else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:")) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns is reserved", this, 0); + } + else if (namespaceURI == null && name.indexOf(':') != -1) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "prefixed name '" + name + + "' needs a URI", this, 0); + } + + Element element = new DomElement(this, namespaceURI, name); + defaultAttributes(element, name); + return element; + } + + private void defaultAttributes(Element element, String name) + { + DomDoctype doctype = (DomDoctype) getDoctype(); + if (doctype == null) + { + return; + } + + // default any attributes that need it + DTDElementTypeInfo info = doctype.getElementTypeInfo(name); + if (info != null) + { + for (Iterator i = info.attributes(); i != null && i.hasNext(); ) + { + DTDAttributeTypeInfo attr = (DTDAttributeTypeInfo) i.next(); + DomAttr node = (DomAttr) createAttribute(attr.name); + + String value = attr.value; + if (value == null) + { + value = ""; + } + node.setValue(value); + node.setSpecified(false); + element.setAttributeNode(node); + } + } + } + + /** + * <b>DOM L1</b> + * Returns a newly created document fragment. + */ + public DocumentFragment createDocumentFragment() + { + return new DomDocumentFragment(this); + } + + /** + * <b>DOM L1</b> + * Returns a newly created text node with the specified value. + */ + public Text createTextNode(String value) + { + if (checkingCharacters) + { + checkChar(value, "1.1".equals(version)); + } + return new DomText(this, value); + } + + /** + * Returns a newly created text node with the specified value. + */ + public Text createTextNode(char[] buf, int off, int len) + { + if (checkingCharacters) + { + checkChar(buf, off, len, "1.1".equals(version)); + } + return new DomText(this, buf, off, len); + } + + /** + * <b>DOM L1</b> + * Returns a newly created comment node with the specified value. + */ + public Comment createComment(String value) + { + if (checkingCharacters) + { + checkChar(value, "1.1".equals(version)); + } + return new DomComment(this, value); + } + + /** + * <b>DOM L1</b> + * Returns a newly created CDATA section node with the specified value. + */ + public CDATASection createCDATASection(String value) + { + if (checkingCharacters) + { + checkChar(value, "1.1".equals(version)); + } + return new DomCDATASection(this, value); + } + + /** + * Returns a newly created CDATA section node with the specified value. + */ + public CDATASection createCDATASection(char[] buf, int off, int len) + { + if (checkingCharacters) + { + checkChar(buf, off, len, "1.1".equals(version)); + } + return new DomCDATASection(this, buf, off, len); + } + + /** + * <b>DOM L1</b> + * Returns a newly created processing instruction. + */ + public ProcessingInstruction createProcessingInstruction(String target, + String data) + { + if (checkingCharacters) + { + boolean xml11 = "1.1".equals(version); + checkName(target, xml11); + if ("xml".equalsIgnoreCase(target)) + { + throw new DomDOMException(DOMException.SYNTAX_ERR, + "illegal PI target name", + this, 0); + } + checkChar(data, xml11); + } + return new DomProcessingInstruction(this, target, data); + } + + /** + * <b>DOM L1</b> + * Returns a newly created attribute with the specified name. + */ + public Attr createAttribute(String name) + { + if (checkingCharacters) + { + checkName(name, "1.1".equals(version)); + } + if (name.startsWith("xml:")) + { + return createAttributeNS(XMLConstants.XML_NS_URI, name); + } + else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:")) + { + return createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, name); + } + else + { + DomAttr ret = new DomAttr(this, null, name); + ret.localName = null; + return ret; + } + } + + /** + * <b>DOM L2</b> + * Returns a newly created attribute with the specified name + * and namespace information. + */ + public Attr createAttributeNS(String namespaceURI, String name) + { + if (checkingCharacters) + { + checkNCName(name, "1.1".equals(version)); + } + + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + if (name.startsWith ("xml:")) + { + if (namespaceURI == null) + { + namespaceURI = XMLConstants.XML_NS_URI; + } + else if (!XMLConstants.XML_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace is always " + + XMLConstants.XML_NS_URI, + this, 0); + } + } + else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:")) + { + if (namespaceURI == null) + { + namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + } + else if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns namespace must be " + + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, + this, 0); + } + } + else if (namespaceURI == null && name.indexOf(':') != -1) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "prefixed name needs a URI: " + name, this, 0); + } + return new DomAttr(this, namespaceURI, name); + } + + /** + * <b>DOM L1</b> + * Returns a newly created reference to the specified entity. + * The caller should populate this with the appropriate children + * and then mark it as readonly. + * + * @see DomNode#makeReadonly + */ + public EntityReference createEntityReference(String name) + { + DomEntityReference ret = new DomEntityReference(this, name); + DocumentType doctype = getDoctype(); + if (doctype != null) + { + DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name); + if (ent != null) + { + for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next) + { + ret.appendChild(ctx.cloneNode(true)); + } + } + } + ret.makeReadonly(); + return ret; + } + + /** + * <b>DOM L2</b> + * Makes a copy of the specified node, with all nodes "owned" by + * this document and with children optionally copied. This type + * of standard utility has become, well, a standard utility. + * + * <p> Note that EntityReference nodes created through this method (either + * directly, or recursively) never have children, and that there is no + * portable way to associate them with such children. + * + * <p> Note also that there is no requirement that the specified node + * be associated with a different document. This differs from the + * <em>cloneNode</em> operation in that the node itself is not given + * an opportunity to participate, so that any information managed + * by node subclasses will be lost. + */ + public Node importNode(Node src, boolean deep) + { + Node dst = null; + switch (src.getNodeType()) + { + case TEXT_NODE: + dst = createTextNode(src.getNodeValue()); + break; + case CDATA_SECTION_NODE: + dst = createCDATASection(src.getNodeValue()); + break; + case COMMENT_NODE: + dst = createComment(src.getNodeValue()); + break; + case PROCESSING_INSTRUCTION_NODE: + dst = createProcessingInstruction(src.getNodeName(), + src.getNodeValue()); + break; + case NOTATION_NODE: + // NOTE: There's no standard way to create + // these, or add them to a doctype. Useless. + Notation notation = (Notation) src; + dst = new DomNotation(this, notation.getNodeName(), + notation.getPublicId(), + notation.getSystemId()); + break; + case ENTITY_NODE: + // NOTE: There's no standard way to create + // these, or add them to a doctype. Useless. + Entity entity = (Entity) src; + dst = new DomEntity(this, entity.getNodeName(), + entity.getPublicId(), + entity.getSystemId(), + entity.getNotationName()); + if (deep) + { + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, deep)); + } + } + break; + case ENTITY_REFERENCE_NODE: + dst = createEntityReference(src.getNodeName()); + break; + case DOCUMENT_FRAGMENT_NODE: + dst = new DomDocumentFragment(this); + if (deep) + { + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, deep)); + } + } + break; + case ATTRIBUTE_NODE: + String attr_nsuri = src.getNamespaceURI(); + if (attr_nsuri != null) + { + dst = createAttributeNS(attr_nsuri, src.getNodeName()); + } + else + { + dst = createAttribute(src.getNodeName()); + } + // this is _always_ done regardless of "deep" setting + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, false)); + } + break; + case ELEMENT_NODE: + String elem_nsuri = src.getNamespaceURI(); + if (elem_nsuri != null) + { + dst = createElementNS(elem_nsuri, src.getNodeName()); + } + else + { + dst = createElement(src.getNodeName()); + } + NamedNodeMap srcAttrs = src.getAttributes(); + NamedNodeMap dstAttrs = dst.getAttributes(); + int len = srcAttrs.getLength(); + for (int i = 0; i < len; i++) + { + Attr a = (Attr) srcAttrs.item(i); + Attr dflt; + + // maybe update defaulted attributes + dflt = (Attr) dstAttrs.getNamedItem(a.getNodeName()); + if (dflt != null) + { + String newval = a.getNodeValue(); + if (!dflt.getNodeValue().equals(newval) + || a.getSpecified () == true) + { + dflt.setNodeValue (newval); + } + continue; + } + + dstAttrs.setNamedItem((Attr) importNode(a, false)); + } + if (deep) + { + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, true)); + } + } + break; + // can't import document or doctype nodes + case DOCUMENT_NODE: + case DOCUMENT_TYPE_NODE: + // FALLTHROUGH + // can't import unrecognized or nonstandard nodes + default: + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, src, 0); + } + + // FIXME cleanup a bit -- for deep copies, copy those + // children in one place, here (code sharing is healthy) + + if (src instanceof DomNode) + { + ((DomNode) src).notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED, + src, dst); + } + return dst; + } + + /** + * <b>DOM L2 (Traversal)</b> + * Returns a newly created node iterator. Don't forget to detach + * this iterator when you're done using it! + * + * @see DomIterator + */ + public NodeIterator createNodeIterator(Node root, + int whatToShow, + NodeFilter filter, + boolean expandEntities) + { + return new DomNodeIterator(root, whatToShow, filter, expandEntities, + false); + } + + public TreeWalker createTreeWalker(Node root, + int whatToShow, + NodeFilter filter, + boolean expandEntities) + { + return new DomNodeIterator(root, whatToShow, filter, expandEntities, + true); + } + + // DOM Level 3 methods + + /** + * DOM L3 + */ + public String getInputEncoding() + { + return inputEncoding; + } + + public void setInputEncoding(String inputEncoding) + { + this.inputEncoding = inputEncoding; + } + + /** + * DOM L3 + */ + public String getXmlEncoding() + { + return encoding; + } + + public void setXmlEncoding(String encoding) + { + this.encoding = encoding; + } + + public boolean getXmlStandalone() + { + return standalone; + } + + public void setXmlStandalone(boolean xmlStandalone) + { + standalone = xmlStandalone; + } + + public String getXmlVersion() + { + return version; + } + + public void setXmlVersion(String xmlVersion) + { + if (xmlVersion == null) + { + xmlVersion = "1.0"; + } + if ("1.0".equals(xmlVersion) || + "1.1".equals(xmlVersion)) + { + version = xmlVersion; + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + } + + public boolean getStrictErrorChecking() + { + return checkingCharacters; + } + + public void setStrictErrorChecking(boolean strictErrorChecking) + { + checkingCharacters = strictErrorChecking; + } + + public String lookupPrefix(String namespaceURI) + { + Node root = getDocumentElement(); + return (root == null) ? null : root.lookupPrefix(namespaceURI); + } + + public boolean isDefaultNamespace(String namespaceURI) + { + Node root = getDocumentElement(); + return (root == null) ? false : root.isDefaultNamespace(namespaceURI); + } + + public String lookupNamespaceURI(String prefix) + { + Node root = getDocumentElement(); + return (root == null) ? null : root.lookupNamespaceURI(prefix); + } + + public String getBaseURI() + { + return getDocumentURI(); + /* + Node root = getDocumentElement(); + if (root != null) + { + NamedNodeMap attrs = root.getAttributes(); + Node xmlBase = attrs.getNamedItemNS(XMLConstants.XML_NS_URI, "base"); + if (xmlBase != null) + { + return xmlBase.getNodeValue(); + } + } + return systemId; + */ + } + + public String getDocumentURI() + { + return systemId; + } + + public void setDocumentURI(String documentURI) + { + systemId = documentURI; + } + + public Node adoptNode(Node source) + { + int sourceNodeType = source.getNodeType(); + switch (sourceNodeType) + { + case DOCUMENT_NODE: + case DOCUMENT_TYPE_NODE: + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + case ENTITY_NODE: + case NOTATION_NODE: + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (source instanceof DomNode) + { + // GNU native + DomNode src = (DomNode) source; + DomNode dst = src; + if (dst.parent != null) + { + dst = (DomNode) dst.cloneNode(true); + } + dst.setOwner(this); + src.notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, src, dst); + return dst; + } + else + { + // Some other implementation + Node dst = null; + switch (sourceNodeType) + { + case Node.ATTRIBUTE_NODE: + { + Attr src = (Attr) source; + String nodeName = src.getNodeName(); + String localName = src.getLocalName(); + String namespaceUri = src.getNamespaceURI(); + dst = (localName == null) ? + createAttribute(nodeName) : + createAttributeNS(namespaceUri, nodeName); + adoptChildren(src, dst); + break; + } + case Node.CDATA_SECTION_NODE: + { + CDATASection src = (CDATASection) source; + dst = createCDATASection(src.getData()); + break; + } + case Node.COMMENT_NODE: + { + Comment src = (Comment) source; + dst = createComment(src.getData()); + break; + } + case Node.DOCUMENT_FRAGMENT_NODE: + { + DocumentFragment src = (DocumentFragment) source; + dst = createDocumentFragment(); + adoptChildren(src, dst); + break; + } + case Node.ELEMENT_NODE: + { + Element src = (Element) source; + String nodeName = src.getNodeName(); + String localName = src.getLocalName(); + String namespaceUri = src.getNamespaceURI(); + dst = (localName == null) ? + createElement(nodeName) : + createElementNS(namespaceUri, nodeName); + adoptAttributes(src, dst); + adoptChildren(src, dst); + break; + } + case Node.ENTITY_REFERENCE_NODE: + { + EntityReference src = (EntityReference) source; + dst = createEntityReference(src.getNodeName()); + adoptChildren(src, dst); + break; + } + case Node.PROCESSING_INSTRUCTION_NODE: + { + ProcessingInstruction src = (ProcessingInstruction) source; + dst = createProcessingInstruction(src.getTarget(), + src.getData()); + break; + } + case Node.TEXT_NODE: + { + Text src = (Text) source; + dst = createTextNode(src.getData()); + break; + } + } + return dst; + } + } + + void adoptChildren(Node src, Node dst) + { + Node node = src.getFirstChild(); + while (node != null) + { + Node next = node.getNextSibling(); + dst.appendChild(adoptNode(node)); + node = next; + } + } + + void adoptAttributes(Node src, Node dst) + { + NamedNodeMap srcAttrs = src.getAttributes(); + NamedNodeMap dstAttrs = dst.getAttributes(); + int len = srcAttrs.getLength(); + for (int i = 0; i < len; i++) + { + Node node = srcAttrs.item(i); + String localName = node.getLocalName(); + if (localName == null) + { + dstAttrs.setNamedItem(adoptNode(node)); + } + else + { + dstAttrs.setNamedItemNS(adoptNode(node)); + } + } + } + + public DOMConfiguration getDomConfig() + { + if (config == null) + { + config = new DomDocumentConfiguration(); + } + return config; + } + + public void normalizeDocument() + { + boolean save = building; + building = true; + normalizeNode(this); + building = save; + } + + void normalizeNode(DomNode node) + { + node.normalize(); + if (config != null) + { + switch (node.nodeType) + { + case CDATA_SECTION_NODE: + if (!config.cdataSections) + { + // replace CDATA section with text node + Text text = createTextNode(node.getNodeValue()); + node.parent.insertBefore(text, node); + node.parent.removeChild(node); + // merge adjacent text nodes + String data = text.getWholeText(); + node = (DomNode) text.replaceWholeText(data); + } + else if (config.splitCdataSections) + { + String value = node.getNodeValue(); + int i = value.indexOf("]]>"); + while (i != -1) + { + Node node2 = createCDATASection(value.substring(0, i)); + node.parent.insertBefore(node2, node); + value = value.substring(i + 3); + node.setNodeValue(value); + i = value.indexOf("]]>"); + } + } + break; + case COMMENT_NODE: + if (!config.comments) + { + node.parent.removeChild(node); + } + break; + case TEXT_NODE: + if (!config.elementContentWhitespace && + ((Text) node).isElementContentWhitespace()) + { + node.parent.removeChild(node); + } + break; + case ENTITY_REFERENCE_NODE: + if (!config.entities) + { + for (DomNode ctx = node.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + node.parent.insertBefore(ctx, node); + ctx = ctxNext; + } + node.parent.removeChild(node); + } + break; + case ELEMENT_NODE: + if (!config.namespaceDeclarations) + { + DomNamedNodeMap attrs = + (DomNamedNodeMap) node.getAttributes(); + boolean aro = attrs.readonly; + attrs.readonly = false; // Ensure we can delete if necessary + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String namespace = attr.getNamespaceURI(); + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace)) + { + attrs.removeNamedItemNS(namespace, + attr.getLocalName()); + i--; + len--; + } + } + attrs.readonly = aro; + } + break; + } + } + for (DomNode ctx = node.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + normalizeNode(ctx); + ctx = ctxNext; + } + } + + public Node renameNode(Node n, String namespaceURI, String qualifiedName) + throws DOMException + { + if (n instanceof DomNsNode) + { + DomNsNode src = (DomNsNode) n; + if (src == null) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + if (src.owner != this) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, src, 0); + } + boolean xml11 = "1.1".equals(version); + checkName(qualifiedName, xml11); + int ci = qualifiedName.indexOf(':'); + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + if (namespaceURI != null) + { + checkNCName(qualifiedName, xml11); + String prefix = (ci == -1) ? "" : + qualifiedName.substring(0, ci); + if (XMLConstants.XML_NS_PREFIX.equals(prefix) && + !XMLConstants.XML_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace must be " + + XMLConstants.XML_NS_URI, src, 0); + } + else if (src.nodeType == ATTRIBUTE_NODE && + (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || + XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)) && + !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns namespace must be " + + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0); + } + if (XMLConstants.XML_NS_URI.equals(namespaceURI) && + !XMLConstants.XML_NS_PREFIX.equals(prefix)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace must be " + + XMLConstants.XML_NS_URI, src, 0); + } + else if (src.nodeType == ATTRIBUTE_NODE && + XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI) && + !(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || + XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName))) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns namespace must be " + + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0); + } + + } + src.setNodeName(qualifiedName); + src.setNamespaceURI(namespaceURI); + src.notifyUserDataHandlers(UserDataHandler.NODE_RENAMED, src, src); + // TODO MutationNameEvents + // DOMElementNameChanged or DOMAttributeNameChanged + return src; + } + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, n, 0); + } + + // -- XPathEvaluator -- + + public XPathExpression createExpression(String expression, + XPathNSResolver resolver) + throws XPathException, DOMException + { + return new DomXPathExpression(this, expression, resolver); + } + + public XPathNSResolver createNSResolver(Node nodeResolver) + { + return new DomXPathNSResolver(nodeResolver); + } + + public Object evaluate(String expression, + Node contextNode, + XPathNSResolver resolver, + short type, + Object result) + throws XPathException, DOMException + { + XPathExpression xpe = + new DomXPathExpression(this, expression, resolver); + return xpe.evaluate(contextNode, type, result); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java b/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java new file mode 100644 index 00000000000..42444e86515 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java @@ -0,0 +1,171 @@ +/* DomDocumentBuilder.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.io.InputStream; +import java.io.IOException; +import java.io.Reader; +import java.net.URL; +import javax.xml.parsers.DocumentBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSParser; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Document builder using the GNU DOM Load & Save implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomDocumentBuilder + extends DocumentBuilder +{ + + final DOMImplementation impl; + final DOMImplementationLS ls; + final LSParser parser; + + DomDocumentBuilder(DOMImplementation impl, + DOMImplementationLS ls, + LSParser parser) + { + this.impl = impl; + this.ls = ls; + this.parser = parser; + } + + public boolean isNamespaceAware() + { + DOMConfiguration config = parser.getDomConfig(); + return ((Boolean) config.getParameter("namespaces")).booleanValue(); + } + + public boolean isValidating() + { + DOMConfiguration config = parser.getDomConfig(); + return ((Boolean) config.getParameter("validating")).booleanValue(); + } + + public boolean isXIncludeAware() + { + DOMConfiguration config = parser.getDomConfig(); + return ((Boolean) config.getParameter("xinclude-aware")).booleanValue(); + } + + public void setEntityResolver(EntityResolver resolver) + { + DOMConfiguration config = parser.getDomConfig(); + config.setParameter("entity-resolver", resolver); + } + + public void setErrorHandler(ErrorHandler handler) + { + DOMConfiguration config = parser.getDomConfig(); + config.setParameter("error-handler", handler); + } + + public DOMImplementation getDOMImplementation() + { + return impl; + } + + public Document newDocument() + { + return impl.createDocument(null, null, null); + } + + public Document parse(InputStream in) + throws SAXException, IOException + { + LSInput input = ls.createLSInput(); + input.setByteStream(in); + return parser.parse(input); + } + + public Document parse(InputStream in, String systemId) + throws SAXException, IOException + { + LSInput input = ls.createLSInput(); + input.setByteStream(in); + input.setSystemId(systemId); + return parser.parse(input); + } + + public Document parse(String systemId) + throws SAXException, IOException + { + return parser.parseURI(systemId); + } + + public Document parse(InputSource is) + throws SAXException, IOException + { + LSInput input = ls.createLSInput(); + String systemId = is.getSystemId(); + InputStream in = is.getByteStream(); + if (in != null) + { + input.setByteStream(in); + } + else + { + Reader reader = is.getCharacterStream(); + if (reader != null) + { + input.setCharacterStream(reader); + } + else + { + URL url = new URL(systemId); + input.setByteStream(url.openStream()); + } + } + input.setPublicId(is.getPublicId()); + input.setSystemId(systemId); + input.setEncoding(is.getEncoding()); + return parser.parse(input); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java b/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java new file mode 100644 index 00000000000..814141c9441 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java @@ -0,0 +1,128 @@ +/* DomDocumentBuilderFactory.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSParser; + +/** + * Document builder factory that uses a DOM Level 3 Load & Save + * implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomDocumentBuilderFactory + extends DocumentBuilderFactory +{ + + final DOMImplementation impl; + final DOMImplementationLS ls; + + public DomDocumentBuilderFactory() + { + try + { + DOMImplementationRegistry reg = + DOMImplementationRegistry.newInstance(); + impl = reg.getDOMImplementation("LS 3.0"); + if (impl == null) + { + throw new FactoryConfigurationError("no LS implementations found"); + } + ls = (DOMImplementationLS) impl; + } + catch (Exception e) + { + throw new FactoryConfigurationError(e); + } + } + + public DocumentBuilder newDocumentBuilder() + throws ParserConfigurationException + { + LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS, + "http://www.w3.org/TR/REC-xml"); + DOMConfiguration config = parser.getDomConfig(); + setParameter(config, "namespaces", + isNamespaceAware() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "element-content-whitespace", + isIgnoringElementContentWhitespace() ? Boolean.FALSE : + Boolean.TRUE); + setParameter(config, "comments", + isIgnoringComments() ? Boolean.FALSE : Boolean.TRUE); + setParameter(config, "expand-entity-references", + isExpandEntityReferences() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "coalescing", + isCoalescing() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "validating", + isValidating() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "xinclude-aware", + isXIncludeAware() ? Boolean.TRUE : Boolean.FALSE); + return new DomDocumentBuilder(impl, ls, parser); + } + + void setParameter(DOMConfiguration config, String name, Object value) + throws ParserConfigurationException + { + if (!config.canSetParameter(name, value)) + { + throw new ParserConfigurationException(name); + } + config.setParameter(name, value); + } + + public Object getAttribute(String name) + { + // TODO + return null; + } + + public void setAttribute(String name, Object value) + { + // TODO + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java b/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java new file mode 100644 index 00000000000..5c589f82fe2 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java @@ -0,0 +1,260 @@ +/* DomDocumentConfiguration.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.Arrays; +import java.util.List; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMErrorHandler; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMStringList; + +/** + * Document configuration, used to store normalization and other parameters. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomDocumentConfiguration + implements DOMConfiguration, DOMStringList +{ + + private static final List SUPPORTED_PARAMETERS = + Arrays.asList(new String[] { "cdata-sections", + "comments", + "element-content-whitespace", + "entities", + "error-handler", + "namespace-declarations", + "split-cdata-sections", + "infoset"}); + + boolean cdataSections = true; + boolean comments = true; + boolean elementContentWhitespace = true; + boolean entities = true; + DOMErrorHandler errorHandler; + boolean namespaceDeclarations = true; + boolean splitCdataSections = true; + + public void setParameter(String name, Object value) + throws DOMException + { + name = name.toLowerCase(); + if ("cdata-sections".equals(name)) + { + cdataSections = "true".equals(value.toString()); + } + else if ("comments".equals(name)) + { + comments = "true".equals(value.toString()); + } + else if ("element-content-whitespace".equals(name)) + { + elementContentWhitespace = "true".equals(value.toString()); + } + else if ("entities".equals(name)) + { + entities = "true".equals(value.toString()); + } + else if ("error-handler".equals(name)) + { + try + { + errorHandler = (DOMErrorHandler) value; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.TYPE_MISMATCH_ERR, + value.getClass().getName(), null, 0); + } + } + else if ("namespace-declarations".equals(name)) + { + namespaceDeclarations = "true".equals(value.toString()); + } + else if ("split-cdata-sections".equals(name)) + { + comments = "true".equals(value.toString()); + } + else if ("infoset".equals(name)) + { + if ("true".equals(value.toString())) + { + entities = false; + cdataSections = false; + namespaceDeclarations = true; + elementContentWhitespace = true; + comments = true; + } + } + else if (("canonical-form".equals(name) || + "check-character-normalization".equals(name) || + "datatype-normalization".equals(name) || + "normalize-characters".equals(name) || + "validate".equals(name) || + "validate-if-schema".equals(name)) && + "false".equals(value.toString())) + { + // NOOP + } + else if (("namespaces".equals(name) || + "well-formed".equals(name)) && + "true".equals(value.toString())) + { + // NOOP + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, + name, null, 0); + } + } + + public Object getParameter(String name) + throws DOMException + { + name = name.toLowerCase(); + if ("cdata-sections".equals(name)) + { + return cdataSections ? Boolean.TRUE : Boolean.FALSE; + } + else if ("comments".equals(name)) + { + return comments ? Boolean.TRUE : Boolean.FALSE; + } + else if ("element-content-whitespace".equals(name)) + { + return elementContentWhitespace ? Boolean.TRUE : Boolean.FALSE; + } + else if ("entities".equals(name)) + { + return entities ? Boolean.TRUE : Boolean.FALSE; + } + else if ("error-handler".equals(name)) + { + return errorHandler; + } + else if ("namespace-declarations".equals(name)) + { + return namespaceDeclarations ? Boolean.TRUE : Boolean.FALSE; + } + else if ("split-cdata-sections".equals(name)) + { + return comments ? Boolean.TRUE : Boolean.FALSE; + } + else if ("canonical-form".equals(name) || + "check-character-normalization".equals(name) || + "datatype-normalization".equals(name) || + "normalize-characters".equals(name) || + "validate".equals(name) || + "validate-if-schema".equals(name)) + { + return Boolean.FALSE; + } + else if ("namespaces".equals(name) || + "well-formed".equals(name)) + { + return Boolean.TRUE; + } + else if ("infoset".equals(name)) + { + return (entities == false && + cdataSections == false && + namespaceDeclarations == true && + comments == true) ? Boolean.TRUE : Boolean.FALSE; + } + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, name, null, 0); + } + + public boolean canSetParameter(String name, Object value) + { + name = name.toLowerCase(); + if ("error-handler".equals(name)) + { + return (value == null || value instanceof DOMErrorHandler); + } + else if (contains(name)) + { + return true; + } + else if ("canonical-form".equals(name) || + "check-character-normalization".equals(name) || + "datatype-normalization".equals(name) || + "normalize-characters".equals(name) || + "validate".equals(name) || + "validate-if-schema".equals(name)) + { + return "false".equals(value.toString()); + } + else if ("namespaces".equals(name) || + "well-formed".equals(name)) + { + return "true".equals(value.toString()); + } + return false; + } + + public DOMStringList getParameterNames() + { + return this; + } + + public String item(int i) + { + try + { + return (String) SUPPORTED_PARAMETERS.get(i); + } + catch (IndexOutOfBoundsException e) + { + return null; + } + } + + public int getLength() + { + return SUPPORTED_PARAMETERS.size(); + } + + public boolean contains(String str) + { + str = str.toLowerCase(); + return SUPPORTED_PARAMETERS.contains(str); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java b/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java new file mode 100644 index 00000000000..8d260377821 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java @@ -0,0 +1,76 @@ +/* DomDocumentFragment.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DocumentFragment; + +/** + * <p> "DocumentFragment" implementation. </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomDocumentFragment + extends DomNode + implements DocumentFragment +{ + + /** + * Constructs a DocumentFragment node associated with the + * specified document. + * + * <p>This constructor should only be invoked by a Document as part of + * its createDocumentFragment functionality, or through a subclass which + * is similarly used in a "Sub-DOM" style layer. + */ + protected DomDocumentFragment(DomDocument owner) + { + super(DOCUMENT_FRAGMENT_NODE, owner); + } + + /** + * <b>DOM L1</b> + * Returns the string "#document-fragment". + */ + final public String getNodeName() + { + return "#document-fragment"; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomElement.java b/libjava/classpath/gnu/xml/dom/DomElement.java new file mode 100644 index 00000000000..34509f647bc --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomElement.java @@ -0,0 +1,523 @@ +/* DomElement.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashSet; +import java.util.Set; +import javax.xml.XMLConstants; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.TypeInfo; + +/** + * <p> "Element" implementation. + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomElement + extends DomNsNode + implements Element +{ + + /** + * User-defined ID attributes. + * Used by DomAttr.isId and DomDocument.getElementById + */ + Set userIdAttrs; + + // Attributes are VERY expensive in DOM, and not just for + // this implementation. Avoid creating them. + private DomNamedNodeMap attributes; + + // xml:space cache + String xmlSpace = ""; + + /** + * Constructs an Element node associated with the specified document. + * + * <p>This constructor should only be invoked by a Document as part + * of its createElement functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this node is associated + * @param namespaceURI Combined with the local part of the name, + * this is used to uniquely identify a type of element + * @param name Name of this element, which may include a prefix + */ + protected DomElement(DomDocument owner, String namespaceURI, String name) + { + super(ELEMENT_NODE, owner, namespaceURI, name); + } + + /** + * <b>DOM L1</b> + * Returns the element's attributes + */ + public NamedNodeMap getAttributes() + { + if (attributes == null) + { + attributes = new DomNamedNodeMap(this, Node.ATTRIBUTE_NODE); + } + return attributes; + } + + /** + * <b>DOM L2></b> + * Returns true iff this is an element node with attributes. + */ + public boolean hasAttributes() + { + return attributes != null && attributes.length != 0; + } + + /** + * Shallow clone of the element, except that associated + * attributes are (deep) cloned. + */ + public Object clone() + { + DomElement node = (DomElement) super.clone(); + + if (attributes != null) + { + node.attributes = new DomNamedNodeMap(node, Node.ATTRIBUTE_NODE); + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + node.attributes.setNamedItemNS(ctx.cloneNode(true)); + } + } + return node; + } + + void setOwner(DomDocument doc) + { + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + ctx.setOwner(doc); + } + } + super.setOwner(doc); + } + + /** + * Marks this element, its children, and its associated attributes as + * readonly. + */ + public void makeReadonly() + { + super.makeReadonly(); + if (attributes != null) + { + attributes.makeReadonly(); + } + } + + /** + * <b>DOM L1</b> + * Returns the element name (same as getNodeName). + */ + final public String getTagName() + { + return getNodeName(); + } + + /** + * <b>DOM L1</b> + * Returns the value of the specified attribute, or an + * empty string. + */ + public String getAttribute(String name) + { + if ("xml:space" == name) // NB only works on interned string + { + // Use cached value + return xmlSpace; + } + Attr attr = getAttributeNode(name); + return (attr == null) ? "" : attr.getValue(); + } + + /** + * <b>DOM L2</b> + * Returns true if the element has an attribute with the + * specified name (specified or DTD defaulted). + */ + public boolean hasAttribute(String name) + { + return getAttributeNode(name) != null; + } + + /** + * <b>DOM L2</b> + * Returns true if the element has an attribute with the + * specified name (specified or DTD defaulted). + */ + public boolean hasAttributeNS(String namespaceURI, String local) + { + return getAttributeNodeNS(namespaceURI, local) != null; + } + + /** + * <b>DOM L2</b> + * Returns the value of the specified attribute, or an + * empty string. + */ + public String getAttributeNS(String namespaceURI, String local) + { + Attr attr = getAttributeNodeNS(namespaceURI, local); + return (attr == null) ? "" : attr.getValue(); + } + + /** + * <b>DOM L1</b> + * Returns the appropriate attribute node; the name is the + * nodeName property of the attribute. + */ + public Attr getAttributeNode(String name) + { + return (attributes == null) ? null : + (Attr) attributes.getNamedItem(name); + } + + /** + * <b>DOM L2</b> + * Returns the appropriate attribute node; the name combines + * the namespace name and the local part. + */ + public Attr getAttributeNodeNS(String namespace, String localPart) + { + return (attributes == null) ? null : + (Attr) attributes.getNamedItemNS(namespace, localPart); + } + + /** + * <b>DOM L1</b> + * Modifies an existing attribute to have the specified value, + * or creates a new one with that value. The name used is the + * nodeName value. + */ + public void setAttribute(String name, String value) + { + Attr attr = getAttributeNode(name); + if (attr != null) + { + attr.setNodeValue(value); + ((DomAttr) attr).setSpecified(true); + return; + } + attr = owner.createAttribute(name); + attr.setNodeValue(value); + setAttributeNode(attr); + } + + /** + * <b>DOM L2</b> + * Modifies an existing attribute to have the specified value, + * or creates a new one with that value. + */ + public void setAttributeNS(String uri, String aname, String value) + { + if (("xmlns".equals (aname) || aname.startsWith ("xmlns:")) + && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (uri)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "setting xmlns attribute to illegal value", this, 0); + } + + Attr attr = getAttributeNodeNS(uri, aname); + if (attr != null) + { + attr.setNodeValue(value); + return; + } + attr = owner.createAttributeNS(uri, aname); + attr.setNodeValue(value); + setAttributeNodeNS(attr); + } + + /** + * <b>DOM L1</b> + * Stores the specified attribute, optionally overwriting any + * existing one with that name. + */ + public Attr setAttributeNode(Attr attr) + { + return (Attr) getAttributes().setNamedItem(attr); + } + + /** + * <b>DOM L2</b> + * Stores the specified attribute, optionally overwriting any + * existing one with that name. + */ + public Attr setAttributeNodeNS(Attr attr) + { + return (Attr) getAttributes().setNamedItemNS(attr); + } + + /** + * <b>DOM L1</b> + * Removes the appropriate attribute node. + * If there is no such node, this is (bizarrely enough) a NOP so you + * won't see exceptions if your code deletes non-existent attributes. + * + * <p>Note that since there is no portable way for DOM to record + * DTD information, default values for attributes will never be + * provided automatically. + */ + public void removeAttribute(String name) + { + if (attributes == null) + { + return; + } + + try + { + attributes.removeNamedItem(name); + } + catch (DomDOMException e) + { + if (e.code != DOMException.NOT_FOUND_ERR) + { + throw e; + } + } + } + + /** + * <b>DOM L1</b> + * Removes the appropriate attribute node; the name is the + * nodeName property of the attribute. + * + * <p>Note that since there is no portable way for DOM to record + * DTD information, default values for attributes will never be + * provided automatically. + */ + public Attr removeAttributeNode(Attr node) + { + if (attributes == null) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, null, node, 0); + } + return (Attr) attributes.removeNamedItem(node.getNodeName()); + } + + /** + * <b>DOM L2</b> + * Removes the appropriate attribute node; the name combines + * the namespace name and the local part. + * + * <p>Note that since there is no portable way for DOM to record + * DTD information, default values for attributes will never be + * provided automatically. + */ + public void removeAttributeNS(String namespace, String localPart) + { + if (attributes == null) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, localPart, null, 0); + } + attributes.removeNamedItemNS (namespace, localPart); + } + + // DOM Level 3 methods + + public String lookupPrefix(String namespaceURI) + { + if (namespaceURI == null) + { + return null; + } + String namespace = getNamespaceURI(); + if (namespace != null && namespace.equals(namespaceURI)) + { + return getPrefix(); + } + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + { + String value = ctx.getNodeValue(); + if (value.equals(namespaceURI)) + { + return ctx.getLocalName(); + } + } + } + } + return super.lookupPrefix(namespaceURI); + } + + public boolean isDefaultNamespace(String namespaceURI) + { + String namespace = getNamespaceURI(); + if (namespace != null && namespace.equals(namespaceURI)) + { + return getPrefix() == null; + } + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + { + String qName = ctx.getNodeName(); + return (XMLConstants.XMLNS_ATTRIBUTE.equals(qName)); + } + } + } + return super.isDefaultNamespace(namespaceURI); + } + + public String lookupNamespaceURI(String prefix) + { + String namespace = getNamespaceURI(); + if (namespace != null && equal(prefix, getPrefix())) + { + return namespace; + } + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + { + if (prefix == null) + { + if (XMLConstants.XMLNS_ATTRIBUTE.equals(ctx.getNodeName())) + { + return ctx.getNodeValue(); + } + } + else + { + if (prefix.equals(ctx.getLocalName())) + { + return ctx.getNodeValue(); + } + } + } + } + } + return super.lookupNamespaceURI(prefix); + } + + public String getBaseURI() + { + if (attributes != null) + { + Node xmlBase = + attributes.getNamedItemNS(XMLConstants.XML_NS_URI, "base"); + if (xmlBase != null) + { + return xmlBase.getNodeValue(); + } + } + return super.getBaseURI(); + } + + public TypeInfo getSchemaTypeInfo() + { + // DTD implementation + DomDoctype doctype = (DomDoctype) owner.getDoctype(); + if (doctype != null) + { + return doctype.getElementTypeInfo(getNodeName()); + } + // TODO XML Schema implementation + return null; + } + + public void setIdAttribute(String name, boolean isId) + { + NamedNodeMap attrs = getAttributes(); + Attr attr = (Attr) attrs.getNamedItem(name); + setIdAttributeNode(attr, isId); + } + + public void setIdAttributeNode(Attr attr, boolean isId) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (attr == null || attr.getOwnerElement() != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + if (isId) + { + if (userIdAttrs == null) + { + userIdAttrs = new HashSet(); + } + userIdAttrs.add(attr); + } + else if (userIdAttrs != null) + { + userIdAttrs.remove(attr); + if (userIdAttrs.isEmpty()) + { + userIdAttrs = null; + } + } + } + + public void setIdAttributeNS(String namespaceURI, String localName, + boolean isId) + { + NamedNodeMap attrs = getAttributes(); + Attr attr = (Attr) attrs.getNamedItemNS(namespaceURI, localName); + setIdAttributeNode(attr, isId); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomEntity.java b/libjava/classpath/gnu/xml/dom/DomEntity.java new file mode 100644 index 00000000000..3a76479a389 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomEntity.java @@ -0,0 +1,147 @@ +/* DomEntity.java -- + Copyright (C) 1999,2000,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Entity; + +/** + * <p> "Entity" implementation. This is a non-core DOM class, supporting the + * "XML" feature. There are two types of entities, neither of which works + * particularly well in this API:</p><dl> + * + * <dt><em>Unparsed Entities</em></dt> + * <dd>Since ENTITY/ENTITIES attributes, the only legal use of unparsed + * entities in XML, can't be detected with DOM, there isn't much point in + * trying to use unparsed entities in DOM applications. (XML Linking is + * working to provide a better version of this functionality.) </dd> + * + * <dt><em>Parsed Entities</em></dt> + * <dd> While the DOM specification permits nodes for parsed entities + * to have a readonly set of children, this is not required and there + * is no portable way to provide such children. <em>This implementation + * currently does not permit children to be added to Entities.</em> + * There are related issues with the use of EntityReference nodes. </dd> + * + * </dl> + * + * <p> In short, <em>avoid using this DOM functionality</em>. + * + * @see DomDoctype + * @see DomEntityReference + * @see DomNotation + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomEntity + extends DomExtern + implements Entity +{ + + private String notation; + + /** + * Constructs an Entity node associated with the specified document, + * with the specified descriptive data. + * + * <p>This constructor should only be invoked by a DomDoctype as part + * of its declareEntity functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this entity is associated + * @param name Name of this entity + * @param publicId If non-null, provides the entity's PUBLIC identifier + * @param systemId Provides the entity's SYSTEM identifier (URI) + * @param notation If non-null, provides the unparsed entity's notation. + */ + protected DomEntity(DomDocument owner, + String name, + String publicId, + String systemId, + String notation) + { + super(ENTITY_NODE, owner, name, publicId, systemId); + this.notation = notation; + + // NOTE: if notation == null, this is a parsed entity + // which could reasonably be given child nodes ... + makeReadonly(); + } + + /** + * <b>DOM L1</b> + * Returns the NOTATION identifier associated with this entity, if any. + */ + final public String getNotationName() + { + return notation; + } + + // DOM Level 3 methods + + public String getInputEncoding() + { + // TODO + return null; + } + + public String getXmlEncoding() + { + // TODO + return null; + } + + public String getXmlVersion() + { + // TODO + return null; + } + + /** + * The base URI of an external entity is its system ID. + * The base URI of an internal entity is the parent document's base URI. + * @since DOM Level 3 Core + */ + public String getBaseURI() + { + String systemId = getSystemId(); + return (systemId == null) ? owner.getBaseURI() : systemId; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomEntityReference.java b/libjava/classpath/gnu/xml/dom/DomEntityReference.java new file mode 100644 index 00000000000..d4596b405d4 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomEntityReference.java @@ -0,0 +1,130 @@ +/* DomEntityReference.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DocumentType; +import org.w3c.dom.Entity; +import org.w3c.dom.EntityReference; + +/** + * <p> "EntityReference" implementation (reference to parsed entity). + * This is a non-core DOM class, supporting the "XML" feature. + * It does not represent builtin entities (such as "&amp;") + * or character references, which are always directly expanded in + * DOM trees.</p> + * + * <p> Note that while the DOM specification permits these nodes to have + * a readonly set of children, this is not required. Similarly, it does + * not require a DOM to couple EntityReference nodes with any Entity nodes + * that have the same entity name (and equivalent children). It also + * effectively guarantees that references created directly or indirectly + * through the <em>Document.ImportNode</em> method will not have children. + * The level of functionality you may get is extremely variable. + * + * <p> Also significant is that even at their most functional level, the fact + * that EntityReference children must be readonly has caused significant + * problems when modifying work products held in DOM trees. Other problems + * include issues related to undeclared namespace prefixes (and references + * to the current default namespace) that may be found in the text of such + * parsed entities nodes. These must be contextually bound as part of DOM + * tree construction. When such nodes are moved, the namespace associated + * with a given prefix (or default) may change to be in conflict with the + * namespace bound to the node at creation time. + * + * <p> In short, <em>avoid using this DOM functionality</em>. + * + * @see DomDoctype + * @see DomEntity + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomEntityReference + extends DomNode + implements EntityReference +{ + + private String name; + + /** + * Constructs an EntityReference node associated with the specified + * document. The creator should populate this with whatever contents + * are appropriate, and then mark it as readonly. + * + * <p>This constructor should only be invoked by a Document as part of + * its createEntityReference functionality, or through a subclass which + * is similarly used in a "Sub-DOM" style layer. + * + * @see DomNode#makeReadonly + */ + protected DomEntityReference(DomDocument owner, String name) + { + super(ENTITY_REFERENCE_NODE, owner); + this.name = name; + } + + /** + * Returns the name of the referenced entity. + * @since DOM Level 1 Core + */ + public final String getNodeName() + { + return name; + } + + /** + * The base URI of an entity reference is the base URI where the entity + * declaration occurs. + * @since DOM Level 3 Core + */ + public final String getBaseURI() + { + DocumentType doctype = owner.getDoctype(); + if (doctype == null) + { + return null; + } + Entity entity = (Entity) doctype.getEntities().getNamedItem(name); + if (entity == null) + { + return null; + } + return entity.getBaseURI(); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomEvent.java b/libjava/classpath/gnu/xml/dom/DomEvent.java new file mode 100644 index 00000000000..d57eac0e445 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomEvent.java @@ -0,0 +1,345 @@ +/* DomEvent.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.*; +import org.w3c.dom.events.*; +import org.w3c.dom.views.AbstractView; // used by UIEvent + +/** + * "Event" implementation. Events are + * created (through DocumentEvent interface methods on the document object), + * and are sent to any target node in the document. + * + * <p> Applications may define application specific event subclasses, but + * should otherwise use the <em>DocumentTraversal</em> interface to acquire + * event objects. + * + * @author David Brownell + */ +public class DomEvent + implements Event +{ + + String type; // init + EventTarget target; + EventTarget currentNode; + short eventPhase; + boolean bubbles; // init + boolean cancelable; // init + long timeStamp; // ? + + /** Returns the event's type (name) as initialized */ + public final String getType() + { + return type; + } + + /** + * Returns event's target; delivery of an event is initiated + * by a <em>target.dispatchEvent(event)</em> invocation. + */ + public final EventTarget getTarget() + { + return target; + } + + /** + * Returns the target to which events are currently being + * delivered. When capturing or bubbling, this will not + * be what <em>getTarget</em> returns. + */ + public final EventTarget getCurrentTarget() + { + return currentNode; + } + + /** + * Returns CAPTURING_PHASE, AT_TARGET, or BUBBLING; + * only meaningful within EventListener.handleEvent + */ + public final short getEventPhase() + { + return eventPhase; + } + + /** + * Returns true if the news of the event bubbles to tree tops + * (as specified during initialization). + */ + public final boolean getBubbles() + { + return bubbles; + } + + /** + * Returns true if the default handling may be canceled + * (as specified during initialization). + */ + public final boolean getCancelable() + { + return cancelable; + } + + /** + * Returns the event's timestamp. + */ + public final long getTimeStamp() + { + return timeStamp; + } + + boolean stop; + boolean doDefault; + + /** + * Requests the event no longer be captured or bubbled; only + * listeners on the event target will see the event, if they + * haven't yet been notified. + * + * <p> <em> Avoid using this </em> except for application-specific + * events, for which you the protocol explicitly "blesses" the use + * of this with some event types. Otherwise, you are likely to break + * algorithms which depend on event notification either directly or + * through bubbling or capturing. </p> + * + * <p> Note that this method is not final, specifically to enable + * enforcing of policies about events always propagating. </p> + */ + public void stopPropagation() + { + stop = true; + } + + /** + * Requests that whoever dispatched the event not perform their + * default processing when event delivery completes. Initializes + * event timestamp. + */ + public final void preventDefault() + { + doDefault = false; + } + + /** Initializes basic event state. */ + public void initEvent(String typeArg, + boolean canBubbleArg, + boolean cancelableArg) + { + eventPhase = 0; + type = typeArg; + bubbles = canBubbleArg; + cancelable = cancelableArg; + timeStamp = System.currentTimeMillis(); + } + + /** Constructs, but does not initialize, an event. */ + public DomEvent(String type) + { + this.type = type; + } + + /** + * Returns a basic printable description of the event's type, + * state, and delivery conditions + */ + public String toString() + { + StringBuffer buf = new StringBuffer("[Event "); + buf.append(type); + switch (eventPhase) + { + case CAPTURING_PHASE: + buf.append(", CAPTURING"); + break; + case AT_TARGET: + buf.append(", AT TARGET"); + break; + case BUBBLING_PHASE: + buf.append(", BUBBLING"); + break; + default: + buf.append(", (inactive)"); + break; + } + if (bubbles && eventPhase != BUBBLING_PHASE) + { + buf.append(", bubbles"); + } + if (cancelable) + { + buf.append(", can cancel"); + } + // were we to provide subclass info, this's where it'd live + buf.append("]"); + return buf.toString(); + } + + /** + * "MutationEvent" implementation. + */ + public static final class DomMutationEvent + extends DomEvent + implements MutationEvent + { + + // package private + Node relatedNode; // init + + private String prevValue; // init + private String newValue; // init + + private String attrName; // init + private short attrChange; // init + + /** Returns any "related" node provided by this type of event */ + public final Node getRelatedNode() + { + return relatedNode; + } + + /** Returns any "previous value" provided by this type of event */ + public final String getPrevValue() + { + return prevValue; + } + + /** Returns any "new value" provided by this type of event */ + public final String getNewValue() + { + return newValue; + } + + /** For attribute change events, returns the attribute's name */ + public final String getAttrName() + { + return attrName; + } + + /** For attribute change events, returns how the attribuet changed */ + public final short getAttrChange() + { + return attrChange; + } + + /** Initializes a mutation event */ + public final void initMutationEvent(String typeArg, + boolean canBubbleArg, + boolean cancelableArg, + Node relatedNodeArg, + String prevValueArg, + String newValueArg, + String attrNameArg, + short attrChangeArg) + { + // super.initEvent is inlined here for speed + // (mutation events are issued on all DOM changes) + eventPhase = 0; + type = typeArg; + bubbles = canBubbleArg; + cancelable = cancelableArg; + timeStamp = System.currentTimeMillis(); + + relatedNode = relatedNodeArg; + prevValue = prevValueArg; + newValue = newValueArg; + attrName = attrNameArg; + attrChange = attrChangeArg; + } + + // clear everything that should be GC-able + void clear() + { + type = null; + target = null; + relatedNode = null; + currentNode = null; + prevValue = newValue = attrName = null; + } + + /** Constructs an uninitialized mutation event. */ + public DomMutationEvent(String type) + { + super(type); + } + + } + + /** + * "UIEvent" implementation. + */ + public static class DomUIEvent + extends DomEvent + implements UIEvent + { + + private AbstractView view; // init + private int detail; // init + + /** Constructs an uninitialized User Interface (UI) event */ + public DomUIEvent (String type) { super (type); } + + public final AbstractView getView () { return view; } + public final int getDetail () { return detail; } + + /** Initializes a UI event */ + public final void initUIEvent(String typeArg, + boolean canBubbleArg, + boolean cancelableArg, + AbstractView viewArg, + int detailArg) + { + super.initEvent(typeArg, canBubbleArg, cancelableArg); + view = viewArg; + detail = detailArg; + } + + } + + /* + + static final class DomMouseEvent extends DomUIEvent + implements MouseEvent + { + // another half dozen state variables/accessors + } + + */ + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomExtern.java b/libjava/classpath/gnu/xml/dom/DomExtern.java new file mode 100644 index 00000000000..87a6909f758 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomExtern.java @@ -0,0 +1,117 @@ +/* DomExtern.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +/** + * <p> Abstract implemention of nodes describing external DTD-related + * objects. This facilitates reusing code for Entity, Notation, and + * DocumentType (really, external subset) nodes. Such support is not + * part of the core DOM; it's for the "XML" feature. </p> + * + * <p> Note that you are strongly advised to avoid using the DOM + * features that take advantage of this class, since (as of L2) none + * of them is defined fully enough to permit full use of the + * XML feature they partially expose. </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class DomExtern + extends DomNode +{ + + private final String name; + private final String publicId; + private final String systemId; + + /** + * Constructs a node associated with the specified document, + * with the specified descriptive data. + * + * @param owner The document with which this object is associated + * @param name Name of this object + * @param publicId If non-null, provides the entity's PUBLIC identifier + * @param systemId If non-null, provides the entity's SYSTEM identifier + */ + // package private + DomExtern(short nodeType, + DomDocument owner, + String name, + String publicId, + String systemId) + { + super(nodeType, owner); + this.name = name; + this.publicId = publicId; + this.systemId = systemId; + } + + /** + * <b>DOM L1</b> + * Returns the SYSTEM identifier associated with this object, if any. + */ + public final String getSystemId() + { + return systemId; + } + + /** + * <b>DOM L1</b> + * Returns the PUBLIC identifier associated with this object, if any. + */ + public final String getPublicId() + { + return publicId; + } + + /** + * <b>DOM L1</b> + * Returns the object's name. + */ + public final String getNodeName() + { + return name; + } + + public final String getLocalName() + { + return name; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomImpl.java b/libjava/classpath/gnu/xml/dom/DomImpl.java new file mode 100644 index 00000000000..cabe741c931 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomImpl.java @@ -0,0 +1,278 @@ +/* DomImpl.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Element; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSParser; +import org.w3c.dom.ls.LSSerializer; +import gnu.xml.dom.html2.DomHTMLImpl; +import gnu.xml.dom.ls.DomLSInput; +import gnu.xml.dom.ls.DomLSOutput; +import gnu.xml.dom.ls.DomLSParser; +import gnu.xml.dom.ls.DomLSSerializer; + +/** + * <p> "DOMImplementation" implementation. </p> + * + * <p> At this writing, the following features are supported: + * "XML" (L1, L2, L3), + * "Events" (L2), "MutationEvents" (L2), "USER-Events" (a conformant extension), + * "HTMLEvents" (L2), "UIEvents" (L2), "Traversal" (L2), "XPath" (L3), + * "LS" (L3) "LS-Async" (L3). + * It is possible to compile the package so it doesn't support some of these + * features (notably, Traversal). + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomImpl + implements DOMImplementation, DOMImplementationLS +{ + + /** + * Constructs a DOMImplementation object which supports + * "XML" and other DOM Level 2 features. + */ + public DomImpl() + { + } + + /** + * <b>DOM L1</b> + * Returns true if the specified feature and version are + * supported. Note that the case of the feature name is ignored. + */ + public boolean hasFeature(String name, String version) + { + if (name.length() == 0) + { + return false; + } + name = name.toLowerCase(); + if (name.charAt(0) == '+') + { + name = name.substring(1); + } + + if ("xml".equals(name) || "core".equals(name)) + { + return (version == null || + "".equals(version) || + "1.0".equals(version) || + "2.0".equals(version) || + "3.0".equals(version)); + + } + else if ("ls".equals(name) || "ls-async".equals(name)) + { + return (version == null || + "".equals(version) || + "3.0".equals(version)); + } + else if ("events".equals(name) + || "mutationevents".equals(name) + || "uievents".equals(name) + // || "mouseevents".equals(name) + || "htmlevents".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + + // Extension: "USER-" prefix event types can + // be created and passed through the DOM. + + } + else if ("user-events".equals(name)) + { + return (version == null || + "".equals(version) || + "0.1".equals(version)); + + // NOTE: "hasFeature" for events is here interpreted to + // mean the DOM can manufacture those sorts of events, + // since actually choosing to report the events is more + // often part of the environment or application. It's + // only really an issue for mutation events. + + } + else if (DomNode.reportMutations + && "traversal".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + } + else if ("xpath".equals(name)) + { + return (version == null || + "".equals(version) || + "3.0".equals(version)); + } + else if ("html".equals(name) || "xhtml".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + } + + // views + // stylesheets + // css, css2 + // range + + return false; + } + + /** + * <b>DOM L2</b> + * Creates and returns a DocumentType, associated with this + * implementation. This DocumentType can have no associated + * objects(notations, entities) until the DocumentType is + * first associated with a document. + * + * <p> Note that there is no implication that this DTD will + * be parsed by the DOM, or ever have contents. Moreover, the + * DocumentType created here can only be added to a document by + * the createDocument method(below). <em>That means that the only + * portable way to create a Document object is to start parsing, + * queue comment and processing instruction (PI) nodes, and then only + * create a DOM Document after <b>(a)</b> it's known if a DocumentType + * object is needed, and <b>(b) the name and namespace of the root + * element is known. Queued comment and PI nodes would then be + * inserted appropriately in the document prologue, both before and + * after the DTD node, and additional attributes assigned to the + * root element.</em> + *(One hopes that the final DOM REC fixes this serious botch.) + */ + public DocumentType createDocumentType(String rootName, + String publicId, + String systemId) + // CR2 deleted internal subset, ensuring DocumentType + // is 100% useless instead of just 90% so. + { + DomDocument.checkNCName(rootName, false); + return new DomDoctype(this, rootName, publicId, systemId, null); + } + + /** + * <b>DOM L2</b> + * Creates and returns a Document, populated only with a root element and + * optionally a document type(if that was provided). + */ + public Document createDocument(String namespaceURI, + String rootName, + DocumentType doctype) + { + Document doc = createDocument(); + Element root = null; + + if (rootName != null) + { + root = doc.createElementNS(namespaceURI, rootName); + if (rootName.startsWith("xmlns:")) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns is reserved", null, 0); + } + } + // Bleech -- L2 seemingly _requires_ omission of xmlns attributes. + if (doctype != null) + { + doc.appendChild(doctype); // handles WRONG_DOCUMENT error + } + if (root != null) + { + doc.appendChild(root); + } + return doc; + } + + protected Document createDocument() + { + return new DomDocument(this); + } + + // DOM Level 3 + + public Object getFeature(String feature, String version) + { + if (hasFeature(feature, version)) + { + if ("html".equalsIgnoreCase(feature) || + "xhtml".equalsIgnoreCase(feature)) + { + return new DomHTMLImpl(); + } + return this; + } + return null; + } + + // -- DOMImplementationLS -- + + public LSParser createLSParser(short mode, String schemaType) + throws DOMException + { + return new DomLSParser(mode, schemaType); + } + + public LSSerializer createLSSerializer() + { + return new DomLSSerializer(); + } + + public LSInput createLSInput() + { + return new DomLSInput(); + } + + public LSOutput createLSOutput() + { + return new DomLSOutput(); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomIterator.java b/libjava/classpath/gnu/xml/dom/DomIterator.java new file mode 100644 index 00000000000..472c6e80585 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomIterator.java @@ -0,0 +1,373 @@ +/* DomIterator.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventListener; +import org.w3c.dom.events.EventTarget; +import org.w3c.dom.events.MutationEvent; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; + +/** + * <p> "NodeIterator" implementation, usable with any L2 DOM which + * supports MutationEvents. </p> + * + * @author David Brownell + */ +public final class DomIterator + implements NodeIterator, EventListener +{ + + private Node reference; + private boolean right; + private boolean done; + + private final Node root; + private final int whatToShow; + private final NodeFilter filter; + private final boolean expandEntityReferences; + + /** + * Constructs and initializes an iterator. + */ + protected DomIterator(Node root, + int whatToShow, + NodeFilter filter, + boolean entityReferenceExpansion) + { + if (!root.isSupported("MutationEvents", "2.0")) + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, + "Iterator needs mutation events", root, 0); + } + + this.root = root; + this.whatToShow = whatToShow; + this.filter = filter; + this.expandEntityReferences = entityReferenceExpansion; + + // start condition: going right, seen nothing yet. + reference = null; + right = true; + + EventTarget target = (EventTarget) root; + target.addEventListener("DOMNodeRemoved", this, false); + } + + /** + * <b>DOM L2</b> + * Flags the iterator as done, unregistering its event listener so + * that the iterator can be garbage collected without relying on weak + * references (a "Java 2" feature) in the event subsystem. + */ + public void detach() + { + EventTarget target = (EventTarget) root; + target.removeEventListener("DOMNodeRemoved", this, false); + done = true; + } + + /** + * <b>DOM L2</b> + * Returns the flag controlling whether iteration descends + * through entity references. + */ + public boolean getExpandEntityReferences() + { + return expandEntityReferences; + } + + /** + * <b>DOM L2</b> + * Returns the filter provided during construction. + */ + public NodeFilter getFilter() + { + return filter; + } + + /** + * <b>DOM L2</b> + * Returns the root of the tree this is iterating through. + */ + public Node getRoot() + { + return root; + } + + /** + * <b>DOM L2</b> + * Returns the mask of flags provided during construction. + */ + public int getWhatToShow() + { + return whatToShow; + } + + /** + * <b>DOM L2</b> + * Returns the next node in a forward iteration, masked and filtered. + * Note that the node may be read-only due to entity expansions. + * A null return indicates the iteration is complete, but may still + * be processed backwards. + */ + public Node nextNode() + { + if (done) + { + throw new DomDOMException(DOMException.INVALID_STATE_ERR); + } + right = true; + return walk(true); + } + + /** + * <b>DOM L2</b> + * Returns the next node in a backward iteration, masked and filtered. + * Note that the node may be read-only due to entity expansions. + * A null return indicates the iteration is complete, but may still + * be processed forwards. + */ + public Node previousNode() + { + if (done) + { + throw new DomDOMException(DOMException.INVALID_STATE_ERR); + } + Node previous = reference; + right = false; + walk(false); + return previous; + } + + private boolean shouldShow(Node node) + // raises Runtime exceptions indirectly, via acceptNode() + { + if ((whatToShow & (1 << (node.getNodeType() - 1))) == 0) + { + return false; + } + if (filter == null) + { + return true; + } + return filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT; + } + + // + // scenario: root = 1, sequence = 1 2 ... 3 4 + // forward walk: 1 2 ... 3 4 null + // then backward: 4 3 ... 2 1 null + // + // At the leftmost end, "previous" == null + // At the rightmost end, "previous" == 4 + // + // The current draft spec really seem to make no sense re the + // role of the reference node, so what it says is ignored here. + // + private Node walk(boolean forward) + { + Node here = reference; + + while ((here = successor(here, forward)) != null + && !shouldShow(here)) + { + continue; + } + if (here != null || !forward) + { + reference = here; + } + return here; + } + + private boolean isLeaf(Node here) + { + boolean leaf = !here.hasChildNodes(); + if (!leaf && !expandEntityReferences) + { + leaf = (here.getNodeType() == Node.ENTITY_REFERENCE_NODE); + } + return leaf; + } + + // + // Returns the immediate successor in a forward (or backward) + // document order walk, sans filtering ... except that it knows + // how to stop, returning null when done. This is a depth first + // preorder traversal when run in the forward direction. + // + private Node successor(Node here, boolean forward) + { + Node next; + + // the "leftmost" end is funky + if (here == null) + { + return forward ? root : null; + } + + // + // Forward, this is preorder: children before siblings. + // Backward, it's postorder: we saw the children already. + // + if (forward && !isLeaf(here)) + { + return here.getFirstChild(); + } + + // + // Siblings ... if forward, we visit them, if backwards + // we visit their children first. + // + if (forward) + { + if ((next = here.getNextSibling()) != null) + { + return next; + } + } + else if ((next = here.getPreviousSibling()) != null) + { + if (isLeaf(next)) + { + return next; + } + next = next.getLastChild(); + while (!isLeaf(next)) + { + next = next.getLastChild(); + } + return next; + } + + // + // We can't go down or lateral -- it's up, then. The logic is + // the converse of what's above: backwards is easy (the parent + // is next), forwards isn't. + // + next = here.getParentNode(); + if (!forward) + { + return next; + } + + Node temp = null; + while (next != null + && next != root + && (temp = next.getNextSibling()) == null) + { + next = next.getParentNode(); + } + if (next == root) + { + return null; + } + return temp; + } + + /** + * Not for public use. This lets the iterator know when its + * reference node will be removed from the tree, so that a new + * one may be selected. + * + * <p> This version works by watching removal events as they + * bubble up. So, don't prevent them from bubbling. + */ + public void handleEvent(Event e) + { + MutationEvent event; + Node ancestor, removed; + + if (reference == null + || !"DOMNodeRemoved".equals(e.getType()) + || e.getEventPhase() != Event.BUBBLING_PHASE) + { + return; + } + + event = (MutationEvent) e; + removed = (Node) event.getTarget(); + + // See if the removal will cause trouble for this iterator + // by being the reference node or an ancestor of it. + for (ancestor = reference; + ancestor != null && ancestor != root; + ancestor = ancestor.getParentNode()) + { + if (ancestor == removed) + { + break; + } + } + if (ancestor != removed) + { + return; + } + + // OK, it'll cause trouble. We want to make the "next" + // node in our current traversal direction seem right. + // So we pick the nearest node that's not getting removed, + // but go in the _opposite_ direction from our current + // traversal ... so the "next" doesn't skip anything. + Node candidate; + +search: + while ((candidate = walk(!right)) != null) + { + for (ancestor = candidate; + ancestor != null && ancestor != root; + ancestor = ancestor.getParentNode()) + { + if (ancestor == removed) + { + continue search; + } + } + return; + } + + // The current DOM WD talks about a special case here; + // I've not yet seen it. + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java b/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java new file mode 100644 index 00000000000..4cf57772e9b --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java @@ -0,0 +1,90 @@ +/* DomNSResolverContext.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.Iterator; +import javax.xml.namespace.NamespaceContext; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * Namespace content wrapper for an XPathNSResolver. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomNSResolverContext + implements NamespaceContext, Iterator +{ + + final XPathNSResolver resolver; + + DomNSResolverContext(XPathNSResolver resolver) + { + this.resolver = resolver; + } + + public String getNamespaceURI(String prefix) + { + return resolver.lookupNamespaceURI(prefix); + } + + public String getPrefix(String namespaceURI) + { + return null; + } + + public Iterator getPrefixes(String namespaceURI) + { + return this; + } + + public boolean hasNext() + { + return false; + } + + public Object next() + { + return null; + } + + public void remove() + { + throw new UnsupportedOperationException(); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java b/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java new file mode 100644 index 00000000000..6f224029ac3 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java @@ -0,0 +1,421 @@ +/* DomNamedNodeMap.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * <p> "NamedNodeMap" implementation. </p> + * Used mostly to hold element attributes, but sometimes also + * to list notations or entities. + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomNamedNodeMap + implements NamedNodeMap +{ + + final DomNode owner; + final short type; + + DomNode first; + int length; + boolean readonly; + + // package private + DomNamedNodeMap(DomNode owner, short type) + { + this.owner = owner; + this.type = type; + } + + /** + * Exposes the internal "readonly" flag. In DOM, all NamedNodeMap + * objects found in a DocumentType object are read-only (after + * they are fully constructed), and those holding attributes of + * a readonly element will also be readonly. + */ + public final boolean isReadonly() + { + return readonly; + } + + /** + * Sets the internal "readonly" flag so the node and its + * children can't be changed. + */ + public void makeReadonly() + { + readonly = true; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.makeReadonly(); + } + } + + /** + * <b>DOM L1</b> + * Returns the named item from the map, or null; names are just + * the nodeName property. + */ + public Node getNamedItem(String name) + { + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + if (ctx.getNodeName().equals(name)) + { + return ctx; + } + } + return null; + } + + /** + * <b>DOM L2</b> + * Returns the named item from the map, or null; names are the + * localName and namespaceURI properties, ignoring any prefix. + */ + public Node getNamedItemNS(String namespaceURI, String localName) + { + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + String name = ctx.getLocalName(); + if ((localName == null && name == null) || + (localName != null && localName.equals(name))) + { + String uri = ctx.getNamespaceURI(); + if ("".equals(uri)) + { + uri = null; + } + if ((namespaceURI == null && uri == null) || + (namespaceURI != null && namespaceURI.equals(uri))) + { + return ctx; + } + } + } + return null; + } + + /** + * <b>DOM L1</b> + * Stores the named item into the map, optionally overwriting + * any existing node with that name. The name used is just + * the nodeName attribute. + */ + public Node setNamedItem(Node arg) + { + return setNamedItem(arg, false); + } + + /** + * <b>DOM L2</b> + * Stores the named item into the map, optionally overwriting + * any existing node with that fully qualified name. The name + * used incorporates the localName and namespaceURI properties, + * and ignores any prefix. + */ + public Node setNamedItemNS(Node arg) + { + return setNamedItem(arg, true); + } + + Node setNamedItem(Node arg, boolean ns) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + + DomNode node = (DomNode) arg; + if (node.owner != owner.owner) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR); + } + if (node.nodeType != type) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR); + } + if (node.nodeType == Node.ATTRIBUTE_NODE) + { + DomNode element = node.parent; + if (element != null && element != owner) + { + throw new DomDOMException(DOMException.INUSE_ATTRIBUTE_ERR); + } + node.parent = owner; + node.depth = owner.depth + 1; + } + + String nodeName = node.getNodeName(); + String localName = ns ? node.getLocalName() : null; + String namespaceURI = ns ? node.getNamespaceURI() : null; + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + + // maybe attribute ADDITION events (?) + DomNode last = null; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + boolean test = false; + if (ns) + { + String tln = ctx.getLocalName(); + if (tln == null) + { + tln = ctx.getNodeName(); + } + if (tln.equals(localName)) + { + String tu = ctx.getNamespaceURI(); + if ((tu == null && namespaceURI == null) || + (tu != null && tu.equals(namespaceURI))) + { + test = true; + } + } + } + else + { + test = ctx.getNodeName().equals(nodeName); + } + if (test) + { + // replace + node.previous = ctx.previous; + node.next = ctx.next; + if (ctx.previous != null) + { + ctx.previous.next = node; + } + if (ctx.next != null) + { + ctx.next.previous = node; + } + if (first == ctx) + { + first = node; + } + reparent(node, nodeName, ctx.index); + ctx.parent = null; + ctx.next = null; + ctx.previous = null; + ctx.setDepth(0); + ctx.index = 0; + return ctx; + } + last = ctx; + } + // append + if (last != null) + { + last.next = node; + node.previous = last; + } + else + { + first = node; + } + length++; + reparent(node, nodeName, 0); + return null; + } + + void reparent(DomNode node, String nodeName, int i) + { + node.parent = owner; + node.setDepth(owner.depth + 1); + // index renumbering + for (DomNode ctx = node; ctx != null; ctx = ctx.next) + { + ctx.index = i++; + } + // cache xml:space + boolean xmlSpace = "xml:space".equals(nodeName); + if (xmlSpace && owner instanceof DomElement) + { + ((DomElement) owner).xmlSpace = node.getNodeValue(); + } + } + + /** + * <b>DOM L1</b> + * Removes the named item from the map, or reports an exception; + * names are just the nodeName property. + */ + public Node removeNamedItem(String name) + { + return removeNamedItem(null, name, false); + } + + /** + * <b>DOM L2</b> + * Removes the named item from the map, or reports an exception; + * names are the localName and namespaceURI properties. + */ + public Node removeNamedItemNS(String namespaceURI, String localName) + { + return removeNamedItem(namespaceURI, localName, true); + } + + Node removeNamedItem(String uri, String name, boolean ns) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + + // report attribute REMOVAL event? + + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + boolean test = false; + String nodeName = ctx.getNodeName(); + if (ns) + { + String tln = ctx.getLocalName(); + if (tln.equals(name)) + { + String tu = ctx.getNamespaceURI(); + if ((tu == null && uri == null) || + (tu != null && tu.equals(uri))) + { + test = true; + } + } + } + else + { + test = nodeName.equals(name); + } + if (test) + { + // uncache xml:space + boolean xmlSpace = "xml:space".equals(nodeName); + if (xmlSpace && owner instanceof DomElement) + { + ((DomElement) owner).xmlSpace = ""; + } + // is this a default attribute? + if (ctx.nodeType == Node.ATTRIBUTE_NODE) + { + String def = getDefaultValue(ctx.getNodeName()); + if (def != null) + { + ctx.setNodeValue(def); + ((DomAttr) ctx).setSpecified(false); + return null; + } + } + // remove + if (ctx == first) + { + first = ctx.next; + } + if (ctx.previous != null) + { + ctx.previous.next = ctx.next; + } + if (ctx.next != null) + { + ctx.next.previous = ctx.previous; + } + length--; + ctx.previous = null; + ctx.next = null; + ctx.parent = null; + ctx.setDepth(0); + ctx.index = 0; + return ctx; + } + } + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + + String getDefaultValue(String name) + { + DomDoctype doctype = (DomDoctype) owner.owner.getDoctype(); + if (doctype == null) + { + return null; + } + DTDAttributeTypeInfo info = + doctype.getAttributeTypeInfo(owner.getNodeName(), name); + if (info == null) + { + return null; + } + return info.value; + } + + /** + * <b>DOM L1</b> + * Returns the indexed item from the map, or null. + */ + public Node item(int index) + { + DomNode ctx = first; + int count = 0; + while (ctx != null && count < index) + { + ctx = ctx.next; + count++; + } + return ctx; + } + + /** + * <b>DOM L1</b> + * Returns the length of the map. + */ + public int getLength() + { + return length; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNode.java b/libjava/classpath/gnu/xml/dom/DomNode.java new file mode 100644 index 00000000000..3f29fb1283d --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNode.java @@ -0,0 +1,2189 @@ +/* DomNode.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.w3c.dom.Document; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import org.w3c.dom.events.DocumentEvent; +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventException; +import org.w3c.dom.events.EventListener; +import org.w3c.dom.events.EventTarget; +import org.w3c.dom.events.MutationEvent; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; + +/** + * <p> "Node", "EventTarget", and "DocumentEvent" implementation. + * This provides most of the core DOM functionality; only more + * specialized features are provided by subclasses. Those subclasses may + * have some particular constraints they must implement, by overriding + * methods defined here. Such constraints are noted here in the method + * documentation. </p> + * + * <p> Note that you can create events with type names prefixed with "USER-", + * and pass them through this DOM. This lets you use the DOM event scheme + * for application specific purposes, although you must use a predefined event + * structure (such as MutationEvent) to pass data along with those events. + * Test for existence of this feature with the "USER-Events" DOM feature + * name.</p> + * + * <p> Other kinds of events you can send include the "html" events, + * like "load", "unload", "abort", "error", and "blur"; and the mutation + * events. If this DOM has been compiled with mutation event support + * enabled, it will send mutation events when you change parts of the + * tree; otherwise you may create and send such events yourself, but + * they won't be generated by the DOM itself. </p> + * + * <p> Note that there is a namespace-aware name comparison method, + * <em>nameAndTypeEquals</em>, which compares the names (and types) of + * two nodes in conformance with the "Namespaces in XML" specification. + * While mostly intended for use with elements and attributes, this should + * also be helpful for ProcessingInstruction nodes and some others which + * do not have namespace URIs. + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class DomNode + implements Node, NodeList, EventTarget, DocumentEvent, Cloneable, Comparable +{ + + // package private + //final static String xmlNamespace = "http://www.w3.org/XML/1998/namespace"; + //final static String xmlnsURI = "http://www.w3.org/2000/xmlns/"; + + // tunable + // NKIDS_* affects arrays of children (which grow) + // (currently) fixed size: + // ANCESTORS_* is for event capture/bubbling, # ancestors + // NOTIFICATIONS_* is for per-node event delivery, # events + private static final int NKIDS_DELTA = 8; + private static final int ANCESTORS_INIT = 20; + private static final int NOTIFICATIONS_INIT = 10; + + // tunable: enable mutation events or not? Enabling it costs about + // 10-15% in DOM construction time, last time it was measured. + + // package private !!! + static final boolean reportMutations = true; + + // locking protocol changeable only within this class + private static final Object lockNode = new Object(); + + // NON-FINAL class data + + // Optimize event dispatch by not allocating memory each time + private static boolean dispatchDataLock; + private static DomNode[] ancestors = new DomNode[ANCESTORS_INIT]; + private static ListenerRecord[] notificationSet + = new ListenerRecord[NOTIFICATIONS_INIT]; + + // Ditto for the (most common) event object itself! + private static boolean eventDataLock; + private static DomEvent.DomMutationEvent mutationEvent + = new DomEvent.DomMutationEvent(null); + + // + // PER-INSTANCE DATA + // + + DomDocument owner; + DomNode parent; // parent node; + DomNode previous; // previous sibling node + DomNode next; // next sibling node + DomNode first; // first child node + DomNode last; // last child node + int index; // index of this node in its parent's children + int depth; // depth of the node in the document + int length; // number of children + final short nodeType; + + // Bleech ... "package private" so a builder can populate entity refs. + // writable during construction. DOM spec is nasty. + boolean readonly; + + // event registrations + private ListenerRecord[] listeners; + private int nListeners; + + // DOM Level 3 userData dictionary. + private HashMap userData; + private HashMap userDataHandlers; + + // + // Some of the methods here are declared 'final' because + // knowledge about their implementation is built into this + // class -- for both integrity and performance. + // + + /** + * Reduces space utilization for this node. + */ + public void compact() + { + if (listeners != null && listeners.length != nListeners) + { + if (nListeners == 0) + { + listeners = null; + } + else + { + ListenerRecord[] l = new ListenerRecord[nListeners]; + System.arraycopy(listeners, 0, l, 0, nListeners); + listeners = l; + } + } + } + + /** + * Constructs a node and associates it with its owner. Only + * Document and DocumentType nodes may be created with no owner, + * and DocumentType nodes get an owner as soon as they are + * associated with a document. + */ + protected DomNode(short nodeType, DomDocument owner) + { + this.nodeType = nodeType; + + if (owner == null) + { + // DOM calls never go down this path + if (nodeType != DOCUMENT_NODE && nodeType != DOCUMENT_TYPE_NODE) + { + throw new IllegalArgumentException ("no owner!"); + } + } + this.owner = owner; + } + + + /** + * <b>DOM L1</b> + * Returns null; Element subclasses must override this method. + */ + public NamedNodeMap getAttributes() + { + return null; + } + + /** + * <b>DOM L2></b> + * Returns true iff this is an element node with attributes. + */ + public boolean hasAttributes() + { + return false; + } + + /** + * <b>DOM L1</b> + * Returns a list, possibly empty, of the children of this node. + * In this implementation, to conserve memory, nodes are the same + * as their list of children. This can have ramifications for + * subclasses, which may need to provide their own getLength method + * for reasons unrelated to the NodeList method of the same name. + */ + public NodeList getChildNodes() + { + return this; + } + + /** + * <b>DOM L1</b> + * Returns the first child of this node, or null if there are none. + */ + public Node getFirstChild() + { + return first; + } + + /** + * <b>DOM L1</b> + * Returns the last child of this node, or null if there are none. + */ + public Node getLastChild() + { + return last; + } + + /** + * <b>DOM L1</b> + * Returns true if this node has children. + */ + public boolean hasChildNodes() + { + return length != 0; + } + + + /** + * Exposes the internal "readonly" flag. In DOM, children of + * entities and entity references are readonly, as are the + * objects associated with DocumentType objets. + */ + public final boolean isReadonly() + { + return readonly; + } + + /** + * Sets the internal "readonly" flag so this subtree can't be changed. + * Subclasses need to override this method for any associated content + * that's not a child node, such as an element's attributes or the + * (few) declarations associated with a DocumentType. + */ + public void makeReadonly() + { + readonly = true; + for (DomNode child = first; child != null; child = child.next) + { + child.makeReadonly(); + } + } + + /** + * Used to adopt a node to a new document. + */ + void setOwner(DomDocument doc) + { + this.owner = doc; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.setOwner(doc); + } + } + + // just checks the node for inclusion -- may be called many + // times (docfrag) before anything is allowed to change + private void checkMisc(DomNode child) + { + if (readonly && !owner.building) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, + null, this, 0); + } + for (DomNode ctx = this; ctx != null; ctx = ctx.parent) + { + if (child == ctx) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "can't make ancestor into a child", + this, 0); + } + } + + DomDocument owner = (nodeType == DOCUMENT_NODE) ? (DomDocument) this : + this.owner; + DomDocument childOwner = child.owner; + short childNodeType = child.nodeType; + + if (childOwner != owner) + { + // new in DOM L2, this case -- patch it up later, in reparent() + if (!(childNodeType == DOCUMENT_TYPE_NODE && childOwner == null)) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, child, 0); + } + } + + // enforce various structural constraints + switch (nodeType) + { + case DOCUMENT_NODE: + switch (childNodeType) + { + case ELEMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + case COMMENT_NODE: + case DOCUMENT_TYPE_NODE: + return; + } + break; + + case ATTRIBUTE_NODE: + switch (childNodeType) + { + case TEXT_NODE: + case ENTITY_REFERENCE_NODE: + return; + } + break; + + case DOCUMENT_FRAGMENT_NODE: + case ENTITY_REFERENCE_NODE: + case ELEMENT_NODE: + case ENTITY_NODE: + switch (childNodeType) + { + case ELEMENT_NODE: + case TEXT_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + case CDATA_SECTION_NODE: + case ENTITY_REFERENCE_NODE: + return; + } + break; + } + if (owner.checkingWellformedness) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "can't append " + + nodeTypeToString(childNodeType) + + " to node of type " + + nodeTypeToString(nodeType), + this, 0); + } + } + + // Here's hoping a good optimizer will detect the case when the + // next several methods are never called, and won't allocate + // object code space of any kind. (Case: not reporting any + // mutation events. We can also remove some static variables + // listed above.) + + private void insertionEvent(DomEvent.DomMutationEvent event, + DomNode target) + { + if (owner == null || owner.building) + { + return; + } + boolean doFree = false; + + if (event == null) + { + event = getMutationEvent(); + } + if (event != null) + { + doFree = true; + } + else + { + event = new DomEvent.DomMutationEvent(null); + } + event.initMutationEvent("DOMNodeInserted", + true /* bubbles */, false /* nocancel */, + this /* related */, null, null, null, (short) 0); + target.dispatchEvent(event); + + // XXX should really visit every descendant of 'target' + // and sent a DOMNodeInsertedIntoDocument event to it... + // bleech, there's no way to keep that acceptably fast. + + if (doFree) + { + event.target = null; + event.relatedNode = null; + event.currentNode = null; + eventDataLock = false; + } // else we created work for the GC + } + + private void removalEvent(DomEvent.DomMutationEvent event, + DomNode target) + { + if (owner == null || owner.building) + { + return; + } + boolean doFree = false; + + if (event == null) + { + event = getMutationEvent(); + } + if (event != null) + { + doFree = true; + } + else + { + event = new DomEvent.DomMutationEvent(null); + } + event.initMutationEvent("DOMNodeRemoved", + true /* bubbles */, false /* nocancel */, + this /* related */, null, null, null, (short) 0); + target.dispatchEvent(event); + + // XXX should really visit every descendant of 'target' + // and sent a DOMNodeRemovedFromDocument event to it... + // bleech, there's no way to keep that acceptably fast. + + event.target = null; + event.relatedNode = null; + event.currentNode = null; + if (doFree) + { + eventDataLock = false; + } + // else we created more work for the GC + } + + // + // Avoid creating lots of memory management work, by using a simple + // allocation strategy for the mutation event objects that get used + // at least once per tree modification. We can't use stack allocation, + // so we do the next simplest thing -- more or less, static allocation. + // Concurrent notifications should be rare, anyway. + // + // Returns the preallocated object, which needs to be carefully freed, + // or null to indicate the caller needs to allocate their own. + // + static private DomEvent.DomMutationEvent getMutationEvent() + { + synchronized (lockNode) + { + if (eventDataLock) + { + return null; + } + eventDataLock = true; + return mutationEvent; + } + } + + // NOTE: this is manually inlined in the insertion + // and removal event methods above; change in sync. + static private void freeMutationEvent() + { + // clear fields to enable GC + mutationEvent.clear(); + eventDataLock = false; + } + + void setDepth(int depth) + { + this.depth = depth; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.setDepth(depth + 1); + } + } + + /** + * <b>DOM L1</b> + * Appends the specified node to this node's list of children. + * Document subclasses must override this to enforce the restrictions + * that there be only one element and document type child. + * + * <p> Causes a DOMNodeInserted mutation event to be reported. + * Will first cause a DOMNodeRemoved event to be reported if the + * parameter already has a parent. If the new child is a document + * fragment node, both events will be reported for each child of + * the fragment; the order in which children are removed and + * inserted is implementation-specific. + * + * <p> If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node appendChild(Node newChild) + { + try + { + DomNode child = (DomNode) newChild; + + if (child.nodeType == DOCUMENT_FRAGMENT_NODE) + { + // Append all nodes in the fragment to this node + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + checkMisc(ctx); + } + for (DomNode ctx = child.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + appendChild(ctx); + ctx = ctxNext; + } + } + else + { + checkMisc(child); + if (child.parent != null) + { + child.parent.removeChild(child); + } + child.parent = this; + child.index = length++; + child.setDepth(depth + 1); + child.next = null; + if (last == null) + { + first = child; + child.previous = null; + } + else + { + last.next = child; + child.previous = last; + } + last = child; + + if (reportMutations) + { + insertionEvent(null, child); + } + } + + return child; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, newChild, 0); + } + } + + /** + * <b>DOM L1</b> + * Inserts the specified node in this node's list of children. + * Document subclasses must override this to enforce the restrictions + * that there be only one element and document type child. + * + * <p> Causes a DOMNodeInserted mutation event to be reported. Will + * first cause a DOMNodeRemoved event to be reported if the newChild + * parameter already has a parent. If the new child is a document + * fragment node, both events will be reported for each child of + * the fragment; the order in which children are removed and inserted + * is implementation-specific. + * + * <p> If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node insertBefore(Node newChild, Node refChild) + { + if (refChild == null) + { + return appendChild(newChild); + } + + try + { + DomNode child = (DomNode) newChild; + DomNode ref = (DomNode) refChild; + + if (child.nodeType == DOCUMENT_FRAGMENT_NODE) + { + // Append all nodes in the fragment to this node + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + checkMisc(ctx); + } + for (DomNode ctx = child.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + insertBefore(ctx, ref); + ctx = ctxNext; + } + } + else + { + checkMisc(child); + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + if (ref == child) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "can't insert node before itself", + ref, 0); + } + + if (child.parent != null) + { + child.parent.removeChild(child); + } + child.parent = this; + int i = ref.index; + child.setDepth(depth + 1); + child.next = ref; + if (ref.previous != null) + { + ref.previous.next = child; + } + child.previous = ref.previous; + ref.previous = child; + if (first == ref) + { + first = child; + } + // index renumbering + for (DomNode ctx = child; ctx != null; ctx = ctx.next) + { + ctx.index = i++; + } + + if (reportMutations) + { + insertionEvent(null, child); + } + } + + return child; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, newChild, 0); + } + } + + /** + * <b>DOM L1</b> + * Replaces the specified node in this node's list of children. + * Document subclasses must override this to test the restrictions + * that there be only one element and document type child. + * + * <p> Causes DOMNodeRemoved and DOMNodeInserted mutation event to be + * reported. Will cause another DOMNodeRemoved event to be reported if + * the newChild parameter already has a parent. These events may be + * delivered in any order, except that the event reporting removal + * from such an existing parent will always be delivered before the + * event reporting its re-insertion as a child of some other node. + * The order in which children are removed and inserted is implementation + * specific. + * + * <p> If your application needs to depend on the in which those removal + * and insertion events are delivered, don't use this API. Instead, + * invoke the removeChild and insertBefore methods directly, to guarantee + * a specific delivery order. Similarly, don't use document fragments, + * Otherwise your application code may not work on a DOM which implements + * this method differently. + * + * <p> If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node replaceChild(Node newChild, Node refChild) + { + try + { + DomNode child = (DomNode) newChild; + DomNode ref = (DomNode) refChild; + + DomEvent.DomMutationEvent event = getMutationEvent(); + boolean doFree = (event != null); + + if (child.nodeType == DOCUMENT_FRAGMENT_NODE) + { + // Append all nodes in the fragment to this node + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + checkMisc(ctx); + } + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + + if (reportMutations) + { + removalEvent(event, ref); + } + length--; + length += child.length; + + if (child.length == 0) + { + // Removal + if (ref.previous != null) + { + ref.previous.next = ref.next; + } + if (ref.next != null) + { + ref.next.previous = ref.previous; + } + if (first == ref) + { + first = ref.next; + } + if (last == ref) + { + last = ref.previous; + } + } + else + { + int i = ref.index; + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + // Insertion + ctx.parent = this; + ctx.index = i++; + ctx.setDepth(ref.depth); + if (ctx == child.first) + { + ctx.previous = ref.previous; + } + if (ctx == child.last) + { + ctx.next = ref.next; + } + } + if (first == ref) + { + first = child.first; + } + if (last == ref) + { + last = child.last; + } + } + } + else + { + checkMisc(child); + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + + if (reportMutations) + { + removalEvent(event, ref); + } + + if (child.parent != null) + { + child.parent.removeChild(child); + } + child.parent = this; + child.index = ref.index; + child.setDepth(ref.depth); + if (ref.previous != null) + { + ref.previous.next = child; + } + child.previous = ref.previous; + if (ref.next != null) + { + ref.next.previous = child; + } + child.next = ref.next; + if (first == ref) + { + first = child; + } + if (last == ref) + { + last = child; + } + + if (reportMutations) + { + insertionEvent(event, child); + } + if (doFree) + { + freeMutationEvent(); + } + } + ref.parent = null; + ref.index = 0; + ref.setDepth(0); + ref.previous = null; + ref.next = null; + + return ref; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, newChild, 0); + } + } + + /** + * <b>DOM L1</b> + * Removes the specified child from this node's list of children, + * or else reports an exception. + * + * <p> Causes a DOMNodeRemoved mutation event to be reported. + * + * <p> If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node removeChild(Node refChild) + { + try + { + DomNode ref = (DomNode) refChild; + + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + if (readonly && !owner.building) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, + null, this, 0); + } + + for (DomNode child = first; child != null; child = child.next) + { + if (child == ref) + { + if (reportMutations) + { + removalEvent(null, child); + } + + length--; + if (ref.previous != null) + { + ref.previous.next = ref.next; + } + if (ref.next != null) + { + ref.next.previous = ref.previous; + } + if (first == ref) + { + first = ref.next; + } + if (last == ref) + { + last = ref.previous; + } + // renumber indices + int i = 0; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.index = i++; + } + ref.parent = null; + ref.setDepth(0); + ref.index = 0; + ref.previous = null; + ref.next = null; + + return ref; + } + } + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + "that's no child of mine", refChild, 0); + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, refChild, 0); + } + } + + /** + * <b>DOM L1 (NodeList)</b> + * Returns the item with the specified index in this NodeList, + * else null. + */ + public Node item(int index) + { + DomNode child = first; + int count = 0; + while (child != null && count < index) + { + child = child.next; + count++; + } + return child; + } + + /** + * <b>DOM L1 (NodeList)</b> + * Returns the number of elements in this NodeList. + * (Note that many interfaces have a "Length" property, not just + * NodeList, and if a node subtype must implement one of those, + * it will also need to override getChildNodes.) + */ + public int getLength() + { + return length; + } + + /** + * Minimize extra space consumed by this node to hold children and event + * listeners. + */ + public void trimToSize() + { + if (listeners != null && listeners.length != nListeners) + { + ListenerRecord[] newKids = new ListenerRecord[length]; + System.arraycopy(listeners, 0, newKids, 0, nListeners); + listeners = newKids; + } + } + + /** + * <b>DOM L1</b> + * Returns the previous sibling, if one is known. + */ + public Node getNextSibling() + { + return next; + } + + /** + * <b>DOM L1</b> + * Returns the previous sibling, if one is known. + */ + public Node getPreviousSibling() + { + return previous; + } + + /** + * <b>DOM L1</b> + * Returns the parent node, if one is known. + */ + public Node getParentNode() + { + return parent; + } + + /** + * <b>DOM L2</b> + * Consults the DOM implementation to determine if the requested + * feature is supported. DocumentType subclasses must override + * this method, and associate themselves directly with the + * DOMImplementation node used. (This method relies on being able + * to access the DOMImplementation from the owner document, but + * DocumentType nodes can be created without an owner.) + */ + public boolean isSupported(String feature, String version) + { + Document doc = owner; + DOMImplementation impl = null; + + if (doc == null && nodeType == DOCUMENT_NODE) + { + doc = (Document) this; + } + + if (doc == null) + { + // possible for DocumentType + throw new IllegalStateException ("unbound ownerDocument"); + } + + impl = doc.getImplementation(); + return impl.hasFeature(feature, version); + } + + /** + * <b>DOM L1 (modified in L2)</b> + * Returns the owner document. This is only null for Document nodes, + * and (new in L2) for DocumentType nodes which have not yet been + * associated with the rest of their document. + */ + final public Document getOwnerDocument() + { + return owner; + } + + /** + * <b>DOM L1</b> + * Does nothing; this must be overridden (along with the + * getNodeValue method) for nodes with a non-null defined value. + */ + public void setNodeValue(String value) + { + } + + /** + * <b>DOM L1</b> + * Returns null; this must be overridden for nodes types with + * a defined value, along with the setNodeValue method. + */ + public String getNodeValue() + { + return null; + } + + /** This forces GCJ compatibility. + * Without this method GCJ is unable to compile to byte code. + */ + public final short getNodeType() + { + return nodeType; + } + + /** This forces GCJ compatibility. + * Without this method GCJ seems unable to natively compile GNUJAXP. + */ + public abstract String getNodeName(); + + /** + * <b>DOM L2</b> + * Does nothing; this must be overridden (along with the + * getPrefix method) for element and attribute nodes. + */ + public void setPrefix(String prefix) + { + } + + /** + * <b>DOM L2</b> + * Returns null; this must be overridden for element and + * attribute nodes. + */ + public String getPrefix() + { + return null; + } + + /** + * <b>DOM L2</b> + * Returns null; this must be overridden for element and + * attribute nodes. + */ + public String getNamespaceURI() + { + return null; + } + + /** + * <b>DOM L2</b> + * Returns the node name; this must be overridden for element and + * attribute nodes. + */ + public String getLocalName() + { + return null; + } + + /** + * <b>DOM L1</b> + * Returns a clone of this node which optionally includes cloned + * versions of child nodes. Clones are always mutable, except for + * entity reference nodes. + */ + public Node cloneNode(boolean deep) + { + DomNode node = (DomNode) clone(); + + if (deep) + { + DomDocument doc = (nodeType == DOCUMENT_NODE) ? + (DomDocument) node : node.owner; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + DomNode newChild = (DomNode) ctx.cloneNode(deep); + newChild.setOwner(doc); + node.appendChild(newChild); + } + } + + if (nodeType == ENTITY_REFERENCE_NODE) + { + node.makeReadonly(); + } + notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, node); + return node; + } + + void notifyUserDataHandlers(short op, Node src, Node dst) + { + if (userDataHandlers != null) + { + for (Iterator i = userDataHandlers.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + String key = (String) entry.getKey(); + UserDataHandler handler = (UserDataHandler) entry.getValue(); + Object data = userData.get(key); + handler.handle(op, key, data, src, dst); + } + } + } + + /** + * Clones this node; roughly equivalent to cloneNode(false). + * Element subclasses must provide a new implementation which + * invokes this method to handle the basics, and then arranges + * to clone any element attributes directly. Attribute subclasses + * must make similar arrangements, ensuring that existing ties to + * elements are broken by cloning. + */ + public Object clone() + { + try + { + DomNode node = (DomNode) super.clone(); + + node.parent = null; + node.depth = 0; + node.index = 0; + node.length = 0; + node.first = null; + node.last = null; + node.previous = null; + node.next = null; + + node.readonly = false; + node.listeners = null; + node.nListeners = 0; + return node; + + } + catch (CloneNotSupportedException x) + { + throw new Error("clone didn't work"); + } + } + + // the elements-by-tagname stuff is needed for both + // elements and documents ... this is in lieu of a + // common base class between Node and NodeNS. + + /** + * <b>DOM L1</b> + * Creates a NodeList giving array-style access to elements with + * the specified name. Access is fastest if indices change by + * small values, and the DOM is not modified. + */ + public NodeList getElementsByTagName(String tag) + { + return new ShadowList(null, tag); + } + + /** + * <b>DOM L2</b> + * Creates a NodeList giving array-style access to elements with + * the specified namespace and local name. Access is fastest if + * indices change by small values, and the DOM is not modified. + */ + public NodeList getElementsByTagNameNS(String namespace, String local) + { + return new ShadowList(namespace, local); + } + + + // + // This shadow class is GC-able even when the live list it shadows + // can't be, because of event registration hookups. Its finalizer + // makes that live list become GC-able. + // + final class ShadowList + implements NodeList + { + + private LiveNodeList liveList; + + ShadowList(String ns, String local) + { + liveList = new LiveNodeList(ns, local); + } + + public void finalize() + { + liveList.detach(); + liveList = null; + } + + public Node item(int index) + { + return liveList.item(index); + } + + public int getLength() + { + return liveList.getLength(); + } + } + + final class LiveNodeList + implements NodeList, EventListener, NodeFilter + { + + private final boolean matchAnyURI; + private final boolean matchAnyName; + private final String elementURI; + private final String elementName; + + private DomIterator current; + private int lastIndex; + + LiveNodeList(String uri, String name) + { + elementURI = uri; + elementName = name; + matchAnyURI = "*".equals(uri); + matchAnyName = "*".equals(name); + + DomNode.this.addEventListener("DOMNodeInserted", this, true); + DomNode.this.addEventListener("DOMNodeRemoved", this, true); + } + + void detach() + { + current.detach(); + current = null; + + DomNode.this.removeEventListener("DOMNodeInserted", this, true); + DomNode.this.removeEventListener("DOMNodeRemoved", this, true); + } + + public short acceptNode(Node element) + { + if (element == DomNode.this) + { + return FILTER_SKIP; + } + + // use namespace-aware matching ... + if (elementURI != null) + { + if (!(matchAnyURI + || elementURI.equals(element.getNamespaceURI()))) + { + return FILTER_SKIP; + } + if (!(matchAnyName + || elementName.equals(element.getLocalName()))) + { + return FILTER_SKIP; + } + + // ... or qName-based kind. + } + else + { + if (!(matchAnyName + || elementName.equals(element.getNodeName()))) + { + return FILTER_SKIP; + } + } + return FILTER_ACCEPT; + } + + private DomIterator createIterator() + { + return new DomIterator(DomNode.this, + NodeFilter.SHOW_ELEMENT, + this, /* filter */ + true /* expand entity refs */ + ); + } + + public void handleEvent(Event e) + { + MutationEvent mutation = (MutationEvent) e; + Node related = mutation.getRelatedNode(); + + // XXX if it's got children ... check all kids too, they + // will invalidate our saved index + + if (related.getNodeType() != Node.ELEMENT_NODE || + related.getNodeName() != elementName || + related.getNamespaceURI() != elementURI) + { + return; + } + + current = null; + } + + public Node item(int index) + { + if (current == null) + { + current = createIterator(); + lastIndex = -1; + } + + // last node or before? go backwards + if (index <= lastIndex) { + while (index != lastIndex) { + current.previousNode (); + lastIndex--; + } + Node ret = current.previousNode (); + current = null; + return ret; + } + + // somewhere after last node + while (++lastIndex != index) + current.nextNode (); + Node ret = current.nextNode (); + current = null; + return ret; + } + + public int getLength() + { + int retval = 0; + NodeIterator iter = createIterator(); + + while (iter.nextNode() != null) + { + retval++; + } + current = null; + return retval; + } + + } + + // + // EventTarget support + // + static final class ListenerRecord + { + + String type; + EventListener listener; + boolean useCapture; + + // XXX use JDK 1.2 java.lang.ref.WeakReference to listener, + // and we can both get rid of "shadow" classes and remove + // the need for applications to apply similar trix ... but + // JDK 1.2 support isn't generally available yet + + ListenerRecord(String type, EventListener listener, boolean useCapture) + { + this.type = type.intern(); + this.listener = listener; + this.useCapture = useCapture; + } + + boolean equals(ListenerRecord rec) + { + return listener == rec.listener + && useCapture == rec.useCapture + && type == rec.type; + } + + } + + /** + * <b>DOM L2 (Events)</b> + * Returns an instance of the specified type of event object. + * Understands about DOM Mutation, HTML, and UI events. + * + * <p>If the name of the event type begins with "USER-", then an object + * implementing the "Event" class will be returned; this provides a + * limited facility for application-defined events to use the DOM event + * infrastructure. Alternatively, use one of the standard DOM event + * classes and initialize it using use such a "USER-" event type name; + * or defin, instantiate, and initialize an application-specific subclass + * of DomEvent and pass that to dispatchEvent(). + * + * @param eventType Identifies the particular DOM feature module + * defining the type of event, such as "MutationEvents". + * <em>The event "name" is a different kind of "type".</em> + */ + public Event createEvent(String eventType) + { + eventType = eventType.toLowerCase(); + + if ("mutationevents".equals(eventType)) + { + return new DomEvent.DomMutationEvent(null); + } + + if ("htmlevents".equals(eventType) + || "events".equals(eventType) + || "user-events".equals(eventType)) + { + return new DomEvent(null); + } + + if ("uievents".equals(eventType)) + { + return new DomEvent.DomUIEvent(null); + } + + // mouse events + + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, + eventType, null, 0); + } + + /** + * <b>DOM L2 (Events)</b> + * Registers an event listener's interest in a class of events. + */ + public final void addEventListener(String type, + EventListener listener, + boolean useCapture) + { + if (listeners == null) + { + listeners = new ListenerRecord[1]; + } + else if (nListeners == listeners.length) + { + ListenerRecord[] newListeners = + new ListenerRecord[listeners.length + NKIDS_DELTA]; + System.arraycopy(listeners, 0, newListeners, 0, nListeners); + listeners = newListeners; + } + + // prune duplicates + ListenerRecord record; + + record = new ListenerRecord(type, listener, useCapture); + for (int i = 0; i < nListeners; i++) + { + if (record.equals(listeners[i])) + { + return; + } + } + listeners [nListeners++] = record; + } + + // XXX this exception should be discarded from DOM + + // this class can be instantiated, unlike the one in the spec + static final class DomEventException + extends EventException + { + + DomEventException() + { + super(UNSPECIFIED_EVENT_TYPE_ERR, "unspecified event type"); + } + + } + + /** + * <b>DOM L2 (Events)</b> + * Delivers an event to all relevant listeners, returning true if the + * caller should perform their default action. Note that the event + * must have been provided by the createEvent() method on this + * class, else it can't be dispatched. + * + * @see #createEvent + * + * @exception NullPointerException When a null event is passed. + * @exception ClassCastException When the event wasn't provided by + * the createEvent method, or otherwise isn't a DomEvent. + * @exception EventException If the event type wasn't specified + */ + public final boolean dispatchEvent(Event event) + throws EventException + { + DomEvent e = (DomEvent) event; + DomNode[] ancestors = null; + int ancestorMax = 0; + boolean haveDispatchDataLock = false; + + if (e.type == null) + { + throw new DomEventException(); + } + + e.doDefault = true; + e.target = this; + + // + // Typical case: one nonrecursive dispatchEvent call at a time + // for this class. If that's our case, we can avoid allocating + // garbage, which is overall a big win. Even with advanced GCs + // that deal well with short-lived garbage, and wayfast allocators, + // it still helps. + // + // Remember -- EVERY mutation goes though here at least once. + // + // When populating a DOM tree, trying to send mutation events is + // the primary cost; this dominates the critical path. + // + try + { + DomNode current; + int index; + boolean haveAncestorRegistrations = false; + ListenerRecord[] notificationSet; + int ancestorLen; + + synchronized (lockNode) + { + if (!dispatchDataLock) + { + haveDispatchDataLock = dispatchDataLock = true; + notificationSet = DomNode.notificationSet; + ancestors = DomNode.ancestors; + } + else + { + notificationSet = new ListenerRecord[NOTIFICATIONS_INIT]; + ancestors = new DomNode[ANCESTORS_INIT]; + } + ancestorLen = ancestors.length; + } + + // XXX autogrow ancestors ... based on statistics + + // Climb to the top of this subtree and handle capture, letting + // each node (from the top down) capture until one stops it or + // until we get to this one. + + for (index = 0, current = parent; + current != null && index < ancestorLen; + index++, current = current.parent) + { + if (current.nListeners != 0) + { + haveAncestorRegistrations = true; + } + ancestors [index] = current; + } + if (current != null) + { + throw new RuntimeException("dispatchEvent capture stack size"); + } + + ancestorMax = index; + e.stop = false; + + if (haveAncestorRegistrations) + { + e.eventPhase = Event.CAPTURING_PHASE; + while (!e.stop && index-- > 0) + { + current = ancestors [index]; + if (current.nListeners != 0) + { + notifyNode(e, current, true, notificationSet); + } + } + } + + // Always deliver events to the target node (this) + // unless stopPropagation was called. If we saw + // no registrations yet (typical!), we never will. + if (!e.stop && nListeners != 0) + { + e.eventPhase = Event.AT_TARGET; + notifyNode (e, this, false, notificationSet); + } + else if (!haveAncestorRegistrations) + { + e.stop = true; + } + + // If the event bubbles and propagation wasn't halted, + // walk back up the ancestor list. Stop bubbling when + // any bubbled event handler stops it. + + if (!e.stop && e.bubbles) + { + e.eventPhase = Event.BUBBLING_PHASE; + for (index = 0; + !e.stop + && index < ancestorMax + && (current = ancestors[index]) != null; + index++) + { + if (current.nListeners != 0) + { + notifyNode(e, current, false, notificationSet); + } + } + } + e.eventPhase = 0; + + // Caller chooses whether to perform the default + // action based on return from this method. + return e.doDefault; + + } + finally + { + if (haveDispatchDataLock) + { + // synchronize to force write ordering + synchronized (lockNode) + { + // null out refs to ensure they'll be GC'd + for (int i = 0; i < ancestorMax; i++) + { + ancestors [i] = null; + } + // notificationSet handled by notifyNode + + dispatchDataLock = false; + } + } + } + } + + private void notifyNode(DomEvent e, + DomNode current, + boolean capture, + ListenerRecord[] notificationSet) + { + int count = 0; + + // do any of this set of listeners get notified? + for (int i = 0; i < current.nListeners; i++) + { + ListenerRecord rec = current.listeners[i]; + + if (rec.useCapture != capture) + { + continue; + } + if (!e.type.equals (rec.type)) + { + continue; + } + if (count >= notificationSet.length) + { + // very simple growth algorithm + int len = Math.max(notificationSet.length, 1); + ListenerRecord[] tmp = new ListenerRecord[len * 2]; + System.arraycopy(notificationSet, 0, tmp, 0, + notificationSet.length); + notificationSet = tmp; + } + notificationSet[count++] = rec; + } + + // Notify just those listeners + e.currentNode = current; + for (int i = 0; i < count; i++) + { + try + { + // Late in the DOM CR process (3rd or 4th CR?) the + // removeEventListener spec became asymmetric with respect + // to addEventListener ... effect is now immediate. + for (int j = 0; j < current.nListeners; j++) + { + if (current.listeners[j].equals(notificationSet[i])) + { + notificationSet[i].listener.handleEvent(e); + break; + } + } + + } + catch (Exception x) + { + // ignore all exceptions + } + notificationSet[i] = null; // free for GC + } + } + + /** + * <b>DOM L2 (Events)</b> + * Unregisters an event listener. + */ + public final void removeEventListener(String type, + EventListener listener, + boolean useCapture) + { + for (int i = 0; i < nListeners; i++) + { + if (listeners[i].listener != listener) + { + continue; + } + if (listeners[i].useCapture != useCapture) + { + continue; + } + if (!listeners[i].type.equals(type)) + { + continue; + } + + if (nListeners == 1) + { + listeners = null; + nListeners = 0; + } + else + { + for (int j = i + 1; j < nListeners; j++) + { + listeners[i++] = listeners[j++]; + } + listeners[--nListeners] = null; + } + break; + } + // no exceptions reported + } + + /** + * <b>DOM L1 (relocated in DOM L2)</b> + * In this node and all contained nodes (including attributes if + * relevant) merge adjacent text nodes. This is done while ignoring + * text which happens to use CDATA delimiters). + */ + public final void normalize() + { + // Suspend readonly status + boolean saved = readonly; + readonly = false; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + switch (ctx.nodeType) + { + case TEXT_NODE: + while (ctx.next != null && ctx.next.nodeType == TEXT_NODE) + { + Text text = (Text) ctx; + text.appendData(ctx.next.getNodeValue()); + removeChild(ctx.next); + } + break; + case ELEMENT_NODE: + NamedNodeMap attrs = ctx.getAttributes(); + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + attrs.item(i).normalize(); + } + // Fall through + case DOCUMENT_NODE: + case DOCUMENT_FRAGMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_REFERENCE_NODE: + ctx.normalize(); + break; + } + } + readonly = saved; + } + + /** + * Returns true iff node types match, and either (a) both nodes have no + * namespace and their getNodeName() values are the same, or (b) both + * nodes have the same getNamespaceURI() and same getLocalName() values. + * + * <p>Note that notion of a "Per-Element-Type" attribute name scope, as + * found in a non-normative appendix of the XML Namespaces specification, + * is not supported here. Your application must implement that notion, + * typically by not bothering to check nameAndTypeEquals for attributes + * without namespace URIs unless you already know their elements are + * nameAndTypeEquals. + */ + public boolean nameAndTypeEquals(Node other) + { + if (other == this) + { + return true; + } + // node types must match + if (nodeType != other.getNodeType()) + { + return false; + } + + // if both have namespaces, do a "full" comparision + // this is a "global" partition + String ns1 = this.getNamespaceURI(); + String ns2 = other.getNamespaceURI(); + + if (ns1 != null && ns2 != null) + { + return ns1.equals(ns2) && + equal(getLocalName(), other.getLocalName()); + } + + // if neither has a namespace, this is a "no-namespace" name. + if (ns1 == null && ns2 == null) + { + if (!getNodeName().equals(other.getNodeName())) + { + return false; + } + // can test the non-normative "per-element-type" scope here. + // if this is an attribute node and both nodes have been bound + // to elements (!!), then return the nameAndTypeEquals() + // comparison of those elements. + return true; + } + + // otherwise they're unequal: one scoped, one not. + return false; + } + + // DOM Level 3 methods + + public String getBaseURI() + { + return (parent != null) ? parent.getBaseURI() : null; + } + + public short compareDocumentPosition(Node other) + throws DOMException + { + return (short) compareTo(other); + } + + /** + * DOM nodes have a natural ordering: document order. + */ + public final int compareTo(Object other) + { + if (other instanceof DomNode) + { + DomNode n1 = this; + DomNode n2 = (DomNode) other; + if (n1.owner != n2.owner) + { + return 0; + } + int d1 = n1.depth, d2 = n2.depth; + int delta = d1 - d2; + while (d1 > d2) + { + n1 = n1.parent; + d1--; + } + while (d2 > d1) + { + n2 = n2.parent; + d2--; + } + int c = compareTo2(n1, n2); + return (c != 0) ? c : delta; + } + return 0; + } + + /** + * Compare two nodes at the same depth. + */ + final int compareTo2(DomNode n1, DomNode n2) + { + if (n1 == n2 || n1.depth == 0 || n2.depth == 0) + { + return 0; + } + int c = compareTo2(n1.parent, n2.parent); + return (c != 0) ? c : n1.index - n2.index; + } + + public final String getTextContent() + throws DOMException + { + return getTextContent(true); + } + + final String getTextContent(boolean topLevel) + throws DOMException + { + switch (nodeType) + { + case ELEMENT_NODE: + case ENTITY_NODE: + case ENTITY_REFERENCE_NODE: + case DOCUMENT_FRAGMENT_NODE: + StringBuffer buffer = new StringBuffer(); + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + String textContent = ctx.getTextContent(false); + if (textContent != null) + { + buffer.append(textContent); + } + } + return buffer.toString(); + case TEXT_NODE: + case CDATA_SECTION_NODE: + if (((Text) this).isElementContentWhitespace()) + { + return ""; + } + return getNodeValue(); + case ATTRIBUTE_NODE: + return getNodeValue(); + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + return topLevel ? getNodeValue() : ""; + default: + return null; + } + } + + public void setTextContent(String textContent) + throws DOMException + { + switch (nodeType) + { + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_NODE: + case ENTITY_REFERENCE_NODE: + case DOCUMENT_FRAGMENT_NODE: + for (DomNode ctx = first; ctx != null; ) + { + DomNode n = ctx.next; + removeChild(ctx); + ctx = n; + } + if (textContent != null) + { + Text text = owner.createTextNode(textContent); + appendChild(text); + } + break; + case TEXT_NODE: + case CDATA_SECTION_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + setNodeValue(textContent); + break; + } + } + + public boolean isSameNode(Node other) + { + return this == other; + } + + public String lookupPrefix(String namespaceURI) + { + return (parent == null || parent == owner) ? null : + parent.lookupPrefix(namespaceURI); + } + + public boolean isDefaultNamespace(String namespaceURI) + { + return (parent == null || parent == owner) ? false : + parent.isDefaultNamespace(namespaceURI); + } + + public String lookupNamespaceURI(String prefix) + { + return (parent == null || parent == owner) ? null : + parent.lookupNamespaceURI(prefix); + } + + public boolean isEqualNode(Node arg) + { + if (this == arg) + { + return true; + } + if (arg == null) + { + return false; + } + if (nodeType != arg.getNodeType() || + !equal(getNodeName(), arg.getNodeName()) || + !equal(getLocalName(), arg.getLocalName()) || + !equal(getNamespaceURI(), arg.getNamespaceURI()) || + !equal(getPrefix(), arg.getPrefix()) || + !equal(getNodeValue(), arg.getNodeValue())) + { + return false; + } + // Children + Node argCtx = arg.getFirstChild(); + getFirstChild(); // because of DomAttr lazy children + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + if (!ctx.isEqualNode(argCtx)) + { + return false; + } + argCtx = argCtx.getNextSibling(); + } + if (argCtx != null) + { + return false; + } + + // TODO Attr NamedNodeMap + // TODO DocumentType + return true; + } + + boolean equal(String arg1, String arg2) + { + return ((arg1 == null && arg2 == null) || + (arg1 != null && arg1.equals(arg2))); + } + + public Object getFeature(String feature, String version) + { + DOMImplementation impl = (nodeType == DOCUMENT_NODE) ? + ((Document) this).getImplementation() : owner.getImplementation(); + if (impl.hasFeature(feature, version)) + { + return this; + } + return null; + } + + public Object setUserData(String key, Object data, UserDataHandler handler) + { + if (userData == null) + { + userData = new HashMap(); + } + if (handler != null) + { + if (userDataHandlers == null) + { + userDataHandlers = new HashMap(); + } + userDataHandlers.put(key, handler); + } + return userData.put(key, data); + } + + public Object getUserData(String key) + { + if (userData == null) + { + return null; + } + return userData.get(key); + } + + public String toString() + { + String nodeName = getNodeName(); + String nodeValue = getNodeValue(); + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + if (nodeName != null) + { + buf.append(nodeName); + } + if (nodeValue != null) + { + if (nodeName != null) + { + buf.append('='); + } + buf.append('\''); + buf.append(encode(nodeValue)); + buf.append('\''); + } + buf.append(']'); + return buf.toString(); + } + + String encode(String value) + { + StringBuffer buf = null; + int len = value.length(); + for (int i = 0; i < len; i++) + { + char c = value.charAt(i); + if (c == '\n') + { + if (buf == null) + { + buf = new StringBuffer(value.substring(0, i)); + } + buf.append("\\n"); + } + else if (c == '\r') + { + if (buf == null) + { + buf = new StringBuffer(value.substring(0, i)); + } + buf.append("\\r"); + } + else if (buf != null) + { + buf.append(c); + } + } + return (buf != null) ? buf.toString() : value; + } + + String nodeTypeToString(short nodeType) + { + switch (nodeType) + { + case ELEMENT_NODE: + return "ELEMENT_NODE"; + case ATTRIBUTE_NODE: + return "ATTRIBUTE_NODE"; + case TEXT_NODE: + return "TEXT_NODE"; + case CDATA_SECTION_NODE: + return "CDATA_SECTION_NODE"; + case DOCUMENT_NODE: + return "DOCUMENT_NODE"; + case DOCUMENT_TYPE_NODE: + return "DOCUMENT_TYPE_NODE"; + case COMMENT_NODE: + return "COMMENT_NODE"; + case PROCESSING_INSTRUCTION_NODE: + return "PROCESSING_INSTRUCTION_NODE"; + case DOCUMENT_FRAGMENT_NODE: + return "DOCUMENT_FRAGMENT_NODE"; + case ENTITY_NODE: + return "ENTITY_NODE"; + case ENTITY_REFERENCE_NODE: + return "ENTITY_REFERENCE_NODE"; + case NOTATION_NODE: + return "NOTATION_NODE"; + default: + return "UNKNOWN"; + } + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomNodeIterator.java b/libjava/classpath/gnu/xml/dom/DomNodeIterator.java new file mode 100644 index 00000000000..6079f7a126a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNodeIterator.java @@ -0,0 +1,327 @@ +/* DomNodeIterator.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; +import org.w3c.dom.traversal.TreeWalker; + +/** + * Node iterator and tree walker. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomNodeIterator + implements NodeIterator, TreeWalker +{ + + Node root; + final int whatToShow; + final NodeFilter filter; + final boolean entityReferenceExpansion; + final boolean walk; + Node current; + + public DomNodeIterator(Node root, int whatToShow, NodeFilter filter, + boolean entityReferenceExpansion, boolean walk) + { + if (root == null) + { + throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root"); + } + this.root = root; + this.whatToShow = whatToShow; + this.filter = filter; + this.entityReferenceExpansion = entityReferenceExpansion; + this.walk = walk; + current = root; + } + + public Node getRoot() + { + return root; + } + + public int getWhatToShow() + { + return whatToShow; + } + + public NodeFilter getFilter() + { + return filter; + } + + public boolean getExpandEntityReferences() + { + return entityReferenceExpansion; + } + + public Node nextNode() + throws DOMException + { + if (root == null) + { + throw new DOMException(DOMException.INVALID_STATE_ERR, "null root"); + } + Node ret; + do + { + if (current.equals(root)) + { + ret = root.getFirstChild(); + } + else if (walk) + { + ret = current.getFirstChild(); + if (ret == null) + { + ret = current.getNextSibling(); + } + if (ret == null) + { + Node tmp = current; + ret = tmp.getParentNode(); + while (!ret.equals(root) && tmp.equals(ret.getLastChild())) + { + tmp = ret; + ret = tmp.getParentNode(); + } + if (ret.equals(root)) + { + ret = null; + } + else + { + ret = ret.getNextSibling(); + } + } + } + else + { + ret = current.getNextSibling(); + } + } + while (!accept(ret)); + current = (ret == null) ? current : ret; + return ret; + } + + public Node previousNode() + throws DOMException + { + if (root == null) + { + throw new DOMException(DOMException.INVALID_STATE_ERR, "null root"); + } + Node ret; + do + { + if (current.equals(root)) + { + ret = current.getLastChild(); + } + else if (walk) + { + ret = current.getLastChild(); + if (ret == null) + { + ret = current.getPreviousSibling(); + } + if (ret == null) + { + Node tmp = current; + ret = tmp.getParentNode(); + while (!ret.equals(root) && tmp.equals(ret.getFirstChild())) + { + tmp = ret; + ret = tmp.getParentNode(); + } + if (ret.equals(root)) + { + ret = null; + } + else + { + ret = ret.getPreviousSibling(); + } + } + } + else + { + ret = current.getPreviousSibling(); + } + } + while (!accept(ret)); + current = (ret == null) ? current : ret; + return ret; + } + + public Node getCurrentNode() + { + return current; + } + + public void setCurrentNode(Node current) + throws DOMException + { + if (current == null) + { + throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root"); + } + this.current = current; + } + + public Node parentNode() + { + Node ret = current.getParentNode(); + if (!accept (ret)) + { + ret = null; + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node firstChild () + { + Node ret = current.getFirstChild(); + while (!accept(ret)) + { + ret = ret.getNextSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node lastChild() + { + Node ret = current.getLastChild(); + while (!accept(ret)) + { + ret = ret.getPreviousSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node previousSibling() + { + Node ret = current.getPreviousSibling(); + while (!accept(ret)) + { + ret = ret.getPreviousSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node nextSibling() + { + Node ret = current.getNextSibling(); + while (!accept(ret)) + { + ret = ret.getNextSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public void detach() + { + root = null; + } + + boolean accept(Node node) + { + if (node == null) + { + return true; + } + boolean ret; + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0; + break; + case Node.CDATA_SECTION_NODE: + ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0; + break; + case Node.COMMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0; + break; + case Node.DOCUMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0; + break; + case Node.DOCUMENT_FRAGMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0; + break; + case Node.DOCUMENT_TYPE_NODE: + ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0; + break; + case Node.ELEMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0; + break; + case Node.ENTITY_NODE: + ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0; + break; + case Node.ENTITY_REFERENCE_NODE: + ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0; + ret = ret && entityReferenceExpansion; + break; + case Node.NOTATION_NODE: + ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0; + break; + case Node.PROCESSING_INSTRUCTION_NODE: + ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0; + break; + case Node.TEXT_NODE: + ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0; + break; + default: + ret = true; + } + if (ret && filter != null) + { + ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT); + } + return ret; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNotation.java b/libjava/classpath/gnu/xml/dom/DomNotation.java new file mode 100644 index 00000000000..26e78724f27 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNotation.java @@ -0,0 +1,103 @@ +/* DomNotation.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Notation; + +/** + * <p> "Notation" implementation. This is a non-core DOM class, supporting + * the "XML" feature. </p> + * + * <p> Although unparsed entities using this notation can be detected using + * DOM, neither NOTATIONS nor ENTITY/ENTITIES attributes can be so detected. + * More, there is no portable way to construct a Notation node, so there's + * no way that vendor-neutral DOM construction APIs could even report a + * NOTATION used to identify the intended meaning of a ProcessingInstruction. + * </p> + * + * <p> In short, <em>avoid using this DOM functionality</em>. + * + * @see DomDoctype + * @see DomEntity + * @see DomPI + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomNotation + extends DomExtern + implements Notation +{ + + /** + * Constructs a Notation node associated with the specified document, + * with the specified descriptive data. Note that at least one of + * the PUBLIC and SYSTEM identifiers must be provided; unlike other + * external objects in XML, notations may have only a PUBLIC identifier. + * + * <p>This constructor should only be invoked by a DomDoctype object + * as part of its declareNotation functionality, or through a subclass + * which is similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this notation is associated + * @param name Name of this notation + * @param publicId If non-null, provides the notation's PUBLIC identifier + * @param systemId If non-null, rovides the notation's SYSTEM identifier + */ + protected DomNotation(DomDocument owner, + String name, + String publicId, + String systemId) + { + super(NOTATION_NODE, owner, name, publicId, systemId); + makeReadonly(); + } + + /** + * The base URI of an external entity is its system ID. + * The base URI of an internal entity is the parent document's base URI. + * @since DOM Level 3 Core + */ + public String getBaseURI() + { + String systemId = getSystemId(); + return (systemId == null) ? owner.getBaseURI() : systemId; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomNsNode.java b/libjava/classpath/gnu/xml/dom/DomNsNode.java new file mode 100644 index 00000000000..b27514ecb56 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNsNode.java @@ -0,0 +1,200 @@ +/* DomNsNode.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import javax.xml.XMLConstants; +import org.w3c.dom.DOMException; + +/** + * <p> Abstract implemention of namespace support. This facilitates + * sharing code for attribute and element nodes. + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class DomNsNode + extends DomNode +{ + + private String name; + private String namespace; + private String prefix; + String localName; + + /** + * Constructs a node associated with the specified document, and + * with the specified namespace information. + * + * @param owner The document with which this entity is associated + * @param namespaceURI Combined with the local part of the name, + * this identifies a type of element or attribute; may be null. + * If this is the empty string, it is reassigned as null so that + * applications only need to test that case. + * @param name Name of this node, which may include a prefix + */ + // package private + DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name) + { + super(nodeType, owner); + setNodeName(name); + setNamespaceURI(namespaceURI); + } + + /** + * <b>DOM L1</b> + * Returns the node's name, including any namespace prefix. + */ + public final String getNodeName() + { + return name; + } + + final void setNodeName(String name) + { + this.name = name.intern(); + int index = name.indexOf(':'); + if (index == -1) + { + prefix = null; + localName = this.name; + } + else + { + prefix = name.substring(0, index).intern(); + localName = name.substring(index + 1).intern(); + } + } + + /** + * <b>DOM L2</b> + * Returns the node's namespace URI + * <em>or null</em> if the node name is not namespace scoped. + */ + public final String getNamespaceURI() + { + return namespace; + } + + final void setNamespaceURI(String namespaceURI) + { + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + namespace = (namespaceURI == null) ? null : namespaceURI.intern(); + } + + /** + * <b>DOM L2</b> + * Returns any prefix part of the node's name (before any colon). + */ + public final String getPrefix() + { + return prefix; + } + + /** + * <b>DOM L2</b> + * Assigns the prefix part of the node's name (before any colon). + */ + public final void setPrefix(String prefix) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + + if (prefix == null) + { + name = localName; + return; + } + else if (namespace == null) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "can't set prefix, node has no namespace URI", + this, 0); + } + + DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion())); + if (prefix.indexOf (':') != -1) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "illegal prefix " + prefix, this, 0); + } + + if (XMLConstants.XML_NS_PREFIX.equals(prefix) + && !XMLConstants.XML_NS_URI.equals(namespace)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace is always " + + XMLConstants.XML_NS_URI, this, 0); + } + + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) + { + if (namespace != null || getNodeType() != ATTRIBUTE_NODE) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns attribute prefix is reserved", + this, 0); + } + } + else if (getNodeType () == ATTRIBUTE_NODE + && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:"))) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "namespace declarations can't change names", + this, 0); + } + + this.prefix = prefix.intern(); + } + + /** + * <b>DOM L2</b> + * Returns the local part of the node's name (after any colon). + */ + public final String getLocalName() + { + return localName; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java b/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java new file mode 100644 index 00000000000..2c90967d00f --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java @@ -0,0 +1,147 @@ +/* DomProcessingInstruction.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.ProcessingInstruction; + +/** + * <p> "ProcessingInstruction" (PI) implementation. + * This is a non-core DOM class, supporting the "XML" feature. </p> + * + * <p> Unlike other DOM APIs in the "XML" feature, this one fully + * exposes the functionality it describes. So there is no reason + * inherent in DOM to avoid using this API, unless you want to rely + * on NOTATION declarations to associate meaning with your PIs; + * there is no vendor-neutal way to record those notations in DOM.</p> + * + * <p> Also of note is that PI support is part of SAX, so that XML + * systems using PIs can choose among multiple APIs. </p> + * + * @see DomNotation + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomProcessingInstruction + extends DomNode + implements ProcessingInstruction +{ + + private String target; + private String data; + + /** + * Constructs a ProcessingInstruction node associated with the + * specified document, with the specified data. + * + * <p>This constructor should only be invoked by a Document object as + * part of its createProcessingInstruction functionality, or through + * a subclass which is similarly used in a "Sub-DOM" style layer. + */ + protected DomProcessingInstruction(DomDocument owner, + String target, String data) + { + super(PROCESSING_INSTRUCTION_NODE, owner); + this.target = target; + this.data = data; + } + + /** + * <b>DOM L1</b> + * Returns the target of the processing instruction. + */ + public final String getTarget() + { + return target; + } + + /** + * <b>DOM L1</b> + * Returns the target of the processing instruction + * (same as getTarget). + */ + public final String getNodeName() + { + return target; + } + + /** + * <b>DOM L1</b> + * Returns the data associated with the processing instruction. + */ + public final String getData() + { + return data; + } + + /** + * <b>DOM L1</b> + * Returns the data associated with the processing instruction + * (same as getData). + */ + public final String getNodeValue() + { + return data; + } + + /** + * <b>DOM L1</b> + * Assigns the data associated with the processing instruction; + * same as setNodeValue. + */ + public final void setData(String data) + { + setNodeValue(data); + } + + /** + * <b>DOM L1</b> + * Assigns the data associated with the processing instruction. + */ + public final void setNodeValue(String data) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + this.data = data; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/DomText.java b/libjava/classpath/gnu/xml/dom/DomText.java new file mode 100644 index 00000000000..3ca17dc9b87 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomText.java @@ -0,0 +1,220 @@ +/* DomText.java -- + Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Text; + +/** + * <p> "Text" implementation. </p> + * + * @author David Brownell + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomText + extends DomCharacterData + implements Text +{ + + // NOTE: deleted unused per-instance "isIgnorable" + // support to reclaim its space. + + /** + * Constructs a text node associated with the specified + * document and holding the specified data. + * + * <p>This constructor should only be invoked by a Document object + * as part of its createTextNode functionality, or through a subclass + * which is similarly used in a "Sub-DOM" style layer. + */ + protected DomText(DomDocument owner, String value) + { + super(TEXT_NODE, owner, value); + } + + protected DomText(DomDocument owner, char[] buf, int off, int len) + { + super(TEXT_NODE, owner, buf, off, len); + } + + // Used by DomCDATA + DomText(short nodeType, DomDocument owner, String value) + { + super(nodeType, owner, value); + } + + DomText(short nodeType, DomDocument owner, char[] buf, int off, int len) + { + super(nodeType, owner, buf, off, len); + } + + /** + * <b>DOM L1</b> + * Returns the string "#text". + */ + // can't be 'final' with CDATA subclassing + public String getNodeName() + { + return "#text"; + } + + /** + * <b>DOM L1</b> + * Splits this text node in two parts at the offset, returning + * the new text node (the sibling with the second part). + */ + public Text splitText(int offset) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + try + { + String text = getNodeValue(); + String before = text.substring(0, offset); + String after = text.substring(offset); + Text next; + + if (getNodeType() == TEXT_NODE) + { + next = owner.createTextNode(after); + } + else // CDATA_SECTION_NODE + { + next = owner.createCDATASection(after); + } + + if (this.next != null) + { + parent.insertBefore(next, this.next); + } + else + { + parent.appendChild(next); + } + setNodeValue(before); + return next; + + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + // DOM Level 3 + + public boolean isElementContentWhitespace() + { + if (parent != null) + { + DomDoctype doctype = (DomDoctype) owner.getDoctype(); + if (doctype != null) + { + DTDElementTypeInfo info = + doctype.getElementTypeInfo(parent.getNodeName()); + if (info != null) + { + if (info.model == null && info.model.indexOf("#PCDATA") != -1) + { + return false; + } + return getNodeValue().trim().length() == 0; + } + } + } + return false; + } + + public String getWholeText() + { + DomNode ref = this; + DomNode ctx; + for (ctx = previous; ctx != null && + (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE); + ctx = ctx.previous) + { + ref = ctx; + } + StringBuffer buf = new StringBuffer(ref.getNodeValue()); + for (ctx = ref.next; ctx != null && + (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE); + ctx = ctx.next) + { + buf.append(ctx.getNodeValue()); + } + return buf.toString (); + } + + public Text replaceWholeText(String content) + throws DOMException + { + boolean isEmpty = (content == null || content.length () == 0); + if (!isEmpty) + { + setNodeValue(content); + } + + DomNode ref = this; + DomNode ctx; + for (ctx = previous; ctx != null && + (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE); + ctx = ctx.previous) + { + ref = ctx; + } + ctx = ref.next; + if ((isEmpty || ref != this) && parent != null) + { + parent.removeChild(ref); + } + for (; ctx != null && + (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE); + ctx = ref) + { + ref = ctx.next; + if ((isEmpty || ctx != this) && parent != null) + { + parent.removeChild(ctx); + } + } + return (isEmpty) ? null : this; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomXPathExpression.java b/libjava/classpath/gnu/xml/dom/DomXPathExpression.java new file mode 100644 index 00000000000..25187520c0f --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomXPathExpression.java @@ -0,0 +1,147 @@ +/* DomXPathExpression.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathNSResolver; +import org.w3c.dom.xpath.XPathResult; +import gnu.xml.xpath.DocumentOrderComparator; + +/** + * An XPath expression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomXPathExpression +implements org.w3c.dom.xpath.XPathExpression +{ + + final DomDocument doc; + final XPathExpression expression; + final XPathNSResolver resolver; + + DomXPathExpression(DomDocument doc, String expression, + XPathNSResolver resolver) + throws XPathException + { + this.doc = doc; + this.resolver = resolver; + + XPathFactory factory = XPathFactory.newInstance(); + XPath xpath = factory.newXPath(); + if (resolver != null) + { + xpath.setNamespaceContext(new DomNSResolverContext(resolver)); + } + try + { + this.expression = xpath.compile(expression); + } + catch (XPathExpressionException e) + { + throw new XPathException(XPathException.INVALID_EXPRESSION_ERR, + e.getMessage ()); + } + } + + public Object evaluate(Node contextNode, short type, Object result) + throws XPathException, DOMException + { + try + { + QName typeName = null; + switch (type) + { + case XPathResult.BOOLEAN_TYPE: + typeName = XPathConstants.BOOLEAN; + break; + case XPathResult.NUMBER_TYPE: + typeName = XPathConstants.NUMBER; + break; + case XPathResult.STRING_TYPE: + typeName = XPathConstants.STRING; + break; + case XPathResult.ANY_UNORDERED_NODE_TYPE: + case XPathResult.FIRST_ORDERED_NODE_TYPE: + typeName = XPathConstants.NODE; + break; + case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: + case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE: + case XPathResult.ORDERED_NODE_ITERATOR_TYPE: + case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: + typeName = XPathConstants.NODESET; + break; + default: + throw new XPathException(XPathException.TYPE_ERR, null); + } + Object val = expression.evaluate(contextNode, typeName); + switch (type) + { + case XPathResult.ORDERED_NODE_ITERATOR_TYPE: + case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: + // Sort the nodes + List ns = new ArrayList((Collection) val); + Collections.sort(ns, new DocumentOrderComparator()); + val = ns; + } + return new DomXPathResult(val, type); + } + catch (javax.xml.xpath.XPathException e) + { + throw new XPathException(XPathException.TYPE_ERR, e.getMessage()); + } + } + + public String toString () + { + return getClass ().getName () + "[expression=" + expression + "]"; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java b/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java new file mode 100644 index 00000000000..96dfd97d46a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java @@ -0,0 +1,64 @@ +/* DomXPathNSResolver.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * Generic XPath namespace resolver using a DOM Node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomXPathNSResolver +implements XPathNSResolver +{ + + Node node; + + DomXPathNSResolver (Node node) + { + this.node = node; + } + + public String lookupNamespaceURI (String prefix) + { + return node.lookupNamespaceURI (prefix); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomXPathResult.java b/libjava/classpath/gnu/xml/dom/DomXPathResult.java new file mode 100644 index 00000000000..bed249624f9 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomXPathResult.java @@ -0,0 +1,233 @@ +/* DomXPathResult.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.Collection; +import java.util.Iterator; +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathResult; + +/** + * An XPath result object. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomXPathResult +implements XPathResult +{ + + final Object value; + final short type; + Iterator iterator; + + DomXPathResult (Object value, short requestedType) + { + this.value = value; + if (value instanceof Boolean) + { + type = XPathResult.BOOLEAN_TYPE; + } + else if (value instanceof Double) + { + type = XPathResult.NUMBER_TYPE; + } + else if (value instanceof String) + { + type = XPathResult.STRING_TYPE; + } + else if (value instanceof Collection) + { + Collection ns = (Collection) value; + switch (requestedType) + { + case XPathResult.ANY_TYPE: + case XPathResult.ANY_UNORDERED_NODE_TYPE: + type = (ns.size () == 1) ? XPathResult.FIRST_ORDERED_NODE_TYPE : + XPathResult.ORDERED_NODE_ITERATOR_TYPE; + break; + default: + type = requestedType; + } + iterator = ns.iterator (); + } + else + { + throw new IllegalArgumentException (); + } + } + + public boolean getBooleanValue() + { + if (type == XPathResult.BOOLEAN_TYPE) + { + return ((Boolean) value).booleanValue (); + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public boolean getInvalidIteratorState() + { + return iterator == null; + } + + public double getNumberValue() + { + if (type == XPathResult.NUMBER_TYPE) + { + return ((Double) value).doubleValue (); + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public short getResultType() + { + return type; + } + + public Node getSingleNodeValue() + { + switch (type) + { + case XPathResult.FIRST_ORDERED_NODE_TYPE: + case XPathResult.ORDERED_NODE_ITERATOR_TYPE: + case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: + case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: + case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE: + Collection ns = (Collection) value; + if (ns.isEmpty ()) + { + return null; + } + else + { + return (Node) ns.iterator ().next (); + } + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public int getSnapshotLength() + { + switch (type) + { + case XPathResult.FIRST_ORDERED_NODE_TYPE: + case XPathResult.ORDERED_NODE_ITERATOR_TYPE: + case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: + case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: + case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE: + return ((Collection) value).size (); + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public String getStringValue() + { + if (type == XPathResult.STRING_TYPE) + { + return (String) value; + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public Node iterateNext() + { + if (iterator != null) + { + if (iterator.hasNext ()) + { + return (Node) iterator.next (); + } + else + { + iterator = null; + return null; + } + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public Node snapshotItem(int index) + { + switch (type) + { + case XPathResult.FIRST_ORDERED_NODE_TYPE: + case XPathResult.ORDERED_NODE_ITERATOR_TYPE: + case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: + case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: + case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE: + Collection ns = (Collection) value; + Node[] nodes = new Node[ns.size ()]; + ns.toArray (nodes); + return nodes[index]; + } + throw new XPathException (XPathException.TYPE_ERR, value.toString ()); + } + + public String toString () + { + return getClass ().getName () + "[type=" + typeName (type) + ",value=" + + value + ']'; + } + + private String typeName (short type) + { + switch (type) + { + case XPathResult.BOOLEAN_TYPE: + return "BOOLEAN_TYPE"; + case XPathResult.NUMBER_TYPE: + return "NUMBER_TYPE"; + case XPathResult.STRING_TYPE: + return "STRING_TYPE"; + case XPathResult.FIRST_ORDERED_NODE_TYPE: + return "FIRST_ORDERED_NODE_TYPE"; + case XPathResult.ORDERED_NODE_ITERATOR_TYPE: + return "ORDERED_NODE_ITERATOR_TYPE"; + case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE: + return "ORDERED_NODE_SNAPSHOT_TYPE"; + case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: + return "UNORDERED_NODE_ITERATOR_TYPE"; + case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE: + return "UNORDERED_NODE_SNAPSHOT_TYPE"; + default: + return "(unknown)"; + } + } + +} diff --git a/libjava/classpath/gnu/xml/dom/ImplementationList.java b/libjava/classpath/gnu/xml/dom/ImplementationList.java new file mode 100644 index 00000000000..8c0f4491610 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ImplementationList.java @@ -0,0 +1,70 @@ +/* ImplementationList.java -- + Copyright (C) 2004 Free Software Foundation, Inc.. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.xml.dom; + +import java.util.List; + +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.DOMImplementationList; + +/** + * Implementation list for GNU JAXP. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class ImplementationList + implements DOMImplementationList +{ + + private List list; + + ImplementationList(List list) + { + this.list = list; + } + + public int getLength() + { + return list.size(); + } + + public DOMImplementation item(int index) + { + return (DOMImplementation) list.get(index); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/ImplementationSource.java b/libjava/classpath/gnu/xml/dom/ImplementationSource.java new file mode 100644 index 00000000000..913079cd0c3 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ImplementationSource.java @@ -0,0 +1,167 @@ +/* ImplementationSource.java -- + Copyright (C) 2004 Free Software Foundation, Inc.. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.xml.dom; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.DOMImplementationList; +import org.w3c.dom.DOMImplementationSource; + +/** + * Implementation source for GNU JAXP. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class ImplementationSource + implements DOMImplementationSource +{ + + private static final String DIGITS = "1234567890"; + + /* + * GNU DOM implementations. + */ + private static final DOMImplementation[] implementations; + + static + { + List acc = new ArrayList(); + acc.add(new gnu.xml.dom.DomImpl()); + try + { + Class t = Class.forName("gnu.xml.libxmlj.dom.GnomeDocumentBuilder"); + acc.add(t.newInstance()); + } + catch (Exception e) + { + // libxmlj not available + } + catch (UnsatisfiedLinkError e) + { + // libxmlj not available + } + implementations = new DOMImplementation[acc.size()]; + acc.toArray(implementations); + } + + public DOMImplementation getDOMImplementation(String features) + { + List available = getImplementations(features); + if (available.isEmpty()) + { + return null; + } + return (DOMImplementation) available.get(0); + } + + public DOMImplementationList getDOMImplementationList(String features) + { + List available = getImplementations(features); + return new ImplementationList(available); + } + + /** + * Returns a list of the implementations that support the specified + * features. + */ + private final List getImplementations(String features) + { + List available = new ArrayList(Arrays.asList(implementations)); + for (Iterator i = parseFeatures(features).iterator(); i.hasNext(); ) + { + String feature = (String) i.next(); + String version = null; + int si = feature.indexOf(' '); + if (si != -1) + { + version = feature.substring(si + 1); + feature = feature.substring(0, si); + } + for (Iterator j = available.iterator(); j.hasNext(); ) + { + DOMImplementation impl = (DOMImplementation) j.next(); + if (!impl.hasFeature(feature, version)) + { + j.remove(); + } + } + } + return available; + } + + /** + * Parses the feature list into feature tokens. + */ + final List parseFeatures(String features) + { + List list = new ArrayList(); + int pos = 0, start = 0; + int len = features.length(); + for (; pos < len; pos++) + { + char c = features.charAt(pos); + if (c == ' ') + { + if (pos + 1 < len && + DIGITS.indexOf(features.charAt(pos + 1)) == -1) + { + list.add(getFeature(features, start, pos)); + start = pos + 1; + } + } + } + if (pos > start) + { + list.add(getFeature(features, start, len)); + } + return list; + } + + final String getFeature(String features, int start, int end) + { + if (features.length() > 0 && features.charAt(start) == '+') + { + return features.substring(start + 1, end); + } + return features.substring(start, end); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/JAXPFactory.java b/libjava/classpath/gnu/xml/dom/JAXPFactory.java new file mode 100644 index 00000000000..8f481fad643 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/JAXPFactory.java @@ -0,0 +1,286 @@ +/* JAXPFactory.java -- + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.io.IOException; + +import org.w3c.dom.Document; +import org.w3c.dom.DOMImplementation; + +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; + + +/** + * DOM bootstrapping API, for use with JAXP. + * + * @see Consumer + * + * @author David Brownell + */ +public final class JAXPFactory + extends DocumentBuilderFactory +{ + + private static final String PROPERTY = "http://xml.org/sax/properties/"; + private static final String FEATURE = "http://xml.org/sax/features/"; + + private SAXParserFactory pf; + + /** + * Default constructor. + */ + public JAXPFactory() + { + } + + /** + * Constructs a JAXP document builder which uses the default + * JAXP SAX2 parser and the DOM implementation in this package. + */ + public DocumentBuilder newDocumentBuilder() + throws ParserConfigurationException + { + if (pf == null) + { + // Force use of AElfred2 since not all JAXP parsers + // conform very well to the SAX2 API spec ... + pf = new gnu.xml.aelfred2.JAXPFactory(); + // pf = SAXParserFactory.newInstance (); + } + + // JAXP default: false + pf.setValidating(isValidating()); + + // FIXME: this namespace setup may cause errors in some + // conformant SAX2 parsers, which we CAN patch up by + // splicing a "NSFilter" stage up front ... + + // JAXP default: false + pf.setNamespaceAware(isNamespaceAware()); + + try + { + // undo rude "namespace-prefixes=false" default + pf.setFeature(FEATURE + "namespace-prefixes", true); + + return new JAXPBuilder(pf.newSAXParser().getXMLReader(), this); + } + catch (SAXException e) + { + String msg = "can't create JAXP DocumentBuilder: " + e.getMessage(); + throw new ParserConfigurationException(msg); + } + } + + /** There seems to be no useful specification for attribute names */ + public void setAttribute(String name, Object value) + throws IllegalArgumentException + { + if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name)) + { + // TODO + } + else + { + throw new IllegalArgumentException(name); + } + } + + /** There seems to be no useful specification for attribute names */ + public Object getAttribute(String name) + throws IllegalArgumentException + { + throw new IllegalArgumentException(name); + } + + static final class JAXPBuilder + extends DocumentBuilder + implements ErrorHandler + { + + private Consumer consumer; + private XMLReader producer; + private DomImpl impl; + + JAXPBuilder(XMLReader parser, JAXPFactory factory) + throws ParserConfigurationException + { + impl = new DomImpl(); + + // set up consumer side + try + { + consumer = new Consumer(); + } + catch (SAXException e) + { + throw new ParserConfigurationException(e.getMessage()); + } + + // JAXP defaults: true, noise nodes are good (bleech) + consumer.setHidingReferences(factory.isExpandEntityReferences()); + consumer.setHidingComments(factory.isIgnoringComments()); + consumer.setHidingWhitespace(factory.isIgnoringElementContentWhitespace()); + consumer.setHidingCDATA(factory.isCoalescing()); + + // set up producer side + producer = parser; + producer.setContentHandler(consumer.getContentHandler()); + producer.setDTDHandler(consumer.getDTDHandler()); + + try + { + String id; + + // if validating, report validity errors, and default + // to treating them as fatal + if (factory.isValidating ()) + { + producer.setFeature(FEATURE + "validation", true); + producer.setErrorHandler(this); + } + + // always save prefix info, maybe do namespace processing + producer.setFeature(FEATURE + "namespace-prefixes", true); + producer.setFeature(FEATURE + "namespaces", + factory.isNamespaceAware()); + + // set important handlers + id = PROPERTY + "lexical-handler"; + producer.setProperty(id, consumer.getProperty(id)); + + id = PROPERTY + "declaration-handler"; + producer.setProperty(id, consumer.getProperty(id)); + + } + catch (SAXException e) + { + throw new ParserConfigurationException(e.getMessage()); + } + } + + public Document parse(InputSource source) + throws SAXException, IOException + { + producer.parse(source); + Document doc = consumer.getDocument(); + // TODO inputEncoding + doc.setDocumentURI(source.getSystemId()); + return doc; + } + + public boolean isNamespaceAware() + { + try + { + return producer.getFeature(FEATURE + "namespaces"); + } + catch (SAXException e) + { + // "can't happen" + throw new RuntimeException(e.getMessage()); + } + } + + public boolean isValidating() + { + try + { + return producer.getFeature(FEATURE + "validation"); + } + catch (SAXException e) + { + // "can't happen" + throw new RuntimeException(e.getMessage()); + } + } + + public void setEntityResolver(EntityResolver resolver) + { + producer.setEntityResolver(resolver); + } + + public void setErrorHandler(ErrorHandler handler) + { + producer.setErrorHandler(handler); + consumer.setErrorHandler(handler); + } + + public DOMImplementation getDOMImplementation() + { + return impl; + } + + public Document newDocument() + { + return new DomDocument(); + } + + // implementation of error handler that's used when validating + public void fatalError(SAXParseException e) + throws SAXException + { + throw e; + } + + public void error(SAXParseException e) + throws SAXException + { + throw e; + } + + public void warning(SAXParseException e) + throws SAXException + { + /* ignore */ + } + + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java new file mode 100644 index 00000000000..5da91330604 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java @@ -0,0 +1,189 @@ +/* DomHTMLAnchorElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLAnchorElement; + +/** + * An HTML 'A' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLAnchorElement + extends DomHTMLElement + implements HTMLAnchorElement +{ + + protected DomHTMLAnchorElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public String getCharset() + { + return getHTMLAttribute("charset"); + } + + public void setCharset(String charset) + { + setHTMLAttribute("charset", charset); + } + + public String getCoords() + { + return getHTMLAttribute("coords"); + } + + public void setCoords(String coords) + { + setHTMLAttribute("coords", coords); + } + + public String getHref() + { + return getHTMLAttribute("href"); + } + + public void setHref(String href) + { + setHTMLAttribute("href", href); + } + + public String getHreflang() + { + return getHTMLAttribute("hreflang"); + } + + public void setHreflang(String hreflang) + { + setHTMLAttribute("hreflang", hreflang); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getRel() + { + return getHTMLAttribute("rel"); + } + + public void setRel(String rel) + { + setHTMLAttribute("rel", rel); + } + + public String getRev() + { + return getHTMLAttribute("rev"); + } + + public void setRev(String rev) + { + setHTMLAttribute("rev", rev); + } + + public String getShape() + { + return getHTMLAttribute("shape"); + } + + public void setShape(String shape) + { + setHTMLAttribute("shape", shape); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public String getTarget() + { + return getHTMLAttribute("target"); + } + + public void setTarget(String target) + { + setHTMLAttribute("target", target); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + + public void blur() + { + dispatchUIEvent("blur"); + } + + public void focus() + { + dispatchUIEvent("focus"); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java new file mode 100644 index 00000000000..8ec4d3c83bb --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java @@ -0,0 +1,169 @@ +/* DomHTMLAppletElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLAppletElement; + +/** + * An HTML 'APPLET' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLAppletElement + extends DomHTMLElement + implements HTMLAppletElement +{ + + protected DomHTMLAppletElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getAlt() + { + return getHTMLAttribute("alt"); + } + + public void setAlt(String alt) + { + setHTMLAttribute("alt", alt); + } + + public String getArchive() + { + return getHTMLAttribute("archive"); + } + + public void setArchive(String archive) + { + setHTMLAttribute("archive", archive); + } + + public String getCode() + { + return getHTMLAttribute("code"); + } + + public void setCode(String code) + { + setHTMLAttribute("code", code); + } + + public String getCodeBase() + { + return getHTMLAttribute("codebase"); + } + + public void setCodeBase(String codeBase) + { + setHTMLAttribute("codebase", codeBase); + } + + public String getHeight() + { + return getHTMLAttribute("height"); + } + + public void setHeight(String height) + { + setHTMLAttribute("height", height); + } + + public int getHspace() + { + return getIntHTMLAttribute("hspace"); + } + + public void setHspace(int hspace) + { + setIntHTMLAttribute("hspace", hspace); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getObject() + { + return getHTMLAttribute("object"); + } + + public void setObject(String object) + { + setHTMLAttribute("object", object); + } + + public int getVspace() + { + return getIntHTMLAttribute("vspace"); + } + + public void setVspace(int vspace) + { + setIntHTMLAttribute("vspace", vspace); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java new file mode 100644 index 00000000000..d7eed07dd1f --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java @@ -0,0 +1,139 @@ +/* DomHTMLAreaElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLAreaElement; + +/** + * An HTML 'AREA' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLAreaElement + extends DomHTMLElement + implements HTMLAreaElement +{ + + protected DomHTMLAreaElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public String getAlt() + { + return getHTMLAttribute("alt"); + } + + public void setAlt(String alt) + { + setHTMLAttribute("alt", alt); + } + + public String getCoords() + { + return getHTMLAttribute("coords"); + } + + public void setCoords(String coords) + { + setHTMLAttribute("coords", coords); + } + + public String getHref() + { + return getHTMLAttribute("href"); + } + + public void setHref(String href) + { + setHTMLAttribute("href", href); + } + + public boolean getNoHref() + { + return getBooleanHTMLAttribute("nohref"); + } + + public void setNoHref(boolean nohref) + { + setBooleanHTMLAttribute("nohref", nohref); + } + + public String getShape() + { + return getHTMLAttribute("shape"); + } + + public void setShape(String shape) + { + setHTMLAttribute("shape", shape); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public String getTarget() + { + return getHTMLAttribute("target"); + } + + public void setTarget(String target) + { + setHTMLAttribute("target", target); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java new file mode 100644 index 00000000000..673699d527a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java @@ -0,0 +1,69 @@ +/* DomHTMLBRElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLBRElement; + +/** + * An HTML 'BR' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLBRElement + extends DomHTMLElement + implements HTMLBRElement +{ + + protected DomHTMLBRElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getClear() + { + return getHTMLAttribute("clear"); + } + + public void setClear(String clear) + { + setHTMLAttribute("clear", clear); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java new file mode 100644 index 00000000000..ddc8053079f --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java @@ -0,0 +1,79 @@ +/* DomHTMLBaseElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLBaseElement; + +/** + * An HTML 'BASE' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLBaseElement + extends DomHTMLElement + implements HTMLBaseElement +{ + + protected DomHTMLBaseElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getHref() + { + return getHTMLAttribute("href"); + } + + public void setHref(String href) + { + setHTMLAttribute("href", href); + } + + public String getTarget() + { + return getHTMLAttribute("target"); + } + + public void setTarget(String target) + { + setHTMLAttribute("target", target); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java new file mode 100644 index 00000000000..73172d3bf11 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java @@ -0,0 +1,89 @@ +/* DomHTMLBaseFontElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLBaseFontElement; + +/** + * An HTML 'BASEFONT' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLBaseFontElement + extends DomHTMLElement + implements HTMLBaseFontElement +{ + + protected DomHTMLBaseFontElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getColor() + { + return getHTMLAttribute("color"); + } + + public void setColor(String color) + { + setHTMLAttribute("color", color); + } + + public String getFace() + { + return getHTMLAttribute("face"); + } + + public void setFace(String face) + { + setHTMLAttribute("face", face); + } + + public int getSize() + { + return getIntHTMLAttribute("size"); + } + + public void setSize(int size) + { + setIntHTMLAttribute("size", size); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java new file mode 100644 index 00000000000..44fbcf23cc5 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java @@ -0,0 +1,119 @@ +/* DomHTMLBodyElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLBodyElement; + +/** + * An HTML 'BODY' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLBodyElement + extends DomHTMLElement + implements HTMLBodyElement +{ + + protected DomHTMLBodyElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getALink() + { + return getHTMLAttribute("alink"); + } + + public void setALink(String alink) + { + setHTMLAttribute("alink", alink); + } + + public String getBackground() + { + return getHTMLAttribute("background"); + } + + public void setBackground(String background) + { + setHTMLAttribute("background", background); + } + + public String getBgColor() + { + return getHTMLAttribute("bgcolor"); + } + + public void setBgColor(String bgcolor) + { + setHTMLAttribute("bgcolor", bgcolor); + } + + public String getLink() + { + return getHTMLAttribute("link"); + } + + public void setLink(String link) + { + setHTMLAttribute("link", link); + } + + public String getText() + { + return getHTMLAttribute("text"); + } + + public void setText(String text) + { + setHTMLAttribute("text", text); + } + + public String getVLink() + { + return getHTMLAttribute("vlink"); + } + + public void setVLink(String vlink) + { + setHTMLAttribute("vlink", vlink); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java new file mode 100644 index 00000000000..5aff5f8c1a9 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java @@ -0,0 +1,121 @@ +/* DomHTMLButtonElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.Node; +import org.w3c.dom.html2.HTMLButtonElement; +import org.w3c.dom.html2.HTMLFormElement; + +/** + * An HTML 'BUTTON' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLButtonElement + extends DomHTMLElement + implements HTMLButtonElement +{ + + protected DomHTMLButtonElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public String getValue() + { + return getHTMLAttribute("value"); + } + + public void setValue(String value) + { + setHTMLAttribute("value", value); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java new file mode 100644 index 00000000000..577337a3c3a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java @@ -0,0 +1,227 @@ +/* DomHTMLCollection.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDOMException; +import gnu.xml.dom.DomElement; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLOptionsCollection; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; + +/** + * An HTML element collection. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DomHTMLCollection + implements HTMLCollection, HTMLOptionsCollection, NodeList, NodeFilter +{ + + final DomHTMLDocument doc; + final Node root; + List nodeNames; + List attributeNames; + List results; + + DomHTMLCollection(DomHTMLDocument doc, Node root) + { + this.doc = doc; + this.root = root; + } + + // -- Node name and attribute filtering -- + + void addNodeName(String name) + { + if (nodeNames == null) + { + nodeNames = new LinkedList(); + } + nodeNames.add(name); + } + + void addAttributeName(String name) + { + if (attributeNames == null) + { + attributeNames = new LinkedList(); + } + attributeNames.add(name); + } + + public short acceptNode(Node n) + { + if (n.getNodeType() != Node.ELEMENT_NODE) + { + return NodeFilter.FILTER_SKIP; + } + String localName = n.getLocalName(); + if (localName == null) + { + localName = n.getNodeName(); + } + if (nodeNames != null && !acceptName(localName)) + { + return NodeFilter.FILTER_SKIP; + } + if (attributeNames != null && !acceptAttributes(n.getAttributes())) + { + return NodeFilter.FILTER_SKIP; + } + return NodeFilter.FILTER_ACCEPT; + } + + private boolean acceptName(String name) + { + for (Iterator i = nodeNames.iterator(); i.hasNext(); ) + { + String nodeName = (String) i.next(); + if (nodeName.equalsIgnoreCase(name)) + { + return true; + } + } + return false; + } + + private boolean acceptAttributes(NamedNodeMap attrs) + { + for (Iterator i = attributeNames.iterator(); i.hasNext(); ) + { + String attributeName = (String) i.next(); + Node attr = getNamedItem(attrs, attributeName); + if (attr != null) + { + // Check that attribute has a non-null value + String nodeValue = attr.getNodeValue(); + if (nodeValue != null && nodeValue.length() > 0) + { + return true; + } + } + } + return false; + } + + /** + * Case-insensitive version of getNamedItem. + */ + private Node getNamedItem(NamedNodeMap attrs, String name) + { + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String attrName = attr.getLocalName(); + if (attrName == null) + { + attrName = attr.getNodeName(); + } + if (name.equalsIgnoreCase(attrName)) + { + return attr; + } + } + return null; + } + + // -- Perform query -- + + void evaluate() + { + NodeIterator i = doc.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, + this, true); + results = new ArrayList(); + for (Node node = i.nextNode(); node != null; node = i.nextNode()) + { + results.add(node); + } + } + + // -- HTMLCollection/NodeList interface -- + + public int getLength() + { + return results.size(); + } + + public void setLength(int length) + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + + public Node item(int index) + { + return (Node) results.get(index); + } + + public Node namedItem(String name) + { + boolean xhtml = false; // FIXME detect XHTML document + for (Iterator i = results.iterator(); i.hasNext(); ) + { + Node node = (Node) i.next(); + NamedNodeMap attrs = node.getAttributes(); + Node attr = getNamedItem(attrs, "id"); + if (name.equals(attr.getTextContent())) + { + return node; + } + if (!xhtml) + { + attr = getNamedItem(attrs, "name"); + if (name.equals(attr.getTextContent())) + { + return node; + } + } + } + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java new file mode 100644 index 00000000000..2b9dbf23623 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java @@ -0,0 +1,69 @@ +/* DomHTMLDListElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLDListElement; + +/** + * An HTML 'DL' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLDListElement + extends DomHTMLElement + implements HTMLDListElement +{ + + protected DomHTMLDListElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getCompact() + { + return getBooleanHTMLAttribute("compact"); + } + + public void setCompact(boolean compact) + { + setBooleanHTMLAttribute("compact", compact); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java new file mode 100644 index 00000000000..26af97e781b --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java @@ -0,0 +1,69 @@ +/* DomHTMLDirectoryElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLDirectoryElement; + +/** + * An HTML 'DIR' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLDirectoryElement + extends DomHTMLElement + implements HTMLDirectoryElement +{ + + protected DomHTMLDirectoryElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getCompact() + { + return getBooleanHTMLAttribute("compact"); + } + + public void setCompact(boolean compact) + { + setBooleanHTMLAttribute("compact", compact); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java new file mode 100644 index 00000000000..462069dcf1a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java @@ -0,0 +1,69 @@ +/* DomHTMLDivElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLDivElement; + +/** + * An HTML 'DIV' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLDivElement + extends DomHTMLElement + implements HTMLDivElement +{ + + protected DomHTMLDivElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java new file mode 100644 index 00000000000..10ee9e74767 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java @@ -0,0 +1,425 @@ +/* DomHTMLDocument.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDocument; +import gnu.xml.dom.DomDOMException; +import java.lang.reflect.Constructor; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLDocument; +import org.w3c.dom.html2.HTMLElement; + +/** + * An HTML document. + * This is the factory object used to create HTML elements. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLDocument + extends DomDocument + implements HTMLDocument +{ + + private static final Class[] ELEMENT_PT = new Class[] { + DomHTMLDocument.class, + String.class, + String.class + }; + + private static Map ELEMENT_CLASSES; + static + { + Map map = new HashMap(); + map.put("a", DomHTMLAnchorElement.class); + map.put("applet", DomHTMLAppletElement.class); + map.put("area", DomHTMLAreaElement.class); + map.put("base", DomHTMLBaseElement.class); + map.put("basefont", DomHTMLBaseFontElement.class); + map.put("body", DomHTMLBodyElement.class); + map.put("br", DomHTMLBRElement.class); + map.put("button", DomHTMLButtonElement.class); + map.put("dir", DomHTMLDirectoryElement.class); + map.put("div", DomHTMLDivElement.class); + map.put("dlist", DomHTMLDListElement.class); + map.put("fieldset", DomHTMLFieldSetElement.class); + map.put("font", DomHTMLFontElement.class); + map.put("form", DomHTMLFormElement.class); + map.put("frame", DomHTMLFrameElement.class); + map.put("frameset", DomHTMLFrameSetElement.class); + map.put("head", DomHTMLHeadElement.class); + map.put("h1", DomHTMLHeadingElement.class); + map.put("h2", DomHTMLHeadingElement.class); + map.put("h3", DomHTMLHeadingElement.class); + map.put("h4", DomHTMLHeadingElement.class); + map.put("h5", DomHTMLHeadingElement.class); + map.put("h6", DomHTMLHeadingElement.class); + map.put("html", DomHTMLHtmlElement.class); + map.put("iframe", DomHTMLIFrameElement.class); + map.put("img", DomHTMLImageElement.class); + map.put("input", DomHTMLInputElement.class); + map.put("isindex", DomHTMLIsIndexElement.class); + map.put("label", DomHTMLLabelElement.class); + map.put("legend", DomHTMLLegendElement.class); + map.put("li", DomHTMLLIElement.class); + map.put("link", DomHTMLLinkElement.class); + map.put("map", DomHTMLMapElement.class); + map.put("menu", DomHTMLMenuElement.class); + map.put("meta", DomHTMLMetaElement.class); + map.put("ins", DomHTMLModElement.class); + map.put("del", DomHTMLModElement.class); + map.put("object", DomHTMLObjectElement.class); + map.put("ol", DomHTMLOListElement.class); + map.put("optgroup", DomHTMLOptGroupElement.class); + map.put("option", DomHTMLOptionElement.class); + map.put("p", DomHTMLParagraphElement.class); + map.put("param", DomHTMLParamElement.class); + map.put("pre", DomHTMLPreElement.class); + map.put("q", DomHTMLQuoteElement.class); + map.put("blockquote", DomHTMLQuoteElement.class); + map.put("script", DomHTMLScriptElement.class); + map.put("select", DomHTMLSelectElement.class); + map.put("style", DomHTMLStyleElement.class); + map.put("caption", DomHTMLTableCaptionElement.class); + map.put("th", DomHTMLTableCellElement.class); + map.put("td", DomHTMLTableCellElement.class); + map.put("col", DomHTMLTableColElement.class); + map.put("colgroup", DomHTMLTableColElement.class); + map.put("table", DomHTMLTableElement.class); + map.put("tr", DomHTMLTableRowElement.class); + map.put("thead", DomHTMLTableSectionElement.class); + map.put("tfoot", DomHTMLTableSectionElement.class); + map.put("tbody", DomHTMLTableSectionElement.class); + map.put("textarea", DomHTMLTextAreaElement.class); + map.put("title", DomHTMLTitleElement.class); + map.put("ul", DomHTMLUListElement.class); + ELEMENT_CLASSES = Collections.unmodifiableMap(map); + } + + private static Set HTML_NS_URIS; + static + { + Set set = new HashSet(); + set.add("http://www.w3.org/TR/html4/strict"); + set.add("http://www.w3.org/TR/html4/loose"); + set.add("http://www.w3.org/TR/html4/frameset"); + set.add("http://www.w3.org/1999/xhtml"); + set.add("http://www.w3.org/TR/xhtml1/strict"); + set.add("http://www.w3.org/TR/xhtml1/loose"); + set.add("http://www.w3.org/TR/xhtml1/frameset"); + HTML_NS_URIS = Collections.unmodifiableSet(set); + } + + /** + * Convenience constructor. + */ + public DomHTMLDocument() + { + this(new DomHTMLImpl()); + } + + /** + * Constructor. + * This is called by the DOMImplementation. + */ + public DomHTMLDocument(DomHTMLImpl impl) + { + super(impl); + } + + private Node getChildNodeByName(Node parent, String name) + { + for (Node ctx = parent.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (name.equalsIgnoreCase(ctx.getNodeName())) + { + return ctx; + } + } + return null; + } + + public String getTitle() + { + Node html = getDocumentElement(); + if (html != null) + { + Node head = getChildNodeByName(html, "head"); + if (head != null) + { + Node title = getChildNodeByName(head, "title"); + if (title != null) + { + return title.getTextContent(); + } + } + } + return null; + } + + public void setTitle(String title) + { + Node html = getDocumentElement(); + if (html == null) + { + html = createElement("html"); + appendChild(html); + } + Node head = getChildNodeByName(html, "head"); + if (head == null) + { + head = createElement("head"); + Node first = html.getFirstChild(); + if (first != null) + { + html.insertBefore(first, head); + } + else + { + html.appendChild(head); + } + } + Node titleNode = getChildNodeByName(head, "title"); + if (titleNode == null) + { + titleNode = createElement("title"); + Node first = head.getFirstChild(); + if (first != null) + { + head.insertBefore(first, titleNode); + } + else + { + head.appendChild(titleNode); + } + } + titleNode.setTextContent(title); + } + + public String getReferrer() + { + // TODO getReferrer + return null; + } + + public String getDomain() + { + try + { + URL url = new URL(getDocumentURI()); + return url.getHost(); + } + catch (MalformedURLException e) + { + return null; + } + } + + public String getURL() + { + return getDocumentURI(); + } + + public HTMLElement getBody() + { + Node html = getDocumentElement(); + if (html != null) + { + Node body = getChildNodeByName(html, "body"); + if (body == null) + { + body = getChildNodeByName(html, "frameset"); + } + return (HTMLElement) body; + } + return null; + } + + public void setBody(HTMLElement body) + { + Node html = getDocumentElement(); + if (html == null) + { + html = createElement("html"); + appendChild(html); + } + Node ref = getBody(); + if (ref == null) + { + html.appendChild(body); + } + else + { + html.replaceChild(body, ref); + } + } + + public HTMLCollection getImages() + { + DomHTMLCollection ret = new DomHTMLCollection(this, this); + ret.addNodeName("img"); + ret.evaluate(); + return ret; + } + + public HTMLCollection getApplets() + { + DomHTMLCollection ret = new DomHTMLCollection(this, this); + ret.addNodeName("object"); + ret.addNodeName("applet"); + ret.evaluate(); + return ret; + } + + public HTMLCollection getLinks() + { + DomHTMLCollection ret = new DomHTMLCollection(this, this); + ret.addNodeName("area"); + ret.addNodeName("a"); + ret.evaluate(); + return ret; + } + + public HTMLCollection getForms() + { + DomHTMLCollection ret = new DomHTMLCollection(this, this); + ret.addNodeName("form"); + ret.evaluate(); + return ret; + } + + public HTMLCollection getAnchors() + { + DomHTMLCollection ret = new DomHTMLCollection(this, this); + ret.addNodeName("a"); + ret.addAttributeName("name"); + ret.evaluate(); + return ret; + } + + public String getCookie() + { + // TODO getCookie + return null; + } + + public void setCookie(String cookie) + { + // TODO setCookie + } + + public void open() + { + // TODO open + } + + public void close() + { + // TODO close + } + + public void write(String text) + { + // TODO write + } + + public void writeln(String text) + { + // TODO write + } + + public NodeList getElementsByName(String name) + { + DomHTMLCollection ret = new DomHTMLCollection(this, this); + ret.addNodeName(name); + ret.evaluate(); + return ret; + // TODO xhtml: return only form controls (?) + } + + public Element createElement(String tagName) + { + return createElementNS(null, tagName); + } + + public Element createElementNS(String uri, String qName) + { + /* If a non-HTML element, use the default implementation. */ + if (uri != null && !HTML_NS_URIS.contains(uri)) + { + return super.createElementNS(uri, qName); + } + String localName = qName.toLowerCase(); + int ci = qName.indexOf(':'); + if (ci != -1) + { + localName = qName.substring(ci + 1); + } + Class t = (Class) ELEMENT_CLASSES.get(localName); + /* If a non-HTML element, use the default implementation. */ + if (t == null) + { + return super.createElementNS(uri, qName); + } + try + { + Constructor c = t.getDeclaredConstructor(ELEMENT_PT); + Object[] args = new Object[] { this, uri, qName }; + return (Element) c.newInstance(args); + } + catch (Exception e) + { + DOMException e2 = new DomDOMException(DOMException.TYPE_MISMATCH_ERR); + e2.initCause(e); + throw e2; + } + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java new file mode 100644 index 00000000000..b957083d680 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java @@ -0,0 +1,287 @@ +/* DomHTMLElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDOMException; +import gnu.xml.dom.DomElement; +import gnu.xml.dom.DomEvent; +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.events.UIEvent; +import org.w3c.dom.html2.HTMLElement; + +/** + * Abstract implementation of an HTML element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class DomHTMLElement + extends DomElement + implements HTMLElement +{ + + protected DomHTMLElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + /** + * Returns the value of the specified attribute. + * The attribute name is case insensitive. + */ + protected String getHTMLAttribute(String name) + { + if (hasAttributes()) + { + NamedNodeMap attrs = getAttributes(); + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String attrName = attr.getLocalName(); + if (attrName == null) + { + attrName = attr.getNodeName(); + } + if (attrName.equalsIgnoreCase(name)) + { + return attr.getNodeValue(); + } + } + } + return ""; + } + + protected int getIntHTMLAttribute(String name) + { + String value = getHTMLAttribute(name); + if (value == null) + { + return -1; + } + try + { + return Integer.parseInt(value); + } + catch (NumberFormatException e) + { + return -1; + } + } + + protected boolean getBooleanHTMLAttribute(String name) + { + String value = getHTMLAttribute(name); + return value != null; + } + + /** + * Sets the value of the specified attribute. + * The attribute name is case insensitive. + */ + protected void setHTMLAttribute(String name, String value) + { + Node attr; + NamedNodeMap attrs = getAttributes(); + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + attr = attrs.item(i); + String attrName = attr.getLocalName(); + if (attrName == null) + { + attrName = attr.getNodeName(); + } + if (attrName.equalsIgnoreCase(name)) + { + if (value != null) + { + attr.setNodeValue(value); + } + else + { + attrs.removeNamedItem(attr.getNodeName()); + } + return; + } + } + if (value != null) + { + // Create a new attribute + DomHTMLDocument doc = (DomHTMLDocument) getOwnerDocument(); + // XXX namespace URI for attribute? + attr = doc.createAttribute(name); + attr.setNodeValue(value); + } + } + + protected void setIntHTMLAttribute(String name, int value) + { + setHTMLAttribute(name, Integer.toString(value)); + } + + protected void setBooleanHTMLAttribute(String name, boolean value) + { + setHTMLAttribute(name, value ? name : null); + } + + /** + * Returns the first parent element with the specified name. + */ + protected Node getParentElement(String name) + { + for (Node parent = getParentNode(); parent != null; + parent = parent.getParentNode()) + { + String parentName = parent.getLocalName(); + if (parentName == null) + { + parentName = parent.getNodeName(); + } + if (name.equalsIgnoreCase(parentName)) + { + return parent; + } + } + return null; + } + + /** + * Returns the first child element with the specified name. + */ + protected Node getChildElement(String name) + { + for (Node child = getFirstChild(); child != null; + child = child.getNextSibling()) + { + String childName = child.getLocalName(); + if (childName == null) + { + childName = child.getLocalName(); + } + if (name.equalsIgnoreCase(childName)) + { + return child; + } + } + return null; + } + + /** + * Returns the index of this element among elements of the same name, + * relative to its parent. + */ + protected int getIndex() + { + int index = 0; + Node parent = getParentNode(); + if (parent != null) + { + for (Node ctx = parent.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx == this) + { + return index; + } + index++; + } + } + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + + protected void dispatchUIEvent(String name) + { + UIEvent event = new DomEvent.DomUIEvent(name); + dispatchEvent(event); + } + + public String getId() + { + return getHTMLAttribute("id"); + } + + public void setId(String id) + { + setHTMLAttribute("id", id); + } + + public String getTitle() + { + return getHTMLAttribute("title"); + } + + public void setTitle(String title) + { + setHTMLAttribute("title", title); + } + + public String getLang() + { + return getHTMLAttribute("lang"); + } + + public void setLang(String lang) + { + setHTMLAttribute("lang", lang); + } + + public String getDir() + { + return getHTMLAttribute("dir"); + } + + public void setDir(String dir) + { + setHTMLAttribute("dir", dir); + } + + public String getClassName() + { + return getHTMLAttribute("class"); + } + + public void setClassName(String className) + { + setHTMLAttribute("class", className); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java new file mode 100644 index 00000000000..2c857721947 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java @@ -0,0 +1,65 @@ +/* DomHTMLFieldSetElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFieldSetElement; +import org.w3c.dom.html2.HTMLFormElement; + +/** + * An HTML 'FIELDSET' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLFieldSetElement + extends DomHTMLElement + implements HTMLFieldSetElement +{ + + protected DomHTMLFieldSetElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java new file mode 100644 index 00000000000..08bd349fa62 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java @@ -0,0 +1,89 @@ +/* DomHTMLFontElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFontElement; + +/** + * An HTML 'FONT' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLFontElement + extends DomHTMLElement + implements HTMLFontElement +{ + + protected DomHTMLFontElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getColor() + { + return getHTMLAttribute("color"); + } + + public void setColor(String color) + { + setHTMLAttribute("color", color); + } + + public String getFace() + { + return getHTMLAttribute("face"); + } + + public void setFace(String face) + { + setHTMLAttribute("face", face); + } + + public String getSize() + { + return getHTMLAttribute("size"); + } + + public void setSize(String size) + { + setHTMLAttribute("size", size); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java new file mode 100644 index 00000000000..119d97eb885 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java @@ -0,0 +1,150 @@ +/* DomHTMLFormElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLFormElement; + +/** + * An HTML 'FORM' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLFormElement + extends DomHTMLElement + implements HTMLFormElement +{ + + protected DomHTMLFormElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLCollection getElements() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("input"); + ret.addNodeName("button"); + ret.addNodeName("select"); + ret.addNodeName("textarea"); + ret.addNodeName("isindex"); + ret.addNodeName("label"); + ret.addNodeName("option"); + ret.evaluate(); + return ret; + } + + public int getLength() + { + return getElements().getLength(); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getAcceptCharset() + { + return getHTMLAttribute("accept-charset"); + } + + public void setAcceptCharset(String acceptCharset) + { + setHTMLAttribute("accept-charset", acceptCharset); + } + + public String getAction() + { + return getHTMLAttribute("action"); + } + + public void setAction(String action) + { + setHTMLAttribute("action", action); + } + + public String getEnctype() + { + return getHTMLAttribute("enctype"); + } + + public void setEnctype(String enctype) + { + setHTMLAttribute("enctype", enctype); + } + + public String getMethod() + { + return getHTMLAttribute("method"); + } + + public void setMethod(String method) + { + setHTMLAttribute("method", method); + } + + public String getTarget() + { + return getHTMLAttribute("target"); + } + + public void setTarget(String target) + { + setHTMLAttribute("target", target); + } + + public void submit() + { + dispatchUIEvent("submit"); + } + + public void reset() + { + dispatchUIEvent("reset"); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java new file mode 100644 index 00000000000..124f35268e8 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java @@ -0,0 +1,146 @@ +/* DomHTMLFrameElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.Document; +import org.w3c.dom.html2.HTMLFrameElement; + +/** + * An HTML 'FRAME' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLFrameElement + extends DomHTMLElement + implements HTMLFrameElement +{ + + protected DomHTMLFrameElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getFrameBorder() + { + return getHTMLAttribute("frameborder"); + } + + public void setFrameBorder(String frameBorder) + { + setHTMLAttribute("frameborder", frameBorder); + } + + public String getLongDesc() + { + return getHTMLAttribute("longdesc"); + } + + public void setLongDesc(String longDesc) + { + setHTMLAttribute("longdesc", longDesc); + } + + public String getMarginHeight() + { + return getHTMLAttribute("marginheight"); + } + + public void setMarginHeight(String marginHeight) + { + setHTMLAttribute("marginheight", marginHeight); + } + + public String getMarginWidth() + { + return getHTMLAttribute("marginwidth"); + } + + public void setMarginWidth(String marginWidth) + { + setHTMLAttribute("marginwidth", marginWidth); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public boolean getNoResize() + { + return getBooleanHTMLAttribute("noresize"); + } + + public void setNoResize(boolean noResize) + { + setBooleanHTMLAttribute("noresize", noResize); + } + + public String getScrolling() + { + return getHTMLAttribute("scrolling"); + } + + public void setScrolling(String scrolling) + { + setHTMLAttribute("scrolling", scrolling); + } + + public String getSrc() + { + return getHTMLAttribute("src"); + } + + public void setSrc(String src) + { + setHTMLAttribute("src", src); + } + + public Document getContentDocument() + { + // TODO getContentDocument + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java new file mode 100644 index 00000000000..44d317f64a3 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java @@ -0,0 +1,79 @@ +/* DomHTMLFrameSetElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFrameSetElement; + +/** + * An HTML 'FRAMESET' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLFrameSetElement + extends DomHTMLElement + implements HTMLFrameSetElement +{ + + protected DomHTMLFrameSetElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getCols() + { + return getHTMLAttribute("cols"); + } + + public void setCols(String cols) + { + setHTMLAttribute("cols", cols); + } + + public String getRows() + { + return getHTMLAttribute("rows"); + } + + public void setRows(String rows) + { + setHTMLAttribute("rows", rows); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java new file mode 100644 index 00000000000..d1b48012175 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java @@ -0,0 +1,99 @@ +/* DomHTMLHRElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLHRElement; + +/** + * An HTML 'HR' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLHRElement + extends DomHTMLElement + implements HTMLHRElement +{ + + protected DomHTMLHRElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public boolean getNoShade() + { + return getBooleanHTMLAttribute("noshade"); + } + + public void setNoShade(boolean noShade) + { + setBooleanHTMLAttribute("noshade", noShade); + } + + public String getSize() + { + return getHTMLAttribute("size"); + } + + public void setSize(String size) + { + setHTMLAttribute("size", size); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java new file mode 100644 index 00000000000..f68e8c7fad3 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java @@ -0,0 +1,69 @@ +/* DomHTMLHeadElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLHeadElement; + +/** + * An HTML 'HEAD' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLHeadElement + extends DomHTMLElement + implements HTMLHeadElement +{ + + protected DomHTMLHeadElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getProfile() + { + return getHTMLAttribute("profile"); + } + + public void setProfile(String profile) + { + setHTMLAttribute("profile", profile); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java new file mode 100644 index 00000000000..f1427ad89e6 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java @@ -0,0 +1,69 @@ +/* DomHTMLHeadingElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLHeadingElement; + +/** + * An HTML 'H1', 'H2', 'H3', 'H4', 'H5', or 'H6' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLHeadingElement + extends DomHTMLElement + implements HTMLHeadingElement +{ + + protected DomHTMLHeadingElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java new file mode 100644 index 00000000000..95d45264eee --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java @@ -0,0 +1,69 @@ +/* DomHTMLHtmlElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLHtmlElement; + +/** + * An HTML 'HTML' top-level element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLHtmlElement + extends DomHTMLElement + implements HTMLHtmlElement +{ + + protected DomHTMLHtmlElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getVersion() + { + return getHTMLAttribute("version"); + } + + public void setVersion(String version) + { + setHTMLAttribute("version", version); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java new file mode 100644 index 00000000000..c5ca75d8c21 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java @@ -0,0 +1,166 @@ +/* DomHTMLIFrameElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.Document; +import org.w3c.dom.html2.HTMLIFrameElement; + +/** + * An HTML 'IFRAME' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLIFrameElement + extends DomHTMLElement + implements HTMLIFrameElement +{ + + protected DomHTMLIFrameElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getFrameBorder() + { + return getHTMLAttribute("frameborder"); + } + + public void setFrameBorder(String frameBorder) + { + setHTMLAttribute("frameborder", frameBorder); + } + + public String getHeight() + { + return getHTMLAttribute("height"); + } + + public void setHeight(String height) + { + setHTMLAttribute("height", height); + } + + public String getLongDesc() + { + return getHTMLAttribute("longdesc"); + } + + public void setLongDesc(String longDesc) + { + setHTMLAttribute("longdesc", longDesc); + } + + public String getMarginHeight() + { + return getHTMLAttribute("marginheight"); + } + + public void setMarginHeight(String marginHeight) + { + setHTMLAttribute("marginheight", marginHeight); + } + + public String getMarginWidth() + { + return getHTMLAttribute("marginwidth"); + } + + public void setMarginWidth(String marginWidth) + { + setHTMLAttribute("marginwidth", marginWidth); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getScrolling() + { + return getHTMLAttribute("scrolling"); + } + + public void setScrolling(String scrolling) + { + setHTMLAttribute("scrolling", scrolling); + } + + public String getSrc() + { + return getHTMLAttribute("src"); + } + + public void setSrc(String src) + { + setHTMLAttribute("src", src); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + + public Document getContentDocument() + { + // TODO getContentDocument + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java new file mode 100644 index 00000000000..c96e5fede4d --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java @@ -0,0 +1,179 @@ +/* DomHTMLImageElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLImageElement; + +/** + * An HTML 'IMG' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLImageElement + extends DomHTMLElement + implements HTMLImageElement +{ + + protected DomHTMLImageElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getAlt() + { + return getHTMLAttribute("alt"); + } + + public void setAlt(String alt) + { + setHTMLAttribute("alt", alt); + } + + public String getBorder() + { + return getHTMLAttribute("border"); + } + + public void setBorder(String border) + { + setHTMLAttribute("border", border); + } + + public int getHeight() + { + return getIntHTMLAttribute("height"); + } + + public void setHeight(int height) + { + setIntHTMLAttribute("height", height); + } + + public int getHspace() + { + return getIntHTMLAttribute("hspace"); + } + + public void setHspace(int hspace) + { + setIntHTMLAttribute("hspace", hspace); + } + + public boolean getIsMap() + { + return getBooleanHTMLAttribute("ismap"); + } + + public void setIsMap(boolean isMap) + { + setBooleanHTMLAttribute("ismap", isMap); + } + + public String getLongDesc() + { + return getHTMLAttribute("longdesc"); + } + + public void setLongDesc(String longDesc) + { + setHTMLAttribute("longdesc", longDesc); + } + + public String getSrc() + { + return getHTMLAttribute("src"); + } + + public void setSrc(String src) + { + setHTMLAttribute("src", src); + } + + public String getUseMap() + { + return getHTMLAttribute("usemap"); + } + + public void setUseMap(String useMap) + { + setHTMLAttribute("usemap", useMap); + } + + public int getVspace() + { + return getIntHTMLAttribute("vspace"); + } + + public void setVspace(int vspace) + { + setIntHTMLAttribute("vspace", vspace); + } + + public int getWidth() + { + return getIntHTMLAttribute("width"); + } + + public void setWidth(int width) + { + setIntHTMLAttribute("width", width); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java new file mode 100644 index 00000000000..9f0db085040 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java @@ -0,0 +1,67 @@ +/* DomHTMLImpl.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomImpl; +import org.w3c.dom.Document; + +/** + * Specialised DOMImplementation for creating HTML documents. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLImpl + extends DomImpl +{ + + protected Document createDocument() + { + return new DomHTMLDocument(this); + } + + public Object getFeature(String feature, String version) + { + if (hasFeature(feature, version)) + { + return this; + } + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java new file mode 100644 index 00000000000..1dbc9c28de0 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java @@ -0,0 +1,266 @@ +/* DomHTMLInputElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLInputElement; + +/** + * An HTML 'INPUT' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLInputElement + extends DomHTMLElement + implements HTMLInputElement +{ + + protected String value; + protected Boolean checked; + + protected DomHTMLInputElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getDefaultValue() + { + return getHTMLAttribute("value"); + } + + public void setDefaultValue(String defaultValue) + { + setHTMLAttribute("value", defaultValue); + } + + public boolean getDefaultChecked() + { + return getBooleanHTMLAttribute("checked"); + } + + public void setDefaultChecked(boolean defaultChecked) + { + setBooleanHTMLAttribute("checked", defaultChecked); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getAccept() + { + return getHTMLAttribute("accept"); + } + + public void setAccept(String accept) + { + setHTMLAttribute("accept", accept); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getAlt() + { + return getHTMLAttribute("alt"); + } + + public void setAlt(String alt) + { + setHTMLAttribute("alt", alt); + } + + public boolean getChecked() + { + if (checked == null) + { + checked = Boolean.valueOf(getDefaultChecked()); + } + return checked.booleanValue(); + } + + public void setChecked(boolean checked) + { + this.checked = Boolean.valueOf(checked); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public int getMaxLength() + { + return getIntHTMLAttribute("maxLength"); + } + + public void setMaxLength(int maxLength) + { + setIntHTMLAttribute("maxLength", maxLength); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public boolean getReadOnly() + { + return getBooleanHTMLAttribute("readonly"); + } + + public void setReadOnly(boolean readOnly) + { + setBooleanHTMLAttribute("readonly", readOnly); + } + + public int getSize() + { + return getIntHTMLAttribute("size"); + } + + public void setSize(int size) + { + setIntHTMLAttribute("size", size); + } + + public String getSrc() + { + return getHTMLAttribute("src"); + } + + public void setSrc(String src) + { + setHTMLAttribute("src", src); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + + public String getUseMap() + { + return getHTMLAttribute("usemap"); + } + + public void setUseMap(String useMap) + { + setHTMLAttribute("usemap", useMap); + } + + public String getValue() + { + if (value == null) + { + value = getDefaultValue(); + } + return value; + } + + public void setValue(String value) + { + this.value = value; + } + + public void blur() + { + dispatchUIEvent("blur"); + } + + public void focus() + { + dispatchUIEvent("focus"); + } + + public void select() + { + dispatchUIEvent("select"); + } + + public void click() + { + dispatchUIEvent("click"); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java new file mode 100644 index 00000000000..087f4a0cd79 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java @@ -0,0 +1,75 @@ +/* DomHTMLIsIndexElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLIsIndexElement; + +/** + * An HTML 'ISINDEX' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLIsIndexElement + extends DomHTMLElement + implements HTMLIsIndexElement +{ + + protected DomHTMLIsIndexElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getPrompt() + { + return getHTMLAttribute("prompt"); + } + + public void setPrompt(String prompt) + { + setHTMLAttribute("prompt", prompt); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java new file mode 100644 index 00000000000..11eccdf5df3 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java @@ -0,0 +1,79 @@ +/* DomHTMLLIElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLLIElement; + +/** + * An HTML 'LI' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLLIElement + extends DomHTMLElement + implements HTMLLIElement +{ + + protected DomHTMLLIElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + + public int getValue() + { + return getIntHTMLAttribute("value"); + } + + public void setValue(int value) + { + setIntHTMLAttribute("value", value); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java new file mode 100644 index 00000000000..c0cc39f8e96 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java @@ -0,0 +1,85 @@ +/* DomHTMLLabelElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLLabelElement; + +/** + * An HTML 'LABEL' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLLabelElement + extends DomHTMLElement + implements HTMLLabelElement +{ + + protected DomHTMLLabelElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public String getHtmlFor() + { + return getHTMLAttribute("for"); + } + + public void setHtmlFor(String htmlFor) + { + setHTMLAttribute("for", htmlFor); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java new file mode 100644 index 00000000000..a500f8fa14c --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java @@ -0,0 +1,85 @@ +/* DomHTMLLegendElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLLegendElement; + +/** + * An HTML 'LEGEND' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLLegendElement + extends DomHTMLElement + implements HTMLLegendElement +{ + + protected DomHTMLLegendElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java new file mode 100644 index 00000000000..127726ef7e3 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java @@ -0,0 +1,149 @@ +/* DomHTMLLinkElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLLinkElement; + +/** + * An HTML 'LINK' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLLinkElement + extends DomHTMLElement + implements HTMLLinkElement +{ + + protected DomHTMLLinkElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public String getCharset() + { + return getHTMLAttribute("charset"); + } + + public void setCharset(String charset) + { + setHTMLAttribute("charset", charset); + } + + public String getHref() + { + return getHTMLAttribute("href"); + } + + public void setHref(String href) + { + setHTMLAttribute("href", href); + } + + public String getHreflang() + { + return getHTMLAttribute("hreflang"); + } + + public void setHreflang(String hreflang) + { + setHTMLAttribute("hreflang", hreflang); + } + + public String getMedia() + { + return getHTMLAttribute("media"); + } + + public void setMedia(String media) + { + setHTMLAttribute("media", media); + } + + public String getRel() + { + return getHTMLAttribute("rel"); + } + + public void setRel(String rel) + { + setHTMLAttribute("rel", rel); + } + + public String getRev() + { + return getHTMLAttribute("rev"); + } + + public void setRev(String rev) + { + setHTMLAttribute("rev", rev); + } + + public String getTarget() + { + return getHTMLAttribute("target"); + } + + public void setTarget(String target) + { + setHTMLAttribute("target", target); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java new file mode 100644 index 00000000000..1f7182f4f29 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java @@ -0,0 +1,79 @@ +/* DomHTMLMapElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLMapElement; + +/** + * An HTML 'MAP' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLMapElement + extends DomHTMLElement + implements HTMLMapElement +{ + + protected DomHTMLMapElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLCollection getAreas() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("area"); + ret.evaluate(); + return ret; + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java new file mode 100644 index 00000000000..17f6f589f9a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java @@ -0,0 +1,69 @@ +/* DomHTMLMenuElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLMenuElement; + +/** + * An HTML 'MENU' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLMenuElement + extends DomHTMLElement + implements HTMLMenuElement +{ + + protected DomHTMLMenuElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getCompact() + { + return getBooleanHTMLAttribute("compact"); + } + + public void setCompact(boolean compact) + { + setBooleanHTMLAttribute("compact", compact); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java new file mode 100644 index 00000000000..1a0440504de --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java @@ -0,0 +1,99 @@ +/* DomHTMLMetaElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLMetaElement; + +/** + * An HTML 'META' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLMetaElement + extends DomHTMLElement + implements HTMLMetaElement +{ + + protected DomHTMLMetaElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getContent() + { + return getHTMLAttribute("content"); + } + + public void setContent(String content) + { + setHTMLAttribute("content", content); + } + + public String getHttpEquiv() + { + return getHTMLAttribute("http-equiv"); + } + + public void setHttpEquiv(String httpEquiv) + { + setHTMLAttribute("http-equiv", httpEquiv); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getScheme() + { + return getHTMLAttribute("scheme"); + } + + public void setScheme(String scheme) + { + setHTMLAttribute("scheme", scheme); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java new file mode 100644 index 00000000000..493f7242ba1 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java @@ -0,0 +1,79 @@ +/* DomHTMLModElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLModElement; + +/** + * An HTML 'INS' or 'DEL' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLModElement + extends DomHTMLElement + implements HTMLModElement +{ + + protected DomHTMLModElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getCite() + { + return getHTMLAttribute("cite"); + } + + public void setCite(String cite) + { + setHTMLAttribute("cite", cite); + } + + public String getDateTime() + { + return getHTMLAttribute("datetime"); + } + + public void setDateTime(String dateTime) + { + setHTMLAttribute("datetime", dateTime); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java new file mode 100644 index 00000000000..fb98cf40d79 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java @@ -0,0 +1,89 @@ +/* DomHTMLOListElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLOListElement; + +/** + * An HTML 'OL' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLOListElement + extends DomHTMLElement + implements HTMLOListElement +{ + + protected DomHTMLOListElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getCompact() + { + return getBooleanHTMLAttribute("compact"); + } + + public void setCompact(boolean compact) + { + setBooleanHTMLAttribute("compact", compact); + } + + public int getStart() + { + return getIntHTMLAttribute("start"); + } + + public void setStart(int start) + { + setIntHTMLAttribute("start", start); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java new file mode 100644 index 00000000000..fdea9b15373 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java @@ -0,0 +1,242 @@ +/* DomHTMLObjectElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.Document; +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLObjectElement; + +/** + * An HTML 'OBJECT' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLObjectElement + extends DomHTMLElement + implements HTMLObjectElement +{ + + protected DomHTMLObjectElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getCode() + { + return getHTMLAttribute("code"); + } + + public void setCode(String code) + { + setHTMLAttribute("code", code); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getArchive() + { + return getHTMLAttribute("archive"); + } + + public void setArchive(String archive) + { + setHTMLAttribute("archive", archive); + } + + public String getBorder() + { + return getHTMLAttribute("border"); + } + + public void setBorder(String border) + { + setHTMLAttribute("border", border); + } + + public String getCodeBase() + { + return getHTMLAttribute("codebase"); + } + + public void setCodeBase(String codeBase) + { + setHTMLAttribute("codebase", codeBase); + } + + public String getCodeType() + { + return getHTMLAttribute("codetype"); + } + + public void setCodeType(String codeType) + { + setHTMLAttribute("codetype", codeType); + } + + public String getData() + { + return getHTMLAttribute("data"); + } + + public void setData(String data) + { + setHTMLAttribute("data", data); + } + + public boolean getDeclare() + { + return getBooleanHTMLAttribute("declare"); + } + + public void setDeclare(boolean declare) + { + setBooleanHTMLAttribute("declare", declare); + } + + public String getHeight() + { + return getHTMLAttribute("height"); + } + + public void setHeight(String height) + { + setHTMLAttribute("height", height); + } + + public int getHspace() + { + return getIntHTMLAttribute("hspace"); + } + + public void setHspace(int hspace) + { + setIntHTMLAttribute("hspace", hspace); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getStandby() + { + return getHTMLAttribute("standby"); + } + + public void setStandby(String standby) + { + setHTMLAttribute("standby", standby); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + + public String getUseMap() + { + return getHTMLAttribute("usemap"); + } + + public void setUseMap(String useMap) + { + setHTMLAttribute("usemap", useMap); + } + + public int getVspace() + { + return getIntHTMLAttribute("vspace"); + } + + public void setVspace(int vspace) + { + setIntHTMLAttribute("vspace", vspace); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + + public Document getContentDocument() + { + // TODO getContentDocument + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java new file mode 100644 index 00000000000..76545e130cb --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java @@ -0,0 +1,79 @@ +/* DomHTMLOptGroupElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLOptGroupElement; + +/** + * An HTML 'OPTGROUP' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLOptGroupElement + extends DomHTMLElement + implements HTMLOptGroupElement +{ + + protected DomHTMLOptGroupElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public String getLabel() + { + return getHTMLAttribute("label"); + } + + public void setLabel(String label) + { + setHTMLAttribute("label", label); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java new file mode 100644 index 00000000000..69c059d9d43 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java @@ -0,0 +1,131 @@ +/* DomHTMLOptionElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLOptionElement; + +/** + * An HTML 'OPTION' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLOptionElement + extends DomHTMLElement + implements HTMLOptionElement +{ + + protected Boolean selected; + + protected DomHTMLOptionElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public boolean getDefaultSelected() + { + return getBooleanHTMLAttribute("selected"); + } + + public void setDefaultSelected(boolean defaultSelected) + { + setBooleanHTMLAttribute("selected", defaultSelected); + } + + public String getText() + { + return getTextContent(); + } + + public int getIndex() + { + return super.getIndex(); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public String getLabel() + { + return getHTMLAttribute("label"); + } + + public void setLabel(String label) + { + setHTMLAttribute("label", label); + } + + public boolean getSelected() + { + if (selected == null) + { + selected = Boolean.valueOf(getDefaultSelected()); + } + return selected.booleanValue(); + } + + public void setSelected(boolean selected) + { + this.selected = Boolean.valueOf(selected); + } + + public String getValue() + { + return getHTMLAttribute("value"); + } + + public void setValue(String value) + { + setHTMLAttribute("value", value); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java new file mode 100644 index 00000000000..2a8c4f6fb11 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java @@ -0,0 +1,69 @@ +/* DomHTMLParagraphElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLParagraphElement; + +/** + * An HTML 'P' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLParagraphElement + extends DomHTMLElement + implements HTMLParagraphElement +{ + + protected DomHTMLParagraphElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java new file mode 100644 index 00000000000..81c6559529e --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java @@ -0,0 +1,99 @@ +/* DomHTMLParamElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLParamElement; + +/** + * An HTML 'PARAM' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLParamElement + extends DomHTMLElement + implements HTMLParamElement +{ + + protected DomHTMLParamElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + + public String getValue() + { + return getHTMLAttribute("value"); + } + + public void setValue(String value) + { + setHTMLAttribute("value", value); + } + + public String getValueType() + { + return getHTMLAttribute("valuetype"); + } + + public void setValueType(String valueType) + { + setHTMLAttribute("valuetype", valueType); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java new file mode 100644 index 00000000000..7b445622509 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java @@ -0,0 +1,266 @@ +/* DomHTMLParser.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.xml.dom.html2; + +import gnu.javax.swing.text.html.parser.support.Parser; + +import java.io.IOException; +import java.io.Reader; + +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedList; + +import javax.swing.text.AttributeSet; +import javax.swing.text.html.HTML; +import javax.swing.text.html.parser.DTD; +import javax.swing.text.html.parser.TagElement; + +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.html2.HTMLDocument; + +/** + * This parser reads HTML from the given stream and stores into + * {@link HTMLDocument}. The HTML tag becomes the {@link Node}. + * The tag attributes become the node attributes. The text inside + * HTML tag is inserted as one or several text nodes. The nested + * HTML tags are inserted as child nodes. + * + * If the strict tree structure, closing the tag means closing all + * nested tags. To work around this, this parser closes the nested + * tags and immediately reopens them after the closed tag. + * In this way, <code><b><i>c</b>d</code> + * is parsed as <code><b><i>c</i></b><i>d</code> . + * + * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) + */ +public class DomHTMLParser + extends gnu.javax.swing.text.html.parser.support.Parser +{ + /** + * The target where HTML document will be inserted. + */ + protected DomHTMLDocument document; + + /** + * The subsequently created new nodes will be inserted as the + * childs of this cursor. + */ + protected Node cursor; + + /** + * Create parser using the given DTD. + * + * @param dtd the DTD (for example, + * {@link gnu.javax.swing.text.html.parser.HTML_401F}). + */ + public DomHTMLParser(DTD dtd) + { + super(dtd); + } + + /** + * Parse SGML insertion ( <! ... > ). + * Currently just treats it as comment. + */ + public boolean parseMarkupDeclarations(StringBuffer strBuff) + throws java.io.IOException + { + Node c = document.createComment(strBuff.toString()); + cursor.appendChild(c); + return false; + } + + /** + * Read the document, present in the given stream, and + * return the corresponding {@link HTMLDocument}. + * + * @param input a stream to read from. + * @return a document, reflecting the structure of the provided HTML + * text. + * + * @throws IOException if the reader throws one. + */ + public HTMLDocument parseDocument(Reader input) + throws IOException + { + try + { + document = new DomHTMLDocument(); + + cursor = document; + + parse(input); + + DomHTMLDocument h = document; + document = null; + return h; + } + catch (Exception ex) + { + ex.printStackTrace(); + throw new IOException("Exception: " + ex.getMessage()); + } + } + + /** + * Create a new node. + * @param name the name of node, case insensitive. + * @return the created node. + */ + protected Node createNode(String name) + { + Node new_node = document.createElement(name.toLowerCase()); + AttributeSet hatts = getAttributes(); + NamedNodeMap natts = new_node.getAttributes(); + + Enumeration enumeration = hatts.getAttributeNames(); + Object key; + Node attribute; + + while (hatts != null) + { + while (enumeration.hasMoreElements()) + { + key = enumeration.nextElement(); + attribute = document.createAttribute(key.toString()); + attribute.setNodeValue(hatts.getAttribute(key).toString()); + natts.setNamedItem(attribute); + } + + // The default values are stored in a parent node. + hatts = hatts.getResolveParent(); + } + + return new_node; + } + + /** + * Handle comment by inserting the comment node. + * @param text the comment text. + */ + protected void handleComment(char[] text) + { + Node c = document.createComment(new String(text)); + cursor.appendChild(c); + } + + /** + * Handle the tag with no content. + * @param tag the tag to handle. + */ + protected void handleEmptyTag(TagElement tag) + { + String name = tag.getHTMLTag().toString(); + + if (name.equalsIgnoreCase("#pcdata")) + return; + + Node c = createNode(name); + cursor.appendChild(c); + } + + /** + * Close the given tag. Close and reopen all nested tags. + * @param tag the tag to close. + */ + protected void handleEndTag(TagElement tag) + { + String name = tag.getHTMLTag().toString(); + String nname = cursor.getNodeName(); + + // Closing the current tag. + if (nname != null && nname.equalsIgnoreCase(name)) + { + cursor = cursor.getParentNode(); + } + else + { + Node nCursor = cursor.getParentNode(); + + // Remember the opened nodes. + LinkedList open = new LinkedList(); + Node close = cursor; + while (close != null && !close.getNodeName().equalsIgnoreCase(name)) + { + if (close != document) + open.addFirst(close); + close = close.getParentNode(); + } + + if (close == null) + cursor = document; + else + cursor = close.getParentNode(); + + // Insert the copies of the opened nodes. + Iterator iter = open.iterator(); + while (iter.hasNext()) + { + Node item = (Node) iter.next(); + Node copy = item.cloneNode(true); + cursor.appendChild(copy); + cursor = copy; + } + } + } + + /** + * Handle the start tag by inserting the HTML element. + * @param tag the tag to handle. + */ + protected void handleStartTag(TagElement tag) + { + HTML.Tag h = tag.getHTMLTag(); + Node c = createNode(h.toString()); + cursor.appendChild(c); + cursor = c; + } + + /** + * Handle text by inserting the text node. + * @param text the text to insert. + */ + protected void handleText(char[] text) + { + Node c = document.createTextNode(text, 0, text.length); + cursor.appendChild(c); + } +} diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java new file mode 100644 index 00000000000..9b36a288700 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java @@ -0,0 +1,69 @@ +/* DomHTMLPreElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLPreElement; + +/** + * An HTML 'PRE' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLPreElement + extends DomHTMLElement + implements HTMLPreElement +{ + + protected DomHTMLPreElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public int getWidth() + { + return getIntHTMLAttribute("width"); + } + + public void setWidth(int width) + { + setIntHTMLAttribute("width", width); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java new file mode 100644 index 00000000000..678a5ada697 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java @@ -0,0 +1,69 @@ +/* DomHTMLQuoteElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLQuoteElement; + +/** + * An HTML 'Q' or 'BLOCKQUOTE' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLQuoteElement + extends DomHTMLElement + implements HTMLQuoteElement +{ + + protected DomHTMLQuoteElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getCite() + { + return getHTMLAttribute("cite"); + } + + public void setCite(String cite) + { + setHTMLAttribute("cite", cite); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java new file mode 100644 index 00000000000..8f8fff58a82 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java @@ -0,0 +1,129 @@ +/* DomHTMLScriptElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLScriptElement; + +/** + * An HTML 'SCRIPT' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLScriptElement + extends DomHTMLElement + implements HTMLScriptElement +{ + + protected DomHTMLScriptElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getText() + { + return getTextContent(); + } + + public void setText(String text) + { + setTextContent(text); + } + + public String getHtmlFor() + { + return getHTMLAttribute("for"); + } + + public void setHtmlFor(String htmlFor) + { + setHTMLAttribute("for", htmlFor); + } + + public String getEvent() + { + return getHTMLAttribute("event"); + } + + public void setEvent(String event) + { + setHTMLAttribute("event", event); + } + + public String getCharset() + { + return getHTMLAttribute("charset"); + } + + public void setCharset(String charset) + { + setHTMLAttribute("charset", charset); + } + + public boolean getDefer() + { + return getBooleanHTMLAttribute("defer"); + } + + public void setDefer(boolean defer) + { + setBooleanHTMLAttribute("defer", defer); + } + + public String getSrc() + { + return getHTMLAttribute("src"); + } + + public void setSrc(String src) + { + setHTMLAttribute("src", src); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java new file mode 100644 index 00000000000..09752e1ffe4 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java @@ -0,0 +1,211 @@ +/* DomHTMLSelectElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDOMException; +import org.w3c.dom.DOMException; +import org.w3c.dom.html2.HTMLElement; +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLOptionElement; +import org.w3c.dom.html2.HTMLOptionsCollection; +import org.w3c.dom.html2.HTMLSelectElement; + +/** + * An HTML 'SELECT' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLSelectElement + extends DomHTMLElement + implements HTMLSelectElement +{ + + protected DomHTMLSelectElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public int getSelectedIndex() + { + HTMLOptionsCollection options = getOptions(); + int len = options.getLength(); + for (int i = 0; i < len; i++) + { + HTMLOptionElement option = (HTMLOptionElement) options.item(i); + if (option.getSelected()) + { + return i; + } + } + return -1; + } + + public void setSelectedIndex(int selectedIndex) + { + HTMLOptionsCollection options = getOptions(); + int len = options.getLength(); + if (selectedIndex < 0 || selectedIndex >= len) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + for (int i = 0; i < len; i++) + { + HTMLOptionElement option = (HTMLOptionElement) options.item(i); + option.setSelected(i == selectedIndex); + } + } + + public String getValue() + { + return getHTMLAttribute("value"); + } + + public void setValue(String value) + { + setHTMLAttribute("value", value); + } + + public int getLength() + { + return getIntHTMLAttribute("length"); + } + + public void setLength(int length) + { + setIntHTMLAttribute("length", length); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public HTMLOptionsCollection getOptions() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("option"); + ret.evaluate(); + return ret; + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public boolean getMultiple() + { + return getBooleanHTMLAttribute("multiple"); + } + + public void setMultiple(boolean multiple) + { + setBooleanHTMLAttribute("multiple", multiple); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public int getSize() + { + return getIntHTMLAttribute("size"); + } + + public void setSize(int size) + { + setIntHTMLAttribute("size", size); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public void add(HTMLElement element, HTMLElement before) + { + insertBefore(before, element); + } + + public void remove(int index) + { + HTMLOptionsCollection options = getOptions(); + int len = options.getLength(); + if (index < 0 || index >= len) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + HTMLOptionElement option = (HTMLOptionElement) options.item(index); + option.getParentNode().removeChild(option); + } + + public void blur() + { + dispatchUIEvent("blur"); + } + + public void focus() + { + dispatchUIEvent("focus"); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java new file mode 100644 index 00000000000..f6f1fcc6844 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java @@ -0,0 +1,89 @@ +/* DomHTMLStyleElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLStyleElement; + +/** + * An HTML 'STYLE' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLStyleElement + extends DomHTMLElement + implements HTMLStyleElement +{ + + protected DomHTMLStyleElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public String getMedia() + { + return getHTMLAttribute("media"); + } + + public void setMedia(String media) + { + setHTMLAttribute("media", media); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java new file mode 100644 index 00000000000..28ea3418895 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java @@ -0,0 +1,70 @@ +/* DomHTMLTableCaptionElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLTableCaptionElement; + +/** + * An HTML 'CAPTION' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTableCaptionElement + extends DomHTMLElement + implements HTMLTableCaptionElement +{ + + protected DomHTMLTableCaptionElement(DomHTMLDocument owner, + String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java new file mode 100644 index 00000000000..41d33af26f8 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java @@ -0,0 +1,205 @@ +/* DomHTMLTableCellElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLTableCellElement; + +/** + * An HTML 'TH' or 'TD' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTableCellElement + extends DomHTMLElement + implements HTMLTableCellElement +{ + + protected DomHTMLTableCellElement(DomHTMLDocument owner, + String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public int getCellIndex() + { + return getIndex(); + } + + public String getAbbr() + { + return getHTMLAttribute("abbr"); + } + + public void setAbbr(String abbr) + { + setHTMLAttribute("abbr", abbr); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getAxis() + { + return getHTMLAttribute("axis"); + } + + public void setAxis(String axis) + { + setHTMLAttribute("axis", axis); + } + + public String getBgColor() + { + return getHTMLAttribute("bgcolor"); + } + + public void setBgColor(String bgColor) + { + setHTMLAttribute("bgcolor", bgColor); + } + + public String getCh() + { + return getHTMLAttribute("char"); + } + + public void setCh(String ch) + { + setHTMLAttribute("char", ch); + } + + public String getChOff() + { + return getHTMLAttribute("charoff"); + } + + public void setChOff(String chOff) + { + setHTMLAttribute("charoff", chOff); + } + + public int getColSpan() + { + return getIntHTMLAttribute("colspan"); + } + + public void setColSpan(int colSpan) + { + setIntHTMLAttribute("colspan", colSpan); + } + + public String getHeaders() + { + return getHTMLAttribute("headers"); + } + + public void setHeaders(String headers) + { + setHTMLAttribute("headers", headers); + } + + public String getHeight() + { + return getHTMLAttribute("height"); + } + + public void setHeight(String height) + { + setHTMLAttribute("height", height); + } + + public boolean getNoWrap() + { + return getBooleanHTMLAttribute("nowrap"); + } + + public void setNoWrap(boolean noWrap) + { + setBooleanHTMLAttribute("nowrap", noWrap); + } + + public int getRowSpan() + { + return getIntHTMLAttribute("rowspan"); + } + + public void setRowSpan(int rowSpan) + { + setIntHTMLAttribute("rowspan", rowSpan); + } + + public String getScope() + { + return getHTMLAttribute("scope"); + } + + public void setScope(String scope) + { + setHTMLAttribute("scope", scope); + } + + public String getVAlign() + { + return getHTMLAttribute("valign"); + } + + public void setVAlign(String vAlign) + { + setHTMLAttribute("valign", vAlign); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java new file mode 100644 index 00000000000..799447dc81e --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java @@ -0,0 +1,120 @@ +/* DomHTMLTableColElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLTableColElement; + +/** + * An HTML 'COL' or 'COLGROUP' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTableColElement + extends DomHTMLElement + implements HTMLTableColElement +{ + + protected DomHTMLTableColElement(DomHTMLDocument owner, + String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getCh() + { + return getHTMLAttribute("char"); + } + + public void setCh(String ch) + { + setHTMLAttribute("char", ch); + } + + public String getChOff() + { + return getHTMLAttribute("charoff"); + } + + public void setChOff(String chOff) + { + setHTMLAttribute("charoff", chOff); + } + + public int getSpan() + { + return getIntHTMLAttribute("span"); + } + + public void setSpan(int span) + { + setIntHTMLAttribute("span", span); + } + + public String getVAlign() + { + return getHTMLAttribute("valign"); + } + + public void setVAlign(String vAlign) + { + setHTMLAttribute("valign", vAlign); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java new file mode 100644 index 00000000000..ea925400e56 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java @@ -0,0 +1,398 @@ +/* DomHTMLTableElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDOMException; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLElement; +import org.w3c.dom.html2.HTMLTableCaptionElement; +import org.w3c.dom.html2.HTMLTableElement; +import org.w3c.dom.html2.HTMLTableSectionElement; + +/** + * An HTML 'TABLE' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTableElement + extends DomHTMLElement + implements HTMLTableElement +{ + + protected DomHTMLTableElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public HTMLTableCaptionElement getCaption() + { + return (HTMLTableCaptionElement) getChildElement("caption"); + } + + public void setCaption(HTMLTableCaptionElement caption) + { + HTMLTableCaptionElement ref = getCaption(); + if (ref == null) + { + appendChild(caption); + } + else + { + replaceChild(caption, ref); + } + } + + public HTMLTableSectionElement getTHead() + { + return (HTMLTableSectionElement) getChildElement("thead"); + } + + public void setTHead(HTMLTableSectionElement tHead) + { + HTMLTableSectionElement ref = getTHead(); + if (ref == null) + { + appendChild(tHead); + } + else + { + replaceChild(tHead, ref); + } + } + + public HTMLTableSectionElement getTFoot() + { + return (HTMLTableSectionElement) getChildElement("tfoot"); + } + + public void setTFoot(HTMLTableSectionElement tFoot) + { + HTMLTableSectionElement ref = getTFoot(); + if (ref == null) + { + appendChild(tFoot); + } + else + { + replaceChild(tFoot, ref); + } + } + + public HTMLCollection getRows() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("tr"); + ret.evaluate(); + return ret; + } + + public HTMLCollection getTBodies() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("tbody"); + ret.evaluate(); + return ret; + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getBgColor() + { + return getHTMLAttribute("bgcolor"); + } + + public void setBgColor(String bgColor) + { + setHTMLAttribute("bgcolor", bgColor); + } + + public String getBorder() + { + return getHTMLAttribute("border"); + } + + public void setBorder(String border) + { + setHTMLAttribute("border", border); + } + + public String getCellPadding() + { + return getHTMLAttribute("cellpadding"); + } + + public void setCellPadding(String cellPadding) + { + setHTMLAttribute("cellpadding", cellPadding); + } + + public String getCellSpacing() + { + return getHTMLAttribute("cellspacing"); + } + + public void setCellSpacing(String cellSpacing) + { + setHTMLAttribute("cellspacing", cellSpacing); + } + + public String getFrame() + { + return getHTMLAttribute("frame"); + } + + public void setFrame(String frame) + { + setHTMLAttribute("frame", frame); + } + + public String getRules() + { + return getHTMLAttribute("rules"); + } + + public void setRules(String rules) + { + setHTMLAttribute("rules", rules); + } + + public String getSummary() + { + return getHTMLAttribute("summary"); + } + + public void setSummary(String summary) + { + setHTMLAttribute("summary", summary); + } + + public String getWidth() + { + return getHTMLAttribute("width"); + } + + public void setWidth(String width) + { + setHTMLAttribute("width", width); + } + + public HTMLElement createTHead() + { + HTMLTableSectionElement ref = getTHead(); + if (ref == null) + { + return (HTMLElement) getOwnerDocument().createElement("thead"); + } + else + { + return ref; + } + } + + public void deleteTHead() + { + HTMLTableSectionElement ref = getTHead(); + if (ref != null) + { + removeChild(ref); + } + } + + public HTMLElement createTFoot() + { + HTMLTableSectionElement ref = getTFoot(); + if (ref == null) + { + return (HTMLElement) getOwnerDocument().createElement("tfoot"); + } + else + { + return ref; + } + } + + public void deleteTFoot() + { + HTMLTableSectionElement ref = getTFoot(); + if (ref != null) + { + removeChild(ref); + } + } + + public HTMLElement createCaption() + { + HTMLTableCaptionElement ref = getCaption(); + if (ref == null) + { + return (HTMLElement) getOwnerDocument().createElement("caption"); + } + else + { + return ref; + } + } + + public void deleteCaption() + { + HTMLTableCaptionElement ref = getCaption(); + if (ref != null) + { + removeChild(ref); + } + } + + public HTMLElement insertRow(int index) + { + Node ref = getRow(index); + Node row = getOwnerDocument().createElement("tr"); + if (ref == null) + { + Node tbody = getChildElement("tbody"); + if (tbody == null) + { + tbody = getOwnerDocument().createElement("tfoot"); + appendChild(tbody); + } + tbody.appendChild(row); + } + else + { + ref.getParentNode().insertBefore(row, ref); + } + return (HTMLElement) row; + } + + public void deleteRow(int index) + { + Node ref = getRow(index); + if (ref == null) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + ref.getParentNode().removeChild(ref); + } + + Node getRow(final int index) + { + int i = 0; + Node thead = getChildElement("thead"); + if (thead != null) + { + for (Node ctx = thead.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + String ctxName = ctx.getLocalName(); + if (ctxName == null) + { + ctxName = ctx.getNodeName(); + } + if (!"tr".equalsIgnoreCase(ctxName)) + { + continue; + } + if (index == i) + { + return ctx; + } + i++; + } + } + Node tbody = getChildElement("tbody"); + if (tbody == null) + { + tbody = this; + } + for (Node ctx = tbody.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + String ctxName = ctx.getLocalName(); + if (ctxName == null) + { + ctxName = ctx.getNodeName(); + } + if (!"tr".equalsIgnoreCase(ctxName)) + { + continue; + } + if (index == i) + { + return ctx; + } + i++; + } + Node tfoot = getChildElement("tfoot"); + if (tfoot != null) + { + for (Node ctx = tfoot.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + String ctxName = ctx.getLocalName(); + if (ctxName == null) + { + ctxName = ctx.getNodeName(); + } + if (!"tr".equalsIgnoreCase(ctxName)) + { + continue; + } + if (index == i) + { + return ctx; + } + i++; + } + } + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java new file mode 100644 index 00000000000..ecd07a743b4 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java @@ -0,0 +1,229 @@ +/* DomHTMLTableRowElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDOMException; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLElement; +import org.w3c.dom.html2.HTMLTableRowElement; + +/** + * An HTML 'TR' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTableRowElement + extends DomHTMLElement + implements HTMLTableRowElement +{ + + protected DomHTMLTableRowElement(DomHTMLDocument owner, + String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public int getRowIndex() + { + return getIndex(); + } + + public int getSectionRowIndex() + { + int index = 0; + DomHTMLElement parent = (DomHTMLElement) getParentElement("table"); + if (parent != null) + { + Node thead = parent.getChildElement("thead"); + if (thead != null) + { + for (Node ctx = thead.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx == this) + { + return index; + } + index++; + } + } + Node tbody = parent.getChildElement("tbody"); + if (tbody != null) + { + for (Node ctx = tbody.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx == this) + { + return index; + } + index++; + } + } + Node tfoot = parent.getChildElement("tfoot"); + if (tfoot != null) + { + for (Node ctx = tfoot.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx == this) + { + return index; + } + index++; + } + } + } + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + + public HTMLCollection getCells() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("th"); + ret.addNodeName("td"); + ret.evaluate(); + return ret; + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getBgColor() + { + return getHTMLAttribute("bgcolor"); + } + + public void setBgColor(String bgColor) + { + setHTMLAttribute("bgcolor", bgColor); + } + + public String getCh() + { + return getHTMLAttribute("char"); + } + + public void setCh(String ch) + { + setHTMLAttribute("char", ch); + } + + public String getChOff() + { + return getHTMLAttribute("charoff"); + } + + public void setChOff(String chOff) + { + setHTMLAttribute("charoff", chOff); + } + + public String getVAlign() + { + return getHTMLAttribute("valign"); + } + + public void setVAlign(String vAlign) + { + setHTMLAttribute("valign", vAlign); + } + + public HTMLElement insertCell(int index) + { + Node ref = getCell(index); + Node cell = getOwnerDocument().createElement("td"); + if (ref == null) + { + appendChild(cell); + } + else + { + insertBefore(cell, ref); + } + return (HTMLElement) cell; + } + + public void deleteCell(int index) + { + Node ref = getCell(index); + if (ref == null) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + removeChild(ref); + } + + Node getCell(final int index) + { + int i = 0; + for (Node ctx = getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + String name = ctx.getLocalName(); + if (name == null) + { + name = ctx.getNodeName(); + } + if (!"td".equalsIgnoreCase(name) && + !"th".equalsIgnoreCase(name)) + { + continue; + } + if (index == i) + { + return ctx; + } + i++; + } + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java new file mode 100644 index 00000000000..da015f1f67a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java @@ -0,0 +1,163 @@ +/* DomHTMLTableSectionElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import gnu.xml.dom.DomDOMException; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.html2.HTMLCollection; +import org.w3c.dom.html2.HTMLElement; +import org.w3c.dom.html2.HTMLTableSectionElement; + +/** + * An HTML 'THEAD', 'TFOOT', or 'TBODY' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTableSectionElement + extends DomHTMLElement + implements HTMLTableSectionElement +{ + + protected DomHTMLTableSectionElement(DomHTMLDocument owner, + String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getAlign() + { + return getHTMLAttribute("align"); + } + + public void setAlign(String align) + { + setHTMLAttribute("align", align); + } + + public String getCh() + { + return getHTMLAttribute("char"); + } + + public void setCh(String ch) + { + setHTMLAttribute("char", ch); + } + + public String getChOff() + { + return getHTMLAttribute("charoff"); + } + + public void setChOff(String chOff) + { + setHTMLAttribute("charoff", chOff); + } + + public String getVAlign() + { + return getHTMLAttribute("valign"); + } + + public void setVAlign(String vAlign) + { + setHTMLAttribute("valign", vAlign); + } + + public HTMLCollection getRows() + { + DomHTMLCollection ret = + new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this); + ret.addNodeName("tr"); + ret.evaluate(); + return ret; + } + + public HTMLElement insertRow(int index) + { + Node ref = getRow(index); + Node row = getOwnerDocument().createElement("tr"); + if (ref == null) + { + appendChild(row); + } + else + { + insertBefore(row, ref); + } + return (HTMLElement) row; + } + + public void deleteRow(int index) + { + Node ref = getRow(index); + if (ref == null) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + removeChild(ref); + } + + Node getRow(final int index) + { + int i = 0; + for (Node ctx = getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + String name = ctx.getLocalName(); + if (name == null) + { + name = ctx.getNodeName(); + } + if (!"tr".equalsIgnoreCase(name)) + { + continue; + } + if (index == i) + { + return ctx; + } + i++; + } + return null; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java new file mode 100644 index 00000000000..22d6105696f --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java @@ -0,0 +1,182 @@ +/* DomHTMLTextAreaElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLFormElement; +import org.w3c.dom.html2.HTMLTextAreaElement; + +/** + * An HTML 'TEXTAREA' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTextAreaElement + extends DomHTMLElement + implements HTMLTextAreaElement +{ + + protected String value; + + protected DomHTMLTextAreaElement(DomHTMLDocument owner, + String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getDefaultValue() + { + return getHTMLAttribute("value"); + } + + public void setDefaultValue(String defaultValue) + { + setHTMLAttribute("value", defaultValue); + } + + public HTMLFormElement getForm() + { + return (HTMLFormElement) getParentElement("form"); + } + + public String getAccessKey() + { + return getHTMLAttribute("accesskey"); + } + + public void setAccessKey(String accessKey) + { + setHTMLAttribute("accesskey", accessKey); + } + + public int getCols() + { + return getIntHTMLAttribute("cols"); + } + + public void setCols(int cols) + { + setIntHTMLAttribute("cols", cols); + } + + public boolean getDisabled() + { + return getBooleanHTMLAttribute("disabled"); + } + + public void setDisabled(boolean disabled) + { + setBooleanHTMLAttribute("disabled", disabled); + } + + public String getName() + { + return getHTMLAttribute("name"); + } + + public void setName(String name) + { + setHTMLAttribute("name", name); + } + + public boolean getReadOnly() + { + return getBooleanHTMLAttribute("readOnly"); + } + + public void setReadOnly(boolean readOnly) + { + setBooleanHTMLAttribute("readonly", readOnly); + } + + public int getRows() + { + return getIntHTMLAttribute("rows"); + } + + public void setRows(int rows) + { + setIntHTMLAttribute("rows", rows); + } + + public int getTabIndex() + { + return getIntHTMLAttribute("tabindex"); + } + + public void setTabIndex(int tabIndex) + { + setIntHTMLAttribute("tabindex", tabIndex); + } + + public String getType() + { + return "textarea"; + } + + public String getValue() + { + if (value == null) + { + value = getDefaultValue(); + } + return value; + } + + public void setValue(String value) + { + this.value = value; + } + + public void blur() + { + dispatchUIEvent("blur"); + } + + public void focus() + { + dispatchUIEvent("focus"); + } + + public void select() + { + dispatchUIEvent("select"); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java new file mode 100644 index 00000000000..f62342d6ff5 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java @@ -0,0 +1,69 @@ +/* DomHTMLTitleElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLTitleElement; + +/** + * An HTML 'TITLE' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLTitleElement + extends DomHTMLElement + implements HTMLTitleElement +{ + + protected DomHTMLTitleElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public String getText() + { + return getTextContent(); + } + + public void setText(String text) + { + setTextContent(text); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java new file mode 100644 index 00000000000..c07adeabf20 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java @@ -0,0 +1,79 @@ +/* DomHTMLUListElement.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.html2; + +import org.w3c.dom.html2.HTMLUListElement; + +/** + * An HTML 'UL' element node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomHTMLUListElement + extends DomHTMLElement + implements HTMLUListElement +{ + + protected DomHTMLUListElement(DomHTMLDocument owner, String namespaceURI, + String name) + { + super(owner, namespaceURI, name); + } + + public boolean getCompact() + { + return getBooleanHTMLAttribute("compact"); + } + + public void setCompact(boolean compact) + { + setBooleanHTMLAttribute("compact", compact); + } + + public String getType() + { + return getHTMLAttribute("type"); + } + + public void setType(String type) + { + setHTMLAttribute("type", type); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSException.java b/libjava/classpath/gnu/xml/dom/ls/DomLSException.java new file mode 100644 index 00000000000..6ac49e66934 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/DomLSException.java @@ -0,0 +1,57 @@ +/* DomLSException.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import org.w3c.dom.ls.LSException; + +/** + * A DOM LS exception incorporating a cause. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomLSException + extends LSException +{ + + public DomLSException(short type, Exception cause) + { + super(type, (cause == null) ? null : cause.getMessage()); + initCause(cause); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java b/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java new file mode 100644 index 00000000000..44274ec4734 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java @@ -0,0 +1,158 @@ +/* DomLSInput.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import org.w3c.dom.ls.LSInput; + +/** + * Specification of XML input to parse. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomLSInput + implements LSInput +{ + + private InputStream in; + private String systemId; + private String publicId; + private String baseURI; + private String encoding; + private boolean certifiedText; + + public Reader getCharacterStream() + { + return new InputStreamReader(in); + } + + public void setCharacterStream(Reader characterStream) + { + in = new ReaderInputStream(characterStream); + } + + public InputStream getByteStream() + { + return in; + } + + public void setByteStream(InputStream byteStream) + { + in = byteStream; + } + + public String getStringData() + { + StringBuffer acc = new StringBuffer(); + Reader reader = getCharacterStream(); + try + { + char[] buf = new char[4096]; + for (int len = reader.read(buf); len != -1; len = reader.read(buf)) + { + acc.append(buf, 0, len); + } + } + catch (IOException e) + { + return null; // ? + } + return acc.toString(); + } + + public void setStringData(String stringData) + { + in = new ReaderInputStream(new StringReader(stringData)); + } + + public String getSystemId() + { + return systemId; + } + + public void setSystemId(String systemId) + { + this.systemId = systemId; + } + + public String getPublicId() + { + return publicId; + } + + public void setPublicId(String publicId) + { + this.publicId = publicId; + } + + public String getBaseURI() + { + return baseURI; + } + + public void setBaseURI(String baseURI) + { + this.baseURI = baseURI; + } + + public String getEncoding() + { + return encoding; + } + + public void setEncoding(String encoding) + { + this.encoding = encoding; + } + + public boolean getCertifiedText() + { + return certifiedText; + } + + public void setCertifiedText(boolean certifiedText) + { + this.certifiedText = certifiedText; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java b/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java new file mode 100644 index 00000000000..c2f4d4132ce --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java @@ -0,0 +1,98 @@ +/* DomLSOutput.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import org.w3c.dom.ls.LSOutput; + +/** + * Specification of XML output to produce. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomLSOutput + implements LSOutput +{ + + private OutputStream out; + private String systemId; + private String encoding; + + public Writer getCharacterStream() + { + return new OutputStreamWriter(out); + } + + public void setCharacterStream(Writer characterStream) + { + out = new WriterOutputStream(characterStream); + } + + public OutputStream getByteStream() + { + return out; + } + + public void setByteStream(OutputStream out) + { + this.out = out; + } + + public String getSystemId() + { + return systemId; + } + + public void setSystemId(String systemId) + { + this.systemId = systemId; + } + + public String getEncoding() + { + return encoding; + } + + public void setEncoding(String encoding) + { + this.encoding = encoding; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java b/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java new file mode 100644 index 00000000000..eb7c1c49f3a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java @@ -0,0 +1,574 @@ +/* DomLSParser.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.List; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import org.w3c.dom.Document; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMStringList; +import org.w3c.dom.Node; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSException; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSParser; +import org.w3c.dom.ls.LSParserFilter; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import gnu.xml.dom.DomDocument; +import gnu.xml.dom.DomDOMException; + +/** + * Parser implementation for GNU DOM. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomLSParser + implements LSParser, DOMConfiguration, DOMStringList, ErrorHandler +{ + + private static final List SUPPORTED_PARAMETERS + = Arrays.asList(new String[] { "cdata-sections", + "comments", + "element-content-whitespace", + "namespaces", + "expand-entity-references", + "coalescing", + "validating", + "xinclude-aware", + "entity-resolver", + "error-handler" }); + + private LSParserFilter filter; + private final boolean async; + private String schemaType; + private SAXEventSink eventSink; + private SAXParserFactory factory; + private XMLReader reader; + + private boolean namespaceAware = true; + private boolean ignoreWhitespace; + private boolean expandEntityReferences; + private boolean ignoreComments; + private boolean coalescing; + private boolean validating; + private boolean xIncludeAware; + private EntityResolver entityResolver; + private ErrorHandler errorHandler; + + public DomLSParser(short mode, String schemaType) + throws DOMException + { + switch (mode) + { + case DOMImplementationLS.MODE_ASYNCHRONOUS: + async = true; + break; + case DOMImplementationLS.MODE_SYNCHRONOUS: + async = false; + break; + default: + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + // TODO schemaType + this.schemaType = schemaType; + factory = SAXParserFactory.newInstance(); + } + + // -- LSParser -- + + public DOMConfiguration getDomConfig() + { + return this; + } + + public LSParserFilter getFilter() + { + return filter; + } + + public void setFilter(LSParserFilter filter) + { + this.filter = filter; + } + + public boolean getAsync() + { + return async; + } + + public boolean getBusy() + { + return eventSink != null; + } + + public Document parse(LSInput input) + throws DOMException, LSException + { + if (async) + { + return doParse(input); + } + else + { + synchronized (this) + { + return doParse(input); + } + } + } + + public Document parseURI(String uri) + throws DOMException, LSException + { + LSInput input = new DomLSInput(); + input.setSystemId(uri); + return parse(input); + } + + public Node parseWithContext(LSInput input, Node context, short action) + throws DOMException, LSException + { + Document doc = (context.getNodeType() == Node.DOCUMENT_NODE) ? + (Document) context : context.getOwnerDocument(); + input.setBaseURI(doc.getDocumentURI()); + // TODO use namespaces defined on context node + Document ret = parse(input); + Node root = ret.getDocumentElement(); + root = doc.adoptNode(root); + switch (action) + { + case ACTION_APPEND_AS_CHILDREN: + context.appendChild(root); + break; + case ACTION_REPLACE_CHILDREN: + Node c1 = context.getFirstChild(); + while (c1 != null) + { + Node next = c1.getNextSibling(); + context.removeChild(c1); + c1 = next; + } + context.appendChild(root); + break; + case ACTION_INSERT_BEFORE: + Node p1 = context.getParentNode(); + p1.insertBefore(root, context); + break; + case ACTION_INSERT_AFTER: + Node p2 = context.getParentNode(); + Node r1 = context.getNextSibling(); + if (r1 == null) + { + p2.appendChild(root); + } + else + { + p2.insertBefore(root, r1); + } + break; + case ACTION_REPLACE: + Node p3 = context.getParentNode(); + Node r2 = context.getNextSibling(); + p3.removeChild(context); + if (r2 == null) + { + p3.appendChild(root); + } + else + { + p3.insertBefore(root, r2); + } + break; + } + return root; + } + + public void abort() + { + if (eventSink != null) + { + eventSink.interrupt(); + } + } + + private Document doParse(LSInput input) + throws DOMException, LSException + { + // create event sink + if (eventSink != null) + { + throw new LSException(LSException.PARSE_ERR, "parse in progress"); + } + InputSource source = getInputSource(input); + eventSink = (filter == null) ? new SAXEventSink() : + new FilteredSAXEventSink(filter); + // configure sink + eventSink.namespaceAware = namespaceAware; + eventSink.ignoreWhitespace = ignoreWhitespace; + eventSink.expandEntityReferences = expandEntityReferences; + eventSink.ignoreComments = ignoreComments; + eventSink.coalescing = coalescing; + // get and configure reader + XMLReader reader = getXMLReader(); + try + { + reader.setContentHandler(eventSink); + reader.setDTDHandler(eventSink); + reader.setProperty("http://xml.org/sax/properties/lexical-handler", + eventSink); + reader.setProperty("http://xml.org/sax/properties/declaration-handler", + eventSink); + reader.setFeature("http://xml.org/sax/features/namespaces", + namespaceAware); + reader.setFeature("http://xml.org/sax/features/namespace-prefixes", + true); + reader.setFeature("http://xml.org/sax/features/validation", + validating); + try + { + reader.setFeature("http://xml.org/sax/features/use-attributes2", + true); + } + catch (SAXNotRecognizedException e) + { + // ignore + } + try + { + reader.setFeature("http://xml.org/sax/features/external-general-entities", + true); + } + catch (SAXNotRecognizedException e) + { + // ignore + } + reader.setEntityResolver(entityResolver); + reader.setErrorHandler(errorHandler); + // parse + reader.parse(source); + } + catch (DOMException e) + { + reader = null; + eventSink = null; + throw e; + } + catch (SAXException e) + { + reader = null; + eventSink = null; + throw new DomLSException(LSException.PARSE_ERR, e); + } + catch (IOException e) + { + reader = null; + eventSink = null; + throw new DomLSException(LSException.PARSE_ERR, e); + } + // return document + Document ret = eventSink.doc; + String systemId = input.getSystemId(); + if (systemId != null && ret instanceof DomDocument) + { + ((DomDocument) ret).setDocumentURI(systemId); + } + eventSink = null; + return ret; + } + + private XMLReader getXMLReader() + throws LSException + { + if (reader == null) + { + factory.setNamespaceAware(namespaceAware); + factory.setValidating(validating); + factory.setXIncludeAware(xIncludeAware); + try + { + SAXParser parser = factory.newSAXParser(); + reader = parser.getXMLReader(); + } + catch (ParserConfigurationException e) + { + throw new DomLSException(LSException.PARSE_ERR, e); + } + catch (SAXException e) + { + throw new DomLSException(LSException.PARSE_ERR, e); + } + } + return reader; + } + + private InputSource getInputSource(LSInput input) + throws LSException + { + InputSource source = null; + String systemId = input.getSystemId(); + InputStream in = input.getByteStream(); + if (in != null) + { + source = new InputSource(in); + source.setSystemId(systemId); + } + if (source == null && entityResolver != null) + { + String publicId = input.getPublicId(); + try + { + source = entityResolver.resolveEntity(publicId, systemId); + } + catch (SAXException e) + { + throw new DomLSException(LSException.PARSE_ERR, e); + } + catch (IOException e) + { + throw new DomLSException(LSException.PARSE_ERR, e); + } + } + if (source == null) + { + URL url = null; + String base = input.getBaseURI(); + try + { + try + { + URL baseURL = (base == null) ? null : new URL(base); + url = (baseURL == null) ? new URL(systemId) : + new URL(baseURL, systemId); + } + catch (MalformedURLException e) + { + File baseFile = (base == null) ? null : new File(base); + url = (baseFile == null) ? new File(systemId).toURL() : + new File(baseFile, systemId).toURL(); + } + in = url.openStream(); + systemId = url.toString(); + source = new InputSource(in); + source.setSystemId(systemId); + } + catch (IOException e) + { + throw new DomLSException(LSException.PARSE_ERR, e); + } + } + return source; + } + + // -- DOMConfiguration -- + + public void setParameter(String name, Object value) + throws DOMException + { + name = name.toLowerCase(); + if ("cdata-sections".equals(name)) + { + coalescing = !((Boolean) value).booleanValue(); + } + else if ("comments".equals(name)) + { + ignoreComments = !((Boolean) value).booleanValue(); + } + else if ("element-content-whitespace".equals(name)) + { + ignoreWhitespace = !((Boolean) value).booleanValue(); + } + else if ("namespaces".equals(name)) + { + namespaceAware = ((Boolean) value).booleanValue(); + } + else if ("expand-entity-references".equals(name)) + { + expandEntityReferences = ((Boolean) value).booleanValue(); + } + else if ("coalescing".equals(name)) + { + coalescing = ((Boolean) value).booleanValue(); + } + else if ("validating".equals(name)) + { + validating = ((Boolean) value).booleanValue(); + } + else if ("xinclude-aware".equals(name)) + { + xIncludeAware = ((Boolean) value).booleanValue(); + } + else if ("entity-resolver".equals(name)) + { + entityResolver = (EntityResolver) value; + } + else if ("error-handler".equals(name)) + { + errorHandler = (ErrorHandler) value; + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + // invalidate reader, a new one will be created + reader = null; + } + + public Object getParameter(String name) + throws DOMException + { + name = name.toLowerCase(); + if ("cdata-sections".equals(name)) + { + return coalescing ? Boolean.FALSE : Boolean.TRUE; + } + else if ("comments".equals(name)) + { + return ignoreComments ? Boolean.FALSE : Boolean.TRUE; + } + else if ("element-content-whitespace".equals(name)) + { + return ignoreWhitespace ? Boolean.FALSE : Boolean.TRUE; + } + else if ("namespaces".equals(name)) + { + return namespaceAware ? Boolean.TRUE : Boolean.FALSE; + } + else if ("expand-entity-references".equals(name)) + { + return expandEntityReferences ? Boolean.TRUE : Boolean.FALSE; + } + else if ("coalescing".equals(name)) + { + return coalescing ? Boolean.TRUE : Boolean.FALSE; + } + else if ("validating".equals(name)) + { + return validating ? Boolean.TRUE : Boolean.FALSE; + } + else if ("xinclude-aware".equals(name)) + { + return xIncludeAware ? Boolean.TRUE : Boolean.FALSE; + } + else if ("entity-resolver".equals(name)) + { + return entityResolver; + } + else if ("error-handler".equals(name)) + { + return errorHandler; + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + } + + public boolean canSetParameter(String name, Object value) + { + return contains(name); + } + + public DOMStringList getParameterNames() + { + return this; + } + + // -- DOMStringList -- + + public String item(int i) + { + return (String) SUPPORTED_PARAMETERS.get(i); + } + + public int getLength() + { + return SUPPORTED_PARAMETERS.size(); + } + + public boolean contains(String str) + { + return SUPPORTED_PARAMETERS.contains(str); + } + + // -- ErrorHandler -- + + public void warning(SAXParseException e) + throws SAXException + { + if (errorHandler != null) + { + errorHandler.warning(e); + } + } + + public void error(SAXParseException e) + throws SAXException + { + if (errorHandler != null) + { + errorHandler.error(e); + } + } + + public void fatalError(SAXParseException e) + throws SAXException + { + if (errorHandler != null) + { + errorHandler.fatalError(e); + } + abort(); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java b/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java new file mode 100644 index 00000000000..cedaf151e70 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java @@ -0,0 +1,354 @@ +/* DomLSSerializer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; +import java.util.List; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMStringList; +import org.w3c.dom.Node; +import org.w3c.dom.ls.LSException; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSSerializer; +import org.w3c.dom.ls.LSSerializerFilter; +import org.w3c.dom.traversal.NodeFilter; +import gnu.xml.dom.DomDOMException; +import gnu.xml.transform.StreamSerializer; + +/** + * Serialize a DOM node to a stream. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DomLSSerializer + extends StreamSerializer + implements LSSerializer, DOMConfiguration, DOMStringList +{ + + private static final List SUPPORTED_PARAMETERS = + Arrays.asList(new String[] {"discard-default-content", + "xml-declaration"}); + + private LSSerializerFilter filter; + private StreamSerializer serializer; + + public DomLSSerializer() + { + super(); + discardDefaultContent = true; + } + + // -- LSSerializer -- + + public DOMConfiguration getDomConfig() + { + return this; + } + + public String getNewLine() + { + return eol; + } + + public void setNewLine(String newLine) + { + if (newLine == null) + { + newLine = System.getProperty("line.separator"); + } + eol = newLine; + } + + public LSSerializerFilter getFilter() + { + return filter; + } + + public void setFilter(LSSerializerFilter filter) + { + this.filter = filter; + } + + public boolean write(Node node, LSOutput output) + throws LSException + { + OutputStream out = output.getByteStream(); + try + { + if (out == null) + { + String systemId = output.getSystemId(); + try + { + URL url = new URL(systemId); + URLConnection connection = url.openConnection(); + connection.setDoOutput(true); + if (connection instanceof HttpURLConnection) + { + ((HttpURLConnection) connection).setRequestMethod("PUT"); + } + out = connection.getOutputStream(); + } + catch (MalformedURLException e) + { + File file = new File(systemId); + out = new FileOutputStream(file); + } + } + serialize(node, out); + out.flush(); + return true; + } + catch (IOException e) + { + throw new DomLSException(LSException.SERIALIZE_ERR, e); + } + } + + public boolean writeToURI(Node node, String uri) + throws LSException + { + LSOutput output = new DomLSOutput(); + output.setSystemId(uri); + return write(node, output); + } + + public String writeToString(Node node) + throws DOMException, LSException + { + Writer writer = new StringWriter(); + LSOutput output = new DomLSOutput(); + output.setCharacterStream(writer); + write(node, output); + return writer.toString(); + } + + public void serialize(Node node, OutputStream out) + throws IOException + { + if (filter == null) + { + super.serialize(node, out); + } + else + { + int wts = filter.getWhatToShow(); + if (wts != NodeFilter.SHOW_ALL) + { + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + if ((wts & NodeFilter.SHOW_ATTRIBUTE) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.TEXT_NODE: + if ((wts & NodeFilter.SHOW_TEXT) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.ELEMENT_NODE: + if ((wts & NodeFilter.SHOW_ELEMENT) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.CDATA_SECTION_NODE: + if ((wts & NodeFilter.SHOW_CDATA_SECTION) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.COMMENT_NODE: + if ((wts & NodeFilter.SHOW_COMMENT) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.DOCUMENT_NODE: + if ((wts & NodeFilter.SHOW_DOCUMENT) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.DOCUMENT_TYPE_NODE: + if ((wts & NodeFilter.SHOW_DOCUMENT_TYPE) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.PROCESSING_INSTRUCTION_NODE: + if ((wts & NodeFilter.SHOW_PROCESSING_INSTRUCTION) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.DOCUMENT_FRAGMENT_NODE: + if ((wts & NodeFilter.SHOW_DOCUMENT_FRAGMENT) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.ENTITY_NODE: + if ((wts & NodeFilter.SHOW_ENTITY) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.ENTITY_REFERENCE_NODE: + if ((wts & NodeFilter.SHOW_ENTITY_REFERENCE) == 0) + { + super.serialize(node, out); + return; + } + break; + case Node.NOTATION_NODE: + if ((wts & NodeFilter.SHOW_NOTATION) == 0) + { + super.serialize(node, out); + return; + } + break; + } + } + switch (filter.acceptNode(node)) + { + case NodeFilter.FILTER_ACCEPT: + super.serialize(node, out); + break; + case NodeFilter.FILTER_REJECT: + break; + case NodeFilter.FILTER_SKIP: + Node first = node.getFirstChild(); + if (first != null) + { + serialize(first, out); + } + break; + } + } + } + + // -- DOMConfiguration -- + + public void setParameter(String name, Object value) + throws DOMException + { + if ("discard-default-content".equals(name)) + { + discardDefaultContent = "true".equals(value.toString()); + } + else if ("xml-declaration".equals(name)) + { + xmlDeclaration = "false".equals(value.toString()); + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + } + + public Object getParameter(String name) + throws DOMException + { + if ("discard-default-content".equals(name)) + { + return discardDefaultContent ? "true" : "false"; + } + else if ("xml-declaration".equals(name)) + { + return xmlDeclaration ? "true" : "false"; + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + } + + public boolean canSetParameter(String name, Object value) + { + return contains(name); + } + + public DOMStringList getParameterNames() + { + return this; + } + + // -- DOMStringList -- + + public String item(int i) + { + return (String) SUPPORTED_PARAMETERS.get(i); + } + + public int getLength() + { + return SUPPORTED_PARAMETERS.size(); + } + + public boolean contains(String str) + { + return SUPPORTED_PARAMETERS.contains(str); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java b/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java new file mode 100644 index 00000000000..1c99ee524fa --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java @@ -0,0 +1,354 @@ +/* FilteredSAXEventSink.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.util.LinkedList; +import org.w3c.dom.Attr; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import org.w3c.dom.ls.LSParserFilter; +import org.w3c.dom.traversal.NodeFilter; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +/** + * A SAX event sink that calls out to a parser filter in order to decide + * whether to insert nodes into the tree. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class FilteredSAXEventSink + extends SAXEventSink +{ + + final LSParserFilter filter; + final int whatToShow; + + /** + * Stack of elements to insert. + */ + LinkedList nodes; + + /** + * Corresponding stack of filter decisions about the nodes. + */ + LinkedList decisions; + + /** + * True when rejecting child nodes. + */ + boolean rejecting; + + FilteredSAXEventSink(LSParserFilter filter) + { + this.filter = filter; + whatToShow = filter.getWhatToShow(); + } + + public void startDocument() + throws SAXException + { + if (interrupted) + { + return; + } + nodes = new LinkedList(); + decisions = new LinkedList(); + + super.startDocument(); + } + + public void endDocument() + throws SAXException + { + if (interrupted) + { + return; + } + super.endDocument(); + + switch (getDecision(ctx, false)) + { + case LSParserFilter.FILTER_REJECT: + ctx = null; + doc = null; + break; + } + + nodes = null; + decisions = null; + } + + public void startElement(String uri, String localName, String qName, + Attributes atts) + throws SAXException + { + if (rejecting || interrupted) + { + return; + } + Element element = createElement(uri, localName, qName, atts); + ctx = element; + + short decision = getDecision(element, true); + nodes.addLast(element); + decisions.addLast(new Short(decision)); + + switch (decision) + { + case LSParserFilter.FILTER_REJECT: + rejecting = true; + break; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + break; + } + } + + protected Attr createAttr(Attributes atts, int index) + { + Attr attr = super.createAttr(atts, index); + short decision = getDecision(attr, false); + switch (decision) + { + case LSParserFilter.FILTER_REJECT: + return null; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + return null; + } + return attr; + } + + public void endElement(String uri, String localName, String qName) + throws SAXException + { + if (rejecting || interrupted) + { + return; + } + super.endElement(uri, localName, qName); + + Element element = (Element) nodes.removeLast(); + Node parent = nodes.isEmpty() ? doc : (Node) nodes.getLast(); + ctx = parent; + short decision = ((Short) decisions.removeLast()).shortValue(); + switch (decision) + { + case LSParserFilter.FILTER_SKIP: + // Add all children of element to parent + for (Node child = element.getFirstChild(); child != null; + child = child.getNextSibling()) + { + parent.insertBefore(child, element); + } + return; + case LSParserFilter.FILTER_REJECT: + rejecting = false; + break; + } + decision = getDecision(element, false); + switch (decision) + { + case LSParserFilter.FILTER_ACCEPT: + parent.appendChild(element); + break; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + break; + } + } + + public void characters(char[] c, int off, int len) + throws SAXException + { + if (rejecting || interrupted) + { + return; + } + Text text = createText(c, off, len); + short decision = getDecision(text, false); + switch (decision) + { + case LSParserFilter.FILTER_ACCEPT: + ctx.appendChild(text); + break; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + break; + } + } + + public void processingInstruction(String target, String data) + throws SAXException + { + if (rejecting || interrupted || inDTD) + { + return; + } + Node pi = createProcessingInstruction(target, data); + short decision = getDecision(pi, false); + switch (decision) + { + case LSParserFilter.FILTER_ACCEPT: + ctx.appendChild(pi); + break; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + break; + } + } + + public void startDTD(String name, String publicId, String systemId) + throws SAXException + { + if (interrupted) + { + return; + } + Node doctype = createDocumentType(name, publicId, systemId); + ctx = doctype; + inDTD = true; + nodes.addLast(doctype); + decisions.addLast(new Short(LSParserFilter.FILTER_ACCEPT)); + } + + public void endDTD() + throws SAXException + { + if (interrupted) + { + return; + } + Node doctype = (Node) nodes.removeLast(); + decisions.removeLast(); + inDTD = false; + ctx = doc; + short decision = getDecision(doctype, false); + switch (decision) + { + case LSParserFilter.FILTER_ACCEPT: + ctx.appendChild(doctype); + break; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + break; + } + } + + public void comment(char[] c, int off, int len) + throws SAXException + { + if (rejecting || interrupted || inDTD) + { + return; + } + Node comment = createComment(c, off, len); + short decision = getDecision(comment, false); + switch (decision) + { + case LSParserFilter.FILTER_ACCEPT: + ctx.appendChild(comment); + break; + case LSParserFilter.FILTER_INTERRUPT: + interrupted = true; + break; + } + } + + // TODO declarations + + short getDecision(Node node, boolean start) + { + boolean show = (whatToShow == NodeFilter.SHOW_ALL); + if (!show) + { + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + show = ((whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0); + break; + case Node.TEXT_NODE: + show = ((whatToShow & NodeFilter.SHOW_TEXT) != 0); + break; + case Node.CDATA_SECTION_NODE: + show = ((whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0); + break; + case Node.ELEMENT_NODE: + show = ((whatToShow & NodeFilter.SHOW_ELEMENT) != 0); + break; + case Node.COMMENT_NODE: + show = ((whatToShow & NodeFilter.SHOW_COMMENT) != 0); + break; + case Node.DOCUMENT_NODE: + show = ((whatToShow & NodeFilter.SHOW_DOCUMENT) != 0); + break; + case Node.PROCESSING_INSTRUCTION_NODE: + show = ((whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0); + break; + case Node.DOCUMENT_FRAGMENT_NODE: + show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0); + break; + case Node.DOCUMENT_TYPE_NODE: + show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0); + break; + case Node.ENTITY_REFERENCE_NODE: + show = ((whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0); + break; + case Node.ENTITY_NODE: + show = ((whatToShow & NodeFilter.SHOW_ENTITY) != 0); + break; + case Node.NOTATION_NODE: + show = ((whatToShow & NodeFilter.SHOW_NOTATION) != 0); + break; + } + } + if (!show) + { + return LSParserFilter.FILTER_ACCEPT; + } + if (start) + { + return filter.startElement((Element) node); + } + return filter.acceptNode(node); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java b/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java new file mode 100644 index 00000000000..5b41abd3d74 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java @@ -0,0 +1,237 @@ +/* ReaderInputStream.java -- + Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.io.InputStream; +import java.io.IOException; +import java.io.Reader; + +/** + * Character stream wrapper. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + * @author <a href='mailto:mark@klomp.org'>Mark Wielaard</a> + */ +public class ReaderInputStream + extends InputStream +{ + + private Reader reader; + private String encoding; + + // Holds extra spillover data if necessary + private byte extra[]; + private int pos; + + private byte extra_marked[]; + private int pos_marked; + + public ReaderInputStream(Reader reader) + { + this.reader = reader; + this.encoding = "UTF-8"; + } + + void setEncoding(String encoding) + { + this.encoding = encoding; + } + + public int read() + throws IOException + { + if (extra != null) + { + int result = extra[pos]; + pos++; + if (pos >= extra.length) + { + extra = null; + } + return result; + } + return reader.read(); + } + + public int read(byte[] b) + throws IOException + { + return read(b, 0, b.length); + } + + public int read(byte[] b, int off, int len) + throws IOException + { + if (len == 0) + { + return 0; + } + + if (extra != null) + { + int available = extra.length - pos; + int l = available < len ? available : len; + System.arraycopy(extra, 0, b, off, l); + pos += l; + if (pos >= extra.length) + { + extra = null; + } + return l; + } + + char[] c = new char[len]; + int l = reader.read(c, 0, len); + if (l == -1) + { + return -1; + } + + String s = new String(c, 0, l); + byte[] d = s.getBytes(encoding); + + int available = d.length; + int more = d.length - len; + if (more > 0) + { + extra = new byte[more]; + pos = 0; + System.arraycopy(d, len, extra, 0, more); + available -= more; + } + + System.arraycopy(d, 0, b, off, available); + return available; + } + + public void close() + throws IOException + { + reader.close(); + } + + public boolean markSupported() + { + return reader.markSupported(); + } + + public void mark(int limit) + { + if (extra != null) + { + extra_marked = new byte[extra.length]; + System.arraycopy(extra, 0, extra_marked, 0, extra.length); + pos_marked = pos; + } + else + { + extra_marked = null; + } + + try + { + // Note that this might be a bit more than asked for. + // Because we might also have the extra_marked bytes. + // That is fine (and necessary for reset() to work). + reader.mark(limit); + } + catch (IOException ioe) + { + throw new RuntimeException(ioe); + } + } + + public void reset() + throws IOException + { + extra = extra_marked; + pos = pos_marked; + extra_marked = null; + + reader.reset(); + } + + public long skip(long n) + throws IOException + { + long done = 0; + if (extra != null) + { + int available = extra.length - pos; + done = available < n ? available : n; + pos += done; + if (pos >= extra.length) + { + extra = null; + } + } + + n -= done; + if (n > 0) + { + return reader.skip(n) + done; + } + else + { + return done; + } + } + + /** + * Returns conservative number of bytes available without blocking. + * Actual number of bytes that can be read without blocking might + * be (much) bigger. + */ + public int available() + throws IOException + { + if (extra != null) + { + return pos - extra.length; + } + + return reader.ready() ? 1 : 0; + } + + public String toString() + { + return getClass().getName() + "[" + reader + ", " + encoding + "]"; + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java b/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java new file mode 100644 index 00000000000..a850460b1e2 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java @@ -0,0 +1,557 @@ +/* SAXEventSink.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.xml.XMLConstants; +import org.w3c.dom.Attr; +import org.w3c.dom.DocumentType; +import org.w3c.dom.Element; +import org.w3c.dom.Entity; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import org.xml.sax.Attributes; +import org.xml.sax.DTDHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.ext.Attributes2; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; +import gnu.xml.aelfred2.ContentHandler2; +import gnu.xml.dom.DomAttr; +import gnu.xml.dom.DomDocument; +import gnu.xml.dom.DomDoctype; + +/** + * A SAX content and lexical handler used to construct a DOM document. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class SAXEventSink + implements ContentHandler2, LexicalHandler, DTDHandler, DeclHandler +{ + + private static final String XMLNS_URI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + private static final String XMLNS_PREFIX = XMLConstants.XMLNS_ATTRIBUTE; + + boolean namespaceAware; + boolean ignoreWhitespace; + boolean expandEntityReferences; + boolean ignoreComments; + boolean coalescing; + + DomDocument doc; // document being constructed + Node ctx; // current context (parent node) + LinkedList entityCtx; // entity context + List pending; // namespace nodes waiting for a declaring element + Locator locator; + boolean inCDATA; + boolean inDTD; + boolean interrupted; + + void interrupt() + { + interrupted = true; + } + + // -- ContentHandler2 -- + + public void setDocumentLocator(Locator locator) + { + this.locator = locator; + } + + public void startDocument() + throws SAXException + { + if (namespaceAware) + { + pending = new LinkedList(); + } + doc = new DomDocument(); + doc.setStrictErrorChecking(false); + doc.setBuilding(true); + ctx = doc; + } + + public void xmlDecl(String version, String encoding, boolean standalone, + String inputEncoding) + throws SAXException + { + if (interrupted) + { + return; + } + doc.setXmlVersion(version); + doc.setXmlEncoding(encoding); + doc.setXmlStandalone(standalone); + doc.setInputEncoding(inputEncoding); + } + + public void endDocument() + throws SAXException + { + doc.setStrictErrorChecking(true); + doc.setBuilding(false); + DomDoctype doctype = (DomDoctype) doc.getDoctype(); + if (doctype != null) + { + doctype.makeReadonly(); + } + ctx = null; + locator = null; + } + + public void startPrefixMapping(String prefix, String uri) + throws SAXException + { + if (namespaceAware) + { + String nsName = (prefix != null && prefix.length() > 0) ? + XMLNS_PREFIX + ":" + prefix : XMLNS_PREFIX; + DomAttr ns = (DomAttr) doc.createAttributeNS(XMLNS_URI, nsName); + ns.setNodeValue(uri); + if (ctx.getNodeType() == Node.ATTRIBUTE_NODE) + { + // Add to owner element + Node target = ((Attr) ctx).getOwnerElement(); + target.getAttributes().setNamedItemNS(ns); + } + else + { + // Add to pending list; namespace node will be inserted when + // element is seen + pending.add(ns); + } + } + } + + public void endPrefixMapping(String prefix) + throws SAXException + { + } + + public void startElement(String uri, String localName, String qName, + Attributes atts) + throws SAXException + { + if (interrupted) + { + return; + } + Element element = createElement(uri, localName, qName, atts); + // add element to context + ctx.appendChild(element); + ctx = element; + } + + protected Element createElement(String uri, String localName, String qName, + Attributes atts) + throws SAXException + { + // create element node + Element element = namespaceAware ? + doc.createElementNS(uri, qName) : + doc.createElement(qName); + NamedNodeMap attrs = element.getAttributes(); + if (namespaceAware && !pending.isEmpty()) + { + // add pending namespace nodes + for (Iterator i = pending.iterator(); i.hasNext(); ) + { + Node ns = (Node) i.next(); + attrs.setNamedItemNS(ns); + } + pending.clear(); + } + // add attributes + int len = atts.getLength(); + for (int i = 0; i < len; i++) + { + // create attribute + Attr attr = createAttr(atts, i); + if (attr != null) + { + // add attribute to element + if (namespaceAware) + { + attrs.setNamedItemNS(attr); + } + else + { + attrs.setNamedItem(attr); + } + } + } + return element; + } + + protected Attr createAttr(Attributes atts, int index) + { + DomAttr attr; + if (namespaceAware) + { + String a_uri = atts.getURI(index); + String a_qName = atts.getQName(index); + attr = (DomAttr) doc.createAttributeNS(a_uri, a_qName); + } + else + { + String a_qName = atts.getQName(index); + attr = (DomAttr) doc.createAttribute(a_qName); + } + attr.setNodeValue(atts.getValue(index)); + if (atts instanceof Attributes2) + { + Attributes2 atts2 = (Attributes2) atts; + // TODO attr.setDeclared(atts2.isDeclared(index)); + attr.setSpecified(atts2.isSpecified(index)); + } + return attr; + } + + public void endElement(String uri, String localName, String qName) + throws SAXException + { + if (interrupted) + { + return; + } + if (namespaceAware) + { + pending.clear(); + } + ctx = ctx.getParentNode(); + } + + public void characters(char[] c, int off, int len) + throws SAXException + { + if (interrupted) + { + return; + } + ctx.appendChild(createText(c, off, len)); + } + + protected Text createText(char[] c, int off, int len) + throws SAXException + { + Text text = (inCDATA && !coalescing) ? + doc.createCDATASection(new String(c, off, len)) : + doc.createTextNode(new String(c, off, len)); + return text; + } + + public void ignorableWhitespace(char[] c, int off, int len) + throws SAXException + { + if (interrupted) + { + return; + } + if (!ignoreWhitespace) + { + characters(c, off, len); + } + } + + public void processingInstruction(String target, String data) + throws SAXException + { + if (interrupted) + { + return; + } + if (!inDTD) + { + Node pi = createProcessingInstruction(target, data); + ctx.appendChild(pi); + } + } + + protected Node createProcessingInstruction(String target, String data) + { + return doc.createProcessingInstruction(target, data); + } + + public void skippedEntity(String name) + throws SAXException + { + // This callback is totally pointless + } + + // -- LexicalHandler -- + + public void startDTD(String name, String publicId, String systemId) + throws SAXException + { + if (interrupted) + { + return; + } + Node doctype = createDocumentType(name, publicId, systemId); + doc.appendChild(doctype); + ctx = doctype; + inDTD = true; + } + + protected Node createDocumentType(String name, String publicId, + String systemId) + { + return new DomDoctype(doc, name, publicId, systemId); + } + + public void endDTD() + throws SAXException + { + if (interrupted) + { + return; + } + inDTD = false; + ctx = ctx.getParentNode(); + } + + public void startEntity(String name) + throws SAXException + { + DocumentType doctype = doc.getDoctype(); + if (doctype == null) + { + throw new SAXException("SAX parser error: " + + "reference to entity in undeclared doctype"); + } + if ("[dtd]".equals(name) || name.charAt(0) == '%') + { + // Ignore DTD and parameter entities + ctx = doctype; + return; + } + if ("lt".equals(name) || + "gt".equals(name) || + "amp".equals(name) || + "apos".equals(name) || + "quot".equals(name)) + { + return; + } + // Get entity + NamedNodeMap entities = doctype.getEntities(); + Entity entity = (Entity) entities.getNamedItem(name); + if (entity == null) + { + throw new SAXException("SAX parser error: " + + "reference to undeclared entity: " + name); + } + pushEntity(entity); + } + + public void endEntity(String name) + throws SAXException + { + if ("[dtd]".equals(name) || name.charAt(0) == '%') + { + // Ignore DTD and parameter entities + return; + } + if ("lt".equals(name) || + "gt".equals(name) || + "amp".equals(name) || + "apos".equals(name) || + "quot".equals(name)) + { + return; + } + // Get entity + Entity entity = popEntity(); + // TODO resolve external entities to ensure that entity has content + if (expandEntityReferences) + { + // Get entity content + for (Node child = entity.getFirstChild(); child != null; + child = child.getNextSibling()) + { + ctx.appendChild(child); + } + } + else + { + Node entityReference = doc.createEntityReference(name); + ctx.appendChild(entityReference); + } + } + + void pushEntity(Node entity) + { + if (entityCtx == null) + { + entityCtx = new LinkedList(); + } + entityCtx.addLast(ctx); + ctx = entity; + } + + Entity popEntity() + { + Entity ret = (Entity) ctx; + ctx = (Node) entityCtx.removeLast(); + return ret; + } + + public void startCDATA() + throws SAXException + { + inCDATA = true; + } + + public void endCDATA() + throws SAXException + { + inCDATA = false; + } + + public void comment(char[] c, int off, int len) + throws SAXException + { + if (interrupted) + { + return; + } + if (!inDTD) + { + Node comment = createComment(c, off, len); + ctx.appendChild(comment); + } + } + + protected Node createComment(char[] c, int off, int len) + { + return doc.createComment(new String(c, off, len)); + } + + // -- DTDHandler -- + + public void notationDecl(String name, String publicId, String systemId) + throws SAXException + { + if (interrupted) + { + return; + } + DomDoctype doctype = (DomDoctype) ctx; + doctype.declareNotation(name, publicId, systemId); + } + + public void unparsedEntityDecl(String name, String publicId, String systemId, + String notationName) + throws SAXException + { + if (interrupted) + { + return; + } + DomDoctype doctype = (DomDoctype) ctx; + Entity entity = doctype.declareEntity(name, publicId, systemId, + notationName); + } + + // -- DeclHandler -- + + public void elementDecl(String name, String model) + throws SAXException + { + if (interrupted) + { + return; + } + // Ignore fake element declarations generated by ValidationConsumer. + // If an element is not really declared in the DTD it will not be + // declared in the document model. + if (!(ctx instanceof DomDoctype)) + { + return; + } + DomDoctype doctype = (DomDoctype) ctx; + doctype.elementDecl(name, model); + } + + public void attributeDecl(String eName, String aName, String type, + String mode, String value) + throws SAXException + { + if (interrupted) + { + return; + } + DomDoctype doctype = (DomDoctype) ctx; + doctype.attributeDecl(eName, aName, type, mode, value); + } + + public void internalEntityDecl(String name, String value) + throws SAXException + { + if (interrupted) + { + return; + } + DomDoctype doctype = (DomDoctype) ctx; + Entity entity = doctype.declareEntity(name, null, null, null); + if (entity != null) + { + Node text = doc.createTextNode(value); + entity.appendChild(text); + } + } + + public void externalEntityDecl(String name, String publicId, String systemId) + throws SAXException + { + if (interrupted) + { + return; + } + DomDoctype doctype = (DomDoctype) ctx; + Entity entity = doctype.declareEntity(name, publicId, systemId, null); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java b/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java new file mode 100644 index 00000000000..2fb20614722 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java @@ -0,0 +1,98 @@ +/* WriterOutputStream.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom.ls; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; + +/** + * Character stream wrapper. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class WriterOutputStream + extends OutputStream +{ + + private Writer writer; + private String encoding; + + public WriterOutputStream(Writer writer) + { + this.writer = writer; + this.encoding = "UTF-8"; + } + + void setEncoding(String encoding) + { + this.encoding = encoding; + } + + public void write(int c) + throws IOException + { + writer.write(c); + } + + public void write(byte[] b) + throws IOException + { + write(b, 0, b.length); + } + + public void write(byte[] b, int off, int len) + throws IOException + { + writer.write(new String(b, off, len, encoding)); + } + + public void close() + throws IOException + { + writer.close(); + } + + public void flush() + throws IOException + { + writer.flush(); + } + +} + diff --git a/libjava/classpath/gnu/xml/dom/package.html b/libjava/classpath/gnu/xml/dom/package.html new file mode 100644 index 00000000000..fbc864a4d74 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/package.html @@ -0,0 +1,273 @@ +<html> +<body> + +<p> +This is a Free Software DOM Level 3 implementation, supporting these features: +<ul> +<li>"XML"</li> +<li>"Events"</li> +<li>"MutationEvents"</li> +<li>"HTMLEvents" (won't generate them though)</li> +<li>"UIEvents" (also won't generate them)</li> +<li>"USER-Events" (a conformant extension)</li> +<li>"Traversal" (optional)</li> +<li>"XPath"</li> +<li>"LS" and "LS-Async"</li> +</ul> +It is intended to be a reasonable base both for +experimentation and supporting additional DOM modules as clean layers. +</p> + +<p> +Note that while DOM does not specify its behavior in the +face of concurrent access, this implementation does. +Specifically: +<ul> +<li>If only one thread at a time accesses a Document, +of if several threads cooperate for read-only access, +then no concurrency conflicts will occur.</li> +<li>If several threads mutate a given document +(or send events using it) at the same time, +there is currently no guarantee that +they won't interfere with each other.</li> +</ul> +</p> + +<h3>Design Goals</h3> + +<p> +A number of DOM implementations are available in Java, including +commercial ones from Sun, IBM, Oracle, and DataChannel as well as +noncommercial ones from Docuverse, OpenXML, and Silfide. Why have +another? Some of the goals of this version: +</p> + +<ul> +<li>Advanced DOM support. This was the first generally available +implementation of DOM Level 2 in Java, and one of the first Level 3 +and XPath implementations.</li> + +<li> Free Software. This one is distributed under the GPL (with +"library exception") so it can be used with a different class of +application.</li> + +<li>Second implementation syndrome. I can do it simpler this time +around ... and heck, writing it only takes a bit over a day once you +know your way around.</li> + +<li>Sanity check the then-current Last Call DOM draft. Best to find +bugs early, when they're relatively fixable. Yes, bugs were found.</li> + +<li>Modularity. Most of the implementations mentioned above are part +of huge packages; take all (including bugs, of which some have far +too many), or take nothing. I prefer a menu approach, when possible. +This code is standalone, not beholden to any particular parser or XSL +or XPath code.</li> + +<li>OK, I'm a hacker, I like to write code.</li> +</ul> + +<p> +This also works with the GNU Compiler for Java (GCJ). GCJ promises +to be quite the environment for programming Java, both directly and from +C++ using the new CNI interfaces (which really use C++, unlike JNI). </p> + + +<h3>Open Issues</h3> + +<p>At this writing:</p> +<ul> +<li>See below for some restrictions on the mutation event +support ... some events aren't reported (and likely won't be).</li> + +<li>More testing and conformance work is needed.</li> + +<li>We need an XML Schema validator (actually we need validation in the DOM +full stop).</li> +</ul> + +<p> +I ran a profiler a few times and remove some of the performance hotspots, +but it's not tuned. Reporting mutation events, in particular, is +rather costly -- it started at about a 40% penalty for appendNode calls, +I've got it down around 12%, but it'll be hard to shrink it much further. +The overall code size is relatively small, though you may want to be rid of +many of the unused DOM interface classes (HTML, CSS, and so on). +</p> + + +<h2><a name="features">Features of this Package</a></h2> + +<p> Starting with DOM Level 2, you can really see that DOM is constructed +as a bunch of optional modules around a core of either XML or HTML +functionality. Different implementations will support different optional +modules. This implementation provides a set of features that should be +useful if you're not depending on the HTML functionality (lots of convenience +functions that mostly don't buy much except API surface area) and user +interface support. That is, browsers will want more -- but what they +need should be cleanly layered over what's already here. </p> + +<h3> Core Feature Set: "XML" </h3> + +<p> This DOM implementation supports the "XML" feature set, which basically +gets you four things over the bare core (which you're officially not supposed +to implement except in conjunction with the "XML" or "HTML" feature). In +order of decreasing utility, those four things are: </p> <ol> + + <li> ProcessingInstruction nodes. These are probably the most + valuable thing. Handy little buggers, in part because all the APIs + you need to use them are provided, and they're designed to let you + escape XML document structure rules in controlled ways.</li> + + <li> CDATASection nodes. These are of of limited utility since CDATA + is just text that prints funny. These are of use to some sorts of + applications, though I encourage folk to not use them. </li> + + <li> DocumentType nodes, and associated Notation and Entity nodes. + These appear to be useless. Briefly, these "Type" nodes expose no + typing information. They're only really usable to expose some lexical + structure that almost every application needs to ignore. (XML editors + might like to see them, but they need true typing information much more.) + I strongly encourage people not to use these. </li> + + <li> EntityReference nodes can show up. These are actively annoying, + since they add an extra level of hierarchy, are the cause of most of + the complexity in attribute values, and their contents are immutable. + Avoid these.</li> + + </ol> + +<h3> Optional Feature Sets: "Events", and friends </h3> + +<p> Events may be one of the more interesting new features in Level 2. +This package provides the core feature set and exposes mutation events. +No gooey events though; if you want that, write a layered implementation! </p> + +<p> Three mutation events aren't currently generated:</p> <ul> + + <li> <em>DOMSubtreeModified</em> is poorly specified. Think of this + as generating one such event around the time of finalization, which + is a fully conformant implementation. This implementation is exactly + as useful as that one. </li> + + <li> <em>DOMNodeRemovedFromDocument</em> and + <em>DOMNodeInsertedIntoDocument</em> are supposed to get sent to + every node in a subtree that gets removed or inserted (respectively). + This can be <em>extremely costly</em>, and the removal and insertion + processing is already significantly slower due to event reporting. + It's much easier, and more efficient, to have a listener higher in the + tree watch removal and insertion events through the bubbling or capture + mechanisms, than it is to watch for these two events.</li> + + </ul> + +<p> In addition, certain kinds of attribute modification aren't reported. +A fix is known, but it couldn't report the previous value of the attribute. +More work could fix all of this (as well as reduce the generally high cost +of childful attributes), but that's not been done yet. </p> + +<p> Also, note that it is a <em>Bad Thing™</em> to have the listener +for a mutation event change the ancestry for the target of that event. +Or to prevent mutation events from bubbling to where they're needed. +Just don't do those, OK? </p> + +<p> As an experimental feature (named "USER-Events"), you can provide +your own "user" events. Just name them anything starting with "USER-" +and you're set. Dispatch them through, bubbling, capturing, or what +ever takes your fancy. One important thing you can't currently do is +pass any data (like an object) with those events. Maybe later there +will be a "UserEvent" interface letting you get some substantial use +out of this mechanism even if you're not "inside" of a DOM package.</p> + +<p> You can create and send HTML events. Ditto UIEvents. Since DOM +doesn't require a UI, it's the UI's job to send them; perhaps that's +part of your application. </p> + +<p><em>This package may be built without the ability to report mutation +events, gaining a significant speedup in DOM construction time. However, +if that is done then certain other features -- notably node iterators +and getElementsByTagname -- will not be available.</em> + + +<h3> Optional Feature: "Traversal" </h3> + +<p> Each DOM node has all you need to walk to everything connected +to that node. Lightweight, efficient utilities are easily layered on +top of just the core APIs. </p> + +<p> Traversal APIs are an optional part of DOM Level 2, providing +a not-so-lightweight way to walk over DOM trees, if your application +didn't already have such utilities for use with data represented via +DOM. Implementing this helped debug the (optional) event and mutation +event subsystems, so it's provided here. </p> + +<p> At this writing, the "TreeWalker" interface isn't implemented. </p> + + + +<h2><a name='avoid'>DOM Functionality to Avoid</a></h2> + +<p> For what appear to be a combination of historical and "committee +logic" reasons, DOM has a number of <em>features which I strongly advise +you to avoid using</em> in your library and application code. These +include the following types of DOM nodes; see the documentation for the +implementation class for more information: <ul> + + <li> CDATASection + (<a href='DomCDATA.html'>DomCDATA</a> class) + ... use normal Text nodes instead, so you don't have to make + every algorithm recognize multiple types of character data + + <li> DocumentType + (<a href='DomDoctype.html'>DomDocType</a> class) + ... if this held actual typing information, it might be useful + + <li> Entity + (<a href='DomEntity.html'>DomEntity</a> class) + ... neither parsed nor unparsed entities work well in DOM; it + won't even tell you which attributes identify unparsed entities + + <li> EntityReference + (<a href='DomEntityReference.html'>DomEntityReference</a> class) + ... permitted implementation variances are extreme, all children + are readonly, and these can interact poorly with namespaces + + <li> Notation + (<a href='DomNotation.html'>DomNotation</a> class) + ... only really usable with unparsed entities (which aren't well + supported; see above) or perhaps with PIs after the DTD, not with + NOTATION attributes + + </ul> + +<p> If you really need to use unparsed entities or notations, use SAX; +it offers better support for all DTD-related functionality. +It also exposes actual +document typing information (such as element content models).</p> + +<p> Also, when accessing attribute values, use methods that provide their +values as single strings, rather than those which expose value substructure +(Text and EntityReference nodes). (See the <a href='DomAttr.html'>DomAttr</a> +documentation for more information.) </p> + +<p> Note that many of these features were provided as partial support for +editor functionality (including the incomplete DTD access). Full editor +functionality requires access to potentially malformed lexical structure, +at the level of unparsed tokens and below. Access at such levels is so +complex that using it in non-editor applications sacrifices all the +benefits of XML; editor aplications need extremely specialized APIs. </p> + +<p> (This isn't a slam against DTDs, note; only against the broken support +for them in DOM. Even despite inclusion of some dubious SGML legacy features +such as notations and unparsed entities, +and the ongoing proliferation of alternative schema and validation tools, +DTDs are still the most widely adopted tool +to constrain XML document structure. +Alternative schemes generally focus on data transfer style +applications; open document architectures comparable to +DocBook 4.0 don't yet exist in the schema world. +Feel free to use DTDs; just don't expect DOM to help you.) </p> + +</body> +</html> + diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java new file mode 100644 index 00000000000..24088fb8154 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java @@ -0,0 +1,116 @@ +/* GnomeAttr.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.TypeInfo; + +/** + * A DOM attribute node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeAttr + extends GnomeNode + implements Attr +{ + + GnomeAttr(Object id) + { + super(id); + } + + public String getName() + { + return getNodeName(); + } + + public native boolean getSpecified(); + + public native String getValue(); + + public native void setValue(String value) + throws DOMException; + + public Node getParentNode() + { + return null; + } + + public Element getOwnerElement() + { + return (Element) super.getParentNode(); + } + + // DOM Level 3 methods + + public TypeInfo getSchemaTypeInfo() + { + return new GnomeTypeInfo(id); + } + + public boolean isId() + { + if (xmljIsId()) + { + return true; + } + GnomeElement element = (GnomeElement) getOwnerElement(); + return (element != null && + element.userIdAttrs != null && + element.userIdAttrs.contains(this)); + } + + private native boolean xmljIsId(); + + public String toString() + { + StringBuffer buffer = new StringBuffer(getClass().getName()); + buffer.append("[name="); + buffer.append(getName()); + buffer.append(",value="); + buffer.append(getValue()); + buffer.append("]"); + return buffer.toString(); + } + +} + diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java new file mode 100644 index 00000000000..8fb89ff4dea --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java @@ -0,0 +1,57 @@ +/* GnomeCDATASection.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.CDATASection; + +/** + * A DOM CDATA section node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeCDATASection + extends GnomeText + implements CDATASection +{ + + GnomeCDATASection(Object id) + { + super(id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java new file mode 100644 index 00000000000..a3200921df6 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java @@ -0,0 +1,117 @@ +/* GnomeCharacterData.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.CharacterData; +import org.w3c.dom.DOMException; + +/** + * A DOM character data node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +abstract class GnomeCharacterData + extends GnomeNode + implements CharacterData +{ + + GnomeCharacterData(Object id) + { + super(id); + } + + public String getData() + throws DOMException + { + return getNodeValue(); + } + + public void setData(String data) + throws DOMException + { + setNodeValue(data); + } + + public int getLength() + { + return getData().length(); + } + + public String substringData(int offset, int count) + throws DOMException + { + return getData().substring(offset, offset + count); + } + + public void appendData(String arg) + throws DOMException + { + setData(getData() + arg); + } + + public void insertData(int offset, String arg) + throws DOMException + { + String data = getData(); + setData(data.substring(0, offset) + arg + data.substring(offset)); + } + + public void deleteData(int offset, int count) + throws DOMException + { + String data = getData(); + setData(data.substring(0, offset) + data.substring(offset + count)); + } + + public void replaceData(int offset, int count, String arg) + { + String data = getData(); + setData(data.substring(0, offset) + arg + + data.substring(offset + count)); + } + + public String toString() + { + StringBuffer buffer = new StringBuffer(getClass().getName()); + buffer.append("[data="); + buffer.append(getData()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java new file mode 100644 index 00000000000..a0cad5b3ea1 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java @@ -0,0 +1,57 @@ +/* GnomeComment.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Comment; + +/** + * A DOM comment node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeComment + extends GnomeCharacterData + implements Comment +{ + + GnomeComment(Object id) + { + super(id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java new file mode 100644 index 00000000000..eae07876f3c --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java @@ -0,0 +1,98 @@ +/* GnomeDOMException.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; + +class GnomeDOMException + extends DOMException +{ + + GnomeDOMException(short code, String message) + { + super(code, createMessage(code, message)); + } + + private static String createMessage(int code, String message) + { + if (message != null) + { + return message; + } + switch (code) + { + case INDEX_SIZE_ERR: + return "INDEX_SIZE_ERR"; + case DOMSTRING_SIZE_ERR: + return "DOMSTRING_SIZE_ERR"; + case HIERARCHY_REQUEST_ERR: + return "HIERARCHY_REQUEST_ERR"; + case WRONG_DOCUMENT_ERR: + return "WRONG_DOCUMENT_ERR"; + case INVALID_CHARACTER_ERR: + return "INVALID_CHARACTER_ERR"; + case NO_DATA_ALLOWED_ERR: + return "NO_DATA_ALLOWED_ERR"; + case NO_MODIFICATION_ALLOWED_ERR: + return "NO_MODIFICATION_ALLOWED_ERR"; + case NOT_FOUND_ERR: + return "NOT_FOUND_ERR"; + case NOT_SUPPORTED_ERR: + return "NOT_SUPPORTED_ERR"; + case INUSE_ATTRIBUTE_ERR: + return "INUSE_ATTRIBUTE_ERR"; + case INVALID_STATE_ERR: + return "INVALID_STATE_ERR"; + case SYNTAX_ERR: + return "SYNTAX_ERR"; + case INVALID_MODIFICATION_ERR: + return "INVALID_MODIFICATION_ERR"; + case NAMESPACE_ERR: + return "NAMESPACE_ERR"; + case INVALID_ACCESS_ERR: + return "INVALID_ACCESS_ERR"; + case VALIDATION_ERR: + return "VALIDATION_ERR"; + case TYPE_MISMATCH_ERR: + return "TYPE_MISMATCH_ERR"; + default: + return null; + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java new file mode 100644 index 00000000000..3456acd993c --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java @@ -0,0 +1,84 @@ +/* GnomeDOMStringList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMStringList; + +/** + * Implementation of a string list using an array of strings. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeDOMStringList + implements DOMStringList +{ + + final String[] values; + + GnomeDOMStringList(String[] values) + { + this.values = values; + } + + public int getLength() + { + return values.length; + } + + public String item(int index) + { + if (index < 0 || index >= values.length) + { + return null; + } + return values[index]; + } + + public boolean contains(String value) + { + for (int i = 0; i < values.length; i++) + { + if (values[i].equalsIgnoreCase(value)) + { + return true; + } + } + return false; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java new file mode 100644 index 00000000000..3706fba84f5 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java @@ -0,0 +1,561 @@ +/* GnomeDocument.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import java.util.Iterator; + +import org.w3c.dom.Attr; +import org.w3c.dom.CDATASection; +import org.w3c.dom.Comment; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMErrorHandler; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.DOMStringList; +import org.w3c.dom.Element; +import org.w3c.dom.EntityReference; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.ProcessingInstruction; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import org.w3c.dom.traversal.DocumentTraversal; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; +import org.w3c.dom.traversal.TreeWalker; +import org.w3c.dom.xpath.XPathEvaluator; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathExpression; +import org.w3c.dom.xpath.XPathNSResolver; + +import gnu.xml.dom.DomNodeIterator; + +/** + * A DOM document node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeDocument + extends GnomeNode + implements Document, DOMConfiguration, XPathEvaluator, DocumentTraversal +{ + + DOMImplementation dom; + + /** + * Not currently used. + */ + boolean strictErrorChecking; + + /* DOMConfiguration */ + boolean canonicalForm = false; + boolean cdataSections = true; + boolean checkCharacterNormalization = false; + boolean comments = true; + boolean datatypeNormalization = false; + boolean elementContentWhitespace = true; + boolean entities = true; + DOMErrorHandler errorHandler; + boolean namespaces = true; + boolean namespaceDeclarations = true; + boolean normalizeCharacters = false; + boolean splitCdataSections = true; + boolean validate = false; + boolean validateIfSchema = false; + boolean wellFormed = true; + + GnomeDocument(Object id) + { + super(id); + strictErrorChecking = true; + } + + protected void finalize() + { + free(id); + } + + private native void free(Object id); + + public native DocumentType getDoctype(); + + public DOMImplementation getImplementation() + { + return dom; + } + + public native Element getDocumentElement(); + + public Element createElement(String tagName) + throws DOMException + { + return createElementNS(null, tagName); + } + + public native DocumentType createDocumentType(String name, String publicId, + String systemId); + + public native DocumentFragment createDocumentFragment(); + + public native Text createTextNode(String data); + + public native Comment createComment(String data); + + public native CDATASection createCDATASection(String data) + throws DOMException; + + public native ProcessingInstruction createProcessingInstruction(String target, + String data) + throws DOMException; + + public Attr createAttribute(String name) + throws DOMException + { + return createAttributeNS(null, name); + } + + public native EntityReference createEntityReference(String name) + throws DOMException; + + public native NodeList getElementsByTagName(String tagName); + + public Node importNode(Node importedNode, boolean deep) + throws DOMException + { + Node ret = xmljImportNode(importedNode, deep); + if (importedNode instanceof GnomeNode) + { + ((GnomeNode) importedNode) + .notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED, + importedNode, ret); + } + return ret; + } + + private native Node xmljImportNode(Node importedNode, boolean deep) + throws DOMException; + + public native Element createElementNS(String namespaceURI, String + qualifiedName) + throws DOMException; + + public native Attr createAttributeNS(String namespaceURI, String + qualifiedName) + throws DOMException; + + public native NodeList getElementsByTagNameNS(String namespaceURI, + String localName); + + public Element getElementById(String elementId) + { + Element element = xmljGetElementById(elementId); + if (element == null) + { + TreeWalker walker = createTreeWalker(this, NodeFilter.SHOW_ELEMENT, + null, false); + for (Node node = walker.nextNode(); node != null; + node = walker.nextNode()) + { + GnomeElement e = (GnomeElement) node; + if (e.userIdAttrs != null) + { + for (Iterator i = e.userIdAttrs.iterator(); i.hasNext(); ) + { + Attr attr = (Attr) i.next(); + if (attr.getNodeValue().equals(elementId)) + { + return e; + } + } + } + } + } + return element; + } + + private native Element xmljGetElementById(String elementId); + + // DOM Level 3 methods + + public native String getInputEncoding(); + + public native String getXmlEncoding(); + + public native boolean getXmlStandalone(); + + public native void setXmlStandalone(boolean xmlStandalone); + + public native String getXmlVersion(); + + public native void setXmlVersion(String xmlVersion); + + public boolean getStrictErrorChecking() + { + return strictErrorChecking; + } + + public void setStrictErrorChecking(boolean strictErrorChecking) + { + this.strictErrorChecking = strictErrorChecking; + } + + public native String getDocumentURI(); + + public native void setDocumentURI(String documentURI); + + public Node adoptNode(Node source) + throws DOMException + { + if (source == null || !(source instanceof GnomeNode)) + { + return null; + } + Node ret = xmljAdoptNode(source); + if (source instanceof GnomeNode) + { + ((GnomeNode) source). + notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, + source, ret); + } + return ret; + } + + private native Node xmljAdoptNode(Node source) + throws DOMException; + + public DOMConfiguration getDomConfig() + { + return this; + } + + public void normalizeDocument() + { + normalize(); + } + + public native Node renameNode(Node n, String namespaceURI, + String qualifiedName); + + // -- DOMConfiguration methods -- + + public void setParameter(String name, Object value) + throws DOMException + { + name = name.toLowerCase(); + if ("canonical-form".equals(name)) + { + /* optional + canonicalForm = getBooleanValue(value);*/ + } + else if ("cdata-sections".equals(name)) + { + cdataSections = getBooleanValue(value); + } + else if ("check-character-normalization".equals(name)) + { + /* optional + checkCharacterNormalization = getBooleanValue(value);*/ + } + else if ("comments".equals(name)) + { + comments = getBooleanValue(value); + } + else if ("datatype-normalization".equals(name)) + { + /* optional + datatypeNormalization = getBooleanValue(value);*/ + } + else if ("element-content-whitespace".equals(name)) + { + /* optional + elementContentWhitespace = getBooleanValue(value);*/ + } + else if ("entities".equals(name)) + { + entities = getBooleanValue(value); + } + else if ("error-handler".equals(name)) + { + errorHandler = (DOMErrorHandler) value; + } + else if ("infoset".equals(name)) + { + if (getBooleanValue(value)) + { + validateIfSchema = false; + entities = false; + datatypeNormalization = false; + cdataSections = false; + namespaceDeclarations = true; + wellFormed = true; + elementContentWhitespace = true; + comments = true; + namespaces = true; + } + } + else if ("namespaces".equals(name)) + { + /* optional + namespaces = getBooleanValue(value);*/ + } + else if ("namespace-declarations".equals(name)) + { + namespaceDeclarations = getBooleanValue(value); + } + else if ("normalize-characters".equals(name)) + { + /* optional + normalizeCharacters = getBooleanValue(value);*/ + } + else if ("split-cdata-sections".equals(name)) + { + splitCdataSections = getBooleanValue(value); + } + else if ("validate".equals(name)) + { + /* optional + validate = getBooleanValue(value);*/ + } + else if ("validate-if-schema".equals(name)) + { + /* optional + validateIfSchema = getBooleanValue(value);*/ + } + else if ("well-formed".equals(name)) + { + /* optional + wellFormed = getBooleanValue(value);*/ + } + else + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, name); + } + } + + public Object getParameter(String name) + throws DOMException + { + name = name.toLowerCase(); + if ("canonical-form".equals(name)) + { + return new Boolean(canonicalForm); + } + else if ("cdata-sections".equals(name)) + { + return new Boolean(cdataSections); + } + else if ("check-character-normalization".equals(name)) + { + return new Boolean(checkCharacterNormalization); + } + else if ("comments".equals(name)) + { + return new Boolean(comments); + } + else if ("datatype-normalization".equals(name)) + { + return new Boolean(datatypeNormalization); + } + else if ("element-content-whitespace".equals(name)) + { + return new Boolean(elementContentWhitespace); + } + else if ("entities".equals(name)) + { + return new Boolean(entities); + } + else if ("error-handler".equals(name)) + { + return errorHandler; + } + else if ("infoset".equals(name)) + { + return new Boolean(!validateIfSchema && + !entities && + !datatypeNormalization && + !cdataSections && + namespaceDeclarations && + wellFormed && + elementContentWhitespace && + comments && + namespaces); + } + else if ("namespaces".equals(name)) + { + return new Boolean(namespaces); + } + else if ("namespace-declarations".equals(name)) + { + return new Boolean(namespaceDeclarations); + } + else if ("normalize-characters".equals(name)) + { + return new Boolean(normalizeCharacters); + } + else if ("split-cdata-sections".equals(name)) + { + return new Boolean(splitCdataSections); + } + else if ("validate".equals(name)) + { + return new Boolean(validate); + } + else if ("validate-if-schema".equals(name)) + { + return new Boolean(validateIfSchema); + } + else if ("well-formed".equals(name)) + { + return new Boolean(wellFormed); + } + else + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, name); + } + } + + public boolean canSetParameter(String name, Object value) + { + name = name.toLowerCase(); + if ("error-handler".equals(name)) + { + return (value == null || value instanceof DOMErrorHandler); + } + return ("cdata-sections".equals(name) || + "comments".equals(name) || + "entities".equals(name) || + "namespace-declarations".equals(name) || + "split-cdata-sections".equals(name)); + } + + public DOMStringList getParameterNames() + { + String[] names = new String[] { + "canonical-form", + "cdata-sections", + "check-character-normalization", + "comments", + "datatype-normalization", + "element-content-whitespace", + "entities", + "error-handler", + "infoset", + "namespaces", + "namespace-declarations", + "normalize-characters", + "split-cdata-sections", + "validate", + "validate-if-schema", + "well-formed" + }; + return new GnomeDOMStringList(names); + } + + private boolean getBooleanValue(Object value) + { + if (value instanceof Boolean) + { + return ((Boolean) value).booleanValue(); + } + else if (value instanceof String) + { + return new Boolean ((String) value).booleanValue(); + } + return false; + } + + // -- XPathEvaluator methods -- + + public XPathExpression createExpression(String expression, + XPathNSResolver resolver) + throws XPathException, DOMException + { + return new GnomeXPathExpression(this, expression, resolver); + } + + public XPathNSResolver createNSResolver(Node nodeResolver) + { + return new GnomeXPathNSResolver(nodeResolver); + } + + public native Object evaluate(String expression, + Node contextNode, + XPathNSResolver resolver, + short type, + Object result) + throws XPathException, DOMException; + + // -- DocumentTraversal methods -- + + public NodeIterator createNodeIterator(Node root, + int whatToShow, + NodeFilter filter, + boolean entityReferenceExpansion) + throws DOMException + { + return new DomNodeIterator(root, whatToShow, filter, + entityReferenceExpansion, false); + } + + public TreeWalker createTreeWalker(Node root, + int whatToShow, + NodeFilter filter, + boolean entityReferenceExpansion) + throws DOMException + { + return new DomNodeIterator(root, whatToShow, filter, + entityReferenceExpansion, true); + } + + // -- Debugging -- + + public String toString() + { + StringBuffer buffer = new StringBuffer(getClass().getName()); + buffer.append("[version="); + buffer.append(getXmlVersion()); + buffer.append(",standalone="); + buffer.append(getXmlStandalone()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java new file mode 100644 index 00000000000..4decee0d823 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java @@ -0,0 +1,326 @@ +/* GnomeDocumentBuilder.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import java.io.InputStream; +import java.io.IOException; +import java.net.URL; +import javax.xml.parsers.DocumentBuilder; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMImplementation; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import gnu.xml.libxmlj.util.NamedInputStream; +import gnu.xml.libxmlj.util.StandaloneDocumentType; +import gnu.xml.libxmlj.util.StandaloneLocator; +import gnu.xml.libxmlj.util.XMLJ; + +/** + * A JAXP DOM implementation that uses Gnome libxml2 as the underlying + * parser and node representation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeDocumentBuilder + extends DocumentBuilder + implements DOMImplementation +{ + + static + { + XMLJ.init(); + } + + // -- DocumentBuilder -- + + private boolean validate; + private boolean coalesce; + private boolean expandEntities; + private EntityResolver entityResolver; + private ErrorHandler errorHandler; + private boolean seenFatalError; + + /** + * Constructs a new validating document builder. + */ + public GnomeDocumentBuilder() + { + this(true, false, false); + } + + /** + * Constructs a new document builder. + * @param validate whether to validate during parsing + * @param coalesce whether to merge CDATA as text nodes + * @param expandEntities whether to expand entity references + */ + public GnomeDocumentBuilder(boolean validate, + boolean coalesce, + boolean expandEntities) + { + this.validate = validate; + this.coalesce = coalesce; + this.expandEntities = expandEntities; + } + + public DOMImplementation getDOMImplementation() + { + return this; + } + + public boolean isNamespaceAware() + { + return true; + } + + public boolean isValidating() + { + return validate; + } + + public Document newDocument() + { + return createDocument(null, null, null); + } + + public Document parse(InputSource input) + throws SAXException, IOException + { + NamedInputStream in = XMLJ.getInputStream(input); + byte[] detectBuffer = in.getDetectBuffer(); + String publicId = input.getPublicId(); + String systemId = input.getSystemId(); + String base = XMLJ.getBaseURI(systemId); + // Handle zero-length document + if (detectBuffer == null) + { + throw new SAXParseException("No document element", publicId, + systemId, 0, 0); + } + seenFatalError = false; + return parseStream(in, + detectBuffer, + publicId, + systemId, + base, + validate, + coalesce, + expandEntities, + true, //entityResolver != null, + errorHandler != null); + } + + private native Document parseStream(InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean validate, + boolean coalesce, + boolean expandEntities, + boolean entityResolver, + boolean errorHandler); + + public void setEntityResolver(EntityResolver resolver) + { + entityResolver = resolver; + } + + public void setErrorHandler(ErrorHandler handler) + { + errorHandler = handler; + } + + // -- DOMImplementation -- + + public boolean hasFeature(String name, String version) + { + if (name.length() == 0) + { + return false; + } + name = name.toLowerCase(); + if (name.charAt(0) == '+') + { + name = name.substring(1); + } + + if ("xml".equals(name) || "core".equals(name)) + { + return (version == null || + "".equals(version) || + "1.0".equals(version) || + "2.0".equals(version) || + "3.0".equals(version)); + + } + else if ("ls".equals(name) || "ls-async".equals(name)) + { + // TODO + /* + return (version == null || + "".equals(version) || + "3.0".equals(version)); + */ + return false; + } + else if ("traversal".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + } + else if ("xpath".equals(name)) + { + return (version == null || + "".equals(version) || + "3.0".equals(version)); + } + return false; + } + + // DOM Level 3 + + public Object getFeature(String feature, String version) + { + if (hasFeature(feature, version)) + { + return this; + } + return null; + } + + public native Document createDocument(String namespaceURI, + String qualifiedName, + DocumentType doctype); + + public DocumentType createDocumentType(String qualifiedName, + String publicId, + String systemId) + { + return new StandaloneDocumentType(qualifiedName, publicId, systemId); + } + + // Callback hooks from JNI + + private void setDocumentLocator(Object ctx, Object loc) + { + // ignore + } + + private InputStream resolveEntity(String publicId, String systemId, + String base) + throws SAXException, IOException + { + String url = XMLJ.getAbsoluteURI(base, systemId); + InputStream in = null; + if (entityResolver != null) + { + InputSource source = entityResolver.resolveEntity(publicId, url); + if (source != null) + { + in = XMLJ.getInputStream(source); + } + } + if (in == null) + { + in = XMLJ.getInputStream(new URL(url)); + } + return in; + } + + private void warning(String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws SAXException + { + if (!seenFatalError && errorHandler != null) + { + Locator l = new StandaloneLocator(lineNumber, + columnNumber, + publicId, + systemId); + errorHandler.warning(new SAXParseException(message, l)); + } + } + + private void error(String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws SAXException + { + if (!seenFatalError && errorHandler != null) + { + Locator l = new StandaloneLocator(lineNumber, + columnNumber, + publicId, + systemId); + errorHandler.error(new SAXParseException(message, l)); + } + } + + private void fatalError(String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws SAXException + { + if (!seenFatalError && errorHandler != null) + { + seenFatalError = true; + Locator l = new StandaloneLocator(lineNumber, + columnNumber, + publicId, + systemId); + errorHandler.fatalError(new SAXParseException(message, l)); + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java new file mode 100644 index 00000000000..c4f0ce20158 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java @@ -0,0 +1,94 @@ +/* GnomeDocumentBuilderFactory.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +/** + * Factory for JAXP document builders using the libxml2 implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeDocumentBuilderFactory +extends DocumentBuilderFactory +{ + + public GnomeDocumentBuilderFactory () + { + setNamespaceAware (true); + } + + public Object getAttribute (String name) + { + // TODO + return null; + } + + public DocumentBuilder newDocumentBuilder () + throws ParserConfigurationException + { + /* + if (!isNamespaceAware ()) + { + String msg = "Parser must be namespace-aware"; + throw new ParserConfigurationException (msg); + } + if (isIgnoringComments ()) + { + String msg = "Ignoring comments not supported"; + throw new ParserConfigurationException (msg); + } + if (isIgnoringElementContentWhitespace ()) + { + String msg = "Ignoring element content whitespace not supported"; + throw new ParserConfigurationException (msg); + } + */ + return new GnomeDocumentBuilder (isValidating (), + isCoalescing (), + isExpandEntityReferences ()); + } + + public void setAttribute (String name, Object value) + { + // TODO + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java new file mode 100644 index 00000000000..e5b355e428d --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java @@ -0,0 +1,57 @@ +/* GnomeDocumentFragment.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DocumentFragment; + +/** + * A DOM document fragment node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeDocumentFragment +extends GnomeNode +implements DocumentFragment +{ + + GnomeDocumentFragment (Object id) + { + super (id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java new file mode 100644 index 00000000000..257fbbceb69 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java @@ -0,0 +1,96 @@ +/* GnomeDocumentType.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DocumentType; +import org.w3c.dom.NamedNodeMap; + +/** + * A DOM document type node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeDocumentType +extends GnomeNode +implements DocumentType +{ + + GnomeDocumentType (Object id) + { + super (id); + } + + public String getName () + { + return getNodeName (); + } + + public NamedNodeMap getEntities () + { + return new GnomeNamedNodeMap (id, 1); + } + + public NamedNodeMap getNotations () + { + return new GnomeNamedNodeMap (id, 2); + } + + public native String getPublicId (); + + public native String getSystemId (); + + public native String getInternalSubset (); + + public String toString () + { + String publicId = getPublicId (); + StringBuffer buffer = new StringBuffer (getClass ().getName ()); + buffer.append ("["); + if (publicId != null) + { + buffer.append ("publicId="); + buffer.append (publicId); + buffer.append (","); + } + buffer.append ("systemId="); + buffer.append (getSystemId ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java new file mode 100644 index 00000000000..2770cbbfd1e --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java @@ -0,0 +1,182 @@ +/* GnomeElement.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import java.util.HashSet; +import java.util.Set; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.w3c.dom.TypeInfo; + +/** + * A DOM element node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeElement + extends GnomeNode + implements Element +{ + + /** + * User-defined ID attributes. + */ + Set userIdAttrs; + + GnomeElement(Object id) + { + super(id); + } + + public String getTagName() + { + return getNodeName(); + } + + public native String getAttribute(String name); + + public native void setAttribute(String name, String value) + throws DOMException; + + public void removeAttribute(String name) + throws DOMException + { + Attr attr = getAttributeNode(name); + if (attr != null) + { + removeAttributeNode(attr); + } + } + + public native Attr getAttributeNode(String name); + + public native Attr setAttributeNode(Attr newAttr) + throws DOMException; + + public native Attr removeAttributeNode(Attr oldAttr) + throws DOMException; + + public native NodeList getElementsByTagName(String name); + + public native String getAttributeNS(String namespaceURI, String localName); + + public native void setAttributeNS(String namespaceURI, String + qualifiedName, String value) + throws DOMException; + + public void removeAttributeNS(String namespaceURI, String localName) + throws DOMException + { + Attr attr = getAttributeNodeNS(namespaceURI, localName); + if (attr != null) + { + removeAttributeNode(attr); + } + } + + public native Attr getAttributeNodeNS(String namespaceURI, + String localName); + + public native Attr setAttributeNodeNS(Attr newAttr) + throws DOMException; + + public native NodeList getElementsByTagNameNS(String namespaceURI, + String localName); + + public native boolean hasAttribute(String name); + + public native boolean hasAttributeNS(String namespaceURI, + String localName); + + // DOM Level 3 methods + + public TypeInfo getSchemaTypeInfo() + { + return new GnomeTypeInfo(id); + } + + public void setIdAttribute(String name, boolean isId) + { + Attr attr = getAttributeNode(name); + setIdAttributeNode(attr, isId); + } + + public void setIdAttributeNode(Attr attr, boolean isId) + { + if (attr == null)// FIXME || !attr.getOwnerElement().equals(this)) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + if (isId) + { + if (userIdAttrs == null) + { + userIdAttrs = new HashSet(); + } + userIdAttrs.add(attr); + } + else if (userIdAttrs != null) + { + userIdAttrs.remove(attr); + if (userIdAttrs.isEmpty()) + { + userIdAttrs = null; + } + } + } + + public void setIdAttributeNS(String namespaceURI, String localName, + boolean isId) + { + Attr attr = getAttributeNodeNS(namespaceURI, localName); + setIdAttributeNode(attr, isId); + } + + public String toString() + { + StringBuffer buffer = new StringBuffer(getClass().getName()); + buffer.append("[tagName="); + buffer.append(getTagName()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java new file mode 100644 index 00000000000..76367f8bd4f --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java @@ -0,0 +1,102 @@ +/* GnomeEntity.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Entity; + +/** + * A DOM entity node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeEntity +extends GnomeNode +implements Entity +{ + + GnomeEntity (Object id) + { + super (id); + } + + public native String getPublicId (); + + public native String getSystemId (); + + public native String getNotationName (); + + // DOM Level 3 methods + + public String getInputEncoding () + { + // TODO + return null; + } + + public String getXmlEncoding () + { + // TODO + return null; + } + + public String getXmlVersion () + { + // TODO + return null; + } + + public String toString () + { + String publicId = getPublicId (); + StringBuffer buffer = new StringBuffer (getClass ().getName ()); + buffer.append ("["); + if (publicId != null) + { + buffer.append ("publicId="); + buffer.append (publicId); + buffer.append (","); + } + buffer.append ("systemId="); + buffer.append (getSystemId ()); + buffer.append (",notationName="); + buffer.append (getNotationName ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java new file mode 100644 index 00000000000..136b3a69dae --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java @@ -0,0 +1,57 @@ +/* GnomeEntityReference.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.EntityReference; + +/** + * A DOM entity reference node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeEntityReference +extends GnomeNode +implements EntityReference +{ + + GnomeEntityReference (Object id) + { + super (id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java new file mode 100644 index 00000000000..8ca892128c0 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java @@ -0,0 +1,92 @@ +/* GnomeNamedNodeMap.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * A DOM named node map implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeNamedNodeMap +implements NamedNodeMap +{ + + /** + * The node id. + */ + private final Object id; + + /** + * The map type. + * 0=attributes + * 1=entities + * 2=notations + */ + private final int type; + + GnomeNamedNodeMap (Object id, int type) + { + this.id = id; + this.type = type; + } + + public native Node getNamedItem (String name); + + public native Node setNamedItem (Node arg) + throws DOMException; + + public native Node removeNamedItem (String name) + throws DOMException; + + public native Node item (int index); + + public native int getLength (); + + public native Node getNamedItemNS (String namespaceURI, String localName); + + public native Node setNamedItemNS (Node arg) + throws DOMException; + + public native Node removeNamedItemNS (String namespaceURI, + String localName); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java new file mode 100644 index 00000000000..4a2e9ff48d6 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java @@ -0,0 +1,499 @@ +/* GnomeNode.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; + +import gnu.xml.libxmlj.util.StandaloneDocumentType; + +/** + * A DOM node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeNode + implements Node, Comparable +{ + + /** + * Maps document pointers to a map of node pointers to node instances. + */ + static Map instances; + + /** + * Retrieves the node instance for the specified node pointer. + * This creates a new instance and adds it to the cache if required. + * @param doc the document pointer + * @param node the node pointer + * @param type the node type + */ + static GnomeNode newInstance(final Object doc, final Object node, + final int type) + { + if (doc == null) + { + throw new NullPointerException("doc"); + } + if (node == null) + { + throw new NullPointerException("node"); + } + if (instances == null) + { + instances = new HashMap(); + } + Map docNodes = (Map) instances.get(doc); + if (docNodes == null) + { + docNodes = new HashMap(1024); // TODO review optimal initial capacity + instances.put(doc, docNodes); + } + GnomeNode nodeInstance = (GnomeNode) docNodes.get(node); + if (nodeInstance != null) + { + return nodeInstance; // Return cached version + } + switch (type) + { + case ELEMENT_NODE: + nodeInstance = new GnomeElement(node); + break; + case ATTRIBUTE_NODE: + nodeInstance = new GnomeAttr(node); + break; + case TEXT_NODE: + nodeInstance = new GnomeText(node); + break; + case CDATA_SECTION_NODE: + nodeInstance = new GnomeCDATASection(node); + break; + case ENTITY_REFERENCE_NODE: + nodeInstance = new GnomeEntityReference(node); + break; + case ENTITY_NODE: + nodeInstance = new GnomeEntity(node); + break; + case PROCESSING_INSTRUCTION_NODE: + nodeInstance = new GnomeProcessingInstruction(node); + break; + case COMMENT_NODE: + nodeInstance = new GnomeComment(node); + break; + case DOCUMENT_NODE: + nodeInstance = new GnomeDocument(node); + break; + case DOCUMENT_TYPE_NODE: + nodeInstance = new GnomeDocumentType(node); + break; + case DOCUMENT_FRAGMENT_NODE: + nodeInstance = new GnomeDocumentFragment(node); + break; + case NOTATION_NODE: + nodeInstance = new GnomeNotation(node); + break; + default: + throw new IllegalArgumentException("Unknown node type: " + type); + } + docNodes.put(node, nodeInstance); + return nodeInstance; + } + + /** + * Frees the specified document. + * This removes all its nodes from the cache. + */ + static void freeDocument(final Object doc) + { + if (instances == null || doc == null) + { + return; + } + instances.remove(doc); + //System.out.println("Freed "+instances.remove(doc)); + } + + /** + * xmlNodePtr + */ + final Object id; + + Map userData; + Map userDataHandlers; + + GnomeNode(final Object id) + { + this.id = id; + } + + public native String getNodeName(); + + public native String getNodeValue() + throws DOMException; + + public native void setNodeValue(String nodeValue) + throws DOMException; + + public native short getNodeType(); + + public native Node getParentNode(); + + public NodeList getChildNodes() + { + return new GnomeNodeList(id); + } + + public native Node getFirstChild(); + + public native Node getLastChild(); + + public native Node getPreviousSibling(); + + public native Node getNextSibling(); + + public NamedNodeMap getAttributes() + { + return new GnomeNamedNodeMap(id, 0); + } + + public native Document getOwnerDocument(); + + public Node insertBefore(Node newChild, Node refChild) + throws DOMException + { + if (newChild instanceof StandaloneDocumentType) + { + DocumentType dt = (DocumentType) newChild; + newChild = ((GnomeDocument) getOwnerDocument()) + .createDocumentType(dt.getName(), dt.getPublicId(), + dt.getSystemId()); + } + if (newChild == null) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + if (!(newChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null); + } + if (refChild == null || !(refChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + return xmljInsertBefore(newChild, refChild); + } + + private native Node xmljInsertBefore(Node newChild, Node refChild) + throws DOMException; + + public Node replaceChild(Node newChild, Node oldChild) + throws DOMException + { + if (newChild instanceof StandaloneDocumentType) + { + DocumentType dt = (DocumentType) newChild; + newChild = ((GnomeDocument) getOwnerDocument()) + .createDocumentType(dt.getName(), dt.getPublicId(), + dt.getSystemId()); + } + if (newChild == null) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + if (!(newChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, newChild.toString()); + } + if (oldChild == null || !(oldChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + return xmljReplaceChild(newChild, oldChild); + } + + private native Node xmljReplaceChild(Node newChild, Node oldChild) + throws DOMException; + + public Node removeChild(Node oldChild) + throws DOMException + { + if (!(oldChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null); + } + return xmljRemoveChild(oldChild); + } + + private native Node xmljRemoveChild(Node oldChild) + throws DOMException; + + public Node appendChild(Node newChild) + throws DOMException + { + if (newChild instanceof StandaloneDocumentType) + { + DocumentType dt = (DocumentType) newChild; + newChild = ((GnomeDocument) getOwnerDocument()) + .createDocumentType(dt.getName(), dt.getPublicId(), + dt.getSystemId()); + } + if (!(newChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null); + } + return xmljAppendChild(newChild); + } + + private native Node xmljAppendChild(Node newChild) + throws DOMException; + + public native boolean hasChildNodes(); + + public Node cloneNode(boolean deep) + { + Node ret = xmljCloneNode(deep); + notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, ret); + return ret; + } + + private native Node xmljCloneNode(boolean deep); + + public native void normalize(); + + public boolean isSupported(String feature, String version) + { + return getOwnerDocument().getImplementation() + .hasFeature(feature, version); + } + + public native String getNamespaceURI(); + + public native String getPrefix(); + + public native void setPrefix(String prefix) + throws DOMException; + + public native String getLocalName(); + + public native boolean hasAttributes(); + + public int hashCode() + { + return id.hashCode(); + } + + public boolean equals(Object other) + { + if (other == this) + { + return true; + } + return (other instanceof GnomeNode && + ((GnomeNode) other).id == id); + } + + // DOM Level 3 methods + + public native String getBaseURI(); + + public short compareDocumentPosition(Node other) + throws DOMException + { + return (short) compareTo(other); + } + + public final int compareTo(Object other) + { + if (other instanceof GnomeNode) + { + return xmljCompareTo(other); + } + return 0; + } + + private native int xmljCompareTo(Object other); + + public String getTextContent() + throws DOMException + { + switch (getNodeType()) + { + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_NODE: + case ENTITY_REFERENCE_NODE: + case DOCUMENT_FRAGMENT_NODE: + StringBuffer buffer = new StringBuffer(); + NodeList children = getChildNodes(); + int len = children.getLength(); + for (int i = 0; i < len; i++) + { + Node child = children.item(i); + String textContent = child.getTextContent(); + if (textContent != null) + { + buffer.append(textContent); + } + } + return buffer.toString(); + case TEXT_NODE: + case CDATA_SECTION_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + return getNodeValue(); + default: + return null; + } + } + + public void setTextContent(String textContent) + throws DOMException + { + switch (getNodeType()) + { + case ENTITY_REFERENCE_NODE: + // entity references are read only + throw new GnomeDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, + null); + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_NODE: + case DOCUMENT_FRAGMENT_NODE: + NodeList children = getChildNodes(); + int len = children.getLength(); + for (int i = 0; i < len; i++) + { + Node child = children.item(i); + removeChild(child); + } + if (textContent != null) + { + Text text = getOwnerDocument().createTextNode(textContent); + appendChild(text); + } + break; + case TEXT_NODE: + case CDATA_SECTION_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + setNodeValue(textContent); + break; + } + } + + public boolean isSameNode(Node other) + { + return equals(other); + } + + public native String lookupPrefix(String namespaceURI); + + public native boolean isDefaultNamespace(String namespaceURI); + + public native String lookupNamespaceURI(String prefix); + + public native boolean isEqualNode(Node arg); + + public Object getFeature(String feature, String version) + { + return getOwnerDocument().getImplementation() + .getFeature(feature, version); + } + + public Object setUserData(String key, Object data, UserDataHandler handler) + { + // TODO handler + if (userData == null) + { + userData = new HashMap(); + } + if (handler != null) + { + if (userDataHandlers == null) + { + userDataHandlers = new HashMap(); + } + userDataHandlers.put(key, handler); + } + return userData.put(key, data); + } + + public Object getUserData(String key) + { + if (userData == null) + { + return null; + } + return userData.get(key); + } + + void notifyUserDataHandlers(short op, Node src, Node dst) + { + if (userDataHandlers != null) + { + for (Iterator i = userDataHandlers.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + String key = (String) entry.getKey(); + UserDataHandler handler = (UserDataHandler) entry.getValue(); + Object data = userData.get(key); + handler.handle(op, key, data, src, dst); + } + } + } + + public String toString() + { + StringBuffer buffer = new StringBuffer(getClass().getName()); + buffer.append("[nodeName="); + buffer.append(getNodeName()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java new file mode 100644 index 00000000000..efd07acb0b0 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java @@ -0,0 +1,66 @@ +/* GnomeNodeList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A DOM node list implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeNodeList +implements NodeList +{ + + /** + * The node id. + */ + private final Object id; + + GnomeNodeList (Object id) + { + this.id = id; + } + + public native Node item (int index); + + public native int getLength (); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java new file mode 100644 index 00000000000..ca1560e21bc --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java @@ -0,0 +1,78 @@ +/* GnomeNotation.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Notation; + +/** + * A DOM notation node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeNotation +extends GnomeNode +implements Notation +{ + + GnomeNotation (Object id) + { + super (id); + } + + public native String getPublicId (); + + public native String getSystemId (); + + public String toString () + { + String publicId = getPublicId (); + StringBuffer buffer = new StringBuffer (getClass ().getName ()); + buffer.append ("["); + if (publicId != null) + { + buffer.append ("publicId="); + buffer.append (publicId); + buffer.append (","); + } + buffer.append ("systemId="); + buffer.append (getSystemId ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java new file mode 100644 index 00000000000..a6c47b2e1ca --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java @@ -0,0 +1,77 @@ +/* GnomeProcessingInstruction.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.ProcessingInstruction; + +/** + * A DOM processing instruction node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeProcessingInstruction +extends GnomeNode +implements ProcessingInstruction +{ + + GnomeProcessingInstruction (Object id) + { + super (id); + } + + public String getTarget () + { + return getNodeName (); + } + + public native String getData (); + + public native void setData (String data) + throws DOMException; + + public String toString () + { + StringBuffer buffer = new StringBuffer (getClass ().getName ()); + buffer.append ("[data="); + buffer.append (getData ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java new file mode 100644 index 00000000000..77677a56223 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java @@ -0,0 +1,130 @@ +/* GnomeText.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.Text; + +/** + * A DOM text node implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeText +extends GnomeCharacterData +implements Text +{ + + GnomeText (Object id) + { + super (id); + } + + public Text splitText (int offset) + throws DOMException + { + String value = getNodeValue (); + String part1 = value.substring (0, offset); + String part2 = value.substring (offset); + Text text = getOwnerDocument ().createTextNode (part1); + getParentNode ().insertBefore (text, this); + setNodeValue (part2); + return text; + } + + // DOM Level 3 + + public boolean isElementContentWhitespace () + { + return getTextContent ().trim ().length () == 0; + } + + public String getWholeText () + { + Node first = this; + Node node = getPreviousSibling (); + while (node != null && node instanceof Text) + { + first = node; + node = node.getPreviousSibling (); + } + StringBuffer buf = new StringBuffer (first.getNodeValue ()); + node = first.getNextSibling (); + while (node != null && node instanceof Text) + { + buf.append (node.getNodeValue ()); + node = node.getNextSibling (); + } + return buf.toString (); + } + + public Text replaceWholeText (String content) throws DOMException + { + boolean isEmpty = (content == null || content.length () == 0); + if (!isEmpty) + { + setNodeValue (content); + } + + Node first = this; + Node node = getPreviousSibling (); + while (node != null && node instanceof Text) + { + first = node; + node = node.getPreviousSibling (); + } + node = first.getNextSibling (); + Node parent = getParentNode (); + if (first != this || isEmpty) + { + parent.removeChild (first); + } + while (node != null && node instanceof Text) + { + Node tmp = node; + node = node.getNextSibling (); + if (tmp != this || isEmpty) + { + parent.removeChild (tmp); + } + } + return (isEmpty) ? null : this; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java new file mode 100644 index 00000000000..d365f0d9fc6 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java @@ -0,0 +1,65 @@ +/* GnomeTypeInfo.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.TypeInfo; + +/** + * Provides XML Schema information about an element or attribute. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeTypeInfo implements TypeInfo +{ + + final Object id; + + GnomeTypeInfo(Object id) + { + this.id = id; + } + + public native String getTypeName (); + + public native String getTypeNamespace (); + + public native boolean isDerivedFrom (String typeNamespaceArg, + String typeNameArg, + int derivationMethod); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java new file mode 100644 index 00000000000..815804799d3 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java @@ -0,0 +1,86 @@ +/* GnomeXPathExpression.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathExpression; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * A compiled XPath expression implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeXPathExpression +implements XPathExpression +{ + + /** + * xmlXPathCompExprPtr + */ + final Object expr; + + GnomeXPathExpression (GnomeDocument doc, String expression, + XPathNSResolver resolver) + { + expr = init (expression); + // TODO resolver + } + + protected void finalize () + { + free (expr); + } + + private native Object init (String expression); + + private native void free (Object expr); + + public Object evaluate (Node contextNode, short type, Object result) + throws XPathException, DOMException + { + return doEvaluate (expr, contextNode, type, result); + } + + private native Object doEvaluate (Object expr, Node contextNode, + short type, Object result) + throws XPathException, DOMException; + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java new file mode 100644 index 00000000000..a9ba85509a4 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java @@ -0,0 +1,65 @@ +/* GnomeXPathNSResolver.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * XPath namespace URI resolver implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeXPathNSResolver + implements XPathNSResolver +{ + + Node node; + + GnomeXPathNSResolver(Node node) + { + this.node = node; + } + + public String lookupNamespaceURI(String prefix) + { + return node.lookupNamespaceURI(prefix); + } + +} + diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java new file mode 100644 index 00000000000..dde3be2d903 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java @@ -0,0 +1,73 @@ +/* GnomeXPathNodeList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A node list that uses an XPath result object. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeXPathNodeList +implements NodeList +{ + + /** + * xmlXPathObjectPtr + */ + final Object obj; + + GnomeXPathNodeList (Object obj) + { + this.obj = obj; + } + + protected void finalize () + { + free(obj); + } + + private native void free (Object obj); + + public native int getLength (); + + public native Node item (int index); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java new file mode 100644 index 00000000000..b384e4ac87b --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java @@ -0,0 +1,132 @@ +/* GnomeXPathResult.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathResult; + +/** + * An XPath result object implemented in libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeXPathResult +implements XPathResult +{ + + /** + * xmlXPathObjectPtr + */ + final Object obj; + + GnomeXPathResult (Object obj) + { + this.obj = obj; + } + + protected void finalize () + { + free (obj); + } + + private native void free (Object obj); + + public native short getResultType (); + + public native double getNumberValue () + throws XPathException; + + public native String getStringValue () + throws XPathException; + + public native boolean getBooleanValue () + throws XPathException; + + public native Node getSingleNodeValue () + throws XPathException; + + public native boolean getInvalidIteratorState(); + + public native int getSnapshotLength () + throws XPathException; + + public native Node iterateNext () + throws XPathException, DOMException; + + public native Node snapshotItem (int index) + throws XPathException; + + public String toString () + { + short type = getResultType (); + switch (type) + { + case STRING_TYPE: + return getStringValue (); + case NUMBER_TYPE: + return new Double (getNumberValue ()).toString (); + case BOOLEAN_TYPE: + return Boolean.valueOf (getBooleanValue ()).toString (); + case UNORDERED_NODE_SNAPSHOT_TYPE: + int len = getSnapshotLength (); + switch (len) { + case 0: + return "[no matches]"; + case 1: + return getSingleNodeValue ().toString (); + default: + StringBuffer buffer = new StringBuffer (); + for (int i = 0; i < len; i++) + { + if (i > 0) + { + buffer.append (','); + } + buffer.append (snapshotItem (i)); + } + return buffer.toString (); + } + default: + return getClass ().getName () + "[type=" + type + ",length=" + + getSnapshotLength () + ']'; + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java new file mode 100644 index 00000000000..0c812f683d8 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java @@ -0,0 +1,99 @@ +/* GnomeLocator.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import org.xml.sax.Locator; + +/** + * SAX Locator implementation that uses libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeLocator +implements Locator +{ + + // An xmlParserCtxtPtr + private final Object ctx; + + // An xmlSAXLocatorPtr + private final Object loc; + + GnomeLocator (Object ctx, Object loc) + { + this.ctx = ctx; + this.loc = loc; + if (ctx == null) + { + throw new NullPointerException ("ctx"); + } + if (loc == null) + { + throw new NullPointerException ("loc"); + } + } + + public String getPublicId () + { + return publicId (ctx, loc); + } + + private native String publicId (Object ctx, Object loc); + + public String getSystemId () + { + return systemId (ctx, loc); + } + + private native String systemId (Object ctx, Object loc); + + public int getLineNumber () + { + return lineNumber (ctx, loc); + } + + private native int lineNumber (Object ctx, Object loc); + + public int getColumnNumber () + { + return columnNumber (ctx, loc); + } + + private native int columnNumber (Object ctx, Object loc); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java new file mode 100644 index 00000000000..a5cdc42b74e --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java @@ -0,0 +1,105 @@ +/* GnomeSAXParser.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import javax.xml.parsers.SAXParser; + +import org.xml.sax.Parser; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; + +/** + * JAXP SAX parser implementation that uses libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class GnomeSAXParser +extends SAXParser +{ + + private boolean namespaceAware; + private boolean validating; + + /** + * Creates a new SAX parser. + */ + GnomeSAXParser (boolean namespaceAware, boolean validating) + { + this.namespaceAware = namespaceAware; + this.validating = validating; + } + + public Parser getParser () + throws SAXException + { + throw new SAXNotSupportedException ("SAX version 1 not supported"); + } + + public XMLReader getXMLReader () + throws SAXException + { + return new GnomeXMLReader (namespaceAware, validating); + } + + public Object getProperty (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkPropertyName (name); + throw new SAXNotSupportedException (name); + } + + public void setProperty (String name, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkPropertyName (name); + throw new SAXNotSupportedException (name); + } + + public boolean isNamespaceAware () + { + return namespaceAware; + } + + public boolean isValidating () + { + return validating; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java new file mode 100644 index 00000000000..5152d540f09 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java @@ -0,0 +1,92 @@ +/* GnomeSAXParserFactory.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import java.util.Map; +import java.util.HashMap; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; + +/** + * JAXP SAX parser factory implementation that uses libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeSAXParserFactory +extends SAXParserFactory +{ + + private Map features; + + /** + * Creates a new SAX parser factory. + */ + public GnomeSAXParserFactory () + { + features = new HashMap (); + } + + public SAXParser newSAXParser () + throws ParserConfigurationException, SAXException + { + // TODO features + return new GnomeSAXParser (isNamespaceAware (), isValidating ()); + } + + public boolean getFeature (String name) + throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkFeatureName (name); + Boolean val = (Boolean) features.get (name); + return (val == null) ? false : val.booleanValue (); + } + + public void setFeature (String name, boolean flag) + throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkFeatureName (name); + features.put (name, flag ? Boolean.TRUE : Boolean.FALSE); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java new file mode 100644 index 00000000000..846015a1c31 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java @@ -0,0 +1,1065 @@ +/* GnomeXMLReader.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; + +import gnu.xml.libxmlj.util.NamedInputStream; +import gnu.xml.libxmlj.util.StandaloneLocator; +import gnu.xml.libxmlj.util.XMLJ; + +/** + * A SAX2 parser that uses libxml2. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeXMLReader +implements XMLReader +{ + + static + { + XMLJ.init (); + } + + private static final String FEATURES_PREFIX = + "http://xml.org/sax/features/"; + + private static final List RECOGNIZED_FEATURES = + Arrays.asList (new String[] + { + "external-general-entities", + "external-parameter-entities", + "is-standalone", + "lexical-handler/parameter-entities", + "namespaces", + "namespace-prefixes", + "resolve-dtd-uris", + "string-interning", + "use-attributes2", + "use-locator2", + "use-entity-resolver2", + "validation" + }); + + private static final String PROPERTIES_PREFIX = + "http://xml.org/sax/properties/"; + + private static final List RECOGNIZED_PROPERTIES = + Arrays.asList (new String[] + { + "declaration-handler", + "dom-node", + "lexical-handler", + "xml-string" + }); + + // Features + + private transient boolean standalone; + private boolean namespaces; + private boolean namespacePrefixes; + private boolean validation; + + // Callback handlers + + private ContentHandler contentHandler; + + private DTDHandler dtdHandler; + + private EntityResolver entityResolver; + + private ErrorHandler errorHandler; + + private DeclHandler declarationHandler; + + private LexicalHandler lexicalHandler; + + private GnomeLocator locator; + + // Namespace helper for handling callbacks + private transient Namespaces ns; + + // If true, do not invoke callback methods except endDocument + private transient boolean seenFatalError; + + private transient boolean seenStartDocument; + + private transient String base; + + public GnomeXMLReader () + { + this (true, true); + } + + public GnomeXMLReader (boolean namespaces, boolean validation) + { + this.namespaces = namespaces; + this.validation = validation; + ns = new Namespaces (); + } + + public ContentHandler getContentHandler () + { + return contentHandler; + } + + public void setContentHandler (ContentHandler handler) + { + contentHandler = handler; + } + + public DTDHandler getDTDHandler () + { + return dtdHandler; + } + + public void setDTDHandler (DTDHandler handler) + { + dtdHandler = handler; + } + + public EntityResolver getEntityResolver () + { + return entityResolver; + } + + public void setEntityResolver (EntityResolver resolver) + { + entityResolver = resolver; + } + + public ErrorHandler getErrorHandler () + { + return errorHandler; + } + + public void setErrorHandler (ErrorHandler handler) + { + errorHandler = handler; + } + + // Features + + public boolean getFeature (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkFeatureName (name); + String key = name.substring (FEATURES_PREFIX.length ()); + if ("external-general-entities".equals (key)) + { + return validation; // TODO check this + } + else if ("external-parameter-entities".equals (key)) + { + return validation; // TODO check this + } + else if ("is-standalone".equals (key)) + { + return standalone; + } + else if ("namespaces".equals (key)) + { + return namespaces; + } + else if ("namespace-prefixes".equals (key)) + { + return namespacePrefixes; + } + else if ("resolve-dtd-uris".equals (key)) + { + return true; + } + else if ("validation".equals (key)) + { + return validation; + } + else + { + return false; + } + } + + public void setFeature (String name, boolean value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkFeatureName (name); + String key = name.substring (FEATURES_PREFIX.length ()); + if ("namespaces".equals (key)) + { + namespaces = value; + } + else if ("namespace-prefixes".equals (key)) + { + namespacePrefixes = value; + } + else if ("validation".equals (key)) + { + validation = value; + } + } + + /** + * Check that the specified feature name is recognized. + */ + static void checkFeatureName (String name) + throws SAXNotRecognizedException + { + if (name == null || !name.startsWith (FEATURES_PREFIX)) + { + throw new SAXNotRecognizedException (name); + } + String key = name.substring (FEATURES_PREFIX.length ()); + if (!RECOGNIZED_FEATURES.contains (key)) + { + throw new SAXNotRecognizedException (name); + } + } + + // Properties + + public Object getProperty (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkPropertyName (name); + String key = name.substring (PROPERTIES_PREFIX.length ()); + if ("declaration-handler".equals (key)) + { + return getDeclarationHandler (); + } + else if ("lexical-handler".equals (key)) + { + return getLexicalHandler (); + } + else + { + throw new SAXNotSupportedException (name); + } + } + + public void setProperty (String name, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkPropertyName (name); + String key = name.substring (PROPERTIES_PREFIX.length ()); + if ("declaration-handler".equals (key)) + { + setDeclarationHandler ((DeclHandler) value); + } + else if ("lexical-handler".equals (key)) + { + setLexicalHandler ((LexicalHandler) value); + } + } + + public DeclHandler getDeclarationHandler () + { + return declarationHandler; + } + + public void setDeclarationHandler (DeclHandler declarationHandler) + { + this.declarationHandler = declarationHandler; + } + + public LexicalHandler getLexicalHandler () + { + return lexicalHandler; + } + + public void setLexicalHandler (LexicalHandler lexicalHandler) + { + this.lexicalHandler = lexicalHandler; + } + + /** + * Check that the specified property name is recognized. + */ + static void checkPropertyName (String name) + throws SAXNotRecognizedException + { + if (!name.startsWith (PROPERTIES_PREFIX)) + { + throw new SAXNotRecognizedException (name); + } + String key = name.substring (PROPERTIES_PREFIX.length ()); + if (!RECOGNIZED_PROPERTIES.contains (key)) + { + throw new SAXNotRecognizedException (name); + } + } + + // Parse + + public void parse (String systemId) + throws IOException, SAXException + { + URL url = null; + try + { + url = new URL (systemId); + } + catch (MalformedURLException e) + { + File file = new File(systemId); + if (!file.exists ()) + { + throw new FileNotFoundException (systemId); + } + String path = file.getAbsolutePath(); + if (File.separatorChar != '/') + { + path = path.replace (File.separatorChar, '/'); + } + if (!path.startsWith ("/")) + { + path = "/" + path; + } + if (!path.endsWith ("/") && file.isDirectory ()) + { + path = path + "/"; + } + url = new URL ("file:" + path); + } + InputSource source = new InputSource(url.toString ()); + source.setByteStream (url.openStream ()); + parse (source); + } + + public synchronized void parse (InputSource input) + throws IOException, SAXException + { + NamedInputStream in = XMLJ.getInputStream (input); + byte[] detectBuffer = in.getDetectBuffer (); + String publicId = input.getPublicId (); + String systemId = input.getSystemId (); + base = XMLJ.getBaseURI (systemId); + // Reset state + standalone = false; + seenFatalError = false; + seenStartDocument = false; + if (systemId != null) + { + int lsi = systemId.lastIndexOf ('/'); + if (lsi != -1) + { + base = systemId.substring (0, lsi + 1); + } + } + // Handle zero-length document + if (detectBuffer == null) + { + startDocument (true); + fatalError ("No document element", 0, 0, publicId, systemId); + endDocument (); + return; + } + // Parse + parseStream(in, + detectBuffer, + publicId, + systemId, + base, + validation, + contentHandler != null, + dtdHandler != null, + entityResolver != null, + errorHandler != null, + declarationHandler != null, + lexicalHandler != null); + in.close (); + } + + native void parseStream (InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean validate, + boolean contentHandler, + boolean dtdHandler, + boolean entityResolver, + boolean errorHandler, + boolean declarationHandler, + boolean lexicalHandler) + throws IOException, SAXException; + + String getURI (String prefix) + { + if (!namespaces) + { + return null; + } + return ns.getURI (prefix); + } + + // Callbacks from libxmlj + + private void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (seenFatalError || lexicalHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + lexicalHandler.startDTD (name, publicId, systemId); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void externalEntityDecl (String name, String publicId, + String systemId) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + declarationHandler.externalEntityDecl (name, publicId, systemId); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void internalEntityDecl (String name, String value) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + declarationHandler.internalEntityDecl (name, value); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private InputStream resolveEntity (String publicId, String systemId) + throws SAXException, IOException + { + if (entityResolver == null) + { + return null; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + InputSource source = entityResolver.resolveEntity (publicId, systemId); + return (source == null) ? null : XMLJ.getInputStream (source); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + if (seenFatalError || dtdHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + dtdHandler.notationDecl (name, publicId, systemId); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void attributeDecl (String eName, String aName, String type, + String mode, String value) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + declarationHandler.attributeDecl (eName, aName, type, mode, value); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void elementDecl (String name, String model) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + declarationHandler.elementDecl (name, model); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void unparsedEntityDecl (String name, String publicId, + String systemId, String notationName) + throws SAXException + { + if (seenFatalError || dtdHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + dtdHandler.unparsedEntityDecl (name, publicId, systemId, + notationName); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void setDocumentLocator (Object ctx, Object loc) + { + locator = new GnomeLocator (ctx, loc); + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + contentHandler.setDocumentLocator (locator); + } + catch (Exception e) + { + } + } + + private void startDocument (boolean standalone) + throws SAXException + { + this.standalone = standalone; + seenStartDocument = true; + if (contentHandler == null) + { + return; + } + try + { + contentHandler.startDocument (); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void endDocument () + throws SAXException + { + if (contentHandler == null) + { + return; + } + try + { + contentHandler.endDocument(); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void startElement(String name, String[] attrs) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + XMLName xName = new XMLName (this, name); + if (namespaces) + { + // Handle defined namespaces + ns.push (); + int len = (attrs == null) ? 0 : attrs.length; + if (len > 0) + { + ArrayList filtered = new ArrayList (len); + for (int i = 0; i < len; i += 2) + { + String attName = attrs[i]; + String attValue = attrs[i + 1]; + if (attName.equals ("xmlns")) + { + startPrefixMapping ("", attValue); + } + else if (attName.startsWith ("xmlns:")) + { + startPrefixMapping (attName.substring (6), attValue); + } + else + { + filtered.add (attName); + filtered.add (attValue); + } + } + // Remove xmlns attributes + attrs = new String[filtered.size ()]; + filtered.toArray (attrs); + } + } + // Construct attributes + Attributes atts = new StringArrayAttributes (this, attrs); + contentHandler.startElement (xName.uri, xName.localName, xName.qName, + atts); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void endElement (String name) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + XMLName xName = new XMLName (this, name); + String uri = (xName.uri == null) ? "" : xName.uri; + contentHandler.endElement (uri, xName.localName, xName.qName); + // Handle undefining namespaces + if (namespaces) + { + for (Iterator i = ns.currentPrefixes (); i.hasNext (); ) + { + endPrefixMapping ((String) i.next ()); + } + ns.pop (); // releases current depth + } + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + ns.define (prefix, uri); + contentHandler.startPrefixMapping (prefix, uri); + } + + private void endPrefixMapping (String prefix) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + contentHandler.endPrefixMapping (prefix); + } + + private void characters (String text) + throws SAXException + { + if (seenFatalError || contentHandler == null || text == null) + { + return; + } + try + { + char[] ch = text.toCharArray (); + contentHandler.characters (ch, 0, ch.length); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void ignorableWhitespace (String text) + throws SAXException + { + if (seenFatalError || contentHandler == null || text == null) + { + return; + } + try + { + char[] ch = text.toCharArray (); + contentHandler.ignorableWhitespace (ch, 0, ch.length); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void processingInstruction (String target, String data) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + if (data == null) + { + data = ""; + } + contentHandler.processingInstruction (target, data); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void comment (String text) + throws SAXException + { + if (seenFatalError || lexicalHandler == null || text == null) + { + return; + } + try + { + char[] ch = text.toCharArray (); + lexicalHandler.comment (ch, 0, ch.length); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void cdataBlock (String text) + throws SAXException + { + if (seenFatalError || text == null) + { + return; + } + try + { + if (lexicalHandler == null) + { + characters(text); + } + else + { + lexicalHandler.startCDATA(); + characters(text); + lexicalHandler.endCDATA(); + } + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void warning (String message, + int lineNumber, int columnNumber, + String publicId, String systemId) + throws SAXException + { + if (seenFatalError || errorHandler == null) + { + return; + } + try + { + Locator l = new StandaloneLocator (lineNumber, columnNumber, + publicId, systemId); + errorHandler.warning (new SAXParseException (message, l)); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void error (String message, + int lineNumber, int columnNumber, + String publicId, String systemId) + throws SAXException + { + if (seenFatalError || errorHandler == null) + { + return; + } + try + { + Locator l = new StandaloneLocator (lineNumber, columnNumber, + publicId, systemId); + errorHandler.error (new SAXParseException (message, l)); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void fatalError (String message, + int lineNumber, int columnNumber, + String publicId, String systemId) + throws SAXException + { + if (seenFatalError || errorHandler == null) + { + return; + } + try + { + if (!seenStartDocument) + { + startDocument (false); + } + seenFatalError = true; + Locator l = new StandaloneLocator (lineNumber, columnNumber, + publicId, systemId); + errorHandler.fatalError (new SAXParseException (message, l)); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java b/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java new file mode 100644 index 00000000000..646d7b408cb --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java @@ -0,0 +1,122 @@ +/* Namespaces.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; + +/** + * Helper class for managing namespaces. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class Namespaces +{ + + ArrayList stack = new ArrayList (); + + /** + * Increments the tree depth. + * This allocates a new potential namespace entry. + */ + void push () + { + stack.add (null); + } + + /** + * Decrements the tree depth. + * This removes namespaces defined at the extremity. + */ + void pop () + { + stack.remove (stack.size() - 1); + } + + /** + * Searches for the namespace URI corresponding to the specified prefix. + */ + String getURI (String prefix) + { + for (int i = stack.size () - 1; i >= 0; i--) + { + HashMap ns = (HashMap) stack.get (i); + if (ns != null && ns.containsKey (prefix)) + { + String ret = (String) ns.get (prefix); + return (ret == null) ? "" : ret; + } + } + return ""; + } + + /** + * Defines the specified prefix-URI mapping at the current depth in the + * tree. + */ + void define (String prefix, String uri) + { + int index = stack.size () - 1; + HashMap ns = (HashMap) stack.get (index); + if (ns == null) + { + ns = new HashMap (); + stack.set (index, ns); + } + ns.put (prefix, uri); + } + + /** + * Returns an iterator over the prefixes defined at the current depth. + */ + Iterator currentPrefixes () + { + HashMap ns = (HashMap) stack.get (stack.size () - 1); + if (ns == null) + { + return Collections.EMPTY_LIST.iterator (); + } + else + { + return ns.keySet ().iterator (); + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java b/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java new file mode 100644 index 00000000000..f534efbcf95 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java @@ -0,0 +1,171 @@ +/* StringArrayAttributes.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import org.xml.sax.Attributes; + +/** + * An implementation of Attributes that reads values from an array of + * strings, supplied by libxml2. + * Each pair of elements in the array represents a key followed by a value. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class StringArrayAttributes +implements Attributes +{ + + private int len; + private XMLName[] keys; + private String[] values; + + StringArrayAttributes (GnomeXMLReader parser, String[] pairs) + { + len = (pairs == null) ? 0 : pairs.length / 2; + keys = new XMLName[len]; + values = new String[len]; + for (int i = 0; i < len; i++) + { + int pairIndex = i * 2; + keys[i] = new XMLName (parser, pairs[pairIndex]); + values[i] = pairs[pairIndex + 1]; + } + } + + public int getLength () + { + return len; + } + + public String getURI (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return keys[index].uri; + } + + public String getLocalName (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return keys[index].localName; + } + + public String getQName (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return keys[index].qName; + } + + public String getType (int index) + { + if (index < 0 || index >= len) + { + return null; + } + // TODO can we get this information from libxml2? + return "CDATA"; + } + + public String getValue (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return values[index]; + } + + public int getIndex (String uri, String localName) + { + for (int i = 0; i < len; i++) + { + XMLName key = keys[i]; + if (key.localName.equals (localName)) + { + if ((key.uri == null && uri == null) || + (key.uri != null && key.uri.equals(uri))) + { + return i; + } + } + } + return -1; + } + + public int getIndex (String qName) + { + for (int i = 0; i < len; i++) + { + if (keys[i].qName.equals (qName)) + { + return i; + } + } + return -1; + } + + public String getType (String uri, String localName) + { + return getType (getIndex (uri, localName)); + } + + public String getType (String qName) + { + return getType (getIndex (qName)); + } + + public String getValue (String uri, String localName) + { + return getValue (getIndex (uri, localName)); + } + + public String getValue (String qName) + { + return getValue (getIndex (qName)); + } + +} + diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java b/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java new file mode 100644 index 00000000000..5950a7996a0 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java @@ -0,0 +1,92 @@ +/* XMLName.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +/** + * Structure containing the components of an XML element/attribute name. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class XMLName +{ + + private static final String XML_URI = "http://www.w3.org/XML/1998/namespace"; + + final String uri; + final String localName; + final String qName; + final String prefix; + + XMLName (GnomeXMLReader parser, String qName) + { + this.qName = qName; + int ci = qName.lastIndexOf (':'); + if (ci < 1) + { + localName = qName; + prefix = null; + uri = ""; + } + else + { + localName = qName.substring (ci + 1); + prefix = qName.substring (0, ci); + if ("xml".equals (prefix)) + { + if ("lang".equals (localName) || "space".equals (localName)) + { + uri = XML_URI; + } + else + { + uri = parser.getURI (prefix); + } + } + else + { + uri = parser.getURI (prefix); + } + } + } + + public String toString () + { + return qName; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java b/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java new file mode 100644 index 00000000000..0601e5e8987 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java @@ -0,0 +1,111 @@ +/* ErrorListenerErrorHandler.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.transform; + +import javax.xml.transform.ErrorListener; +import javax.xml.transform.TransformerException; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * Provides a SAX ErrorHandler interface to an ErrorListener. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class ErrorListenerErrorHandler +implements ErrorHandler +{ + + private ErrorListener listener; + + ErrorListenerErrorHandler (ErrorListener listener) + { + this.listener = listener; + } + + public void warning (SAXParseException e) + throws SAXException + { + try + { + listener.warning (new TransformerException (e)); + } + catch (TransformerException te) + { + throw getSAXException (te); + } + } + + public void error (SAXParseException e) + throws SAXException + { + try + { + listener.error (new TransformerException (e)); + } + catch (TransformerException te) + { + throw getSAXException (te); + } + } + + public void fatalError (SAXParseException e) + throws SAXException + { + try + { + listener.fatalError (new TransformerException (e)); + } + catch (TransformerException te) + { + throw getSAXException (te); + } + } + + private SAXException getSAXException (TransformerException e) + { + Throwable cause = e.getCause (); + if (cause instanceof SAXException) + { + return (SAXException) cause; + } + return new SAXException (e); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java new file mode 100755 index 00000000000..37aa05b9359 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java @@ -0,0 +1,572 @@ +/* GnomeTransformer.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.transform; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.net.URL; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +import javax.xml.transform.ErrorListener; +import javax.xml.transform.Source; +import javax.xml.transform.SourceLocator; +import javax.xml.transform.Result; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; + +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Node; + +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; + +import gnu.xml.libxmlj.dom.GnomeDocument; +import gnu.xml.libxmlj.sax.GnomeXMLReader; +import gnu.xml.libxmlj.util.NamedInputStream; +import gnu.xml.libxmlj.util.StandaloneLocator; +import gnu.xml.libxmlj.util.XMLJ; + +/** + * An implementation of {@link javax.xml.transform.Transformer} which + * performs XSLT transformation using <code>libxslt</code>. + * + * @author Julian Scheid + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeTransformer + extends Transformer + implements Templates +{ + + /** + * The parameters added by the user via {@link setParameter()}. + */ + private Map parameters; + + /** + * The output properties set by the user. + */ + private Properties outputProperties; + + /** + * The URI resolver to use during transformation. + */ + private URIResolver resolver; + + /** + * The error listener for transformation errors. + */ + private ErrorListener errorListener; + + /** + * Handle to the source stylesheet. + * This is a native pointer of type xsltStylesheetPtr. + */ + private Object stylesheet; + + /** + * Constructor. + * @param source the XSLT stylesheet document source + * @param resolver the resolver to use during transformation + * @param errorListener the error listener for transformation errors + */ + GnomeTransformer (Source source, + URIResolver resolver, + ErrorListener errorListener) + throws TransformerConfigurationException + { + this.resolver = resolver; + this.errorListener = errorListener; + parameters = new HashMap (); + outputProperties = new Properties (); + + if (source == null) + { + stylesheet = newStylesheet (); + } + else if (source instanceof StreamSource) + { + try + { + StreamSource ss = (StreamSource) source; + NamedInputStream in = XMLJ.getInputStream (ss); + String systemId = ss.getSystemId (); + String publicId = ss.getPublicId (); + String base = XMLJ.getBaseURI (systemId); + byte[] detectBuffer = in.getDetectBuffer (); + if (detectBuffer == null) + { + String msg = "No document element"; + throw new TransformerConfigurationException (msg); + } + stylesheet = newStylesheetFromStream (in, detectBuffer, publicId, + systemId, base, + (resolver != null), + (errorListener != null)); + } + catch (IOException e) + { + throw new TransformerConfigurationException (e); + } + } + else if (source instanceof DOMSource) + { + DOMSource ds = (DOMSource) source; + Node node = ds.getNode (); + if (!(node instanceof GnomeDocument)) + { + String msg = "Node is not a GnomeDocument"; + throw new TransformerConfigurationException (msg); + } + GnomeDocument doc = (GnomeDocument) node; + stylesheet = newStylesheetFromDoc (doc); + } + else + { + String msg = "Source type not supported (" + source + ")"; + throw new TransformerConfigurationException (msg); + } + } + + /** + * Copy constructor. + */ + private GnomeTransformer (Object stylesheet, + URIResolver resolver, + ErrorListener errorListener, + Map parameters, + Properties outputProperties) + { + this.stylesheet = stylesheet; + this.resolver = resolver; + this.errorListener = errorListener; + this.parameters = parameters; + this.outputProperties = outputProperties; + } + + private native Object newStylesheet () + throws TransformerConfigurationException; + + private native Object newStylesheetFromStream (InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean entityResolver, + boolean errorHandler) + throws TransformerConfigurationException; + + private native Object newStylesheetFromDoc (GnomeDocument doc) + throws TransformerConfigurationException; + + //--- Implementation of javax.xml.transform.Transformer follows. + + // Set, get and clear the parameters to use on transformation + + public synchronized void setParameter (String parameter, Object value) + { + parameters.put (parameter, value); + } + + public synchronized Object getParameter (String name) + { + return parameters.get (name); + } + + public synchronized void clearParameters () + { + parameters.clear (); + } + + // Set and get the ErrorListener to use on transformation + + public void setErrorListener (ErrorListener listener) + { + this.errorListener = listener; + } + + public ErrorListener getErrorListener () + { + return errorListener; + } + + // Set and get the URIResolver to use on transformation + + public void setURIResolver (URIResolver resolver) + { + this.resolver = resolver; + } + + public URIResolver getURIResolver () + { + return resolver; + } + + // Set the output properties to use on transformation; get default + // output properties and output properties specified in the + // stylesheet or by the user. + + public void setOutputProperties (Properties outputProperties) + { + // Note: defensive copying + this.outputProperties = new Properties (outputProperties); + } + + public void setOutputProperty (String name, String value) + { + outputProperties.setProperty (name, value); + } + + public Properties getOutputProperties () + { + // Note: defensive copying + return new Properties (this.outputProperties); + } + + public String getOutputProperty (String name) + { + return outputProperties.getProperty (name); + } + + // -- Templates -- + + public Transformer newTransformer () + { + return new GnomeTransformer (stylesheet, resolver, errorListener, + new HashMap (parameters), + new Properties (outputProperties)); + } + + // -- transform -- + + /** + * Transforms the given source and writes the result to the + * given target. + */ + public void transform (Source source, Result result) + throws TransformerException + { + if (source instanceof StreamSource) + { + try + { + StreamSource ss = (StreamSource) source; + NamedInputStream in = XMLJ.getInputStream (ss); + String publicId = ss.getPublicId (); + String systemId = ss.getSystemId (); + String base = XMLJ.getBaseURI (systemId); + byte[] detectBuffer = in.getDetectBuffer (); + if (detectBuffer == null) + { + throw new TransformerException ("No document element"); + } + if (result instanceof StreamResult) + { + OutputStream out = XMLJ.getOutputStream ((StreamResult) result); + transformStreamToStream (in, detectBuffer, publicId, systemId, + base, (resolver != null), + (errorListener != null), out); + } + else if (result instanceof DOMResult) + { + DOMResult dr = (DOMResult) result; + GnomeDocument ret = + transformStreamToDoc (in, detectBuffer, publicId, systemId, + base, (resolver != null), + (errorListener != null)); + dr.setNode (ret); + dr.setSystemId (null); + } + else if (result instanceof SAXResult) + { + SAXResult sr = (SAXResult) result; + transformStreamToSAX (in, detectBuffer, publicId, systemId, + base, (resolver != null), + (errorListener != null), + getSAXContext (sr)); + } + else + { + String msg = "Result type not supported (" + result + ")"; + throw new TransformerConfigurationException (msg); + } + } + catch (IOException e) + { + throw new TransformerException (e); + } + } + else if (source instanceof DOMSource) + { + DOMSource ds = (DOMSource) source; + Node node = ds.getNode (); + if (!(node instanceof GnomeDocument)) + { + String msg = "Node is not a GnomeDocument (" + node + ")"; + throw new TransformerException (msg); + } + GnomeDocument doc = (GnomeDocument) node; + if (result instanceof StreamResult) + { + try + { + OutputStream out = XMLJ.getOutputStream ((StreamResult) result); + transformDocToStream (doc, out); + } + catch (IOException e) + { + throw new TransformerException (e); + } + } + else if (result instanceof DOMResult) + { + DOMResult dr = (DOMResult) result; + GnomeDocument ret = transformDocToDoc (doc); + dr.setNode (ret); + dr.setSystemId (null); + } + else if (result instanceof SAXResult) + { + SAXResult sr = (SAXResult) result; + transformDocToSAX (doc, getSAXContext (sr)); + } + else + { + String msg = "Result type not supported"; + throw new TransformerConfigurationException (msg); + } + } + else + { + String msg = "Source type not supported"; + throw new TransformerConfigurationException (msg); + } + } + + private GnomeXMLReader getSAXContext (SAXResult result) + { + GnomeXMLReader ctx = new GnomeXMLReader (); + ctx.setContentHandler (result.getHandler ()); + ctx.setLexicalHandler (result.getLexicalHandler ()); + if (errorListener != null) + { + ErrorHandler errorHandler = + new ErrorListenerErrorHandler (errorListener); + ctx.setErrorHandler (errorHandler); + } + if (resolver != null) + { + EntityResolver entityResolver = + new URIResolverEntityResolver (resolver); + ctx.setEntityResolver (entityResolver); + } + return ctx; + } + + private native void transformStreamToStream (InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean entityResolver, + boolean errorHandler, + OutputStream out) + throws TransformerException; + + private native GnomeDocument transformStreamToDoc (InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean entityResolver, + boolean errorHandler) + throws TransformerException; + + private native void transformStreamToSAX (InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean entityResolver, + boolean errorHandler, + GnomeXMLReader out) + throws TransformerException; + + private native void transformDocToStream (GnomeDocument in, + OutputStream out) + throws TransformerException; + + private native GnomeDocument transformDocToDoc (GnomeDocument in) + throws TransformerException; + + private native void transformDocToSAX (GnomeDocument in, + GnomeXMLReader out) + throws TransformerException; + + /* + * Retrieve parameters as a string array. + * This is a convenience method called from native code. + */ + private String[] getParameterArray () + { + String[] parameterArray = new String[parameters.size () * 2]; + int index = 0; + for (Iterator it = parameters.keySet ().iterator (); + it.hasNext (); + ++index) + { + String parameterKey = (String) it.next (); + String parameterValue = (String) parameters.get (parameterKey); + parameterArray[index * 2 + 0] = parameterKey; + parameterArray[index * 2 + 1] = + "'" + ((parameterValue != null) ? parameterValue : "") + "'"; + // FIXME encode parameter value correctly for XPath + } + return parameterArray; + } + + // -- Free xsltStylesheet handle -- + + public void finalize () + { + if (stylesheet != null) + { + free (); + stylesheet = null; + } + } + + private native void free (); + + // -- Callbacks -- + + private InputStream resolveEntity (String publicId, String systemId) + throws TransformerException + { + if (resolver != null) + { + systemId = resolver.resolve (null, systemId).getSystemId (); + } + if (systemId == null) + { + return null; + } + try + { + URL url = new URL (systemId); + return XMLJ.getInputStream (url); + } + catch (IOException e) + { + throw new TransformerException (e); + } + } + + private void setDocumentLocator (Object ctx, Object loc) + { + } + + private void warning (String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws TransformerException + { + if (errorListener == null) + { + return; + } + SourceLocator l = new StandaloneLocator (lineNumber, + columnNumber, + publicId, + systemId); + errorListener.warning (new TransformerException (message, l)); + } + + private void error (String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws TransformerException + { + if (errorListener == null) + { + return; + } + SourceLocator l = new StandaloneLocator (lineNumber, + columnNumber, + publicId, + systemId); + errorListener.error (new TransformerException (message, l)); + } + + private void fatalError (String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws TransformerException + { + if (errorListener == null) + { + return; + } + SourceLocator l = new StandaloneLocator (lineNumber, + columnNumber, + publicId, + systemId); + errorListener.fatalError (new TransformerException (message, l)); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java new file mode 100755 index 00000000000..78dfe2148b6 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java @@ -0,0 +1,349 @@ +/* GnomeTransformerFactory.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.transform; + +import java.io.InputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import javax.xml.transform.ErrorListener; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.URIResolver; + +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; + +import gnu.xml.libxmlj.util.XMLJ; + +/** + * An implementation of <code>TransformerFactory</code> producing + * <code>Transformer</code> objects which use <code>libxslt</code> + * for transformation. + * + * @author Julian Scheid + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class GnomeTransformerFactory + extends TransformerFactory +{ + + static + { + XMLJ.init (); + } + + /** + * URIResolver set by user, or default implementation. + */ + private URIResolver uriResolver; + + /** + * ErrorListener set by user, or default implementation. + */ + private ErrorListener errorListener; + + /** + * Attributes set by user. + */ + private Map attributes = new HashMap (); + + //--- Implementation of javax.xml.transform.TransformerFactory + //--- follows. + + // -- begin getAssociatedStylesheet implementation -- + + /** + * Returns the stylesheet associated with the specified XML source, or + * <code>null</code> if no associated stylesheet could be found. + */ + public Source getAssociatedStylesheet(Source source, String media, + String title, String charset) + throws TransformerConfigurationException + { + String href= null; + String base = source.getSystemId(); + if (source instanceof DOMSource) + { + Node node = ((DOMSource) source).getNode(); + Document doc = (node.getNodeType() == Node.DOCUMENT_NODE) ? + (Document) node : node.getOwnerDocument(); + if (base == null) + { + base = doc.getDocumentURI(); + } + for (node = doc.getFirstChild(); node != null; + node = node.getNextSibling()) + { + if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && + "xml-stylesheet".equals(node.getNodeName())) + { + String data = node.getNodeValue(); + if (media != null && + !media.equals(parseParameter(data, "type"))) + { + continue; + } + if (title != null && + !title.equals(parseParameter(data, "title"))) + { + continue; + } + href = parseParameter(data, "href"); + } + } + } + else + { + InputSource input; + XMLReader parser = null; + try + { + if (source instanceof SAXSource) + { + SAXSource sax = (SAXSource) source; + input = sax.getInputSource(); + parser = sax.getXMLReader(); + } + else + { + StreamSource stream = (StreamSource) source; + InputStream in = stream.getInputStream(); + input = new InputSource(in); + } + input.setSystemId(base); + if (parser == null) + { + parser = createXMLReader(); + } + AssociatedStylesheetHandler ash = + new AssociatedStylesheetHandler(); + ash.media = media; + ash.title = title; + parser.setContentHandler(ash); + parser.parse(input); + href = ash.href; + } + catch (SAXException e) + { + throw new TransformerConfigurationException(e); + } + catch (IOException e) + { + throw new TransformerConfigurationException(e); + } + } + if (href == null) + { + return null; + } + if (base != null) + { + base = XMLJ.getBaseURI(base); + } + href = XMLJ.getAbsoluteURI(base, href); + return new StreamSource(href); + } + + private XMLReader createXMLReader() + throws TransformerConfigurationException + { + try + { + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser parser = factory.newSAXParser(); + return parser.getXMLReader(); + } + catch (FactoryConfigurationError e) + { + throw new TransformerConfigurationException(e); + } + catch (ParserConfigurationException e) + { + throw new TransformerConfigurationException(e); + } + catch (SAXException e) + { + throw new TransformerConfigurationException(e); + } + } + + class AssociatedStylesheetHandler + extends DefaultHandler + { + + String media; + String title; + String href; + + public void processingInstruction(String target, String data) + throws SAXException + { + if ("xml-stylesheet".equals(target)) + { + if (media != null && !media.equals(parseParameter(data, "type"))) + { + return; + } + if (title != null && !title.equals(parseParameter(data, "title"))) + { + return; + } + href = parseParameter(data, "href"); + } + } + + } + + String parseParameter(String data, String name) + { + int start = data.indexOf(name + "="); + if (start != -1) + { + start += name.length() + 2; + char delim = data.charAt(start - 1); + int end = data.indexOf(delim, start); + if (end != -1) + { + return data.substring(start, end); + } + } + return null; + } + + // -- end getAssociatedStylesheet implementation -- + + public synchronized void setAttribute (String name, Object value) + { + this.attributes.put (name, value); + } + + public synchronized Object getAttribute (String name) + { + return attributes.get (name); + } + + public void setErrorListener (ErrorListener errorListener) + { + this.errorListener = errorListener; + } + + public ErrorListener getErrorListener () + { + return errorListener; + } + + public void setURIResolver (URIResolver uriResolver) + { + this.uriResolver = uriResolver; + } + + public URIResolver getURIResolver () + { + return uriResolver; + } + + public boolean getFeature (String name) + { + return (StreamSource.FEATURE.equals (name) || + StreamResult.FEATURE.equals (name) || + DOMSource.FEATURE.equals (name) || + DOMResult.FEATURE.equals (name)); + } + + public void setFeature(String name, boolean value) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException(name); + } + + /** + * Returns a new instance of class {@link Transformer} for a + * null souce. + */ + public Transformer newTransformer () + throws TransformerConfigurationException + { + return newTransformer (null); + } + + /** + * Returns a new instance of class {@link Transformer} for + * the given souce. + */ + public Transformer newTransformer (Source source) + throws TransformerConfigurationException + { + return new GnomeTransformer (source, uriResolver, errorListener); + } + + /** + * Returns a new instance of class {@link Templates} for + * the given souce. + */ + public Templates newTemplates (Source source) + throws TransformerConfigurationException + { + return new GnomeTransformer (source, uriResolver, errorListener); + } + + /** + * Perform native cleanup. + */ + public static native void freeLibxsltGlobal (); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java b/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java new file mode 100644 index 00000000000..88a58e1a2fa --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java @@ -0,0 +1,87 @@ +/* URIResolverEntityResolver.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.transform; + +import java.io.IOException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.TransformerException; +import javax.xml.transform.sax.SAXSource; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Provides an EntityResolver interface to a URIResolver. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class URIResolverEntityResolver +implements EntityResolver +{ + + private URIResolver resolver; + + URIResolverEntityResolver (URIResolver resolver) + { + this.resolver = resolver; + } + + public InputSource resolveEntity (String publicId, String systemId) + throws SAXException, IOException + { + try + { + return SAXSource.sourceToInputSource (resolver.resolve (systemId, + null)); + } + catch (TransformerException e) + { + Throwable cause = e.getCause (); + if (cause instanceof SAXException) + { + throw (SAXException) cause; + } + else if (cause instanceof IOException) + { + throw (IOException) cause; + } + throw new SAXException (e); + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/package.html b/libjava/classpath/gnu/xml/libxmlj/transform/package.html new file mode 100755 index 00000000000..dac1027ffa6 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/package.html @@ -0,0 +1,14 @@ +<body> +<p> + A JAXP-compliant wrapper for the XSLT C library for Gnome, also + known as libxslt. Allows to use libxslt via the Java API for XML + processing. +</p> + +<p> + <b>Usage:</b> + <li>Set the system property <code>javax.xml.transform.TransformerFactory</code> + to <code>gnu.xml.libxmlj.GnomeTransformerFactory</code>.</li> + </ul> +</p> +</body> diff --git a/libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java b/libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java new file mode 100644 index 00000000000..24a08229ced --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java @@ -0,0 +1,62 @@ +/* EmptyNodeList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.util; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * An empty node list. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class EmptyNodeList +implements NodeList +{ + + public Node item (int index) + { + return null; + } + + public int getLength () + { + return 0; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java b/libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java new file mode 100644 index 00000000000..8d20ec4e57b --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java @@ -0,0 +1,99 @@ +/* NamedInputStream.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.util; + +import java.io.FilterInputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.PushbackInputStream; + +/** + * An input stream associated with an XML system ID. + * It can report the system ID and the first few bytes of the stream + * in order to detect the character encoding of the stream. + * + * @author <a href='dog@gnu.org'>Chris Burdess</a> + */ +public class NamedInputStream +extends FilterInputStream +{ + + private static int DETECT_BUFFER_SIZE = 50; + + private String name; + + NamedInputStream (String name, InputStream in, int size) + { + super (new PushbackInputStream (in, size)); + this.name = name; + } + + /** + * Returns the name of the stream (the XML system ID). + */ + public String getName () + { + return name; + } + + /** + * Returns the first few bytes of the stream for character encoding + * purposes. The entire stream can thereafter be read normally from the + * beginning. This method is only valid if no bytes have yet been read + * from the stream. + */ + public byte[] getDetectBuffer () + throws IOException + { + PushbackInputStream p = (PushbackInputStream) in; + byte[] buffer = new byte[DETECT_BUFFER_SIZE]; + int len = p.read (buffer); + if (len < 0) + { + return null; + } + else + { + p.unread (buffer, 0, len); + byte[] ret = new byte[len]; + System.arraycopy (buffer, 0, ret, 0, len); + return ret; + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java new file mode 100644 index 00000000000..d47cdb05d66 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java @@ -0,0 +1,294 @@ +/* StandaloneDocumentType.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.util; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.UserDataHandler; + +/** + * A "standalone" document type, i.e. one that isn't attached to a document + * node. + * This can be used to create new documents. + */ +public final class StandaloneDocumentType +implements DocumentType +{ + + private final String name; + private final String publicId; + private final String systemId; + + public StandaloneDocumentType (String name, String publicId, String systemId) + { + this.name = name; + this.publicId = publicId; + this.systemId = systemId; + } + + public String getName () + { + return name; + } + + public NamedNodeMap getEntities () + { + // TODO + return null; + } + + public NamedNodeMap getNotations () + { + // TODO + return null; + } + + public String getPublicId () + { + return publicId; + } + + public String getSystemId () + { + return systemId; + } + + public String getInternalSubset () + { + return null; + } + + // -- Node -- + + public String getNodeName () + { + return getName (); + } + + public String getNodeValue () + throws DOMException + { + return null; + } + + public void setNodeValue (String nodeValue) + throws DOMException + { + } + + public short getNodeType () + { + return DOCUMENT_TYPE_NODE; + } + + public Node getParentNode () + { + return null; + } + + public NodeList getChildNodes () + { + return new EmptyNodeList (); + } + + public Node getFirstChild () + { + return null; + } + + public Node getLastChild () + { + return null; + } + + public Node getPreviousSibling () + { + return null; + } + + public Node getNextSibling () + { + return null; + } + + public NamedNodeMap getAttributes () + { + return null; + } + + public Document getOwnerDocument () + { + return null; + } + + public Node insertBefore (Node newChild, Node refChild) + throws DOMException + { + throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null); + } + + public Node replaceChild (Node newChild, Node oldChild) + throws DOMException + { + throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null); + } + + public Node removeChild (Node oldChild) + throws DOMException + { + throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null); + } + + public Node appendChild (Node oldChild) + throws DOMException + { + throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null); + } + + public boolean hasChildNodes () + { + return false; + } + + public Node cloneNode (boolean deep) + { + return new StandaloneDocumentType (name, publicId, systemId); + } + + public void normalize () + { + } + + public boolean isSupported (String feature, String version) + { + return false; + } + + public String getNamespaceURI () + { + return null; + } + + public String getPrefix () + { + return null; + } + + public void setPrefix (String prefix) + { + throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null); + } + + public String getLocalName () + { + return getName (); + } + + public boolean hasAttributes () + { + return false; + } + + // DOM Level 3 + + public String getBaseURI () + { + return null; + } + + public short compareDocumentPosition (Node node) + { + return -1; + } + + public String getTextContent () + { + return null; + } + + public void setTextContent (String content) + { + throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, null); + } + + public boolean isSameNode (Node other) + { + return equals (other); + } + + public String lookupPrefix (String namespace) + { + return null; + } + + public boolean isDefaultNamespace (String namespace) + { + return false; + } + + public String lookupNamespaceURI (String prefix) + { + return null; + } + + public boolean isEqualNode (Node other) + { + return equals (other); + } + + public Object getFeature (String feature, String version) + { + return null; + } + + public Object setUserData (String name, Object value, + UserDataHandler handler) + { + return null; + } + + public Object getUserData (String name) + { + return null; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java new file mode 100644 index 00000000000..5430330cc4e --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java @@ -0,0 +1,89 @@ +/* StandaloneLocator.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.util; + +import javax.xml.transform.SourceLocator; +import org.xml.sax.Locator; + +/** + * SAX Locator implementation that uses the specified values. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class StandaloneLocator +implements Locator, SourceLocator +{ + + private final int lineNumber; + + private final int columnNumber; + + private final String publicId; + + private final String systemId; + + public StandaloneLocator (int lineNumber, int columnNumber, + String publicId, String systemId) + { + this.lineNumber = lineNumber; + this.columnNumber = columnNumber; + this.publicId = publicId; + this.systemId = systemId; + } + + public String getPublicId () + { + return publicId; + } + + public String getSystemId () + { + return systemId; + } + + public int getLineNumber () + { + return lineNumber; + } + + public int getColumnNumber () + { + return columnNumber; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java b/libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java new file mode 100644 index 00000000000..0d5f9f689dc --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java @@ -0,0 +1,280 @@ +/* XMLJ.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.util; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.xml.sax.InputSource; + +import gnu.xml.libxmlj.transform.GnomeTransformerFactory; + +import gnu.xml.dom.ls.ReaderInputStream; +import gnu.xml.dom.ls.WriterOutputStream; + +/** + * Utility functions for libxmlj. + */ +public final class XMLJ +{ + + static class XMLJShutdownHook + implements Runnable + { + + public void run () + { + // Make sure finalizers are run + System.gc (); + Runtime.getRuntime ().runFinalization (); + + // Perform global cleanup on the native level + GnomeTransformerFactory.freeLibxsltGlobal (); + } + + } + + private static boolean initialised = false; + + public static void init () + { + if (!initialised) + { + System.loadLibrary ("xmlj"); + + XMLJShutdownHook hook = new XMLJShutdownHook (); + Runtime.getRuntime ().addShutdownHook (new Thread (hook)); + } + initialised = true; + } + + private static final int LOOKAHEAD = 50; + + /** + * Returns an input stream for the specified input source. + * This returns a pushback stream that libxmlj can use to detect the + * character encoding of the stream. + */ + public static NamedInputStream getInputStream (InputSource input) + throws IOException + { + InputStream in = input.getByteStream (); + String systemId = input.getSystemId (); + if (in == null) + { + Reader r = input.getCharacterStream(); + if (r != null) + in = new ReaderInputStream(r); + } + if (in == null) + { + in = getInputStream(systemId); + } + return new NamedInputStream (systemId, in, LOOKAHEAD); + } + + /** + * Returns an input stream for the specified transformer source. + * This returns a pushback stream that libxmlj can use to detect the + * character encoding of the stream. + */ + public static NamedInputStream getInputStream (Source source) + throws IOException + { + if (source instanceof SAXSource) + { + return getInputStream (((SAXSource) source).getInputSource ()); + } + InputStream in = null; + String systemId = source.getSystemId (); + if (source instanceof StreamSource) + { + in = ((StreamSource) source).getInputStream (); + } + if (in == null) + { + in = getInputStream(systemId); + } + return new NamedInputStream (systemId, in, LOOKAHEAD); + } + + private static InputStream getInputStream(String systemId) + throws IOException + { + if (systemId == null) + { + throw new IOException("no system ID"); + } + try + { + return new URL(systemId).openStream(); + } + catch (MalformedURLException e) + { + return new FileInputStream(systemId); + } + } + + /** + * Returns an input stream for the specified URL. + * This returns a pushback stream that libxmlj can use to detect the + * character encoding of the stream. + */ + public static NamedInputStream getInputStream (URL url) + throws IOException + { + return new NamedInputStream (url.toString (), url.openStream(), + LOOKAHEAD); + } + + /** + * Convenience method for xmljDocLoader + */ + static NamedInputStream xmljGetInputStream(String base, String url) + throws IOException + { + try + { + if (base != null) + { + url = new URL(new URL(base), url).toString(); + } + } + catch (MalformedURLException e) + { + } + InputStream in = getInputStream(url); + return new NamedInputStream(url, in, LOOKAHEAD); + } + + /** + * Returns an output stream for the specified transformer result. + */ + public static OutputStream getOutputStream (Result result) + throws IOException + { + OutputStream out = null; + if (result instanceof StreamResult) + { + out = ((StreamResult) result).getOutputStream (); + } + if (out == null) + { + Writer w = ((StreamResult) result).getWriter (); + if (w != null) + out = new WriterOutputStream (w); + } + if (out == null) + { + String systemId = result.getSystemId (); + if (systemId == null) + { + throw new IOException ("no system ID"); + } + try + { + URL url = new URL (systemId); + URLConnection connection = url.openConnection (); + connection.setDoOutput (true); + out = connection.getOutputStream (); + } + catch (MalformedURLException e) + { + out = new FileOutputStream (systemId); + } + } + + return out; + } + + /** + * Returns the absolute form of the specified URI. + * If the URI is already absolute, returns it as-is. + * Otherwise returns a new URI relative to the given base URI. + */ + public static String getAbsoluteURI (String base, String uri) + { + if (uri != null && + base != null && + (uri.length() > 0) && + (uri.indexOf(':') == -1) && + (uri.charAt(0) != '/')) + { + // URI is relative + if (base.charAt(base.length() - 1) != '/') + { + int i = base.lastIndexOf('/'); + base = base.substring(0, i + 1); + } + return base + uri; + } + else + { + // URI is absolute or no base specified + return uri; + } + } + + public static String getBaseURI(String uri) + { + if (uri != null) + { + int si = uri.lastIndexOf('/'); + if (si != -1) + { + uri = uri.substring(0, si + 1); + } + } + return uri; + } + +} diff --git a/libjava/classpath/gnu/xml/pipeline/CallFilter.java b/libjava/classpath/gnu/xml/pipeline/CallFilter.java new file mode 100644 index 00000000000..3b337517377 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/CallFilter.java @@ -0,0 +1,257 @@ +/* CallFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.net.URLConnection; +import java.io.Writer; + +import org.xml.sax.DTDHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +import gnu.xml.util.Resolver; +import gnu.xml.util.XMLWriter; + + +/** + * Input is sent as an XML request to given URI, and the output of this + * filter is the parsed response to that request. + * A connection is opened to the remote URI when the startDocument call is + * issued through this filter, and the request is finished when the + * endDocument call is issued. Events should be written quickly enough to + * prevent the remote HTTP server from aborting the connection due to + * inactivity; you may want to buffer text in an earlier pipeline stage. + * If your application requires validity checking of such + * outputs, have the output pipeline include a validation stage. + * + * <p>In effect, this makes a remote procedure call to the URI, with the + * request and response document syntax as chosen by the application. + * <em>Note that all the input events must be seen, and sent to the URI, + * before the first output event can be seen. </em> Clients are delayed + * at least by waiting for the server to respond, constraining concurrency. + * Services can thus be used to synchronize concurrent activities, and + * even to prioritize service among different clients. + * + * <p> You are advised to avoid restricting yourself to an "RPC" model + * for distributed computation. With a World Wide Web, network latencies + * and failures (e.g. non-availability) + * are significant; adopting a "procedure" model, rather than a workflow + * model where bulk requests are sent and worked on asynchronously, is not + * generally an optimal system-wide architecture. When the messages may + * need authentication, such as with an OpenPGP signature, or when server + * loads don't argue in favor of immediate responses, non-RPC models can + * be advantageous. (So-called "peer to peer" computing models are one + * additional type of model, though too often that term is applied to + * systems that still have a centralized control structure.) + * + * <p> <em>Be strict in what you send, liberal in what you accept,</em> as + * the Internet tradition goes. Strictly conformant data should never cause + * problems to its receiver; make your request pipeline be very strict, and + * don't compromise on that. Make your response pipeline strict as well, + * but be ready to tolerate specific mild, temporary, and well-documented + * variations from specific communications peers. + * + * @see XmlServlet + * + * @author David Brownell + */ +final public class CallFilter implements EventConsumer +{ + private Requestor req; + private EventConsumer next; + private URL target; + private URLConnection conn; + private ErrorHandler errHandler; + + + /** + * Initializes a call filter so that its inputs are sent to the + * specified URI, and its outputs are sent to the next consumer + * provided. + * + * @exception IOException if the URI isn't accepted as a URL + */ + // constructor used by PipelineFactory + public CallFilter (String uri, EventConsumer next) + throws IOException + { + this.next = next; + req = new Requestor (); + setCallTarget (uri); + } + + /** + * Assigns the URI of the call target to be used. + * Does not affect calls currently being made. + */ + final public void setCallTarget (String uri) + throws IOException + { + target = new URL (uri); + } + + /** + * Assigns the error handler to be used to present most fatal + * errors. + */ + public void setErrorHandler (ErrorHandler handler) + { + req.setErrorHandler (handler); + } + + + /** + * Returns the call target's URI. + */ + final public String getCallTarget () + { + return target.toString (); + } + + /** Returns the content handler currently in use. */ + final public org.xml.sax.ContentHandler getContentHandler () + { + return req; + } + + /** Returns the DTD handler currently in use. */ + final public DTDHandler getDTDHandler () + { + return req; + } + + + /** + * Returns the declaration or lexical handler currently in + * use, or throws an exception for other properties. + */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + if (EventFilter.DECL_HANDLER.equals (id)) + return req; + if (EventFilter.LEXICAL_HANDLER.equals (id)) + return req; + throw new SAXNotRecognizedException (id); + } + + + // JDK 1.1 seems to need it to be done this way, sigh + ErrorHandler getErrorHandler () { return errHandler; } + + // + // Takes input and echoes to server as POST input. + // Then sends the POST reply to the next pipeline element. + // + final class Requestor extends XMLWriter + { + Requestor () + { + super ((Writer)null); + } + + public synchronized void startDocument () throws SAXException + { + // Connect to remote object and set up to send it XML text + try { + if (conn != null) + throw new IllegalStateException ("call is being made"); + + conn = target.openConnection (); + conn.setDoOutput (true); + conn.setRequestProperty ("Content-Type", + "application/xml;charset=UTF-8"); + + setWriter (new OutputStreamWriter ( + conn.getOutputStream (), + "UTF8"), "UTF-8"); + + } catch (IOException e) { + fatal ("can't write (POST) to URI: " + target, e); + } + + // NOW base class can safely write that text! + super.startDocument (); + } + + public void endDocument () throws SAXException + { + // + // Finish writing the request (for HTTP, a POST); + // this closes the output stream. + // + super.endDocument (); + + // + // Receive the response. + // Produce events for the next stage. + // + InputSource source; + XMLReader producer; + String encoding; + + try { + + source = new InputSource (conn.getInputStream ()); + +// FIXME if status is anything but success, report it!! It'd be good to +// save the request data just in case we need to deal with a forward. + + encoding = Resolver.getEncoding (conn.getContentType ()); + if (encoding != null) + source.setEncoding (encoding); + + producer = XMLReaderFactory.createXMLReader (); + producer.setErrorHandler (getErrorHandler ()); + EventFilter.bind (producer, next); + producer.parse (source); + conn = null; + + } catch (IOException e) { + fatal ("I/O Exception reading response, " + e.getMessage (), e); + } + } + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/DomConsumer.java b/libjava/classpath/gnu/xml/pipeline/DomConsumer.java new file mode 100644 index 00000000000..389e02bb387 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/DomConsumer.java @@ -0,0 +1,982 @@ +/* DomConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import gnu.xml.aelfred2.ContentHandler2; +import gnu.xml.util.DomParser; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.helpers.AttributesImpl; +import org.w3c.dom.Attr; +import org.w3c.dom.CDATASection; +import org.w3c.dom.CharacterData; +import org.w3c.dom.Document; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Element; +import org.w3c.dom.EntityReference; +import org.w3c.dom.Node; +import org.w3c.dom.ProcessingInstruction; +import org.w3c.dom.Text; + +/** + * This consumer builds a DOM Document from its input, acting either as a + * pipeline terminus or as an intermediate buffer. When a document's worth + * of events has been delivered to this consumer, that document is read with + * a {@link DomParser} and sent to the next consumer. It is also available + * as a read-once property. + * + * <p>The DOM tree is constructed as faithfully as possible. There are some + * complications since a DOM should expose behaviors that can't be implemented + * without API backdoors into that DOM, and because some SAX parsers don't + * report all the information that DOM permits to be exposed. The general + * problem areas involve information from the Document Type Declaration (DTD). + * DOM only represents a limited subset, but has some behaviors that depend + * on much deeper knowledge of a document's DTD. You shouldn't have much to + * worry about unless you change handling of "noise" nodes from its default + * setting (which ignores them all); note if you use JAXP to populate your + * DOM trees, it wants to save "noise" nodes by default. (Such nodes include + * ignorable whitespace, comments, entity references and CDATA boundaries.) + * Otherwise, your + * main worry will be if you use a SAX parser that doesn't flag ignorable + * whitespace unless it's validating (few don't). + * + * <p> The SAX2 events used as input must contain XML Names for elements + * and attributes, with original prefixes. In SAX2, + * this is optional unless the "namespace-prefixes" parser feature is set. + * Moreover, many application components won't provide completely correct + * structures anyway. <em>Before you convert a DOM to an output document, + * you should plan to postprocess it to create or repair such namespace + * information.</em> The {@link NSFilter} pipeline stage does such work. + * + * <p> <em>Note: changes late in DOM L2 process made it impractical to + * attempt to create the DocumentType node in any implementation-neutral way, + * much less to populate it (L1 didn't support even creating such nodes). + * To create and populate such a node, subclass the inner + * {@link DomConsumer.Handler} class and teach it about the backdoors into + * whatever DOM implementation you want. It's possible that some revised + * DOM API (L3?) will make this problem solvable again. </em> + * + * @see DomParser + * + * @author David Brownell + */ +public class DomConsumer implements EventConsumer +{ + private Class domImpl; + + private boolean hidingCDATA = true; + private boolean hidingComments = true; + private boolean hidingWhitespace = true; + private boolean hidingReferences = true; + + private Handler handler; + private ErrorHandler errHandler; + + private EventConsumer next; + + // FIXME: this can't be a generic pipeline stage just now, + // since its input became a Class not a String (to be turned + // into a class, using the right class loader) + + + /** + * Configures this pipeline terminus to use the specified implementation + * of DOM when constructing its result value. + * + * @param impl class implementing {@link org.w3c.dom.Document Document} + * which publicly exposes a default constructor + * + * @exception SAXException when there is a problem creating an + * empty DOM document using the specified implementation + */ + public DomConsumer (Class impl) + throws SAXException + { + domImpl = impl; + handler = new Handler (this); + } + + /** + * This is the hook through which a subclass provides a handler + * which knows how to access DOM extensions, specific to some + * implementation, to record additional data in a DOM. + * Treat this as part of construction; don't call it except + * before (or between) parses. + */ + protected void setHandler (Handler h) + { + handler = h; + } + + + private Document emptyDocument () + throws SAXException + { + try { + return (Document) domImpl.newInstance (); + } catch (IllegalAccessException e) { + throw new SAXException ("can't access constructor: " + + e.getMessage ()); + } catch (InstantiationException e) { + throw new SAXException ("can't instantiate Document: " + + e.getMessage ()); + } + } + + + /** + * Configures this consumer as a buffer/filter, using the specified + * DOM implementation when constructing its result value. + * + * <p> This event consumer acts as a buffer and filter, in that it + * builds a DOM tree and then writes it out when <em>endDocument</em> + * is invoked. Because of the limitations of DOM, much information + * will as a rule not be seen in that replay. To get a full fidelity + * copy of the input event stream, use a {@link TeeConsumer}. + * + * @param impl class implementing {@link org.w3c.dom.Document Document} + * which publicly exposes a default constructor + * @param next receives a "replayed" sequence of parse events when + * the <em>endDocument</em> method is invoked. + * + * @exception SAXException when there is a problem creating an + * empty DOM document using the specified DOM implementation + */ + public DomConsumer (Class impl, EventConsumer n) + throws SAXException + { + this (impl); + next = n; + } + + + /** + * Returns the document constructed from the preceding + * sequence of events. This method should not be + * used again until another sequence of events has been + * given to this EventConsumer. + */ + final public Document getDocument () + { + return handler.clearDocument (); + } + + public void setErrorHandler (ErrorHandler handler) + { + errHandler = handler; + } + + + /** + * Returns true if the consumer is hiding entity references nodes + * (the default), and false if EntityReference nodes should + * instead be created. Such EntityReference nodes will normally be + * empty, unless an implementation arranges to populate them and then + * turn them back into readonly objects. + * + * @see #setHidingReferences + */ + final public boolean isHidingReferences () + { return hidingReferences; } + + /** + * Controls whether the consumer will hide entity expansions, + * or will instead mark them with entity reference nodes. + * + * @see #isHidingReferences + * @param flag False if entity reference nodes will appear + */ + final public void setHidingReferences (boolean flag) + { hidingReferences = flag; } + + + /** + * Returns true if the consumer is hiding comments (the default), + * and false if they should be placed into the output document. + * + * @see #setHidingComments + */ + public final boolean isHidingComments () + { return hidingComments; } + + /** + * Controls whether the consumer is hiding comments. + * + * @see #isHidingComments + */ + public final void setHidingComments (boolean flag) + { hidingComments = flag; } + + + /** + * Returns true if the consumer is hiding ignorable whitespace + * (the default), and false if such whitespace should be placed + * into the output document as children of element nodes. + * + * @see #setHidingWhitespace + */ + public final boolean isHidingWhitespace () + { return hidingWhitespace; } + + /** + * Controls whether the consumer hides ignorable whitespace + * + * @see #isHidingComments + */ + public final void setHidingWhitespace (boolean flag) + { hidingWhitespace = flag; } + + + /** + * Returns true if the consumer is saving CDATA boundaries, or + * false (the default) otherwise. + * + * @see #setHidingCDATA + */ + final public boolean isHidingCDATA () + { return hidingCDATA; } + + /** + * Controls whether the consumer will save CDATA boundaries. + * + * @see #isHidingCDATA + * @param flag True to treat CDATA text differently from other + * text nodes + */ + final public void setHidingCDATA (boolean flag) + { hidingCDATA = flag; } + + + + /** Returns the document handler being used. */ + final public ContentHandler getContentHandler () + { return handler; } + + /** Returns the DTD handler being used. */ + final public DTDHandler getDTDHandler () + { return handler; } + + /** + * Returns the lexical handler being used. + * (DOM construction can't really use declaration handlers.) + */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + if ("http://xml.org/sax/properties/lexical-handler".equals (id)) + return handler; + if ("http://xml.org/sax/properties/declaration-handler".equals (id)) + return handler; + throw new SAXNotRecognizedException (id); + } + + EventConsumer getNext () { return next; } + + ErrorHandler getErrorHandler () { return errHandler; } + + /** + * Class used to intercept various parsing events and use them to + * populate a DOM document. Subclasses would typically know and use + * backdoors into specific DOM implementations, used to implement + * DTD-related functionality. + * + * <p> Note that if this ever throws a DOMException (runtime exception) + * that will indicate a bug in the DOM (e.g. doesn't support something + * per specification) or the parser (e.g. emitted an illegal name, or + * accepted illegal input data). </p> + */ + public static class Handler + implements ContentHandler2, LexicalHandler, + DTDHandler, DeclHandler + { + protected DomConsumer consumer; + + private DOMImplementation impl; + private Document document; + private boolean isL2; + + private Locator locator; + private Node top; + private boolean inCDATA; + private boolean mergeCDATA; + private boolean inDTD; + private String currentEntity; + + private boolean recreatedAttrs; + private AttributesImpl attributes = new AttributesImpl (); + + /** + * Subclasses may use SAX2 events to provide additional + * behaviors in the resulting DOM. + */ + protected Handler (DomConsumer consumer) + throws SAXException + { + this.consumer = consumer; + document = consumer.emptyDocument (); + impl = document.getImplementation (); + isL2 = impl.hasFeature ("XML", "2.0"); + } + + private void fatal (String message, Exception x) + throws SAXException + { + SAXParseException e; + ErrorHandler errHandler = consumer.getErrorHandler ();; + + if (locator == null) + e = new SAXParseException (message, null, null, -1, -1, x); + else + e = new SAXParseException (message, locator, x); + if (errHandler != null) + errHandler.fatalError (e); + throw e; + } + + /** + * Returns and forgets the document produced. If the handler is + * reused, a new document may be created. + */ + Document clearDocument () + { + Document retval = document; + document = null; + locator = null; + return retval; + } + + /** + * Returns the document under construction. + */ + protected Document getDocument () + { return document; } + + /** + * Returns the current node being populated. This is usually + * an Element or Document, but it might be an EntityReference + * node if some implementation-specific code knows how to put + * those into the result tree and later mark them as readonly. + */ + protected Node getTop () + { return top; } + + + // SAX1 + public void setDocumentLocator (Locator locator) + { + this.locator = locator; + } + + // SAX1 + public void startDocument () + throws SAXException + { + if (document == null) + try { + if (isL2) { + // couple to original implementation + document = impl.createDocument (null, "foo", null); + document.removeChild (document.getFirstChild ()); + } else { + document = consumer.emptyDocument (); + } + } catch (Exception e) { + fatal ("DOM create document", e); + } + top = document; + } + + // ContentHandler2 + public void xmlDecl(String version, + String encoding, + boolean standalone, + String inputEncoding) + throws SAXException + { + if (document != null) + { + document.setXmlVersion(version); + document.setXmlStandalone(standalone); + } + } + + // SAX1 + public void endDocument () + throws SAXException + { + try { + if (consumer.getNext () != null && document != null) { + DomParser parser = new DomParser (document); + + EventFilter.bind (parser, consumer.getNext ()); + parser.parse ("ignored"); + } + } finally { + top = null; + } + } + + // SAX1 + public void processingInstruction (String target, String data) + throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (currentEntity != null) + return; + + ProcessingInstruction pi; + + if (isL2 + // && consumer.isUsingNamespaces () + && target.indexOf (':') != -1) + namespaceError ( + "PI target name is namespace nonconformant: " + + target); + if (inDTD) + return; + pi = document.createProcessingInstruction (target, data); + top.appendChild (pi); + } + + /** + * Subclasses may overrride this method to provide a more efficient + * way to construct text nodes. + * Typically, copying the text into a single character array will + * be more efficient than doing that as well as allocating other + * needed for a String, including an internal StringBuffer. + * Those additional memory and CPU costs can be incurred later, + * if ever needed. + * Unfortunately the standard DOM factory APIs encourage those costs + * to be incurred early. + */ + protected Text createText ( + boolean isCDATA, + char ch [], + int start, + int length + ) { + String value = new String (ch, start, length); + + if (isCDATA) + return document.createCDATASection (value); + else + return document.createTextNode (value); + } + + // SAX1 + public void characters (char ch [], int start, int length) + throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly + // at creation time) + if (currentEntity != null) + return; + + Node lastChild = top.getLastChild (); + + // merge consecutive text or CDATA nodes if appropriate. + if (lastChild instanceof Text) { + if (consumer.isHidingCDATA () + // consecutive Text content ... always merge + || (!inCDATA + && !(lastChild instanceof CDATASection)) + // consecutive CDATASection content ... don't + // merge between sections, only within them + || (inCDATA && mergeCDATA + && lastChild instanceof CDATASection) + ) { + CharacterData last = (CharacterData) lastChild; + String value = new String (ch, start, length); + + last.appendData (value); + return; + } + } + if (inCDATA && !consumer.isHidingCDATA ()) { + top.appendChild (createText (true, ch, start, length)); + mergeCDATA = true; + } else + top.appendChild (createText (false, ch, start, length)); + } + + // SAX2 + public void skippedEntity (String name) + throws SAXException + { + // this callback is useless except to report errors, since + // we can't know if the ref was in content, within an + // attribute, within a declaration ... only one of those + // cases supports more intelligent action than a panic. + fatal ("skipped entity: " + name, null); + } + + // SAX2 + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + // reconstruct "xmlns" attributes deleted by all + // SAX2 parsers without "namespace-prefixes" = true + if ("".equals (prefix)) + attributes.addAttribute ("", "", "xmlns", + "CDATA", uri); + else + attributes.addAttribute ("", "", "xmlns:" + prefix, + "CDATA", uri); + recreatedAttrs = true; + } + + // SAX2 + public void endPrefixMapping (String prefix) + throws SAXException + { } + + // SAX2 + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (currentEntity != null) + return; + + // parser discarded basic information; DOM tree isn't writable + // without massaging to assign prefixes to all nodes. + // the "NSFilter" class does that massaging. + if (qName.length () == 0) + qName = localName; + + + Element element; + int length = atts.getLength (); + + if (!isL2) { + element = document.createElement (qName); + + // first the explicit attributes ... + length = atts.getLength (); + for (int i = 0; i < length; i++) + element.setAttribute (atts.getQName (i), + atts.getValue (i)); + // ... then any recreated ones (DOM deletes duplicates) + if (recreatedAttrs) { + recreatedAttrs = false; + length = attributes.getLength (); + for (int i = 0; i < length; i++) + element.setAttribute (attributes.getQName (i), + attributes.getValue (i)); + attributes.clear (); + } + + top.appendChild (element); + top = element; + return; + } + + // For an L2 DOM when namespace use is enabled, use + // createElementNS/createAttributeNS except when + // (a) it's an element in the default namespace, or + // (b) it's an attribute with no prefix + String namespace; + + if (localName.length () != 0) + namespace = (uri.length () == 0) ? null : uri; + else + namespace = getNamespace (getPrefix (qName), atts); + + if (namespace == null) + element = document.createElement (qName); + else + element = document.createElementNS (namespace, qName); + + populateAttributes (element, atts); + if (recreatedAttrs) { + recreatedAttrs = false; + // ... DOM deletes any duplicates + populateAttributes (element, attributes); + attributes.clear (); + } + + top.appendChild (element); + top = element; + } + + final static String xmlnsURI = "http://www.w3.org/2000/xmlns/"; + + private void populateAttributes (Element element, Attributes attrs) + throws SAXParseException + { + int length = attrs.getLength (); + + for (int i = 0; i < length; i++) { + String type = attrs.getType (i); + String value = attrs.getValue (i); + String name = attrs.getQName (i); + String local = attrs.getLocalName (i); + String uri = attrs.getURI (i); + + // parser discarded basic information, DOM tree isn't writable + if (name.length () == 0) + name = local; + + // all attribute types other than these three may not + // contain scoped names... enumerated attributes get + // reported as NMTOKEN, except for NOTATION values + if (!("CDATA".equals (type) + || "NMTOKEN".equals (type) + || "NMTOKENS".equals (type))) { + if (value.indexOf (':') != -1) { + namespaceError ( + "namespace nonconformant attribute value: " + + "<" + element.getNodeName () + + " " + name + "='" + value + "' ...>"); + } + } + + // xmlns="" is legal (undoes default NS) + // xmlns:foo="" is illegal + String prefix = getPrefix (name); + String namespace; + + if ("xmlns".equals (prefix)) { + if ("".equals (value)) + namespaceError ("illegal null namespace decl, " + name); + namespace = xmlnsURI; + } else if ("xmlns".equals (name)) + namespace = xmlnsURI; + + else if (prefix == null) + namespace = null; + else if (!"".equals(uri) && uri.length () != 0) + namespace = uri; + else + namespace = getNamespace (prefix, attrs); + + if (namespace == null) + element.setAttribute (name, value); + else + element.setAttributeNS (namespace, name, value); + } + } + + private String getPrefix (String name) + { + int temp; + + if ((temp = name.indexOf (':')) > 0) + return name.substring (0, temp); + return null; + } + + // used with SAX1-level parser output + private String getNamespace (String prefix, Attributes attrs) + throws SAXParseException + { + String namespace; + String decl; + + // defaulting + if (prefix == null) { + decl = "xmlns"; + namespace = attrs.getValue (decl); + if ("".equals (namespace)) + return null; + else if (namespace != null) + return namespace; + + // "xmlns" is like a keyword + // ... according to the Namespace REC, but DOM L2 CR2+ + // and Infoset violate that by assigning a namespace. + // that conflict is resolved elsewhere. + } else if ("xmlns".equals (prefix)) + return null; + + // "xml" prefix is fixed + else if ("xml".equals (prefix)) + return "http://www.w3.org/XML/1998/namespace"; + + // otherwise, expect a declaration + else { + decl = "xmlns:" + prefix; + namespace = attrs.getValue (decl); + } + + // if we found a local declaration, great + if (namespace != null) + return namespace; + + + // ELSE ... search up the tree we've been building + for (Node n = top; + n != null && n.getNodeType () != Node.DOCUMENT_NODE; + n = (Node) n.getParentNode ()) { + if (n.getNodeType () == Node.ENTITY_REFERENCE_NODE) + continue; + Element e = (Element) n; + Attr attr = e.getAttributeNode (decl); + if (attr != null) + return attr.getNodeValue (); + } + // see above re "xmlns" as keyword + if ("xmlns".equals (decl)) + return null; + + namespaceError ("Undeclared namespace prefix: " + prefix); + return null; + } + + // SAX2 + public void endElement (String uri, String localName, String qName) + throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (currentEntity != null) + return; + + top = top.getParentNode (); + } + + // SAX1 (mandatory reporting if validating) + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + if (consumer.isHidingWhitespace ()) + return; + characters (ch, start, length); + } + + // SAX2 lexical event + public void startCDATA () + throws SAXException + { + inCDATA = true; + // true except for the first fragment of a cdata section + mergeCDATA = false; + } + + // SAX2 lexical event + public void endCDATA () + throws SAXException + { + inCDATA = false; + } + + // SAX2 lexical event + // + // this SAX2 callback merges two unrelated things: + // - Declaration of the root element type ... belongs with + // the other DTD declaration methods, NOT HERE. + // - IDs for the optional external subset ... belongs here + // with other lexical information. + // + // ...and it doesn't include the internal DTD subset, desired + // both to support DOM L2 and to enable "pass through" processing + // + public void startDTD (String name, String publicId, String SystemId) + throws SAXException + { + // need to filter out comments and PIs within the DTD + inDTD = true; + } + + // SAX2 lexical event + public void endDTD () + throws SAXException + { + inDTD = false; + } + + // SAX2 lexical event + public void comment (char ch [], int start, int length) + throws SAXException + { + Node comment; + + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (consumer.isHidingComments () + || inDTD + || currentEntity != null) + return; + comment = document.createComment (new String (ch, start, length)); + top.appendChild (comment); + } + + /** + * May be overridden by subclasses to return true, indicating + * that entity reference nodes can be populated and then made + * read-only. + */ + public boolean canPopulateEntityRefs () + { return false; } + + // SAX2 lexical event + public void startEntity (String name) + throws SAXException + { + // are we ignoring what would be contents of an + // entity ref, since we can't populate it? + if (currentEntity != null) + return; + + // Are we hiding all entity boundaries? + if (consumer.isHidingReferences ()) + return; + + // SAX2 shows parameter entities; DOM hides them + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + + // Since we can't create a populated entity ref node in any + // standard way, we create an unpopulated one. + EntityReference ref = document.createEntityReference (name); + top.appendChild (ref); + top = ref; + + // ... allowing subclasses to populate them + if (!canPopulateEntityRefs ()) + currentEntity = name; + } + + // SAX2 lexical event + public void endEntity (String name) + throws SAXException + { + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + if (name.equals (currentEntity)) + currentEntity = null; + if (!consumer.isHidingReferences ()) + top = top.getParentNode (); + } + + + // SAX1 DTD event + public void notationDecl ( + String name, + String publicId, String SystemId + ) throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // SAX1 DTD event + public void unparsedEntityDecl ( + String name, + String publicId, String SystemId, + String notationName + ) throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // SAX2 declaration event + public void elementDecl (String name, String model) + throws SAXException + { + /* IGNORE -- no content model support in DOM L2 */ + } + + // SAX2 declaration event + public void attributeDecl ( + String eName, + String aName, + String type, + String mode, + String value + ) throws SAXException + { + /* IGNORE -- no attribute model support in DOM L2 */ + } + + // SAX2 declaration event + public void internalEntityDecl (String name, String value) + throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // SAX2 declaration event + public void externalEntityDecl ( + String name, + String publicId, + String SystemId + ) throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // + // These really should offer the option of nonfatal handling, + // like other validity errors, though that would cause major + // chaos in the DOM data structures. DOM is already spec'd + // to treat many of these as fatal, so this is consistent. + // + private void namespaceError (String description) + throws SAXParseException + { + SAXParseException err; + + err = new SAXParseException (description, locator); + throw err; + } + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/EventConsumer.java b/libjava/classpath/gnu/xml/pipeline/EventConsumer.java new file mode 100644 index 00000000000..017bc7f74f1 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/EventConsumer.java @@ -0,0 +1,95 @@ +/* EventConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import org.xml.sax.*; + + +/** + * Collects the event consumption apparatus of a SAX pipeline stage. + * Consumers which permit some handlers or other characteristics to be + * configured will provide methods to support that configuration. + * + * <p> Two important categories of consumers include <em>filters</em>, which + * process events and pass them on to other consumers, and <em>terminus</em> + * (or <em>terminal</em>) stages, which don't pass events on. Filters are not + * necessarily derived from the {@link EventFilter} class, although that + * class can substantially simplify their construction by automating the + * most common activities. + * + * <p> Event consumers which follow certain conventions for the signatures + * of their constructors can be automatically assembled into pipelines + * by the {@link PipelineFactory} class. + * + * @author David Brownell + */ +public interface EventConsumer +{ + /** Most stages process these core SAX callbacks. */ + public ContentHandler getContentHandler (); + + /** Few stages will use unparsed entities. */ + public DTDHandler getDTDHandler (); + + /** + * This method works like the SAX2 XMLReader method of the same name, + * and is used to retrieve the optional lexical and declaration handlers + * in a pipeline. + * + * @param id This is a URI identifying the type of property desired. + * @return The value of that property, if it is defined. + * + * @exception SAXNotRecognizedException Thrown if the particular + * pipeline stage does not understand the specified identifier. + */ + public Object getProperty (String id) + throws SAXNotRecognizedException; + + /** + * This method provides a filter stage with a handler that abstracts + * presentation of warnings and both recoverable and fatal errors. + * Most pipeline stages should share a single policy and mechanism + * for such reports, since application components require consistency + * in such activities. Accordingly, typical responses to this method + * invocation involve saving the handler for use; filters will pass + * it on to any other consumers they use. + * + * @param handler encapsulates error handling policy for this stage + */ + public void setErrorHandler (ErrorHandler handler); +} diff --git a/libjava/classpath/gnu/xml/pipeline/EventFilter.java b/libjava/classpath/gnu/xml/pipeline/EventFilter.java new file mode 100644 index 00000000000..6600271718a --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/EventFilter.java @@ -0,0 +1,809 @@ +/* EventFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.xml.sax.*; +import org.xml.sax.ext.*; +import org.xml.sax.helpers.XMLFilterImpl; + +import gnu.xml.aelfred2.ContentHandler2; + +/** + * A customizable event consumer, used to assemble various kinds of filters + * using SAX handlers and an optional second consumer. It can be constructed + * in two ways: <ul> + * + * <li> To serve as a passthrough, sending all events to a second consumer. + * The second consumer may be identified through {@link #getNext}. + * + * <li> To serve as a dead end, with all handlers null; + * {@link #getNext} returns null. + * + * </ul> + * + * <p> Additionally, SAX handlers may be assigned, which completely replace + * the "upstream" view (through {@link EventConsumer}) of handlers, initially + * null or the "next" consumer provided to the constructor. To make + * it easier to build specialized filter classes, this class implements + * all the standard SAX consumer handlers, and those implementations + * delegate "downstream" to the consumer accessed by {@link #getNext}. + * + * <p> The simplest way to create a custom a filter class is to create a + * subclass which overrides one or more handler interface methods. The + * constructor for that subclass then registers itself as a handler for + * those interfaces using a call such as <em>setContentHandler(this)</em>, + * so the "upstream" view of event delivery is modified from the state + * established in the base class constructor. That way, + * the overridden methods intercept those event callbacks + * as they go "downstream", and + * all other event callbacks will pass events to any next consumer. + * Overridden methods may invoke superclass methods (perhaps after modifying + * parameters) if they wish to delegate such calls. Such subclasses + * should use {@link #getErrorHandler} to report errors using the + * common error reporting mechanism. + * + * <p> Another important technique is to construct a filter consisting + * of only a few specific types of handler. For example, one could easily + * prune out lexical events or various declarations by providing handlers + * which don't pass those events downstream, or by providing null handlers. + * + * <hr /> + * + * <p> This may be viewed as the consumer oriented analogue of the SAX2 + * {@link org.xml.sax.helpers.XMLFilterImpl XMLFilterImpl} class. + * Key differences include: <ul> + * + * <li> This fully separates consumer and producer roles: it + * does not implement the producer side <em>XMLReader</em> or + * <em>EntityResolver</em> interfaces, so it can only be used + * in "push" mode (it has no <em>parse()</em> methods). + * + * <li> "Extension" handlers are fully supported, enabling a + * richer set of application requirements. + * And it implements {@link EventConsumer}, which groups related + * consumer methods together, rather than leaving them separated. + * + * <li> The chaining which is visible is "downstream" to the next + * consumer, not "upstream" to the preceding producer. + * It supports "fan-in", where + * a consumer can be fed by several producers. (For "fan-out", + * see the {@link TeeConsumer} class.) + * + * <li> Event chaining is set up differently. It is intended to + * work "upstream" from terminus towards producer, during filter + * construction, as described above. + * This is part of an early binding model: + * events don't need to pass through stages which ignore them. + * + * <li> ErrorHandler support is separated, on the grounds that + * pipeline stages need to share the same error handling policy. + * For the same reason, error handler setup goes "downstream": + * when error handlers get set, they are passed to subsequent + * consumers. + * + * </ul> + * + * <p> The {@link #chainTo chainTo()} convenience routine supports chaining to + * an XMLFilterImpl, in its role as a limited functionality event + * consumer. Its event producer role ({@link XMLFilter}) is ignored. + * + * <hr /> + * + * <p> The {@link #bind bind()} routine may be used associate event pipelines + * with any kind of {@link XMLReader} that will produce the events. + * Such pipelines don't necessarily need to have any members which are + * implemented using this class. That routine has some intelligence + * which supports automatic changes to parser feature flags, letting + * event piplines become largely independent of the particular feature + * sets of parsers. + * + * @author David Brownell + */ +public class EventFilter + implements EventConsumer, ContentHandler2, DTDHandler, + LexicalHandler, DeclHandler +{ + // SAX handlers + private ContentHandler docHandler, docNext; + private DTDHandler dtdHandler, dtdNext; + private LexicalHandler lexHandler, lexNext; + private DeclHandler declHandler, declNext; + // and ideally, one more for the stuff SAX2 doesn't show + + private Locator locator; + private EventConsumer next; + private ErrorHandler errHandler; + + + /** SAX2 URI prefix for standard feature flags. */ + public static final String FEATURE_URI + = "http://xml.org/sax/features/"; + /** SAX2 URI prefix for standard properties (mostly for handlers). */ + public static final String PROPERTY_URI + = "http://xml.org/sax/properties/"; + + /** SAX2 property identifier for {@link DeclHandler} events */ + public static final String DECL_HANDLER + = PROPERTY_URI + "declaration-handler"; + /** SAX2 property identifier for {@link LexicalHandler} events */ + public static final String LEXICAL_HANDLER + = PROPERTY_URI + "lexical-handler"; + + // + // These class objects will be null if the relevant class isn't linked. + // Small configurations (pJava and some kinds of embedded systems) need + // to facilitate smaller executables. So "instanceof" is undesirable + // when bind() sees if it can remove some stages. + // + // SECURITY NOTE: assuming all these classes are part of the same sealed + // package, there's no problem saving these in the instance of this class + // that's associated with "this" class loader. But that wouldn't be true + // for classes in another package. + // + private static boolean loaded; + private static Class nsClass; + private static Class validClass; + private static Class wfClass; + private static Class xincClass; + + static ClassLoader getClassLoader () + { + Method m = null; + + try { + m = Thread.class.getMethod("getContextClassLoader", null); + } catch (NoSuchMethodException e) { + // Assume that we are running JDK 1.1, use the current ClassLoader + return EventFilter.class.getClassLoader(); + } + + try { + return (ClassLoader) m.invoke(Thread.currentThread(), null); + } catch (IllegalAccessException e) { + // assert(false) + throw new UnknownError(e.getMessage()); + } catch (InvocationTargetException e) { + // assert(e.getTargetException() instanceof SecurityException) + throw new UnknownError(e.getMessage()); + } + } + + static Class loadClass (ClassLoader classLoader, String className) + { + try { + if (classLoader == null) + return Class.forName(className); + else + return classLoader.loadClass(className); + } catch (Exception e) { + return null; + } + } + + static private void loadClasses () + { + ClassLoader loader = getClassLoader (); + + nsClass = loadClass (loader, "gnu.xml.pipeline.NSFilter"); + validClass = loadClass (loader, "gnu.xml.pipeline.ValidationConsumer"); + wfClass = loadClass (loader, "gnu.xml.pipeline.WellFormednessFilter"); + xincClass = loadClass (loader, "gnu.xml.pipeline.XIncludeFilter"); + loaded = true; + } + + + /** + * Binds the standard SAX2 handlers from the specified consumer + * pipeline to the specified producer. These handlers include the core + * {@link ContentHandler} and {@link DTDHandler}, plus the extension + * {@link DeclHandler} and {@link LexicalHandler}. Any additional + * application-specific handlers need to be bound separately. + * The {@link ErrorHandler} is handled differently: the producer's + * error handler is passed through to the consumer pipeline. + * The producer is told to include namespace prefix information if it + * can, since many pipeline stages need that Infoset information to + * work well. + * + * <p> At the head of the pipeline, certain standard event filters are + * recognized and handled specially. This facilitates construction + * of processing pipelines that work regardless of the capabilities + * of the XMLReader implementation in use; for example, it permits + * validating output of a {@link gnu.xml.util.DomParser}. <ul> + * + * <li> {@link NSFilter} will be removed if the producer can be + * told not to discard namespace data, using the "namespace-prefixes" + * feature flag. + * + * <li> {@link ValidationConsumer} will be removed if the producer + * can be told to validate, using the "validation" feature flag. + * + * <li> {@link WellFormednessFilter} is always removed, on the + * grounds that no XMLReader is permitted to producee malformed + * event streams and this would just be processing overhead. + * + * <li> {@link XIncludeFilter} stops the special handling, except + * that it's told about the "namespace-prefixes" feature of the + * event producer so that the event stream is internally consistent. + * + * <li> The first consumer which is not one of those classes stops + * such special handling. This means that if you want to force + * one of those filters to be used, you could just precede it with + * an instance of {@link EventFilter} configured as a pass-through. + * You might need to do that if you are using an {@link NSFilter} + * subclass to fix names found in attributes or character data. + * + * </ul> + * + * <p> Other than that, this method works with any kind of event consumer, + * not just event filters. Note that in all cases, the standard handlers + * are assigned; any previous handler assignments for the handler will + * be overridden. + * + * @param producer will deliver events to the specified consumer + * @param consumer pipeline supplying event handlers to be associated + * with the producer (may not be null) + */ + public static void bind (XMLReader producer, EventConsumer consumer) + { + Class klass = null; + boolean prefixes; + + if (!loaded) + loadClasses (); + + // DOM building, printing, layered validation, and other + // things don't work well when prefix info is discarded. + // Include it by default, whenever possible. + try { + producer.setFeature (FEATURE_URI + "namespace-prefixes", + true); + prefixes = true; + } catch (SAXException e) { + prefixes = false; + } + + // NOTE: This loop doesn't use "instanceof", since that + // would prevent compiling/linking without those classes + // being present. + while (consumer != null) { + klass = consumer.getClass (); + + // we might have already changed this problematic SAX2 default. + if (nsClass != null && nsClass.isAssignableFrom (klass)) { + if (!prefixes) + break; + consumer = ((EventFilter)consumer).getNext (); + + // the parser _might_ do DTD validation by default ... + // if not, maybe we can change this setting. + } else if (validClass != null + && validClass.isAssignableFrom (klass)) { + try { + producer.setFeature (FEATURE_URI + "validation", + true); + consumer = ((ValidationConsumer)consumer).getNext (); + } catch (SAXException e) { + break; + } + + // parsers are required not to have such bugs + } else if (wfClass != null && wfClass.isAssignableFrom (klass)) { + consumer = ((WellFormednessFilter)consumer).getNext (); + + // stop on the first pipeline stage we can't remove + } else + break; + + if (consumer == null) + klass = null; + } + + // the actual setting here doesn't matter as much + // as that producer and consumer agree + if (xincClass != null && klass != null + && xincClass.isAssignableFrom (klass)) + ((XIncludeFilter)consumer).setSavingPrefixes (prefixes); + + // Some SAX parsers can't handle null handlers -- bleech + DefaultHandler2 h = new DefaultHandler2 (); + + if (consumer != null && consumer.getContentHandler () != null) + producer.setContentHandler (consumer.getContentHandler ()); + else + producer.setContentHandler (h); + if (consumer != null && consumer.getDTDHandler () != null) + producer.setDTDHandler (consumer.getDTDHandler ()); + else + producer.setDTDHandler (h); + + try { + Object dh; + + if (consumer != null) + dh = consumer.getProperty (DECL_HANDLER); + else + dh = null; + if (dh == null) + dh = h; + producer.setProperty (DECL_HANDLER, dh); + } catch (Exception e) { /* ignore */ } + try { + Object lh; + + if (consumer != null) + lh = consumer.getProperty (LEXICAL_HANDLER); + else + lh = null; + if (lh == null) + lh = h; + producer.setProperty (LEXICAL_HANDLER, lh); + } catch (Exception e) { /* ignore */ } + + // this binding goes the other way around + if (producer.getErrorHandler () == null) + producer.setErrorHandler (h); + if (consumer != null) + consumer.setErrorHandler (producer.getErrorHandler ()); + } + + /** + * Initializes all handlers to null. + */ + // constructor used by PipelineFactory + public EventFilter () { } + + + /** + * Handlers that are not otherwise set will default to those from + * the specified consumer, making it easy to pass events through. + * If the consumer is null, all handlers are initialzed to null. + */ + // constructor used by PipelineFactory + public EventFilter (EventConsumer consumer) + { + if (consumer == null) + return; + + next = consumer; + + // We delegate through the "xxNext" handlers, and + // report the "xxHandler" ones on our input side. + + // Normally a subclass would both override handler + // methods and register itself as the "xxHandler". + + docHandler = docNext = consumer.getContentHandler (); + dtdHandler = dtdNext = consumer.getDTDHandler (); + try { + declHandler = declNext = (DeclHandler) + consumer.getProperty (DECL_HANDLER); + } catch (SAXException e) { /* leave value null */ } + try { + lexHandler = lexNext = (LexicalHandler) + consumer.getProperty (LEXICAL_HANDLER); + } catch (SAXException e) { /* leave value null */ } + } + + /** + * Treats the XMLFilterImpl as a limited functionality event consumer, + * by arranging to deliver events to it; this lets such classes be + * "wrapped" as pipeline stages. + * + * <p> <em>Upstream Event Setup:</em> + * If no handlers have been assigned to this EventFilter, then the + * handlers from specified XMLFilterImpl are returned from this + * {@link EventConsumer}: the XMLFilterImpl is just "wrapped". + * Otherwise the specified handlers will be returned. + * + * <p> <em>Downstream Event Setup:</em> + * Subclasses may chain event delivery to the specified XMLFilterImpl + * by invoking the appropiate superclass methods, + * as if their constructor passed a "next" EventConsumer to the + * constructor for this class. + * If this EventFilter has an ErrorHandler, it is assigned as + * the error handler for the XMLFilterImpl, just as would be + * done for a next stage implementing {@link EventConsumer}. + * + * @param next the next downstream component of the pipeline. + * @exception IllegalStateException if the "next" consumer has + * already been set through the constructor. + */ + public void chainTo (XMLFilterImpl next) + { + if (this.next != null) + throw new IllegalStateException (); + + docNext = next.getContentHandler (); + if (docHandler == null) + docHandler = docNext; + dtdNext = next.getDTDHandler (); + if (dtdHandler == null) + dtdHandler = dtdNext; + + try { + declNext = (DeclHandler) next.getProperty (DECL_HANDLER); + if (declHandler == null) + declHandler = declNext; + } catch (SAXException e) { /* leave value null */ } + try { + lexNext = (LexicalHandler) next.getProperty (LEXICAL_HANDLER); + if (lexHandler == null) + lexHandler = lexNext; + } catch (SAXException e) { /* leave value null */ } + + if (errHandler != null) + next.setErrorHandler (errHandler); + } + + /** + * Records the error handler that should be used by this stage, and + * passes it "downstream" to any subsequent stage. + */ + final public void setErrorHandler (ErrorHandler handler) + { + errHandler = handler; + if (next != null) + next.setErrorHandler (handler); + } + + /** + * Returns the error handler assigned this filter stage, or null + * if no such assigment has been made. + */ + final public ErrorHandler getErrorHandler () + { + return errHandler; + } + + + /** + * Returns the next event consumer in sequence; or null if there + * is no such handler. + */ + final public EventConsumer getNext () + { return next; } + + + /** + * Assigns the content handler to use; a null handler indicates + * that these events will not be forwarded. + * This overrides the previous settting for this handler, which was + * probably pointed to the next consumer by the base class constructor. + */ + final public void setContentHandler (ContentHandler h) + { + docHandler = h; + } + + /** Returns the content handler being used. */ + final public ContentHandler getContentHandler () + { + return docHandler; + } + + /** + * Assigns the DTD handler to use; a null handler indicates + * that these events will not be forwarded. + * This overrides the previous settting for this handler, which was + * probably pointed to the next consumer by the base class constructor. + */ + final public void setDTDHandler (DTDHandler h) + { dtdHandler = h; } + + /** Returns the dtd handler being used. */ + final public DTDHandler getDTDHandler () + { + return dtdHandler; + } + + /** + * Stores the property, normally a handler; a null handler indicates + * that these events will not be forwarded. + * This overrides the previous handler settting, which was probably + * pointed to the next consumer by the base class constructor. + */ + final public void setProperty (String id, Object o) + throws SAXNotRecognizedException, SAXNotSupportedException + { + try { + Object value = getProperty (id); + + if (value == o) + return; + if (DECL_HANDLER.equals (id)) { + declHandler = (DeclHandler) o; + return; + } + if (LEXICAL_HANDLER.equals (id)) { + lexHandler = (LexicalHandler) o; + return; + } + throw new SAXNotSupportedException (id); + + } catch (ClassCastException e) { + throw new SAXNotSupportedException (id); + } + } + + /** Retrieves a property of unknown intent (usually a handler) */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + if (DECL_HANDLER.equals (id)) + return declHandler; + if (LEXICAL_HANDLER.equals (id)) + return lexHandler; + + throw new SAXNotRecognizedException (id); + } + + /** + * Returns any locator provided to the next consumer, if this class + * (or a subclass) is handling {@link ContentHandler } events. + */ + public Locator getDocumentLocator () + { return locator; } + + + // CONTENT HANDLER DELEGATIONS + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void setDocumentLocator (Locator locator) + { + this.locator = locator; + if (docNext != null) + docNext.setDocumentLocator (locator); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void startDocument () throws SAXException + { + if (docNext != null) + docNext.startDocument (); + } + + public void xmlDecl(String version, String encoding, boolean standalone, + String inputEncoding) + throws SAXException + { + if (docNext != null && docNext instanceof ContentHandler2) + { + ((ContentHandler2) docNext).xmlDecl(version, encoding, standalone, + inputEncoding); + } + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void skippedEntity (String name) throws SAXException + { + if (docNext != null) + docNext.skippedEntity (name); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void processingInstruction (String target, String data) + throws SAXException + { + if (docNext != null) + docNext.processingInstruction (target, data); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void characters (char ch [], int start, int length) + throws SAXException + { + if (docNext != null) + docNext.characters (ch, start, length); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + if (docNext != null) + docNext.ignorableWhitespace (ch, start, length); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (docNext != null) + docNext.startPrefixMapping (prefix, uri); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void startElement ( + String uri, String localName, + String qName, Attributes atts + ) throws SAXException + { + if (docNext != null) + docNext.startElement (uri, localName, qName, atts); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void endElement (String uri, String localName, String qName) + throws SAXException + { + if (docNext != null) + docNext.endElement (uri, localName, qName); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void endPrefixMapping (String prefix) throws SAXException + { + if (docNext != null) + docNext.endPrefixMapping (prefix); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void endDocument () throws SAXException + { + if (docNext != null) + docNext.endDocument (); + locator = null; + } + + + // DTD HANDLER DELEGATIONS + + /** <b>SAX1:</b> passes this callback to the next consumer, if any */ + public void unparsedEntityDecl ( + String name, + String publicId, + String systemId, + String notationName + ) throws SAXException + { + if (dtdNext != null) + dtdNext.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + /** <b>SAX1:</b> passes this callback to the next consumer, if any */ + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + if (dtdNext != null) + dtdNext.notationDecl (name, publicId, systemId); + } + + + // LEXICAL HANDLER DELEGATIONS + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (lexNext != null) + lexNext.startDTD (name, publicId, systemId); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void endDTD () + throws SAXException + { + if (lexNext != null) + lexNext.endDTD (); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void comment (char ch [], int start, int length) + throws SAXException + { + if (lexNext != null) + lexNext.comment (ch, start, length); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void startCDATA () + throws SAXException + { + if (lexNext != null) + lexNext.startCDATA (); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void endCDATA () + throws SAXException + { + if (lexNext != null) + lexNext.endCDATA (); + } + + /** + * <b>SAX2:</b> passes this callback to the next consumer, if any. + */ + public void startEntity (String name) + throws SAXException + { + if (lexNext != null) + lexNext.startEntity (name); + } + + /** + * <b>SAX2:</b> passes this callback to the next consumer, if any. + */ + public void endEntity (String name) + throws SAXException + { + if (lexNext != null) + lexNext.endEntity (name); + } + + + // DECLARATION HANDLER DELEGATIONS + + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void elementDecl (String name, String model) + throws SAXException + { + if (declNext != null) + declNext.elementDecl (name, model); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void attributeDecl (String eName, String aName, + String type, String mode, String value) + throws SAXException + { + if (declNext != null) + declNext.attributeDecl (eName, aName, type, mode, value); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + if (declNext != null) + declNext.externalEntityDecl (name, publicId, systemId); + } + + /** <b>SAX2:</b> passes this callback to the next consumer, if any */ + public void internalEntityDecl (String name, String value) + throws SAXException + { + if (declNext != null) + declNext.internalEntityDecl (name, value); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/LinkFilter.java b/libjava/classpath/gnu/xml/pipeline/LinkFilter.java new file mode 100644 index 00000000000..ddb9fda2e75 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/LinkFilter.java @@ -0,0 +1,242 @@ +/* LinkFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + + +/** + * Pipeline filter to remember XHTML links found in a document, + * so they can later be crawled. Fragments are not counted, and duplicates + * are ignored. Callers are responsible for filtering out URLs they aren't + * interested in. Events are passed through unmodified. + * + * <p> Input MUST include a setDocumentLocator() call, as it's used to + * resolve relative links in the absence of a "base" element. Input MUST + * also include namespace identifiers, since it is the XHTML namespace + * identifier which is used to identify the relevant elements. + * + * <p><em>FIXME:</em> handle xml:base attribute ... in association with + * a stack of base URIs. Similarly, recognize/support XLink data. + * + * @author David Brownell + */ +public class LinkFilter extends EventFilter +{ + // for storing URIs + private Vector vector = new Vector (); + + // struct for "full" link record (tbd) + // these for troubleshooting original source: + // original uri + // uri as resolved (base, relative, etc) + // URI of originating doc + // line # + // original element + attrs (img src, desc, etc) + + // XLink model of the link ... for inter-site pairups ? + + private String baseURI; + + private boolean siteRestricted = false; + + // + // XXX leverage blacklist info (like robots.txt) + // + // XXX constructor w/param ... pipeline for sending link data + // probably XHTML --> XLink, providing info as sketched above + // + + + /** + * Constructs a new event filter, which collects links in private data + * structure for later enumeration. + */ + // constructor used by PipelineFactory + public LinkFilter () + { + super.setContentHandler (this); + } + + + /** + * Constructs a new event filter, which collects links in private data + * structure for later enumeration and passes all events, unmodified, + * to the next consumer. + */ + // constructor used by PipelineFactory + public LinkFilter (EventConsumer next) + { + super (next); + super.setContentHandler (this); + } + + + /** + * Returns an enumeration of the links found since the filter + * was constructed, or since removeAllLinks() was called. + * + * @return enumeration of strings. + */ + public Enumeration getLinks () + { + return vector.elements (); + } + + /** + * Removes records about all links reported to the event + * stream, as if the filter were newly created. + */ + public void removeAllLinks () + { + vector = new Vector (); + } + + + /** + * Collects URIs for (X)HTML content from elements which hold them. + */ + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + String link; + + // Recognize XHTML links. + if ("http://www.w3.org/1999/xhtml".equals (uri)) { + + if ("a".equals (localName) || "base".equals (localName) + || "area".equals (localName)) + link = atts.getValue ("href"); + else if ("iframe".equals (localName) || "frame".equals (localName)) + link = atts.getValue ("src"); + else if ("blockquote".equals (localName) || "q".equals (localName) + || "ins".equals (localName) || "del".equals (localName)) + link = atts.getValue ("cite"); + else + link = null; + link = maybeAddLink (link); + + // "base" modifies designated baseURI + if ("base".equals (localName) && link != null) + baseURI = link; + + if ("iframe".equals (localName) || "img".equals (localName)) + maybeAddLink (atts.getValue ("longdesc")); + } + + super.startElement (uri, localName, qName, atts); + } + + private String maybeAddLink (String link) + { + int index; + + // ignore empty links and fragments inside docs + if (link == null) + return null; + if ((index = link.indexOf ("#")) >= 0) + link = link.substring (0, index); + if (link.equals ("")) + return null; + + try { + // get the real URI + URL base = new URL ((baseURI != null) + ? baseURI + : getDocumentLocator ().getSystemId ()); + URL url = new URL (base, link); + + link = url.toString (); + + // ignore duplicates + if (vector.contains (link)) + return link; + + // other than what "base" does, stick to original site: + if (siteRestricted) { + // don't switch protocols + if (!base.getProtocol ().equals (url.getProtocol ())) + return link; + // don't switch servers + if (base.getHost () != null + && !base.getHost ().equals (url.getHost ())) + return link; + } + + vector.addElement (link); + + return link; + + } catch (IOException e) { + // bad URLs we don't want + } + return null; + } + + /** + * Reports an error if no Locator has been made available. + */ + public void startDocument () + throws SAXException + { + if (getDocumentLocator () == null) + throw new SAXException ("no Locator!"); + } + + /** + * Forgets about any base URI information that may be recorded. + * Applications will often want to call removeAllLinks(), likely + * after examining the links which were reported. + */ + public void endDocument () + throws SAXException + { + baseURI = null; + super.endDocument (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/NSFilter.java b/libjava/classpath/gnu/xml/pipeline/NSFilter.java new file mode 100644 index 00000000000..db875e1debf --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/NSFilter.java @@ -0,0 +1,341 @@ +/* NSFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.util.Enumeration; +import java.util.Stack; + +import org.xml.sax.Attributes; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.NamespaceSupport; + +/** + * This filter ensures that element and attribute names are properly prefixed, + * and that such prefixes are declared. Such data is critical for operations + * like writing XML text, and validating against DTDs: names or their prefixes + * may have been discarded, although they are essential to the exchange of + * information using XML. There are various common ways that such data + * gets discarded: <ul> + * + * <li> By default, SAX2 parsers must discard the "xmlns*" + * attributes, and may also choose not to report properly prefixed + * names for elements or attributes. (Some parsers may support + * changing the <em>namespace-prefixes</em> value from the default + * to <em>true</em>, effectively eliminating the need to use this + * filter on their output.) + * + * <li> When event streams are generated from a DOM tree, they may + * have never have had prefixes or declarations for namespaces; or + * the existing prefixes or declarations may have been invalidated + * by structural modifications to that DOM tree. + * + * <li> Other software writing SAX event streams won't necessarily + * be worrying about prefix management, and so they will need to + * have a transparent solution for managing them. + * + * </ul> + * + * <p> This filter uses a heuristic to choose the prefix to assign to any + * particular name which wasn't already corectly prefixed. The associated + * namespace will be correct, and the prefix will be declared. Original + * structures facilitating text editing, such as conventions about use of + * mnemonic prefix names or the scoping of prefixes, can't always be + * reconstructed after they are discarded, as strongly encouraged by the + * current SAX2 defaults. + * + * <p> Note that this can't possibly know whether values inside attribute + * value or document content involve prefixed names. If your application + * requires using prefixed names in such locations you'll need to add some + * appropriate logic (perhaps adding additional heuristics in a subclass). + * + * @author David Brownell + */ +public class NSFilter extends EventFilter +{ + private NamespaceSupport nsStack = new NamespaceSupport (); + private Stack elementStack = new Stack (); + + private boolean pushedContext; + private String nsTemp [] = new String [3]; + private AttributesImpl attributes = new AttributesImpl (); + private boolean usedDefault; + + // gensymmed prefixes use this root name + private static final String prefixRoot = "prefix-"; + + + /** + * Passes events through to the specified consumer, after first + * processing them. + * + * @param next the next event consumer to receive events. + */ + // constructor used by PipelineFactory + public NSFilter (EventConsumer next) + { + super (next); + + setContentHandler (this); + } + + private void fatalError (String message) + throws SAXException + { + SAXParseException e; + ErrorHandler handler = getErrorHandler (); + Locator locator = getDocumentLocator (); + + if (locator == null) + e = new SAXParseException (message, null, null, -1, -1); + else + e = new SAXParseException (message, locator); + if (handler != null) + handler.fatalError (e); + throw e; + } + + + public void startDocument () throws SAXException + { + elementStack.removeAllElements (); + nsStack.reset (); + pushedContext = false; + super.startDocument (); + } + + /** + * This call is not passed to the next consumer in the chain. + * Prefix declarations and scopes are only exposed in the form + * of attributes; this callback just records a declaration that + * will be exposed as an attribute. + */ + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (pushedContext == false) { + nsStack.pushContext (); + pushedContext = true; + } + + // this check is awkward, but the paranoia prevents big trouble + for (Enumeration e = nsStack.getDeclaredPrefixes (); + e.hasMoreElements (); + /* NOP */ ) { + String declared = (String) e.nextElement (); + + if (!declared.equals (prefix)) + continue; + if (uri.equals (nsStack.getURI (prefix))) + return; + fatalError ("inconsistent binding for prefix '" + prefix + + "' ... " + uri + " (was " + nsStack.getURI (prefix) + ")"); + } + + if (!nsStack.declarePrefix (prefix, uri)) + fatalError ("illegal prefix declared: " + prefix); + } + + private String fixName (String ns, String l, String name, boolean isAttr) + throws SAXException + { + if ("".equals (name) || name == null) { + name = l; + if ("".equals (name) || name == null) + fatalError ("empty/null name"); + } + + // can we correctly process the name as-is? + // handles "element scope" attribute names here. + if (nsStack.processName (name, nsTemp, isAttr) != null + && nsTemp [0].equals (ns) + ) { + return nsTemp [2]; + } + + // nope, gotta modify the name or declare a default mapping + int temp; + + // get rid of any current prefix + if ((temp = name.indexOf (':')) >= 0) { + name = name.substring (temp + 1); + + // ... maybe that's enough (use/prefer default namespace) ... + if (!isAttr && nsStack.processName (name, nsTemp, false) != null + && nsTemp [0].equals (ns) + ) { + return nsTemp [2]; + } + } + + // must we define and use the default/undefined prefix? + if ("".equals (ns)) { + if (isAttr) + fatalError ("processName bug"); + if (attributes.getIndex ("xmlns") != -1) + fatalError ("need to undefine default NS, but it's bound: " + + attributes.getValue ("xmlns")); + + nsStack.declarePrefix ("", ""); + attributes.addAttribute ("", "", "xmlns", "CDATA", ""); + return name; + } + + // is there at least one non-null prefix we can use? + for (Enumeration e = nsStack.getDeclaredPrefixes (); + e.hasMoreElements (); + /* NOP */) { + String prefix = (String) e.nextElement (); + String uri = nsStack.getURI (prefix); + + if (uri == null || !uri.equals (ns)) + continue; + return prefix + ":" + name; + } + + // no such luck. create a prefix name, declare it, use it. + for (temp = 0; temp >= 0; temp++) { + String prefix = prefixRoot + temp; + + if (nsStack.getURI (prefix) == null) { + nsStack.declarePrefix (prefix, ns); + attributes.addAttribute ("", "", "xmlns:" + prefix, + "CDATA", ns); + return prefix + ":" + name; + } + } + fatalError ("too many prefixes genned"); + // NOTREACHED + return null; + } + + public void startElement ( + String uri, String localName, + String qName, Attributes atts + ) throws SAXException + { + if (!pushedContext) + nsStack.pushContext (); + pushedContext = false; + + // make sure we have all NS declarations handy before we start + int length = atts.getLength (); + + for (int i = 0; i < length; i++) { + String aName = atts.getQName (i); + + if (!aName.startsWith ("xmlns")) + continue; + + String prefix; + + if ("xmlns".equals (aName)) + prefix = ""; + else if (aName.indexOf (':') == 5) + prefix = aName.substring (6); + else // "xmlnsfoo" etc. + continue; + startPrefixMapping (prefix, atts.getValue (i)); + } + + // put namespace decls at the start of our regenned attlist + attributes.clear (); + for (Enumeration e = nsStack.getDeclaredPrefixes (); + e.hasMoreElements (); + /* NOP */) { + String prefix = (String) e.nextElement (); + + attributes.addAttribute ("", "", + ("".equals (prefix) + ? "xmlns" + : "xmlns:" + prefix), + "CDATA", + nsStack.getURI (prefix)); + } + + // name fixups: element, then attributes. + // fixName may declare a new prefix or, for the element, + // redeclare the default (if element name needs it). + qName = fixName (uri, localName, qName, false); + + for (int i = 0; i < length; i++) { + String aName = atts.getQName (i); + String aNS = atts.getURI (i); + String aLocal = atts.getLocalName (i); + String aType = atts.getType (i); + String aValue = atts.getValue (i); + + if (aName.startsWith ("xmlns")) + continue; + aName = fixName (aNS, aLocal, aName, true); + attributes.addAttribute (aNS, aLocal, aName, aType, aValue); + } + + elementStack.push (qName); + + // pass event along, with cleaned-up names and decls. + super.startElement (uri, localName, qName, attributes); + } + + public void endElement (String uri, String localName, String qName) + throws SAXException + { + nsStack.popContext (); + qName = (String) elementStack.pop (); + super.endElement (uri, localName, qName); + } + + /** + * This call is not passed to the next consumer in the chain. + * Prefix declarations and scopes are only exposed in their + * attribute form. + */ + public void endPrefixMapping (String prefix) + throws SAXException + { } + + public void endDocument () throws SAXException + { + elementStack.removeAllElements (); + nsStack.reset (); + super.endDocument (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java b/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java new file mode 100644 index 00000000000..f88ab164311 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java @@ -0,0 +1,723 @@ +/* PipelineFactory.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.reflect.Constructor; +import java.util.StringTokenizer; + +import org.xml.sax.*; +import org.xml.sax.ext.*; + + +/** + * This provides static factory methods for creating simple event pipelines. + * These pipelines are specified by strings, suitable for passing on + * command lines or embedding in element attributes. For example, one way + * to write a pipeline that restores namespace syntax, validates (stopping + * the pipeline on validity errors) and then writes valid data to standard + * output is this: <pre> + * nsfix | validate | write ( stdout )</pre> + * + * <p> In this syntax, the tokens are always separated by whitespace, and each + * stage of the pipeline may optionally have a parameter (which can be a + * pipeline) in parentheses. Interior stages are called filters, and the + * rightmost end of a pipeline is called a terminus. + * + * <p> Stages are usually implemented by a single class, which may not be + * able to act as both a filter and a terminus; but any terminus can be + * automatically turned into a filter, through use of a {@link TeeConsumer}. + * The stage identifiers are either class names, or are one of the following + * short identifiers built into this class. (Most of these identifiers are + * no more than aliases for classes.) The built-in identifiers include:</p> + + <table border="1" cellpadding="3" cellspacing="0"> + <tr bgcolor="#ccccff" class="TableHeadingColor"> + <th align="center" width="5%">Stage</th> + <th align="center" width="9%">Parameter</th> + <th align="center" width="1%">Terminus</th> + <th align="center">Description</th> + </tr> + + <tr valign="top" align="center"> + <td><a href="../dom/Consumer.html">dom</a></td> + <td><em>none</em></td> + <td> yes </td> + <td align="left"> Applications code can access a DOM Document built + from the input event stream. When used as a filter, this buffers + data up to an <em>endDocument</em> call, and then uses a DOM parser + to report everything that has been recorded (which can easily be + less than what was reported to it). </td> + </tr> + <tr valign="top" align="center"> + <td><a href="NSFilter.html">nsfix</a></td> + <td><em>none</em></td> + <td>no</td> + <td align="left">This stage ensures that the XML element and attribute + names in its output use namespace prefixes and declarations correctly. + That is, so that they match the "Namespace plus LocalName" naming data + with which each XML element and attribute is already associated. </td> + </tr> + <tr valign="top" align="center"> + <td><a href="EventFilter.html">null</a></td> + <td><em>none</em></td> + <td>yes</td> + <td align="left">This stage ignores all input event data.</td> + </tr> + <tr valign="top" align="center"> + <td><a href="CallFilter.html">server</a></td> + <td><em>required</em><br> server URL </td> + <td>no</td> + <td align="left">Sends its input as XML request to a remote server, + normally a web application server using the HTTP or HTTPS protocols. + The output of this stage is the parsed response from that server.</td> + </tr> + <tr valign="top" align="center"> + <td><a href="TeeConsumer.html">tee</a></td> + <td><em>required</em><br> first pipeline</td> + <td>no</td> + <td align="left">This sends its events down two paths; its parameter + is a pipeline descriptor for the first path, and the second path + is the output of this stage.</td> + </tr> + + <tr valign="top" align="center"> + <td><a href="ValidationConsumer.html">validate</a></td> + <td><em>none</em></td> + <td>yes</td> + <td align="left">This checks for validity errors, and reports them + through its error handler. The input must include declaration events + and some lexical events. </td> + </tr> + <tr valign="top" align="center"> + <td><a href="WellFormednessFilter.html">wf</a></td> + <td><em>none</em></td> + <td>yes</td> + <td align="left"> This class provides some basic "well formedness" + tests on the input event stream, and reports a fatal error if any + of them fail. One example: start/end calls for elements must match. + No SAX parser is permitted to produce malformed output, but other + components can easily do so.</td> + </tr> + <tr valign="top" align="center"> + <td>write</td> + <td><em>required</em><br> "stdout", "stderr", or filename</td> + <td>yes</td> + <td align="left"> Writes its input to the specified output, as pretty + printed XML text encoded using UTF-8. Input events must be well + formed and "namespace fixed", else the output won't be XML (or possibly + namespace) conformant. The symbolic names represent + <em>System.out</em> and <em>System.err</em> respectively; names must + correspond to files which don't yet exist.</td> + </tr> + <tr valign="top" align="center"> + <td>xhtml</td> + <td><em>required</em><br> "stdout", "stderr", or filename</td> + <td>yes</td> + <td align="left"> Like <em>write</em> (above), except that XHTML rules + are followed. The XHTML 1.0 Transitional document type is declared, + and only ASCII characters are written (for interoperability). Other + characters are written as entity or character references; the text is + pretty printed.</td> + </tr> + <tr valign="top" align="center"> + <td><a href="XIncludeFilter.html">xinclude</a></td> + <td><em>none</em></td> + <td>no</td> + <td align="left">This stage handles XInclude processing. + This is like entity inclusion, except that the included content + is declared in-line rather than in the DTD at the beginning of + a document. + </td> + </tr> + <tr valign="top" align="center"> + <td><a href="XsltFilter.html">xslt</a></td> + <td><em>required</em><br> XSLT stylesheet URI</td> + <td>no</td> + <td align="left">This stage handles XSLT transformation + according to a stylesheet. + The implementation of the transformation may not actually + stream data, although if such an XSLT engine is in use + then that can happen. + </td> + </tr> + + </table> + + * <p> Note that {@link EventFilter#bind} can automatically eliminate + * some filters by setting SAX2 parser features appropriately. This means + * that you can routinely put filters like "nsfix", "validate", or "wf" at the + * front of a pipeline (for components that need inputs conditioned to match + * that level of correctness), and know that it won't actually be used unless + * it's absolutely necessary. + * + * @author David Brownell + */ +public class PipelineFactory +{ + /** + * Creates a simple pipeline according to the description string passed in. + */ + public static EventConsumer createPipeline (String description) + throws IOException + { + return createPipeline (description, null); + } + + /** + * Extends an existing pipeline by prepending the filter pipeline to the + * specified consumer. Some pipelines need more customization than can + * be done through this simplified syntax. When they are set up with + * direct API calls, use this method to merge more complex pipeline + * segments with easily configured ones. + */ + public static EventConsumer createPipeline ( + String description, + EventConsumer next + ) throws IOException + { + // tokens are (for now) what's separated by whitespace; + // very easy to parse, but IDs never have spaces. + + StringTokenizer tokenizer; + String tokens []; + + tokenizer = new StringTokenizer (description); + tokens = new String [tokenizer.countTokens ()]; + for (int i = 0; i < tokens.length; i++) + tokens [i] = tokenizer.nextToken (); + + PipelineFactory factory = new PipelineFactory (); + Pipeline pipeline = factory.parsePipeline (tokens, next); + + return pipeline.createPipeline (); + } + + + private PipelineFactory () { /* NYET */ } + + + /** + * Extends an existing pipeline by prepending a pre-tokenized filter + * pipeline to the specified consumer. Tokens are class names (or the + * predefined aliases) left and right parenthesis, and the vertical bar. + */ + public static EventConsumer createPipeline ( + String tokens [], + EventConsumer next + ) throws IOException + { + PipelineFactory factory = new PipelineFactory (); + Pipeline pipeline = factory.parsePipeline (tokens, next); + + return pipeline.createPipeline (); + } + + + private String tokens []; + private int index; + + private Pipeline parsePipeline (String toks [], EventConsumer next) + { + tokens = toks; + index = 0; + + Pipeline retval = parsePipeline (next); + + if (index != toks.length) + throw new ArrayIndexOutOfBoundsException ( + "extra token: " + tokens [index]); + return retval; + } + + // pipeline ::= stage | stage '|' pipeline + private Pipeline parsePipeline (EventConsumer next) + { + Pipeline retval = new Pipeline (parseStage ()); + + // minimal pipelines: "stage" and "... | id" + if (index > (tokens.length - 2) + || !"|".equals (tokens [index]) + ) { + retval.next = next; + return retval; + } + index++; + retval.rest = parsePipeline (next); + return retval; + } + + // stage ::= id | id '(' pipeline ')' + private Stage parseStage () + { + Stage retval = new Stage (tokens [index++]); + + // minimal stages: "id" and "id ( id )" + if (index > (tokens.length - 2) + || !"(".equals (tokens [index]) /*)*/ + ) + return retval; + + index++; + retval.param = parsePipeline (null); + if (index >= tokens.length) + throw new ArrayIndexOutOfBoundsException ( + "missing right paren"); + if (/*(*/ !")".equals (tokens [index++])) + throw new ArrayIndexOutOfBoundsException ( + "required right paren, not: " + tokens [index - 1]); + return retval; + } + + + // + // these classes obey the conventions for constructors, so they're + // only built in to this table of shortnames + // + // - filter (one or two types of arglist) + // * last constructor is 'next' element + // * optional (first) string parameter + // + // - terminus (one or types of arglist) + // * optional (only) string parameter + // + // terminus stages are transformed into filters if needed, by + // creating a "tee". filter stages aren't turned to terminus + // stages though; either eliminate such stages, or add some + // terminus explicitly. + // + private static final String builtinStages [][] = { + { "dom", "gnu.xml.dom.Consumer" }, + { "nsfix", "gnu.xml.pipeline.NSFilter" }, + { "null", "gnu.xml.pipeline.EventFilter" }, + { "server", "gnu.xml.pipeline.CallFilter" }, + { "tee", "gnu.xml.pipeline.TeeConsumer" }, + { "validate", "gnu.xml.pipeline.ValidationConsumer" }, + { "wf", "gnu.xml.pipeline.WellFormednessFilter" }, + { "xinclude", "gnu.xml.pipeline.XIncludeFilter" }, + { "xslt", "gnu.xml.pipeline.XsltFilter" }, + +// XXX want: option for validate, to preload external part of a DTD + + // xhtml, write ... nyet generic-ready + }; + + private static class Stage + { + String id; + Pipeline param; + + Stage (String name) + { id = name; } + + public String toString () + { + if (param == null) + return id; + return id + " ( " + param + " )"; + } + + private void fail (String message) + throws IOException + { + throw new IOException ("in '" + id + + "' stage of pipeline, " + message); + } + + EventConsumer createStage (EventConsumer next) + throws IOException + { + String name = id; + + // most builtins are just class aliases + for (int i = 0; i < builtinStages.length; i++) { + if (id.equals (builtinStages [i][0])) { + name = builtinStages [i][1]; + break; + } + } + + // Save output as XML or XHTML text + if ("write".equals (name) || "xhtml".equals (name)) { + String filename; + boolean isXhtml = "xhtml".equals (name); + OutputStream out = null; + TextConsumer consumer; + + if (param == null) + fail ("parameter is required"); + + filename = param.toString (); + if ("stdout".equals (filename)) + out = System.out; + else if ("stderr".equals (filename)) + out = System.err; + else { + File f = new File (filename); + +/* + if (!f.isAbsolute ()) + fail ("require absolute file paths"); + */ + if (f.exists ()) + fail ("file already exists: " + f.getName ()); + +// XXX this races against the existence test + out = new FileOutputStream (f); + } + + if (!isXhtml) + consumer = new TextConsumer (out); + else + consumer = new TextConsumer ( + new OutputStreamWriter (out, "8859_1"), + true); + + consumer.setPrettyPrinting (true); + if (next == null) + return consumer; + return new TeeConsumer (consumer, next); + + } else { + // + // Here go all the builtins that are just aliases for + // classes, and all stage IDs that started out as such + // class names. The following logic relies on several + // documented conventions for constructor invocation. + // + String msg = null; + + try { + Class klass = Class.forName (name); + Class argTypes [] = null; + Constructor constructor = null; + boolean filter = false; + Object params [] = null; + Object obj = null; + + // do we need a filter stage? + if (next != null) { + // "next" consumer is always passed, with + // or without the optional string param + if (param == null) { + argTypes = new Class [1]; + argTypes [0] = EventConsumer.class; + + params = new Object [1]; + params [0] = next; + + msg = "no-param filter"; + } else { + argTypes = new Class [2]; + argTypes [0] = String.class; + argTypes [1] = EventConsumer.class; + + params = new Object [2]; + params [0] = param.toString (); + params [1] = next; + + msg = "one-param filter"; + } + + + try { + constructor = klass.getConstructor (argTypes); + } catch (NoSuchMethodException e) { + // try creating a filter from a + // terminus and a tee + filter = true; + msg += " built from "; + } + } + + // build from a terminus stage, with or + // without the optional string param + if (constructor == null) { + String tmp; + + if (param == null) { + argTypes = new Class [0]; + params = new Object [0]; + + tmp = "no-param terminus"; + } else { + argTypes = new Class [1]; + argTypes [0] = String.class; + + params = new Object [1]; + params [0] = param.toString (); + + tmp = "one-param terminus"; + } + if (msg == null) + msg = tmp; + else + msg += tmp; + constructor = klass.getConstructor (argTypes); + // NOT creating terminus by dead-ending + // filters ... users should think about + // that one, something's likely wrong + } + + obj = constructor.newInstance (params); + + // return EventConsumers directly, perhaps after + // turning them into a filter + if (obj instanceof EventConsumer) { + if (filter) + return new TeeConsumer ((EventConsumer) obj, next); + return (EventConsumer) obj; + } + + // if it's not a handler, it's an error + // we can wrap handlers in a filter + EventFilter retval = new EventFilter (); + boolean updated = false; + + if (obj instanceof ContentHandler) { + retval.setContentHandler ((ContentHandler) obj); + updated = true; + } + if (obj instanceof DTDHandler) { + retval.setDTDHandler ((DTDHandler) obj); + updated = true; + } + if (obj instanceof LexicalHandler) { + retval.setProperty ( + EventFilter.PROPERTY_URI + "lexical-handler", + obj); + updated = true; + } + if (obj instanceof DeclHandler) { + retval.setProperty ( + EventFilter.PROPERTY_URI + "declaration-handler", + obj); + updated = true; + } + + if (!updated) + fail ("class is neither Consumer nor Handler"); + + if (filter) + return new TeeConsumer (retval, next); + return retval; + + } catch (IOException e) { + throw e; + + } catch (NoSuchMethodException e) { + fail (name + " constructor missing -- " + msg); + + } catch (ClassNotFoundException e) { + fail (name + " class not found"); + + } catch (Exception e) { + // e.printStackTrace (); + fail ("stage not available: " + e.getMessage ()); + } + } + // NOTREACHED + return null; + } + } + + private static class Pipeline + { + Stage stage; + + // rest may be null + Pipeline rest; + EventConsumer next; + + Pipeline (Stage s) + { stage = s; } + + public String toString () + { + if (rest == null && next == null) + return stage.toString (); + if (rest != null) + return stage + " | " + rest; + throw new IllegalArgumentException ("next"); + } + + EventConsumer createPipeline () + throws IOException + { + if (next == null) { + if (rest == null) + next = stage.createStage (null); + else + next = stage.createStage (rest.createPipeline ()); + } + return next; + } + } + +/* + public static void main (String argv []) + { + try { + // three basic terminus cases + createPipeline ("null"); + createPipeline ("validate"); + createPipeline ("write ( stdout )"); + + // four basic filters + createPipeline ("nsfix | write ( stderr )"); + createPipeline ("wf | null"); + createPipeline ("null | null"); + createPipeline ( +"call ( http://www.example.com/services/xml-1a ) | xhtml ( stdout )"); + + // tee junctions + createPipeline ("tee ( validate ) | write ( stdout )"); + createPipeline ("tee ( nsfix | write ( stdout ) ) | validate"); + + // longer pipeline + createPipeline ("nsfix | tee ( validate ) | write ( stdout )"); + createPipeline ( + "null | wf | nsfix | tee ( validate ) | write ( stdout )"); + + // try some parsing error cases + try { + createPipeline ("null ("); // extra token '(' + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("nsfix |"); // extra token '|' + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("xhtml ( foo"); // missing right paren + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("xhtml ( foo bar"); // required right paren + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("tee ( nsfix | validate");// missing right paren + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + // try some construction error cases + + try { + createPipeline ("call"); // missing param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("call ( foobar )"); // broken param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("nsfix ( foobar )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("null ( foobar )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("wf ( foobar )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("xhtml ( foobar.html )"); + new File ("foobar.html").delete (); + // now supported + } catch (Exception e) { + System.err.println ("** err: " + e.getMessage ()); } + try { + createPipeline ("xhtml"); // missing param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("write ( stdout ) | null"); // nonterminal + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("validate | null"); + // now supported + } catch (Exception e) { + System.err.println ("** err: " + e.getMessage ()); } + try { + createPipeline ("validate ( foo )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("tee"); // missing param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + // only builtins so far + createPipeline ("com.example.xml.FilterClass"); + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + } catch (Exception e) { + e.printStackTrace (); + } + } +/**/ + +} diff --git a/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java b/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java new file mode 100644 index 00000000000..8186de4df66 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java @@ -0,0 +1,417 @@ +/* TeeConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; + +/** + * Fans its events out to two other consumers, a "tee" filter stage in an + * event pipeline. Networks can be assembled with multiple output points. + * + * <p> Error handling should be simple if you remember that exceptions + * you throw will cancel later stages in that callback's pipeline, and + * generally the producer will stop if it sees such an exception. You + * may want to protect your pipeline against such backflows, making a + * kind of reverse filter (or valve?) so that certain exceptions thrown by + * your pipeline will caught and handled before the producer sees them. + * Just use a "try/catch" block, rememebering that really important + * cleanup tasks should be in "finally" clauses. + * + * <p> That issue isn't unique to "tee" consumers, but tee consumers have + * the additional twist that exceptions thrown by the first consumer + * will cause the second consumer not to see the callback (except for + * the endDocument callback, which signals state cleanup). + * + * @author David Brownell + */ +final public class TeeConsumer + implements EventConsumer, + ContentHandler, DTDHandler, + LexicalHandler,DeclHandler +{ + private EventConsumer first, rest; + + // cached to minimize time overhead + private ContentHandler docFirst, docRest; + private DeclHandler declFirst, declRest; + private LexicalHandler lexFirst, lexRest; + + + /** + * Constructs a consumer which sends all its events to the first + * consumer, and then the second one. If the first consumer throws + * an exception, the second one will not see the event which + * caused that exception to be reported. + * + * @param car The first consumer to get the events + * @param cdr The second consumer to get the events + */ + public TeeConsumer (EventConsumer car, EventConsumer cdr) + { + if (car == null || cdr == null) + throw new NullPointerException (); + first = car; + rest = cdr; + + // + // Cache the handlers. + // + docFirst = first.getContentHandler (); + docRest = rest.getContentHandler (); + // DTD handler isn't cached (rarely needed) + + try { + declFirst = null; + declFirst = (DeclHandler) first.getProperty ( + EventFilter.DECL_HANDLER); + } catch (SAXException e) {} + try { + declRest = null; + declRest = (DeclHandler) rest.getProperty ( + EventFilter.DECL_HANDLER); + } catch (SAXException e) {} + + try { + lexFirst = null; + lexFirst = (LexicalHandler) first.getProperty ( + EventFilter.LEXICAL_HANDLER); + } catch (SAXException e) {} + try { + lexRest = null; + lexRest = (LexicalHandler) rest.getProperty ( + EventFilter.LEXICAL_HANDLER); + } catch (SAXException e) {} + } + +/* FIXME + /** + * Constructs a pipeline, and is otherwise a shorthand for the + * two-consumer constructor for this class. + * + * @param first Description of the first pipeline to get events, + * which will be passed to {@link PipelineFactory#createPipeline} + * @param rest The second pipeline to get the events + * / + // constructor used by PipelineFactory + public TeeConsumer (String first, EventConsumer rest) + throws IOException + { + this (PipelineFactory.createPipeline (first), rest); + } +*/ + + /** Returns the first pipeline to get event calls. */ + public EventConsumer getFirst () + { return first; } + + /** Returns the second pipeline to get event calls. */ + public EventConsumer getRest () + { return rest; } + + /** Returns the content handler being used. */ + final public ContentHandler getContentHandler () + { + if (docRest == null) + return docFirst; + if (docFirst == null) + return docRest; + return this; + } + + /** Returns the dtd handler being used. */ + final public DTDHandler getDTDHandler () + { + // not cached (hardly used) + if (rest.getDTDHandler () == null) + return first.getDTDHandler (); + if (first.getDTDHandler () == null) + return rest.getDTDHandler (); + return this; + } + + /** Returns the declaration or lexical handler being used. */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + // + // in degenerate cases, we have no work to do. + // + Object firstProp = null, restProp = null; + + try { firstProp = first.getProperty (id); } + catch (SAXNotRecognizedException e) { /* ignore */ } + try { restProp = rest.getProperty (id); } + catch (SAXNotRecognizedException e) { /* ignore */ } + + if (restProp == null) + return firstProp; + if (firstProp == null) + return restProp; + + // + // we've got work to do; handle two builtin cases. + // + if (EventFilter.DECL_HANDLER.equals (id)) + return this; + if (EventFilter.LEXICAL_HANDLER.equals (id)) + return this; + + // + // non-degenerate, handled by both consumers, but we don't know + // how to handle this. + // + throw new SAXNotRecognizedException ("can't tee: " + id); + } + + /** + * Provides the error handler to both subsequent nodes of + * this filter stage. + */ + public void setErrorHandler (ErrorHandler handler) + { + first.setErrorHandler (handler); + rest.setErrorHandler (handler); + } + + + // + // ContentHandler + // + public void setDocumentLocator (Locator locator) + { + // this call is not made by all parsers + docFirst.setDocumentLocator (locator); + docRest.setDocumentLocator (locator); + } + + public void startDocument () + throws SAXException + { + docFirst.startDocument (); + docRest.startDocument (); + } + + public void endDocument () + throws SAXException + { + try { + docFirst.endDocument (); + } finally { + docRest.endDocument (); + } + } + + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + docFirst.startPrefixMapping (prefix, uri); + docRest.startPrefixMapping (prefix, uri); + } + + public void endPrefixMapping (String prefix) + throws SAXException + { + docFirst.endPrefixMapping (prefix); + docRest.endPrefixMapping (prefix); + } + + public void skippedEntity (String name) + throws SAXException + { + docFirst.skippedEntity (name); + docRest.skippedEntity (name); + } + + public void startElement (String uri, String localName, + String qName, Attributes atts) + throws SAXException + { + docFirst.startElement (uri, localName, qName, atts); + docRest.startElement (uri, localName, qName, atts); + } + + public void endElement (String uri, String localName, String qName) + throws SAXException + { + docFirst.endElement (uri, localName, qName); + docRest.endElement (uri, localName, qName); + } + + public void processingInstruction (String target, String data) + throws SAXException + { + docFirst.processingInstruction (target, data); + docRest.processingInstruction (target, data); + } + + public void characters (char ch [], int start, int length) + throws SAXException + { + docFirst.characters (ch, start, length); + docRest.characters (ch, start, length); + } + + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + docFirst.ignorableWhitespace (ch, start, length); + docRest.ignorableWhitespace (ch, start, length); + } + + + // + // DTDHandler + // + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + DTDHandler l1 = first.getDTDHandler (); + DTDHandler l2 = rest.getDTDHandler (); + + l1.notationDecl (name, publicId, systemId); + l2.notationDecl (name, publicId, systemId); + } + + public void unparsedEntityDecl (String name, + String publicId, String systemId, + String notationName + ) throws SAXException + { + DTDHandler l1 = first.getDTDHandler (); + DTDHandler l2 = rest.getDTDHandler (); + + l1.unparsedEntityDecl (name, publicId, systemId, notationName); + l2.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + + // + // DeclHandler + // + public void attributeDecl (String eName, String aName, + String type, + String mode, String value) + throws SAXException + { + declFirst.attributeDecl (eName, aName, type, mode, value); + declRest.attributeDecl (eName, aName, type, mode, value); + } + + public void elementDecl (String name, String model) + throws SAXException + { + declFirst.elementDecl (name, model); + declRest.elementDecl (name, model); + } + + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + declFirst.externalEntityDecl (name, publicId, systemId); + declRest.externalEntityDecl (name, publicId, systemId); + } + + public void internalEntityDecl (String name, String value) + throws SAXException + { + declFirst.internalEntityDecl (name, value); + declRest.internalEntityDecl (name, value); + } + + + // + // LexicalHandler + // + public void comment (char ch [], int start, int length) + throws SAXException + { + lexFirst.comment (ch, start, length); + lexRest.comment (ch, start, length); + } + + public void startCDATA () + throws SAXException + { + lexFirst.startCDATA (); + lexRest.startCDATA (); + } + + public void endCDATA () + throws SAXException + { + lexFirst.endCDATA (); + lexRest.endCDATA (); + } + + public void startEntity (String name) + throws SAXException + { + lexFirst.startEntity (name); + lexRest.startEntity (name); + } + + public void endEntity (String name) + throws SAXException + { + lexFirst.endEntity (name); + lexRest.endEntity (name); + } + + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + lexFirst.startDTD (name, publicId, systemId); + lexRest.startDTD (name, publicId, systemId); + } + + public void endDTD () + throws SAXException + { + lexFirst.endDTD (); + lexRest.endDTD (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/TextConsumer.java b/libjava/classpath/gnu/xml/pipeline/TextConsumer.java new file mode 100644 index 00000000000..67bd23b00d6 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/TextConsumer.java @@ -0,0 +1,117 @@ +/* TextConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.*; + +import org.xml.sax.*; + +import gnu.xml.util.XMLWriter; + + +/** + * Terminates a pipeline, consuming events to print them as well formed + * XML (or XHTML) text. + * + * <p> Input must be well formed, and must include XML names (e.g. the + * prefixes and prefix declarations must be present), or the output of + * this class is undefined. + * + * @see NSFilter + * @see WellFormednessFilter + * + * @author David Brownell + */ +public class TextConsumer extends XMLWriter implements EventConsumer +{ + /** + * Constructs an event consumer which echoes its input as text, + * optionally adhering to some basic XHTML formatting options + * which increase interoperability with old (v3) browsers. + * + * <p> For the best interoperability, when writing as XHTML only + * ASCII characters are emitted; other characters are turned to + * entity or character references as needed, and no XML declaration + * is provided in the document. + */ + public TextConsumer (Writer w, boolean isXhtml) + throws IOException + { + super (w, isXhtml ? "US-ASCII" : null); + setXhtml (isXhtml); + } + + /** + * Constructs a consumer that writes its input as XML text. + * XHTML rules are not followed. + */ + public TextConsumer (Writer w) + throws IOException + { + this (w, false); + } + + /** + * Constructs a consumer that writes its input as XML text, + * encoded in UTF-8. XHTML rules are not followed. + */ + public TextConsumer (OutputStream out) + throws IOException + { + this (new OutputStreamWriter (out, "UTF8"), false); + } + + /** <b>EventConsumer</b> Returns the document handler being used. */ + public ContentHandler getContentHandler () + { return this; } + + /** <b>EventConsumer</b> Returns the dtd handler being used. */ + public DTDHandler getDTDHandler () + { return this; } + + /** <b>XMLReader</b>Retrieves a property (lexical and decl handlers) */ + public Object getProperty (String propertyId) + throws SAXNotRecognizedException + { + if (EventFilter.LEXICAL_HANDLER.equals (propertyId)) + return this; + if (EventFilter.DECL_HANDLER.equals (propertyId)) + return this; + throw new SAXNotRecognizedException (propertyId); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java b/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java new file mode 100644 index 00000000000..0fbfa9264aa --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java @@ -0,0 +1,1928 @@ +/* ValidationConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.EmptyStackException; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Stack; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +/** + * This class checks SAX2 events to report validity errors; it works as + * both a filter and a terminus on an event pipeline. It relies on the + * producer of SAX events to: </p> <ol> + * + * <li> Conform to the specification of a non-validating XML parser that + * reads all external entities, reported using SAX2 events. </li> + * + * <li> Report ignorable whitespace as such (through the ContentHandler + * interface). This is, strictly speaking, optional for nonvalidating + * XML processors. </li> + * + * <li> Make SAX2 DeclHandler callbacks, with default + * attribute values already normalized (and without "<").</li> + * + * <li> Make SAX2 LexicalHandler startDTD() and endDTD () + * callbacks. </li> + * + * <li> Act as if the <em>(URI)/namespace-prefixes</em> property were + * set to true, by providing XML 1.0 names and all <code>xmlns*</code> + * attributes (rather than omitting either or both). </li> + * + * </ol> + * + * <p> At this writing, the major SAX2 parsers (such as Ælfred2, + * Crimson, and Xerces) meet these requirements, and this validation + * module is used by the optional Ælfred2 validation support. + * </p> + * + * <p> Note that because this is a layered validator, it has to duplicate some + * work that the parser is doing; there are also other cost to layering. + * However, <em>because of layering it doesn't need a parser</em> in order + * to work! You can use it with anything that generates SAX events, such + * as an application component that wants to detect invalid content in + * a changed area without validating an entire document, or which wants to + * ensure that it doesn't write invalid data to a communications partner.</p> + * + * <p> Also, note that because this is a layered validator, the line numbers + * reported for some errors may seem strange. For example, if an element does + * not permit character content, the validator + * will use the locator provided to it. + * That might reflect the last character of a <em>characters</em> event + * callback, rather than the first non-whitespace character. </p> + * + * <hr /> + * + * <!-- + * <p> Of interest is the fact that unlike most currently known XML validators, + * this one can report some cases of non-determinism in element content models. + * It is a compile-time option, enabled by default. This will only report + * such XML errors if they relate to content actually appearing in a document; + * content models aren't aggressively scanned for non-deterministic structure. + * Documents which trigger such non-deterministic transitions may be handled + * differently by different validating parsers, without losing conformance + * to the XML specification. </p> + * --> + * + * <p> Current limitations of the validation performed are in roughly three + * categories. </p> + * + * <p> The first category represents constraints which demand violations + * of software layering: exposing lexical details, one of the first things + * that <em>application</em> programming interfaces (APIs) hide. These + * invariably relate to XML entity handling, and to historical oddities + * of the XML validation semantics. Curiously, + * recent (Autumn 1999) conformance testing showed that these constraints are + * among those handled worst by existing XML validating parsers. Arguments + * have been made that each of these VCs should be turned into WFCs (most + * of them) or discarded (popular for the standalone declaration); in short, + * that these are bugs in the XML specification (not all via SGML): </p><ul> + * + * <li> The <em>Proper Declaration/PE Nesting</em> and + * <em>Proper Group/PE Nesting</em> VCs can't be tested because they + * require access to particularly low level lexical level information. + * In essence, the reason XML isn't a simple thing to parse is that + * it's not a context free grammar, and these constraints elevate that + * SGML-derived context sensitivity to the level of a semantic rule. + * + * <li> The <em>Standalone Document Declaration</em> VC can't be + * tested. This is for two reasons. First, this flag isn't made + * available through SAX2. Second, it also requires breaking that + * lexical layering boundary. (If you ever wondered why classes + * in compiler construction or language design barely mention the + * existence of context-sensitive grammars, it's because of messy + * issues like these.) + * + * <li> The <em>Entity Declared</em> VC can't be tested, because it + * also requires breaking that lexical layering boundary! There's also + * another issue: the VC wording (and seemingly intent) is ambiguous. + * (This is still true in the "Second edition" XML spec.) + * Since there is a WFC of the same name, everyone's life would be + * easier if references to undeclared parsed entities were always well + * formedness errors, regardless of whether they're parameter entities + * or not. (Note that nonvalidating parsers are not required + * to report all such well formedness errors if they don't read external + * parameter entities, although currently most XML parsers read them + * in an attempt to avoid problems from inconsistent parser behavior.) + * + * </ul> + * + * <p> The second category of limitations on this validation represent + * constraints associated with information that is not guaranteed to be + * available (or in one case, <em>is guaranteed not to be available</em>, + * through the SAX2 API: </p><ul> + * + * <li> The <em>Unique Element Type Declaration</em> VC may not be + * reportable, if the underlying parser happens not to expose + * multiple declarations. (Ælfred2 reports these validity + * errors directly.)</li> + * + * <li> Similarly, the <em>Unique Notation Name</em> VC, added in the + * 14-January-2000 XML spec errata to restrict typing models used by + * elements, may not be reportable. (Ælfred reports these + * validity errors directly.) </li> + * + * </ul> + * + * <p> A third category relates to ease of implementation. (Think of this + * as "bugs".) The most notable issue here is character handling. Rather + * than attempting to implement the voluminous character tables in the XML + * specification (Appendix B), Unicode rules are used directly from + * the java.lang.Character class. Recent JVMs have begun to diverge from + * the original specification for that class (Unicode 2.0), meaning that + * different JVMs may handle that aspect of conformance differently. + * </p> + * + * <p> Note that for some of the validity errors that SAX2 does not + * expose, a nonvalidating parser is permitted (by the XML specification) + * to report validity errors. When used with a parser that does so for + * the validity constraints mentioned above (or any other SAX2 event + * stream producer that does the same thing), overall conformance is + * substantially improved. + * + * @see gnu.xml.aelfred2.SAXDriver + * @see gnu.xml.aelfred2.XmlReader + * + * @author David Brownell + */ +public final class ValidationConsumer extends EventFilter +{ + // report error if we happen to notice a non-deterministic choice? + // we won't report buggy content models; just buggy instances + private static final boolean warnNonDeterministic = false; + + // for tracking active content models + private String rootName; + private Stack contentStack = new Stack (); + + // flags for "saved DTD" processing + private boolean disableDeclarations; + private boolean disableReset; + + // + // most VCs get tested when we see element start tags. the per-element + // info (including attributes) recorded here duplicates that found inside + // many nonvalidating parsers, hence dual lookups etc ... that's why a + // layered validator isn't going to be as fast as a non-layered one. + // + + // key = element name; value = ElementInfo + private Hashtable elements = new Hashtable (); + + // some VCs relate to ID/IDREF/IDREFS attributes + // key = id; value = boolean true (defd) or false (refd) + private Hashtable ids = new Hashtable (); + + // we just record declared notation and unparsed entity names. + // the implementation here is simple/slow; these features + // are seldom used, one hopes they'll wither away soon + private Vector notations = new Vector (5, 5); + private Vector nDeferred = new Vector (5, 5); + private Vector unparsed = new Vector (5, 5); + private Vector uDeferred = new Vector (5, 5); + + // note: DocBk 3.1.7 XML defines over 2 dozen notations, + // used when defining unparsed entities for graphics + // (and maybe in other places) + + + + /** + * Creates a pipeline terminus which consumes all events passed to + * it; this will report validity errors as if they were fatal errors, + * unless an error handler is assigned. + * + * @see #setErrorHandler + */ + // constructor used by PipelineFactory + // ... and want one taking system ID of an external subset + public ValidationConsumer () + { + this (null); + } + + /** + * Creates a pipeline filter which reports validity errors and then + * passes events on to the next consumer if they were not fatal. + * + * @see #setErrorHandler + */ + // constructor used by PipelineFactory + // ... and want one taking system ID of an external subset + // (which won't send declaration events) + public ValidationConsumer (EventConsumer next) + { + super (next); + + setContentHandler (this); + setDTDHandler (this); + try { setProperty (DECL_HANDLER, this); } + catch (Exception e) { /* "can't happen" */ } + try { setProperty (LEXICAL_HANDLER, this); } + catch (Exception e) { /* "can't happen" */ } + } + + + private static final String fakeRootName + = ":Nobody:in:their_Right.Mind_would:use:this-name:1x:"; + + /** + * Creates a validation consumer which is preloaded with the DTD provided. + * It does this by constructing a document with that DTD, then parsing + * that document and recording its DTD declarations. Then it arranges + * not to modify that information. + * + * <p> The resulting validation consumer will only validate against + * the specified DTD, regardless of whether some other DTD is found + * in a document being parsed. + * + * @param rootName The name of the required root element; if this is + * null, any root element name will be accepted. + * @param publicId If non-null and there is a non-null systemId, this + * identifier provides an alternate access identifier for the DTD's + * external subset. + * @param systemId If non-null, this is a URI (normally URL) that + * may be used to access the DTD's external subset. + * @param internalSubset If non-null, holds literal markup declarations + * comprising the DTD's internal subset. + * @param resolver If non-null, this will be provided to the parser for + * use when resolving parameter entities (including any external subset). + * @param resolver If non-null, this will be provided to the parser for + * use when resolving parameter entities (including any external subset). + * @param minimalElement If non-null, a minimal valid document. + * + * @exception SAXNotSupportedException If the default SAX parser does + * not support the standard lexical or declaration handlers. + * @exception SAXParseException If the specified DTD has either + * well-formedness or validity errors + * @exception IOException If the specified DTD can't be read for + * some reason + */ + public ValidationConsumer ( + String rootName, + String publicId, + String systemId, + String internalSubset, + EntityResolver resolver, + String minimalDocument + ) throws SAXException, IOException + { + this (null); + + disableReset = true; + if (rootName == null) + rootName = fakeRootName; + + // + // Synthesize document with that DTD; is it possible to do + // better for the declaration of the root element? + // + // NOTE: can't use SAX2 to write internal subsets. + // + StringWriter writer = new StringWriter (); + + writer.write ("<!DOCTYPE "); + writer.write (rootName); + if (systemId != null) { + writer.write ("\n "); + if (publicId != null) { + writer.write ("PUBLIC '"); + writer.write (publicId); + writer.write ("'\n\t'"); + } else + writer.write ("SYSTEM '"); + writer.write (systemId); + writer.write ("'"); + } + writer.write (" [ "); + if (rootName == fakeRootName) { + writer.write ("\n<!ELEMENT "); + writer.write (rootName); + writer.write (" EMPTY>"); + } + if (internalSubset != null) + writer.write (internalSubset); + writer.write ("\n ]>"); + + if (minimalDocument != null) { + writer.write ("\n"); + writer.write (minimalDocument); + writer.write ("\n"); + } else { + writer.write (" <"); + writer.write (rootName); + writer.write ("/>\n"); + } + minimalDocument = writer.toString (); + + // + // OK, load it + // + XMLReader producer; + + producer = XMLReaderFactory.createXMLReader (); + bind (producer, this); + + if (resolver != null) + producer.setEntityResolver (resolver); + + InputSource in; + + in = new InputSource (new StringReader (minimalDocument)); + producer.parse (in); + + disableDeclarations = true; + if (rootName == fakeRootName) + this.rootName = null; + } + + private void resetState () + { + if (!disableReset) { + rootName = null; + contentStack.removeAllElements (); + elements.clear (); + ids.clear (); + + notations.removeAllElements (); + nDeferred.removeAllElements (); + unparsed.removeAllElements (); + uDeferred.removeAllElements (); + } + } + + + private void warning (String description) + throws SAXException + { + ErrorHandler errHandler = getErrorHandler (); + Locator locator = getDocumentLocator (); + SAXParseException err; + + if (errHandler == null) + return; + + if (locator == null) + err = new SAXParseException (description, null, null, -1, -1); + else + err = new SAXParseException (description, locator); + errHandler.warning (err); + } + + // package private (for ChildrenRecognizer) + private void error (String description) + throws SAXException + { + ErrorHandler errHandler = getErrorHandler (); + Locator locator = getDocumentLocator (); + SAXParseException err; + + if (locator == null) + err = new SAXParseException (description, null, null, -1, -1); + else + err = new SAXParseException (description, locator); + if (errHandler != null) + errHandler.error (err); + else // else we always treat it as fatal! + throw err; + } + + private void fatalError (String description) + throws SAXException + { + ErrorHandler errHandler = getErrorHandler (); + Locator locator = getDocumentLocator (); + SAXParseException err; + + if (locator != null) + err = new SAXParseException (description, locator); + else + err = new SAXParseException (description, null, null, -1, -1); + if (errHandler != null) + errHandler.fatalError (err); + // we always treat this as fatal, regardless of the handler + throw err; + } + + + private static boolean isExtender (char c) + { + // [88] Extender ::= ... + return c == 0x00b7 || c == 0x02d0 || c == 0x02d1 || c == 0x0387 + || c == 0x0640 || c == 0x0e46 || c == 0x0ec6 || c == 0x3005 + || (c >= 0x3031 && c <= 0x3035) + || (c >= 0x309d && c <= 0x309e) + || (c >= 0x30fc && c <= 0x30fe); + } + + + // use augmented Unicode rules, not full XML rules + private boolean isName (String name, String context, String id) + throws SAXException + { + char buf [] = name.toCharArray (); + boolean pass = true; + + if (!Character.isUnicodeIdentifierStart (buf [0]) + && ":_".indexOf (buf [0]) == -1) + pass = false; + else { + int max = buf.length; + for (int i = 1; pass && i < max; i++) { + char c = buf [i]; + if (!Character.isUnicodeIdentifierPart (c) + && ":-_.".indexOf (c) == -1 + && !isExtender (c)) + pass = false; + } + } + + if (!pass) + error ("In " + context + " for " + id + + ", '" + name + "' is not a name"); + return pass; // true == OK + } + + // use augmented Unicode rules, not full XML rules + private boolean isNmtoken (String nmtoken, String context, String id) + throws SAXException + { + char buf [] = nmtoken.toCharArray (); + boolean pass = true; + int max = buf.length; + + // XXX make this share code with isName + + for (int i = 0; pass && i < max; i++) { + char c = buf [i]; + if (!Character.isUnicodeIdentifierPart (c) + && ":-_.".indexOf (c) == -1 + && !isExtender (c)) + pass = false; + } + + if (!pass) + error ("In " + context + " for " + id + + ", '" + nmtoken + "' is not a name token"); + return pass; // true == OK + } + + private void checkEnumeration (String value, String type, String name) + throws SAXException + { + if (!hasMatch (value, type)) + // VC: Enumeration + error ("Value '" + value + + "' for attribute '" + name + + "' is not permitted: " + type); + } + + // used to test enumerated attributes and mixed content models + // package private + static boolean hasMatch (String value, String orList) + { + int len = value.length (); + int max = orList.length () - len; + + for (int start = 0; + (start = orList.indexOf (value, start)) != -1; + start++) { + char c; + + if (start > max) + break; + c = orList.charAt (start - 1); + if (c != '|' && c != '('/*)*/) + continue; + c = orList.charAt (start + len); + if (c != '|' && /*(*/ c != ')') + continue; + return true; + } + return false; + } + + /** + * <b>LexicalHandler</b> Records the declaration of the root + * element, so it can be verified later. + * Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (disableDeclarations) + return; + + rootName = name; + super.startDTD (name, publicId, systemId); + } + + /** + * <b>LexicalHandler</b> Verifies that all referenced notations + * and unparsed entities have been declared. + * Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void endDTD () + throws SAXException + { + if (disableDeclarations) + return; + + // this is a convenient hook for end-of-dtd checks, but we + // could also trigger it in the first startElement call. + // locator info is more appropriate here though. + + // VC: Notation Declared (NDATA can refer to them before decls, + // as can NOTATION attribute enumerations and defaults) + int length = nDeferred.size (); + for (int i = 0; i < length; i++) { + String notation = (String) nDeferred.elementAt (i); + if (!notations.contains (notation)) { + error ("A declaration referred to notation '" + notation + + "' which was never declared"); + } + } + nDeferred.removeAllElements (); + + // VC: Entity Name (attribute values can refer to them + // before they're declared); VC Attribute Default Legal + length = uDeferred.size (); + for (int i = 0; i < length; i++) { + String entity = (String) uDeferred.elementAt (i); + if (!unparsed.contains (entity)) { + error ("An attribute default referred to entity '" + entity + + "' which was never declared"); + } + } + uDeferred.removeAllElements (); + super.endDTD (); + } + + + // These are interned, so we can rely on "==" to find the type of + // all attributes except enumerations ... + // "(this|or|that|...)" and "NOTATION (this|or|that|...)" + static final String types [] = { + "CDATA", + "ID", "IDREF", "IDREFS", + "NMTOKEN", "NMTOKENS", + "ENTITY", "ENTITIES" + }; + + + /** + * <b>DecllHandler</b> Records attribute declaration for later use + * in validating document content, and checks validity constraints + * that are applicable to attribute declarations. + * Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void attributeDecl ( + String eName, + String aName, + String type, + String mode, + String value + ) throws SAXException + { + if (disableDeclarations) + return; + + ElementInfo info = (ElementInfo) elements.get (eName); + AttributeInfo ainfo = new AttributeInfo (); + boolean checkOne = false; + boolean interned = false; + + // cheap interning of type names and #FIXED, #REQUIRED + // for faster startElement (we can use "==") + for (int i = 0; i < types.length; i++) { + if (types [i].equals (type)) { + type = types [i]; + interned = true; + break; + } + } + if ("#FIXED".equals (mode)) + mode = "#FIXED"; + else if ("#REQUIRED".equals (mode)) + mode = "#REQUIRED"; + + ainfo.type = type; + ainfo.mode = mode; + ainfo.value = value; + + // we might not have seen the content model yet + if (info == null) { + info = new ElementInfo (eName); + elements.put (eName, info); + } + if ("ID" == type) { + checkOne = true; + if (!("#REQUIRED" == mode || "#IMPLIED".equals (mode))) { + // VC: ID Attribute Default + error ("ID attribute '" + aName + + "' must be #IMPLIED or #REQUIRED"); + } + + } else if (!interned && type.startsWith ("NOTATION ")) { + checkOne = true; + + // VC: Notation Attributes (notations must be declared) + StringTokenizer tokens = new StringTokenizer ( + type.substring (10, type.lastIndexOf (')')), + "|"); + while (tokens.hasMoreTokens ()) { + String token = tokens.nextToken (); + if (!notations.contains (token)) + nDeferred.addElement (token); + } + } + if (checkOne) { + for (Enumeration e = info.attributes.keys (); + e.hasMoreElements (); + /* NOP */) { + String name; + AttributeInfo ainfo2; + + name = (String) e.nextElement (); + ainfo2 = (AttributeInfo) info.attributes.get (name); + if (type == ainfo2.type || !interned /* NOTATION */) { + // VC: One ID per Element Type + // VC: One Notation per Element TYpe + error ("Element '" + eName + + "' already has an attribute of type " + + (interned ? "NOTATION" : type) + + " ('" + name + + "') so '" + aName + + "' is a validity error"); + } + } + } + + // VC: Attribute Default Legal + if (value != null) { + + if ("CDATA" == type) { + // event source rejected '<' + + } else if ("NMTOKEN" == type) { + // VC: Name Token (is a nmtoken) + isNmtoken (value, "attribute default", aName); + + } else if ("NMTOKENS" == type) { + // VC: Name Token (is a nmtoken; at least one value) + StringTokenizer tokens = new StringTokenizer (value); + if (!tokens.hasMoreTokens ()) + error ("Default for attribute '" + aName + + "' must have at least one name token."); + else do { + String token = tokens.nextToken (); + isNmtoken (token, "attribute default", aName); + } while (tokens.hasMoreTokens ()); + + } else if ("IDREF" == type || "ENTITY" == type) { + // VC: Entity Name (is a name) + // VC: IDREF (is a name) (is declared) + isName (value, "attribute default", aName); + if ("ENTITY" == type && !unparsed.contains (value)) + uDeferred.addElement (value); + + } else if ("IDREFS" == type || "ENTITIES" == type) { + // VC: Entity Name (is a name; at least one value) + // VC: IDREF (is a name; at least one value) + StringTokenizer names = new StringTokenizer (value); + if (!names.hasMoreTokens ()) + error ("Default for attribute '" + aName + + "' must have at least one name."); + else do { + String name = names.nextToken (); + isName (name, "attribute default", aName); + if ("ENTITIES" == type && !unparsed.contains (name)) + uDeferred.addElement (value); + } while (names.hasMoreTokens ()); + + } else if (type.charAt (0) == '(' /*)*/ ) { + // VC: Enumeration (must match) + checkEnumeration (value, type, aName); + + } else if (!interned && checkOne) { /* NOTATION */ + // VC: Notation attributes (must be names) + isName (value, "attribute default", aName); + + // VC: Notation attributes (must be declared) + if (!notations.contains (value)) + nDeferred.addElement (value); + + // VC: Enumeration (must match) + checkEnumeration (value, type, aName); + + } else if ("ID" != type) + throw new RuntimeException ("illegal attribute type: " + type); + } + + if (info.attributes.get (aName) == null) + info.attributes.put (aName, ainfo); + /* + else + warning ("Element '" + eName + + "' already has an attribute named '" + aName + "'"); + */ + + if ("xml:space".equals (aName)) { + if (!("(default|preserve)".equals (type) + || "(preserve|default)".equals (type) + // these next two are arguable; XHTML's DTD doesn't + // deserve errors. After all, it's not like any + // illegal _value_ could pass ... + || "(preserve)".equals (type) + || "(default)".equals (type) + )) + error ( + "xml:space attribute type must be like '(default|preserve)'" + + " not '" + type + "'" + ); + + } + super.attributeDecl (eName, aName, type, mode, value); + } + + /** + * <b>DecllHandler</b> Records the element declaration for later use + * when checking document content, and checks validity constraints that + * apply to element declarations. Passed to the next consumer, unless + * this one was preloaded with a particular DTD. + */ + public void elementDecl (String name, String model) + throws SAXException + { + if (disableDeclarations) + return; + + ElementInfo info = (ElementInfo) elements.get (name); + + // we might have seen an attribute decl already + if (info == null) { + info = new ElementInfo (name); + elements.put (name, info); + } + if (info.model != null) { + // NOTE: not all parsers can report such duplicates. + // VC: Unique Element Type Declaration + error ("Element type '" + name + + "' was already declared."); + } else { + info.model = model; + + // VC: No Duplicate Types (in mixed content models) + if (model.charAt (1) == '#') // (#PCDATA... + info.getRecognizer (this); + } + super.elementDecl (name, model); + } + + /** + * <b>DecllHandler</b> passed to the next consumer, unless this + * one was preloaded with a particular DTD + */ + public void internalEntityDecl (String name, String value) + throws SAXException + { + if (!disableDeclarations) + super.internalEntityDecl (name, value); + } + + /** + * <b>DecllHandler</b> passed to the next consumer, unless this + * one was preloaded with a particular DTD + */ + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + if (!disableDeclarations) + super.externalEntityDecl (name, publicId, systemId); + } + + + /** + * <b>DTDHandler</b> Records the notation name, for checking + * NOTATIONS attribute values and declararations of unparsed + * entities. Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + if (disableDeclarations) + return; + + notations.addElement (name); + super.notationDecl (name, publicId, systemId); + } + + /** + * <b>DTDHandler</b> Records the entity name, for checking + * ENTITY and ENTITIES attribute values; records the notation + * name if it hasn't yet been declared. Passed to the next consumer, + * unless this one was preloaded with a particular DTD. + */ + public void unparsedEntityDecl ( + String name, + String publicId, + String systemId, + String notationName + ) throws SAXException + { + if (disableDeclarations) + return; + + unparsed.addElement (name); + if (!notations.contains (notationName)) + nDeferred.addElement (notationName); + super.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + + /** + * <b>ContentHandler</b> Ensures that state from any previous parse + * has been deleted. + * Passed to the next consumer. + */ + public void startDocument () + throws SAXException + { + resetState (); + super.startDocument (); + } + + + private static boolean isAsciiLetter (char c) + { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); + } + + + /** + * <b>ContentHandler</b> Reports a fatal exception. Validating + * XML processors may not skip any entities. + */ + public void skippedEntity (String name) + throws SAXException + { + fatalError ("may not skip entities"); + } + + /* + * SAX2 doesn't expand non-PE refs in attribute defaults... + */ + private String expandDefaultRefs (String s) + throws SAXException + { + if (s.indexOf ('&') < 0) + return s; + +// FIXME: handle &#nn; &#xnn; &name; + String message = "Can't expand refs in attribute default: " + s; + warning (message); + + return s; + } + + /** + * <b>ContentHandler</b> Performs validity checks against element + * (and document) content models, and attribute values. + * Passed to the next consumer. + */ + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + // + // First check content model for the enclosing scope. + // + if (contentStack.isEmpty ()) { + // VC: Root Element Type + if (!qName.equals (rootName)) { + if (rootName == null) + warning ("This document has no DTD, can't be valid"); + else + error ("Root element type '" + qName + + "' was declared to be '" + rootName + "'"); + } + } else { + Recognizer state = (Recognizer) contentStack.peek (); + + if (state != null) { + Recognizer newstate = state.acceptElement (qName); + + if (newstate == null) + error ("Element type '" + qName + + "' in element '" + state.type.name + + "' violates content model " + state.type.model + ); + if (newstate != state) { + contentStack.pop (); + contentStack.push (newstate); + } + } + } + + // + // Then check that this element was declared, and push the + // object used to validate its content model onto our stack. + // + // This is where the recognizer gets created, if needed; if + // it's a "children" (elements) content model, an NDFA is + // created. (One recognizer is used per content type, no + // matter how complex that recognizer is.) + // + ElementInfo info; + + info = (ElementInfo) elements.get (qName); + if (info == null || info.model == null) { + // VC: Element Valid (base clause) + error ("Element type '" + qName + "' was not declared"); + contentStack.push (null); + + // for less diagnostic noise, fake a declaration. + elementDecl (qName, "ANY"); + } else + contentStack.push (info.getRecognizer (this)); + + // + // Then check each attribute present + // + int len; + String aname; + AttributeInfo ainfo; + + if (atts != null) + len = atts.getLength (); + else + len = 0; + + for (int i = 0; i < len; i++) { + aname = atts.getQName (i); + + if (info == null + || (ainfo = (AttributeInfo) info.attributes.get (aname)) + == null) { + // VC: Attribute Value Type + error ("Attribute '" + aname + + "' was not declared for element type " + qName); + continue; + } + + String value = atts.getValue (i); + + // note that "==" for type names and "#FIXED" is correct + // (and fast) since we've interned those literals. + + if ("#FIXED" == ainfo.mode) { + String expanded = expandDefaultRefs (ainfo.value); + + // VC: Fixed Attribute Default + if (!value.equals (expanded)) { + error ("Attribute '" + aname + + "' must match " + expanded + ); + continue; + } + } + + if ("CDATA" == ainfo.type) + continue; + + // + // For all other attribute types, there are various + // rules to follow. + // + + if ("ID" == ainfo.type) { + // VC: ID (must be a name) + if (isName (value, "ID attribute", aname)) { + if (Boolean.TRUE == ids.get (value)) + // VC: ID (appears once) + error ("ID attribute " + aname + + " uses an ID value '" + value + + "' which was already declared."); + else + // any forward refs are no longer problems + ids.put (value, Boolean.TRUE); + } + continue; + } + + if ("IDREF" == ainfo.type) { + // VC: IDREF (value must be a name) + if (isName (value, "IDREF attribute", aname)) { + // VC: IDREF (must match some ID attribute) + if (ids.get (value) == null) + // new -- assume it's a forward ref + ids.put (value, Boolean.FALSE); + } + continue; + } + + if ("IDREFS" == ainfo.type) { + StringTokenizer tokens = new StringTokenizer (value, " "); + + if (!tokens.hasMoreTokens ()) { + // VC: IDREF (one or more values) + error ("IDREFS attribute " + aname + + " must have at least one ID ref"); + } else do { + String id = tokens.nextToken (); + + // VC: IDREF (value must be a name) + if (isName (id, "IDREFS attribute", aname)) { + // VC: IDREF (must match some ID attribute) + if (ids.get (id) == null) + // new -- assume it's a forward ref + ids.put (id, Boolean.FALSE); + } + } while (tokens.hasMoreTokens ()); + continue; + } + + if ("NMTOKEN" == ainfo.type) { + // VC: Name Token (is a name token) + isNmtoken (value, "NMTOKEN attribute", aname); + continue; + } + + if ("NMTOKENS" == ainfo.type) { + StringTokenizer tokens = new StringTokenizer (value, " "); + + if (!tokens.hasMoreTokens ()) { + // VC: Name Token (one or more values) + error ("NMTOKENS attribute " + aname + + " must have at least one name token"); + } else do { + String token = tokens.nextToken (); + + // VC: Name Token (is a name token) + isNmtoken (token, "NMTOKENS attribute", aname); + } while (tokens.hasMoreTokens ()); + continue; + } + + if ("ENTITY" == ainfo.type) { + if (!unparsed.contains (value)) + // VC: Entity Name + error ("Value of attribute '" + aname + + "' refers to unparsed entity '" + value + + "' which was not declared."); + continue; + } + + if ("ENTITIES" == ainfo.type) { + StringTokenizer tokens = new StringTokenizer (value, " "); + + if (!tokens.hasMoreTokens ()) { + // VC: Entity Name (one or more values) + error ("ENTITIES attribute " + aname + + " must have at least one name token"); + } else do { + String entity = tokens.nextToken (); + + if (!unparsed.contains (entity)) + // VC: Entity Name + error ("Value of attribute '" + aname + + "' refers to unparsed entity '" + entity + + "' which was not declared."); + } while (tokens.hasMoreTokens ()); + continue; + } + + // + // check for enumerations last; more expensive + // + if (ainfo.type.charAt (0) == '(' /*)*/ + || ainfo.type.startsWith ("NOTATION ") + ) { + // VC: Enumeration (value must be defined) + checkEnumeration (value, ainfo.type, aname); + continue; + } + } + + // + // Last, check that all #REQUIRED attributes were provided + // + if (info != null) { + Hashtable table = info.attributes; + + if (table.size () != 0) { + Enumeration e = table.keys (); + + // XXX table.keys uses the heap, bleech -- slows things + + while (e.hasMoreElements ()) { + aname = (String) e.nextElement (); + ainfo = (AttributeInfo) table.get (aname); + + // "#REQUIRED" mode was interned in attributeDecl + if ("#REQUIRED" == ainfo.mode + && atts.getValue (aname) == null) { + // VC: Required Attribute + error ("Attribute '" + aname + "' must be specified " + + "for element type " + qName); + } + } + } + } + super.startElement (uri, localName, qName, atts); + } + + /** + * <b>ContentHandler</b> Reports a validity error if the element's content + * model does not permit character data. + * Passed to the next consumer. + */ + public void characters (char ch [], int start, int length) + throws SAXException + { + Recognizer state; + + if (contentStack.empty ()) + state = null; + else + state = (Recognizer) contentStack.peek (); + + // NOTE: if this ever supports with SAX parsers that don't + // report ignorable whitespace as such (only XP?), this class + // needs to morph it into ignorableWhitespace() as needed ... + + if (state != null && !state.acceptCharacters ()) + // VC: Element Valid (clauses three, four -- see recognizer) + error ("Character content not allowed in element " + + state.type.name); + + super.characters (ch, start, length); + } + + + /** + * <b>ContentHandler</b> Reports a validity error if the element's content + * model does not permit end-of-element yet, or a well formedness error + * if there was no matching startElement call. + * Passed to the next consumer. + */ + public void endElement (String uri, String localName, String qName) + throws SAXException + { + try { + Recognizer state = (Recognizer) contentStack.pop (); + + if (state != null && !state.completed ()) + // VC: Element valid (clauses two, three, four; see Recognizer) + error ("Premature end for element '" + + state.type.name + + "', content model " + + state.type.model); + + // could insist on match of start element, but that's + // something the input stream must to guarantee. + + } catch (EmptyStackException e) { + fatalError ("endElement without startElement: " + qName + + ((uri == null) + ? "" + : ( " { '" + uri + "', " + localName + " }"))); + } + super.endElement (uri, localName, qName); + } + + /** + * <b>ContentHandler</b> Checks whether all ID values that were + * referenced have been declared, and releases all resources. + * Passed to the next consumer. + * + * @see #setDocumentLocator + */ + public void endDocument () + throws SAXException + { + for (Enumeration idNames = ids.keys (); + idNames.hasMoreElements (); + /* NOP */) { + String id = (String) idNames.nextElement (); + + if (Boolean.FALSE == ids.get (id)) { + // VC: IDREF (must match ID) + error ("Undeclared ID value '" + id + + "' was referred to by an IDREF/IDREFS attribute"); + } + } + + resetState (); + super.endDocument (); + } + + + /** Holds per-element declarations */ + static private final class ElementInfo + { + String name; + String model; + + // key = attribute name; value = AttributeInfo + Hashtable attributes = new Hashtable (11); + + ElementInfo (String n) { name = n; } + + private Recognizer recognizer; + + // for validating content models: one per type, shared, + // and constructed only on demand ... so unused elements do + // not need to consume resources. + Recognizer getRecognizer (ValidationConsumer consumer) + throws SAXException + { + if (recognizer == null) { + if ("ANY".equals (model)) + recognizer = ANY; + else if ("EMPTY".equals (model)) + recognizer = new EmptyRecognizer (this); + else if ('#' == model.charAt (1)) + // n.b. this constructor does a validity check + recognizer = new MixedRecognizer (this, consumer); + else + recognizer = new ChildrenRecognizer (this, consumer); + } + return recognizer; + } + } + + /** Holds per-attribute declarations */ + static private final class AttributeInfo + { + String type; + String mode; // #REQUIRED, etc (or null) + String value; // or null + } + + + // + // Content model validation + // + + static private final Recognizer ANY = new Recognizer (null); + + + // Base class defines the calls used to validate content, + // and supports the "ANY" content model + static private class Recognizer + { + final ElementInfo type; + + Recognizer (ElementInfo t) { type = t; } + + // return true iff character data is legal here + boolean acceptCharacters () + throws SAXException + // VC: Element Valid (third and fourth clauses) + { return true; } + + // null return = failure + // otherwise, next state (like an FSM) + // prerequisite: tested that name was declared + Recognizer acceptElement (String name) + throws SAXException + // VC: Element Valid (fourth clause) + { return this; } + + // return true iff model is completed, can finish + boolean completed () + throws SAXException + // VC: Element Valid (fourth clause) + { return true; } + + public String toString () + // n.b. "children" is the interesting case! + { return (type == null) ? "ANY" : type.model; } + } + + // "EMPTY" content model -- no characters or elements + private static final class EmptyRecognizer extends Recognizer + { + public EmptyRecognizer (ElementInfo type) + { super (type); } + + // VC: Element Valid (first clause) + boolean acceptCharacters () + { return false; } + + // VC: Element Valid (first clause) + Recognizer acceptElement (String name) + { return null; } + } + + // "Mixed" content model -- ANY, but restricts elements + private static final class MixedRecognizer extends Recognizer + { + private String permitted []; + + // N.B. constructor tests for duplicated element names (VC) + public MixedRecognizer (ElementInfo t, ValidationConsumer v) + throws SAXException + { + super (t); + + // (#PCDATA...)* or (#PCDATA) ==> ... or empty + // with the "..." being "|elname|..." + StringTokenizer tokens = new StringTokenizer ( + t.model.substring (8, t.model.lastIndexOf (')')), + "|"); + Vector vec = new Vector (); + + while (tokens.hasMoreTokens ()) { + String token = tokens.nextToken (); + + if (vec.contains (token)) + v.error ("element " + token + + " is repeated in mixed content model: " + + t.model); + else + vec.addElement (token.intern ()); + } + permitted = new String [vec.size ()]; + for (int i = 0; i < permitted.length; i++) + permitted [i] = (String) vec.elementAt (i); + + // in one large machine-derived DTD sample, most of about + // 250 mixed content models were empty, and 25 had ten or + // more entries. 2 had over a hundred elements. Linear + // search isn't obviously wrong. + } + + // VC: Element Valid (third clause) + Recognizer acceptElement (String name) + { + int length = permitted.length; + + // first pass -- optimistic w.r.t. event source interning + // (and document validity) + for (int i = 0; i < length; i++) + if (permitted [i] == name) + return this; + // second pass -- pessimistic w.r.t. event source interning + for (int i = 0; i < length; i++) + if (permitted [i].equals (name)) + return this; + return null; + } + } + + + // recognizer loop flags, see later + private static final int F_LOOPHEAD = 0x01; + private static final int F_LOOPNEXT = 0x02; + + // for debugging -- used to label/count nodes in toString() + private static int nodeCount; + + /** + * "Children" content model -- these are nodes in NDFA state graphs. + * They work in fixed space. Note that these graphs commonly have + * cycles, handling features such as zero-or-more and one-or-more. + * + * <p>It's readonly, so only one copy is ever needed. The content model + * stack may have any number of pointers into each graph, when a model + * happens to be needed more than once due to element nesting. Since + * traversing the graph just moves to another node, and never changes + * it, traversals never interfere with each other. + * + * <p>There is an option to report non-deterministic models. These are + * always XML errors, but ones which are not often reported despite the + * fact that they can lead to different validating parsers giving + * different results for the same input. (The XML spec doesn't require + * them to be reported.) + * + * <p><b>FIXME</b> There's currently at least one known bug here, in that + * it's not actually detecting the non-determinism it tries to detect. + * (Of the "optional.xml" test, the once-or-twice-2* tests are all non-D; + * maybe some others.) This may relate to the issue flagged below as + * "should not" happen (but it was), which showed up when patching the + * graph to have one exit node (or more EMPTY nodes). + */ + private static final class ChildrenRecognizer extends Recognizer + implements Cloneable + { + // for reporting non-deterministic content models + // ... a waste of space if we're not reporting those! + // ... along with the 'model' member (in base class) + private ValidationConsumer consumer; + + // for CHOICE nodes -- each component is an arc that + // accepts a different NAME (or is EMPTY indicating + // NDFA termination). + private Recognizer components []; + + // for NAME/SEQUENCE nodes -- accepts that NAME and + // then goes to the next node (CHOICE, NAME, EMPTY). + private String name; + private Recognizer next; + + // loops always point back to a CHOICE node. we mark such choice + // nodes (F_LOOPHEAD) for diagnostics and faster deep cloning. + // We also mark nodes before back pointers (F_LOOPNEXT), to ensure + // termination when we patch sequences and loops. + private int flags; + + + // prevent a needless indirection between 'this' and 'node' + private void copyIn (ChildrenRecognizer node) + { + // model & consumer are already set + components = node.components; + name = node.name; + next = node.next; + flags = node.flags; + } + + // used to construct top level "children" content models, + public ChildrenRecognizer (ElementInfo type, ValidationConsumer vc) + { + this (vc, type); + populate (type.model.toCharArray (), 0); + patchNext (new EmptyRecognizer (type), null); + } + + // used internally; populating is separate + private ChildrenRecognizer (ValidationConsumer vc, ElementInfo type) + { + super (type); + consumer = vc; + } + + + // + // When rewriting some graph nodes we need deep clones in one case; + // mostly shallow clones (what the JVM handles for us) are fine. + // + private ChildrenRecognizer shallowClone () + { + try { + return (ChildrenRecognizer) clone (); + } catch (CloneNotSupportedException e) { + throw new Error ("clone"); + } + } + + private ChildrenRecognizer deepClone () + { + return deepClone (new Hashtable (37)); + } + + private ChildrenRecognizer deepClone (Hashtable table) + { + ChildrenRecognizer retval; + + if ((flags & F_LOOPHEAD) != 0) { + retval = (ChildrenRecognizer) table.get (this); + if (retval != null) + return this; + + retval = shallowClone (); + table.put (this, retval); + } else + retval = shallowClone (); + + if (next != null) { + if (next instanceof ChildrenRecognizer) + retval.next = ((ChildrenRecognizer)next) + .deepClone (table); + else if (!(next instanceof EmptyRecognizer)) + throw new RuntimeException ("deepClone"); + } + + if (components != null) { + retval.components = new Recognizer [components.length]; + for (int i = 0; i < components.length; i++) { + Recognizer temp = components [i]; + + if (temp == null) + retval.components [i] = null; + else if (temp instanceof ChildrenRecognizer) + retval.components [i] = ((ChildrenRecognizer)temp) + .deepClone (table); + else if (!(temp instanceof EmptyRecognizer)) + throw new RuntimeException ("deepClone"); + } + } + + return retval; + } + + // connect subgraphs, first to next (sequencing) + private void patchNext (Recognizer theNext, Hashtable table) + { + // backpointers must not be repatched or followed + if ((flags & F_LOOPNEXT) != 0) + return; + + // XXX this table "shouldn't" be needed, right? + // but some choice nodes looped if it isn't there. + if (table != null && table.get (this) != null) + return; + if (table == null) + table = new Hashtable (); + + // NAME/SEQUENCE + if (name != null) { + if (next == null) + next = theNext; + else if (next instanceof ChildrenRecognizer) { + ((ChildrenRecognizer)next).patchNext (theNext, table); + } else if (!(next instanceof EmptyRecognizer)) + throw new RuntimeException ("patchNext"); + return; + } + + // CHOICE + for (int i = 0; i < components.length; i++) { + if (components [i] == null) + components [i] = theNext; + else if (components [i] instanceof ChildrenRecognizer) { + ((ChildrenRecognizer)components [i]) + .patchNext (theNext, table); + } else if (!(components [i] instanceof EmptyRecognizer)) + throw new RuntimeException ("patchNext"); + } + + if (table != null && (flags | F_LOOPHEAD) != 0) + table.put (this, this); + } + + /** + * Parses a 'children' spec (or recursively 'cp') and makes this + * become a regular graph node. + * + * @return index after this particle + */ + private int populate (char parseBuf [], int startPos) + { + int nextPos = startPos + 1; + char c; + + if (nextPos < 0 || nextPos >= parseBuf.length) + throw new IndexOutOfBoundsException (); + + // Grammar of the string is from the XML spec, but + // with whitespace removed by the SAX parser. + + // children ::= (choice | seq) ('?' | '*' | '+')? + // cp ::= (Name | choice | seq) ('?' | '*' | '+')? + // choice ::= '(' cp ('|' choice)* ')' + // seq ::= '(' cp (',' choice)* ')' + + // interior nodes only + // cp ::= name ... + if (parseBuf [startPos] != '('/*)*/) { + boolean done = false; + do { + switch (c = parseBuf [nextPos]) { + case '?': case '*': case '+': + case '|': case ',': + case /*(*/ ')': + done = true; + continue; + default: + nextPos++; + continue; + } + } while (!done); + name = new String (parseBuf, startPos, nextPos - startPos); + + // interior OR toplevel nodes + // cp ::= choice .. + // cp ::= seq .. + } else { + // collect everything as a separate list, and merge it + // into "this" later if we can (SEQUENCE or singleton) + ChildrenRecognizer first; + + first = new ChildrenRecognizer (consumer, type); + nextPos = first.populate (parseBuf, nextPos); + c = parseBuf [nextPos++]; + + if (c == ',' || c == '|') { + ChildrenRecognizer current = first; + char separator = c; + Vector v = null; + + if (separator == '|') { + v = new Vector (); + v.addElement (first); + } + + do { + ChildrenRecognizer link; + + link = new ChildrenRecognizer (consumer, type); + nextPos = link.populate (parseBuf, nextPos); + + if (separator == ',') { + current.patchNext (link, null); + current = link; + } else + v.addElement (link); + + c = parseBuf [nextPos++]; + } while (c == separator); + + // choice ... collect everything into one array. + if (separator == '|') { + // assert v.size() > 1 + components = new Recognizer [v.size ()]; + for (int i = 0; i < components.length; i++) { + components [i] = (Recognizer) + v.elementAt (i); + } + // assert flags == 0 + + // sequence ... merge into "this" to be smaller. + } else + copyIn (first); + + // treat singletons like one-node sequences. + } else + copyIn (first); + + if (c != /*(*/ ')') + throw new RuntimeException ("corrupt content model"); + } + + // + // Arity is optional, and the root of all fun. We keep the + // FSM state graph simple by only having NAME/SEQUENCE and + // CHOICE nodes (or EMPTY to terminate a model), easily + // evaluated. So we rewrite each node that has arity, using + // those primitives. We create loops here, if needed. + // + if (nextPos < parseBuf.length) { + c = parseBuf [nextPos]; + if (c == '?' || c == '*' || c == '+') { + nextPos++; + + // Rewrite 'zero-or-one' "?" arity to a CHOICE: + // - SEQUENCE (clone, what's next) + // - or, what's next + // Size cost: N --> N + 1 + if (c == '?') { + Recognizer once = shallowClone (); + + components = new Recognizer [2]; + components [0] = once; + // components [1] initted to null + name = null; + next = null; + flags = 0; + + + // Rewrite 'zero-or-more' "*" arity to a CHOICE. + // - LOOP (clone, back to this CHOICE) + // - or, what's next + // Size cost: N --> N + 1 + } else if (c == '*') { + ChildrenRecognizer loop = shallowClone (); + + loop.patchNext (this, null); + loop.flags |= F_LOOPNEXT; + flags = F_LOOPHEAD; + + components = new Recognizer [2]; + components [0] = loop; + // components [1] initted to null + name = null; + next = null; + + + // Rewrite 'one-or-more' "+" arity to a SEQUENCE. + // Basically (a)+ --> ((a),(a)*). + // - this + // - CHOICE + // * LOOP (clone, back to the CHOICE) + // * or, whatever's next + // Size cost: N --> 2N + 1 + } else if (c == '+') { + ChildrenRecognizer loop = deepClone (); + ChildrenRecognizer choice; + + choice = new ChildrenRecognizer (consumer, type); + loop.patchNext (choice, null); + loop.flags |= F_LOOPNEXT; + choice.flags = F_LOOPHEAD; + + choice.components = new Recognizer [2]; + choice.components [0] = loop; + // choice.components [1] initted to null + // choice.name, choice.next initted to null + + patchNext (choice, null); + } + } + } + + return nextPos; + } + + // VC: Element Valid (second clause) + boolean acceptCharacters () + { return false; } + + // VC: Element Valid (second clause) + Recognizer acceptElement (String type) + throws SAXException + { + // NAME/SEQUENCE + if (name != null) { + if (name.equals (type)) + return next; + return null; + } + + // CHOICE ... optionally reporting nondeterminism we + // run across. we won't check out every transition + // for nondeterminism; only the ones we follow. + Recognizer retval = null; + + for (int i = 0; i < components.length; i++) { + Recognizer temp = components [i].acceptElement (type); + + if (temp == null) + continue; + else if (!warnNonDeterministic) + return temp; + else if (retval == null) + retval = temp; + else if (retval != temp) + consumer.error ("Content model " + this.type.model + + " is non-deterministic for " + type); + } + return retval; + } + + // VC: Element Valid (second clause) + boolean completed () + throws SAXException + { + // expecting a specific element + if (name != null) + return false; + + // choice, some sequences + for (int i = 0; i < components.length; i++) { + if (components [i].completed ()) + return true; + } + + return false; + } + +/** / + // FOR DEBUGGING ... flattens the graph for printing. + + public String toString () + { + StringBuffer buf = new StringBuffer (); + + // only one set of loop labels can be generated + // at a time... + synchronized (ANY) { + nodeCount = 0; + + toString (buf, new Hashtable ()); + return buf.toString (); + } + } + + private void toString (StringBuffer buf, Hashtable table) + { + // When we visit a node, label and count it. + // Nodes are never visited/counted more than once. + // For small models labels waste space, but if arity + // mappings were used the savings are substantial. + // (Plus, the output can be more readily understood.) + String temp = (String) table.get (this); + + if (temp != null) { + buf.append ('{'); + buf.append (temp); + buf.append ('}'); + return; + } else { + StringBuffer scratch = new StringBuffer (15); + + if ((flags & F_LOOPHEAD) != 0) + scratch.append ("loop"); + else + scratch.append ("node"); + scratch.append ('-'); + scratch.append (++nodeCount); + temp = scratch.toString (); + + table.put (this, temp); + buf.append ('['); + buf.append (temp); + buf.append (']'); + buf.append (':'); + } + + // NAME/SEQUENCE + if (name != null) { + // n.b. some output encodings turn some name chars into '?' + // e.g. with Japanese names and ASCII output + buf.append (name); + if (components != null) // bug! + buf.append ('$'); + if (next == null) + buf.append (",*"); + else if (next instanceof EmptyRecognizer) // patch-to-next + buf.append (",{}"); + else if (next instanceof ChildrenRecognizer) { + buf.append (','); + ((ChildrenRecognizer)next).toString (buf, table); + } else // bug! + buf.append (",+"); + return; + } + + // CHOICE + buf.append ("<"); + for (int i = 0; i < components.length; i++) { + if (i != 0) + buf.append ("|"); + if (components [i] instanceof EmptyRecognizer) { + buf.append ("{}"); + } else if (components [i] == null) { // patch-to-next + buf.append ('*'); + } else { + ChildrenRecognizer r; + + r = (ChildrenRecognizer) components [i]; + r.toString (buf, table); + } + } + buf.append (">"); + } +/**/ + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java b/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java new file mode 100644 index 00000000000..ef430165203 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java @@ -0,0 +1,363 @@ +/* WellFormednessFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.util.EmptyStackException; +import java.util.Stack; + +import org.xml.sax.Attributes; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * This filter reports fatal exceptions in the case of event streams that + * are not well formed. The rules currently tested include: <ul> + * + * <li>setDocumentLocator ... may be called only before startDocument + * + * <li>startDocument/endDocument ... must be paired, and all other + * calls (except setDocumentLocator) must be nested within these. + * + * <li>startElement/endElement ... must be correctly paired, and + * may never appear within CDATA sections. + * + * <li>comment ... can't contain "--" + * + * <li>character data ... can't contain "]]>" + * + * <li>whitespace ... can't contain CR + * + * <li>whitespace and character data must be within an element + * + * <li>processing instruction ... can't contain "?>" or CR + * + * <li>startCDATA/endCDATA ... must be correctly paired. + * + * </ul> + * + * <p> Other checks for event stream correctness may be provided in + * the future. For example, insisting that + * entity boundaries nest correctly, + * namespace scopes nest correctly, + * namespace values never contain relative URIs, + * attributes don't have "<" characters; + * and more. + * + * @author David Brownell + */ +public final class WellFormednessFilter extends EventFilter +{ + private boolean startedDoc; + private Stack elementStack = new Stack (); + private boolean startedCDATA; + private String dtdState = "before"; + + + /** + * Swallows all events after performing well formedness checks. + */ + // constructor used by PipelineFactory + public WellFormednessFilter () + { this (null); } + + + /** + * Passes events through to the specified consumer, after first + * processing them. + */ + // constructor used by PipelineFactory + public WellFormednessFilter (EventConsumer consumer) + { + super (consumer); + + setContentHandler (this); + setDTDHandler (this); + + try { + setProperty (LEXICAL_HANDLER, this); + } catch (SAXException e) { /* can't happen */ } + } + + /** + * Resets state as if any preceding event stream was well formed. + * Particularly useful if it ended through some sort of error, + * and the endDocument call wasn't made. + */ + public void reset () + { + startedDoc = false; + startedCDATA = false; + elementStack.removeAllElements (); + } + + + private SAXParseException getException (String message) + { + SAXParseException e; + Locator locator = getDocumentLocator (); + + if (locator == null) + return new SAXParseException (message, null, null, -1, -1); + else + return new SAXParseException (message, locator); + } + + private void fatalError (String message) + throws SAXException + { + SAXParseException e = getException (message); + ErrorHandler handler = getErrorHandler (); + + if (handler != null) + handler.fatalError (e); + throw e; + } + + /** + * Throws an exception when called after startDocument. + * + * @param locator the locator, to be used in error reporting or relative + * URI resolution. + * + * @exception IllegalStateException when called after the document + * has already been started + */ + public void setDocumentLocator (Locator locator) + { + if (startedDoc) + throw new IllegalStateException ( + "setDocumentLocator called after startDocument"); + super.setDocumentLocator (locator); + } + + public void startDocument () throws SAXException + { + if (startedDoc) + fatalError ("startDocument called more than once"); + startedDoc = true; + startedCDATA = false; + elementStack.removeAllElements (); + super.startDocument (); + } + + public void startElement ( + String uri, String localName, + String qName, Attributes atts + ) throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if ("inside".equals (dtdState)) + fatalError ("element inside DTD?"); + else + dtdState = "after"; + if (startedCDATA) + fatalError ("element inside CDATA section"); + if (qName == null || "".equals (qName)) + fatalError ("startElement name missing"); + elementStack.push (qName); + super.startElement (uri, localName, qName, atts); + } + + public void endElement (String uri, String localName, String qName) + throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if (startedCDATA) + fatalError ("element inside CDATA section"); + if (qName == null || "".equals (qName)) + fatalError ("endElement name missing"); + + try { + String top = (String) elementStack.pop (); + + if (!qName.equals (top)) + fatalError ("<" + top + " ...>...</" + qName + ">"); + // XXX could record/test namespace info + } catch (EmptyStackException e) { + fatalError ("endElement without startElement: </" + qName + ">"); + } + super.endElement (uri, localName, qName); + } + + public void endDocument () throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + dtdState = "before"; + startedDoc = false; + super.endDocument (); + } + + + public void startDTD (String root, String publicId, String systemId) + throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if ("before" != dtdState) + fatalError ("two DTDs?"); + if (!elementStack.empty ()) + fatalError ("DTD must precede root element"); + dtdState = "inside"; + super.startDTD (root, publicId, systemId); + } + + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { +// FIXME: not all parsers will report startDTD() ... +// we'd rather insist we're "inside". + if ("after" == dtdState) + fatalError ("not inside DTD"); + super.notationDecl (name, publicId, systemId); + } + + public void unparsedEntityDecl (String name, + String publicId, String systemId, String notationName) + throws SAXException + { +// FIXME: not all parsers will report startDTD() ... +// we'd rather insist we're "inside". + if ("after" == dtdState) + fatalError ("not inside DTD"); + super.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + // FIXME: add the four DeclHandler calls too + + public void endDTD () + throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if ("inside" != dtdState) + fatalError ("DTD ends without start?"); + dtdState = "after"; + super.endDTD (); + } + + public void characters (char ch [], int start, int length) + throws SAXException + { + int here = start, end = start + length; + if (elementStack.empty ()) + fatalError ("characters must be in an element"); + while (here < end) { + if (ch [here++] != ']') + continue; + if (here == end) // potential problem ... + continue; + if (ch [here++] != ']') + continue; + if (here == end) // potential problem ... + continue; + if (ch [here++] == '>') + fatalError ("character data can't contain \"]]>\""); + } + super.characters (ch, start, length); + } + + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + int here = start, end = start + length; + if (elementStack.empty ()) + fatalError ("characters must be in an element"); + while (here < end) { + if (ch [here++] == '\r') + fatalError ("whitespace can't contain CR"); + } + super.ignorableWhitespace (ch, start, length); + } + + public void processingInstruction (String target, String data) + throws SAXException + { + if (data.indexOf ('\r') > 0) + fatalError ("PIs can't contain CR"); + if (data.indexOf ("?>") > 0) + fatalError ("PIs can't contain \"?>\""); + } + + public void comment (char ch [], int start, int length) + throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if (startedCDATA) + fatalError ("comments can't nest in CDATA"); + int here = start, end = start + length; + while (here < end) { + if (ch [here] == '\r') + fatalError ("comments can't contain CR"); + if (ch [here++] != '-') + continue; + if (here == end) + fatalError ("comments can't end with \"--->\""); + if (ch [here++] == '-') + fatalError ("comments can't contain \"--\""); + } + super.comment (ch, start, length); + } + + public void startCDATA () + throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if (startedCDATA) + fatalError ("CDATA starts can't nest"); + startedCDATA = true; + super.startCDATA (); + } + + public void endCDATA () + throws SAXException + { + if (!startedDoc) + fatalError ("callback outside of document?"); + if (!startedCDATA) + fatalError ("CDATA end without start?"); + startedCDATA = false; + super.endCDATA (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java b/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java new file mode 100644 index 00000000000..02607a4e06d --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java @@ -0,0 +1,579 @@ +/* XIncludeFilter.java -- + Copyright (C) 2001,2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.Hashtable; +import java.util.Stack; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +import gnu.xml.util.Resolver; + + + +/** + * Filter to process an XPointer-free subset of + * <a href="http://www.w3.org/TR/xinclude">XInclude</a>, supporting its + * use as a kind of replacement for parsed general entities. + * XInclude works much like the <code>#include</code> of C/C++ but + * works for XML documents as well as unparsed text files. + * Restrictions from the 17-Sept-2002 CR draft of XInclude are as follows: + * + * <ul> + * + * <li> URIs must not include fragment identifiers. + * The CR specifies support for XPointer <em>element()</em> fragment IDs, + * which is not currently implemented here. + * + * <li> <em>xi:fallback</em> handling of resource errors is not + * currently supported. + * + * <li> DTDs are not supported in included files, since the SAX DTD events + * must have completely preceded any included file. + * The CR explicitly allows the DTD related portions of the infoset to + * grow as an effect of including XML documents. + * + * <li> <em>xml:base</em> fixup isn't done. + * + * </ul> + * + * <p> XML documents that are included will normally be processed using + * the default SAX namespace rules, meaning that prefix information may + * be discarded. This may be changed with {@link #setSavingPrefixes + * setSavingPrefixes()}. <em>You are strongly advised to do this.</em> + * + * <p> Note that XInclude allows highly incompatible implementations, which + * are specialized to handle application-specific infoset extensions. Some + * such implementations can be implemented by subclassing this one, but + * they may only be substituted in applications at "user option". + * + * <p>TBD: "IURI" handling. + * + * @author David Brownell + */ +public class XIncludeFilter extends EventFilter implements Locator +{ + private Hashtable extEntities = new Hashtable (5, 5); + private int ignoreCount; + private Stack uris = new Stack (); + private Locator locator; + private Vector inclusions = new Vector (5, 5); + private boolean savingPrefixes; + + /** + */ + public XIncludeFilter (EventConsumer next) + throws SAXException + { + super (next); + setContentHandler (this); + // DTDHandler callbacks pass straight through + setProperty (DECL_HANDLER, this); + setProperty (LEXICAL_HANDLER, this); + } + + private void fatal (SAXParseException e) throws SAXException + { + ErrorHandler eh; + + eh = getErrorHandler (); + if (eh != null) + eh.fatalError (e); + throw e; + } + + /** + * Passes "this" down the filter chain as a proxy locator. + */ + public void setDocumentLocator (Locator locator) + { + this.locator = locator; + super.setDocumentLocator (this); + } + + /** Used for proxy locator; do not call directly. */ + public String getSystemId () + { return (locator == null) ? null : locator.getSystemId (); } + /** Used for proxy locator; do not call directly. */ + public String getPublicId () + { return (locator == null) ? null : locator.getPublicId (); } + /** Used for proxy locator; do not call directly. */ + public int getLineNumber () + { return (locator == null) ? -1 : locator.getLineNumber (); } + /** Used for proxy locator; do not call directly. */ + public int getColumnNumber () + { return (locator == null) ? -1 : locator.getColumnNumber (); } + + /** + * Assigns the flag controlling the setting of the SAX2 + * <em>namespace-prefixes</em> flag. + */ + public void setSavingPrefixes (boolean flag) + { savingPrefixes = flag; } + + /** + * Returns the flag controlling the setting of the SAX2 + * <em>namespace-prefixes</em> flag when parsing included documents. + * The default value is the SAX2 default (false), which discards + * information that can be useful. + */ + public boolean isSavingPrefixes () + { return savingPrefixes; } + + // + // Two mechanisms are interacting here. + // + // - XML Base implies a stack of base URIs, updated both by + // "real entity" boundaries and element boundaries. + // + // - Active "Real Entities" (for document and general entities, + // and by xincluded files) are tracked to prevent circular + // inclusions. + // + private String addMarker (String uri) + throws SAXException + { + if (locator != null && locator.getSystemId () != null) + uri = locator.getSystemId (); + + // guard against InputSource objects without system IDs + if (uri == null) + fatal (new SAXParseException ("Entity URI is unknown", locator)); + + try { + URL url = new URL (uri); + + uri = url.toString (); + if (inclusions.contains (uri)) + fatal (new SAXParseException ( + "XInclude, circular inclusion", locator)); + inclusions.addElement (uri); + uris.push (url); + } catch (IOException e) { + // guard against illegal relative URIs (Xerces) + fatal (new SAXParseException ("parser bug: relative URI", + locator, e)); + } + return uri; + } + + private void pop (String uri) + { + inclusions.removeElement (uri); + uris.pop (); + } + + // + // Document entity boundaries get both treatments. + // + public void startDocument () throws SAXException + { + ignoreCount = 0; + addMarker (null); + super.startDocument (); + } + + public void endDocument () throws SAXException + { + inclusions.setSize (0); + extEntities.clear (); + uris.setSize (0); + super.endDocument (); + } + + // + // External general entity boundaries get both treatments. + // + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + if (name.charAt (0) == '%') + return; + try { + URL url = new URL (locator.getSystemId ()); + systemId = new URL (url, systemId).toString (); + } catch (IOException e) { + // what could we do? + } + extEntities.put (name, systemId); + } + + public void startEntity (String name) + throws SAXException + { + if (ignoreCount != 0) { + ignoreCount++; + return; + } + + String uri = (String) extEntities.get (name); + if (uri != null) + addMarker (uri); + super.startEntity (name); + } + + public void endEntity (String name) + throws SAXException + { + if (ignoreCount != 0) { + if (--ignoreCount != 0) + return; + } + + String uri = (String) extEntities.get (name); + + if (uri != null) + pop (uri); + super.endEntity (name); + } + + // + // element boundaries only affect the base URI stack, + // unless they're XInclude elements. + // + public void + startElement (String uri, String localName, String qName, Attributes atts) + throws SAXException + { + if (ignoreCount != 0) { + ignoreCount++; + return; + } + + URL baseURI = (URL) uris.peek (); + String base; + + base = atts.getValue ("http://www.w3.org/XML/1998/namespace", "base"); + if (base == null) + uris.push (baseURI); + else { + URL url; + + if (base.indexOf ('#') != -1) + fatal (new SAXParseException ( + "xml:base with fragment: " + base, + locator)); + + try { + baseURI = new URL (baseURI, base); + uris.push (baseURI); + } catch (Exception e) { + fatal (new SAXParseException ( + "xml:base with illegal uri: " + base, + locator, e)); + } + } + + if (!"http://www.w3.org/2001/XInclude".equals (uri)) { + super.startElement (uri, localName, qName, atts); + return; + } + + if ("include".equals (localName)) { + String href = atts.getValue ("href"); + String parse = atts.getValue ("parse"); + String encoding = atts.getValue ("encoding"); + URL url = (URL) uris.peek (); + SAXParseException x = null; + + if (href == null) + fatal (new SAXParseException ( + "XInclude missing href", + locator)); + if (href.indexOf ('#') != -1) + fatal (new SAXParseException ( + "XInclude with fragment: " + href, + locator)); + + if (parse == null || "xml".equals (parse)) + x = xinclude (url, href); + else if ("text".equals (parse)) + x = readText (url, href, encoding); + else + fatal (new SAXParseException ( + "unknown XInclude parsing mode: " + parse, + locator)); + if (x == null) { + // strip out all child content + ignoreCount++; + return; + } + + // FIXME the 17-Sept-2002 CR of XInclude says we "must" + // use xi:fallback elements to handle resource errors, + // if they exist. + fatal (x); + + } else if ("fallback".equals (localName)) { + fatal (new SAXParseException ( + "illegal top level XInclude 'fallback' element", + locator)); + } else { + ErrorHandler eh = getErrorHandler (); + + // CR doesn't say this is an error + if (eh != null) + eh.warning (new SAXParseException ( + "unrecognized toplevel XInclude element: " + localName, + locator)); + super.startElement (uri, localName, qName, atts); + } + } + + public void endElement (String uri, String localName, String qName) + throws SAXException + { + if (ignoreCount != 0) { + if (--ignoreCount != 0) + return; + } + + uris.pop (); + if (!("http://www.w3.org/2001/XInclude".equals (uri) + && "include".equals (localName))) + super.endElement (uri, localName, qName); + } + + // + // ignore all content within non-empty xi:include elements + // + public void characters (char ch [], int start, int length) + throws SAXException + { + if (ignoreCount == 0) + super.characters (ch, start, length); + } + + public void processingInstruction (String target, String value) + throws SAXException + { + if (ignoreCount == 0) + super.processingInstruction (target, value); + } + + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + if (ignoreCount == 0) + super.ignorableWhitespace (ch, start, length); + } + + public void comment (char ch [], int start, int length) + throws SAXException + { + if (ignoreCount == 0) + super.comment (ch, start, length); + } + + public void startCDATA () throws SAXException + { + if (ignoreCount == 0) + super.startCDATA (); + } + + public void endCDATA () throws SAXException + { + if (ignoreCount == 0) + super.endCDATA (); + } + + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (ignoreCount == 0) + super.startPrefixMapping (prefix, uri); + } + + public void endPrefixMapping (String prefix) throws SAXException + { + if (ignoreCount == 0) + super.endPrefixMapping (prefix); + } + + public void skippedEntity (String name) throws SAXException + { + if (ignoreCount == 0) + super.skippedEntity (name); + } + + // JDK 1.1 seems to need it to be done this way, sigh + void setLocator (Locator l) { locator = l; } + Locator getLocator () { return locator; } + + + // + // for XIncluded entities, manage the current locator and + // filter out events that would be incorrect to report + // + private class Scrubber extends EventFilter + { + Scrubber (EventFilter f) + throws SAXException + { + // delegation passes to next in chain + super (f); + + // process all content events + super.setContentHandler (this); + super.setProperty (LEXICAL_HANDLER, this); + + // drop all DTD events + super.setDTDHandler (null); + super.setProperty (DECL_HANDLER, null); + } + + // maintain proxy locator + // only one startDocument()/endDocument() pair per event stream + public void setDocumentLocator (Locator l) + { setLocator (l); } + public void startDocument () + { } + public void endDocument () + { } + + private void reject (String message) throws SAXException + { fatal (new SAXParseException (message, getLocator ())); } + + // only the DTD from the "base document" gets reported + public void startDTD (String root, String publicId, String systemId) + throws SAXException + { reject ("XIncluded DTD: " + systemId); } + public void endDTD () + throws SAXException + { reject ("XIncluded DTD"); } + // ... so this should never happen + public void skippedEntity (String name) throws SAXException + { reject ("XInclude skipped entity: " + name); } + + // since we rejected DTDs, only builtin entities can be reported + } + + // <xi:include parse='xml' ...> + // relative to the base URI passed + private SAXParseException xinclude (URL url, String href) + throws SAXException + { + XMLReader helper; + Scrubber scrubber; + Locator savedLocator = locator; + + // start with a parser acting just like our input + // modulo DTD-ish stuff (validation flag, entity resolver) + helper = XMLReaderFactory.createXMLReader (); + helper.setErrorHandler (getErrorHandler ()); + helper.setFeature (FEATURE_URI + "namespace-prefixes", true); + + // Set up the proxy locator and event filter. + scrubber = new Scrubber (this); + locator = null; + bind (helper, scrubber); + + // Merge the included document, except its DTD + try { + url = new URL (url, href); + href = url.toString (); + + if (inclusions.contains (href)) + fatal (new SAXParseException ( + "XInclude, circular inclusion", locator)); + + inclusions.addElement (href); + uris.push (url); + helper.parse (new InputSource (href)); + return null; + } catch (java.io.IOException e) { + return new SAXParseException (href, locator, e); + } finally { + pop (href); + locator = savedLocator; + } + } + + // <xi:include parse='text' ...> + // relative to the base URI passed + private SAXParseException readText (URL url, String href, String encoding) + throws SAXException + { + InputStream in = null; + + try { + URLConnection conn; + InputStreamReader reader; + char buf [] = new char [4096]; + int count; + + url = new URL (url, href); + conn = url.openConnection (); + in = conn.getInputStream (); + if (encoding == null) + encoding = Resolver.getEncoding (conn.getContentType ()); + if (encoding == null) { + ErrorHandler eh = getErrorHandler (); + if (eh != null) + eh.warning (new SAXParseException ( + "guessing text encoding for URL: " + url, + locator)); + reader = new InputStreamReader (in); + } else + reader = new InputStreamReader (in, encoding); + + while ((count = reader.read (buf, 0, buf.length)) != -1) + super.characters (buf, 0, count); + in.close (); + return null; + } catch (IOException e) { + return new SAXParseException ( + "can't XInclude text", + locator, e); + } + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/XsltFilter.java b/libjava/classpath/gnu/xml/pipeline/XsltFilter.java new file mode 100644 index 00000000000..0a1872c5a80 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/XsltFilter.java @@ -0,0 +1,130 @@ +/* XsltFilter.java -- + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; + +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.sax.*; +import javax.xml.transform.stream.StreamSource; + +import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; + + +/** + * Packages an XSLT transform as a pipeline component. + * Note that all DTD events (callbacks to DeclHandler and DTDHandler + * interfaces) are discarded, although XSLT transforms may be set up to + * use the LexicalHandler to write DTDs with only an external subset. + * Not every XSLT engine will necessarily be usable with this filter, + * but current versions of + * <a href="http://saxon.sourceforge.net">SAXON</a> and + * <a href="http://xml.apache.org/xalan-j">Xalan</a> should work well. + * + * @see TransformerFactory + * + * @author David Brownell + */ +final public class XsltFilter extends EventFilter +{ + /** + * Creates a filter that performs the specified transform. + * Uses the JAXP 1.1 interfaces to access the default XSLT + * engine configured for in the current execution context, + * and parses the stylesheet without custom EntityResolver + * or ErrorHandler support. + * + * @param stylesheet URI for the stylesheet specifying the + * XSLT transform + * @param next provides the ContentHandler and LexicalHandler + * to receive XSLT output. + * @exception SAXException if the stylesheet can't be parsed + * @exception IOException if there are difficulties + * bootstrapping the XSLT engine, such as it not supporting + * SAX well enough to use this way. + */ + public XsltFilter (String stylesheet, EventConsumer next) + throws SAXException, IOException + { + // First, get a transformer with the stylesheet preloaded + TransformerFactory tf = null; + TransformerHandler th; + + try { + SAXTransformerFactory stf; + + tf = TransformerFactory.newInstance (); + if (!tf.getFeature (SAXTransformerFactory.FEATURE) // sax inputs + || !tf.getFeature (SAXResult.FEATURE) // sax outputs + || !tf.getFeature (StreamSource.FEATURE) // stylesheet + ) + throw new IOException ("XSLT factory (" + + tf.getClass ().getName () + + ") does not support SAX"); + stf = (SAXTransformerFactory) tf; + th = stf.newTransformerHandler (new StreamSource (stylesheet)); + } catch (TransformerConfigurationException e) { + throw new IOException ("XSLT factory (" + + (tf == null + ? "none available" + : tf.getClass ().getName ()) + + ") configuration error, " + + e.getMessage () + ); + } + + // Hook its outputs up to the pipeline ... + SAXResult out = new SAXResult (); + + out.setHandler (next.getContentHandler ()); + try { + LexicalHandler lh; + lh = (LexicalHandler) next.getProperty (LEXICAL_HANDLER); + out.setLexicalHandler (lh); + } catch (Exception e) { + // ignore + } + th.setResult (out); + + // ... and make sure its inputs look like ours. + setContentHandler (th); + setProperty (LEXICAL_HANDLER, th); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/package.html b/libjava/classpath/gnu/xml/pipeline/package.html new file mode 100644 index 00000000000..352f4c87c2c --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/package.html @@ -0,0 +1,255 @@ +<html><head><title> +blah +<!-- +/* + * Copyright (C) 1999-2001 The Free Software Foundation, Inc. + */ +--> +</title></head><body> + +<p>This package exposes a kind of XML processing pipeline, based on sending +SAX events, which can be used as components of application architectures. +Pipelines are used to convey streams of processing events from a producer +to one or more consumers, and to let each consumer control the data seen by +later consumers. + +<p> There is a <a href="PipelineFactory.html">PipelineFactory</a> class which +accepts a syntax describing how to construct some simple pipelines. Strings +describing such pipelines can be used in command line tools (see the +<a href="../util/DoParse.html">DoParse</a> class) +and in other places that it is +useful to let processing be easily reconfigured. Pipelines can of course +be constructed programmatically, providing access to options that the +factory won't. + +<p> Web applications are supported by making it easy for servlets (or +non-Java web application components) to be part of a pipeline. They can +originate XML (or XHTML) data through an <em>InputSource</em> or in +response to XML messages sent from clients using <em>CallFilter</em> +pipeline stages. Such facilities are available using the simple syntax +for pipeline construction. + + +<h2> Programming Models </h2> + +<p> Pipelines should be simple to understand. + +<ul> + <li> XML content, typically entire documents, + is pushed through consumers by producers. + + <li> Pipelines are basically about consuming SAX2 callback events, + where the events encapsulate XML infoset-level data.<ul> + + <li> Pipelines are constructed by taking one or more consumer + stages and combining them to produce a composite consumer. + + <li> A pipeline is presumed to have pending tasks and state from + the beginning of its ContentHandler.startDocument() callback until + it's returned from its ContentHandler.doneDocument() callback. + + <li> Pipelines may have multiple output stages ("fan-out") + or multiple input stages ("fan-in") when appropriate. + + <li> Pipelines may be long-lived, but need not be. + + </ul> + + <li> There is flexibility about event production. <ul> + + <li> SAX2 XMLReader objects are producers, which + provide a high level "pull" model: documents (text or DOM) are parsed, + and the parser pushes individual events through the pipeline. + + <li> Events can be pushed directly to event consumer components + by application modules, if they invoke SAX2 callbacks directly. + That is, application modules use the XML Infoset as exposed + through SAX2 event callbacks. + + </ul> + + <li> Multiple producer threads may concurrently access a pipeline, + if they coordinate appropriately. + + <li> Pipeline processing is not the only framework applications + will use. + + </ul> + + +<h3> Producers: XMLReader or Custom </h3> + +<p> Many producers will be SAX2 XMLReader objects, and +will read (pull) data which is then written (pushed) as events. +Typically these will parse XML text (acquired from +<code>org.xml.sax.helpers.XMLReaderFactory</code>) or a DOM tree +(using a <code><a href="../util/DomParser.html">DomParser</a></code>) +These may be bound to event consumer using a convenience routine, +<em><a href="EventFilter.html">EventFilter</a>.bind()</em>. +Once bound, these producers may be given additional documents to +sent through its pipeline. + +<p> In other cases, you will write producers yourself. For example, some +data structures might know how to write themselves out using one or +more XML models, expressed as sequences of SAX2 event callbacks. +An application module might +itself be a producer, issuing startDocument and endDocument events +and then asking those data structures to write themselves out to a +given EventConsumer, or walking data structures (such as JDBC query +results) and applying its own conversion rules. WAP format XML +(WBMXL) can be directly converted to producer output. + +<p> SAX2 introduced an "XMLFilter" interface, which is a kind of XMLReader. +It is most useful in conjunction with its XMLFilterImpl helper class; +see the <em><a href="EventFilter.html">EventFilter</a></em> javadoc +for information contrasting that XMLFilterImpl approach with the +relevant parts of this pipeline framework. Briefly, such XMLFilterImpl +children can be either producers or consumers, and are more limited in +configuration flexibility. In this framework, the focus of filters is +on the EventConsumer side; see the section on +<a href="#fitting">pipe fitting</a> below. + + +<h3> Consume to Standard or Custom Data Representations </h3> + +<p> Many consumers will be used to create standard representations of XML +data. The <a href="TextConsumer.html">TextConsumer</a> takes its events +and writes them as text for a single XML document, +using an internal <a href="../util/XMLWriter.html">XMLWriter</a>. +The <a href="DomConsumer.html">DomConsumer</a> takes its events and uses +them to create and populate a DOM Document. + +<p> In other cases, you will write consumers yourself. For example, +you might use a particular unmarshaling filter to produce objects +that fit your application's requirements, instead of using DOM. +Such consumers work at the level of XML data models, rather than with +specific representations such as XML text or a DOM tree. You could +convert your output directly to WAP format data (WBXML). + + +<h3><a name="fitting">Pipe Fitting</a></h3> + +<p> Pipelines are composite event consumers, with each stage having +the opportunity to transform the data before delivering it to any +subsequent stages. + +<p> The <a href="PipelineFactory.html">PipelineFactory</a> class +provides access to much of this functionality through a simple syntax. +See the table in that class's javadoc describing a number of standard +components. Direct API calls are still needed for many of the most +interesting pipeline configurations, including ones leveraging actual +or logical concurrency. + +<p> Four basic types of pipe fitting are directly supported. These may +be used to construct complex pipeline networks. <ul> + + <li> <a href="TeeConsumer.html">TeeConsumer</a> objects split event + flow so it goes to two two different consumers, one before the other. + This is a basic form of event fan-out; you can use this class to + copy events to any number of output pipelines. + + <li> Clients can call remote components through HTTP or HTTPS using + the <a href="CallFilter.html">CallFilter</a> component, and Servlets + can implement such components by extending the + <a href="XmlServlet.html">XmlServlet</a> component. Java is not + required on either end, and transport protocols other than HTTP may + also be used. + + <li> <a href="EventFilter.html">EventFilter</a> objects selectively + provide handling for callbacks, and can pass unhandled ones to a + subsequent stage. They are often subclassed, since much of the + basic filtering machinery is already in place in the base class. + + <li> Applications can merge two event flows by just using the same + consumer in each one. If multiple threads are in use, synchronization + needs to be addressed by the appropriate application level policy. + + </ul> + +<p> Note that filters can be as complex as +<a href="XsltFilter.html">XSLT transforms</a> +available) on input data, or as simple as removing simple syntax data +such as ignorable whitespace, comments, and CDATA delimiters. +Some simple "built-in" filters are part of this package. + + +<h3> Coding Conventions: Filter and Terminus Stages</h3> + +<p> If you follow these coding conventions, your classes may be used +directly (give the full class name) in pipeline descriptions as understood +by the PipelineFactory. There are four constructors the factory may +try to use; in order of decreasing numbers of parameters, these are: <ul> + + <li> Filters that need a single String setup parameter should have + a public constructor with two parameters: that string, then the + EventConsumer holding the "next" consumer to get events. + + <li> Filters that don't need setup parameters should have a public + constructor that accepts a single EventConsumer holding the "next" + consumer to get events when they are done. + + <li> Terminus stages may have a public constructor taking a single + paramter: the string value of that parameter. + + <li> Terminus stages may have a public no-parameters constructor. + + </ul> + +<p> Of course, classes may support more than one such usage convention; +if they do, they can automatically be used in multiple modes. If you +try to use a terminus class as a filter, and that terminus has a constructor +with the appropriate number of arguments, it is automatically wrapped in +a "tee" filter. + + +<h2> Debugging Tip: "Tee" Joints can Snapshot Data</h2> + +<p> It can sometimes be hard to see what's happening, when something +goes wrong. Easily fixed: just snapshot the data. Then you can find +out where things start to go wrong. + +<p> If you're using pipeline descriptors so that they're easily +administered, just stick a <em>write ( filename )</em> +filter into the pipeline at an appropriate point. + +<p> Inside your programs, you can do the same thing directly: perhaps +by saving a Writer (perhaps a StringWriter) in a variable, using that +to create a TextConsumer, and making that the first part of a tee -- +splicing that into your pipeline at a convenient location. + +<p> You can also use a DomConsumer to buffer the data, but remember +that DOM doesn't save all the information that XML provides, so that DOM +snapshots are relatively low fidelity. They also are substantially more +expensive in terms of memory than a StringWriter holding similar data. + +<h2> Debugging Tip: Non-XML Producers</h2> + +<p> Producers in pipelines don't need to start from XML +data structures, such as text in XML syntax (likely coming +from some <em>XMLReader</em> that parses XML) or a +DOM representation (perhaps with a +<a href="../util/DomParser.html">DomParser</a>). + +<p> One common type of event producer will instead make +direct calls to SAX event handlers returned from an +<a href="EventConsumer.html">EventConsumer</a>. +For example, making <em>ContentHandler.startElement</em> +calls and matching <em>ContentHandler.endElement</em> calls. + +<p> Applications making such calls can catch certain +common "syntax errors" by using a +<a href="WellFormednessFilter.html">WellFormednessFilter</a>. +That filter will detect (and report) erroneous input data +such as mismatched document, element, or CDATA start/end calls. +Use such a filter near the head of the pipeline that your +producer feeds, at least while debugging, to help ensure that +you're providing legal XML Infoset data. + +<p> You can also arrange to validate data on the fly. +For DTD validation, you can configure a +<a href="ValidationConsumer.html">ValidationConsumer</a> +to work as a filter, using any DTD you choose. +Other validation schemes can be handled with other +validation filters. + +</body></html> diff --git a/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java b/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java new file mode 100644 index 00000000000..91029d6d070 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java @@ -0,0 +1,328 @@ +/* AbstractNumberNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import gnu.xml.xpath.Expr; + +/** + * A template node representing the XSL <code>number</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +abstract class AbstractNumberNode + extends TemplateNode +{ + + static final int ALPHABETIC = 0; + static final int TRADITIONAL = 1; + + final TemplateNode format; + final String lang; + final int letterValue; + final String groupingSeparator; + final int groupingSize; + + AbstractNumberNode(TemplateNode format, String lang, + int letterValue, String groupingSeparator, + int groupingSize) + { + this.format = format; + this.lang = lang; + this.letterValue = letterValue; + this.groupingSeparator = groupingSeparator; + this.groupingSize = groupingSize; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + DocumentFragment fragment = doc.createDocumentFragment(); + format.apply(stylesheet, mode, context, pos, len, fragment, null); + String f = Expr._string(context, Collections.singleton(fragment)); + String value = format(f, compute(stylesheet, context, pos, len)); + Text text = doc.createTextNode(value); + if (nextSibling != null) + { + parent.insertBefore(text, nextSibling); + } + else + { + parent.appendChild(text); + } + // xsl:number doesn't process children + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + String format(String format, int[] number) + { + if (number.length == 0) + { + return ""; + } + int start = 0, end = 0, len = format.length(); // region of format + // Tokenize + List tokens = new ArrayList((number.length * 2) + 1); + List types = new ArrayList(tokens.size()); + while (end < len) + { + while (end < len && !isAlphanumeric(format.charAt(end))) + { + end++; + } + if (end > start) + { + tokens.add(format.substring(start, end)); + types.add(Boolean.FALSE); + } + start = end; + while (end < len && isAlphanumeric(format.charAt(end))) + { + end++; + } + if (end > start) + { + tokens.add(format.substring(start, end)); + types.add(Boolean.TRUE); + } + start = end; + } + // Process tokens + StringBuffer buf = new StringBuffer(); + len = tokens.size(); + int pos = 0; + for (int i = 0; i < len; i++) + { + String token = (i < 0) ? "." : (String) tokens.get(i); + boolean alpha = (i < 0) ? true : + ((Boolean) types.get(i)).booleanValue(); + if (!alpha) + { + buf.append(token); + } + else + { + if (pos < number.length) + { + format(buf, number[pos++], token); + if (((i + 1 == len) || (i + 2 == len)) && + (pos < number.length)) + { + // More numbers than tokens, reuse last token + i -= 2; + } + } + if (pos == number.length && i < (len - 2)) + { + // No more numbers. Skip to the end... + i = len - 2; + if (((Boolean) types.get(i + 1)).booleanValue()) + { + // number formatting token, ignore + i++; + } + } + } + } + //System.err.println("format: '"+format+"' "+asList(number)+" = '"+buf.toString()+"'"); + return buf.toString(); + } + + /*List asList(int[] number) + { + List l = new ArrayList(); + for (int i = 0; i < number.length; i++) + l.add(new Integer(number[i])); + return l; + }*/ + + void format(StringBuffer buf, int number, String formatToken) + { + int len = formatToken.length(); + char c = formatToken.charAt(len - 1); + if (Character.digit(c, 10) == 1) + { + // Check preceding characters + for (int i = len - 2; i >= 0; i--) + { + if (formatToken.charAt(i) != (c - 1)) + { + format(buf, number, "1"); + return; + } + } + // Decimal representation + String val = Integer.toString(number); + for (int d = len - val.length(); d > 0; d--) + { + buf.append('0'); + } + buf.append(val); + } + else if ("A".equals(formatToken)) + { + buf.append(alphabetic('@', number)); + } + else if ("a".equals(formatToken)) + { + buf.append(alphabetic('`', number)); + } + else if ("i".equals(formatToken)) + { + buf.append(roman(false, number)); + } + else if ("I".equals(formatToken)) + { + buf.append(roman(true, number)); + } + else + { + // Unknown numbering sequence + format(buf, number, "1"); + } + } + + static final boolean isAlphanumeric(char c) + { + switch (Character.getType(c)) + { + case Character.DECIMAL_DIGIT_NUMBER: // Nd + case Character.LETTER_NUMBER: // Nl + case Character.OTHER_NUMBER: // No + case Character.UPPERCASE_LETTER: // Lu + case Character.LOWERCASE_LETTER: // Ll + case Character.TITLECASE_LETTER: // Lt + case Character.MODIFIER_LETTER: // Lm + case Character.OTHER_LETTER: // Lo + return true; + default: + return false; + } + } + + static final String alphabetic(char offset, int number) + { + StringBuffer buf = new StringBuffer(); + while (number > 0) + { + int r = number % 26; + number = number / 26; + buf.insert(0, (char) (offset + r)); + } + return buf.toString(); + } + + static final int[] roman_numbers = {1, 5, 10, 50, 100, 500, 1000}; + static final char[] roman_chars = {'i', 'v', 'x', 'l', 'c', 'd', 'm'}; + + static final String roman(boolean upper, int number) + { + StringBuffer buf = new StringBuffer(); + for (int pos = roman_numbers.length - 1; pos >= 0; pos -= 2) + { + int f = number / roman_numbers[pos]; + if (f != 0) + { + number = number % (f * roman_numbers[pos]); + } + if (f > 4 && f < 9) + { + buf.append(roman_chars[pos + 1]); + f -= 5; + } + if (f == 4) + { + buf.append(roman_chars[pos]); + buf.append(roman_chars[pos + 1]); + } + else if (f == 9) + { + buf.append(roman_chars[pos]); + buf.append(roman_chars[pos + 2]); + } + else + { + for (; f > 0; f--) + { + buf.append(roman_chars[pos]); + } + } + } + return upper ? buf.toString().toUpperCase() : buf.toString(); + } + + abstract int[] compute(Stylesheet stylesheet, Node context, int pos, int len) + throws TransformerException; + + public boolean references(QName var) + { + if (format.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("format="); + buf.append(format); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java b/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java new file mode 100644 index 00000000000..60dec85d378 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java @@ -0,0 +1,86 @@ +/* ApplyImportsNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; + +/** + * A template node representing an XSLT <code>apply-imports</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ApplyImportsNode + extends TemplateNode +{ + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new ApplyImportsNode(); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + TemplateNode t = stylesheet.getTemplate(mode, context, true); + if (t != null) + { + t.apply(stylesheet, mode, context, pos, len, + parent, nextSibling); + } + if (next != null) + { + next.apply(stylesheet, mode, context, pos, len, + parent, nextSibling); + } + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java b/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java new file mode 100644 index 00000000000..ab26058bcb4 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java @@ -0,0 +1,235 @@ +/* ApplyTemplatesNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing the XSL <code>apply-templates</code> + * instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ApplyTemplatesNode + extends TemplateNode +{ + + final Expr select; + final QName mode; + final List sortKeys; + final List withParams; + final boolean isDefault; + + ApplyTemplatesNode(Expr select, QName mode, + List sortKeys, List withParams, boolean isDefault) + { + this.select = select; + this.mode = mode; + this.sortKeys = sortKeys; + this.withParams = withParams; + this.isDefault = isDefault; + } + + TemplateNode clone(Stylesheet stylesheet) + { + int len = sortKeys.size(); + List sortKeys2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet)); + } + len = withParams.size(); + List withParams2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet)); + } + TemplateNode ret = new ApplyTemplatesNode(select.clone(stylesheet), + mode, sortKeys2, withParams2, + isDefault); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Object ret = select.evaluate(context, pos, len); + if (ret != null && ret instanceof Collection) + { + if (withParams != null) + { + // compute the parameter values + LinkedList values = new LinkedList(); + for (Iterator i = withParams.iterator(); i.hasNext(); ) + { + WithParam p = (WithParam) i.next(); + Object value = p.getValue(stylesheet, mode, context, pos, len); + Object[] pair = new Object[2]; + pair[0] = p.name; + pair[1] = value; + values.add(pair); + } + // push the parameter context + stylesheet.bindings.push(Bindings.WITH_PARAM); + // set the parameters + for (Iterator i = values.iterator(); i.hasNext(); ) + { + Object[] pair = (Object[]) i.next(); + QName name = (QName) pair[0]; + Object value = pair[1]; + stylesheet.bindings.set(name, value, Bindings.WITH_PARAM); + } + } + Collection ns = (Collection) ret; + List nodes = new ArrayList(ns); + if (sortKeys != null) + { + for (Iterator i = sortKeys.iterator(); i.hasNext(); ) + { + SortKey sortKey = (SortKey) i.next(); + sortKey.init(stylesheet, mode, context, pos, len, parent, + nextSibling); + } + Collections.sort(nodes, new XSLComparator(sortKeys)); + } + else + { + Collections.sort(nodes, documentOrderComparator); + } + int l = nodes.size(); + QName effectiveMode = isDefault ? mode : this.mode; + for (int i = 0; i < l; i++) + { + Node node = (Node) nodes.get(i); + TemplateNode t = stylesheet.getTemplate(effectiveMode, node, + false); + if (t != null) + { + stylesheet.current = node; + t.apply(stylesheet, effectiveMode, node, i + 1, l, + parent, nextSibling); + } + } + if (withParams != null) + { + // pop the variable context + stylesheet.bindings.pop(Bindings.WITH_PARAM); + } + } + // apply-templates doesn't have processable children + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + if (withParams != null) + { + for (Iterator i = withParams.iterator(); i.hasNext(); ) + { + if (((WithParam) i.next()).references(var)) + { + return true; + } + } + } + if (sortKeys != null) + { + for (Iterator i = sortKeys.iterator(); i.hasNext(); ) + { + if (((SortKey) i.next()).references(var)) + { + return true; + } + } + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + boolean o = false; + if (select != null) + { + buf.append("select="); + buf.append(select); + o = true; + } + if (mode != null) + { + if (o) + { + buf.append(','); + } + buf.append("mode="); + buf.append(mode); + } + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/AttributeNode.java b/libjava/classpath/gnu/xml/transform/AttributeNode.java new file mode 100644 index 00000000000..1e0eb1e961a --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/AttributeNode.java @@ -0,0 +1,264 @@ +/* AttributeNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Attr; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSL <code>attribute</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class AttributeNode + extends TemplateNode +{ + + final TemplateNode name; + final TemplateNode namespace; + final Node source; + + AttributeNode(TemplateNode name, + TemplateNode namespace, Node source) + { + this.name = name; + this.namespace = namespace; + this.source = source; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new AttributeNode(name.clone(stylesheet), + (namespace == null) ? null : + namespace.clone(stylesheet), + source); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + // Create a document fragment to hold the name + DocumentFragment fragment = doc.createDocumentFragment(); + // Apply name to the fragment + name.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + String nameValue = Expr.stringValue(fragment); + + String namespaceValue = null; + if (namespace != null) + { + // Create a document fragment to hold the namespace + fragment = doc.createDocumentFragment(); + // Apply namespace to the fragment + namespace.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + namespaceValue = Expr.stringValue(fragment); + if (namespaceValue.length() == 0) + { + namespaceValue = null; + } + } + + String prefix = getPrefix(nameValue); + if (namespaceValue == null) + { + if (prefix != null) + { + if (XMLConstants.XML_NS_PREFIX.equals(prefix)) + { + namespaceValue = XMLConstants.XML_NS_URI; + } + else + { + // Resolve namespace for this prefix + namespaceValue = source.lookupNamespaceURI(prefix); + } + } + } + else + { + if (prefix != null) + { + String ns2 = source.lookupNamespaceURI(prefix); + if (ns2 != null && !ns2.equals(namespaceValue)) + { + // prefix clashes, reset it + prefix = null; + int ci = nameValue.indexOf(':'); + nameValue = nameValue.substring(ci + 1); + } + } + } + if (prefix == null) + { + // Resolve prefix for this namespace + prefix = source.lookupPrefix(namespaceValue); + if (prefix != null) + { + nameValue = prefix + ":" + nameValue; + } + else + { + if (namespaceValue != null) + { + // Must invent a prefix + prefix = inventPrefix(parent); + nameValue = prefix + ":" + nameValue; + } + } + } + NamedNodeMap attrs = parent.getAttributes(); + boolean insert = true; + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceValue) || + XMLConstants.XMLNS_ATTRIBUTE.equals(nameValue) || + nameValue.startsWith("xmlns:")) + { + // Namespace declaration, do not output + insert = false; + } + if (prefix != null && namespaceValue == null) + { + // Not a QName + insert = false; + } + if (parent.getNodeType() == Node.ELEMENT_NODE && + parent.getFirstChild() != null) + { + // XSLT 7.1.3 Adding an attribute to an element after children have + // been added to it is an error + insert = false; + } + if (insert) + { + // Insert attribute + Attr attr = (namespaceValue != null) ? + doc.createAttributeNS(namespaceValue, nameValue) : + doc.createAttribute(nameValue); + if (attrs != null) + { + if (namespace != null) + { + attrs.setNamedItemNS(attr); + } + else + { + attrs.setNamedItem(attr); + } + } + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + attr, null); + } + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + final String getPrefix(String name) + { + int ci = name.indexOf(':'); + return (ci == -1) ? null : name.substring(0, ci); + } + + final String inventPrefix(Node parent) + { + String base = "ns"; + int count = 0; + String ret = base + Integer.toString(count); + while (parent.lookupNamespaceURI(ret) != null) + { + count++; + ret = base + Integer.toString(count); + } + return ret; + } + + public boolean references(QName var) + { + if (name != null && name.references(var)) + { + return true; + } + if (namespace != null && namespace.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("name="); + buf.append(name); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/AttributeSet.java b/libjava/classpath/gnu/xml/transform/AttributeSet.java new file mode 100644 index 00000000000..3ece9c83f5e --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/AttributeSet.java @@ -0,0 +1,67 @@ +/* AttributeSet.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +/** + * An attribute-set entry in a stylesheet. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class AttributeSet +{ + + final TemplateNode children; + final String name; + final String uas; + + AttributeSet(TemplateNode children, String name, String uas) + { + this.children = children; + this.name = name; + this.uas = uas; + } + + AttributeSet clone(Stylesheet stylesheet) + { + return new AttributeSet((children == null) ? null : + children.clone(stylesheet), + name, uas); + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/Bindings.java b/libjava/classpath/gnu/xml/transform/Bindings.java new file mode 100644 index 00000000000..c372ea83091 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/Bindings.java @@ -0,0 +1,325 @@ +/* Bindings.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathVariableResolver; +import org.w3c.dom.Node; + +/** + * The set of variable bindings in effect for a stylesheet. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class Bindings + implements XPathVariableResolver, Cloneable +{ + + static final int VARIABLE = 0; + static final int PARAM = 1; + static final int WITH_PARAM = 2; + + final Stylesheet stylesheet; + + /** + * Global variables. + */ + final LinkedList variables; + + /** + * Parameter value stack. + */ + final LinkedList parameters; + + /** + * Argument (with-param) value stack. + */ + final LinkedList withParameters; + + Bindings(Stylesheet stylesheet) + { + this.stylesheet = stylesheet; + variables = new LinkedList(); + parameters = new LinkedList(); + withParameters = new LinkedList(); + for (int i = 0; i < 3; i++) + { + push(i); + } + } + + public Object clone() + { + try + { + return (Bindings) super.clone(); + } + catch (CloneNotSupportedException e) + { + throw new Error(e.getMessage()); + } + } + + void push(int type) + { + switch (type) + { + case VARIABLE: + variables.addFirst(new HashMap()); + break; + case PARAM: + parameters.addFirst(new HashMap()); + break; + case WITH_PARAM: + withParameters.addFirst(new HashMap()); + break; + } + } + + void pop(int type) + { + switch (type) + { + case VARIABLE: + variables.removeFirst(); + break; + case PARAM: + parameters.removeFirst(); + break; + case WITH_PARAM: + withParameters.removeFirst(); + break; + } + } + + public boolean containsKey(QName name, int type) + { + Iterator i = null; + switch (type) + { + case VARIABLE: + i = variables.iterator(); + break; + case PARAM: + i = parameters.iterator(); + break; + case WITH_PARAM: + Map ctx = (Map) withParameters.getFirst(); + return ctx.containsKey(name); + } + if (i != null) + { + while (i.hasNext()) + { + Map ctx = (Map) i.next(); + if (ctx.containsKey(name)) + { + return true; + } + } + } + return false; + } + + public Object get(QName name, Node context, int pos, int len) + { + //System.err.println("bindings.get: "+name); + //System.err.println("\t"+toString()); + Object ret = null; + //if (parameters.size() > 1 && containsKey(name, PARAM)) + // check that template defines parameter + { + Map cwp = (Map) withParameters.getFirst(); + ret = cwp.get(name); + //System.err.println("\twith-param: ret="+ret); + } + if (ret == null) + { + for (Iterator i = variables.iterator(); i.hasNext() && ret == null; ) + { + Map vctx = (Map) i.next(); + ret = vctx.get(name); + } + //System.err.println("\tvariable: ret="+ret); + } + if (ret == null) + { + for (Iterator i = parameters.iterator(); i.hasNext() && ret == null; ) + { + Map pctx = (Map) i.next(); + ret = pctx.get(name); + } + //System.err.println("\tparam: ret="+ret); + } + /*if (ret instanceof Expr && context != null) + { + Expr expr = (Expr) ret; + ret = expr.evaluate(context, 1, 1); + }*/ + if (ret instanceof Node) + { + ret = Collections.singleton(ret); + } + if (ret == null) + { + ret = ""; + } + //System.err.println("\tret="+ret); + return ret; + } + + void set(QName name, Object value, int type) + { + switch (type) + { + case VARIABLE: + Map vctx = (Map) variables.getFirst(); + vctx.put(name, value); + break; + case PARAM: + Map pctx = (Map) parameters.getFirst(); + pctx.put(name, value); + break; + case WITH_PARAM: + Map wctx = (Map) withParameters.getFirst(); + wctx.put(name, value); + break; + } + //System.err.println("Set "+name+"="+value); + } + + public Object resolveVariable(QName qName) + { + return get(qName, null, 1, 1); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(); + boolean next = false; + Collection seen = new HashSet(); + Map wctx = (Map) withParameters.getFirst(); + buf.append('('); + for (Iterator i = wctx.entrySet().iterator(); i.hasNext(); ) + { + if (next) + { + buf.append(','); + } + else + { + next = true; + } + Map.Entry entry = (Map.Entry) i.next(); + Object key = entry.getKey(); + if (!seen.contains(key)) + { + buf.append(key); + buf.append('='); + buf.append(entry.getValue()); + seen.add(key); + } + } + buf.append(')'); + next = false; + seen.clear(); + buf.append('{'); + for (Iterator i = variables.iterator(); i.hasNext(); ) + { + Map ctx = (Map) i.next(); + for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); ) + { + if (next) + { + buf.append(','); + } + else + { + next = true; + } + Map.Entry entry = (Map.Entry) j.next(); + Object key = entry.getKey(); + if (!seen.contains(key)) + { + buf.append(key); + buf.append('='); + buf.append(entry.getValue()); + seen.add(key); + } + } + } + buf.append('}'); + next = false; + seen.clear(); + buf.append('['); + for (Iterator i = parameters.iterator(); i.hasNext(); ) + { + Map ctx = (Map) i.next(); + for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); ) + { + if (next) + { + buf.append(','); + } + else + { + next = true; + } + Map.Entry entry = (Map.Entry) j.next(); + Object key = entry.getKey(); + if (!seen.contains(key)) + { + buf.append(key); + buf.append('='); + buf.append(entry.getValue()); + seen.add(key); + } + } + } + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/CallTemplateNode.java b/libjava/classpath/gnu/xml/transform/CallTemplateNode.java new file mode 100644 index 00000000000..b678219d780 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/CallTemplateNode.java @@ -0,0 +1,165 @@ +/* CallTemplateNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; + +/** + * A template node representing the XSL <code>call-template</code> + * instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CallTemplateNode + extends TemplateNode +{ + + final QName name; + final List withParams; + + CallTemplateNode(QName name, List withParams) + { + this.name = name; + this.withParams = withParams; + } + + TemplateNode clone(Stylesheet stylesheet) + { + int len = withParams.size(); + List withParams2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet)); + } + TemplateNode ret = new CallTemplateNode(name, withParams2); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + if (withParams != null) + { + // compute the parameter values + LinkedList values = new LinkedList(); + for (Iterator i = withParams.iterator(); i.hasNext(); ) + { + WithParam p = (WithParam) i.next(); + Object value = p.getValue(stylesheet, mode, context, pos, len); + Object[] pair = new Object[2]; + pair[0] = p.name; + pair[1] = value; + values.add(pair); + } + // push the parameter context + stylesheet.bindings.push(Bindings.WITH_PARAM); + // set the parameters + for (Iterator i = values.iterator(); i.hasNext(); ) + { + Object[] pair = (Object[]) i.next(); + QName name = (QName) pair[0]; + Object value = pair[1]; + stylesheet.bindings.set(name, value, Bindings.WITH_PARAM); + if (stylesheet.debug) + { + System.err.println("with-param: " + name + " = " + value); + } + } + } + TemplateNode t = stylesheet.getTemplate(mode, name); + if (t != null) + { + t.apply(stylesheet, mode, context, pos, len, + parent, nextSibling); + } + if (withParams != null) + { + // pop the variable context + stylesheet.bindings.pop(Bindings.WITH_PARAM); + } + // call-template doesn't have processable children + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public boolean references(QName var) + { + if (withParams != null) + { + for (Iterator i = withParams.iterator(); i.hasNext(); ) + { + if (((WithParam) i.next()).references(var)) + { + return true; + } + } + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("name="); + buf.append(name); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/ChooseNode.java b/libjava/classpath/gnu/xml/transform/ChooseNode.java new file mode 100644 index 00000000000..fb1f2c45e75 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ChooseNode.java @@ -0,0 +1,94 @@ +/* ChooseNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; + +/** + * A template node representing an XSL <code>choose</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ChooseNode + extends TemplateNode +{ + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new ChooseNode(); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/CommentNode.java b/libjava/classpath/gnu/xml/transform/CommentNode.java new file mode 100644 index 00000000000..1428a46fca1 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/CommentNode.java @@ -0,0 +1,116 @@ +/* CommentNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Comment; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing the XSL <code>comment</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CommentNode + extends TemplateNode +{ + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new CommentNode(); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + String value = ""; + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + if (children != null) + { + // Create a document fragment to hold the text + DocumentFragment fragment = doc.createDocumentFragment(); + // Apply children to the fragment + children.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + value = Expr.stringValue(fragment); + } + Comment comment = doc.createComment(value); + // Insert into result tree + if (nextSibling != null) + { + parent.insertBefore(comment, nextSibling); + } + else + { + parent.appendChild(comment); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/CopyNode.java b/libjava/classpath/gnu/xml/transform/CopyNode.java new file mode 100644 index 00000000000..3e019445aaa --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/CopyNode.java @@ -0,0 +1,183 @@ +/* CopyNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Iterator; +import java.util.StringTokenizer; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * A template node representing the XSL <code>copy</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CopyNode + extends TemplateNode +{ + + final String uas; + + CopyNode(String uas) + { + this.uas = uas; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new CopyNode(uas); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Node copy = parent; + switch (context.getNodeType()) + { + case Node.TEXT_NODE: + case Node.ATTRIBUTE_NODE: + case Node.ELEMENT_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + case Node.COMMENT_NODE: + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + copy = context.cloneNode(false); + copy = doc.adoptNode(copy); + if (copy.getNodeType() == Node.ATTRIBUTE_NODE) + { + if (parent.getFirstChild() != null) + { + // Ignore attempt to add attribute after children + } + else + { + NamedNodeMap attrs = parent.getAttributes(); + if (attrs != null) + { + attrs.setNamedItemNS(copy); + } + } + } + else + { + if (nextSibling != null) + { + parent.insertBefore(copy, nextSibling); + } + else + { + parent.appendChild(copy); + } + } + } + if (uas != null) + { + StringTokenizer st = new StringTokenizer(uas, " "); + while (st.hasMoreTokens()) + { + addAttributeSet(stylesheet, mode, context, pos, len, + copy, null, st.nextToken()); + } + } + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + copy, null); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + void addAttributeSet(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling, String attributeSet) + throws TransformerException + { + for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); ) + { + AttributeSet as = (AttributeSet) i.next(); + if (!as.name.equals(attributeSet)) + { + continue; + } + if (as.uas != null) + { + StringTokenizer st = new StringTokenizer(as.uas, " "); + while (st.hasMoreTokens()) + { + addAttributeSet(stylesheet, mode, context, pos, len, + parent, nextSibling, st.nextToken()); + } + } + if (as.children != null) + { + as.children.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/CopyOfNode.java b/libjava/classpath/gnu/xml/transform/CopyOfNode.java new file mode 100644 index 00000000000..a43e3ba841e --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/CopyOfNode.java @@ -0,0 +1,192 @@ +/* CopyOfNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSLT <code>copy-of</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CopyOfNode + extends TemplateNode +{ + + final Expr select; + + CopyOfNode(Expr select) + { + this.select = select; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new CopyOfNode(select.clone(stylesheet)); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Object ret = select.evaluate(context, pos, len); + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + if (ret instanceof Collection) + { + Collection ns = (Collection) ret; + List list = new ArrayList(ns); + Collections.sort(list, documentOrderComparator); + for (Iterator i = list.iterator(); i.hasNext(); ) + { + Node src = (Node) i.next(); + short nodeType = src.getNodeType(); + if (nodeType == Node.DOCUMENT_NODE) + { + // Use document element + src = ((Document) src).getDocumentElement(); + if (src == null) + { + continue; + } + nodeType = Node.ELEMENT_NODE; + } + else if (nodeType == Node.ATTRIBUTE_NODE) + { + if (parent.getFirstChild() != null) + { + // Ignore attempt to add attribute after children + continue; + } + } + if (parent.getNodeType() == Node.ATTRIBUTE_NODE && + nodeType != Node.TEXT_NODE && + nodeType != Node.ENTITY_REFERENCE_NODE) + { + // Ignore + continue; + } + Node node = src.cloneNode(true); + node = doc.adoptNode(node); + if (nodeType == Node.ATTRIBUTE_NODE) + { + NamedNodeMap attrs = parent.getAttributes(); + if (attrs != null) + { + attrs.setNamedItemNS(node); + } + } + else + { + if (nextSibling != null) + { + parent.insertBefore(node, nextSibling); + } + else + { + parent.appendChild(node); + } + } + } + } + else + { + String value = Expr._string(context, ret); + if (value != null && value.length() > 0) + { + Text textNode = doc.createTextNode(value); + if (nextSibling != null) + { + parent.insertBefore(textNode, nextSibling); + } + else + { + parent.appendChild(textNode); + } + } + } + // copy-of doesn't process children + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("select="); + buf.append(select); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/CurrentFunction.java b/libjava/classpath/gnu/xml/transform/CurrentFunction.java new file mode 100644 index 00000000000..0395396bc2e --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/CurrentFunction.java @@ -0,0 +1,104 @@ +/* CurrentFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collections; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>current()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CurrentFunction + extends Expr + implements Function, XPathFunction +{ + + final Stylesheet stylesheet; + + CurrentFunction(Stylesheet stylesheet) + { + this.stylesheet = stylesheet; + } + + public Object evaluate(List args) + throws XPathFunctionException + { + // We can't do anything useful here. + // So much for the JAXP API... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + } + + public Object evaluate(Node context, int pos, int len) + { + return Collections.singleton(stylesheet.current); + } + + public Expr clone(Object context) + { + Stylesheet s = stylesheet; + if (context instanceof Stylesheet) + { + s = (Stylesheet) context; + } + return new CurrentFunction(s); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + return "current()"; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/DOMSourceLocator.java b/libjava/classpath/gnu/xml/transform/DOMSourceLocator.java new file mode 100644 index 00000000000..9bc8fb1b6c8 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/DOMSourceLocator.java @@ -0,0 +1,84 @@ +/* DOMSourceLocator.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.transform.dom.DOMLocator; +import org.w3c.dom.Node; + +/** + * Simple DOMLocator implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class DOMSourceLocator + implements DOMLocator +{ + + final Node node; + + DOMSourceLocator(Node node) + { + this.node = node; + } + + public Node getOriginatingNode() + { + return node; + } + + public String getPublicId() + { + return null; + } + + public String getSystemId() + { + return null; + } + + public int getLineNumber() + { + return -1; + } + + public int getColumnNumber() + { + return -1; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/DocumentFunction.java b/libjava/classpath/gnu/xml/transform/DocumentFunction.java new file mode 100644 index 00000000000..d8f6090be66 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/DocumentFunction.java @@ -0,0 +1,256 @@ +/* DocumentFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.TreeSet; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import javax.xml.transform.dom.DOMSource; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Constant; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; +import gnu.xml.xpath.IdFunction; + +/** + * The XSLT <code>document()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class DocumentFunction + extends Expr + implements Function, XPathFunction +{ + + final Stylesheet stylesheet; + final Node base; + List args; + List values; + + DocumentFunction(Stylesheet stylesheet, Node base) + { + this.stylesheet = stylesheet; + this.base = base; + } + + public Object evaluate(List args) + throws XPathFunctionException + { + values = args; + return evaluate(null, 1, 1); + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + int arity = args.size(); + if (values == null) + { + values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + } + Object ret; + switch (arity) + { + case 1: + Object arg = values.get(0); + if (arg instanceof Collection) + { + Collection ns = (Collection) arg; + Collection acc = new TreeSet(); + for (Iterator i = ns.iterator(); i.hasNext(); ) + { + Node node = (Node) i.next(); + String s = Expr.stringValue(node); + acc.addAll(document(s, node.getBaseURI())); + } + ret = acc; + } + else + { + String s = Expr._string(context, arg); + ret = document(s, base.getBaseURI()); + } + break; + case 2: + Object arg1 = values.get(0); + Object arg2 = values.get(1); + if (!(arg2 instanceof Collection)) + { + throw new RuntimeException("second argument is not a node-set"); + } + Collection arg2ns = (Collection) arg2; + String base2 = arg2ns.isEmpty() ? null : + ((Node) arg2ns.iterator().next()).getBaseURI(); + if (arg1 instanceof Collection) + { + Collection arg1ns = (Collection) arg1; + Collection acc = new TreeSet(); + for (Iterator i = arg1ns.iterator(); i.hasNext(); ) + { + Node node = (Node) i.next(); + String s = Expr.stringValue(node); + acc.addAll(document(s, base2)); + } + ret = acc; + } + else + { + String s = Expr._string(context, arg1); + ret = document(s, base2); + } + break; + default: + throw new RuntimeException("invalid arity"); + } + values = null; + return ret; + } + + /** + * The XSL <code>document</code> function. + * @see XSLT 12.1 + * @param uri the URI from which to retrieve nodes + * @param base the base URI for relative URIs + */ + Collection document(String uri, String base) + { + if ("".equals(uri) || uri == null) + { + uri = this.base.getBaseURI(); + } + + // Get fragment + Expr fragment = null; + int hi = uri.indexOf('#'); + if (hi != -1) + { + String f = uri.substring(hi + 1); + uri = uri.substring(0, hi); + // TODO handle xpointer() here + // this only handles IDs + fragment = new IdFunction(new Constant(f)); + } + + // Get document source + try + { + DOMSource source; + XSLURIResolver resolver = stylesheet.factory.resolver; + synchronized (resolver) + { + if (stylesheet.transformer != null) + { + resolver.setUserResolver(stylesheet.transformer.uriResolver); + resolver.setUserListener(stylesheet.transformer.errorListener); + } + source = resolver.resolveDOM(null, base, uri); + } + Node node = source.getNode(); + if (fragment == null) + { + return Collections.singleton(node); + } + else + { + Object ret = fragment.evaluate(node, 1, 1); + if (!(ret instanceof Collection)) + { + // XXX Report error? + return Collections.EMPTY_SET; + } + return (Collection) ret; + } + } + catch (TransformerException e) + { + String msg = "can't open " + uri; + if (base != null) + { + msg += " with base " + base; + } + throw new RuntimeException(msg); + } + } + + public Expr clone(Object context) + { + Stylesheet s = stylesheet; + if (context instanceof Stylesheet) + { + s = (Stylesheet) context; + } + DocumentFunction f = new DocumentFunction(s, base); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java b/libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java new file mode 100644 index 00000000000..84cb6207af9 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java @@ -0,0 +1,183 @@ +/* ElementAvailableFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.TreeSet; +import javax.xml.namespace.QName; +import javax.xml.namespace.NamespaceContext; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>element-available</code> function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class ElementAvailableFunction + extends Expr + implements Function, XPathFunction +{ + + static final Collection elements; + static + { + TreeSet acc = new TreeSet(); + acc.add("stylesheet"); + acc.add("template"); + acc.add("param"); + acc.add("variable"); + acc.add("include"); + acc.add("import"); + acc.add("output"); + acc.add("preserve-space"); + acc.add("strip-space"); + acc.add("key"); + acc.add("decimal-format"); + acc.add("namespace-alias"); + acc.add("attribute-set"); + acc.add("apply-templates"); + acc.add("call-template"); + acc.add("value-of"); + acc.add("for-each"); + acc.add("if"); + acc.add("choose"); + acc.add("when"); + acc.add("otherwise"); + acc.add("element"); + acc.add("attribute"); + acc.add("text"); + acc.add("copy"); + acc.add("processing-instruction"); + acc.add("comment"); + acc.add("number"); + acc.add("copy-of"); + acc.add("message"); + acc.add("sort"); + acc.add("with-param"); + acc.add("fallback"); + acc.add("apply-imports"); + elements = Collections.unmodifiableSet(acc); + } + + final NamespaceContext nsctx; + List args; + + ElementAvailableFunction(NamespaceContext nsctx) + { + this.nsctx = nsctx; + } + + public Object evaluate(List args) + throws XPathFunctionException + { + // Useless... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + Expr arg = (Expr) args.get(0); + Object val = arg.evaluate(context, pos, len); + String name = _string(context, val); + String prefix, localName, uri; + int ci = name.indexOf(':'); + if (ci == -1) + { + prefix = null; + localName = name; + } + else + { + prefix = name.substring(0, ci); + localName = name.substring(ci + 1); + } + uri = nsctx.getNamespaceURI(prefix); + if (Stylesheet.XSL_NS.equals(uri)) + { + return elements.contains(localName) ? + Boolean.TRUE : Boolean.FALSE; + // TODO extension elements + } + return Boolean.FALSE; + } + + public Expr clone(Object context) + { + NamespaceContext n = nsctx; + if (context instanceof NamespaceContext) + { + n = (NamespaceContext) context; + } + ElementAvailableFunction f = new ElementAvailableFunction(n); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/ElementNode.java b/libjava/classpath/gnu/xml/transform/ElementNode.java new file mode 100644 index 00000000000..d8f7f6db6db --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ElementNode.java @@ -0,0 +1,296 @@ +/* ElementNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.StringTokenizer; +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSL <code>element</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ElementNode + extends TemplateNode +{ + + final TemplateNode name; + final TemplateNode namespace; + final String uas; + final Node source; + final Collection elementExcludeResultPrefixes; + + ElementNode(TemplateNode name, + TemplateNode namespace, String uas, Node source) + { + this.name = name; + this.namespace = namespace; + this.uas = uas; + this.source = source; + NamedNodeMap attrs = source.getAttributes(); + Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS, + "exclude-result-prefixes"); + if (attr != null) + { + elementExcludeResultPrefixes = new HashSet(); + StringTokenizer st = new StringTokenizer(attr.getNodeValue()); + while (st.hasMoreTokens()) + { + elementExcludeResultPrefixes.add(st.nextToken()); + } + } + else + { + elementExcludeResultPrefixes = Collections.EMPTY_SET; + } + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new ElementNode(name.clone(stylesheet), + (namespace == null) ? null : + namespace.clone(stylesheet), + uas, source); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + // Create a document fragment to hold the name + DocumentFragment fragment = doc.createDocumentFragment(); + // Apply name to the fragment + name.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + String nameValue = Expr.stringValue(fragment); + + String namespaceValue = null; + if (namespace != null) + { + // Create a document fragment to hold the namespace + fragment = doc.createDocumentFragment(); + // Apply namespace to the fragment + namespace.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + namespaceValue = Expr.stringValue(fragment); + if (namespaceValue.length() == 0) + { + namespaceValue = null; + } + } + + String prefix = getPrefix(nameValue); + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) + { + int ci = nameValue.indexOf(':'); + nameValue = nameValue.substring(ci + 1); + } + else + { + // Namespace aliasing + if (prefix == null) + { + prefix = "#default"; + } + String resultPrefix = + (String) stylesheet.namespaceAliases.get(prefix); + if (resultPrefix != null) + { + if ("#default".equals(resultPrefix)) + { + resultPrefix = null; + } + namespaceValue = source.lookupNamespaceURI(resultPrefix); + } + if (prefix == "#default") + { + prefix = null; + } + // Look up ordinary namespace for this prefix + if (namespaceValue == null) + { + if (XMLConstants.XML_NS_PREFIX.equals(prefix)) + { + namespaceValue = XMLConstants.XML_NS_URI; + } + else + { + // Resolve namespace for this prefix + namespaceValue = source.lookupNamespaceURI(prefix); + } + } + /*if (prefix == null) + { + // Resolve prefix for this namespace + prefix = parent.lookupPrefix(namespaceValue); + if (prefix != null) + { + nameValue = prefix + ":" + nameValue; + } + }*/ + } + // Create element + Element element = (namespaceValue != null) ? + doc.createElementNS(namespaceValue, nameValue) : + doc.createElement(nameValue); + if (nextSibling != null) + { + parent.insertBefore(element, nextSibling); + } + else + { + parent.appendChild(element); + } + stylesheet.addNamespaceNodes(source, element, doc, + elementExcludeResultPrefixes); + if (uas != null) + { + StringTokenizer st = new StringTokenizer(uas, " "); + while (st.hasMoreTokens()) + { + addAttributeSet(stylesheet, mode, context, pos, len, + element, null, st.nextToken()); + } + } + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + element, null); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + final String getPrefix(String name) + { + int ci = name.indexOf(':'); + return (ci == -1) ? null : name.substring(0, ci); + } + + void addAttributeSet(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling, String attributeSet) + throws TransformerException + { + for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); ) + { + AttributeSet as = (AttributeSet) i.next(); + if (!as.name.equals(attributeSet)) + { + continue; + } + if (as.uas != null) + { + StringTokenizer st = new StringTokenizer(as.uas, " "); + while (st.hasMoreTokens()) + { + addAttributeSet(stylesheet, mode, context, pos, len, + parent, nextSibling, st.nextToken()); + } + } + if (as.children != null) + { + as.children.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + } + + public boolean references(QName var) + { + if (name != null && name.references(var)) + { + return true; + } + if (namespace != null && namespace.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("name="); + buf.append(name); + if (uas != null) + { + buf.append(",uas="); + buf.append(uas); + } + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java b/libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java new file mode 100644 index 00000000000..d79bb15467a --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java @@ -0,0 +1,101 @@ +/* ErrorListenerErrorHandler.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.transform.ErrorListener; +import javax.xml.transform.TransformerException; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * An ErrorHandler that wraps an ErrorListener. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class ErrorListenerErrorHandler + implements ErrorHandler +{ + + final ErrorListener listener; + + ErrorListenerErrorHandler(ErrorListener listener) + { + this.listener = listener; + } + + public void warning(SAXParseException e) + throws SAXException + { + try + { + listener.warning(new TransformerException(e)); + } + catch (TransformerException e2) + { + throw new SAXException(e2); + } + } + + public void error(SAXParseException e) + throws SAXException + { + try + { + listener.error(new TransformerException(e)); + } + catch (TransformerException e2) + { + throw new SAXException(e2); + } + } + + public void fatalError(SAXParseException e) + throws SAXException + { + try + { + listener.fatalError(new TransformerException(e)); + } + catch (TransformerException e2) + { + throw new SAXException(e2); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/ForEachNode.java b/libjava/classpath/gnu/xml/transform/ForEachNode.java new file mode 100644 index 00000000000..8f9220f679b --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ForEachNode.java @@ -0,0 +1,171 @@ +/* ForEachNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSLT <code>for-each</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ForEachNode + extends TemplateNode +{ + + final Expr select; + final List sortKeys; + + ForEachNode(Expr select, List sortKeys) + { + this.select = select; + this.sortKeys = sortKeys; + } + + TemplateNode clone(Stylesheet stylesheet) + { + int len = sortKeys.size(); + List sortKeys2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet)); + } + TemplateNode ret = new ForEachNode(select.clone(stylesheet), + sortKeys2); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + if (children != null) + { + // Set current template to null + Template saved = stylesheet.currentTemplate; + stylesheet.currentTemplate = null; + Object ret = select.evaluate(context, pos, len); + //System.err.println(toString() + ": " + context+" -> "+ret); + if (ret instanceof Collection) + { + Collection ns = (Collection) ret; + List list = new ArrayList(ns); + if (sortKeys != null) + { + for (Iterator i = sortKeys.iterator(); i.hasNext(); ) + { + SortKey sortKey = (SortKey) i.next(); + sortKey.init(stylesheet, mode, context, pos, len, parent, + nextSibling); + } + Collections.sort(list, new XSLComparator(sortKeys)); + } + else + { + Collections.sort(list, documentOrderComparator); + } + // Perform children for each node + int l = list.size(); + int p = 1; + for (Iterator i = list.iterator(); i.hasNext(); ) + { + Node node = (Node) i.next(); + stylesheet.current = node; + children.apply(stylesheet, mode, + node, p++, l, + parent, nextSibling); + } + } + // Restore current template + stylesheet.currentTemplate = saved; + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + if (sortKeys != null) + { + for (Iterator i = sortKeys.iterator(); i.hasNext(); ) + { + if (((SortKey) i.next()).references(var)) + { + return true; + } + } + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("select="); + buf.append(select); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/FormatNumberFunction.java b/libjava/classpath/gnu/xml/transform/FormatNumberFunction.java new file mode 100644 index 00000000000..34717b39c34 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/FormatNumberFunction.java @@ -0,0 +1,146 @@ +/* FormatNumberFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>format-number()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class FormatNumberFunction + extends Expr + implements XPathFunction, Function +{ + + final Stylesheet stylesheet; + List args; + + FormatNumberFunction(Stylesheet stylesheet) + { + this.stylesheet = stylesheet; + } + + public Object evaluate(List args) + throws XPathFunctionException + { + // Useless... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + int arity = args.size(); + List values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + double number = _number(context, values.get(0)); + String pattern = _string(context, values.get(1)); + // Currency symbol ¤ is not supposed to be present + if (pattern.indexOf('\u00a4') != -1) + { + // Replace with $ (Xalan does this) + pattern = pattern.replace('\u00a4', '$'); + } + String dfName = null; + if (arity > 2) + { + dfName = _string(context, values.get(2)); + // otherwise the default decimal-format will be used + } + DecimalFormat df = (DecimalFormat) stylesheet.decimalFormats.get(dfName); + if (df == null) + { + throw new IllegalArgumentException("No such decimal-format: " + + dfName); + } + df.applyLocalizedPattern(pattern); + return df.format(number); + } + + public Expr clone(Object context) + { + Stylesheet s = stylesheet; + if (context instanceof Stylesheet) + { + s = (Stylesheet) context; + } + FormatNumberFunction f = new FormatNumberFunction(s); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java b/libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java new file mode 100644 index 00000000000..7daf7ea3f70 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java @@ -0,0 +1,189 @@ +/* FunctionAvailableFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.TreeSet; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>function-available</code> function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class FunctionAvailableFunction + extends Expr + implements Function, XPathFunction +{ + + static final Collection xsltFunctions; + static final Collection xpathFunctions; + static + { + TreeSet acc = new TreeSet(); + acc.add("document"); + acc.add("key"); + acc.add("format-number"); + acc.add("current"); + acc.add("unparsed-entity-uri"); + acc.add("generate-id"); + acc.add("system-property"); + acc.add("element-available"); + acc.add("function-available"); + xsltFunctions = Collections.unmodifiableSet(acc); + acc = new TreeSet(); + acc.add("boolean"); + acc.add("ceiling"); + acc.add("concat"); + acc.add("contains"); + acc.add("count"); + acc.add("false"); + acc.add("floor"); + acc.add("id"); + acc.add("lang"); + acc.add("last"); + acc.add("local-name"); + acc.add("name"); + acc.add("namespace-uri"); + acc.add("normalize-space"); + acc.add("not"); + acc.add("number"); + acc.add("position"); + acc.add("round"); + acc.add("starts-with"); + acc.add("string"); + acc.add("string-length"); + acc.add("substring-after"); + acc.add("substring-before"); + acc.add("substring"); + acc.add("sum"); + acc.add("translate"); + acc.add("true"); + xpathFunctions = Collections.unmodifiableSet(acc); + } + + final NamespaceContext nsctx; + List args; + + FunctionAvailableFunction(NamespaceContext nsctx) + { + this.nsctx = nsctx; + } + + public Object evaluate(List args) + throws XPathFunctionException + { + // Useless... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + Expr arg = (Expr) args.get(0); + Object val = arg.evaluate(context, pos, len); + String name = _string(context, val); + String prefix, localName, uri; + int ci = name.indexOf(':'); + if (ci == -1) + { + prefix = null; + localName = name; + } + else + { + prefix = name.substring(0, ci); + localName = name.substring(ci + 1); + } + uri = nsctx.getNamespaceURI(prefix); + if (uri == null) + { + return xsltFunctions.contains(localName) || + xpathFunctions.contains(localName) ? + Boolean.TRUE : Boolean.FALSE; + // TODO extension functions + } + return Boolean.FALSE; + } + + public Expr clone(Object context) + { + NamespaceContext n = nsctx; + if (context instanceof NamespaceContext) + { + n = (NamespaceContext) context; + } + FunctionAvailableFunction f = new FunctionAvailableFunction(n); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/GenerateIdFunction.java b/libjava/classpath/gnu/xml/transform/GenerateIdFunction.java new file mode 100644 index 00000000000..f0d3e6dd9db --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/GenerateIdFunction.java @@ -0,0 +1,140 @@ +/* GenerateIdFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>generate-id()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class GenerateIdFunction + extends Expr + implements XPathFunction, Function +{ + + List args; + + public Object evaluate(List args) + throws XPathFunctionException + { + // Useless... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + int arity = args.size(); + List values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + Node node; + Collection ns = (arity == 0) ? Collections.EMPTY_SET : + (Collection) values.get(0); + if (ns.isEmpty()) + { + node = context; + } + else + { + List list = new ArrayList(ns); + Collections.sort(list, documentOrderComparator); + node = (Node) list.get(0); + } + + String name = node.getNodeName(); + int index = 0, depth = 0; + for (Node ctx = node.getPreviousSibling(); ctx != null; + ctx = ctx.getPreviousSibling()) + { + index++; + } + for (Node ctx = node.getParentNode(); ctx != null; + ctx = ctx.getParentNode()) + { + depth++; + } + return name + "-" + index + "-" + depth; + } + + public Expr clone(Object context) + { + GenerateIdFunction f = new GenerateIdFunction(); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/IfNode.java b/libjava/classpath/gnu/xml/transform/IfNode.java new file mode 100644 index 00000000000..17e2486fe83 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/IfNode.java @@ -0,0 +1,120 @@ +/* IfNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSL <code>if</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class IfNode + extends TemplateNode +{ + + final Expr test; + + IfNode(Expr test) + { + this.test = test; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new IfNode(test.clone(stylesheet)); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Object ret = test.evaluate(context, pos, len); + boolean success = (ret instanceof Boolean) ? + ((Boolean) ret).booleanValue() : + Expr._boolean(context, ret); + if (success) + { + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public boolean references(QName var) + { + if (test != null && test.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("test="); + buf.append(test); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/Key.java b/libjava/classpath/gnu/xml/transform/Key.java new file mode 100644 index 00000000000..72bd90aedb0 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/Key.java @@ -0,0 +1,71 @@ +/* Key.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Pattern; + +/** + * An XSL key. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class Key +{ + + final QName name; + final Pattern match; + final Expr use; + + Key(QName name, Pattern match, Expr use) + { + this.name = name; + this.match = match; + this.use = use; + } + + Key clone(Stylesheet stylesheet) + { + return new Key(name, + (Pattern) match.clone(stylesheet), + use.clone(stylesheet)); + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/KeyFunction.java b/libjava/classpath/gnu/xml/transform/KeyFunction.java new file mode 100644 index 00000000000..a705dc6b009 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/KeyFunction.java @@ -0,0 +1,228 @@ +/* KeyFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; +import gnu.xml.xpath.Pattern; + +/** + * The XSLT <code>key()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class KeyFunction + extends Pattern + implements XPathFunction, Function +{ + + final Stylesheet stylesheet; + List args; + + KeyFunction(Stylesheet stylesheet) + { + this.stylesheet = stylesheet; + } + + public Object evaluate(List args) + throws XPathFunctionException + { + // Useless... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + this.args = args; + } + + public boolean matches(Node context) + { + Object ret = evaluate(context, 1, 1); + return !((Collection) ret).isEmpty(); + } + + public Object evaluate(Node context, int pos, int len) + { + // Evaluate arguments + int arity = args.size(); + List values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + // Get key name + QName keyName = QName.valueOf(_string(context, values.get(0))); + // Expand qualified name + String uri = keyName.getNamespaceURI(); + String prefix = keyName.getPrefix(); + if ((uri == null || uri.length() == 0) && + (prefix != null && prefix.length() > 0)) + { + uri = stylesheet.getNamespaceURI(prefix); + if (uri != null && uri.length() > 0) + { + String localName = keyName.getLocalPart(); + keyName = new QName(uri, localName, prefix); + } + } + // Compute matching key set + Collection keySet = new LinkedList(); + for (Iterator i = stylesheet.keys.iterator(); i.hasNext(); ) + { + Key key = (Key) i.next(); + if (key.name.equals(keyName)) + { + keySet.add(key); + } + } + // Get target + Object target = values.get(1); + Collection acc = new LinkedHashSet(); + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + if (target instanceof Collection) + { + for (Iterator i = ((Collection) target).iterator(); i.hasNext(); ) + { + String val = Expr.stringValue((Node) i.next()); + addKeyNodes(doc, keySet, val, acc); + } + } + else + { + String val = Expr._string(context, target); + addKeyNodes(doc, keySet, val, acc); + } + List ret = new ArrayList(acc); + Collections.sort(ret, documentOrderComparator); + return ret; + } + + final void addKeyNodes(Node node, Collection keySet, + String value, Collection acc) + { + addKeyNodeIfMatch(node, keySet, value, acc); + // Apply children + for (Node ctx = node.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + addKeyNodes(ctx, keySet, value, acc); + } + } + + final void addKeyNodeIfMatch(Node node, Collection keySet, + String value, Collection acc) + { + for (Iterator i = keySet.iterator(); i.hasNext(); ) + { + Key key = (Key) i.next(); + if (key.match.matches(node)) + { + Object eval = key.use.evaluate(node, 1, 1); + if (eval instanceof Collection) + { + for (Iterator j = ((Collection) eval).iterator(); + j.hasNext(); ) + { + String keyValue = Expr.stringValue((Node) j.next()); + if (value.equals(keyValue)) + { + acc.add(node); + return; + } + } + } + else + { + String keyValue = Expr._string(node, eval); + if (value.equals(keyValue)) + { + acc.add(node); + return; + } + } + } + } + } + + public Expr clone(Object context) + { + Stylesheet s = stylesheet; + if (context instanceof Stylesheet) + { + s = (Stylesheet) context; + } + KeyFunction f = new KeyFunction(s); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/LiteralNode.java b/libjava/classpath/gnu/xml/transform/LiteralNode.java new file mode 100644 index 00000000000..0be2b35c84d --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/LiteralNode.java @@ -0,0 +1,231 @@ +/* LiteralNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.StringTokenizer; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * A template node that copies a DOM node in the template to the result + * tree. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class LiteralNode + extends TemplateNode +{ + + /** + * The source node in the XSL template. + */ + final Node source; + + final Collection elementExcludeResultPrefixes; + + LiteralNode(Node source) + { + this.source = source; + if (source.getNodeType() == Node.ELEMENT_NODE) + { + NamedNodeMap attrs = source.getAttributes(); + Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS, + "exclude-result-prefixes"); + if (attr != null) + { + elementExcludeResultPrefixes = new HashSet(); + StringTokenizer st = new StringTokenizer(attr.getNodeValue()); + while (st.hasMoreTokens()) + { + elementExcludeResultPrefixes.add(st.nextToken()); + } + } + else + { + elementExcludeResultPrefixes = Collections.EMPTY_SET; + } + } + else + { + elementExcludeResultPrefixes = null; + } + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new LiteralNode(source); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Node result = null; + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + short nodeType = source.getNodeType(); + if (nodeType == Node.ATTRIBUTE_NODE && + parent.getFirstChild() != null) + { + // Ignore attributes added after child elements + } + else + { + // Namespace aliasing + if (nodeType == Node.ELEMENT_NODE) + { + String prefix = source.getPrefix(); + if (prefix == null) + { + prefix = "#default"; + } + String resultPrefix = + (String) stylesheet.namespaceAliases.get(prefix); + if (resultPrefix != null) + { + if ("#default".equals(resultPrefix)) + { + resultPrefix = null; + } + String uri = source.lookupNamespaceURI(resultPrefix); + String name = source.getNodeName(); + // Create a new element node in the result document + result = doc.createElementNS(uri, name); + // copy attributes + NamedNodeMap srcAttrs = source.getAttributes(); + NamedNodeMap dstAttrs = result.getAttributes(); + int l = srcAttrs.getLength(); + for (int i = 0; i < l; i++) + { + Node attr = srcAttrs.item(i); + if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) + { + attr = attr.cloneNode(true); + attr = doc.adoptNode(attr); + dstAttrs.setNamedItemNS(attr); + } + } + } + } + if (result == null) + { + // Create result node + result = source.cloneNode(false); + // Remove any XSL attributes + NamedNodeMap attrs = result.getAttributes(); + if (attrs != null) + { + int l = attrs.getLength(); + for (int i = 0; i < l; i++) + { + Node attr = attrs.item(i); + if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) + { + attrs.removeNamedItem(attr.getNodeName()); + i--; + l--; + } + } + } + Node result2 = doc.adoptNode(result); + if (result2 == null) + { + String msg = "Error adopting node to result tree: " + + result + " (" + result.getClass().getName() + ")"; + DOMSourceLocator l = new DOMSourceLocator(context); + throw new TransformerException(msg, l); + } + result = result2; + } + if (nextSibling != null) + { + parent.insertBefore(result, nextSibling); + } + else + { + parent.appendChild(result); + } + if (nodeType == Node.ELEMENT_NODE) + { + stylesheet.addNamespaceNodes(source, result, doc, + elementExcludeResultPrefixes); + } + // children + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + result, null); + } + } + // next sibling + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("source="); + buf.append(source); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/MessageNode.java b/libjava/classpath/gnu/xml/transform/MessageNode.java new file mode 100644 index 00000000000..1df716836e7 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/MessageNode.java @@ -0,0 +1,101 @@ +/* MessageNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * An XSL <code>message</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class MessageNode + extends TemplateNode +{ + + final boolean terminate; + + MessageNode(boolean terminate) + { + this.terminate = terminate; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new MessageNode(terminate); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + if (children != null) + { + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + DocumentFragment fragment = doc.createDocumentFragment(); + children.apply(stylesheet, mode, context, pos, len, fragment, null); + String message = Expr.stringValue(fragment); + System.err.println(message); + if (terminate) + { + stylesheet.terminated = true; + } + } + if (next != null && !terminate) + { + next.apply(stylesheet, mode, context, pos, len, parent, nextSibling); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/NodeNumberNode.java b/libjava/classpath/gnu/xml/transform/NodeNumberNode.java new file mode 100644 index 00000000000..0427686de8c --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/NodeNumberNode.java @@ -0,0 +1,269 @@ +/* NodeNumberNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Pattern; +import gnu.xml.xpath.Selector; +import gnu.xml.xpath.UnionExpr; + +/** + * A template node representing the XSL <code>number</code> instruction + * with no <code>value</code> expression, i.e. the value is computed from + * the document position of the context node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NodeNumberNode + extends AbstractNumberNode +{ + + static final int SINGLE = 0; + static final int MULTIPLE = 1; + static final int ANY = 2; + + final int level; + final Pattern count; + final Pattern from; + + NodeNumberNode(int level, Pattern count, Pattern from, + TemplateNode format, String lang, + int letterValue, String groupingSeparator, int groupingSize) + { + super(format, lang, letterValue, groupingSeparator, groupingSize); + this.level = level; + this.count = count; + this.from = from; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new NodeNumberNode(level, + (count == null) ? null : + (Pattern) count.clone(stylesheet), + (from == null) ? from : + (Pattern) from.clone(stylesheet), + format, lang, letterValue, + groupingSeparator, groupingSize); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + int[] compute(Stylesheet stylesheet, Node context, int pos, int len) + throws TransformerException + { + /*if (from != null) + { + Object ret = from.evaluate(context, pos, len); + if (ret instanceof Collection) + { + Collection ns = (Collection) ret; + if (ns.size() > 0) + { + List list = new ArrayList(ns); + Collections.sort(list, documentOrderComparator); + context = (Node) list.get(0); + } + else + { + return new int[0]; + } + } + else + { + return new int[0]; + } + }*/ + Node current = context; + switch (level) + { + case SINGLE: + if (from == null) + { + while (context != null && !countMatches(current, context)) + { + context = context.getParentNode(); + } + } + else + { + while (context != null && !countMatches(current, context) && + !fromMatches(context)) + { + context = context.getParentNode(); + } + } + return (context == null) ? new int[0] : + new int[] { (context == current) ? pos : getIndex(current, context) }; + case MULTIPLE: + List ancestors = new ArrayList(); + while (context != null) + { + if (countMatches(current, context)) + { + if (from == null || fromMatches(context)) + { + ancestors.add(context); + } + } + context = context.getParentNode(); + } + Collections.sort(ancestors, documentOrderComparator); + int[] ret = new int[ancestors.size()]; + for (int i = 0; i < ret.length; i++) + { + ret[i] = getIndex(current, (Node) ancestors.get(i)); + } + return ret; + case ANY: + Expr preceding = new Selector(Selector.PRECEDING, + Collections.EMPTY_LIST); + Expr ancestorOrSelf = new Selector(Selector.ANCESTOR_OR_SELF, + Collections.EMPTY_LIST); + Expr any = new UnionExpr(preceding, ancestorOrSelf); + Object eval = any.evaluate(context, pos, len); + if (eval instanceof Collection) + { + Collection ns = (Collection) eval; + List candidates = new ArrayList(); + for (Iterator i = ns.iterator(); i.hasNext(); ) + { + Node candidate = (Node) i.next(); + if (countMatches(current, candidate)) + { + candidates.add(candidate); + if (from != null && from.matches(candidate)) + { + break; + } + } + } + return new int[] { candidates.size() }; + } + return new int[0]; + default: + throw new TransformerException("invalid level"); + } + } + + boolean countMatches(Node current, Node node) + { + if (count == null) + { + int cnt = current.getNodeType(); + int nnt = node.getNodeType(); + if (cnt != nnt) + { + return false; + } + if (nnt == Node.ELEMENT_NODE || nnt == Node.ATTRIBUTE_NODE) + { + String curi = current.getNamespaceURI(); + String nuri = node.getNamespaceURI(); + if ((curi == null && nuri != null) || + (curi != null && !curi.equals(nuri))) + { + return false; + } + String cn = current.getLocalName(); + if (cn == null) + { + cn = current.getNodeName(); + } + String nn = node.getLocalName(); + if (nn == null) + { + nn = node.getNodeName(); + } + if (!cn.equals(nn)) + { + return false; + } + } + return true; + } + else + { + return count.matches(node); + } + } + + boolean fromMatches(Node node) + { + for (Node ctx = node.getParentNode(); ctx != null; + ctx = ctx.getParentNode()) + { + if (from.matches(ctx)) + { + return true; + } + } + return false; + } + + int getIndex(Node current, Node node) + { + int index = 0; + do + { + do + { + node = node.getPreviousSibling(); + } + while (node != null && !countMatches(current, node)); + index++; + } + while (node != null); + return index; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/NumberNode.java b/libjava/classpath/gnu/xml/transform/NumberNode.java new file mode 100644 index 00000000000..b19a7b5e9ee --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/NumberNode.java @@ -0,0 +1,88 @@ +/* NumberNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing the XSL <code>number</code> instruction + * with a <code>value</code> expression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NumberNode + extends AbstractNumberNode +{ + + final Expr value; + + NumberNode(Expr value, TemplateNode format, String lang, + int letterValue, String groupingSeparator, int groupingSize) + { + super(format, lang, letterValue, groupingSeparator, groupingSize); + this.value = value; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new NumberNode(value.clone(stylesheet), + format, lang, letterValue, + groupingSeparator, groupingSize); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + int[] compute(Stylesheet stylesheet, Node context, int pos, int len) + throws TransformerException + { + Object ret = value.evaluate(context, pos, len); + Double d = (ret instanceof Double) ? ((Double) ret) : + new Double(Expr._number(context, ret)); + return new int[] { d.intValue() }; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/OtherwiseNode.java b/libjava/classpath/gnu/xml/transform/OtherwiseNode.java new file mode 100644 index 00000000000..570310f6bd5 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/OtherwiseNode.java @@ -0,0 +1,94 @@ +/* OtherwiseNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; + +/** + * A template node representing an XSL <code>otherwise</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class OtherwiseNode + extends TemplateNode +{ + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new OtherwiseNode(); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/ParameterNode.java b/libjava/classpath/gnu/xml/transform/ParameterNode.java new file mode 100644 index 00000000000..ef09ea5f9b2 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ParameterNode.java @@ -0,0 +1,198 @@ +/* ParameterNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collections; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node that sets a variable or parameter during template + * processing. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ParameterNode + extends TemplateNode + implements Comparable +{ + + final QName name; + final Expr select; + final int type; + + ParameterNode(QName name, Expr select, int type) + { + this.name = name; + this.select = select; + this.type = type; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new ParameterNode(name, + select.clone(stylesheet), + type); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + // push the variable context + stylesheet.bindings.push(type); + // set the variable + Object value = getValue(stylesheet, mode, context, pos, len); + if (value != null) + { + stylesheet.bindings.set(name, value, type); + if (stylesheet.debug) + { + System.err.println(this + ": set to " + value); + } + } + // variable and param don't process children as such + // all subsequent instructions are processed with that variable context + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + // pop the variable context + stylesheet.bindings.pop(type); + } + + Object getValue(Stylesheet stylesheet, QName mode, + Node context, int pos, int len) + throws TransformerException + { + if (select != null) + { + return select.evaluate(context, pos, len); + } + else if (children != null) + { + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + DocumentFragment fragment = doc.createDocumentFragment(); + children.apply(stylesheet, mode, context, pos, len, fragment, null); + return Collections.singleton(fragment); + } + else + { + return null; + } + } + + public boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + return super.references(var); + } + + public int compareTo(Object other) + { + if (other instanceof ParameterNode) + { + ParameterNode pn = (ParameterNode) other; + boolean r1 = references(pn.name); + boolean r2 = pn.references(name); + if (r1 && r2) + { + throw new IllegalArgumentException("circular definitions"); + } + if (r1) + { + return 1; + } + if (r2) + { + return -1; + } + } + return 0; + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("name="); + buf.append(name); + if (select != null) + { + buf.append(",select="); + buf.append(select); + } + buf.append(",type="); + switch (type) + { + case Bindings.VARIABLE: + buf.append("variable"); + break; + case Bindings.PARAM: + buf.append("param"); + break; + case Bindings.WITH_PARAM: + buf.append("with-param"); + break; + } + buf.append(']'); + return buf.toString(); + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java b/libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java new file mode 100644 index 00000000000..d75f693663e --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java @@ -0,0 +1,126 @@ +/* ProcessingInstructionNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import org.w3c.dom.ProcessingInstruction; +import gnu.xml.xpath.Expr; + +/** + * A template node representing the XSL <code>processing-instruction</code> + * instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ProcessingInstructionNode + extends TemplateNode +{ + + final String name; + + ProcessingInstructionNode(String name) + { + this.name = name; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new ProcessingInstructionNode(name); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + String data = null; + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + if (children != null) + { + // Create a document fragment to hold the text + DocumentFragment fragment = doc.createDocumentFragment(); + // Apply children to the fragment + children.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + data = Expr.stringValue(fragment); + } + ProcessingInstruction pi = doc.createProcessingInstruction(name, data); + // Insert into result tree + if (nextSibling != null) + { + parent.insertBefore(pi, nextSibling); + } + else + { + parent.appendChild(pi); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("name="); + buf.append(name); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/SAXSerializer.java b/libjava/classpath/gnu/xml/transform/SAXSerializer.java new file mode 100644 index 00000000000..9650e3e052d --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/SAXSerializer.java @@ -0,0 +1,305 @@ +/* SAXSerializer.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import org.w3c.dom.Attr; +import org.w3c.dom.DocumentType; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; + +/** + * Serializes a DOM node to a sequence of SAX events. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class SAXSerializer + implements Attributes +{ + + transient NamedNodeMap attrs; + transient LinkedList namespaces = new LinkedList(); + + boolean isDefined(String prefix, String uri) + { + for (Iterator i = namespaces.iterator(); i.hasNext(); ) + { + HashMap ctx = (HashMap) i.next(); + if (uri.equals(ctx.get(prefix))) + { + return true; + } + } + return false; + } + + void define(String prefix, String uri) + { + for (Iterator i = namespaces.iterator(); i.hasNext(); ) + { + HashMap ctx = (HashMap) i.next(); + if (ctx.containsKey(prefix)) + { + HashMap newCtx = new HashMap(); + newCtx.put(prefix, uri); + namespaces.addFirst(newCtx); + return; + } + } + HashMap ctx; + if (namespaces.isEmpty()) + { + ctx = new HashMap(); + namespaces.add(ctx); + } + else + { + ctx = (HashMap) namespaces.getFirst(); + } + ctx.put(prefix, uri); + } + + void undefine(String prefix, String uri) + { + for (Iterator i = namespaces.iterator(); i.hasNext(); ) + { + HashMap ctx = (HashMap) i.next(); + if (uri.equals(ctx.get(prefix))) + { + ctx.remove(prefix); + if (ctx.isEmpty()) + { + namespaces.remove(ctx); + } + return; + } + } + } + + public int getLength() + { + return attrs.getLength(); + } + + public String getURI(int index) + { + return attrs.item(index).getNamespaceURI(); + } + + public String getLocalName(int index) + { + return attrs.item(index).getLocalName(); + } + + public String getQName(int index) + { + return attrs.item(index).getNodeName(); + } + + public String getType(int index) + { + Attr attr = (Attr) attrs.item(index); + return attr.isId() ? "ID" : "CDATA"; + } + + public String getValue(int index) + { + return attrs.item(index).getNodeValue(); + } + + public int getIndex(String uri, String localName) + { + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String a_uri = attr.getNamespaceURI(); + String a_localName = attr.getLocalName(); + if (((a_uri == null && uri == null) || + (a_uri != null && a_uri.equals(uri))) && + a_localName.equals(localName)) + { + return i; + } + } + return -1; + } + + public int getIndex(String qName) + { + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String a_name = attr.getNodeName(); + if (a_name.equals(qName)) + { + return i; + } + } + return -1; + } + + public String getType(String uri, String localName) + { + Attr attr = (Attr) attrs.getNamedItemNS(uri, localName); + return attr.isId() ? "ID" : "CDATA"; + } + + public String getType(String qName) + { + Attr attr = (Attr) attrs.getNamedItem(qName); + return attr.isId() ? "ID" : "CDATA"; + } + + public String getValue(String uri, String localName) + { + return attrs.getNamedItemNS(uri, localName).getNodeValue(); + } + + public String getValue(String qName) + { + return attrs.getNamedItem(qName).getNodeValue(); + } + + void serialize(Node node, ContentHandler ch, LexicalHandler lh) + throws SAXException + { + attrs = node.getAttributes(); + Node children; + Node next = node.getNextSibling(); + switch (node.getNodeType()) + { + case Node.ELEMENT_NODE: + String uri = node.getNamespaceURI(); + String prefix = node.getPrefix(); + boolean defined = isDefined(prefix, uri); + if (!defined) + { + define(prefix, uri); + ch.startPrefixMapping(prefix, uri); + } + String localName = node.getLocalName(); + String qName = node.getNodeName(); + ch.startElement(uri, localName, qName, this); + children = node.getFirstChild(); + if (children != null) + { + serialize(children, ch, lh); + } + ch.endElement(uri, localName, qName); + if (!defined) + { + ch.endPrefixMapping(prefix); + undefine(prefix, uri); + } + break; + case Node.TEXT_NODE: + char[] chars = node.getNodeValue().toCharArray(); + ch.characters(chars, 0, chars.length); + break; + case Node.CDATA_SECTION_NODE: + char[] cdata = node.getNodeValue().toCharArray(); + if (lh != null) + { + lh.startCDATA(); + ch.characters(cdata, 0, cdata.length); + lh.endCDATA(); + } + else + { + ch.characters(cdata, 0, cdata.length); + } + break; + case Node.COMMENT_NODE: + if (lh != null) + { + char[] comment = node.getNodeValue().toCharArray(); + lh.comment(comment, 0, comment.length); + } + break; + case Node.DOCUMENT_NODE: + case Node.DOCUMENT_FRAGMENT_NODE: + ch.startDocument(); + children = node.getFirstChild(); + if (children != null) + { + serialize(children, ch, lh); + } + ch.endDocument(); + break; + case Node.DOCUMENT_TYPE_NODE: + if (lh != null) + { + DocumentType doctype = (DocumentType) node; + String publicId = doctype.getPublicId(); + String systemId = doctype.getSystemId(); + lh.startDTD(node.getNodeName(), publicId, systemId); + NamedNodeMap entities = doctype.getEntities(); + int len = entities.getLength(); + for (int i = 0; i < len; i++) + { + Node entity = entities.item(i); + String entityName = entity.getNodeName(); + lh.startEntity(entityName); + lh.endEntity(entityName); + } + lh.endDTD(); + } + break; + case Node.PROCESSING_INSTRUCTION_NODE: + ch.processingInstruction(node.getNodeName(), node.getNodeValue()); + break; + case Node.ENTITY_REFERENCE_NODE: + ch.skippedEntity(node.getNodeName()); + break; + } + attrs = null; + if (next != null) + { + serialize(next, ch, lh); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/SortKey.java b/libjava/classpath/gnu/xml/transform/SortKey.java new file mode 100644 index 00000000000..d4ffb05e224 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/SortKey.java @@ -0,0 +1,179 @@ +/* SortKey.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * An XSL sort key. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class SortKey +{ + + static final int DEFAULT = 0; + static final int UPPER_FIRST = 1; + static final int LOWER_FIRST = 2; + + final Expr select; + final TemplateNode langTemplate; + final TemplateNode dataTypeTemplate; + final TemplateNode orderTemplate; + final TemplateNode caseOrderTemplate; + + transient String lang; + transient String dataType; + transient boolean descending; + transient int caseOrder; + + SortKey(Expr select, TemplateNode lang, TemplateNode dataType, + TemplateNode order, TemplateNode caseOrder) + { + this.select = select; + this.langTemplate = lang; + this.dataTypeTemplate = dataType; + this.orderTemplate = order; + this.caseOrderTemplate = caseOrder; + } + + String key(Node node) + { + Object ret = select.evaluate(node, 1, 1); + if (ret instanceof String) + { + return (String) ret; + } + else + { + return Expr._string(node, ret); + } + } + + /** + * Prepare for a sort. + * This sets all transient variables from their AVTs. + */ + void init(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + if (langTemplate == null) + { + lang = null; + } + else + { + DocumentFragment fragment = doc.createDocumentFragment(); + langTemplate.apply(stylesheet, mode, context, pos, len, + fragment, null); + lang = Expr.stringValue(fragment); + } + if (dataTypeTemplate == null) + { + dataType = "text"; + } + else + { + DocumentFragment fragment = doc.createDocumentFragment(); + dataTypeTemplate.apply(stylesheet, mode, context, pos, len, + fragment, null); + dataType = Expr.stringValue(fragment); + } + if (orderTemplate == null) + { + descending = false; + } + else + { + DocumentFragment fragment = doc.createDocumentFragment(); + orderTemplate.apply(stylesheet, mode, context, pos, len, + fragment, null); + String order = Expr.stringValue(fragment); + descending = "descending".equals(order); + } + if (caseOrderTemplate == null) + { + caseOrder = DEFAULT; + } + else + { + DocumentFragment fragment = doc.createDocumentFragment(); + caseOrderTemplate.apply(stylesheet, mode, context, pos, len, + fragment, null); + String co = Expr.stringValue(fragment); + caseOrder = "upper-first".equals(co) ? UPPER_FIRST : + "lower-first".equals(co) ? LOWER_FIRST : + DEFAULT; + } + } + + boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + if (langTemplate != null && langTemplate.references(var)) + { + return true; + } + if (dataTypeTemplate != null && dataTypeTemplate.references(var)) + { + return true; + } + if (orderTemplate != null && orderTemplate.references(var)) + { + return true; + } + if (caseOrderTemplate != null && caseOrderTemplate.references(var)) + { + return true; + } + return false; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/StreamSerializer.java b/libjava/classpath/gnu/xml/transform/StreamSerializer.java new file mode 100644 index 00000000000..eb045393dff --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/StreamSerializer.java @@ -0,0 +1,762 @@ +/* StreamSerializer.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import javax.xml.XMLConstants; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * Serializes a DOM node to an output stream. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class StreamSerializer +{ + + static final int SPACE = 0x20; + static final int BANG = 0x21; // ! + static final int APOS = 0x27; // ' + static final int SLASH = 0x2f; // / + static final int BRA = 0x3c; // < + static final int KET = 0x3e; // > + static final int EQ = 0x3d; // = + + /** + * HTML 4.01 boolean attributes + */ + static final Map HTML_BOOLEAN_ATTRIBUTES = new HashMap(); + static + { + HashSet set; + + set = new HashSet(); + set.add("nohref"); + HTML_BOOLEAN_ATTRIBUTES.put("area", set); + + set = new HashSet(); + set.add("ismap"); + HTML_BOOLEAN_ATTRIBUTES.put("img", set); + + set = new HashSet(); + set.add("declare"); + HTML_BOOLEAN_ATTRIBUTES.put("object", set); + + set = new HashSet(); + set.add("noshade"); + HTML_BOOLEAN_ATTRIBUTES.put("hr", set); + + set = new HashSet(); + set.add("compact"); + HTML_BOOLEAN_ATTRIBUTES.put("dl", set); + HTML_BOOLEAN_ATTRIBUTES.put("ol", set); + HTML_BOOLEAN_ATTRIBUTES.put("ul", set); + HTML_BOOLEAN_ATTRIBUTES.put("dir", set); + HTML_BOOLEAN_ATTRIBUTES.put("menu", set); + + set = new HashSet(); + set.add("checked"); + set.add("disabled"); + set.add("readonly"); + set.add("ismap"); + HTML_BOOLEAN_ATTRIBUTES.put("input", set); + + set = new HashSet(); + set.add("multiple"); + set.add("disabled"); + HTML_BOOLEAN_ATTRIBUTES.put("select", set); + + set = new HashSet(); + set.add("disabled"); + HTML_BOOLEAN_ATTRIBUTES.put("optgroup", set); + + set = new HashSet(); + set.add("selected"); + set.add("disabled"); + HTML_BOOLEAN_ATTRIBUTES.put("option", set); + + set = new HashSet(); + set.add("disabled"); + set.add("readonly"); + HTML_BOOLEAN_ATTRIBUTES.put("textarea", set); + + set = new HashSet(); + set.add("disabled"); + HTML_BOOLEAN_ATTRIBUTES.put("button", set); + + set = new HashSet(); + set.add("nowrap"); + HTML_BOOLEAN_ATTRIBUTES.put("th", set); + HTML_BOOLEAN_ATTRIBUTES.put("td", set); + + set = new HashSet(); + set.add("noresize"); + HTML_BOOLEAN_ATTRIBUTES.put("frame", set); + + set = new HashSet(); + set.add("defer"); + HTML_BOOLEAN_ATTRIBUTES.put("script", set); + } + + protected final String encoding; + final Charset charset; + final CharsetEncoder encoder; + final int mode; + final Map namespaces; + protected String eol; + Collection cdataSectionElements = Collections.EMPTY_SET; + + protected boolean discardDefaultContent; + protected boolean xmlDeclaration = true; + + public StreamSerializer() + { + this(Stylesheet.OUTPUT_XML, null, null); + } + + public StreamSerializer(String encoding) + { + this(Stylesheet.OUTPUT_XML, encoding, null); + } + + public StreamSerializer(int mode, String encoding, String eol) + { + this.mode = mode; + if (encoding == null) + { + encoding = "UTF-8"; + } + this.encoding = encoding.intern(); + charset = Charset.forName(this.encoding); + encoder = charset.newEncoder(); + this.eol = (eol != null) ? eol : System.getProperty("line.separator"); + namespaces = new HashMap(); + } + + void setCdataSectionElements(Collection c) + { + cdataSectionElements = c; + } + + public void serialize(final Node node, final OutputStream out) + throws IOException + { + serialize(node, out, false); + } + + void serialize(Node node, final OutputStream out, + boolean convertToCdata) + throws IOException + { + while (node != null) + { + Node next = node.getNextSibling(); + doSerialize(node, out, convertToCdata); + node = next; + } + } + + private void doSerialize(final Node node, final OutputStream out, + boolean convertToCdata) + throws IOException + { + if (out == null) + { + throw new NullPointerException("no output stream"); + } + String value, prefix; + Node children; + String uri = node.getNamespaceURI(); + boolean defined = false; + short nt = node.getNodeType(); + if (convertToCdata && nt == Node.TEXT_NODE) + { + nt = Node.CDATA_SECTION_NODE; + } + switch (nt) + { + case Node.ATTRIBUTE_NODE: + prefix = node.getPrefix(); + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) || + XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || + (prefix != null && prefix.startsWith("xmlns:"))) + { + String nsuri = node.getNodeValue(); + if (isDefined(nsuri)) + { + break; + } + String name = node.getLocalName(); + if (name == null) + { + name = node.getNodeName(); + } + define(nsuri, name); + } + else if (uri != null && !isDefined(uri)) + { + prefix = define(uri, prefix); + String nsname = (prefix == null) ? "xmlns" : "xmlns:" + prefix; + out.write(SPACE); + out.write(encodeText(nsname)); + out.write(EQ); + String nsvalue = "'" + encode(uri, true, true) + "'"; + out.write(nsvalue.getBytes(encoding)); + defined = true; + } + out.write(SPACE); + String a_nodeName = node.getNodeName(); + out.write(encodeText(a_nodeName)); + String a_nodeValue = node.getNodeValue(); + if (mode == Stylesheet.OUTPUT_HTML && + a_nodeName.equals(a_nodeValue) && + isHTMLBoolean((Attr) node, a_nodeName)) + { + break; + } + out.write(EQ); + value = "'" + encode(a_nodeValue, true, true) + "'"; + out.write(encodeText(value)); + break; + case Node.ELEMENT_NODE: + value = node.getNodeName(); + out.write(BRA); + out.write(encodeText(value)); + if (uri != null && !isDefined(uri)) + { + prefix = define(uri, node.getPrefix()); + String nsname = (prefix == null) ? "xmlns" : "xmlns:" + prefix; + out.write(SPACE); + out.write(encodeText(nsname)); + out.write(EQ); + String nsvalue = "'" + encode(uri, true, true) + "'"; + out.write(encodeText(nsvalue)); + defined = true; + } + NamedNodeMap attrs = node.getAttributes(); + if (attrs != null) + { + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Attr attr = (Attr) attrs.item(i); + if (discardDefaultContent && !attr.getSpecified()) + { + // NOOP + } + else + { + serialize(attr, out, false); + } + } + } + convertToCdata = cdataSectionElements.contains(value); + children = node.getFirstChild(); + if (children == null) + { + out.write(SLASH); + out.write(KET); + } + else + { + out.write(KET); + serialize(children, out, convertToCdata); + out.write(BRA); + out.write(SLASH); + out.write(encodeText(value)); + out.write(KET); + } + break; + case Node.TEXT_NODE: + value = node.getNodeValue(); + if (!"yes".equals(node.getUserData("disable-output-escaping"))) + { + value = encode(value, false, false); + } + out.write(encodeText(value)); + break; + case Node.CDATA_SECTION_NODE: + value = "<![CDATA[" + node.getNodeValue() + "]]>"; + out.write(encodeText(value)); + break; + case Node.COMMENT_NODE: + value = "<!--" + node.getNodeValue() + "-->"; + out.write(encodeText(value)); + Node cp = node.getParentNode(); + if (cp != null && cp.getNodeType() == Node.DOCUMENT_NODE) + { + out.write(encodeText(eol)); + } + break; + case Node.DOCUMENT_NODE: + case Node.DOCUMENT_FRAGMENT_NODE: + if (mode == Stylesheet.OUTPUT_XML) + { + if ("UTF-16".equalsIgnoreCase(encoding)) + { + out.write(0xfe); + out.write(0xff); + } + if (!"yes".equals(node.getUserData("omit-xml-declaration")) && + xmlDeclaration) + { + Document doc = (node instanceof Document) ? + (Document) node : null; + String version = (doc != null) ? doc.getXmlVersion() : null; + if (version == null) + { + version = (String) node.getUserData("version"); + } + if (version == null) + { + version = "1.0"; + } + out.write(BRA); + out.write(0x3f); + out.write("xml version='".getBytes("US-ASCII")); + out.write(version.getBytes("US-ASCII")); + out.write(APOS); + if (!("UTF-8".equalsIgnoreCase(encoding))) + { + out.write(" encoding='".getBytes("US-ASCII")); + out.write(encoding.getBytes("US-ASCII")); + out.write(APOS); + } + if ((doc != null && doc.getXmlStandalone()) || + "yes".equals(node.getUserData("standalone"))) + { + out.write(" standalone='yes'".getBytes("US-ASCII")); + } + out.write(0x3f); + out.write(KET); + out.write(encodeText(eol)); + } + // TODO warn if not outputting the declaration would be a + // problem + } + else if (mode == Stylesheet.OUTPUT_HTML) + { + // Ensure that encoding is accessible + String mediaType = (String) node.getUserData("media-type"); + if (mediaType == null) + { + mediaType = "text/html"; + } + String contentType = mediaType + "; charset=" + + ((encoding.indexOf(' ') != -1) ? + "\"" + encoding + "\"" : + encoding); + Document doc = (node instanceof Document) ? (Document) node : + node.getOwnerDocument(); + Node html = null; + for (Node ctx = node.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx.getNodeType() == Node.ELEMENT_NODE) + { + html = ctx; + break; + } + } + if (html == null) + { + html = doc.createElement("html"); + node.appendChild(html); + } + Node head = null; + for (Node ctx = html.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx.getNodeType() == Node.ELEMENT_NODE) + { + String name = ctx.getLocalName(); + if (name == null) + { + name = ctx.getNodeName(); + } + if ("head".equalsIgnoreCase(name)) + { + head = ctx; + break; + } + } + } + if (head == null) + { + head = doc.createElement("head"); + Node c1 = null; + for (Node ctx = html.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx.getNodeType() == Node.ELEMENT_NODE) + { + c1 = ctx; + break; + } + } + if (c1 != null) + { + html.insertBefore(head, c1); + } + else + { + html.appendChild(head); + } + } + Node meta = null; + Node metaContent = null; + for (Node ctx = head.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + if (ctx.getNodeType() == Node.ELEMENT_NODE) + { + String name = ctx.getLocalName(); + if (name == null) + { + name = ctx.getNodeName(); + } + if ("meta".equalsIgnoreCase(name)) + { + NamedNodeMap metaAttrs = ctx.getAttributes(); + int len = metaAttrs.getLength(); + String httpEquiv = null; + Node content = null; + for (int i = 0; i < len; i++) + { + Node attr = metaAttrs.item(i); + String attrName = attr.getNodeName(); + if ("http-equiv".equalsIgnoreCase(attrName)) + { + httpEquiv = attr.getNodeValue(); + } + else if ("content".equalsIgnoreCase(attrName)) + { + content = attr; + } + } + if ("Content-Type".equalsIgnoreCase(httpEquiv)) + { + meta = ctx; + metaContent = content; + break; + } + } + } + } + if (meta == null) + { + meta = doc.createElement("meta"); + // Insert first + Node first = head.getFirstChild(); + if (first == null) + { + head.appendChild(meta); + } + else + { + head.insertBefore(meta, first); + } + Node metaHttpEquiv = doc.createAttribute("http-equiv"); + meta.getAttributes().setNamedItem(metaHttpEquiv); + metaHttpEquiv.setNodeValue("Content-Type"); + } + if (metaContent == null) + { + metaContent = doc.createAttribute("content"); + meta.getAttributes().setNamedItem(metaContent); + } + metaContent.setNodeValue(contentType); + // phew + } + children = node.getFirstChild(); + if (children != null) + { + serialize(children, out, convertToCdata); + } + break; + case Node.DOCUMENT_TYPE_NODE: + DocumentType doctype = (DocumentType) node; + out.write(BRA); + out.write(BANG); + out.write(encodeText("DOCTYPE ")); + value = doctype.getNodeName(); + out.write(encodeText(value)); + String publicId = doctype.getPublicId(); + if (publicId != null) + { + out.write(encodeText(" PUBLIC ")); + out.write(APOS); + out.write(encodeText(publicId)); + out.write(APOS); + } + String systemId = doctype.getSystemId(); + if (systemId != null) + { + out.write(encodeText(" SYSTEM ")); + out.write(APOS); + out.write(encodeText(systemId)); + out.write(APOS); + } + String internalSubset = doctype.getInternalSubset(); + if (internalSubset != null) + { + out.write(encodeText(internalSubset)); + } + out.write(KET); + out.write(eol.getBytes(encoding)); + break; + case Node.ENTITY_REFERENCE_NODE: + value = "&" + node.getNodeValue() + ";"; + out.write(encodeText(value)); + break; + case Node.PROCESSING_INSTRUCTION_NODE: + value = "<?" + node.getNodeName() + " " + node.getNodeValue() + "?>"; + out.write(encodeText(value)); + Node pp = node.getParentNode(); + if (pp != null && pp.getNodeType() == Node.DOCUMENT_NODE) + { + out.write(encodeText(eol)); + } + break; + } + if (defined) + { + undefine(uri); + } + } + + boolean isDefined(String uri) + { + return XMLConstants.XML_NS_URI.equals(uri) || + XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) || + namespaces.containsKey(uri); + } + + String define(String uri, String prefix) + { + while (namespaces.containsValue(prefix)) + { + // Fabricate new prefix + prefix = prefix + "_"; + } + namespaces.put(uri, prefix); + return prefix; + } + + void undefine(String uri) + { + namespaces.remove(uri); + } + + final byte[] encodeText(String text) + throws IOException + { + encoder.reset(); + if (!encoder.canEncode(text)) + { + // Check each character + StringBuffer buf = new StringBuffer(); + int len = text.length(); + for (int i = 0; i < len; i++) + { + char c = text.charAt(i); + if (encoder.canEncode(c)) + { + buf.append(c); + } + else + { + // Replace with character entity reference + String hex = Integer.toHexString((int) c); + buf.append("&#x"); + buf.append(hex); + buf.append(';'); + } + } + text = buf.toString(); + } + ByteBuffer encoded = encoder.encode(CharBuffer.wrap(text)); + int len = encoded.limit() - encoded.position(); + if (encoded.hasArray()) + { + byte[] ret = encoded.array(); + if (ret.length > len) + { + // Why? + byte[] ret2 = new byte[len]; + System.arraycopy(ret, 0, ret2, 0, len); + ret = ret2; + } + return ret; + } + encoded.flip(); + byte[] ret = new byte[len]; + encoded.get(ret, 0, len); + return ret; + } + + String encode(String text, boolean encodeCtl, boolean inAttr) + { + int len = text.length(); + StringBuffer buf = null; + for (int i = 0; i < len; i++) + { + char c = text.charAt(i); + if (c == '<') + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("<"); + } + else if (c == '>') + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append(">"); + } + else if (c == '&') + { + if (mode == Stylesheet.OUTPUT_HTML && (i + 1) < len && + text.charAt(i + 1) == '{') + { + if (buf != null) + { + buf.append(c); + } + } + else + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("&"); + } + } + else if (c == '\'' && inAttr) + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("'"); + } + else if (c == '"' && inAttr) + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("""); + } + else if (encodeCtl) + { + if (c < 0x20) + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append('&'); + buf.append('#'); + buf.append((int) c); + buf.append(';'); + } + else if (buf != null) + { + buf.append(c); + } + } + else if (buf != null) + { + buf.append(c); + } + } + return (buf == null) ? text : buf.toString(); + } + + String toString(Node node) + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try + { + serialize(node, out); + return new String(out.toByteArray(), encoding); + } + catch (IOException e) + { + throw new RuntimeException(e.getMessage()); + } + } + + boolean isHTMLBoolean(Attr attr, String attrName) + { + attrName = attrName.toLowerCase(); + Node element = attr.getOwnerElement(); + String elementName = element.getLocalName(); + if (elementName == null) + { + elementName = element.getNodeName(); + } + elementName = elementName.toLowerCase(); + Collection attributes = + (Collection) HTML_BOOLEAN_ATTRIBUTES.get(elementName); + return (attributes != null && attributes.contains(attrName)); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/Stylesheet.java b/libjava/classpath/gnu/xml/transform/Stylesheet.java new file mode 100644 index 00000000000..99431b699d8 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/Stylesheet.java @@ -0,0 +1,1832 @@ +/* Stylesheet.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.TreeSet; +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionResolver; +import javax.xml.xpath.XPathExpressionException; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.NameTest; +import gnu.xml.xpath.NodeTypeTest; +import gnu.xml.xpath.Pattern; +import gnu.xml.xpath.Selector; +import gnu.xml.xpath.Root; +import gnu.xml.xpath.Test; +import gnu.xml.xpath.XPathImpl; + +/** + * An XSL stylesheet. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class Stylesheet + implements NamespaceContext, XPathFunctionResolver, UserDataHandler, Cloneable +{ + + static final String XSL_NS = "http://www.w3.org/1999/XSL/Transform"; + + static final int OUTPUT_XML = 0; + static final int OUTPUT_HTML = 1; + static final int OUTPUT_TEXT = 2; + + final TransformerFactoryImpl factory; + TransformerImpl transformer; + Stylesheet parent; + final XPathImpl xpath; + final String systemId; + final int precedence; + + final boolean debug; + + /** + * Version of XSLT. + */ + String version; + + Collection extensionElementPrefixes; + Collection excludeResultPrefixes; + + /** + * Set of element names for which we should strip whitespace. + */ + Set stripSpace; + + /** + * Set of element names for which we should preserve whitespace. + */ + Set preserveSpace; + + /** + * Output options. + */ + Node output; + int outputMethod; + String outputVersion; + String outputEncoding; + boolean outputOmitXmlDeclaration; + boolean outputStandalone; + String outputPublicId; + String outputSystemId; + Collection outputCdataSectionElements; + boolean outputIndent; + String outputMediaType; + + /** + * Keys. + */ + Collection keys; + + /** + * Decimal formats. + */ + Map decimalFormats; + + /** + * Namespace aliases. + */ + Map namespaceAliases; + + /** + * Attribute-sets. + */ + List attributeSets; + + /** + * Variables. + */ + List variables; + + /** + * Variable and parameter bindings. + */ + Bindings bindings; + + /** + * Templates. + */ + LinkedList templates; + + TemplateNode builtInNodeTemplate; + TemplateNode builtInTextTemplate; + + /** + * Holds the current node while parsing. + * Necessary to associate the document function with its declaring node, + * to resolve namespaces, and to maintain the current node for the + * current() function. + */ + Node current; + + /** + * Set by a terminating message. + */ + transient boolean terminated; + + /** + * Current template in force. + */ + transient Template currentTemplate; + + Stylesheet(TransformerFactoryImpl factory, + Stylesheet parent, + Document doc, + String systemId, + int precedence) + throws TransformerConfigurationException + { + this.factory = factory; + this.systemId = systemId; + this.precedence = precedence; + this.parent = parent; + extensionElementPrefixes = new HashSet(); + excludeResultPrefixes = new HashSet(); + stripSpace = new LinkedHashSet(); + preserveSpace = new LinkedHashSet(); + outputCdataSectionElements = new LinkedHashSet(); + xpath = (XPathImpl) factory.xpathFactory.newXPath(); + if (parent == null) + { + bindings = new Bindings(this); + attributeSets = new LinkedList(); + variables = new LinkedList(); + namespaceAliases = new LinkedHashMap(); + templates = new LinkedList(); + keys = new LinkedList(); + decimalFormats = new LinkedHashMap(); + initDefaultDecimalFormat(); + xpath.setNamespaceContext(this); + xpath.setXPathFunctionResolver(this); + } + else + { + /* Test for import circularity */ + for (Stylesheet ctx = this; ctx.parent != null; ctx = ctx.parent) + { + if (systemId != null && systemId.equals(ctx.parent.systemId)) + { + String msg = "circularity importing " + systemId; + throw new TransformerConfigurationException(msg); + } + } + /* OK */ + Stylesheet root = getRootStylesheet(); + bindings = root.bindings; + attributeSets = root.attributeSets; + variables = root.variables; + namespaceAliases = root.namespaceAliases; + templates = root.templates; + keys = root.keys; + decimalFormats = root.decimalFormats; + xpath.setNamespaceContext(root); + xpath.setXPathFunctionResolver(root); + } + xpath.setXPathVariableResolver(bindings); + + Test anyNode = new NodeTypeTest((short) 0); + List tests = Collections.singletonList(anyNode); + builtInNodeTemplate = + new ApplyTemplatesNode(new Selector(Selector.CHILD, tests), + null, null, null, true); + builtInTextTemplate = + new ValueOfNode(new Selector(Selector.SELF, tests), + false); + + parse(doc.getDocumentElement(), true); + current = doc; // Alow namespace resolution during processing + + debug = ("yes".equals(System.getProperty("xsl.debug"))); + + if (debug) + { + System.err.println("Stylesheet: " + doc.getDocumentURI()); + for (Iterator i = templates.iterator(); i.hasNext(); ) + { + Template t = (Template) i.next(); + t.list(System.err); + System.err.println("--------------------"); + } + } + } + + Stylesheet getRootStylesheet() + { + Stylesheet stylesheet = this; + while (stylesheet.parent != null) + { + stylesheet = stylesheet.parent; + } + return stylesheet; + } + + void initDefaultDecimalFormat() + { + DecimalFormat defaultDecimalFormat = new DecimalFormat(); + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator('.'); + symbols.setGroupingSeparator(','); + symbols.setPercent('%'); + symbols.setPerMill('\u2030'); + symbols.setZeroDigit('0'); + symbols.setDigit('#'); + symbols.setPatternSeparator(';'); + symbols.setInfinity("Infinity"); + symbols.setNaN("NaN"); + symbols.setMinusSign('-'); + defaultDecimalFormat.setDecimalFormatSymbols(symbols); + decimalFormats.put(null, defaultDecimalFormat); + } + + // -- Cloneable -- + + public Object clone() + { + try + { + Stylesheet clone = (Stylesheet) super.clone(); + clone.bindings = (Bindings) bindings.clone(); + + LinkedList templates2 = new LinkedList(); + for (Iterator i = templates.iterator(); i.hasNext(); ) + { + Template t = (Template) i.next(); + templates2.add(t.clone(clone)); + } + clone.templates = templates2; + + LinkedList attributeSets2 = new LinkedList(); + for (Iterator i = attributeSets.iterator(); i.hasNext(); ) + { + AttributeSet as = (AttributeSet) i.next(); + attributeSets2.add(as.clone(clone)); + } + clone.attributeSets = attributeSets2; + + LinkedList variables2 = new LinkedList(); + for (Iterator i = variables.iterator(); i.hasNext(); ) + { + ParameterNode var = (ParameterNode) i.next(); + variables2.add(var.clone(clone)); + } + clone.variables = variables2; + + LinkedList keys2 = new LinkedList(); + for (Iterator i = keys.iterator(); i.hasNext(); ) + { + Key k = (Key) i.next(); + keys2.add(k.clone(clone)); + } + clone.keys = keys2; + + return clone; + } + catch (CloneNotSupportedException e) + { + throw new Error(e.getMessage()); + } + } + + // -- Variable evaluation -- + + void initTopLevelVariables(Node context) + throws TransformerException + { + current = context; + // Sort the variables into order + // See XSLT 11.4: "If the template or expression specifying the value of + // a global variable x references a global variable y, then the value + // for y must be computed before the value of x." + List topLevel = new ArrayList(variables); + Collections.sort(topLevel); + for (Iterator i = topLevel.iterator(); i.hasNext(); ) + { + ParameterNode var = (ParameterNode) i.next(); + bindings.set(var.name, + var.getValue(this, null, context, 1, 1), + var.type); + } + current = null; + } + + // -- NamespaceContext -- + + public String getNamespaceURI(String prefix) + { + return (current == null) ? null : current.lookupNamespaceURI(prefix); + } + + public String getPrefix(String namespaceURI) + { + return (current == null) ? null : current.lookupPrefix(namespaceURI); + } + + public Iterator getPrefixes(String namespaceURI) + { + // TODO + return Collections.singleton(getPrefix(namespaceURI)).iterator(); + } + + final QName getQName(String name) + { + String localName = name, uri = null, prefix = null; + int ci = name.indexOf(':'); + if (ci != -1) + { + prefix = name.substring(0, ci); + localName = name.substring(ci + 1); + uri = getNamespaceURI(prefix); + } + return new QName(uri, localName, prefix); + } + + // -- Template selection -- + + TemplateNode getTemplate(QName mode, Node context, boolean applyImports) + throws TransformerException + { + if (debug) + { + System.err.println("getTemplate: mode="+mode+" context="+context); + } + Set candidates = new TreeSet(); + for (Iterator j = templates.iterator(); j.hasNext(); ) + { + Template t = (Template) j.next(); + boolean isMatch = t.matches(mode, context); + if (applyImports) + { + if (currentTemplate == null) + { + String msg = "current template may not be null " + + "during apply-imports"; + throw new TransformerException(msg); + } + if (!currentTemplate.imports(t)) + { + isMatch = false; + } + } + //System.err.println("\t"+context+" "+t+"="+isMatch); + if (isMatch) + { + candidates.add(t); + } + } + //System.err.println("\tcandidates="+candidates); + if (candidates.isEmpty()) + { + // Apply built-in template + // Current template is unchanged + if (debug) + { + System.err.println("\tbuiltInTemplate context="+context); + } + switch (context.getNodeType()) + { + case Node.ELEMENT_NODE: + case Node.DOCUMENT_NODE: + case Node.DOCUMENT_FRAGMENT_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + case Node.COMMENT_NODE: + return builtInNodeTemplate; + case Node.TEXT_NODE: + case Node.ATTRIBUTE_NODE: + return builtInTextTemplate; + default: + return null; + } + } + else + { + Template t = (Template) candidates.iterator().next(); + // Set current template + currentTemplate = t; + if (debug) + { + System.err.println("\ttemplate="+t+" context="+context); + } + return t.node; + } + } + + TemplateNode getTemplate(QName mode, QName name) + throws TransformerException + { + //System.err.println("getTemplate: mode="+mode+" name="+name); + Set candidates = new TreeSet(); + for (Iterator j = templates.iterator(); j.hasNext(); ) + { + Template t = (Template) j.next(); + boolean isMatch = t.matches(name); + //System.err.println("\t"+name+" "+t+"="+isMatch); + if (isMatch) + { + candidates.add(t); + } + } + if (candidates.isEmpty()) + { + return null; + //throw new TransformerException("template '" + name + "' not found"); + } + Template t = (Template) candidates.iterator().next(); + //System.err.println("\ttemplate="+t+" context="+context); + return t.node; + } + + /** + * template + */ + final Template parseTemplate(Node node, NamedNodeMap attrs) + throws TransformerConfigurationException, XPathExpressionException + { + String n = getAttribute(attrs, "name"); + QName name = (n == null) ? null : getQName(n); + String m = getAttribute(attrs, "match"); + Pattern match = null; + if (m != null) + { + try + { + match = (Pattern) xpath.compile(m); + } + catch (ClassCastException e) + { + String msg = "illegal pattern: " + m; + throw new TransformerConfigurationException(msg); + } + } + String p = getAttribute(attrs, "priority"); + String mm = getAttribute(attrs, "mode"); + QName mode = (mm == null) ? null : getQName(mm); + double priority = (p == null) ? Template.DEFAULT_PRIORITY : + Double.parseDouble(p); + Node children = node.getFirstChild(); + return new Template(this, name, match, parse(children), + precedence, priority, mode); + } + + /** + * output + */ + final void parseOutput(Node node, NamedNodeMap attrs) + throws TransformerConfigurationException + { + output = node; + String method = getAttribute(attrs, "method"); + if ("xml".equals(method) || method == null) + { + outputMethod = OUTPUT_XML; + } + else if ("html".equals(method)) + { + outputMethod = OUTPUT_HTML; + } + else if ("text".equals(method)) + { + outputMethod = OUTPUT_TEXT; + } + else + { + String msg = "unsupported output method: " + method; + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(msg, l); + } + outputPublicId = getAttribute(attrs, "public-id"); + outputSystemId = getAttribute(attrs, "system-id"); + outputEncoding = getAttribute(attrs, "encoding"); + String indent = getAttribute(attrs, "indent"); + if (indent != null) + { + outputIndent = "yes".equals(indent); + } + outputVersion = getAttribute(attrs, "version"); + String omitXmlDecl = getAttribute(attrs, "omit-xml-declaration"); + if (omitXmlDecl != null) + { + outputOmitXmlDeclaration = "yes".equals(omitXmlDecl); + } + String standalone = getAttribute(attrs, "standalone"); + if (standalone != null) + { + outputStandalone = "yes".equals(standalone); + } + outputMediaType = getAttribute(attrs, "media-type"); + String cdataSectionElements = + getAttribute(attrs, "cdata-section-elements"); + if (cdataSectionElements != null) + { + StringTokenizer st = new StringTokenizer(cdataSectionElements, " "); + while (st.hasMoreTokens()) + { + outputCdataSectionElements.add(st.nextToken()); + } + } + } + + /** + * key + */ + final void parseKey(Node node, NamedNodeMap attrs) + throws TransformerConfigurationException, XPathExpressionException + { + String n = getRequiredAttribute(attrs, "name", node); + String m = getRequiredAttribute(attrs, "match", node); + String u = getRequiredAttribute(attrs, "use", node); + QName name = getQName(n); + Expr use = (Expr) xpath.compile(u); + try + { + Pattern match = (Pattern) xpath.compile(m); + Key key = new Key(name, match, use); + keys.add(key); + } + catch (ClassCastException e) + { + throw new TransformerConfigurationException("invalid pattern: " + m); + } + } + + /** + * decimal-format + */ + final void parseDecimalFormat(Node node, NamedNodeMap attrs) + throws TransformerConfigurationException + { + String dfName = getAttribute(attrs, "name"); + DecimalFormat df = new DecimalFormat(); + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator(parseDFChar(attrs, "decimal-separator", '.')); + symbols.setGroupingSeparator(parseDFChar(attrs, "grouping-separator", ',')); + symbols.setInfinity(parseDFString(attrs, "infinity", "Infinity")); + symbols.setMinusSign(parseDFChar(attrs, "minus-sign", '-')); + symbols.setNaN(parseDFString(attrs, "NaN", "NaN")); + symbols.setPercent(parseDFChar(attrs, "percent", '%')); + symbols.setPerMill(parseDFChar(attrs, "per-mille", '\u2030')); + symbols.setZeroDigit(parseDFChar(attrs, "zero-digit", '0')); + symbols.setDigit(parseDFChar(attrs, "digit", '#')); + symbols.setPatternSeparator(parseDFChar(attrs, "pattern-separator", ';')); + df.setDecimalFormatSymbols(symbols); + decimalFormats.put(dfName, df); + } + + private final char parseDFChar(NamedNodeMap attrs, String name, char def) + throws TransformerConfigurationException + { + Node attr = attrs.getNamedItem(name); + try + { + return (attr == null) ? def : attr.getNodeValue().charAt(0); + } + catch (StringIndexOutOfBoundsException e) + { + throw new TransformerConfigurationException("empty attribute '" + + name + + "' in decimal-format", e); + } + } + + private final String parseDFString(NamedNodeMap attrs, String name, + String def) + { + Node attr = attrs.getNamedItem(name); + return (attr == null) ? def : attr.getNodeValue(); + } + + /** + * namespace-alias + */ + final void parseNamespaceAlias(Node node, NamedNodeMap attrs) + throws TransformerConfigurationException + { + String sp = getRequiredAttribute(attrs, "stylesheet-prefix", node); + String rp = getRequiredAttribute(attrs, "result-prefix", node); + namespaceAliases.put(sp, rp); + } + + /** + * attribute-set + */ + final void parseAttributeSet(Node node, NamedNodeMap attrs) + throws TransformerConfigurationException, XPathExpressionException + { + TemplateNode children = parse(node.getFirstChild()); + String name = getRequiredAttribute(attrs, "name", node); + String uas = getAttribute(attrs, "use-attribute-sets"); + attributeSets.add(new AttributeSet(children, name, uas)); + } + + /** + * Parse top-level elements. + */ + void parse(Node node, boolean root) + throws TransformerConfigurationException + { + while (node != null) + { + current = node; + doParse(node, root); + node = node.getNextSibling(); + } + } + + void doParse(Node node, boolean root) + throws TransformerConfigurationException + { + try + { + String namespaceUri = node.getNamespaceURI(); + if (XSL_NS.equals(namespaceUri) && + node.getNodeType() == Node.ELEMENT_NODE) + { + String name = node.getLocalName(); + NamedNodeMap attrs = node.getAttributes(); + if ("stylesheet".equals(name)) + { + version = getAttribute(attrs, "version"); + String eep = getAttribute(attrs, "extension-element-prefixes"); + if (eep != null) + { + StringTokenizer st = new StringTokenizer(eep); + while (st.hasMoreTokens()) + { + extensionElementPrefixes.add(st.nextToken()); + } + } + String erp = getAttribute(attrs, "exclude-result-prefixes"); + if (erp != null) + { + StringTokenizer st = new StringTokenizer(erp); + while (st.hasMoreTokens()) + { + excludeResultPrefixes.add(st.nextToken()); + } + } + parse(node.getFirstChild(), false); + } + else if ("template".equals(name)) + { + templates.add(parseTemplate(node, attrs)); + } + else if ("param".equals(name) || + "variable".equals(name)) + { + int type = "variable".equals(name) ? + Bindings.VARIABLE : Bindings.PARAM; + TemplateNode content = parse(node.getFirstChild()); + QName paramName = + getQName(getRequiredAttribute(attrs, "name", node)); + String select = getAttribute(attrs, "select"); + ParameterNode param; + if (select != null && select.length() > 0) + { + if (content != null) + { + String msg = "parameter '" + paramName + + "' has both select and content"; + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(msg, l); + } + Expr expr = (Expr) xpath.compile(select); + param = new ParameterNode(paramName, expr, type); + } + else + { + param = new ParameterNode(paramName, null, type); + param.children = content; + } + variables.add(param); + } + else if ("include".equals(name) || "import".equals(name)) + { + int delta = "import".equals(name) ? -1 : 0; + String href = getRequiredAttribute(attrs, "href", node); + Source source; + synchronized (factory.resolver) + { + if (transformer != null) + { + factory.resolver + .setUserResolver(transformer.getURIResolver()); + factory.resolver + .setUserListener(transformer.getErrorListener()); + } + source = factory.resolver.resolve(systemId, href); + } + factory.newStylesheet(source, precedence + delta, this); + } + else if ("output".equals(name)) + { + parseOutput(node, attrs); + } + else if ("preserve-space".equals(name)) + { + String elements = + getRequiredAttribute(attrs, "elements", node); + StringTokenizer st = new StringTokenizer(elements, + " \t\n\r"); + while (st.hasMoreTokens()) + { + preserveSpace.add(parseNameTest(st.nextToken())); + } + } + else if ("strip-space".equals(name)) + { + String elements = + getRequiredAttribute(attrs, "elements", node); + StringTokenizer st = new StringTokenizer(elements, + " \t\n\r"); + while (st.hasMoreTokens()) + { + stripSpace.add(parseNameTest(st.nextToken())); + } + } + else if ("key".equals(name)) + { + parseKey(node, attrs); + } + else if ("decimal-format".equals(name)) + { + parseDecimalFormat(node, attrs); + } + else if ("namespace-alias".equals(name)) + { + parseNamespaceAlias(node, attrs); + } + else if ("attribute-set".equals(name)) + { + parseAttributeSet(node, attrs); + } + } + else if (root) + { + // Literal document element + Attr versionNode = + ((Element)node).getAttributeNodeNS(XSL_NS, "version"); + if (versionNode == null) + { + String msg = "no xsl:version attribute on literal result node"; + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(msg, l); + } + version = versionNode.getValue(); + Node rootClone = node.cloneNode(true); + NamedNodeMap attrs = rootClone.getAttributes(); + attrs.removeNamedItemNS(XSL_NS, "version"); + templates.add(new Template(this, null, new Root(), + parse(rootClone), + precedence, + Template.DEFAULT_PRIORITY, + null)); + } + else + { + // Skip unknown elements, text, comments, etc + } + } + catch (TransformerException e) + { + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(e.getMessage(), l, e); + } + catch (DOMException e) + { + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(e.getMessage(), l, e); + } + catch (XPathExpressionException e) + { + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(e.getMessage(), l, e); + } + } + + final NameTest parseNameTest(String token) + { + if ("*".equals(token)) + { + return new NameTest(null, true, true); + } + else if (token.endsWith(":*")) + { + QName qName = getQName(token.substring(0, token.length() - 2)); + return new NameTest(qName, true, false); + } + else + { + QName qName = getQName(token); + return new NameTest(qName, false, false); + } + } + + final TemplateNode parseAttributeValueTemplate(String value, Node source) + throws TransformerConfigurationException, XPathExpressionException + { + current = source; + // Tokenize + int len = value.length(); + int off = 0; + List tokens = new ArrayList(); // text tokens + List types = new ArrayList(); // literal or expression + int depth = 0; + for (int i = 0; i < len; i++) + { + char c = value.charAt(i); + if (c == '{') + { + if (i < (len - 1) && value.charAt(i + 1) == '{') + { + tokens.add(value.substring(off, i + 1)); + types.add(Boolean.FALSE); + i++; + off = i + 1; + continue; + } + if (depth == 0) + { + if (i - off > 0) + { + tokens.add(value.substring(off, i)); + types.add(Boolean.FALSE); + } + off = i + 1; + } + depth++; + } + else if (c == '}') + { + if (i < (len - 1) && value.charAt(i + 1) == '}') + { + tokens.add(value.substring(off, i + 1)); + types.add(Boolean.FALSE); + i++; + off = i + 1; + continue; + } + if (depth == 1) + { + if (i - off > 0) + { + tokens.add(value.substring(off, i)); + types.add(Boolean.TRUE); + } + else + { + String msg = "attribute value template " + + "must contain expression: " + value; + DOMSourceLocator l = new DOMSourceLocator(source); + throw new TransformerConfigurationException(msg, l); + } + off = i + 1; + } + depth--; + } + } + if (depth > 0) + { + String msg = "invalid attribute value template: " + value; + throw new TransformerConfigurationException(msg); + } + if (len - off > 0) + { + // Trailing text + tokens.add(value.substring(off)); + types.add(Boolean.FALSE); + } + + // Construct template node tree + TemplateNode ret = null; + Document doc = source.getOwnerDocument(); + len = tokens.size(); + for (int i = len - 1; i >= 0; i--) + { + String token = (String) tokens.get(i); + Boolean type = (Boolean) types.get(i); + if (type == Boolean.TRUE) + { + // Expression text + Expr select = (Expr) xpath.compile(token); + TemplateNode ret2 = new ValueOfNode(select, false); + ret2.next = ret; + ret = ret2; + } + else + { + // Verbatim text + TemplateNode ret2 = new LiteralNode(doc.createTextNode(token)); + ret2.next = ret; + ret = ret2; + } + } + return ret; + } + + boolean isPreserved(Text text) + throws TransformerConfigurationException + { + // Check characters in text + String value = text.getData(); + if (value != null) + { + int len = value.length(); + for (int i = 0; i < len; i++) + { + char c = value.charAt(i); + if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d) + { + return true; + } + } + } + // Check parent node + Node ctx = text.getParentNode(); + if (!preserveSpace.isEmpty()) + { + for (Iterator i = preserveSpace.iterator(); i.hasNext(); ) + { + NameTest preserveTest = (NameTest) i.next(); + if (preserveTest.matches(ctx, 1, 1)) + { + boolean override = false; + if (!stripSpace.isEmpty()) + { + for (Iterator j = stripSpace.iterator(); j.hasNext(); ) + { + NameTest stripTest = (NameTest) j.next(); + if (stripTest.matches(ctx, 1, 1)) + { + override = true; + break; + } + } + } + if (!override) + { + return true; + } + } + } + } + // Check whether any ancestor specified xml:space + while (ctx != null) + { + if (ctx.getNodeType() == Node.ELEMENT_NODE) + { + Element element = (Element) ctx; + String xmlSpace = element.getAttribute("xml:space"); + if ("default".equals(xmlSpace)) + { + break; + } + else if ("preserve".equals(xmlSpace)) + { + return true; + } + else if (xmlSpace.length() > 0) + { + String msg = "Illegal value for xml:space: " + xmlSpace; + throw new TransformerConfigurationException(msg); + } + else if ("text".equals(ctx.getLocalName()) && + XSL_NS.equals(ctx.getNamespaceURI())) + { + // xsl:text implies xml:space='preserve' + return true; + } + } + ctx = ctx.getParentNode(); + } + return false; + } + + public XPathFunction resolveFunction(QName name, int arity) + { + String uri = name.getNamespaceURI(); + if (XSL_NS.equals(uri) || uri == null || uri.length() == 0) + { + String localName = name.getLocalPart(); + if ("document".equals(localName) && (arity == 1 || arity == 2)) + { + if (current == null) + { + throw new RuntimeException("current is null"); + } + return new DocumentFunction(getRootStylesheet(), current); + } + else if ("key".equals(localName) && (arity == 2)) + { + return new KeyFunction(getRootStylesheet()); + } + else if ("format-number".equals(localName) && + (arity == 2 || arity == 3)) + { + return new FormatNumberFunction(getRootStylesheet()); + } + else if ("current".equals(localName) && (arity == 0)) + { + return new CurrentFunction(getRootStylesheet()); + } + else if ("unparsed-entity-uri".equals(localName) && (arity == 1)) + { + return new UnparsedEntityUriFunction(); + } + else if ("generate-id".equals(localName) && + (arity == 1 || arity == 0)) + { + return new GenerateIdFunction(); + } + else if ("system-property".equals(localName) && (arity == 1)) + { + return new SystemPropertyFunction(); + } + else if ("element-available".equals(localName) && (arity == 1)) + { + return new ElementAvailableFunction(this); + } + else if ("function-available".equals(localName) && (arity == 1)) + { + return new FunctionAvailableFunction(this); + } + } + return null; + } + + // -- Parsing -- + + /** + * apply-templates + */ + final TemplateNode parseApplyTemplates(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String m = getAttribute(attrs, "mode"); + QName mode = (m == null) ? null : getQName(m); + String s = getAttribute(attrs, "select"); + if (s == null) + { + s = "child::node()"; + } + Node children = node.getFirstChild(); + List sortKeys = parseSortKeys(children); + List withParams = parseWithParams(children); + Expr select = (Expr) xpath.compile(s); + return new ApplyTemplatesNode(select, mode, + sortKeys, withParams, false); + } + + /** + * call-template + */ + final TemplateNode parseCallTemplate(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String n = getRequiredAttribute(attrs, "name", node); + QName name = getQName(n); + Node children = node.getFirstChild(); + List withParams = parseWithParams(children); + return new CallTemplateNode(name, withParams); + } + + /** + * value-of + */ + final TemplateNode parseValueOf(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String s = getRequiredAttribute(attrs, "select", node); + String doe = getAttribute(attrs, "disable-output-escaping"); + boolean d = "yes".equals(doe); + Expr select = (Expr) xpath.compile(s); + return new ValueOfNode(select, d); + } + + /** + * for-each + */ + final TemplateNode parseForEach(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String s = getRequiredAttribute(attrs, "select", node); + Node children = node.getFirstChild(); + List sortKeys = parseSortKeys(children); + Expr select = (Expr) xpath.compile(s); + ForEachNode ret = new ForEachNode(select, sortKeys); + ret.children = parse(children); + return ret; + } + + /** + * if + */ + final TemplateNode parseIf(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String t = getRequiredAttribute(attrs, "test", node); + Expr test = (Expr) xpath.compile(t); + Node children = node.getFirstChild(); + IfNode ret = new IfNode(test); + ret.children = parse(children); + return ret; + } + + /** + * when + */ + final TemplateNode parseWhen(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String t = getRequiredAttribute(attrs, "test", node); + Expr test = (Expr) xpath.compile(t); + Node children = node.getFirstChild(); + WhenNode ret = new WhenNode(test); + ret.children = parse(children); + return ret; + } + + /** + * element + */ + final TemplateNode parseElement(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String name = getRequiredAttribute(attrs, "name", node); + String namespace = getAttribute(attrs, "namespace"); + String uas = getAttribute(attrs, "use-attribute-sets"); + TemplateNode n = parseAttributeValueTemplate(name, node); + TemplateNode ns = (namespace == null) ? null : + parseAttributeValueTemplate(namespace, node); + Node children = node.getFirstChild(); + ElementNode ret = new ElementNode(n, ns, uas, node); + ret.children = parse(children); + return ret; + } + + /** + * attribute + */ + final TemplateNode parseAttribute(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String name = getRequiredAttribute(attrs, "name", node); + String namespace = getAttribute(attrs, "namespace"); + TemplateNode n = parseAttributeValueTemplate(name, node); + TemplateNode ns = (namespace == null) ? null : + parseAttributeValueTemplate(namespace, node); + Node children = node.getFirstChild(); + AttributeNode ret = new AttributeNode(n, ns, node); + ret.children = parse(children); + return ret; + } + + /** + * text + */ + final TemplateNode parseText(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String doe = getAttribute(attrs, "disable-output-escaping"); + boolean d = "yes".equals(doe); + Node children = node.getFirstChild(); + TextNode ret = new TextNode(d); + ret.children = parse(children); + return ret; + } + + /** + * copy + */ + final TemplateNode parseCopy(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String uas = getAttribute(attrs, "use-attribute-sets"); + Node children = node.getFirstChild(); + CopyNode ret = new CopyNode(uas); + ret.children = parse(children); + return ret; + } + + /** + * processing-instruction + */ + final TemplateNode parseProcessingInstruction(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String name = getRequiredAttribute(attrs, "name", node); + Node children = node.getFirstChild(); + ProcessingInstructionNode ret = new ProcessingInstructionNode(name); + ret.children = parse(children); + return ret; + } + + /** + * number + */ + final TemplateNode parseNumber(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String v = getAttribute(attrs, "value"); + String ff = getAttribute(attrs, "format"); + if (ff == null) + { + ff = "1"; + } + TemplateNode format = parseAttributeValueTemplate(ff, node); + String lang = getAttribute(attrs, "lang"); + String lv = getAttribute(attrs, "letter-value"); + int letterValue = "traditional".equals(lv) ? + AbstractNumberNode.TRADITIONAL : + AbstractNumberNode.ALPHABETIC; + String gs = getAttribute(attrs, "grouping-separator"); + String gz = getAttribute(attrs, "grouping-size"); + int gz2 = (gz != null && gz.length() > 0) ? + Integer.parseInt(gz) : 1; + Node children = node.getFirstChild(); + TemplateNode ret; + if (v != null && v.length() > 0) + { + Expr value = (Expr) xpath.compile(v); + ret = new NumberNode(value, format, lang, + letterValue, gs, gz2); + } + else + { + String l = getAttribute(attrs, "level"); + int level = + "multiple".equals(l) ? NodeNumberNode.MULTIPLE : + "any".equals(l) ? NodeNumberNode.ANY : + NodeNumberNode.SINGLE; + String c = getAttribute(attrs, "count"); + String f = getAttribute(attrs, "from"); + Pattern count = null; + Pattern from = null; + if (c != null) + { + try + { + count = (Pattern) xpath.compile(c); + } + catch (ClassCastException e) + { + String msg = "invalid pattern: " + c; + throw new TransformerConfigurationException(msg); + } + } + if (f != null) + { + try + { + from = (Pattern) xpath.compile(f); + } + catch (ClassCastException e) + { + String msg = "invalid pattern: " + f; + throw new TransformerConfigurationException(msg); + } + } + ret = new NodeNumberNode(level, count, from, + format, lang, + letterValue, gs, gz2); + } + ret.children = parse(children); + return ret; + } + + /** + * copy-of + */ + final TemplateNode parseCopyOf(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String s = getRequiredAttribute(attrs, "select", node); + Expr select = (Expr) xpath.compile(s); + Node children = node.getFirstChild(); + CopyOfNode ret = new CopyOfNode(select); + ret.children = parse(children); + return ret; + } + + /** + * message + */ + final TemplateNode parseMessage(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + NamedNodeMap attrs = node.getAttributes(); + String t = getAttribute(attrs, "terminate"); + boolean terminate = "yes".equals(t); + Node children = node.getFirstChild(); + MessageNode ret = new MessageNode(terminate); + ret.children = parse(children); + return ret; + } + + /** + * Parse template-level elements. + */ + final TemplateNode parse(Node node) + throws TransformerConfigurationException + { + TemplateNode first = null; + TemplateNode previous = null; + while (node != null) + { + Node next = node.getNextSibling(); + TemplateNode tnode = doParse(node); + if (tnode != null) + { + if (first == null) + { + first = tnode; + } + if (previous != null) + { + previous.next = tnode; + } + previous = tnode; + } + node = next; + } + return first; + } + + private final TemplateNode doParse(Node node) + throws TransformerConfigurationException + { + // Hack to associate the document function with its declaring node + current = node; + try + { + String namespaceUri = node.getNamespaceURI(); + if (Stylesheet.XSL_NS.equals(namespaceUri) && + Node.ELEMENT_NODE == node.getNodeType()) + { + String name = node.getLocalName(); + if ("apply-templates".equals(name)) + { + return parseApplyTemplates(node); + } + else if ("call-template".equals(name)) + { + return parseCallTemplate(node); + } + else if ("value-of".equals(name)) + { + return parseValueOf(node); + } + else if ("for-each".equals(name)) + { + return parseForEach(node); + } + else if ("if".equals(name)) + { + return parseIf(node); + } + else if ("choose".equals(name)) + { + Node children = node.getFirstChild(); + ChooseNode ret = new ChooseNode(); + ret.children = parse(children); + return ret; + } + else if ("when".equals(name)) + { + return parseWhen(node); + } + else if ("otherwise".equals(name)) + { + Node children = node.getFirstChild(); + OtherwiseNode ret = new OtherwiseNode(); + ret.children = parse(children); + return ret; + } + else if ("element".equals(name)) + { + return parseElement(node); + } + else if ("attribute".equals(name)) + { + return parseAttribute(node); + } + else if ("text".equals(name)) + { + return parseText(node); + } + else if ("copy".equals(name)) + { + return parseCopy(node); + } + else if ("processing-instruction".equals(name)) + { + return parseProcessingInstruction(node); + } + else if ("comment".equals(name)) + { + Node children = node.getFirstChild(); + CommentNode ret = new CommentNode(); + ret.children = parse(children); + return ret; + } + else if ("number".equals(name)) + { + return parseNumber(node); + } + else if ("param".equals(name) || + "variable".equals(name)) + { + int type = "variable".equals(name) ? + Bindings.VARIABLE : Bindings.PARAM; + NamedNodeMap attrs = node.getAttributes(); + Node children = node.getFirstChild(); + TemplateNode content = parse(children); + QName paramName = + getQName(getRequiredAttribute(attrs, "name", node)); + String select = getAttribute(attrs, "select"); + ParameterNode ret; + if (select != null) + { + if (content != null) + { + String msg = "parameter '" + paramName + + "' has both select and content"; + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(msg, l); + } + Expr expr = (Expr) xpath.compile(select); + ret = new ParameterNode(paramName, expr, type); + } + else + { + ret = new ParameterNode(paramName, null, type); + ret.children = content; + } + return ret; + } + else if ("copy-of".equals(name)) + { + return parseCopyOf(node); + } + else if ("message".equals(name)) + { + return parseMessage(node); + } + else if ("apply-imports".equals(name)) + { + Node children = node.getFirstChild(); + ApplyImportsNode ret = new ApplyImportsNode(); + ret.children = parse(children); + return ret; + } + else + { + // xsl:fallback + // Pass over any other XSLT nodes + return null; + } + } + String prefix = node.getPrefix(); + if (extensionElementPrefixes.contains(prefix)) + { + // Pass over extension elements + return null; + } + switch (node.getNodeType()) + { + case Node.TEXT_NODE: + // Determine whether to strip whitespace + Text text = (Text) node; + if (!isPreserved(text)) + { + // Strip + /*String data = text.getData().trim(); + if (data.length() > 0) + { + text.setData(data); + } // else */ + text.getParentNode().removeChild(text); + return null; + } + break; + case Node.COMMENT_NODE: + // Ignore comments + return null; + case Node.ELEMENT_NODE: + // Check for attribute value templates and use-attribute-sets + NamedNodeMap attrs = node.getAttributes(); + boolean convert = false; + String useAttributeSets = null; + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String value = attr.getNodeValue(); + if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI()) && + "use-attribute-sets".equals(attr.getLocalName())) + { + useAttributeSets = value; + convert = true; + break; + } + int start = value.indexOf('{'); + int end = value.indexOf('}'); + if (start != -1 || end != -1) + { + convert = true; + break; + } + } + if (convert) + { + // Create an element-producing template node instead + // with appropriate attribute-producing child template nodes + Node children = node.getFirstChild(); + TemplateNode child = parse(children); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String ans = attr.getNamespaceURI(); + String aname = attr.getNodeName(); + if (Stylesheet.XSL_NS.equals(ans) && + "use-attribute-sets".equals(attr.getLocalName())) + { + continue; + } + String value = attr.getNodeValue(); + TemplateNode grandchild = + parseAttributeValueTemplate(value, node); + TemplateNode n = + parseAttributeValueTemplate(aname, node); + TemplateNode ns = (ans == null) ? null : + parseAttributeValueTemplate(ans, node); + TemplateNode newChild = new AttributeNode(n, ns, attr); + newChild.children = grandchild; + newChild.next = child; + child = newChild; + } + String ename = node.getNodeName(); + TemplateNode n = parseAttributeValueTemplate(ename, node); + TemplateNode ns = (namespaceUri == null) ? null : + parseAttributeValueTemplate(namespaceUri, node); + ElementNode ret = new ElementNode(n, ns, useAttributeSets, + node); + ret.children = child; + return ret; + } + // Otherwise fall through + break; + } + } + catch (XPathExpressionException e) + { + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(e.getMessage(), l, e); + } + Node children = node.getFirstChild(); + LiteralNode ret = new LiteralNode(node); + ret.children = parse(children); + return ret; + } + + final List parseSortKeys(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + List ret = new LinkedList(); + while (node != null) + { + String namespaceUri = node.getNamespaceURI(); + if (Stylesheet.XSL_NS.equals(namespaceUri) && + Node.ELEMENT_NODE == node.getNodeType() && + "sort".equals(node.getLocalName())) + { + NamedNodeMap attrs = node.getAttributes(); + String s = getAttribute(attrs, "select"); + if (s == null) + { + s = "."; + } + Expr select = (Expr) xpath.compile(s); + String l = getAttribute(attrs, "lang"); + TemplateNode lang = (l == null) ? null : + parseAttributeValueTemplate(l, node); + String dt = getAttribute(attrs, "data-type"); + TemplateNode dataType = (dt == null) ? null : + parseAttributeValueTemplate(dt, node); + String o = getAttribute(attrs, "order"); + TemplateNode order = (o == null) ? null : + parseAttributeValueTemplate(o, node); + String co = getAttribute(attrs, "case-order"); + TemplateNode caseOrder = (co == null) ? null : + parseAttributeValueTemplate(co, node); + ret.add(new SortKey(select, lang, dataType, order, caseOrder)); + } + node = node.getNextSibling(); + } + return ret.isEmpty() ? null : ret; + } + + final List parseWithParams(Node node) + throws TransformerConfigurationException, XPathExpressionException + { + List ret = new LinkedList(); + while (node != null) + { + String namespaceUri = node.getNamespaceURI(); + if (Stylesheet.XSL_NS.equals(namespaceUri) && + Node.ELEMENT_NODE == node.getNodeType() && + "with-param".equals(node.getLocalName())) + { + NamedNodeMap attrs = node.getAttributes(); + TemplateNode content = parse(node.getFirstChild()); + QName name = + getQName(getRequiredAttribute(attrs, "name", node)); + String select = getAttribute(attrs, "select"); + if (select != null) + { + if (content != null) + { + String msg = "parameter '" + name + + "' has both select and content"; + DOMSourceLocator l = new DOMSourceLocator(node); + throw new TransformerConfigurationException(msg, l); + } + Expr expr = (Expr) xpath.compile(select); + ret.add(new WithParam(name, expr)); + } + else + { + ret.add(new WithParam(name, content)); + } + } + node = node.getNextSibling(); + } + return ret.isEmpty() ? null : ret; + } + + /** + * Created element nodes have a copy of the namespace nodes in the + * stylesheet, except the XSLT namespace, extension namespaces, and + * exclude-result-prefixes. + */ + final void addNamespaceNodes(Node source, Node target, Document doc, + Collection elementExcludeResultPrefixes) + { + NamedNodeMap attrs = source.getAttributes(); + if (attrs != null) + { + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String uri = attr.getNamespaceURI(); + if (uri == XMLConstants.XMLNS_ATTRIBUTE_NS_URI) + { + String prefix = attr.getLocalName(); + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) + { + prefix = "#default"; + } + String ns = attr.getNodeValue(); + // Should the namespace be excluded? + if (XSL_NS.equals(ns) || + extensionElementPrefixes.contains(prefix) || + elementExcludeResultPrefixes.contains(prefix) || + excludeResultPrefixes.contains(prefix)) + { + continue; + } + // Is the namespace already defined on the target? + if (prefix == "#default") + { + prefix = null; + } + if (target.lookupNamespaceURI(prefix) != null) + { + continue; + } + attr = attr.cloneNode(true); + attr = doc.adoptNode(attr); + target.getAttributes().setNamedItemNS(attr); + } + } + } + Node parent = source.getParentNode(); + if (parent != null) + { + addNamespaceNodes(parent, target, doc, elementExcludeResultPrefixes); + } + } + + static final String getAttribute(NamedNodeMap attrs, String name) + { + Node attr = attrs.getNamedItem(name); + if (attr == null) + { + return null; + } + String ret = attr.getNodeValue(); + if (ret.length() == 0) + { + return null; + } + return ret; + } + + static final String getRequiredAttribute(NamedNodeMap attrs, String name, + Node source) + throws TransformerConfigurationException + { + String value = getAttribute(attrs, name); + if (value == null || value.length() == 0) + { + String msg = + name + " attribute is required on " + source.getNodeName(); + DOMSourceLocator l = new DOMSourceLocator(source); + throw new TransformerConfigurationException(msg, l); + } + return value; + } + + // Handle user data changes when nodes are cloned etc + + public void handle(short op, String key, Object data, Node src, Node dst) + { + dst.setUserData(key, data, this); + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java b/libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java new file mode 100644 index 00000000000..038df0169b9 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java @@ -0,0 +1,141 @@ +/* SystemPropertyFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>system-property()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class SystemPropertyFunction + extends Expr + implements XPathFunction, Function +{ + + List args; + + public Object evaluate(List args) + throws XPathFunctionException + { + String name = (String) args.get(0); + return systemProperty(QName.valueOf(name)); + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + int arity = args.size(); + List values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + String name = _string(context, values.get(0)); + return systemProperty(QName.valueOf(name)); + } + + Object systemProperty(QName name) + { + String localName = name.getLocalPart(); + String prefix = name.getPrefix(); + String uri = name.getNamespaceURI(); + if (Stylesheet.XSL_NS.equals(uri) || + "xsl".equals(prefix)) + { + if ("version".equals(localName)) + { + return new Double(1.0d); + } + else if ("vendor".equals(localName)) + { + return "The Free Software Foundation"; + } + else if ("vendor-url".equals(localName)) + { + return "http://www.gnu.org/"; + } + else + { + return ""; + } + } + return System.getProperty(localName); + } + + public Expr clone(Object context) + { + SystemPropertyFunction f = new SystemPropertyFunction(); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/Template.java b/libjava/classpath/gnu/xml/transform/Template.java new file mode 100644 index 00000000000..e3c172fb559 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/Template.java @@ -0,0 +1,252 @@ +/* Template.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.PrintStream; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.NameTest; +import gnu.xml.xpath.NodeTypeTest; +import gnu.xml.xpath.Pattern; +import gnu.xml.xpath.Selector; +import gnu.xml.xpath.Test; + +/** + * A template in an XSL stylesheet. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class Template + implements Comparable +{ + + static final double DEFAULT_PRIORITY = 0.5d; + + final Stylesheet stylesheet; + final QName name; + final Pattern match; + final TemplateNode node; + final double priority; + final int precedence; + final QName mode; + + Template(Stylesheet stylesheet, + QName name, Pattern match, TemplateNode node, + int precedence, double priority, QName mode) + { + this.stylesheet = stylesheet; + this.name = name; + this.match = match; + this.node = node; + // adjust priority if necessary + // see XSLT section 5.5 + Test test = getNodeTest(match); + if (test != null) + { + if (test instanceof NameTest) + { + NameTest nameTest = (NameTest) test; + if (nameTest.matchesAny() || + nameTest.matchesAnyLocalName()) + { + priority = -0.25d; + } + else + { + priority = 0.0d; + } + } + else + { + NodeTypeTest nodeTypeTest = (NodeTypeTest) test; + if (nodeTypeTest.getNodeType() == + Node.PROCESSING_INSTRUCTION_NODE && + nodeTypeTest.getData() != null) + { + priority = 0.0d; + } + else + { + priority = -0.5d; + } + } + } + this.precedence = precedence; + this.priority = priority; + this.mode = mode; + } + + Template clone(Stylesheet stylesheet) + { + // FIXME by cloning we lose the imports() functionality, so + // apply-imports will be broken. + return new Template(stylesheet, + name, + (match == null) ? null : + (Pattern) match.clone(stylesheet), + (node == null) ? null : node.clone(stylesheet), + precedence, + priority, + mode); + } + + public int compareTo(Object other) + { + if (other instanceof Template) + { + Template t = (Template) other; + int d = t.precedence - precedence; + if (d != 0) + { + return d; + } + double d2 = t.priority - priority; + if (d2 != 0.0d) + { + return (int) Math.round(d2 * 1000.0d); + } + } + return 0; + } + + Test getNodeTest(Expr expr) + { + if (expr instanceof Selector) + { + Selector selector = (Selector) expr; + Test[] tests = selector.getTests(); + if (tests.length > 0) + { + return tests[0]; + } + } + return null; + } + + boolean matches(QName mode, Node node) + { + if ((mode == null && this.mode != null) || + (mode != null && !mode.equals(this.mode))) + { + return false; + } + if (match == null) + { + return false; + } + return match.matches(node); + } + + boolean matches(QName name) + { + return name.equals(this.name); + } + + boolean imports(Template other) + { + for (Stylesheet ctx = other.stylesheet.parent; + ctx != null; + ctx = ctx.parent) + { + if (ctx == stylesheet) + { + return true; + } + } + return false; + } + + /** + * @param stylesheet the stylesheet + * @param parent the parent of result nodes + * @param context the context node in the source document + * @param pos the context position + * @param len the context size + * @param nextSibling if non-null, add result nodes before this node + */ + void apply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + System.err.println("...applying " + toString() + " to " + context); + if (node != null) + { + node.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + if (name != null) + { + buf.append("name="); + buf.append(name); + } + else if (match != null) + { + buf.append("match="); + buf.append(match); + } + if (mode != null) + { + buf.append(",mode="); + buf.append(mode); + } + buf.append(']'); + return buf.toString(); + + //return (name != null) ? name.toString() : match.toString(); + } + + void list(PrintStream out) + { + out.println(toString()); + if (node != null) + { + node.list(1, out, true); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/TemplateNode.java b/libjava/classpath/gnu/xml/transform/TemplateNode.java new file mode 100644 index 00000000000..36b25cf5222 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/TemplateNode.java @@ -0,0 +1,124 @@ +/* TemplateNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.PrintStream; +import java.util.Comparator; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.DocumentOrderComparator; + +/** + * Wrapper for a source node in a template. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +abstract class TemplateNode +{ + + static final Comparator documentOrderComparator = + new DocumentOrderComparator(); + + TemplateNode children; + TemplateNode next; + + final void apply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + if (stylesheet.terminated) + { + return; + } + if (Thread.currentThread().isInterrupted()) + { + // Try to head off any infinite loops at the pass + return; + } + if (stylesheet.debug) + { + System.err.println("Applying " + toString()); + System.err.println("\twith context=" + context + ", pos=" + pos + + ", len=" + len); + } + doApply(stylesheet, mode, context, pos, len, parent, nextSibling); + } + + abstract void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException; + + abstract TemplateNode clone(Stylesheet stylesheet); + + public boolean references(QName var) + { + if (children != null && children.references(var)) + { + return true; + } + if (next != null && next.references(var)) + { + return true; + } + return false; + } + + /** + * Debugging + */ + void list(int depth, PrintStream out, boolean listNext) + { + for (int i = 0; i < depth; i++) + { + out.print(" "); + } + out.println(toString()); + if (children != null) + { + children.list(depth + 1, out, true); + } + if (listNext && next != null) + { + next.list(depth, out, listNext); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/TemplatesImpl.java b/libjava/classpath/gnu/xml/transform/TemplatesImpl.java new file mode 100644 index 00000000000..527bd979db0 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/TemplatesImpl.java @@ -0,0 +1,80 @@ +/* TemplatesImpl.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Properties; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; + +/** + * GNU precompiled stylesheet implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class TemplatesImpl + implements Templates +{ + + final TransformerFactoryImpl factory; + final Stylesheet stylesheet; + final Properties outputProperties; + + TemplatesImpl(TransformerFactoryImpl factory, Stylesheet stylesheet) + { + this.factory = factory; + this.stylesheet = stylesheet; + outputProperties = new TransformerOutputProperties(stylesheet); + } + + public Transformer newTransformer() + throws TransformerConfigurationException + { + Stylesheet stylesheet = (Stylesheet) this.stylesheet.clone(); + TransformerImpl transformer = + new TransformerImpl(factory, stylesheet, outputProperties); + stylesheet.transformer = transformer; + return transformer; + } + + public Properties getOutputProperties() + { + return (Properties) outputProperties.clone(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/TextNode.java b/libjava/classpath/gnu/xml/transform/TextNode.java new file mode 100644 index 00000000000..1b581e5acbc --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/TextNode.java @@ -0,0 +1,119 @@ +/* TextNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import gnu.xml.xpath.Expr; + +/** + * A template node representing the XSL <code>text</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class TextNode + extends TemplateNode +{ + + final boolean disableOutputEscaping; + + TextNode(boolean disableOutputEscaping) + { + this.disableOutputEscaping = disableOutputEscaping; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new TextNode(disableOutputEscaping); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + String value = ""; + Document doc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + if (children != null) + { + // Create a document fragment to hold the text + DocumentFragment fragment = doc.createDocumentFragment(); + // Apply children to the fragment + children.apply(stylesheet, mode, + context, pos, len, + fragment, null); + // Use XPath string-value of fragment + value = Expr.stringValue(fragment); + } + Text text = doc.createTextNode(value); + if (disableOutputEscaping) + { + text.setUserData("disable-output-escaping", "yes", stylesheet); + } + // Insert into result tree + if (nextSibling != null) + { + parent.insertBefore(text, nextSibling); + } + else + { + parent.appendChild(text); + } + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java b/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java new file mode 100644 index 00000000000..dde2017ff6b --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java @@ -0,0 +1,345 @@ +/* TransformerFactoryImpl.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.IOException; +import java.net.URL; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Properties; +import javax.xml.transform.ErrorListener; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.URIResolver; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.xpath.XPathFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import gnu.xml.dom.DomDocument; + +/** + * GNU transformer factory implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class TransformerFactoryImpl + extends TransformerFactory +{ + + final XPathFactory xpathFactory; + final XSLURIResolver resolver; + ErrorListener userListener; + URIResolver userResolver; + + public TransformerFactoryImpl() + { + xpathFactory = new gnu.xml.xpath.XPathFactoryImpl(); + resolver = new XSLURIResolver(); + } + + public Transformer newTransformer(Source source) + throws TransformerConfigurationException + { + Stylesheet stylesheet = newStylesheet(source, 0, null); + Properties outputProperties = + new TransformerOutputProperties(stylesheet); + TransformerImpl transformer = + new TransformerImpl(this, stylesheet, outputProperties); + stylesheet.transformer = transformer; + return transformer; + } + + public Transformer newTransformer() + throws TransformerConfigurationException + { + return new TransformerImpl(this, null, new Properties()); + } + + public Templates newTemplates(Source source) + throws TransformerConfigurationException + { + Stylesheet stylesheet = newStylesheet(source, 0, null); + return new TemplatesImpl(this, stylesheet); + } + + Stylesheet newStylesheet(Source source, int precedence, Stylesheet parent) + throws TransformerConfigurationException + { + Document doc = null; + String systemId = null; + if (source != null) + { + try + { + DOMSource ds; + synchronized (resolver) + { + resolver.setUserResolver(userResolver); + resolver.setUserListener(userListener); + ds = resolver.resolveDOM(source, null, null); + } + Node node = ds.getNode(); + if (node == null) + { + throw new TransformerConfigurationException("no source document"); + } + doc = (node instanceof Document) ? (Document) node : + node.getOwnerDocument(); + systemId = ds.getSystemId(); + } + catch (TransformerException e) + { + throw new TransformerConfigurationException(e); + } + } + return new Stylesheet(this, parent, doc, systemId, precedence); + } + + public Source getAssociatedStylesheet(Source source, + String media, + String title, + String charset) + throws TransformerConfigurationException + { + try + { + DOMSource ds; + synchronized (resolver) + { + resolver.setUserResolver(userResolver); + resolver.setUserListener(userListener); + ds = resolver.resolveDOM(source, null, null); + } + Node node = ds.getNode(); + if (node == null) + { + throw new TransformerConfigurationException("no source document"); + } + Document doc = (node instanceof Document) ? (Document) node : + node.getOwnerDocument(); + LinkedList matches = new LinkedList(); + for (node = doc.getFirstChild(); + node != null; + node = node.getNextSibling()) + { + if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && + "xml-stylesheet".equals(node.getNodeName())) + { + Map params = parseParameters(node.getNodeValue()); + if (media != null && !media.equals(params.get("media"))) + { + continue; + } + if (title != null && !title.equals(params.get("title"))) + { + continue; + } + if (charset != null && !charset.equals(params.get("charset"))) + { + continue; + } + String href = (String) params.get("href"); + URL url = resolver.resolveURL(null, node.getBaseURI(), href); + matches.add(url); + } + } + switch (matches.size()) + { + case 0: + return null; + case 1: + return new StreamSource(((URL) matches.getFirst()).toString()); + default: + // Create a source representing a stylesheet with a list of + // imports + DomDocument ssDoc = new DomDocument(); + ssDoc.setBuilding(true); + // Create document element + Node root = + ssDoc.createElementNS(Stylesheet.XSL_NS, "stylesheet"); + Node version = + ssDoc.createAttributeNS(null, "version"); + version.setNodeValue("1.0"); + root.getAttributes().setNamedItemNS(version); + ssDoc.appendChild(root); + // Create xsl:import for each URL + for (Iterator i = matches.iterator(); i.hasNext(); ) + { + URL url = (URL) i.next(); + Node imp = + ssDoc.createElementNS(Stylesheet.XSL_NS, "import"); + Node href = + ssDoc.createAttributeNS(null, "href"); + href.setNodeValue(url.toString()); + imp.getAttributes().setNamedItemNS(href); + root.appendChild(imp); + } + ssDoc.setBuilding(false); + return new DOMSource(ssDoc); + } + } + catch (IOException e) + { + throw new TransformerConfigurationException(e); + } + catch (TransformerException e) + { + throw new TransformerConfigurationException(e); + } + } + + Map parseParameters(String data) + { + Map ret = new LinkedHashMap(); + int len = data.length(); + String key = null; + int start = 0; + char quoteChar = '\u0000'; + for (int i = 0; i < len; i++) + { + char c = data.charAt(i); + if (quoteChar == '\u0000' && c == ' ') + { + if (key == null && start < i) + { + key = data.substring(start, i); + } + else + { + String val = unquote(data.substring(start, i).trim()); + ret.put(key, val); + key = null; + } + start = i + 1; + } + else if (c == '"') + { + quoteChar = (quoteChar == c) ? '\u0000' : c; + } + else if (c == '\'') + { + quoteChar = (quoteChar == c) ? '\u0000' : c; + } + } + if (start < len && key != null) + { + String val = unquote(data.substring(start, len).trim()); + ret.put(key, val); + } + return ret; + } + + String unquote(String text) + { + int end = text.length() - 1; + if (text.charAt(0) == '\'' && text.charAt(end) == '\'') + { + return text.substring(1, end); + } + if (text.charAt(0) == '"' && text.charAt(end) == '"') + { + return text.substring(1, end); + } + return text; + } + + public void setURIResolver(URIResolver resolver) + { + userResolver = resolver; + } + + public URIResolver getURIResolver() + { + return userResolver; + } + + public void setFeature(String name, boolean value) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException("not supported"); + } + + public boolean getFeature(String name) + { + if (SAXSource.FEATURE.equals(name) || + SAXResult.FEATURE.equals(name) || + StreamSource.FEATURE.equals(name) || + StreamResult.FEATURE.equals(name) || + DOMSource.FEATURE.equals(name) || + DOMResult.FEATURE.equals(name)) + { + return true; + } + return false; + } + + public void setAttribute(String name, Object value) + throws IllegalArgumentException + { + throw new IllegalArgumentException("not supported"); + } + + public Object getAttribute(String name) + throws IllegalArgumentException + { + throw new IllegalArgumentException("not supported"); + } + + public void setErrorListener(ErrorListener listener) + throws IllegalArgumentException + { + userListener = listener; + } + + public ErrorListener getErrorListener() + { + return userListener; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/TransformerImpl.java b/libjava/classpath/gnu/xml/transform/TransformerImpl.java new file mode 100644 index 00000000000..a36aa6173f2 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/TransformerImpl.java @@ -0,0 +1,769 @@ +/* TransformerImpl.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.net.MalformedURLException; +import java.net.UnknownServiceException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.StringTokenizer; +import javax.xml.namespace.QName; +import javax.xml.transform.ErrorListener; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; +import gnu.xml.dom.DomDoctype; +import gnu.xml.dom.DomDocument; +import gnu.xml.dom.ls.WriterOutputStream; + +/** + * The transformation process for a given stylesheet. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class TransformerImpl + extends Transformer +{ + + final TransformerFactoryImpl factory; + final Stylesheet stylesheet; + URIResolver uriResolver; + ErrorListener errorListener; + Properties outputProperties; + + TransformerImpl(TransformerFactoryImpl factory, + Stylesheet stylesheet, + Properties outputProperties) + throws TransformerConfigurationException + { + this.factory = factory; + uriResolver = factory.userResolver; + errorListener = factory.userListener; + this.stylesheet = stylesheet; + this.outputProperties = outputProperties; + if (stylesheet != null) + { + // Set up parameter context for this transformer + stylesheet.bindings.push(Bindings.PARAM); + } + } + + public void transform(Source xmlSource, Result outputTarget) + throws TransformerException + { + // Get the source tree + DOMSource source; + synchronized (factory.resolver) + { + factory.resolver.setUserResolver(uriResolver); + factory.resolver.setUserListener(errorListener); + source = factory.resolver.resolveDOM(xmlSource, null, null); + } + Node context = source.getNode(); + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + if (doc instanceof DomDocument) + { + // Suppress mutation events + ((DomDocument) doc).setBuilding(true); + // TODO find a better/more generic way of doing this than + // casting + } + // Get the result tree + Node parent = null, nextSibling = null; + if (outputTarget instanceof DOMResult) + { + DOMResult dr = (DOMResult) outputTarget; + parent = dr.getNode(); + nextSibling = dr.getNextSibling(); + + Document rdoc = (parent instanceof Document) ? (Document) parent : + parent.getOwnerDocument(); + if (rdoc instanceof DomDocument) + { + // Suppress mutation events and allow multiple root elements + DomDocument drdoc = (DomDocument) rdoc; + drdoc.setBuilding(true); + drdoc.setCheckWellformedness(false); + // TODO find a better/more generic way of doing this than + // casting + } + } + boolean created = false; + // Transformation + if (stylesheet != null) + { + if (parent == null) + { + // Create a new document to hold the result + DomDocument resultDoc = new DomDocument(); + resultDoc.setBuilding(true); + resultDoc.setCheckWellformedness(false); + parent = resultDoc; + created = true; + } + // Make a copy of the source node, and strip it + context = context.cloneNode(true); + strip(context); + // XSLT transformation + try + { + // Set output properties in the underlying stylesheet + ((TransformerOutputProperties) outputProperties).apply(); + stylesheet.initTopLevelVariables(context); + TemplateNode t = stylesheet.getTemplate(null, context, false); + if (t != null) + { + stylesheet.current = context; + t.apply(stylesheet, null, context, 1, 1, parent, nextSibling); + } + } + catch (TransformerException e) + { + // Done transforming, reset document + if (doc instanceof DomDocument) + { + ((DomDocument) doc).setBuilding(false); + } + throw e; + } + } + else + { + // Identity transform + Node clone = context.cloneNode(true); + if (context.getNodeType() != Node.DOCUMENT_NODE) + { + Document resultDoc; + if (parent == null) + { + // Create a new document to hold the result + DomDocument rd = new DomDocument(); + rd.setBuilding(true); + rd.setCheckWellformedness(false); + parent = resultDoc = rd; + created = true; + } + else + { + resultDoc = (parent instanceof Document) ? + (Document) parent : + parent.getOwnerDocument(); + } + Document sourceDoc = context.getOwnerDocument(); + if (sourceDoc != resultDoc) + { + clone = resultDoc.adoptNode(clone); + } + if (nextSibling != null) + { + parent.insertBefore(clone, nextSibling); + } + else + { + parent.appendChild(clone); + } + } + else + { + // Cannot append document to another tree + parent = clone; + created = true; + } + } + String method = outputProperties.getProperty(OutputKeys.METHOD); + int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML : + "text".equals(method) ? Stylesheet.OUTPUT_TEXT : + Stylesheet.OUTPUT_XML; + String encoding = outputProperties.getProperty(OutputKeys.ENCODING); + String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC); + String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM); + String version = outputProperties.getProperty(OutputKeys.VERSION); + boolean omitXmlDeclaration = + "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION)); + boolean standalone = + "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE)); + String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE); + String cdataSectionElements = + outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); + boolean indent = + "yes".equals(outputProperties.getProperty(OutputKeys.INDENT)); + if (created) + { + // Discover document element + DomDocument resultDoc = (DomDocument) parent; + Node root = resultDoc.getDocumentElement(); + // Add doctype if specified + if ((publicId != null || systemId != null) && + root != null) + { + // We must know the name of the root element to + // create the document type + resultDoc.appendChild(new DomDoctype(resultDoc, + root.getNodeName(), + publicId, + systemId)); + } + resultDoc.setBuilding(false); + resultDoc.setCheckWellformedness(true); + } + else if (publicId != null || systemId != null) + { + switch (parent.getNodeType()) + { + case Node.DOCUMENT_NODE: + case Node.DOCUMENT_FRAGMENT_NODE: + Document resultDoc = (parent instanceof Document) ? + (Document) parent : + parent.getOwnerDocument(); + DOMImplementation impl = resultDoc.getImplementation(); + DocumentType doctype = + impl.createDocumentType(resultDoc.getNodeName(), + publicId, + systemId); + // Try to insert doctype before first element + Node ctx = parent.getFirstChild(); + for (; ctx != null && + ctx.getNodeType() != Node.ELEMENT_NODE; + ctx = ctx.getNextSibling()) + { + } + if (ctx != null) + { + parent.insertBefore(doctype, ctx); + } + else + { + parent.appendChild(doctype); + } + } + } + if (version != null) + { + parent.setUserData("version", version, stylesheet); + } + if (omitXmlDeclaration) + { + parent.setUserData("omit-xml-declaration", "yes", stylesheet); + } + if (standalone) + { + parent.setUserData("standalone", "yes", stylesheet); + } + if (mediaType != null) + { + parent.setUserData("media-type", mediaType, stylesheet); + } + if (cdataSectionElements != null) + { + List list = new LinkedList(); + StringTokenizer st = new StringTokenizer(cdataSectionElements); + while (st.hasMoreTokens()) + { + String name = st.nextToken(); + String localName = name; + String uri = null; + String prefix = null; + int ci = name.indexOf(':'); + if (ci != -1) + { + // Use namespaces defined on xsl:output node to resolve + // namespaces for QName + prefix = name.substring(0, ci); + localName = name.substring(ci + 1); + uri = stylesheet.output.lookupNamespaceURI(prefix); + } + list.add(new QName(uri, localName, prefix)); + } + if (!list.isEmpty()) + { + Document resultDoc = (parent instanceof Document) ? + (Document) parent : + parent.getOwnerDocument(); + convertCdataSectionElements(resultDoc, parent, list); + } + } + if (indent) + { + parent.normalize(); + strip(parent); + Document resultDoc = (parent instanceof Document) ? + (Document) parent : + parent.getOwnerDocument(); + reindent(resultDoc, parent, 0); + } + // Render result to the target device + if (outputTarget instanceof DOMResult) + { + if (created) + { + DOMResult dr = (DOMResult) outputTarget; + dr.setNode(parent); + dr.setNextSibling(null); + } + } + else if (outputTarget instanceof StreamResult) + { + StreamResult sr = (StreamResult) outputTarget; + IOException ex = null; + try + { + writeStreamResult(parent, sr, outputMethod, encoding); + } + catch (UnsupportedEncodingException e) + { + try + { + writeStreamResult(parent, sr, outputMethod, "UTF-8"); + } + catch (IOException e2) + { + ex = e2; + } + } + catch (IOException e) + { + ex = e; + } + if (ex != null) + { + if (errorListener != null) + { + errorListener.error(new TransformerException(ex)); + } + else + { + ex.printStackTrace(System.err); + } + } + } + else if (outputTarget instanceof SAXResult) + { + SAXResult sr = (SAXResult) outputTarget; + try + { + ContentHandler ch = sr.getHandler(); + LexicalHandler lh = sr.getLexicalHandler(); + if (lh == null && ch instanceof LexicalHandler) + { + lh = (LexicalHandler) ch; + } + SAXSerializer serializer = new SAXSerializer(); + serializer.serialize(parent, ch, lh); + } + catch (SAXException e) + { + if (errorListener != null) + { + errorListener.error(new TransformerException(e)); + } + else + { + e.printStackTrace(System.err); + } + } + } + } + + /** + * Strip whitespace from the source tree. + */ + void strip(Node node) + throws TransformerConfigurationException + { + short nt = node.getNodeType(); + if (nt == Node.ENTITY_REFERENCE_NODE) + { + // Replace entity reference with its content + Node parent = node.getParentNode(); + Node child = node.getFirstChild(); + if (child != null) + { + strip(child); + } + while (child != null) + { + Node next = child.getNextSibling(); + node.removeChild(child); + parent.insertBefore(child, node); + child = next; + } + parent.removeChild(node); + } + if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE) + { + if (!stylesheet.isPreserved((Text) node)) + { + node.getParentNode().removeChild(node); + } + else + { + String text = node.getNodeValue(); + String stripped = text.trim(); + if (!text.equals(stripped)) + { + node.setNodeValue(stripped); + } + } + } + else + { + for (Node child = node.getFirstChild(); child != null; + child = child.getNextSibling()) + { + strip(child); + } + } + } + + /** + * Obtain a suitable output stream for writing the result to, + * and use the StreamSerializer to write the result tree to the stream. + */ + void writeStreamResult(Node node, StreamResult sr, int outputMethod, + String encoding) + throws IOException + { + OutputStream out = null; + try + { + out = sr.getOutputStream(); + if (out == null) + { + Writer writer = sr.getWriter(); + if (writer != null) + { + out = new WriterOutputStream(writer); + } + } + if (out == null) + { + String systemId = sr.getSystemId(); + try + { + URL url = new URL(systemId); + URLConnection connection = url.openConnection(); + connection.setDoOutput(true); + out = connection.getOutputStream(); + } + catch (MalformedURLException e) + { + out = new FileOutputStream(systemId); + } + catch (UnknownServiceException e) + { + URL url = new URL(systemId); + out = new FileOutputStream(url.getPath()); + } + } + out = new BufferedOutputStream(out); + StreamSerializer serializer = + new StreamSerializer(outputMethod, encoding, null); + if (stylesheet != null) + { + Collection celem = stylesheet.outputCdataSectionElements; + serializer.setCdataSectionElements(celem); + } + serializer.serialize(node, out); + out.flush(); + } + finally + { + try + { + if (out != null) + { + out.close(); + } + } + catch (IOException e) + { + } + } + } + + void copyChildren(Document dstDoc, Node src, Node dst) + { + Node srcChild = src.getFirstChild(); + while (srcChild != null) + { + Node dstChild = dstDoc.adoptNode(srcChild); + dst.appendChild(dstChild); + srcChild = srcChild.getNextSibling(); + } + } + + public void setParameter(String name, Object value) + { + if (stylesheet != null) + { + stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM); + } + } + + public Object getParameter(String name) + { + if (stylesheet != null) + { + return stylesheet.bindings.get(new QName(null, name), null, 1, 1); + } + return null; + } + + public void clearParameters() + { + if (stylesheet != null) + { + stylesheet.bindings.pop(Bindings.PARAM); + stylesheet.bindings.push(Bindings.PARAM); + } + } + + public void setURIResolver(URIResolver resolver) + { + uriResolver = resolver; + } + + public URIResolver getURIResolver() + { + return uriResolver; + } + + public void setOutputProperties(Properties oformat) + throws IllegalArgumentException + { + if (oformat == null) + { + outputProperties.clear(); + } + else + { + outputProperties.putAll(oformat); + } + } + + public Properties getOutputProperties() + { + return (Properties) outputProperties.clone(); + } + + public void setOutputProperty(String name, String value) + throws IllegalArgumentException + { + outputProperties.put(name, value); + } + + public String getOutputProperty(String name) + throws IllegalArgumentException + { + return outputProperties.getProperty(name); + } + + public void setErrorListener(ErrorListener listener) + { + errorListener = listener; + } + + public ErrorListener getErrorListener() + { + return errorListener; + } + + static final String INDENT_WHITESPACE = " "; + + /* + * Apply indent formatting to the given tree. + */ + void reindent(Document doc, Node node, int offset) + { + if (node.hasChildNodes()) + { + boolean markupContent = false; + boolean textContent = false; + List children = new LinkedList(); + Node ctx = node.getFirstChild(); + while (ctx != null) + { + switch (ctx.getNodeType()) + { + case Node.ELEMENT_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + case Node.DOCUMENT_TYPE_NODE: + markupContent = true; + break; + case Node.TEXT_NODE: + case Node.CDATA_SECTION_NODE: + case Node.ENTITY_REFERENCE_NODE: + case Node.COMMENT_NODE: + textContent = true; + break; + } + children.add(ctx); + ctx = ctx.getNextSibling(); + } + if (markupContent) + { + if (textContent) + { + // XXX handle mixed content differently? + } + int nodeType = node.getNodeType(); + if (nodeType == Node.DOCUMENT_NODE) + { + for (Iterator i = children.iterator(); i.hasNext(); ) + { + ctx = (Node) i.next(); + reindent(doc, ctx, offset + 1); + } + } + else + { + StringBuffer buf = new StringBuffer(); + buf.append('\n'); + for (int i = 0; i < offset + 1; i++) + { + buf.append(INDENT_WHITESPACE); + } + String ws = buf.toString(); + for (Iterator i = children.iterator(); i.hasNext(); ) + { + ctx = (Node) i.next(); + node.insertBefore(doc.createTextNode(ws), ctx); + reindent(doc, ctx, offset + 1); + } + buf = new StringBuffer(); + buf.append('\n'); + ws = buf.toString(); + for (int i = 0; i < offset; i++) + { + buf.append(INDENT_WHITESPACE); + } + node.appendChild(doc.createTextNode(ws)); + } + } + } + } + + /** + * Converts the text node children of any cdata-section-elements in the + * tree to CDATA section nodes. + */ + void convertCdataSectionElements(Document doc, Node node, List list) + { + if (node.getNodeType() == Node.ELEMENT_NODE) + { + boolean match = false; + for (Iterator i = list.iterator(); i.hasNext(); ) + { + QName qname = (QName) i.next(); + if (match(qname, node)) + { + match = true; + break; + } + } + if (match) + { + Node ctx = node.getFirstChild(); + while (ctx != null) + { + if (ctx.getNodeType() == Node.TEXT_NODE) + { + Node cdata = doc.createCDATASection(ctx.getNodeValue()); + node.replaceChild(cdata, ctx); + ctx = cdata; + } + ctx = ctx.getNextSibling(); + } + } + } + Node ctx = node.getFirstChild(); + while (ctx != null) + { + if (ctx.hasChildNodes()) + { + convertCdataSectionElements(doc, ctx, list); + } + ctx = ctx.getNextSibling(); + } + } + + boolean match(QName qname, Node node) + { + String ln1 = qname.getLocalPart(); + String ln2 = node.getLocalName(); + if (ln2 == null) + { + return ln1.equals(node.getNodeName()); + } + else + { + String uri1 = qname.getNamespaceURI(); + String uri2 = node.getNamespaceURI(); + return (uri1.equals(uri2) && ln1.equals(ln2)); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java b/libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java new file mode 100644 index 00000000000..cc8593c4601 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java @@ -0,0 +1,185 @@ +/* TransformerOutputProperties.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Properties; +import java.util.StringTokenizer; +import javax.xml.transform.OutputKeys; + +/** + * Helper class to manage JAXP user setting of output properties. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class TransformerOutputProperties + extends Properties +{ + + final Properties defaultProperties; + final Stylesheet stylesheet; + boolean dirty; + + TransformerOutputProperties(Stylesheet stylesheet) + { + this.stylesheet = stylesheet; + defaultProperties = new Properties(); + switch (stylesheet.outputMethod) + { + case Stylesheet.OUTPUT_XML: + defaultProperties.put(OutputKeys.METHOD, "xml"); + break; + case Stylesheet.OUTPUT_HTML: + defaultProperties.put(OutputKeys.METHOD, "html"); + break; + case Stylesheet.OUTPUT_TEXT: + defaultProperties.put(OutputKeys.METHOD, "text"); + break; + } + if (stylesheet.outputVersion != null) + { + defaultProperties.put(OutputKeys.VERSION, stylesheet.outputVersion); + } + if (stylesheet.outputEncoding != null) + { + defaultProperties.put(OutputKeys.ENCODING, stylesheet.outputEncoding); + } + defaultProperties.put(OutputKeys.OMIT_XML_DECLARATION, + stylesheet.outputOmitXmlDeclaration ? "yes" : "no"); + defaultProperties.put(OutputKeys.STANDALONE, + stylesheet.outputStandalone ? "yes" : "no"); + if (stylesheet.outputPublicId != null) + { + defaultProperties.put(OutputKeys.DOCTYPE_PUBLIC, + stylesheet.outputPublicId); + } + if (stylesheet.outputSystemId != null) + { + defaultProperties.put(OutputKeys.DOCTYPE_SYSTEM, + stylesheet.outputSystemId); + } + StringBuffer buf = new StringBuffer(); + for (Iterator i = stylesheet.outputCdataSectionElements.iterator(); + i.hasNext(); ) + { + if (buf.length() > 0) + { + buf.append(' '); + } + buf.append((String) i.next()); + } + defaultProperties.put(OutputKeys.CDATA_SECTION_ELEMENTS, buf.toString()); + defaultProperties.put(OutputKeys.INDENT, + stylesheet.outputIndent ? "yes" : "no"); + if (stylesheet.outputMediaType != null) + { + defaultProperties.put(OutputKeys.MEDIA_TYPE, + stylesheet.outputMediaType); + } + } + + public String getProperty(String key) + { + String val = super.getProperty(key); + if (val == null) + { + val = defaultProperties.getProperty(key); + } + return val; + } + + public Object put(Object key, Object value) + { + Object ret = super.put(key, value); + dirty = true; + return ret; + } + + public void clear() + { + super.clear(); + dirty = true; + } + + /** + * Applies the current set of properties to the underlying stylesheet. + */ + void apply() + { + if (!dirty) + { + return; + } + String method = getProperty(OutputKeys.METHOD); + if ("xml".equals(method)) + { + stylesheet.outputMethod = Stylesheet.OUTPUT_XML; + } + else if ("html".equals(method)) + { + stylesheet.outputMethod = Stylesheet.OUTPUT_HTML; + } + else if ("text".equals(method)) + { + stylesheet.outputMethod = Stylesheet.OUTPUT_TEXT; + } + stylesheet.outputVersion = getProperty(OutputKeys.VERSION); + stylesheet.outputEncoding = getProperty(OutputKeys.ENCODING); + stylesheet.outputOmitXmlDeclaration = + "yes".equals(getProperty(OutputKeys.OMIT_XML_DECLARATION)); + stylesheet.outputStandalone = + "yes".equals(getProperty(OutputKeys.STANDALONE)); + stylesheet.outputPublicId = getProperty(OutputKeys.DOCTYPE_PUBLIC); + stylesheet.outputSystemId = getProperty(OutputKeys.DOCTYPE_SYSTEM); + StringTokenizer st = + new StringTokenizer(getProperty(OutputKeys.CDATA_SECTION_ELEMENTS)); + Collection acc = new LinkedHashSet(); + while (st.hasMoreTokens()) + { + acc.add(st.nextToken()); + } + stylesheet.outputCdataSectionElements = acc; + stylesheet.outputIndent = "yes".equals(getProperty(OutputKeys.INDENT)); + stylesheet.outputMediaType = getProperty(OutputKeys.MEDIA_TYPE); + dirty = false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java b/libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java new file mode 100644 index 00000000000..762416fde36 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java @@ -0,0 +1,83 @@ +/* URIResolverEntityResolver.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.IOException; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.sax.SAXSource; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * EntityResolver that wraps a URIResolver. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class URIResolverEntityResolver + implements EntityResolver +{ + + final URIResolver resolver; + + URIResolverEntityResolver(URIResolver resolver) + { + this.resolver = resolver; + } + + public InputSource resolveEntity(String publicId, String systemId) + throws SAXException, IOException + { + try + { + Source source = resolver.resolve(null, systemId); + if (source == null) + { + return null; + } + return SAXSource.sourceToInputSource(source); + } + catch (TransformerException e) + { + throw new SAXException(e); + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java b/libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java new file mode 100644 index 00000000000..92002f1f5b6 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java @@ -0,0 +1,132 @@ +/* UnparsedEntityUriFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.DocumentType; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Notation; +import gnu.xml.xpath.Expr; +import gnu.xml.xpath.Function; + +/** + * The XSLT <code>unparsed-entity-uri()</code>function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class UnparsedEntityUriFunction + extends Expr + implements XPathFunction, Function +{ + + List args; + + public Object evaluate(List args) + throws XPathFunctionException + { + // Useless... + return Collections.EMPTY_SET; + } + + public void setArguments(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + int arity = args.size(); + List values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + String name = _string(context, values.get(0)); + DocumentType doctype = context.getOwnerDocument().getDoctype(); + if (doctype != null) + { + NamedNodeMap notations = doctype.getNotations(); + Notation notation = (Notation) notations.getNamedItem(name); + if (notation != null) + { + String systemId = notation.getSystemId(); + // XXX absolutize? + if (systemId != null) + { + return systemId; + } + } + } + return ""; + } + + public Expr clone(Object context) + { + UnparsedEntityUriFunction f = new UnparsedEntityUriFunction(); + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + f.setArguments(args2); + return f; + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/ValueOfNode.java b/libjava/classpath/gnu/xml/transform/ValueOfNode.java new file mode 100644 index 00000000000..430598a94b3 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/ValueOfNode.java @@ -0,0 +1,161 @@ +/* ValueOfNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collection; +import java.util.Iterator; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.Text; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSLT <code>value-of</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ValueOfNode + extends TemplateNode +{ + + final Expr select; + final boolean disableOutputEscaping; + + ValueOfNode(Expr select, boolean disableOutputEscaping) + { + this.select = select; + this.disableOutputEscaping = disableOutputEscaping; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new ValueOfNode(select.clone(stylesheet), + disableOutputEscaping); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Object ret = select.evaluate(context, pos, len); + /*if (stylesheet.debug) + { + System.err.println("value-of: " + select + " -> " + ret); + }*/ + String value; + if (ret instanceof Collection) + { + StringBuffer buf = new StringBuffer(); + for (Iterator i = ((Collection) ret).iterator(); i.hasNext(); ) + { + Node node = (Node) i.next(); + buf.append(Expr.stringValue(node)); + } + value = buf.toString(); + } + else + { + value = Expr._string(context, ret); + } + if (stylesheet.debug) + { + System.err.println("value-of: "+context+" "+ select + " -> "+ value); + } + if (value != null && value.length() > 0) + { + Document doc = (parent instanceof Document) ? + (Document) parent : parent.getOwnerDocument(); + Text textNode = doc.createTextNode(value); + if (disableOutputEscaping) + { + textNode.setUserData("disable-output-escaping", "yes", stylesheet); + } + if (nextSibling != null) + { + parent.insertBefore(textNode, nextSibling); + } + else + { + parent.appendChild(textNode); + } + } + // value-of doesn't process children + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + + public boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("select="); + buf.append(select); + if (disableOutputEscaping) + { + buf.append(",disableOutputEscaping"); + } + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/WhenNode.java b/libjava/classpath/gnu/xml/transform/WhenNode.java new file mode 100644 index 00000000000..231f2693b5b --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/WhenNode.java @@ -0,0 +1,123 @@ +/* WhenNode.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A template node representing an XSL <code>when</code> instruction. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class WhenNode + extends TemplateNode +{ + + final Expr test; + + WhenNode(Expr test) + { + this.test = test; + } + + TemplateNode clone(Stylesheet stylesheet) + { + TemplateNode ret = new WhenNode(test.clone(stylesheet)); + if (children != null) + { + ret.children = children.clone(stylesheet); + } + if (next != null) + { + ret.next = next.clone(stylesheet); + } + return ret; + } + + void doApply(Stylesheet stylesheet, QName mode, + Node context, int pos, int len, + Node parent, Node nextSibling) + throws TransformerException + { + Object ret = test.evaluate(context, pos, len); + boolean success = (ret instanceof Boolean) ? + ((Boolean) ret).booleanValue() : + Expr._boolean(context, ret); + if (success) + { + if (children != null) + { + children.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + else + { + if (next != null) + { + next.apply(stylesheet, mode, + context, pos, len, + parent, nextSibling); + } + } + } + + public boolean references(QName var) + { + if (test != null && test.references(var)) + { + return true; + } + return super.references(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(getClass().getName()); + buf.append('['); + buf.append("test="); + buf.append(test); + buf.append(']'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/transform/WithParam.java b/libjava/classpath/gnu/xml/transform/WithParam.java new file mode 100644 index 00000000000..0fb09d61eb9 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/WithParam.java @@ -0,0 +1,123 @@ +/* WithParam.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Collections; +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * A specification for setting a variable or parameter during template + * processing with <code>apply-templates</code> or + * <code>call-template</code>. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class WithParam +{ + + final QName name; + final Expr select; + final TemplateNode content; + + WithParam(QName name, Expr select) + { + this.name = name; + this.select = select; + content = null; + } + + WithParam(QName name, TemplateNode content) + { + this.name = name; + this.content = content; + select = null; + } + + Object getValue(Stylesheet stylesheet, QName mode, + Node context, int pos, int len) + throws TransformerException + { + if (select != null) + { + return select.evaluate(context, pos, len); + } + else + { + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + DocumentFragment fragment = doc.createDocumentFragment(); + content.apply(stylesheet, mode, + context, pos, len, + fragment, null); + return Collections.singleton(fragment); + } + } + + WithParam clone(Stylesheet stylesheet) + { + if (content == null) + { + return new WithParam(name, + select.clone(stylesheet)); + } + else + { + return new WithParam(name, + content.clone(stylesheet)); + } + } + + boolean references(QName var) + { + if (select != null && select.references(var)) + { + return true; + } + if (content != null && content.references(var)) + { + return true; + } + return false; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/XSLComparator.java b/libjava/classpath/gnu/xml/transform/XSLComparator.java new file mode 100644 index 00000000000..222f370c8b9 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/XSLComparator.java @@ -0,0 +1,124 @@ +/* XSLComparator.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.text.Collator; +import org.w3c.dom.Node; +import gnu.xml.xpath.Expr; + +/** + * Comparator for sorting lists of nodes according to a list of sort keys. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class XSLComparator + implements Comparator +{ + + final List sortKeys; + + XSLComparator(List sortKeys) + { + this.sortKeys = sortKeys; + } + + public int compare(Object o1, Object o2) + { + if (o1 instanceof Node && o2 instanceof Node) + { + Node n1 = (Node) o1; + Node n2 = (Node) o2; + for (Iterator i = sortKeys.iterator(); i.hasNext(); ) + { + SortKey sortKey = (SortKey) i.next(); + String k1 = sortKey.key(n1); + String k2 = sortKey.key(n2); + if ("text".equals(sortKey.dataType)) + { + Locale locale = (sortKey.lang == null) ? Locale.getDefault() : + new Locale(sortKey.lang); + Collator collator = Collator.getInstance(locale); + int d = collator.compare(k1, k2); + if (d != 0) + { + switch (sortKey.caseOrder) + { + case SortKey.UPPER_FIRST: + // TODO + break; + case SortKey.LOWER_FIRST: + // TODO + break; + } + if (sortKey.descending) + { + d = -d; + } + return d; + } + } + else if ("number".equals(sortKey.dataType)) + { + double kn1 = Expr._number(n1, k1); + double kn2 = Expr._number(n2, k2); + int d; + if (Double.isNaN(kn1) || Double.isInfinite(kn2)) + { + d = -1; + } + else if (Double.isNaN(kn2) || Double.isInfinite(kn1)) + { + d = 1; + } + else + { + // conversion to int may give 0 for small numbers + d = (kn1 > kn2) ? 1 : (kn1 < kn2) ? -1 : 0; + } + return (sortKey.descending) ? -d : d; + } + } + } + return 0; + } + +} diff --git a/libjava/classpath/gnu/xml/transform/XSLURIResolver.java b/libjava/classpath/gnu/xml/transform/XSLURIResolver.java new file mode 100644 index 00000000000..6a49caab458 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/XSLURIResolver.java @@ -0,0 +1,282 @@ +/* XSLURIResolver.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.io.Reader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.HashMap; +import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.ErrorListener; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import gnu.xml.dom.ls.ReaderInputStream; + +/** + * URI resolver for XSLT. + * This resolver parses external entities into DOMSources. It + * maintains a cache of URIs to DOMSources to avoid expensive re-parsing. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class XSLURIResolver + implements URIResolver +{ + + Map lastModifiedCache = new HashMap(); + Map nodeCache = new HashMap(); + DocumentBuilder builder; + URIResolver userResolver; + ErrorListener userListener; + + void setUserResolver(URIResolver userResolver) + { + this.userResolver = userResolver; + } + + void setUserListener(ErrorListener userListener) + { + this.userListener = userListener; + } + + /** + * Clear the cache. + */ + void flush() + { + lastModifiedCache.clear(); + nodeCache.clear(); + } + + public Source resolve(String href, String base) + throws TransformerException + { + Source source = null; + if (userResolver != null) + { + source = userResolver.resolve(base, href); + } + return resolveDOM(source, href, base); + } + + DOMSource resolveDOM(Source source, String base, String href) + throws TransformerException + { + if (source != null && source instanceof DOMSource) + { + return (DOMSource) source; + } + String systemId = (source == null) ? null : source.getSystemId(); + long lastModified = 0L, lastLastModified = 0L; + + try + { + URL url = resolveURL(systemId, base, href); + Node node = null; + InputStream in = null; + if (source instanceof StreamSource) + { + StreamSource ss = (StreamSource) source; + in = ss.getInputStream(); + if (in == null) + { + Reader reader = ss.getReader(); + if (reader != null) + { + in = new ReaderInputStream(reader); + } + } + } + if (in == null) + { + if (url != null) + { + systemId = url.toString(); + node = (Node) nodeCache.get(systemId); + // Is the resource up to date? + URLConnection conn = url.openConnection(); + Long llm = (Long) lastModifiedCache.get(systemId); + if (llm != null) + { + lastLastModified = llm.longValue(); + conn.setIfModifiedSince(lastLastModified); + } + conn.connect(); + lastModified = conn.getLastModified(); + if (node != null && + lastModified > 0L && + lastModified <= lastLastModified) + { + // Resource unchanged + return new DOMSource(node, systemId); + } + else + { + // Resource new or modified + in = conn.getInputStream(); + nodeCache.put(systemId, node); + lastModifiedCache.put(systemId, new Long(lastModified)); + } + } + else + { + throw new TransformerException("can't resolve URL: " + + systemId); + } + } + InputSource input = new InputSource(in); + input.setSystemId(systemId); + DocumentBuilder builder = getDocumentBuilder(); + node = builder.parse(input); + return new DOMSource(node, systemId); + } + catch (IOException e) + { + throw new TransformerException(e); + } + catch (SAXException e) + { + throw new TransformerException(e); + } + } + + URL resolveURL(String systemId, String base, String href) + throws IOException + { + URL url = null; + try + { + if (systemId != null) + { + try + { + url = new URL(systemId); + } + catch (MalformedURLException e) + { + // Try building from base + href + } + } + if (url == null) + { + if (base != null) + { + URL baseURL = new URL(base); + url = new URL(baseURL, href); + } + else if (href != null) + { + url = new URL(href); + } + else + { + // See below + throw new MalformedURLException(systemId); + } + } + return url; + } + catch (MalformedURLException e) + { + // Fall back to local filesystem + File file = null; + if (href == null) + { + href = systemId; + } + if (base != null) + { + int lsi = base.lastIndexOf(File.separatorChar); + if (lsi != -1 && lsi < base.length() - 1) + { + base = base.substring(0, lsi); + } + File baseFile = new File(base); + file = new File(baseFile, href); + } + else if (href != null) + { + file = new File(href); + } + return (file == null) ? null : file.toURL(); + } + } + + DocumentBuilder getDocumentBuilder() + throws TransformerException + { + try + { + if (builder == null) + { + DocumentBuilderFactory factory = + DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setExpandEntityReferences(true); + builder = factory.newDocumentBuilder(); + } + if (userResolver != null) + { + builder.setEntityResolver(new URIResolverEntityResolver(userResolver)); + } + if (userListener != null) + { + builder.setErrorHandler(new ErrorListenerErrorHandler(userListener)); + } + return builder; + } + catch (Exception e) + { + throw new TransformerException(e); + } + } + +} + diff --git a/libjava/classpath/gnu/xml/transform/package.html b/libjava/classpath/gnu/xml/transform/package.html new file mode 100644 index 00000000000..d4355966c59 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/package.html @@ -0,0 +1,77 @@ +<html> +<body> + +<h1>GNU JAXP XSL transformer</h1> + +<div> +This package contains a Java XSL transformer compliant with the JAXP +specification. It depends on the GNU DOM and XPath implementations, and +will generate GNU DOM nodes unless a specific target from another +implementation was given. It understands DOM, SAX, and stream sources +and result sinks and supports these JAXP features. +</div> + +<div> +To use this transformer, set the system property +<code>javax.xml.transform.TransformerFactory</code> to the value +<code>gnu.xml.transform.TransformerFactoryImpl</code>. You can then +instantiate <a href='TransformerFactory.html'>TransformerFactory</a> +and transformers in the ordinary manner. Reuse of stylesheets is +supported using the JAXP <a href='Templates.html'>Templates</a> +mechanism. +</div> + +<h3>Architecture</h3> + +<div> +When given a stylesheet source, this implementation compiles it internally +into a Stylesheet object, which is a container for templates and state. +Each stylesheet instruction is represented by a subclass of TemplateNode, +which is arranged in a directed graph: each TemplateNode has a reference +to its first child and the next node. +</div> + +<div> +The transformation process consists of identifying the Template that matches +the root of the source context, and calling <code>apply</code> on its +corresponding TemplateNode. This in turn processes its children and next +TemplateNode, depending on the semantics of each node type. +</div> + +<div> +Template nodes may reference XPath expressions or patterns. These are fully +compiled to objects of type <a href='../xpath/Expr.html'>Expr</a> at the +time the stylesheet is compiled. +</div> + +<h3>Conformance</h3> + +<div> +This implementation is feature complete, but the XSLT specification is +large and there are still many bugs that need to be ironed out. It has +been tested against the OASIS XSLT TC test suite, comprising unit tests +from the Xalan project and Microsoft. Conformance to these unit tests +is approximately 70% at the current time, although normal usage of the +transformer should involve relatively few surprises (the test suite is +designed to test very complex and obscure functionality). +</div> + +<h3>Known bugs</h3> + +<ul> +<li>When reusing stylesheets using the JAXP Templates mechanism, XSL +<code>apply-imports</code> instructions will not work.</li> +<li>XPath filter expressions do not always work as expected (this is a +problem with the GNU XPath implementation rather than the transformer). +This can result in problems with the <code>position()</code> function, +as well as <code>select</code> expressions and numbering.</li> +</ul> + +<div> +Obviously we'd like to improve conformance and fix these bugs. If you're +interested in working on any of these issues please +<a href='mailto:classpathx-xml@gnu.org'>contact us</a>. +</div> + +</body> +</html> diff --git a/libjava/classpath/gnu/xml/util/DoParse.java b/libjava/classpath/gnu/xml/util/DoParse.java new file mode 100644 index 00000000000..5da086ed8f9 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/DoParse.java @@ -0,0 +1,300 @@ +/* DoParse.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.util; + +import java.io.IOException; + +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +import gnu.xml.pipeline.EventConsumer; +import gnu.xml.pipeline.EventFilter; +import gnu.xml.pipeline.NSFilter; +import gnu.xml.pipeline.PipelineFactory; +import gnu.xml.pipeline.TeeConsumer; +import gnu.xml.pipeline.ValidationConsumer; +import gnu.xml.pipeline.WellFormednessFilter; + +/** + * This class provides a driver which may be invoked from the command line + * to process a document using a SAX2 parser and a specified XML processing + * pipeline. + * This facilitates some common types of command line tools, such as parsing an + * XML document in order test it for well formedness or validity. + * + * <p>The SAX2 XMLReaderFactory should return a SAX2 XML parser which + * supports both of the standardized extension handlers (for declaration + * and lexical events). That parser will be used to produce events. + * + * <p>The first parameter to the command gives the name of the document that + * will be given to that processor. If it is a file name, it is converted + * to a URL first. + * + * <p>The second parameter describes a simple processing pipeline, and will + * be used as input to {@link gnu.xml.pipeline.PipelineFactory} + * methods which identify the processing to be done. Examples of such a + * pipeline include <pre> + * + * nsfix | validate <em>to validate the input document </em> + * nsfix | write ( stdout ) <em>to echo the file as XML text</em> + * dom | nsfix | write ( stdout ) <em>parse into DOM, print the result</em> + * </pre> + * + * <p> Relatively complex pipelines can be described on the command line, but + * not all interesting ones will require as little configuration as can be done + * in that way. Put filters like "nsfix", perhaps followed by "validate", + * at the front of the pipeline so they can be optimized out if a parser + * supports those modes natively. + * + * <p> If the parsing is aborted for any reason, the JVM will exit with a + * failure code. If a validating parse was done then both validation and + * well formedness errors will cause a failure. A non-validating parse + * will report failure on well formedness errors. + * + * @see gnu.xml.pipeline.PipelineFactory + * + * @author David Brownell + */ +final public class DoParse +{ + private DoParse () { /* no instances allowed */ } + + // first reported nonrecoverable error + private static SAXParseException fatal; + + // error categories + private static int errorCount; + private static int fatalCount; + + /** + * Command line invoker for this class; pass a filename or URL + * as the first argument, and a pipeline description as the second. + * Make sure to use filters to condition the input to stages that + * require it; an <em>nsfix</em> filter will be a common requirement, + * to restore syntax that SAX2 parsers delete by default. Some + * conditioning filters may be eliminated by setting parser options. + * (For example, "nsfix" can set the "namespace-prefixes" feature to + * a non-default value of "true". In the same way, "validate" can set + * the "validation" feature to "true".) + */ + public static void main (String argv []) + throws IOException + { + int exitStatus = 1; + + if (argv.length != 2) { + System.err.println ("Usage: DoParse [filename|URL] pipeline-spec"); + System.err.println ("Example pipeline specs:"); + System.err.println (" 'nsfix | validate'"); + System.err.println ( + " ... restore namespace syntax, validate"); + System.err.println (" 'nsfix | write ( stdout )'"); + System.err.println ( + " ... restore namespace syntax, write to stdout as XML" + ); + System.exit (1); + } + + try { + // + // Get input source for specified document (or try ;-) + // + argv [0] = Resolver.getURL (argv [0]); + InputSource input = new InputSource (argv [0]); + + // + // Get the producer, using the system default parser (which + // can be overridden for this particular invocation). + // + // And the pipeline, using commandline options. + // + XMLReader producer; + EventConsumer consumer; + + producer = XMLReaderFactory.createXMLReader (); + + // + // XXX pipeline factory now has a pre-tokenized input + // method, use it ... that way at least some params + // can be written using quotes (have spaces, ...) + // + consumer = PipelineFactory.createPipeline (argv [1]); + + // + // XXX want commandline option for tweaking error handler. + // Want to be able to present warnings. + // + producer.setErrorHandler (new MyErrorHandler ()); + + // XXX need facility enabling resolving to local DTDs + + // + // Parse. The pipeline may get optimized a bit, so we + // can't always fail cleanly for validation without taking + // a look at the filter stages. + // + EventFilter.bind (producer, consumer); + producer.parse (input); + + try { + if (producer.getFeature ( + "http://org.xml/sax/features/validation")) + exitStatus = ((errorCount + fatalCount) > 0) ? 1 : 0; + else if (fatalCount == 0) + exitStatus = 0; + } catch (SAXException e) { + if (hasValidator (consumer)) + exitStatus = ((errorCount + fatalCount) > 0) ? 1 : 0; + else if (fatalCount == 0) + exitStatus = 0; + } + + } catch (java.net.MalformedURLException e) { + System.err.println ("** Malformed URL: " + e.getMessage ()); + System.err.println ("Is '" + argv [0] + "' a non-existent file?"); + e.printStackTrace (); + // e.g. FNF + + } catch (SAXParseException e) { + if (e != fatal) { + System.err.print (printParseException ("Parsing Aborted", e)); + e.printStackTrace (); + if (e.getException () != null) { + System.err.println ("++ Wrapped exception:"); + e.getException ().printStackTrace (); + } + } + + } catch (SAXException e) { + Exception x = e; + if (e.getException () != null) + x = e.getException (); + x.printStackTrace (); + + } catch (Throwable t) { + t.printStackTrace (); + } + + System.exit (exitStatus); + } + + // returns true if saw a validator (before end or unrecognized node) + // false otherwise + private static boolean hasValidator (EventConsumer e) + { + if (e == null) + return false; + if (e instanceof ValidationConsumer) + return true; + if (e instanceof TeeConsumer) { + TeeConsumer t = (TeeConsumer) e; + return hasValidator (t.getFirst ()) + || hasValidator (t.getRest ()); + } + if (e instanceof WellFormednessFilter + || e instanceof NSFilter + ) + return hasValidator (((EventFilter)e).getNext ()); + + // else ... gee, we can't know. Assume not. + + return false; + } + + static class MyErrorHandler implements ErrorHandler + { + // dump validation errors, but continue + public void error (SAXParseException e) + throws SAXParseException + { + errorCount++; + System.err.print (printParseException ("Error", e)); + } + + public void warning (SAXParseException e) + throws SAXParseException + { + // System.err.print (printParseException ("Warning", e)); + } + + // try to continue fatal errors, in case a parser reports more + public void fatalError (SAXParseException e) + throws SAXParseException + { + fatalCount++; + if (fatal == null) + fatal = e; + System.err.print (printParseException ("Nonrecoverable Error", e)); + } + } + + static private String printParseException ( + String label, + SAXParseException e + ) { + StringBuffer buf = new StringBuffer (); + int temp; + + buf.append ("** "); + buf.append (label); + buf.append (": "); + buf.append (e.getMessage ()); + buf.append ('\n'); + if (e.getSystemId () != null) { + buf.append (" URI: "); + buf.append (e.getSystemId ()); + buf.append ('\n'); + } + if ((temp = e.getLineNumber ()) != -1) { + buf.append (" line: "); + buf.append (temp); + buf.append ('\n'); + } + if ((temp = e.getColumnNumber ()) != -1) { + buf.append (" char: "); + buf.append (temp); + buf.append ('\n'); + } + + return buf.toString (); + } +} diff --git a/libjava/classpath/gnu/xml/util/DomParser.java b/libjava/classpath/gnu/xml/util/DomParser.java new file mode 100644 index 00000000000..b28b6103b70 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/DomParser.java @@ -0,0 +1,804 @@ +/* DomParser.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.util; + +import java.util.Enumeration; +import java.util.Locale; + +import org.xml.sax.*; +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.NamespaceSupport; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.DefaultHandler2; +import org.xml.sax.ext.LexicalHandler; + +import org.w3c.dom.*; + + +/** + * This parser emits SAX2 parsing events as it traverses a DOM tree, using + * any conformant implementation of DOM. It exposes all SAX1 features, + * and the following SAX2 features and properties (as + * identified by standard URIs which are not fully provided here). Note + * that if a Level 1 DOM implementation is given, then this behaves as if + * namespaces were disabled, and namespace prefixes were enabled. </p> + * + * <table border="1" width='100%' cellpadding='3' cellspacing='0'> + * <tr bgcolor='#ccccff'> + * <th><font size='+1'>Name</font></th> + * <th><font size='+1'>Notes</font></th></tr> + * + * <tr><td colspan=2><center><em>Features ... URL prefix is + * <b>http://xml.org/sax/features/</b></em></center></td></tr> + * + * <tr><td>(URL)/external-general-entities</td> + * <td>false (does no parsing)</td></tr> + * <tr><td>(URL)/external-parameter-entities</td> + * <td>false (does no parsing)</td></tr> + * <tr><td>(URL)/namespaces</td> + * <td>Value is fixed at <em>true</em></td></tr> + * <tr><td>(URL)/namespace-prefixes</td> + * <td>Value is settable, defaulting to <em>false</em> + * (<code>xmlns</code> attributes hidden, and names aren't prefixed) + * </td></tr> + * <tr><td>(URL)/string-interning</td> + * <td>Value is fixed at <em>false</em> (DOM provides no + * guarantees as to interning)</td></tr> + * <tr><td>(URL)/validation</td> + * <td>false (does no parsing)</td></tr> + * <tr><td>(URL)/lexical-handler/parameter-entities</td> + * <td>false (DOM doesn't do parameter entities)</td></tr> + * + * <tr><td colspan=2><center><em>Properties ... URL prefix is + * <b>http://xml.org/sax/properties/</b></em></center></td></tr> + * + * + * <tr><td>(URL)/dom-node</td> + * <td>This property may be set before parsing to hold a DOM + * <em>Document</em> node; any arguments given to <em>parse</em> + * methods are ignored. When retrieved + * during a parse, this value contains the "current" DOM node. + * </td></tr> + * <tr><td>(URL)/declaration-handler</td> + * <td>A declaration handler may be provided. Declaration of external + * general entities is exposed, but not parameter entities; none of the + * entity names reported here will begin with "%". </td></tr> + * <tr><td>(URL)/lexical-handler</td> + * <td>A lexical handler may be provided. While the start and end of + * any external subset are reported, expansion of other parameter + * entities (e.g. inside attribute list declarations) is not exposed. + * Expansion of general entities within attributes is also not exposed + * (see below).</td></tr> + * </table> + * + * <P> The consequences of modifying a DOM document tree as it is being walked + * by this "parser" are unspecified; don't do it! </P> + * + * @author David Brownell + */ +final public class DomParser implements XMLReader +{ + // Stuff used internally to route events correctly + private DefaultHandler2 defaultHandler = new DefaultHandler2 (); + + // per-parse SAX stuff + private ContentHandler contentHandler = defaultHandler; + private DTDHandler dtdHandler = defaultHandler; + private DeclHandler declHandler = defaultHandler; + private LexicalHandler lexicalHandler = defaultHandler; + + // shared context + private ErrorHandler errHandler = defaultHandler; + private EntityResolver resolver = defaultHandler; + private Locale locale = Locale.getDefault (); + + // parser state + private Node start; + private Node current; + private boolean isL2; + private boolean showNamespaces = true; + private boolean showXML1_0 = false; + private NamespaceSupport prefixStack = new NamespaceSupport (); + private boolean isDocument; + + + /** + * Constructs an unitialized <b>SAX2</b> parser. + */ + public DomParser () { + } + + /** + * Constructs an <b>SAX2</b> parser initialized to traverse the specified + * DOM tree. If the node is a document, the startDocument() and + * endDocument() calls bracket the calls exposing children. + */ + public DomParser (Node node) { + setStart (node); + } + + + // stuff that most components in an application should be sharing: + // resolver and error locale. + + /** + * <b>SAX2</b>: Returns the object used when resolving external + * entities during parsing (both general and parameter entities). + */ + public EntityResolver getEntityResolver () + { + return resolver; + } + + /** + * <b>SAX1</b>: Provides an object which may be used when resolving external + * entities during parsing (both general and parameter entities). + */ + public void setEntityResolver (EntityResolver resolver) + { + if (resolver == null) + resolver = defaultHandler; + this.resolver = resolver; + } + + /** + * <b>SAX1</b>: Identifies the locale which the parser should use for the + * diagnostics it provides. + * + * @exception SAXException as defined in the specification for + * <em>org.xml.sax.Parser.setLocale()</em> + */ + public void setLocale (Locale locale) + throws SAXException + { + if (locale == null) + locale = Locale.getDefault (); + this.locale = locale; + } + + + // different modules will tend to handle error handling the same, + // but it may not be the same through the whole app + + /** + * <b>SAX2</b>: Returns the object used to receive callbacks for XML + * errors of all levels (fatal, nonfatal, warning). + */ + public ErrorHandler getErrorHandler () + { + return errHandler; + } + + /** + * <b>SAX1</b>: Provides an object which receives callbacks for XML errors + * of all levels (fatal, nonfatal, warning). + */ + public void setErrorHandler (ErrorHandler handler) + { + if (handler == null) + handler = defaultHandler; + errHandler = handler; + } + + + // stuff different parts of a module will handle differently + + /** + * <b>SAX2</b>: Returns the object used to report the logical + * content of an XML document. + */ + public ContentHandler getContentHandler () + { + return contentHandler; + } + + /** + * <b>SAX2</b>: Assigns the object used to report the logical + * content of an XML document. + */ + public void setContentHandler (ContentHandler handler) + { + if (handler == null) + handler = defaultHandler; + contentHandler = handler; + } + + /** + * <b>SAX2</b>: Returns the object used to process declarations related + * to notations and unparsed entities. + */ + public DTDHandler getDTDHandler () + { + return dtdHandler; + } + + /** + * <b>SAX1</b>: Provides an object which may be used to intercept + * declarations related to notations and unparsed entities. + */ + public void setDTDHandler (DTDHandler handler) + { + if (handler == null) + handler = defaultHandler; + dtdHandler = handler; + } + + + /** + * <b>SAX1</b>: Parses the previously provided DOM document (the + * input parameter is ignored). When this returns, that same + * document may be parsed again without needing a "reset". + * + * @param uri ignored (pass an empty string) + * @exception SAXException as defined in the specification for + * <em>org.xml.sax.Parser.parse()</em> + */ + public void parse (String uri) throws SAXException + { + parse (); + } + + /** + * <b>SAX1</b>: Parses the previously provided DOM document (the + * input parameter is ignored). When this returns, that same + * document may be parsed again without needing a "reset". + * + * @param input ignored + * @exception SAXException as defined in the specification for + * <em>org.xml.sax.Parser.parse()</em> + */ + public void parse (InputSource input) throws SAXException + { + parse (); + } + + private void parse () throws SAXException + { + try { + walk (); + } finally { + if (isDocument) + contentHandler.endDocument (); + current = null; + prefixStack.reset (); + } + } + + private boolean getIsL2 (Node node) + { + DOMImplementation impl; + Document doc; + + if (node instanceof Document) + doc = (Document) node; + else + doc = node.getOwnerDocument (); + if (doc == null) + throw new RuntimeException ("? unowned node - L2 DTD ?"); + impl = doc.getImplementation (); + return impl.hasFeature ("XML", "2.0"); + } + + + private static final String FEATURES = "http://xml.org/sax/features/"; + private static final String HANDLERS = "http://xml.org/sax/properties/"; + + /** + * <b>SAX2</b>: Tells whether this parser supports the specified feature. + */ + public boolean getFeature (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + // basically, none are relevant -- they relate more to + // parsing than to walking a "parse tree". + + // FIXME: DOM feature to expose interning? + + if ((FEATURES + "validation").equals (name) + || (FEATURES + "external-general-entities") + .equals (name) + || (FEATURES + "external-parameter-entities") + .equals (name) + || (FEATURES + "string-interning").equals (name) + ) + return false; + + if ((FEATURES + "namespaces").equals (name)) + return showNamespaces; + if ((FEATURES + "namespace-prefixes").equals (name)) + return showXML1_0; + + throw new SAXNotRecognizedException (name); + } + + /** + * <b>SAX2</b>: Returns the specified property. At this time only + * the declaration and lexical handlers, and current the "DOM" node, + * are supported. + */ + public Object getProperty (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + if ((HANDLERS + "declaration-handler").equals (name)) + return declHandler == defaultHandler ? null : declHandler; + if ((HANDLERS + "lexical-handler").equals (name)) + return lexicalHandler == defaultHandler ? null : lexicalHandler; + + if ((HANDLERS + "dom-node").equals (name)) + return current; + + // unknown properties + throw new SAXNotRecognizedException (name); + } + + /** + * <b>SAX2</b>: Sets the state of features supported in this parser. + * Only the namespace support features are mutable. + */ + public void setFeature (String name, boolean state) + throws SAXNotRecognizedException, SAXNotSupportedException + { + if (current != null) + throw new IllegalStateException ("feature change midparse"); + + boolean value = getFeature (name); + + if (value == state) + return; + + if ((FEATURES + "namespaces").equals (name)) { + if (!showXML1_0 && state == false) + throw new SAXNotSupportedException ("Illegal namespace " + + "processing configuration"); + showNamespaces = state; + return; + } + if ((FEATURES + "namespace-prefixes").equals (name)) { + if (!showNamespaces && state == false) + throw new SAXNotSupportedException ("Illegal namespace " + + "processing configuration"); + showXML1_0 = state; + return; + } + + throw new SAXNotSupportedException (name); + } + + /** + * <b>SAX2</b>: Assigns the specified property. At this time only + * declaration and lexical handlers, and the initial DOM document, are + * supported. These must not be changed to values of the wrong type. + * Like SAX1 handlers, these handlers may be changed at any time. + * Like SAX1 input source or document URI, the initial DOM document + * may not be changed during a parse. + */ + public void setProperty (String name, Object state) + throws SAXNotRecognizedException, SAXNotSupportedException + { + if ((HANDLERS + "declaration-handler").equals (name)) { + if (!(state instanceof DeclHandler || state == null)) + throw new SAXNotSupportedException (name); + declHandler = (DeclHandler) state; + return; + } + + if ((HANDLERS + "lexical-handler").equals (name)) { + if (!(state instanceof LexicalHandler || state == null)) + throw new SAXNotSupportedException (name); + lexicalHandler = (LexicalHandler) state; + return; + } + + if ((HANDLERS + "dom-node").equals (name)) { + if (state == null || state instanceof Node) { + if (current != null) + throw new SAXNotSupportedException ( + "property is readonly during parse: " + name); + setStart ((Node) state); + return; + } + throw new SAXNotSupportedException ("not a DOM Node"); + } + + // unknown properties + throw new SAXNotRecognizedException (name); + } + + private void setStart (Node property) + { + start = property; + if (start != null) { + isL2 = getIsL2 (start); + isDocument = (start instanceof Document); + } + } + + // + // Non-recursive walk, using DOM state when backtracking is needed + // + private void walk () + throws SAXException + { + int type; + NamedNodeMap nodes; + int length; + AttributesImpl attrs = new AttributesImpl (); + char chars []; + String ns, local; + + synchronized (this) { + if (current != null) + throw new IllegalStateException ("already walking tree"); + + // JVM guarantees assignments are atomic; so no other + // thread could get this far till this walk's done. + current = start; + } + + for (;;) { + type = current.getNodeType (); + + // + // First, visit the current node, including any "start" calls + // + switch (type) { + + case Node.DOCUMENT_NODE: + contentHandler.startDocument (); + break; + + case Node.ELEMENT_NODE: + nodes = current.getAttributes (); + length = nodes.getLength (); + prefixStack.pushContext (); + for (int i = 0; i < length; i++) { + Attr attr = (Attr) nodes.item (i); + String name = attr.getNodeName (); + + if (showNamespaces && name.startsWith ("xmlns")) { + String prefix; + String uri; + + // NOTE: DOM L2 (CR2+ and REC) violate the + // Namespaces REC, treat "xmlns" like a strange + // attribute instead of a magic token + if ("xmlns".equals (name)) + prefix = ""; + else + prefix = name.substring (6); + uri = attr.getNodeValue (); + + prefixStack.declarePrefix (prefix, uri); + contentHandler.startPrefixMapping (prefix, uri); + + if (!showXML1_0) + continue; + } + + // + // NOTE: DOM doesn't record the attribute type info + // which SAX exposes; so this always reports CDATA. + // + // NOTE: SAX doesn't expose the isSpecified info which + // DOM exposes; that's discarded here. Similarly with + // the information DOM hides inside itself about what + // the default values for an attribute are. + // + if (showNamespaces) { + if (isL2) { + if ((ns = attr.getNamespaceURI ()) == null) + ns = ""; + // Note: SAX2 and DOM handle "local" names + // differently + if ((local = attr.getLocalName ()) == null) + local = name; + } else { +// XXX + throw new RuntimeException ( + "NYI, ns lookup when parsing L1 DOM"); + } + } else + ns = local = ""; + attrs.addAttribute (ns, local, name, + "CDATA", attr.getNodeValue ()); + } + if (showNamespaces) { + if (isL2) { + if ((ns = current.getNamespaceURI ()) == null) + ns = ""; + // Note: SAX2 and DOM handle "local" names differently + if ((local = current.getLocalName ()) == null) + local = current.getNodeName (); + } else { +// XXX + throw new RuntimeException ( + "NYI, ns lookup when parsing L1 DOM"); + } + } else + ns = local = ""; + contentHandler.startElement (ns, local, + current.getNodeName (), attrs); + if (length != 0) + attrs.clear (); + break; + + case Node.CDATA_SECTION_NODE: + lexicalHandler.startCDATA (); + chars = current.getNodeValue ().toCharArray (); + contentHandler.characters (chars, 0, chars.length); + lexicalHandler.endCDATA (); + break; + + case Node.COMMENT_NODE: + chars = current.getNodeValue ().toCharArray (); + lexicalHandler.comment (chars, 0, chars.length); + break; + + case Node.DOCUMENT_TYPE_NODE: + { + DocumentType doctype = (DocumentType) current; + + // + // Only DOM L2 supports recreating even some DTDs in full. + // + if (isL2) { + lexicalHandler.startDTD (doctype.getName (), + doctype.getPublicId (), + doctype.getSystemId ()); + } else + lexicalHandler.startDTD (doctype.getName (), + null, null); + + // + // The only sure way to recreate is to provide both the + // internal and external subsets. Otherwise, only part + // of the job can be done ... because from the DTD, DOM + // discards both the critical data, like the attribute and + // element declarations, as well as the PIs and comments + // that are used to hold their documentation. + // + // Even the entity and notation declarations that it can + // expose can't be recorded without proprietary extensions. + // + // We construct a comment to tell what we know about how + // (in)complete this particular really DTD is. + // + { + String message; + char buf []; + + // + // Though DOM L2 lets the whole doctype be recreated, + // SAX2 can't represent it (input or output). + // So this will be the typical case. + // + if (isL2 && doctype.getInternalSubset () != null) + message = + " Full DTD known; can't be shown using SAX2. "; + + // + // Otherwise, we'll concoct a partial DTD. If there's + // any more data here at all, it was provided using a + // (proprietary) extension to DOM. + // + else + message = + " This DTD was was recreated using incomplete DOM L2 records. "; + + buf = message.toCharArray (); + lexicalHandler.comment (buf, 0, buf.length); + } + + // report notations first + nodes = doctype.getNotations (); + length = nodes.getLength (); + for (int i = 0; i < length; i++) { + Notation notation = (Notation) nodes.item (i); + dtdHandler.notationDecl ( + notation.getNodeName (), + notation.getPublicId (), + notation.getSystemId ()); + } + + // then parsed and unparsed external general entities + nodes = doctype.getEntities (); + length = nodes.getLength (); + for (int i = 0; i < length; i++) { + Entity entity = (Entity) nodes.item (i); + String notation = entity.getNotationName (); + + if (notation != null) + dtdHandler.unparsedEntityDecl ( + entity.getNodeName (), + entity.getPublicId (), + entity.getSystemId (), + notation); + else if (entity.getSystemId () != null) + declHandler.externalEntityDecl ( + entity.getNodeName (), + entity.getPublicId (), + entity.getSystemId ()); + + // + // NOTE: DOM doesn't clearly provide internal + // entity support; but in case someone tries to + // fudge such support, we defend ourselves above. + // + // NOTE: DOM doesn't expose parameter entities + // (thank you thank you thank you thank you) + // + } + + // + // NOTE: DOM (levels 1 and 2) doesn't expose real + // typing information (element or attribute decls), + // as exposed by SAX2 declaration handlers. + // + lexicalHandler.endDTD (); + } + break; + + case Node.ENTITY_REFERENCE_NODE: + // this isn't done except (a) in content, and + // (b) not within a start tag (att value) + lexicalHandler.startEntity (current.getNodeName ()); + break; + + case Node.PROCESSING_INSTRUCTION_NODE: + contentHandler.processingInstruction ( + current.getNodeName (), current.getNodeValue ()); + break; + + case Node.TEXT_NODE: + chars = current.getNodeValue ().toCharArray (); + contentHandler.characters (chars, 0, chars.length); + break; + + default: + // e.g. fragments, entities, notations, attributes + throw new SAXException ("Illegal DOM Node type in Document: " + + current.getNodeType ()); + } + + // + // Then, pick the next node to visit. If the next node isn't + // a child, an "end" call may be needed before moving on. + // If there's no next node, we're done. + // + Node next; + + switch (type) { + case Node.DOCUMENT_NODE: + case Node.ELEMENT_NODE: + case Node.ENTITY_REFERENCE_NODE: + // + // For elements that can have children, visit those + // children before any siblings (i.e. depth first) + // and after visiting this node (i.e. preorder) + // + next = current.getFirstChild (); + if (next != null) { + current = next; + break; + } + // + // Else treat this like other childless nodes, but + // handle this node's "end" immediately. + // + callEnd (current); + + // FALLTHROUGH + + case Node.CDATA_SECTION_NODE: + case Node.COMMENT_NODE: + case Node.DOCUMENT_TYPE_NODE: + case Node.ENTITY_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + case Node.TEXT_NODE: + // + // Use next sibling, if there is one. + // Else, climb up a level (calling "end") + // until we find an ancestral sibling + // or until we we climb off the top (FINISH) + // + for (;;) { + if ((next = current.getNextSibling ()) != null) + break; + current = current.getParentNode (); + if (current == null || current == start) + return; + callEnd (current); + } + current = next; + break; + + default: + throw new SAXException ( + "Illegal DOM Node type found: " + current.getNodeType ()); + } + } + } + + private void callEnd (Node node) throws SAXException + { + switch (node.getNodeType ()) { + // only these three container types may ever be found + // directly inside a Document. + case Node.DOCUMENT_NODE: + // for SAX conformance, endDocument must always + // be called ... it's done in a "finally" clause) + return; + + case Node.ELEMENT_NODE: + if (showNamespaces) { + if (isL2) + contentHandler.endElement ( + node.getNamespaceURI (), + node.getLocalName (), + node.getNodeName ()); + else +// XXX + throw new RuntimeException ( + "NYI, ns lookup when parsing L1 DOM"); + for (Enumeration e = prefixStack.getDeclaredPrefixes (); + e.hasMoreElements (); + ) { + contentHandler.endPrefixMapping ((String) e.nextElement ()); + } + } else + contentHandler.endElement ("", "", node.getNodeName ()); + prefixStack.popContext (); + return; + + case Node.ENTITY_REFERENCE_NODE: + // see above -- in content, outside start tags. + lexicalHandler.endEntity (node.getNodeName ()); + return; + + // these can be given at the top level + case Node.DOCUMENT_FRAGMENT_NODE: + case Node.ATTRIBUTE_NODE: + return; + + default: + throw new SAXException ( + "Illegal DOM container type found: " + + current.getNodeType ()); + } + } +} diff --git a/libjava/classpath/gnu/xml/util/Resolver.java b/libjava/classpath/gnu/xml/util/Resolver.java new file mode 100644 index 00000000000..e84b5ade106 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/Resolver.java @@ -0,0 +1,263 @@ +/* Resolver.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.util; + +import java.io.File; +import java.io.IOException; +import java.util.Dictionary; + +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Utility implementation of a SAX resolver, which can be used to improve + * network utilization of SAX based XML components. It does this by + * supporting local caches of external entities. + * SAX parsers <em>should</em> use such local caches when possible. + * + * @see XCat + */ +public class Resolver implements EntityResolver, Cloneable +{ + /** + * Updates a dictionary used to map PUBLIC identifiers to file names, + * so that it uses the mappings in a specified directory. + * + * @param mappings Array of string pairs, where the first member + * of each pair is a PUBLIC identifier and the second is the + * name of a file, relative to the specified directory. + * @param directory File holding the specified files. + */ + public static void addDirectoryMapping ( + Dictionary table, + String mappings [][], + File directory + ) throws IOException + { + for (int i = 0; i < mappings.length; i++) { + File file = new File (directory, mappings [i][1]); + String temp; + + if (!file.exists ()) // ?? log a warning ?? + continue; + + temp = fileToURL (file); + table.put (mappings [i][0], temp); + } + } + + // FIXME: these *URL routines don't quite belong here, except + // that they're all in the same spirit of making it easy to + // use local filesystem URIs with XML parsers. + + /** + * Provides the URL for a named file, without relying on the JDK 1.2 + * {@link java.io.File#toURL File.toURL}() utility method. + * + * @param filename the file name to convert. Relative file names + * are resolved the way the JVM resolves them (current to the + * process-global current working directory). + * + * @exception IOException if the file does not exist + */ + public static String fileNameToURL (String filename) + throws IOException + { + return fileToURL (new File (filename)); + } + + /** + * Provides the URL for a file, without relying on the JDK 1.2 + * {@link java.io.File#toURL File.toURL}() utility method. + * + * @param f the file to convert. Relative file names + * are resolved the way the JVM resolves them (current to the + * process-global current working directory). + * + * @exception IOException if the file does not exist + */ + public static String fileToURL (File f) + throws IOException + { + String temp; + + // NOTE: the javax.xml.parsers.DocumentBuilder and + // javax.xml.transform.stream.StreamSource versions + // of this don't have this test. Some JVM versions + // don't report this error sanely through URL code. + if (!f.exists ()) + throw new IOException ("no such file: " + f.getName ()); + + // FIXME: getAbsolutePath() seems buggy; I'm seeing components + // like "/foo/../" which are clearly not "absolute" + // and should have been resolved with the filesystem. + + // Substituting "/" would be wrong, "foo" may have been + // symlinked ... the URL code will make that change + // later, so that things can get _really_ broken! + + temp = f.getAbsolutePath (); + + if (File.separatorChar != '/') + temp = temp.replace (File.separatorChar, '/'); + if (!temp.startsWith ("/")) + temp = "/" + temp; + if (!temp.endsWith ("/") && f.isDirectory ()) + temp = temp + "/"; + return "file:" + temp; + } + + + /** + * Returns a URL string. Note that if a malformed URL is provided, or + * the parameter names a nonexistent file, the resulting URL may be + * malformed. + * + * @param fileOrURL If this is the name of a file which exists, + * then its URL is returned. Otherwise the argument is returned. + */ + public static String getURL (String fileOrURL) + { + try { + return fileNameToURL (fileOrURL); + } catch (Exception e) { + return fileOrURL; + } + } + + + + // note: cloneable, this is just copied; unguarded against mods + private Dictionary pubidMapping; + + /** + * Constructs a resolver which understands how to map PUBLIC identifiers + * to other URIs, typically for local copies of standard DTD components. + * + * @param dictionary maps PUBLIC identifiers to URIs. This is not + * copied; subsequent modifications will be reported through the + * resolution operations. + */ + public Resolver (Dictionary dict) + { pubidMapping = dict; } + + + // FIXME: want notion of a "system default" resolver, presumably + // loaded with all sorts of useful stuff. At the same time need + // a notion of resolver chaining (failure --> next) so that subsystems + // can set up things that won't interfere with other ones. + + /** + * This parses most MIME content type strings that have <em>charset=...</em> + * encoding declarations to and returns the specified encoding. This + * conforms to RFC 3023, and is useful when constructing InputSource + * objects from URLConnection objects or other objects using MIME + * content typing. + * + * @param contentType the MIME content type that will be parsed; must + * not be null. + * @return the appropriate encoding, or null if the content type is + * not text and there's no <code>charset=...</code> attribute + */ + static public String getEncoding (String contentType) + { + // currently a dumb parsing algorithm that works "mostly" and handles + // ..anything...charset=ABC + // ..anything...charset=ABC;otherAttr=DEF + // ..anything...charset=ABC (comment);otherAttr=DEF + // ..anything...charset= "ABC" (comment);otherAttr=DEF + + int temp; + String encoding; + String defValue = null; + + if (contentType.startsWith ("text/")) + defValue = contentType.startsWith ("text/html") + ? "ISO-8859-1" : "US-ASCII"; + + // Assumes 'charset' is only an attribute name, not part + // of a value, comment, or other attribute name + // ALSO assumes no escaped values like "\;" or "\)" + if ((temp = contentType.indexOf ("charset")) != -1) { + // strip out everything up to '=' ... + temp = contentType.indexOf ('=', temp); + if (temp == -1) + return defValue; + encoding = contentType.substring (temp + 1); + // ... and any subsequent attributes + if ((temp = encoding.indexOf (';')) != -1) + encoding = encoding.substring (0, temp); + // ... and any comments after value + if ((temp = encoding.indexOf ('(')) != -1) + encoding = encoding.substring (0, temp); + // ... then whitespace, and any (double) quotes + encoding = encoding.trim (); + if (encoding.charAt (0) == '"') + encoding = encoding.substring (1, encoding.length () - 1); + } else + encoding = defValue; + return encoding; + } + + + /** + * Uses a local dictionary of public identifiers to resolve URIs, + * normally with the goal of minimizing network traffic or latencies. + */ + public InputSource resolveEntity (String publicId, String systemId) + throws IOException, SAXException + { + InputSource retval = null; + String uri; + + if (publicId != null + && ((uri = (String) pubidMapping.get (publicId)) != null)) { + retval = new InputSource (uri); + retval.setPublicId (publicId); + } + + // Could do URN resolution here + + // URL resolution always done by parser + + // FIXME: chain to "next" resolver + + return retval; + } +} diff --git a/libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java b/libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java new file mode 100644 index 00000000000..81ad8231270 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java @@ -0,0 +1,676 @@ +/* SAXNullTransformerFactory.java -- + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.util; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; +import java.util.Hashtable; +import java.util.Properties; + +import gnu.xml.dom.Consumer; +import gnu.xml.dom.DomDocument; +import gnu.xml.pipeline.DomConsumer; +import gnu.xml.pipeline.EventFilter; + +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.sax.*; +import javax.xml.transform.stream.*; + +import org.xml.sax.*; +import org.xml.sax.helpers.XMLReaderFactory; +import org.xml.sax.helpers.LocatorImpl; + + +/** + * Implements null transforms. XSLT stylesheets are not supported. + * This class provides a way to translate three representations of + * XML data (SAX event stream, DOM tree, and XML text) into each other. + * In essence it's a thinnish wrapper around basic SAX event + * <a href="../pipeline/package-summary.html">pipeline</a> facilities, which + * exposes only limited functionality. The <em>javax.xml.transform</em> + * functionality is implemented as follows: <ul> + * + * <li>The {@link javax.xml.transform.sax.SAXSource SAXSource} class + * just wraps an {@link XMLReader} and {@link InputSource}, while the + * {@link javax.xml.transform.sax.SAXResult SAXResult} class is less + * functional than a {@link gnu.xml.pipeline.EventConsumer EventConsumer}. + * (Notably, it drops all but one declaration from any DTD.)</li> + * + * <li>The {@link javax.xml.transform.dom.DOMSource DOMSource} class + * corresponds to special SAX parsers like {@link DomParser}, and the + * {@link javax.xml.transform.dom.DOMResult DOMResult} class corresponds + * to a {@link gnu.xml.pipeline.DomConsumer DomConsumer}.</li> + * + * <li>The {@link javax.xml.transform.stream.StreamSource StreamSource} + * class corresponds to a SAX {@link InputSource}, and the + * {@link javax.xml.transform.stream.StreamResult StreamResult} class + * corresponds to a {@link gnu.xml.pipeline.TextConsumer TextConsumer}.</li> + * + * </ul> + * + * <p><em>This implementation is preliminary.</em> + * + * @see gnu.xml.pipeline.XsltFilter + * + * @author David Brownell + */ +public class SAXNullTransformerFactory extends SAXTransformerFactory +{ + + private ErrorListener errListener; + private URIResolver uriResolver; + + /** Default constructor */ + public SAXNullTransformerFactory () { } + + // + // only has stuff that makes sense with null transforms + // + + /** + * Returns true if the requested feature is supported. + * All three kinds of input and output are accepted: + * XML text, SAX events, and DOM nodes. + */ + public boolean getFeature (String feature) + { + return SAXTransformerFactory.FEATURE.equals (feature) + || SAXResult.FEATURE.equals (feature) + || SAXSource.FEATURE.equals (feature) + || DOMResult.FEATURE.equals (feature) + || DOMSource.FEATURE.equals (feature) + || StreamResult.FEATURE.equals (feature) + || StreamSource.FEATURE.equals (feature) + ; + } + + public void setFeature(String name, boolean value) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException(name); + } + + + /** Throws an exception (no implementation attributes are supported) */ + public void setAttribute (String key, Object value) + { + throw new IllegalArgumentException (); + } + + /** Throws an exception (no implementation attributes are supported) */ + public Object getAttribute (String key) + { + throw new IllegalArgumentException (); + } + + /** (not yet implemented) */ + public Source getAssociatedStylesheet (Source source, + String media, + String title, + String charset) + throws TransformerConfigurationException + { + // parse, and find the appropriate xsl-stylesheet PI contents + throw new IllegalArgumentException (); + } + + public Transformer newTransformer () + throws TransformerConfigurationException + { + return new NullTransformer (); + } + + /** + * Returns a TransformerHandler that knows how to generate output + * in all three standard formats. Output text is generated using + * {@link XMLWriter}, and the GNU implementation of + * {@link DomDocument DOM} is used. + * + * @see SAXResult + * @see StreamResult + * @see DOMResult + */ + public TransformerHandler newTransformerHandler () + throws TransformerConfigurationException + { + NullTransformer transformer = new NullTransformer (); + return transformer.handler; + } + + // + // Stuff that depends on XSLT support, which we don't provide + // + private static final String noXSLT = "No XSLT support"; + + /** Throws an exception (XSLT is not supported). */ + public Transformer newTransformer (Source stylesheet) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Throws an exception (XSLT is not supported). */ + public Templates newTemplates (Source stylesheet) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Throws an exception (XSLT is not supported). */ + public TemplatesHandler newTemplatesHandler () + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Throws an exception (XSLT is not supported). */ + public TransformerHandler newTransformerHandler (Source stylesheet) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Throws an exception (XSLT is not supported). */ + public TransformerHandler newTransformerHandler (Templates stylesheet) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Throws an exception (XSLT is not supported). */ + public XMLFilter newXMLFilter (Source stylesheet) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Throws an exception (XSLT is not supported). */ + public XMLFilter newXMLFilter (Templates stylesheet) + throws TransformerConfigurationException + { + throw new TransformerConfigurationException (noXSLT); + } + + /** Returns the value assigned by {@link #setErrorListener}. */ + public ErrorListener getErrorListener () + { + return errListener; + } + + /** Assigns a value that would be used when parsing stylesheets */ + public void setErrorListener (ErrorListener e) + { + errListener = e; + } + + /** Returns the value assigned by {@link #setURIResolver}. */ + public URIResolver getURIResolver () + { + return uriResolver; + } + + /** Assigns a value that would be used when parsing stylesheets */ + public void setURIResolver (URIResolver u) + { + uriResolver = u; + } + + + // + // Helper classes. These might in theory be subclassed + // by an XSLT implementation, if they were exported. + // + + static class DomTerminus + extends DomConsumer + { + + DomTerminus (DOMResult result) + throws SAXException + { + // won't really throw SAXException + super (DomDocument.class); + setHandler (new DomHandler (this, result)); + } + + } + + static class DomHandler + extends Consumer.Backdoor + { + + private DOMResult result; + + DomHandler (DomConsumer c, DOMResult r) + throws SAXException + { + // won't really throw SAXException + super (c); + result = r; + } + + public void endDocument () + throws SAXException + { + super.endDocument (); + result.setNode (getDocument ()); + } + + } + + private static OutputStream getOutputStream (String uri) + throws IOException + { + // JDK stupidity: file "protocol does not support output" ... + if (uri.startsWith ("file:")) + return new FileOutputStream (uri.substring (5)); + + // Otherwise ... + URL url = new URL (uri); + URLConnection conn = url.openConnection (); + + conn.setDoOutput (true); + return conn.getOutputStream (); + } + + + static class NullHandler + extends EventFilter + implements TransformerHandler + { + + private String systemId; + private Transformer transformer; + + NullHandler (Transformer t) + { + transformer = t; + } + + public Transformer getTransformer () + { + return transformer; + } + + public String getSystemId () + { + return systemId; + } + + public void setSystemId (String id) + { + systemId = id; + } + + public void setResult (Result result) + { + if (result.getSystemId () != null) + systemId = result.getSystemId (); + + try + { + + // output to partial SAX event stream? + if (result instanceof SAXResult) + { + SAXResult r = (SAXResult) result; + + setContentHandler (r.getHandler ()); + setProperty (LEXICAL_HANDLER, r.getLexicalHandler ()); + // DTD info is filtered out by javax.transform + + // output to DOM tree? + } + else if (result instanceof DOMResult) + { + DomTerminus out = new DomTerminus ((DOMResult) result); + + setContentHandler (out.getContentHandler ()); + setProperty (LEXICAL_HANDLER, + out.getProperty (LEXICAL_HANDLER)); + // save DTD-derived info, if any. + setDTDHandler (out.getDTDHandler ()); + setProperty (DECL_HANDLER, + out.getProperty (DECL_HANDLER)); + + // node is saved into result on endDocument() + + // output to (XML) text? + } + else if (result instanceof StreamResult) + { + StreamResult r = (StreamResult) result; + XMLWriter out; + + // FIXME: when do output properties take effect? + // encoding, standalone decl, xml/xhtml/... ... + + // FIXME: maybe put nsfix filter up front + + try + { + if (r.getWriter () != null) + out = new XMLWriter (r.getWriter ()); + else if (r.getOutputStream () != null) + out = new XMLWriter (r.getOutputStream ()); + else if (r.getSystemId () != null) + out = new XMLWriter ( + getOutputStream (r.getSystemId ())); + else + throw new IllegalArgumentException ( + "bad StreamResult"); + } + catch (IOException e) + { + e.printStackTrace (); + // on jdk 1.4, pass the root cause ... + throw new IllegalArgumentException (e.getMessage ()); + } + + // out.setExpandingEntities (true); + // out.setPrettyPrinting (true); + // out.setXhtml (true); + + setContentHandler (out); + setProperty (LEXICAL_HANDLER, out); + // save DTD info, if any; why not? + setDTDHandler (out); + setProperty (DECL_HANDLER, out); + } + + } + catch (SAXException e) + { + // SAXNotSupportedException or SAXNotRecognizedException: + // "can't happen" ... but SAXException for DOM build probs + // could happen, so ... + // on jdk 1.4, pass the root cause ... + throw new IllegalArgumentException (e.getMessage ()); + } + } + } + + // an interface that adds no value + static class LocatorAdapter + extends LocatorImpl + implements SourceLocator + { + + LocatorAdapter (SAXParseException e) + { + setSystemId (e.getSystemId ()); + setPublicId (e.getPublicId ()); + setLineNumber (e.getLineNumber ()); + setColumnNumber (e.getColumnNumber ()); + } + + } + + // another interface that adds no value + static class ListenerAdapter + implements ErrorHandler + { + + NullTransformer transformer; + + ListenerAdapter (NullTransformer t) + { + transformer = t; + } + + private TransformerException map (SAXParseException e) + { + return new TransformerException ( + e.getMessage (), + new LocatorAdapter (e), + e); + } + + public void error (SAXParseException e) + throws SAXParseException + { + try + { + if (transformer.errListener != null) + transformer.errListener.error (map (e)); + } + catch (TransformerException ex) + { + transformer.ex = ex; + throw e; + } + } + + public void fatalError (SAXParseException e) + throws SAXParseException + { + try + { + if (transformer.errListener != null) + transformer.errListener.fatalError (map (e)); + else + throw map (e); + } catch (TransformerException ex) { + transformer.ex = ex; + throw e; + } + } + + public void warning (SAXParseException e) + throws SAXParseException + { + try + { + if (transformer.errListener != null) + transformer.errListener.warning (map (e)); + } + catch (TransformerException ex) + { + transformer.ex = ex; + throw e; + } + } + } + + static class NullTransformer + extends Transformer + { + + private URIResolver uriResolver; + private Properties props = new Properties (); + private Hashtable params = new Hashtable (7); + + ErrorListener errListener = null; + TransformerException ex = null; + NullHandler handler; + + NullTransformer () + { + super (); + handler = new NullHandler (this); + } + + public ErrorListener getErrorListener () + { + return errListener; + } + + public void setErrorListener (ErrorListener e) + { + errListener = e; + } + + public URIResolver getURIResolver () + { + return uriResolver; + } + + public void setURIResolver (URIResolver u) + { + uriResolver = u; + } + + public void setOutputProperties (Properties p) + { + props = (Properties) p.clone (); + } + + public Properties getOutputProperties () + { + return (Properties) props.clone (); + } + + public void setOutputProperty (String name, String value) + { + props.setProperty (name, value); + } + + public String getOutputProperty (String name) + { + return props.getProperty (name); + } + + public void clearParameters () + { + params.clear (); + } + + public void setParameter (String name, Object value) + { + props.put (name, value); + } + + public Object getParameter (String name) + { + return props.get (name); + } + + public void transform (Source in, Result out) + throws TransformerException + { + try + { + XMLReader producer; + InputSource input; + + // Input from DOM? + if (in instanceof DOMSource) + { + DOMSource source = (DOMSource) in; + + if (source.getNode () == null) + throw new IllegalArgumentException ("no DOM node"); + producer = new DomParser (source.getNode ()); + input = null; + + // Input from SAX? + } + else if (in instanceof SAXSource) + { + SAXSource source = (SAXSource) in; + + producer = source.getXMLReader (); + if (producer == null) + producer = XMLReaderFactory.createXMLReader (); + + input = source.getInputSource (); + if (input == null) + { + if (source.getSystemId () != null) + input = new InputSource (source.getSystemId ()); + else + throw new IllegalArgumentException ( + "missing SAX input"); + } + + // Input from a stream or something? + } + else + { + producer = XMLReaderFactory.createXMLReader (); + input = SAXSource.sourceToInputSource (in); + if (input == null) + throw new IllegalArgumentException ("missing input"); + } + + // preserve original namespace prefixes + try + { + producer.setFeature(EventFilter.FEATURE_URI + + "namespace-prefixes", + true); + } + catch (Exception e) + { + /* ignore */ + // FIXME if we couldn't, "NsFix" stage before the output .. + } + + // arrange the output + handler.setResult (out); + EventFilter.bind (producer, handler); + + // then parse ... single element pipeline + producer.parse (input); + + } + catch (IOException e) + { + throw new TransformerException ("transform failed", e); + + } + catch (SAXException e) + { + if (ex == null && ex.getCause () == e) + throw ex; + else + throw new TransformerException ("transform failed", e); + + } + finally + { + ex = null; + } + } + } + +} diff --git a/libjava/classpath/gnu/xml/util/XCat.java b/libjava/classpath/gnu/xml/util/XCat.java new file mode 100644 index 00000000000..0f163387081 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/XCat.java @@ -0,0 +1,1609 @@ +/* XCat.java -- + Copyright (C) 2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.xml.util; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.StringTokenizer; +import java.util.Stack; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; + +import org.xml.sax.ext.DefaultHandler2; +import org.xml.sax.ext.EntityResolver2; + +import org.xml.sax.helpers.XMLReaderFactory; + +/** + * Packages <a href= + "http://www.oasis-open.org/committees/entity/spec-2001-08-06.html" + >OASIS XML Catalogs</a>, + * primarily for entity resolution by parsers. + * That specification defines an XML syntax for mappings between + * identifiers declared in DTDs (particularly PUBLIC identifiers) and + * locations. SAX has always supported such mappings, but conventions for + * an XML file syntax to maintain them have previously been lacking. + * + * <p> This has three main operational modes. The primary intended mode is + * to create a resolver, then preloading it with one or more site-standard + * catalogs before using it with one or more SAX parsers: <pre> + * XCat catalog = new XCat (); + * catalog.setErrorHandler (diagnosticErrorHandler); + * catalog.loadCatalog ("file:/local/catalogs/catalog.cat"); + * catalog.loadCatalog ("http://shared/catalog.cat"); + * ... + * catalog.disableLoading (); + * parser1.setEntityResolver (catalog); + * parser2.setEntityResolver (catalog); + * ...</pre> + * + * <p>A second mode is to arrange that your application uses instances of + * this class as its entity resolver, and automatically loads catalogs + * referenced by <em><?oasis-xml-catalog...?></em> processing + * instructions found before the DTD in documents it parses. + * It would then discard the resolver after each parse. + * + * <p> A third mode applies catalogs in contexts other than entity + * resolution for parsers. + * The {@link #resolveURI resolveURI()} method supports resolving URIs + * stored in XML application data, rather than inside DTDs. + * Catalogs would be loaded as shown above, and the catalog could + * be used concurrently for parser entity resolution and for + * application URI resolution. + * </p> + * + * <center><hr width='70%'></center> + * + * <p>Errors in catalogs implicitly loaded (during resolution) are ignored + * beyond being reported through any <em>ErrorHandler</em> assigned using + * {@link #setErrorHandler setErrorHandler()}. SAX exceptions + * thrown from such a handler won't abort resolution, although throwing a + * <em>RuntimeException</em> or <em>Error</em> will normally abort both + * resolution and parsing. Useful diagnostic information is available to + * any <em>ErrorHandler</em> used to report problems, or from any exception + * thrown from an explicit {@link #loadCatalog loadCatalog()} invocation. + * Applications can use that information as troubleshooting aids. + * + * <p>While this class requires <em>SAX2 Extensions 1.1</em> classes in + * its class path, basic functionality does not require using a SAX2 + * parser that supports the extended entity resolution functionality. + * See the original SAX1 + * {@link #resolveEntity(java.lang.String,java.lang.String) resolveEntity()} + * method for a list of restrictions which apply when it is used with + * older SAX parsers. + * + * @see EntityResolver2 + * + * @author David Brownell + */ +public class XCat implements EntityResolver2 +{ + private Catalog catalogs []; + private boolean usingPublic = true; + private boolean loadingPermitted = true; + private boolean unified = true; + private String parserClass; + private ErrorHandler errorHandler; + + // private EntityResolver next; // chain to next if we fail... + + // + // NOTE: This is a straightforward implementation, and if + // there are lots of "nextCatalog" or "delegate*" entries + // in use, two tweaks would be worth considering: + // + // - Centralize some sort of cache (key by URI) for individual + // resolvers. That'd avoid multiple copies of a given catalog. + // + // - Have resolution track what catalogs (+modes) have been + // searched. This would support loop detection. + // + + + /** + * Initializes without preloading a catalog. + * This API is convenient when you may want to arrange that catalogs + * are automatically loaded when explicitly referenced in documents, + * using the <em>oasis-xml-catalog</em> processing instruction. + * In such cases you won't usually be able to preload catalogs. + */ + public XCat () { } + + /** + * Initializes, and preloads a catalog using the default SAX parser. + * This API is convenient when you operate with one or more standard + * catalogs. + * + * <p> This just delegates to {@link #loadCatalog loadCatalog()}; + * see it for exception information. + * + * @param uri absolute URI for the catalog file. + */ + public XCat (String uri) + throws SAXException, IOException + { loadCatalog (uri); } + + + /** + * Loads an OASIS XML Catalog. + * It is appended to the list of currently active catalogs, or + * reloaded if a catalog with the same URI was already loaded. + * Callers have control over what parser is used, how catalog parsing + * errors are reported, and whether URIs will be resolved consistently. + * + * <p> The OASIS specification says that errors detected when loading + * catalogs "must recover by ignoring the catalog entry file that + * failed, and proceeding." In this API, that action can be the + * responsibility of applications, when they explicitly load any + * catalog using this method. + * + * <p>Note that catalogs referenced by this one will not be loaded + * at this time. Catalogs referenced through <em>nextCatalog</em> + * or <em>delegate*</em> elements are normally loaded only if needed. + * + * @see #setErrorHandler + * @see #setParserClass + * @see #setUnified + * + * @param uri absolute URI for the catalog file. + * + * @exception IOException As thrown by the parser, typically to + * indicate problems reading data from that URI. + * @exception SAXException As thrown by the parser, typically to + * indicate problems parsing data from that URI. It may also + * be thrown if the parser doesn't support necessary handlers. + * @exception IllegalStateException When attempting to load a + * catalog after loading has been {@link #disableLoading disabled}, + * such as after any entity or URI lookup has been performed. + */ + public synchronized void loadCatalog (String uri) + throws SAXException, IOException + { + Catalog catalog; + int index = -1; + + if (!loadingPermitted) + throw new IllegalStateException (); + + uri = normalizeURI (uri); + if (catalogs != null) { + // maybe just reload + for (index = 0; index < catalogs.length; index++) + if (uri.equals (catalogs [index].catalogURI)) + break; + } + catalog = loadCatalog (parserClass, errorHandler, uri, unified); + + // add to list of catalogs + if (catalogs == null) { + index = 0; + catalogs = new Catalog [1]; + } else if (index == catalogs.length) { + Catalog tmp []; + + tmp = new Catalog [index + 1]; + System.arraycopy (catalogs, 0, tmp, 0, index); + catalogs = tmp; + } + catalogs [index] = catalog; + } + + + /** + * "New Style" external entity resolution for parsers. + * Calls to this method prevent explicit loading of additional catalogs + * using {@link #loadCatalog loadCatalog()}. + * + * <p>This supports the full core catalog functionality for locating + * (and relocating) parsed entities that have been declared in a + * document's DTD. + * + * @param name Entity name, such as "dudley", "%nell", or "[dtd]". + * @param publicId Either a normalized public ID, or null. + * @param baseURI Absolute base URI associated with systemId. + * @param systemId URI found in entity declaration (may be + * relative to baseURI). + * + * @return Input source for accessing the external entity, or null + * if no mapping was found. The input source may have opened + * the stream, and will have a fully resolved URI. + * + * @see #getExternalSubset + */ + public InputSource resolveEntity ( + String name, // UNUSED ... systemId is always non-null + String publicId, + String baseURI, // UNUSED ... it just lets sysId be relative + String systemId + ) throws SAXException, IOException + { + if (loadingPermitted) + disableLoading (); + + try { + // steps as found in OASIS XML catalog spec 7.1.2 + // steps 1, 8 involve looping over the list of catalogs + for (int i = 0; i < catalogs.length; i++) { + InputSource retval; + retval = catalogs [i].resolve (usingPublic, publicId, systemId); + if (retval != null) + return retval;; + } + } catch (DoneDelegation x) { + // done! + } + // step 9 involves returning "no match" + return null; + } + + + /** + * "New Style" parser callback to add an external subset. + * For documents that don't include an external subset, this may + * return one according to <em>doctype</em> catalog entries. + * (This functionality is not a core part of the OASIS XML Catalog + * specification, though it's presented in an appendix.) + * If no such entry is defined, this returns null to indicate that + * this document will not be modified to include such a subset. + * Calls to this method prevent explicit loading of additional catalogs + * using {@link #loadCatalog loadCatalog()}. + * + * <p><em>Warning:</em> That catalog functionality can be dangerous. + * It can provide definitions of general entities, and thereby mask + * certain well formedess errors. + * + * @param name Name of the document element, either as declared in + * a DOCTYPE declaration or as observed in the text. + * @param baseURI Document's base URI (absolute). + * + * @return Input source for accessing the external subset, or null + * if no mapping was found. The input source may have opened + * the stream, and will have a fully resolved URI. + */ + public InputSource getExternalSubset (String name, String baseURI) + throws SAXException, IOException + { + if (loadingPermitted) + disableLoading (); + try { + for (int i = 0; i < catalogs.length; i++) { + InputSource retval = catalogs [i].getExternalSubset (name); + if (retval != null) + return retval; + } + } catch (DoneDelegation x) { + // done! + } + return null; + } + + + /** + * "Old Style" external entity resolution for parsers. + * This API provides only core functionality. + * Calls to this method prevent explicit loading of additional catalogs + * using {@link #loadCatalog loadCatalog()}. + * + * <p>The functional limitations of this interface include:</p><ul> + * + * <li>Since system IDs will be absolutized before the resolver + * sees them, matching against relative URIs won't work. + * This may affect <em>system</em>, <em>rewriteSystem</em>, + * and <em>delegateSystem</em> catalog entries. + * + * <li>Because of that absolutization, documents declaring entities + * with system IDs using URI schemes that the JVM does not recognize + * may be unparsable. URI schemes such as <em>file:/</em>, + * <em>http://</em>, <em>https://</em>, and <em>ftp://</em> + * will usually work reliably. + * + * <li>Because missing external subsets can't be provided, the + * <em>doctype</em> catalog entries will be ignored. + * (The {@link #getExternalSubset getExternalSubset()} method is + * a "New Style" resolution option.) + * + * </ul> + * + * <p>Applications can tell whether this limited functionality will be + * used: if the feature flag associated with the {@link EntityResolver2} + * interface is not <em>true</em>, the limitations apply. Applications + * can't usually know whether a given document and catalog will trigger + * those limitations. The issue can only be bypassed by operational + * procedures such as not using catalogs or documents which involve + * those features. + * + * @param publicId Either a normalized public ID, or null + * @param systemId Always an absolute URI. + * + * @return Input source for accessing the external entity, or null + * if no mapping was found. The input source may have opened + * the stream, and will have a fully resolved URI. + */ + final public InputSource resolveEntity (String publicId, String systemId) + throws SAXException, IOException + { + return resolveEntity (null, publicId, null, systemId); + } + + + /** + * Resolves a URI reference that's not defined to the DTD. + * This is intended for use with URIs found in document text, such as + * <em>xml-stylesheet</em> processing instructions and in attribute + * values, where they are not recognized as URIs by XML parsers. + * Calls to this method prevent explicit loading of additional catalogs + * using {@link #loadCatalog loadCatalog()}. + * + * <p>This functionality is supported by the OASIS XML Catalog + * specification, but will never be invoked by an XML parser. + * It corresponds closely to functionality for mapping system + * identifiers for entities declared in DTDs; closely enough that + * this implementation's default behavior is that they be + * identical, to minimize potential confusion. + * + * <p>This method could be useful when implementing the + * {@link javax.xml.transform.URIResolver} interface, wrapping the + * input source in a {@link javax.xml.transform.sax.SAXSource}. + * + * @see #isUnified + * @see #setUnified + * + * @param baseURI The relevant base URI as specified by the XML Base + * specification. This recognizes <em>xml:base</em> attributes + * as overriding the actual (physical) base URI. + * @param uri Either an absolute URI, or one relative to baseURI + * + * @return Input source for accessing the mapped URI, or null + * if no mapping was found. The input source may have opened + * the stream, and will have a fully resolved URI. + */ + public InputSource resolveURI (String baseURI, String uri) + throws SAXException, IOException + { + if (loadingPermitted) + disableLoading (); + + // NOTE: baseURI isn't used here, but caller MUST have it, + // and heuristics _might_ use it in the future ... plus, + // it's symmetric with resolveEntity (). + + // steps 1, 6 involve looping + try { + for (int i = 0; i < catalogs.length; i++) { + InputSource tmp = catalogs [i].resolveURI (uri); + if (tmp != null) + return tmp; + } + } catch (DoneDelegation x) { + // done + } + // step 7 reports no match + return null; + } + + + /** + * Records that catalog loading is no longer permitted. + * Loading is automatically disabled when lookups are performed, + * and should be manually disabled when <em>startDTD()</em> (or + * any other DTD declaration callback) is invoked, or at the latest + * when the document root element is seen. + */ + public synchronized void disableLoading () + { + // NOTE: this method and loadCatalog() are synchronized + // so that it's impossible to load (top level) catalogs + // after lookups start. Likewise, deferred loading is also + // synchronized (for "next" and delegated catalogs) to + // ensure that parsers can share resolvers. + loadingPermitted = false; + } + + + /** + * Returns the error handler used to report catalog errors. + * Null is returned if the parser's default error handling + * will be used. + * + * @see #setErrorHandler + */ + public ErrorHandler getErrorHandler () + { return errorHandler; } + + /** + * Assigns the error handler used to report catalog errors. + * These errors may come either from the SAX2 parser or + * from the catalog parsing code driven by the parser. + * + * <p> If you're sharing the resolver between parsers, don't + * change this once lookups have begun. + * + * @see #getErrorHandler + * + * @param parser The error handler, or null saying to use the default + * (no diagnostics, and only fatal errors terminate loading). + */ + public void setErrorHandler (ErrorHandler handler) + { errorHandler = handler; } + + + /** + * Returns the name of the SAX2 parser class used to parse catalogs. + * Null is returned if the system default is used. + * @see #setParserClass + */ + public String getParserClass () + { return parserClass; } + + /** + * Names the SAX2 parser class used to parse catalogs. + * + * <p> If you're sharing the resolver between parsers, don't change + * this once lookups have begun. + * + * <p> Note that in order to properly support the <em>xml:base</em> + * attribute and relative URI resolution, the SAX parser used to parse + * the catalog must provide a {@link Locator} and support the optional + * declaration and lexical handlers. + * + * @see #getParserClass + * + * @param parser The parser class name, or null saying to use the + * system default SAX2 parser. + */ + public void setParserClass (String parser) + { parserClass = parser; } + + + /** + * Returns true (the default) if all methods resolve + * a given URI in the same way. + * Returns false if calls resolving URIs as entities (such as + * {@link #resolveEntity resolveEntity()}) use different catalog entries + * than those resolving them as URIs ({@link #resolveURI resolveURI()}), + * which will generally produce different results. + * + * <p>The OASIS XML Catalog specification defines two related schemes + * to map URIs "as URIs" or "as system IDs". + * URIs use <em>uri</em>, <em>rewriteURI</em>, and <em>delegateURI</em> + * elements. System IDs do the same things with <em>systemId</em>, + * <em>rewriteSystemId</em>, and <em>delegateSystemId</em>. + * It's confusing and error prone to maintain two parallel copies of + * such data. Accordingly, this class makes that behavior optional. + * The <em>unified</em> interpretation of URI mappings is preferred, + * since it prevents surprises where one URI gets mapped to different + * contents depending on whether the reference happens to have come + * from a DTD (or not). + * + * @see #setUnified + */ + public boolean isUnified () + { return unified; } + + /** + * Assigns the value of the flag returned by {@link #isUnified}. + * Set it to false to be strictly conformant with the OASIS XML Catalog + * specification. Set it to true to make all mappings for a given URI + * give the same result, regardless of the reason for the mapping. + * + * <p>Don't change this once you've loaded the first catalog. + * + * @param value new flag setting + */ + public void setUnified (boolean value) + { unified = value; } + + + /** + * Returns true (the default) if a catalog's public identifier + * mappings will be used. + * When false is returned, such mappings are ignored except when + * system IDs are discarded, such as for + * entities using the <em>urn:publicid:</em> URI scheme in their + * system identifiers. (See RFC 3151 for information about that + * URI scheme. Using it in system identifiers may not work well + * with many SAX parsers unless the <em>resolve-dtd-uris</em> + * feature flag is set to false.) + * @see #setUsingPublic + */ + public boolean isUsingPublic () + { return usingPublic; } + + /** + * Specifies which catalog search mode is used. + * By default, public identifier mappings are able to override system + * identifiers when both are available. + * Applications may choose to ignore public + * identifier mappings in such cases, so that system identifiers + * declared in DTDs will only be overridden by an explicit catalog + * match for that system ID. + * + * <p> If you're sharing the resolver between parsers, don't + * change this once lookups have begun. + * @see #isUsingPublic + * + * @param value true to always use public identifier mappings, + * false to only use them for system ids using the <em>urn:publicid:</em> + * URI scheme. + */ + public void setUsingPublic (boolean value) + { usingPublic = value; } + + + + // hmm, what's this do? :) + private static Catalog loadCatalog ( + String parserClass, + ErrorHandler eh, + String uri, + boolean unified + ) throws SAXException, IOException + { + XMLReader parser; + Loader loader; + boolean doesIntern = false; + + if (parserClass == null) + parser = XMLReaderFactory.createXMLReader (); + else + parser = XMLReaderFactory.createXMLReader (parserClass); + if (eh != null) + parser.setErrorHandler (eh); + // resolve-dtd-entities is at default value (unrecognized == true) + + try { + doesIntern = parser.getFeature ( + "http://xml.org/sax/features/string-interning"); + } catch (SAXNotRecognizedException e) { } + + loader = new Loader (doesIntern, eh, unified); + loader.cat.parserClass = parserClass; + loader.cat.catalogURI = uri; + + parser.setContentHandler (loader); + parser.setProperty ( + "http://xml.org/sax/properties/declaration-handler", + loader); + parser.setProperty ( + "http://xml.org/sax/properties/lexical-handler", + loader); + parser.parse (uri); + + return loader.cat; + } + + // perform one or both the normalizations for public ids + private static String normalizePublicId (boolean full, String publicId) + { + if (publicId.startsWith ("urn:publicid:")) { + StringBuffer buf = new StringBuffer (); + char chars [] = publicId.toCharArray (); +boolean hasbug = false; + + for (int i = 13; i < chars.length; i++) { + switch (chars [i]) { + case '+': buf.append (' '); continue; + case ':': buf.append ("//"); continue; + case ';': buf.append ("::"); continue; + case '%': +// FIXME unhex that char! meanwhile, warn and fallthrough ... + hasbug = true; + default: buf.append (chars [i]); continue; + } + } + publicId = buf.toString (); +if (hasbug) +System.err.println ("nyet unhexing public id: " + publicId); + full = true; + } + + // SAX parsers do everything except that URN mapping, but + // we can't trust other sources to normalize correctly + if (full) { + StringTokenizer tokens; + String token; + + tokens = new StringTokenizer (publicId, " \r\n"); + publicId = null; + while (tokens.hasMoreTokens ()) { + if (publicId == null) + publicId = tokens.nextToken (); + else + publicId += " " + tokens.nextToken (); + } + } + return publicId; + } + + private static boolean isUriExcluded (int c) + { return c <= 0x20 || c >= 0x7f || "\"<>^`{|}".indexOf (c) != -1; } + + private static int hexNibble (int c) + { + if (c < 10) + return c + '0'; + return ('a' - 10) + c; + } + + // handles URIs with "excluded" characters + private static String normalizeURI (String systemId) + { + int length = systemId.length (); + + for (int i = 0; i < length; i++) { + char c = systemId.charAt (i); + + // escape non-ASCII plus "excluded" characters + if (isUriExcluded (c)) { + byte buf []; + ByteArrayOutputStream out; + int b; + + // a JVM that doesn't know UTF8 and 8859_1 is unusable! + try { + buf = systemId.getBytes ("UTF8"); + out = new ByteArrayOutputStream (buf.length + 10); + + for (i = 0; i < buf.length; i++) { + b = buf [i] & 0x0ff; + if (isUriExcluded (b)) { + out.write ((int) '%'); + out.write (hexNibble (b >> 4)); + out.write (hexNibble (b & 0x0f)); + } else + out.write (b); + } + return out.toString ("8859_1"); + } catch (IOException e) { + throw new RuntimeException ( + "can't normalize URI: " + e.getMessage ()); + } + } + } + return systemId; + } + + // thrown to mark authoritative end of a search + private static class DoneDelegation extends SAXException + { + DoneDelegation () { } + } + + + /** + * Represents a OASIS XML Catalog, and encapsulates much of + * the catalog functionality. + */ + private static class Catalog + { + // loading infrastructure + String catalogURI; + ErrorHandler eh; + boolean unified; + String parserClass; + + // catalog data + boolean hasPreference; + boolean usingPublic; + + Hashtable publicIds; + Hashtable publicDelegations; + + Hashtable systemIds; + Hashtable systemRewrites; + Hashtable systemDelegations; + + Hashtable uris; + Hashtable uriRewrites; + Hashtable uriDelegations; + + Hashtable doctypes; + + Vector next; + + // nonpublic! + Catalog () { } + + + // steps as found in OASIS XML catalog spec 7.1.2 + private InputSource locatePublicId (String publicId) + throws SAXException, IOException + { + // 5. return (first) 'public' entry + if (publicIds != null) { + String retval = (String) publicIds.get (publicId); + if (retval != null) { + // IF the URI is accessible ... + return new InputSource (retval); + } + } + + // 6. return delegatePublic catalog match [complex] + if (publicDelegations != null) + return checkDelegations (publicDelegations, publicId, + publicId, null); + + return null; + } + + // steps as found in OASIS XML catalog spec 7.1.2 or 7.2.2 + private InputSource mapURI ( + String uri, + Hashtable ids, + Hashtable rewrites, + Hashtable delegations + ) throws SAXException, IOException + { + // 7.1.2: 2. return (first) 'system' entry + // 7.2.2: 2. return (first) 'uri' entry + if (ids != null) { + String retval = (String) ids.get (uri); + if (retval != null) { + // IF the URI is accessible ... + return new InputSource (retval); + } + } + + // 7.1.2: 3. return 'rewriteSystem' entries + // 7.2.2: 3. return 'rewriteURI' entries + if (rewrites != null) { + String prefix = null; + String replace = null; + int prefixLen = -1; + + for (Enumeration e = rewrites.keys (); + e.hasMoreElements (); + /* NOP */) { + String temp = (String) e.nextElement (); + int len = -1; + + if (!uri.startsWith (temp)) + continue; + if (prefix != null + && (len = temp.length ()) < prefixLen) + continue; + prefix = temp; + prefixLen = len; + replace = (String) rewrites.get (temp); + } + if (prefix != null) { + StringBuffer buf = new StringBuffer (replace); + buf.append (uri.substring (prefixLen)); + // IF the URI is accessible ... + return new InputSource (buf.toString ()); + } + } + + // 7.1.2: 4. return 'delegateSystem' catalog match [complex] + // 7.2.2: 4. return 'delegateURI' catalog match [complex] + if (delegations != null) + return checkDelegations (delegations, uri, null, uri); + + return null; + } + + + /** + * Returns a URI for an external entity. + */ + public InputSource resolve ( + boolean usingPublic, + String publicId, + String systemId + ) throws SAXException, IOException + { + boolean preferSystem; + InputSource retval; + + if (hasPreference) + preferSystem = !this.usingPublic; + else + preferSystem = !usingPublic; + + if (publicId != null) + publicId = normalizePublicId (false, publicId); + + // behavior here matches section 7.1.1 of the oasis spec + if (systemId != null) { + if (systemId.startsWith ("urn:publicid:")) { + String temp = normalizePublicId (true, systemId); + if (publicId == null) { + publicId = temp; + systemId = null; + } else if (!publicId.equals (temp)) { + // error; ok to recover by: + systemId = null; + } + } else + systemId = normalizeURI (systemId); + } + + if (systemId == null && publicId == null) + return null; + + if (systemId != null) { + retval = mapURI (systemId, systemIds, systemRewrites, + systemDelegations); + if (retval != null) { + retval.setPublicId (publicId); + return retval; + } + } + + if (publicId != null + && !(systemId != null && preferSystem)) { + retval = locatePublicId (publicId); + if (retval != null) { + retval.setPublicId (publicId); + return retval; + } + } + + // 7. apply nextCatalog entries + if (next != null) { + int length = next.size (); + for (int i = 0; i < length; i++) { + Catalog n = getNext (i); + retval = n.resolve (usingPublic, publicId, systemId); + if (retval != null) + return retval; + } + } + + return null; + } + + /** + * Maps one URI into another, for resources that are not defined + * using XML external entity or notation syntax. + */ + public InputSource resolveURI (String uri) + throws SAXException, IOException + { + if (uri.startsWith ("urn:publicid:")) + return resolve (true, normalizePublicId (true, uri), null); + + InputSource retval; + + uri = normalizeURI (uri); + + // 7.2.2 steps 2-4 + retval = mapURI (uri, uris, uriRewrites, uriDelegations); + if (retval != null) + return retval; + + // 7.2.2 step 5. apply nextCatalog entries + if (next != null) { + int length = next.size (); + for (int i = 0; i < length; i++) { + Catalog n = getNext (i); + retval = n.resolveURI (uri); + if (retval != null) + return retval; + } + } + + return null; + } + + + /** + * Finds the external subset associated with a given root element. + */ + public InputSource getExternalSubset (String name) + throws SAXException, IOException + { + if (doctypes != null) { + String value = (String) doctypes.get (name); + if (value != null) { + // IF the URI is accessible ... + return new InputSource (value); + } + } + if (next != null) { + int length = next.size (); + for (int i = 0; i < length; i++) { + Catalog n = getNext (i); + if (n == null) + continue; + InputSource retval = n.getExternalSubset (name); + if (retval != null) + return retval; + } + } + return null; + } + + private synchronized Catalog getNext (int i) + throws SAXException, IOException + { + Object obj; + + if (next == null || i < 0 || i >= next.size ()) + return null; + obj = next.elementAt (i); + if (obj instanceof Catalog) + return (Catalog) obj; + + // ok, we deferred reading that catalog till now. + // load and cache it. + Catalog cat = null; + + try { + cat = loadCatalog (parserClass, eh, (String) obj, unified); + next.setElementAt (cat, i); + } catch (SAXException e) { + // must fail quietly, says the OASIS spec + } catch (IOException e) { + // same applies here + } + return cat; + } + + private InputSource checkDelegations ( + Hashtable delegations, + String id, + String publicId, // only one of public/system + String systemId // will be non-null... + ) throws SAXException, IOException + { + Vector matches = null; + int length = 0; + + // first, see if any prefixes match. + for (Enumeration e = delegations.keys (); + e.hasMoreElements (); + /* NOP */) { + String prefix = (String) e.nextElement (); + + if (!id.startsWith (prefix)) + continue; + if (matches == null) + matches = new Vector (); + + // maintain in longer->shorter sorted order + // NOTE: assumes not many matches will fire! + int index; + + for (index = 0; index < length; index++) { + String temp = (String) matches.elementAt (index); + if (prefix.length () > temp.length ()) { + matches.insertElementAt (prefix, index); + break; + } + } + if (index == length) + matches.addElement (prefix); + length++; + } + if (matches == null) + return null; + + // now we know the list of catalogs to replace our "top level" + // list ... we use it here, rather than somehow going back and + // restarting, since this helps avoid reading most catalogs. + // this assumes stackspace won't be a problem. + for (int i = 0; i < length; i++) { + Catalog catalog = null; + InputSource result; + + // get this catalog. we may not have read it yet. + synchronized (delegations) { + Object prefix = matches.elementAt (i); + Object cat = delegations.get (prefix); + + if (cat instanceof Catalog) + catalog = (Catalog) cat; + else { + try { + // load and cache that catalog + catalog = loadCatalog (parserClass, eh, + (String) cat, unified); + delegations.put (prefix, catalog); + } catch (SAXException e) { + // must ignore, says the OASIS spec + } catch (IOException e) { + // same applies here + } + } + } + + // ignore failed loads, and proceed + if (catalog == null) + continue; + + // we have a catalog ... resolve! + // usingPublic value can't matter, there's no choice + result = catalog.resolve (true, publicId, systemId); + if (result != null) + return result; + } + + // if there were no successes, the entire + // lookup failed (all the way to top level) + throw new DoneDelegation (); + } + } + + + /** This is the namespace URI used for OASIS XML Catalogs. */ + private static final String catalogNamespace = + "urn:oasis:names:tc:entity:xmlns:xml:catalog"; + + + /** + * Loads/unmarshals one catalog. + */ + private static class Loader extends DefaultHandler2 + { + private boolean preInterned; + private ErrorHandler handler; + private boolean unified; + private int ignoreDepth; + private Locator locator; + private boolean started; + private Hashtable externals; + private Stack bases; + + Catalog cat = new Catalog (); + + + /** + * Constructor. + * @param flag true iff the parser already interns strings. + * @param eh Errors and warnings are delegated to this. + * @param unified true keeps one table for URI mappings; + * false matches OASIS spec, storing mappings + * for URIs and SYSTEM ids in parallel tables. + */ + Loader (boolean flag, ErrorHandler eh, boolean unified) + { + preInterned = flag; + handler = eh; + this.unified = unified; + cat.unified = unified; + cat.eh = eh; + } + + + // strips out fragments + private String nofrag (String uri) + throws SAXException + { + if (uri.indexOf ('#') != -1) { + warn ("URI with fragment: " + uri); + uri = uri.substring (0, uri.indexOf ('#')); + } + return uri; + } + + // absolutizes relative URIs + private String absolutize (String uri) + throws SAXException + { + // avoid creating URLs if they're already absolutized, + // or if the URI is already using a known scheme + if (uri.startsWith ("file:/") + || uri.startsWith ("http:/") + || uri.startsWith ("https:/") + || uri.startsWith ("ftp:/") + || uri.startsWith ("urn:") + ) + return uri; + + // otherwise, let's hope the JDK handles this URI scheme. + try { + URL base = (URL) bases.peek (); + return new URL (base, uri).toString (); + } catch (Exception e) { + fatal ("can't absolutize URI: " + uri); + return null; + } + } + + // recoverable error + private void error (String message) + throws SAXException + { + if (handler == null) + return; + handler.error (new SAXParseException (message, locator)); + } + + // nonrecoverable error + private void fatal (String message) + throws SAXException + { + SAXParseException spe; + + spe = new SAXParseException (message, locator); + if (handler != null) + handler.fatalError (spe); + throw spe; + } + + // low severity problem + private void warn (String message) + throws SAXException + { + if (handler == null) + return; + handler.warning (new SAXParseException (message, locator)); + } + + // callbacks: + + public void setDocumentLocator (Locator l) + { locator = l; } + + public void startDocument () + throws SAXException + { + if (locator == null) + error ("no locator!"); + bases = new Stack (); + String uri = locator.getSystemId (); + try { + bases.push (new URL (uri)); + } catch (IOException e) { + fatal ("bad document base URI: " + uri); + } + } + + public void endDocument () + throws SAXException + { + try { + if (!started) + error ("not a catalog!"); + } finally { + locator = null; + handler = null; + externals = null; + bases = null; + } + } + + // XML Base support for external entities. + + // NOTE: expects parser is in default "resolve-dtd-uris" mode. + public void externalEntityDecl (String name, String pub, String sys) + throws SAXException + { + if (externals == null) + externals = new Hashtable (); + if (externals.get (name) == null) + externals.put (name, pub); + } + + public void startEntity (String name) + throws SAXException + { + if (externals == null) + return; + String uri = (String) externals.get (name); + + // NOTE: breaks if an EntityResolver substitutes these URIs. + // If toplevel loader supports one, must intercept calls... + if (uri != null) { + try { + bases.push (new URL (uri)); + } catch (IOException e) { + fatal ("entity '" + name + "', bad URI: " + uri); + } + } + } + + public void endEntity (String name) + { + if (externals == null) + return; + String value = (String) externals.get (name); + + if (value != null) + bases.pop (); + } + + /** + * Processes catalog elements, saving their data. + */ + public void startElement (String namespace, String local, + String qName, Attributes atts) + throws SAXException + { + // must ignore non-catalog elements, and their contents + if (ignoreDepth != 0 || !catalogNamespace.equals (namespace)) { + ignoreDepth++; + return; + } + + // basic sanity checks + if (!preInterned) + local = local.intern (); + if (!started) { + started = true; + if ("catalog" != local) + fatal ("root element not 'catalog': " + local); + } + + // Handle any xml:base attribute + String xmlbase = atts.getValue ("xml:base"); + + if (xmlbase != null) { + URL base = (URL) bases.peek (); + try { + base = new URL (base, xmlbase); + } catch (IOException e) { + fatal ("can't resolve xml:base attribute: " + xmlbase); + } + bases.push (base); + } else + bases.push (bases.peek ()); + + // fetch multi-element attributes, apply standard tweaks + // values (uri, catalog, rewritePrefix) get normalized too, + // as a precaution and since we may compare the values + String catalog = atts.getValue ("catalog"); + if (catalog != null) + catalog = normalizeURI (absolutize (catalog)); + + String rewritePrefix = atts.getValue ("rewritePrefix"); + if (rewritePrefix != null) + rewritePrefix = normalizeURI (absolutize (rewritePrefix)); + + String systemIdStartString; + systemIdStartString = atts.getValue ("systemIdStartString"); + if (systemIdStartString != null) { + systemIdStartString = normalizeURI (systemIdStartString); + // unmatchable <rewriteSystemId>, <delegateSystemId> elements + if (systemIdStartString.startsWith ("urn:publicid:")) { + error ("systemIdStartString is really a publicId!!"); + return; + } + } + + String uri = atts.getValue ("uri"); + if (uri != null) + uri = normalizeURI (absolutize (uri)); + + String uriStartString; + uriStartString = atts.getValue ("uriStartString"); + if (uriStartString != null) { + uriStartString = normalizeURI (uriStartString); + // unmatchable <rewriteURI>, <delegateURI> elements + if (uriStartString.startsWith ("urn:publicid:")) { + error ("uriStartString is really a publicId!!"); + return; + } + } + + // strictly speaking "group" and "catalog" shouldn't nest + // ... arbitrary restriction, no evident motivation + +// FIXME stack "prefer" settings (two elements only!) and use +// them to populate different public mapping/delegation tables + + if ("catalog" == local || "group" == local) { + String prefer = atts.getValue ("prefer"); + + if (prefer != null && !"public".equals (prefer)) { + if (!"system".equals (prefer)) { + error ("in <" + local + " ... prefer='...'>, " + + "assuming 'public'"); + prefer = "public"; + } + } + if (prefer != null) { + if ("catalog" == local) { + cat.hasPreference = true; + cat.usingPublic = "public".equals (prefer); + } else { + if (!cat.hasPreference || cat.usingPublic + != "public".equals (prefer)) { +fatal ("<group prefer=...> case not handled"); + } + } + } else if ("group" == local && cat.hasPreference) { +fatal ("<group prefer=...> case not handled"); + } + + // + // PUBLIC ids: cleanly set up for id substitution + // + } else if ("public" == local) { + String publicId = atts.getValue ("publicId"); + String value = null; + + if (publicId == null || uri == null) { + error ("expecting <public publicId=... uri=.../>"); + return; + } + publicId = normalizePublicId (true, publicId); + uri = nofrag (uri); + if (cat.publicIds == null) + cat.publicIds = new Hashtable (); + else + value = (String) cat.publicIds.get (publicId); + if (value != null) { + if (!value.equals (uri)) + warn ("ignoring <public...> entry for " + publicId); + } else + cat.publicIds.put (publicId, uri); + + } else if ("delegatePublic" == local) { + String publicIdStartString; + Object value = null; + + publicIdStartString = atts.getValue ("publicIdStartString"); + if (publicIdStartString == null || catalog == null) { + error ("expecting <delegatePublic " + + "publicIdStartString=... catalog=.../>"); + return; + } + publicIdStartString = normalizePublicId (true, + publicIdStartString); + if (cat.publicDelegations == null) + cat.publicDelegations = new Hashtable (); + else + value = cat.publicDelegations.get (publicIdStartString); + if (value != null) { + if (!value.equals (catalog)) + warn ("ignoring <delegatePublic...> entry for " + + uriStartString); + } else + cat.publicDelegations.put (publicIdStartString, catalog); + + + // + // SYSTEM ids: need substitution due to operational issues + // + } else if ("system" == local) { + String systemId = atts.getValue ("systemId"); + String value = null; + + if (systemId == null || uri == null) { + error ("expecting <system systemId=... uri=.../>"); + return; + } + systemId = normalizeURI (systemId); + uri = nofrag (uri); + if (systemId.startsWith ("urn:publicid:")) { + error ("systemId is really a publicId!!"); + return; + } + if (cat.systemIds == null) { + cat.systemIds = new Hashtable (); + if (unified) + cat.uris = cat.systemIds; + } else + value = (String) cat.systemIds.get (systemId); + if (value != null) { + if (!value.equals (uri)) + warn ("ignoring <system...> entry for " + systemId); + } else + cat.systemIds.put (systemId, uri); + + } else if ("rewriteSystem" == local) { + String value = null; + + if (systemIdStartString == null || rewritePrefix == null + || systemIdStartString.length () == 0 + || rewritePrefix.length () == 0 + ) { + error ("expecting <rewriteSystem " + + "systemIdStartString=... rewritePrefix=.../>"); + return; + } + if (cat.systemRewrites == null) { + cat.systemRewrites = new Hashtable (); + if (unified) + cat.uriRewrites = cat.systemRewrites; + } else + value = (String) cat.systemRewrites.get ( + systemIdStartString); + if (value != null) { + if (!value.equals (rewritePrefix)) + warn ("ignoring <rewriteSystem...> entry for " + + systemIdStartString); + } else + cat.systemRewrites.put (systemIdStartString, + rewritePrefix); + + } else if ("delegateSystem" == local) { + Object value = null; + + if (systemIdStartString == null || catalog == null) { + error ("expecting <delegateSystem " + + "systemIdStartString=... catalog=.../>"); + return; + } + if (cat.systemDelegations == null) { + cat.systemDelegations = new Hashtable (); + if (unified) + cat.uriDelegations = cat.systemDelegations; + } else + value = cat.systemDelegations.get (systemIdStartString); + if (value != null) { + if (!value.equals (catalog)) + warn ("ignoring <delegateSystem...> entry for " + + uriStartString); + } else + cat.systemDelegations.put (systemIdStartString, catalog); + + + // + // URI: just like "system" ID support, except that + // fragment IDs are disallowed in "system" elements. + // + } else if ("uri" == local) { + String name = atts.getValue ("name"); + String value = null; + + if (name == null || uri == null) { + error ("expecting <uri name=... uri=.../>"); + return; + } + if (name.startsWith ("urn:publicid:")) { + error ("name is really a publicId!!"); + return; + } + name = normalizeURI (name); + if (cat.uris == null) { + cat.uris = new Hashtable (); + if (unified) + cat.systemIds = cat.uris; + } else + value = (String) cat.uris.get (name); + if (value != null) { + if (!value.equals (uri)) + warn ("ignoring <uri...> entry for " + name); + } else + cat.uris.put (name, uri); + + } else if ("rewriteURI" == local) { + String value = null; + + if (uriStartString == null || rewritePrefix == null + || uriStartString.length () == 0 + || rewritePrefix.length () == 0 + ) { + error ("expecting <rewriteURI " + + "uriStartString=... rewritePrefix=.../>"); + return; + } + if (cat.uriRewrites == null) { + cat.uriRewrites = new Hashtable (); + if (unified) + cat.systemRewrites = cat.uriRewrites; + } else + value = (String) cat.uriRewrites.get (uriStartString); + if (value != null) { + if (!value.equals (rewritePrefix)) + warn ("ignoring <rewriteURI...> entry for " + + uriStartString); + } else + cat.uriRewrites.put (uriStartString, rewritePrefix); + + } else if ("delegateURI" == local) { + Object value = null; + + if (uriStartString == null || catalog == null) { + error ("expecting <delegateURI " + + "uriStartString=... catalog=.../>"); + return; + } + if (cat.uriDelegations == null) { + cat.uriDelegations = new Hashtable (); + if (unified) + cat.systemDelegations = cat.uriDelegations; + } else + value = cat.uriDelegations.get (uriStartString); + if (value != null) { + if (!value.equals (catalog)) + warn ("ignoring <delegateURI...> entry for " + + uriStartString); + } else + cat.uriDelegations.put (uriStartString, catalog); + + // + // NON-DELEGATING approach to modularity + // + } else if ("nextCatalog" == local) { + if (catalog == null) { + error ("expecting <nextCatalog catalog=.../>"); + return; + } + if (cat.next == null) + cat.next = new Vector (); + cat.next.addElement (catalog); + + // + // EXTENSIONS from appendix E + // + } else if ("doctype" == local) { + String name = atts.getValue ("name"); + String value = null; + + if (name == null || uri == null) { + error ("expecting <doctype name=... uri=.../>"); + return; + } + name = normalizeURI (name); + if (cat.doctypes == null) + cat.doctypes = new Hashtable (); + else + value = (String) cat.doctypes.get (name); + if (value != null) { + if (!value.equals (uri)) + warn ("ignoring <doctype...> entry for " + + uriStartString); + } else + cat.doctypes.put (name, uri); + + + // + // RESERVED ... ignore (like reserved attributes) but warn + // + } else { + warn ("ignoring unknown catalog element: " + local); + ignoreDepth++; + } + } + + public void endElement (String uri, String local, String qName) + throws SAXException + { + if (ignoreDepth != 0) + ignoreDepth--; + else + bases.pop (); + } + } +} diff --git a/libjava/classpath/gnu/xml/util/XHTMLWriter.java b/libjava/classpath/gnu/xml/util/XHTMLWriter.java new file mode 100644 index 00000000000..272c66cd34c --- /dev/null +++ b/libjava/classpath/gnu/xml/util/XHTMLWriter.java @@ -0,0 +1,112 @@ +/* XHTMLWriter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; + + +/** + * This extends XMLWriter to create a class which defaults to writing + * XHTML text, preferring the US-ASCII encoding. It adds no unique + * functionality, only changing the defaults slightly to simplify writing + * XHTML processing components by providing a bean class whose properties + * have more convenient defaults. An artifact of using the US-ASCII + * encoding is that no XML declaration is written, so that HTML tools + * that can't accept them will not become confused. Components can treat + * the output as UTF-8, ISO-8859-1, or US-ASCII without incurring any + * data loss. + * + * @author David Brownell + */ +public class XHTMLWriter extends XMLWriter +{ + /** + * Constructs this handler with System.out used to write + * SAX events using the US-ASCII encoding, as XHTML. + */ + public XHTMLWriter () + throws IOException + { + this (System.out); + } + + /** + * Constructs this handler such that the specified output stream + * is used to write SAX events in the US-ASCII encoding, as XHTML. + * + * @param out Where US-ASCII encoding of the stream of SAX + * events will be sent. + */ + public XHTMLWriter (OutputStream out) + throws IOException + { + // not all JVMs understand "ASCII" as an encoding name, so + // we use 8859_1 (they all seem to handle that one) and + // make the echo handler filter out non-ASCII characters + this (new OutputStreamWriter (out, "8859_1"), "US-ASCII"); + } + + /** + * Constructs this handler such that the specified output stream + * is used to write SAX events as XHTML. + * + * @param out Where the stream of SAX events will be written. + */ + public XHTMLWriter (Writer out) + { + this (out, null); + } + + /** + * Constructs this handler such that the specified output stream + * is used to write SAX events as XHTML, labeled with the specified + * encoding. + * + * @param out Where the stream of SAX events will be written. + * @param encoding If non-null, this names the encoding to be + * placed in the encoding declaration. + */ + public XHTMLWriter (Writer out, String encoding) + { + super (out, encoding); + setXhtml (true); + } +} diff --git a/libjava/classpath/gnu/xml/util/XMLWriter.java b/libjava/classpath/gnu/xml/util/XMLWriter.java new file mode 100644 index 00000000000..fd36b715325 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/XMLWriter.java @@ -0,0 +1,1927 @@ +/* XMLWriter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.util; + +import java.io.BufferedWriter; +import java.io.CharConversionException; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Stack; + +import org.xml.sax.*; +import org.xml.sax.ext.*; +import org.xml.sax.helpers.*; + + +/** + * This class is a SAX handler which writes all its input as a well formed + * XML or XHTML document. If driven using SAX2 events, this output may + * include a recreated document type declaration, subject to limitations + * of SAX (no internal subset exposed) or DOM (the important declarations, + * with their documentation, are discarded). + * + * <p> By default, text is generated "as-is", but some optional modes + * are supported. Pretty-printing is supported, to make life easier + * for people reading the output. XHTML (1.0) output has can be made + * particularly pretty; all the built-in character entities are known. + * Canonical XML can also be generated, assuming the input is properly + * formed. + * + * <hr> + * + * <p> Some of the methods on this class are intended for applications to + * use directly, rather than as pure SAX2 event callbacks. Some of those + * methods access the JavaBeans properties (used to tweak output formats, + * for example canonicalization and pretty printing). Subclasses + * are expected to add new behaviors, not to modify current behavior, so + * many such methods are final.</p> + * + * <p> The <em>write*()</em> methods may be slightly simpler for some + * applications to use than direct callbacks. For example, they support + * a simple policy for encoding data items as the content of a single element. + * + * <p> To reuse an XMLWriter you must provide it with a new Writer, since + * this handler closes the writer it was given as part of its endDocument() + * handling. (XML documents have an end of input, and the way to encode + * that on a stream is to close it.) </p> + * + * <hr> + * + * <p> Note that any relative URIs in the source document, as found in + * entity and notation declarations, ought to have been fully resolved by + * the parser providing events to this handler. This means that the + * output text should only have fully resolved URIs, which may not be + * the desired behavior in cases where later binding is desired. </p> + * + * <p> <em>Note that due to SAX2 defaults, you may need to manually + * ensure that the input events are XML-conformant with respect to namespace + * prefixes and declarations. {@link gnu.xml.pipeline.NSFilter} is + * one solution to this problem, in the context of processing pipelines.</em> + * Something as simple as connecting this handler to a parser might not + * generate the correct output. Another workaround is to ensure that the + * <em>namespace-prefixes</em> feature is always set to true, if you're + * hooking this directly up to some XMLReader implementation. + * + * @see gnu.xml.pipeline.TextConsumer + * + * @author David Brownell + */ +public class XMLWriter + implements ContentHandler, LexicalHandler, DTDHandler, DeclHandler +{ + // text prints/escapes differently depending on context + // CTX_ENTITY ... entity literal value + // CTX_ATTRIBUTE ... attribute literal value + // CTX_CONTENT ... content of an element + // CTX_UNPARSED ... CDATA, comment, PI, names, etc + // CTX_NAME ... name or nmtoken, no escapes possible + private static final int CTX_ENTITY = 1; + private static final int CTX_ATTRIBUTE = 2; + private static final int CTX_CONTENT = 3; + private static final int CTX_UNPARSED = 4; + private static final int CTX_NAME = 5; + +// FIXME: names (element, attribute, PI, notation, etc) are not +// currently written out with range checks (escapeChars). +// In non-XHTML, some names can't be directly written; panic! + + private static String sysEOL; + + static { + try { + sysEOL = System.getProperty ("line.separator", "\n"); + + // don't use the system's EOL if it's illegal XML. + if (!isLineEnd (sysEOL)) + sysEOL = "\n"; + + } catch (SecurityException e) { + sysEOL = "\n"; + } + } + + private static boolean isLineEnd (String eol) + { + return "\n".equals (eol) + || "\r".equals (eol) + || "\r\n".equals (eol); + } + + private Writer out; + private boolean inCDATA; + private int elementNestLevel; + private String eol = sysEOL; + + private short dangerMask; + private StringBuffer stringBuf; + private Locator locator; + private ErrorHandler errHandler; + + private boolean expandingEntities = false; + private int entityNestLevel; + private boolean xhtml; + private boolean startedDoctype; + private String encoding; + + private boolean canonical; + private boolean inDoctype; + private boolean inEpilogue; + + // pretty printing controls + private boolean prettyPrinting; + private int column; + private boolean noWrap; + private Stack space = new Stack (); + + // this is not a hard'n'fast rule -- longer lines are OK, + // but are to be avoided. Here, prettyprinting is more to + // show structure "cleanly" than to be precise about it. + // better to have ragged layout than one line 24Kb long. + private static final int lineLength = 75; + + + /** + * Constructs this handler with System.out used to write SAX events + * using the UTF-8 encoding. Avoid using this except when you know + * it's safe to close System.out at the end of the document. + */ + public XMLWriter () throws IOException + { this (System.out); } + + /** + * Constructs a handler which writes all input to the output stream + * in the UTF-8 encoding, and closes it when endDocument is called. + * (Yes it's annoying that this throws an exception -- but there's + * really no way around it, since it's barely possible a JDK may + * exist somewhere that doesn't know how to emit UTF-8.) + */ + public XMLWriter (OutputStream out) throws IOException + { + this (new OutputStreamWriter (out, "UTF8")); + } + + /** + * Constructs a handler which writes all input to the writer, and then + * closes the writer when the document ends. If an XML declaration is + * written onto the output, and this class can determine the name of + * the character encoding for this writer, that encoding name will be + * included in the XML declaration. + * + * <P> See the description of the constructor which takes an encoding + * name for imporant information about selection of encodings. + * + * @param writer XML text is written to this writer. + */ + public XMLWriter (Writer writer) + { + this (writer, null); + } + + /** + * Constructs a handler which writes all input to the writer, and then + * closes the writer when the document ends. If an XML declaration is + * written onto the output, this class will use the specified encoding + * name in that declaration. If no encoding name is specified, no + * encoding name will be declared unless this class can otherwise + * determine the name of the character encoding for this writer. + * + * <P> At this time, only the UTF-8 ("UTF8") and UTF-16 ("Unicode") + * output encodings are fully lossless with respect to XML data. If you + * use any other encoding you risk having your data be silently mangled + * on output, as the standard Java character encoding subsystem silently + * maps non-encodable characters to a question mark ("?") and will not + * report such errors to applications. + * + * <p> For a few other encodings the risk can be reduced. If the writer is + * a java.io.OutputStreamWriter, and uses either the ISO-8859-1 ("8859_1", + * "ISO8859_1", etc) or US-ASCII ("ASCII") encodings, content which + * can't be encoded in those encodings will be written safely. Where + * relevant, the XHTML entity names will be used; otherwise, numeric + * character references will be emitted. + * + * <P> However, there remain a number of cases where substituting such + * entity or character references is not an option. Such references are + * not usable within a DTD, comment, PI, or CDATA section. Neither may + * they be used when element, attribute, entity, or notation names have + * the problematic characters. + * + * @param writer XML text is written to this writer. + * @param encoding if non-null, and an XML declaration is written, + * this is the name that will be used for the character encoding. + */ + public XMLWriter (Writer writer, String encoding) + { + setWriter (writer, encoding); + } + + private void setEncoding (String encoding) + { + if (encoding == null && out instanceof OutputStreamWriter) + encoding = ((OutputStreamWriter)out).getEncoding (); + + if (encoding != null) { + encoding = encoding.toUpperCase (); + + // Use official encoding names where we know them, + // avoiding the Java-only names. When using common + // encodings where we can easily tell if characters + // are out of range, we'll escape out-of-range + // characters using character refs for safety. + + // I _think_ these are all the main synonyms for these! + if ("UTF8".equals (encoding)) { + encoding = "UTF-8"; + } else if ("US-ASCII".equals (encoding) + || "ASCII".equals (encoding)) { + dangerMask = (short) 0xff80; + encoding = "US-ASCII"; + } else if ("ISO-8859-1".equals (encoding) + || "8859_1".equals (encoding) + || "ISO8859_1".equals (encoding)) { + dangerMask = (short) 0xff00; + encoding = "ISO-8859-1"; + } else if ("UNICODE".equals (encoding) + || "UNICODE-BIG".equals (encoding) + || "UNICODE-LITTLE".equals (encoding)) { + encoding = "UTF-16"; + + // TODO: UTF-16BE, UTF-16LE ... no BOM; what + // release of JDK supports those Unicode names? + } + + if (dangerMask != 0) + stringBuf = new StringBuffer (); + } + + this.encoding = encoding; + } + + + /** + * Resets the handler to write a new text document. + * + * @param writer XML text is written to this writer. + * @param encoding if non-null, and an XML declaration is written, + * this is the name that will be used for the character encoding. + * + * @exception IllegalStateException if the current + * document hasn't yet ended (with {@link #endDocument}) + */ + final public void setWriter (Writer writer, String encoding) + { + if (out != null) + throw new IllegalStateException ( + "can't change stream in mid course"); + out = writer; + if (out != null) + setEncoding (encoding); + if (!(out instanceof BufferedWriter)) + out = new BufferedWriter (out); + space.push ("default"); + } + + /** + * Assigns the line ending style to be used on output. + * @param eolString null to use the system default; else + * "\n", "\r", or "\r\n". + */ + final public void setEOL (String eolString) + { + if (eolString == null) + eol = sysEOL; + else if (!isLineEnd (eolString)) + eol = eolString; + else + throw new IllegalArgumentException (eolString); + } + + /** + * Assigns the error handler to be used to present most fatal + * errors. + */ + public void setErrorHandler (ErrorHandler handler) + { + errHandler = handler; + } + + /** + * Used internally and by subclasses, this encapsulates the logic + * involved in reporting fatal errors. It uses locator information + * for good diagnostics, if available, and gives the application's + * ErrorHandler the opportunity to handle the error before throwing + * an exception. + */ + protected void fatal (String message, Exception e) + throws SAXException + { + SAXParseException x; + + if (locator == null) + x = new SAXParseException (message, null, null, -1, -1, e); + else + x = new SAXParseException (message, locator, e); + if (errHandler != null) + errHandler.fatalError (x); + throw x; + } + + + // JavaBeans properties + + /** + * Controls whether the output should attempt to follow the "transitional" + * XHTML rules so that it meets the "HTML Compatibility Guidelines" + * appendix in the XHTML specification. A "transitional" Document Type + * Declaration (DTD) is placed near the beginning of the output document, + * instead of whatever DTD would otherwise have been placed there, and + * XHTML empty elements are printed specially. When writing text in + * US-ASCII or ISO-8859-1 encodings, the predefined XHTML internal + * entity names are used (in preference to character references) when + * writing content characters which can't be expressed in those encodings. + * + * <p> When this option is enabled, it is the caller's responsibility + * to ensure that the input is otherwise valid as XHTML. Things to + * be careful of in all cases, as described in the appendix referenced + * above, include: <ul> + * + * <li> Element and attribute names must be in lower case, both + * in the document and in any CSS style sheet. + * <li> All XML constructs must be valid as defined by the XHTML + * "transitional" DTD (including all familiar constructs, + * even deprecated ones). + * <li> The root element must be "html". + * <li> Elements that must be empty (such as <em><br></em> + * must have no content. + * <li> Use both <em>lang</em> and <em>xml:lang</em> attributes + * when specifying language. + * <li> Similarly, use both <em>id</em> and <em>name</em> attributes + * when defining elements that may be referred to through + * URI fragment identifiers ... and make sure that the + * value is a legal NMTOKEN, since not all such HTML 4.0 + * identifiers are valid in XML. + * <li> Be careful with character encodings; make sure you provide + * a <em><meta http-equiv="Content-type" + * content="text/xml;charset=..." /></em> element in + * the HTML "head" element, naming the same encoding + * used to create this handler. Also, if that encoding + * is anything other than US-ASCII, make sure that if + * the document is given a MIME content type, it has + * a <em>charset=...</em> attribute with that encoding. + * </ul> + * + * <p> Additionally, some of the oldest browsers have additional + * quirks, to address with guidelines such as: <ul> + * + * <li> Processing instructions may be rendered, so avoid them. + * (Similarly for an XML declaration.) + * <li> Embedded style sheets and scripts should not contain XML + * markup delimiters: &, <, and ]]> are trouble. + * <li> Attribute values should not have line breaks or multiple + * consecutive white space characters. + * <li> Use no more than one of the deprecated (transitional) + * <em><isindex></em> elements. + * <li> Some boolean attributes (such as <em>compact, checked, + * disabled, readonly, selected,</em> and more) confuse + * some browsers, since they only understand minimized + * versions which are illegal in XML. + * </ul> + * + * <p> Also, some characteristics of the resulting output may be + * a function of whether the document is later given a MIME + * content type of <em>text/html</em> rather than one indicating + * XML (<em>application/xml</em> or <em>text/xml</em>). Worse, + * some browsers ignore MIME content types and prefer to rely URI + * name suffixes -- so an "index.xml" could always be XML, never + * XHTML, no matter its MIME type. + */ + final public void setXhtml (boolean value) + { + if (locator != null) + throw new IllegalStateException ("started parsing"); + xhtml = value; + if (xhtml) + canonical = false; + } + + /** + * Returns true if the output attempts to echo the input following + * "transitional" XHTML rules and matching the "HTML Compatibility + * Guidelines" so that an HTML version 3 browser can read the output + * as HTML; returns false (the default) othewise. + */ + final public boolean isXhtml () + { + return xhtml; + } + + /** + * Controls whether the output text contains references to + * entities (the default), or instead contains the expanded + * values of those entities. + */ + final public void setExpandingEntities (boolean value) + { + if (locator != null) + throw new IllegalStateException ("started parsing"); + expandingEntities = value; + if (!expandingEntities) + canonical = false; + } + + /** + * Returns true if the output will have no entity references; + * returns false (the default) otherwise. + */ + final public boolean isExpandingEntities () + { + return expandingEntities; + } + + /** + * Controls pretty-printing, which by default is not enabled + * (and currently is most useful for XHTML output). + * Pretty printing enables structural indentation, sorting of attributes + * by name, line wrapping, and potentially other mechanisms for making + * output more or less readable. + * + * <p> At this writing, structural indentation and line wrapping are + * enabled when pretty printing is enabled and the <em>xml:space</em> + * attribute has the value <em>default</em> (its other legal value is + * <em>preserve</em>, as defined in the XML specification). The three + * XHTML element types which use another value are recognized by their + * names (namespaces are ignored). + * + * <p> Also, for the record, the "pretty" aspect of printing here + * is more to provide basic structure on outputs that would otherwise + * risk being a single long line of text. For now, expect the + * structure to be ragged ... unless you'd like to submit a patch + * to make this be more strictly formatted! + * + * @exception IllegalStateException thrown if this method is invoked + * after output has begun. + */ + final public void setPrettyPrinting (boolean value) + { + if (locator != null) + throw new IllegalStateException ("started parsing"); + prettyPrinting = value; + if (prettyPrinting) + canonical = false; + } + + /** + * Returns value of flag controlling pretty printing. + */ + final public boolean isPrettyPrinting () + { + return prettyPrinting; + } + + + /** + * Sets the output style to be canonicalized. Input events must + * meet requirements that are slightly more stringent than the + * basic well-formedness ones, and include: <ul> + * + * <li> Namespace prefixes must not have been changed from those + * in the original document. (This may only be ensured by setting + * the SAX2 XMLReader <em>namespace-prefixes</em> feature flag; + * by default, it is cleared.) + * + * <li> Redundant namespace declaration attributes have been + * removed. (If an ancestor element defines a namespace prefix + * and that declaration hasn't been overriden, an element must + * not redeclare it.) + * + * <li> If comments are not to be included in the canonical output, + * they must first be removed from the input event stream; this + * <em>Canonical XML with comments</em> by default. + * + * <li> If the input character encoding was not UCS-based, the + * character data must have been normalized using Unicode + * Normalization Form C. (UTF-8 and UTF-16 are UCS-based.) + * + * <li> Attribute values must have been normalized, as is done + * by any conformant XML processor which processes all external + * parameter entities. + * + * <li> Similarly, attribute value defaulting has been performed. + * + * </ul> + * + * <p> Note that fragments of XML documents, as specified by an XPath + * node set, may be canonicalized. In such cases, elements may need + * some fixup (for <em>xml:*</em> attributes and application-specific + * context). + * + * @exception IllegalArgumentException if the output encoding + * is anything other than UTF-8. + */ + final public void setCanonical (boolean value) + { + if (value && !"UTF-8".equals (encoding)) + throw new IllegalArgumentException ("encoding != UTF-8"); + canonical = value; + if (canonical) { + prettyPrinting = xhtml = false; + expandingEntities = true; + eol = "\n"; + } + } + + + /** + * Returns value of flag controlling canonical output. + */ + final public boolean isCanonical () + { + return canonical; + } + + + /** + * Flushes the output stream. When this handler is used in long lived + * pipelines, it can be important to flush buffered state, for example + * so that it can reach the disk as part of a state checkpoint. + */ + final public void flush () + throws IOException + { + if (out != null) + out.flush (); + } + + + // convenience routines + +// FIXME: probably want a subclass that holds a lot of these... +// and maybe more! + + /** + * Writes the string as if characters() had been called on the contents + * of the string. This is particularly useful when applications act as + * producers and write data directly to event consumers. + */ + final public void write (String data) + throws SAXException + { + char buf [] = data.toCharArray (); + characters (buf, 0, buf.length); + } + + + /** + * Writes an element that has content consisting of a single string. + * @see #writeEmptyElement + * @see #startElement + */ + public void writeElement ( + String uri, + String localName, + String qName, + Attributes atts, + String content + ) throws SAXException + { + if (content == null || content.length () == 0) { + writeEmptyElement (uri, localName, qName, atts); + return; + } + startElement (uri, localName, qName, atts); + char chars [] = content.toCharArray (); + characters (chars, 0, chars.length); + endElement (uri, localName, qName); + } + + + /** + * Writes an element that has content consisting of a single integer, + * encoded as a decimal string. + * @see #writeEmptyElement + * @see #startElement + */ + public void writeElement ( + String uri, + String localName, + String qName, + Attributes atts, + int content + ) throws SAXException + { + writeElement (uri, localName, qName, atts, Integer.toString (content)); + } + + + // SAX1 ContentHandler + /** <b>SAX1</b>: provides parser status information */ + final public void setDocumentLocator (Locator l) + { + locator = l; + } + + + // URL for dtd that validates against all normal HTML constructs + private static final String xhtmlFullDTD = + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"; + + + /** + * <b>SAX1</b>: indicates the beginning of a document parse. + * If you're writing (well formed) fragments of XML, neither + * this nor endDocument should be called. + */ + // NOT final + public void startDocument () + throws SAXException + { + try { + if (out == null) + throw new IllegalStateException ( + "null Writer given to XMLWriter"); + + // Not all parsers provide the locator we want; this also + // flags whether events are being sent to this object yet. + // We could only have this one call if we only printed whole + // documents ... but we also print fragments, so most of the + // callbacks here replicate this test. + + if (locator == null) + locator = new LocatorImpl (); + + // Unless the data is in US-ASCII or we're canonicalizing, write + // the XML declaration if we know the encoding. US-ASCII won't + // normally get mangled by web server confusion about the + // character encodings used. Plus, it's an easy way to + // ensure we can write ASCII that's unlikely to confuse + // elderly HTML parsers. + + if (!canonical + && dangerMask != (short) 0xff80 + && encoding != null) { + rawWrite ("<?xml version='1.0'"); + rawWrite (" encoding='" + encoding + "'"); + rawWrite ("?>"); + newline (); + } + + if (xhtml) { + + rawWrite ("<!DOCTYPE html PUBLIC"); + newline (); + rawWrite (" '-//W3C//DTD XHTML 1.0 Transitional//EN'"); + newline (); + rawWrite (" '"); + // NOTE: URL (above) matches the REC + rawWrite (xhtmlFullDTD); + rawWrite ("'>"); + newline (); + newline (); + + // fake the rest of the handler into ignoring + // everything until the root element, so any + // XHTML DTD comments, PIs, etc are ignored + startedDoctype = true; + } + + entityNestLevel = 0; + + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** + * <b>SAX1</b>: indicates the completion of a parse. + * Note that all complete SAX event streams make this call, even + * if an error is reported during a parse. + */ + // NOT final + public void endDocument () + throws SAXException + { + try { + if (!canonical) { + newline (); + newline (); + } + out.close (); + out = null; + locator = null; + } catch (IOException e) { + fatal ("can't write", e); + } + } + + // XHTML elements declared as EMPTY print differently + final private static boolean isEmptyElementTag (String tag) + { + switch (tag.charAt (0)) { + case 'a': return "area".equals (tag); + case 'b': return "base".equals (tag) + || "basefont".equals (tag) + || "br".equals (tag); + case 'c': return "col".equals (tag); + case 'f': return "frame".equals (tag); + case 'h': return "hr".equals (tag); + case 'i': return "img".equals (tag) + || "input".equals (tag) + || "isindex".equals (tag); + case 'l': return "link".equals (tag); + case 'm': return "meta".equals (tag); + case 'p': return "param".equals (tag); + } + return false; + } + + private static boolean indentBefore (String tag) + { + // basically indent before block content + // and within structure like tables, lists + switch (tag.charAt (0)) { + case 'a': return "applet".equals (tag); + case 'b': return "body".equals (tag) + || "blockquote".equals (tag); + case 'c': return "center".equals (tag); + case 'f': return "frame".equals (tag) + || "frameset".equals (tag); + case 'h': return "head".equals (tag); + case 'm': return "meta".equals (tag); + case 'o': return "object".equals (tag); + case 'p': return "param".equals (tag) + || "pre".equals (tag); + case 's': return "style".equals (tag); + case 't': return "title".equals (tag) + || "td".equals (tag) + || "th".equals (tag); + } + // ... but not inline elements like "em", "b", "font" + return false; + } + + private static boolean spaceBefore (String tag) + { + // blank line AND INDENT before certain structural content + switch (tag.charAt (0)) { + case 'h': return "h1".equals (tag) + || "h2".equals (tag) + || "h3".equals (tag) + || "h4".equals (tag) + || "h5".equals (tag) + || "h6".equals (tag) + || "hr".equals (tag); + case 'l': return "li".equals (tag); + case 'o': return "ol".equals (tag); + case 'p': return "p".equals (tag); + case 't': return "table".equals (tag) + || "tr".equals (tag); + case 'u': return "ul".equals (tag); + } + return false; + } + + // XHTML DTDs say these three have xml:space="preserve" + private static boolean spacePreserve (String tag) + { + return "pre".equals (tag) + || "style".equals (tag) + || "script".equals (tag); + } + + /** + * <b>SAX2</b>: ignored. + */ + final public void startPrefixMapping (String prefix, String uri) + {} + + /** + * <b>SAX2</b>: ignored. + */ + final public void endPrefixMapping (String prefix) + {} + + private void writeStartTag ( + String name, + Attributes atts, + boolean isEmpty + ) throws SAXException, IOException + { + rawWrite ('<'); + rawWrite (name); + + // write out attributes ... sorting is particularly useful + // with output that's been heavily defaulted. + if (atts != null && atts.getLength () != 0) { + + // Set up to write, with optional sorting + int indices [] = new int [atts.getLength ()]; + + for (int i= 0; i < indices.length; i++) + indices [i] = i; + + // optionally sort + +// FIXME: canon xml demands xmlns nodes go first, +// and sorting by URI first (empty first) then localname +// it should maybe use a different sort + + if (canonical || prettyPrinting) { + + // insertion sort by attribute name + for (int i = 1; i < indices.length; i++) { + int n = indices [i], j; + String s = atts.getQName (n); + + for (j = i - 1; j >= 0; j--) { + if (s.compareTo (atts.getQName (indices [j])) + >= 0) + break; + indices [j + 1] = indices [j]; + } + indices [j + 1] = n; + } + } + + // write, sorted or no + for (int i= 0; i < indices.length; i++) { + String s = atts.getQName (indices [i]); + + if (s == null || "".equals (s)) + throw new IllegalArgumentException ("no XML name"); + rawWrite (" "); + rawWrite (s); + rawWrite ("="); + writeQuotedValue (atts.getValue (indices [i]), + CTX_ATTRIBUTE); + } + } + if (isEmpty) + rawWrite (" /"); + rawWrite ('>'); + } + + /** + * <b>SAX2</b>: indicates the start of an element. + * When XHTML is in use, avoid attribute values with + * line breaks or multiple whitespace characters, since + * not all user agents handle them correctly. + */ + final public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + startedDoctype = false; + + if (locator == null) + locator = new LocatorImpl (); + + if (qName == null || "".equals (qName)) + throw new IllegalArgumentException ("no XML name"); + + try { + if (entityNestLevel != 0) + return; + if (prettyPrinting) { + String whitespace = null; + + if (xhtml && spacePreserve (qName)) + whitespace = "preserve"; + else if (atts != null) + whitespace = atts.getValue ("xml:space"); + if (whitespace == null) + whitespace = (String) space.peek (); + space.push (whitespace); + + if ("default".equals (whitespace)) { + if (xhtml) { + if (spaceBefore (qName)) { + newline (); + doIndent (); + } else if (indentBefore (qName)) + doIndent (); + // else it's inlined, modulo line length + // FIXME: incrementing element nest level + // for inlined elements causes ugliness + } else + doIndent (); + } + } + elementNestLevel++; + writeStartTag (qName, atts, xhtml && isEmptyElementTag (qName)); + + if (xhtml) { +// FIXME: if this is an XHTML "pre" element, turn +// off automatic wrapping. + } + + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** + * Writes an empty element. + * @see #startElement + */ + public void writeEmptyElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + if (canonical) { + startElement (uri, localName, qName, atts); + endElement (uri, localName, qName); + } else { + try { + writeStartTag (qName, atts, true); + } catch (IOException e) { + fatal ("can't write", e); + } + } + } + + + /** <b>SAX2</b>: indicates the end of an element */ + final public void endElement (String uri, String localName, String qName) + throws SAXException + { + if (qName == null || "".equals (qName)) + throw new IllegalArgumentException ("no XML name"); + + try { + elementNestLevel--; + if (entityNestLevel != 0) + return; + if (xhtml && isEmptyElementTag (qName)) + return; + rawWrite ("</"); + rawWrite (qName); + rawWrite ('>'); + + if (prettyPrinting) { + if (!space.empty ()) + space.pop (); + else + fatal ("stack discipline", null); + } + if (elementNestLevel == 0) + inEpilogue = true; + + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX1</b>: reports content characters */ + final public void characters (char ch [], int start, int length) + throws SAXException + { + if (locator == null) + locator = new LocatorImpl (); + + try { + if (entityNestLevel != 0) + return; + if (inCDATA) { + escapeChars (ch, start, length, CTX_UNPARSED); + } else { + escapeChars (ch, start, length, CTX_CONTENT); + } + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX1</b>: reports ignorable whitespace */ + final public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + if (locator == null) + locator = new LocatorImpl (); + + try { + if (entityNestLevel != 0) + return; + // don't forget to map NL to CRLF, CR, etc + escapeChars (ch, start, length, CTX_CONTENT); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** + * <b>SAX1</b>: reports a PI. + * This doesn't check for illegal target names, such as "xml" or "XML", + * or namespace-incompatible ones like "big:dog"; the caller is + * responsible for ensuring those names are legal. + */ + final public void processingInstruction (String target, String data) + throws SAXException + { + if (locator == null) + locator = new LocatorImpl (); + + // don't print internal subset for XHTML + if (xhtml && startedDoctype) + return; + + // ancient HTML browsers might render these ... their loss. + // to prevent: "if (xhtml) return;". + + try { + if (entityNestLevel != 0) + return; + if (canonical && inEpilogue) + newline (); + rawWrite ("<?"); + rawWrite (target); + rawWrite (' '); + escapeChars (data.toCharArray (), -1, -1, CTX_UNPARSED); + rawWrite ("?>"); + if (elementNestLevel == 0 && !(canonical && inEpilogue)) + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX1</b>: indicates a non-expanded entity reference */ + public void skippedEntity (String name) + throws SAXException + { + try { + rawWrite ("&"); + rawWrite (name); + rawWrite (";"); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + // SAX2 LexicalHandler + + /** <b>SAX2</b>: called before parsing CDATA characters */ + final public void startCDATA () + throws SAXException + { + if (locator == null) + locator = new LocatorImpl (); + + if (canonical) + return; + + try { + inCDATA = true; + if (entityNestLevel == 0) + rawWrite ("<![CDATA["); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX2</b>: called after parsing CDATA characters */ + final public void endCDATA () + throws SAXException + { + if (canonical) + return; + + try { + inCDATA = false; + if (entityNestLevel == 0) + rawWrite ("]]>"); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** + * <b>SAX2</b>: called when the doctype is partially parsed + * Note that this, like other doctype related calls, is ignored + * when XHTML is in use. + */ + final public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (locator == null) + locator = new LocatorImpl (); + if (xhtml) + return; + try { + inDoctype = startedDoctype = true; + if (canonical) + return; + rawWrite ("<!DOCTYPE "); + rawWrite (name); + rawWrite (' '); + + if (!expandingEntities) { + if (publicId != null) + rawWrite ("PUBLIC '" + publicId + "' '" + systemId + "' "); + else if (systemId != null) + rawWrite ("SYSTEM '" + systemId + "' "); + } + + rawWrite ('['); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX2</b>: called after the doctype is parsed */ + final public void endDTD () + throws SAXException + { + inDoctype = false; + if (canonical || xhtml) + return; + try { + rawWrite ("]>"); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** + * <b>SAX2</b>: called before parsing a general entity in content + */ + final public void startEntity (String name) + throws SAXException + { + try { + boolean writeEOL = true; + + // Predefined XHTML entities (for characters) will get + // mapped back later. + if (xhtml || expandingEntities) + return; + + entityNestLevel++; + if (name.equals ("[dtd]")) + return; + if (entityNestLevel != 1) + return; + if (!name.startsWith ("%")) { + writeEOL = false; + rawWrite ('&'); + } + rawWrite (name); + rawWrite (';'); + if (writeEOL) + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** + * <b>SAX2</b>: called after parsing a general entity in content + */ + final public void endEntity (String name) + throws SAXException + { + if (xhtml || expandingEntities) + return; + entityNestLevel--; + } + + /** + * <b>SAX2</b>: called when comments are parsed. + * When XHTML is used, the old HTML tradition of using comments + * to for inline CSS, or for JavaScript code is discouraged. + * This is because XML processors are encouraged to discard, on + * the grounds that comments are for users (and perhaps text + * editors) not programs. Instead, use external scripts + */ + final public void comment (char ch [], int start, int length) + throws SAXException + { + if (locator == null) + locator = new LocatorImpl (); + + // don't print internal subset for XHTML + if (xhtml && startedDoctype) + return; + // don't print comment in doctype for canon xml + if (canonical && inDoctype) + return; + + try { + boolean indent; + + if (prettyPrinting && space.empty ()) + fatal ("stack discipline", null); + indent = prettyPrinting && "default".equals (space.peek ()); + if (entityNestLevel != 0) + return; + if (indent) + doIndent (); + if (canonical && inEpilogue) + newline (); + rawWrite ("<!--"); + escapeChars (ch, start, length, CTX_UNPARSED); + rawWrite ("-->"); + if (indent) + doIndent (); + if (elementNestLevel == 0 && !(canonical && inEpilogue)) + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + // SAX1 DTDHandler + + /** <b>SAX1</b>: called on notation declarations */ + final public void notationDecl (String name, + String publicId, String systemId) + throws SAXException + { + if (xhtml) + return; + try { + // At this time, only SAX2 callbacks start these. + if (!startedDoctype) + return; + + if (entityNestLevel != 0) + return; + rawWrite ("<!NOTATION " + name + " "); + if (publicId != null) + rawWrite ("PUBLIC \"" + publicId + '"'); + else + rawWrite ("SYSTEM "); + if (systemId != null) + rawWrite ('"' + systemId + '"'); + rawWrite (">"); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX1</b>: called on unparsed entity declarations */ + final public void unparsedEntityDecl (String name, + String publicId, String systemId, + String notationName) + throws SAXException + { + if (xhtml) + return; + try { + // At this time, only SAX2 callbacks start these. + if (!startedDoctype) { + // FIXME: write to temporary buffer, and make the start + // of the root element write these declarations. + return; + } + + if (entityNestLevel != 0) + return; + rawWrite ("<!ENTITY " + name + " "); + if (publicId != null) + rawWrite ("PUBLIC \"" + publicId + '"'); + else + rawWrite ("SYSTEM "); + rawWrite ('"' + systemId + '"'); + rawWrite (" NDATA " + notationName + ">"); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + // SAX2 DeclHandler + + /** <b>SAX2</b>: called on attribute declarations */ + final public void attributeDecl (String eName, String aName, + String type, String mode, String value) + throws SAXException + { + if (xhtml) + return; + try { + // At this time, only SAX2 callbacks start these. + if (!startedDoctype) + return; + if (entityNestLevel != 0) + return; + rawWrite ("<!ATTLIST " + eName + ' ' + aName + ' '); + rawWrite (type); + rawWrite (' '); + if (mode != null) + rawWrite (mode + ' '); + if (value != null) + writeQuotedValue (value, CTX_ATTRIBUTE); + rawWrite ('>'); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX2</b>: called on element declarations */ + final public void elementDecl (String name, String model) + throws SAXException + { + if (xhtml) + return; + try { + // At this time, only SAX2 callbacks start these. + if (!startedDoctype) + return; + if (entityNestLevel != 0) + return; + rawWrite ("<!ELEMENT " + name + ' ' + model + '>'); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX2</b>: called on external entity declarations */ + final public void externalEntityDecl ( + String name, + String publicId, + String systemId) + throws SAXException + { + if (xhtml) + return; + try { + // At this time, only SAX2 callbacks start these. + if (!startedDoctype) + return; + if (entityNestLevel != 0) + return; + rawWrite ("<!ENTITY "); + if (name.startsWith ("%")) { + rawWrite ("% "); + rawWrite (name.substring (1)); + } else + rawWrite (name); + if (publicId != null) + rawWrite (" PUBLIC \"" + publicId + '"'); + else + rawWrite (" SYSTEM "); + rawWrite ('"' + systemId + "\">"); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + /** <b>SAX2</b>: called on internal entity declarations */ + final public void internalEntityDecl (String name, String value) + throws SAXException + { + if (xhtml) + return; + try { + // At this time, only SAX2 callbacks start these. + if (!startedDoctype) + return; + if (entityNestLevel != 0) + return; + rawWrite ("<!ENTITY "); + if (name.startsWith ("%")) { + rawWrite ("% "); + rawWrite (name.substring (1)); + } else + rawWrite (name); + rawWrite (' '); + writeQuotedValue (value, CTX_ENTITY); + rawWrite ('>'); + newline (); + } catch (IOException e) { + fatal ("can't write", e); + } + } + + private void writeQuotedValue (String value, int code) + throws SAXException, IOException + { + char buf [] = value.toCharArray (); + int off = 0, len = buf.length; + + // we can't add line breaks to attribute/entity/... values + noWrap = true; + rawWrite ('"'); + escapeChars (buf, off, len, code); + rawWrite ('"'); + noWrap = false; + } + + // From "HTMLlat1x.ent" ... names of entities for ISO-8859-1 + // (Latin/1) characters, all codes: 160-255 (0xA0-0xFF). + // Codes 128-159 have no assigned values. + private static final String HTMLlat1x [] = { + // 160 + "nbsp", "iexcl", "cent", "pound", "curren", + "yen", "brvbar", "sect", "uml", "copy", + + // 170 + "ordf", "laquo", "not", "shy", "reg", + "macr", "deg", "plusmn", "sup2", "sup3", + + // 180 + "acute", "micro", "para", "middot", "cedil", + "sup1", "ordm", "raquo", "frac14", "frac12", + + // 190 + "frac34", "iquest", "Agrave", "Aacute", "Acirc", + "Atilde", "Auml", "Aring", "AElig", "Ccedil", + + // 200 + "Egrave", "Eacute", "Ecirc", "Euml", "Igrave", + "Iacute", "Icirc", "Iuml", "ETH", "Ntilde", + + // 210 + "Ograve", "Oacute", "Ocirc", "Otilde", "Ouml", + "times", "Oslash", "Ugrave", "Uacute", "Ucirc", + + // 220 + "Uuml", "Yacute", "THORN", "szlig", "agrave", + "aacute", "acirc", "atilde", "auml", "aring", + + // 230 + "aelig", "ccedil", "egrave", "eacute", "ecirc", + "euml", "igrave", "iacute", "icirc", "iuml", + + // 240 + "eth", "ntilde", "ograve", "oacute", "ocirc", + "otilde", "ouml", "divide", "oslash", "ugrave", + + // 250 + "uacute", "ucirc", "uuml", "yacute", "thorn", + "yuml" + }; + + // From "HTMLsymbolx.ent" ... some of the symbols that + // we can conveniently handle. Entities for the Greek. + // alphabet (upper and lower cases) are compact. + private static final String HTMLsymbolx_GR [] = { + // 913 + "Alpha", "Beta", "Gamma", "Delta", "Epsilon", + "Zeta", "Eta", "Theta", "Iota", "Kappa", + + // 923 + "Lambda", "Mu", "Nu", "Xi", "Omicron", + "Pi", "Rho", null, "Sigma", "Tau", + + // 933 + "Upsilon", "Phi", "Chi", "Psi", "Omega" + }; + + private static final String HTMLsymbolx_gr [] = { + // 945 + "alpha", "beta", "gamma", "delta", "epsilon", + "zeta", "eta", "theta", "iota", "kappa", + + // 955 + "lambda", "mu", "nu", "xi", "omicron", + "pi", "rho", "sigmaf", "sigma", "tau", + + // 965 + "upsilon", "phi", "chi", "psi", "omega" + }; + + + // General routine to write text and substitute predefined + // entities (XML, and a special case for XHTML) as needed. + private void escapeChars (char buf [], int off, int len, int code) + throws SAXException, IOException + { + int first = 0; + + if (off < 0) { + off = 0; + len = buf.length; + } + for (int i = 0; i < len; i++) { + String esc; + char c = buf [off + i]; + + switch (c) { + // Note that CTX_ATTRIBUTE isn't explicitly tested here; + // all syntax delimiters are escaped in CTX_ATTRIBUTE, + // otherwise it's similar to CTX_CONTENT + + // ampersand flags entity references; entity replacement + // text has unexpanded references, other text doesn't. + case '&': + if (code == CTX_ENTITY || code == CTX_UNPARSED) + continue; + esc = "amp"; + break; + + // attributes and text may NOT have literal '<', but + // entities may have markup constructs + case '<': + if (code == CTX_ENTITY || code == CTX_UNPARSED) + continue; + esc = "lt"; + break; + + // as above re markup constructs; but otherwise + // except when canonicalizing, this is for consistency + case '>': + if (code == CTX_ENTITY || code == CTX_UNPARSED) + continue; + esc = "gt"; + break; + case '\'': + if (code == CTX_CONTENT || code == CTX_UNPARSED) + continue; + if (canonical) + continue; + esc = "apos"; + break; + + // needed when printing quoted attribute/entity values + case '"': + if (code == CTX_CONTENT || code == CTX_UNPARSED) + continue; + esc = "quot"; + break; + + // make line ends work per host OS convention + case '\n': + esc = eol; + break; + + // + // No other characters NEED special treatment ... except + // for encoding-specific issues, like whether the character + // can really be represented in that encoding. + // + default: + // + // There are characters we can never write safely; getting + // them is an error. + // + // (a) They're never legal in XML ... detected by range + // checks, and (eventually) by remerging surrogate + // pairs on output. (Easy error for apps to prevent.) + // + // (b) This encoding can't represent them, and we + // can't make reference substitution (e.g. inside + // CDATA sections, names, PI data, etc). (Hard for + // apps to prevent, except by using UTF-8 or UTF-16 + // as their output encoding.) + // + // We know a very little bit about what characters + // the US-ASCII and ISO-8859-1 encodings support. For + // other encodings we can't detect the second type of + // error at all. (Never an issue for UTF-8 or UTF-16.) + // + +// FIXME: CR in CDATA is an error; in text, turn to a char ref + +// FIXME: CR/LF/TAB in attributes should become char refs + + if ((c > 0xfffd) + || ((c < 0x0020) && !((c == 0x0009) + || (c == 0x000A) || (c == 0x000D))) + || (((c & dangerMask) != 0) + && (code == CTX_UNPARSED))) { + + // if case (b) in CDATA, we might end the section, + // write a reference, then restart ... possible + // in one DOM L3 draft. + + throw new CharConversionException ( + "Illegal or non-writable character: U+" + + Integer.toHexString (c)); + } + + // + // If the output encoding represents the character + // directly, let it do so! Else we'll escape it. + // + if ((c & dangerMask) == 0) + continue; + esc = null; + + // Avoid numeric refs where symbolic ones exist, as + // symbolic ones make more sense to humans reading! + if (xhtml) { + // all the HTMLlat1x.ent entities + // (all the "ISO-8859-1" characters) + if (c >= 160 && c <= 255) + esc = HTMLlat1x [c - 160]; + + // not quite half the HTMLsymbolx.ent entities + else if (c >= 913 && c <= 937) + esc = HTMLsymbolx_GR [c - 913]; + else if (c >= 945 && c <= 969) + esc = HTMLsymbolx_gr [c - 945]; + + else switch (c) { + // all of the HTMLspecialx.ent entities + case 338: esc = "OElig"; break; + case 339: esc = "oelig"; break; + case 352: esc = "Scaron"; break; + case 353: esc = "scaron"; break; + case 376: esc = "Yuml"; break; + case 710: esc = "circ"; break; + case 732: esc = "tilde"; break; + case 8194: esc = "ensp"; break; + case 8195: esc = "emsp"; break; + case 8201: esc = "thinsp"; break; + case 8204: esc = "zwnj"; break; + case 8205: esc = "zwj"; break; + case 8206: esc = "lrm"; break; + case 8207: esc = "rlm"; break; + case 8211: esc = "ndash"; break; + case 8212: esc = "mdash"; break; + case 8216: esc = "lsquo"; break; + case 8217: esc = "rsquo"; break; + case 8218: esc = "sbquo"; break; + case 8220: esc = "ldquo"; break; + case 8221: esc = "rdquo"; break; + case 8222: esc = "bdquo"; break; + case 8224: esc = "dagger"; break; + case 8225: esc = "Dagger"; break; + case 8240: esc = "permil"; break; + case 8249: esc = "lsaquo"; break; + case 8250: esc = "rsaquo"; break; + case 8364: esc = "euro"; break; + + // the other HTMLsymbox.ent entities + case 402: esc = "fnof"; break; + case 977: esc = "thetasym"; break; + case 978: esc = "upsih"; break; + case 982: esc = "piv"; break; + case 8226: esc = "bull"; break; + case 8230: esc = "hellip"; break; + case 8242: esc = "prime"; break; + case 8243: esc = "Prime"; break; + case 8254: esc = "oline"; break; + case 8260: esc = "frasl"; break; + case 8472: esc = "weierp"; break; + case 8465: esc = "image"; break; + case 8476: esc = "real"; break; + case 8482: esc = "trade"; break; + case 8501: esc = "alefsym"; break; + case 8592: esc = "larr"; break; + case 8593: esc = "uarr"; break; + case 8594: esc = "rarr"; break; + case 8595: esc = "darr"; break; + case 8596: esc = "harr"; break; + case 8629: esc = "crarr"; break; + case 8656: esc = "lArr"; break; + case 8657: esc = "uArr"; break; + case 8658: esc = "rArr"; break; + case 8659: esc = "dArr"; break; + case 8660: esc = "hArr"; break; + case 8704: esc = "forall"; break; + case 8706: esc = "part"; break; + case 8707: esc = "exist"; break; + case 8709: esc = "empty"; break; + case 8711: esc = "nabla"; break; + case 8712: esc = "isin"; break; + case 8713: esc = "notin"; break; + case 8715: esc = "ni"; break; + case 8719: esc = "prod"; break; + case 8721: esc = "sum"; break; + case 8722: esc = "minus"; break; + case 8727: esc = "lowast"; break; + case 8730: esc = "radic"; break; + case 8733: esc = "prop"; break; + case 8734: esc = "infin"; break; + case 8736: esc = "ang"; break; + case 8743: esc = "and"; break; + case 8744: esc = "or"; break; + case 8745: esc = "cap"; break; + case 8746: esc = "cup"; break; + case 8747: esc = "int"; break; + case 8756: esc = "there4"; break; + case 8764: esc = "sim"; break; + case 8773: esc = "cong"; break; + case 8776: esc = "asymp"; break; + case 8800: esc = "ne"; break; + case 8801: esc = "equiv"; break; + case 8804: esc = "le"; break; + case 8805: esc = "ge"; break; + case 8834: esc = "sub"; break; + case 8835: esc = "sup"; break; + case 8836: esc = "nsub"; break; + case 8838: esc = "sube"; break; + case 8839: esc = "supe"; break; + case 8853: esc = "oplus"; break; + case 8855: esc = "otimes"; break; + case 8869: esc = "perp"; break; + case 8901: esc = "sdot"; break; + case 8968: esc = "lceil"; break; + case 8969: esc = "rceil"; break; + case 8970: esc = "lfloor"; break; + case 8971: esc = "rfloor"; break; + case 9001: esc = "lang"; break; + case 9002: esc = "rang"; break; + case 9674: esc = "loz"; break; + case 9824: esc = "spades"; break; + case 9827: esc = "clubs"; break; + case 9829: esc = "hearts"; break; + case 9830: esc = "diams"; break; + } + } + + // else escape with numeric char refs + if (esc == null) { + stringBuf.setLength (0); + stringBuf.append ("#x"); + stringBuf.append (Integer.toHexString (c).toUpperCase ()); + esc = stringBuf.toString (); + + // FIXME: We don't write surrogate pairs correctly. + // They should work as one ref per character, since + // each pair is one character. For reading back into + // Unicode, it matters beginning in Unicode 3.1 ... + } + break; + } + if (i != first) + rawWrite (buf, off + first, i - first); + first = i + 1; + if (esc == eol) + newline (); + else { + rawWrite ('&'); + rawWrite (esc); + rawWrite (';'); + } + } + if (first < len) + rawWrite (buf, off + first, len - first); + } + + + + private void newline () + throws SAXException, IOException + { + out.write (eol); + column = 0; + } + + private void doIndent () + throws SAXException, IOException + { + int space = elementNestLevel * 2; + + newline (); + column = space; + // track tabs only at line starts + while (space > 8) { + out.write ("\t"); + space -= 8; + } + while (space > 0) { + out.write (" "); + space -= 2; + } + } + + private void rawWrite (char c) + throws IOException + { + out.write (c); + column++; + } + + private void rawWrite (String s) + throws SAXException, IOException + { + if (prettyPrinting && "default".equals (space.peek ())) { + char data [] = s.toCharArray (); + rawWrite (data, 0, data.length); + } else { + out.write (s); + column += s.length (); + } + } + + // NOTE: if xhtml, the REC gives some rules about whitespace + // which we could follow ... notably, many places where conformant + // agents "must" consolidate/normalize whitespace. Line ends can + // be removed there, etc. This may not be the right place to do + // such mappings though. + + // Line buffering may help clarify algorithms and improve results. + + // It's likely xml:space needs more attention. + + private void rawWrite (char buf [], int offset, int length) + throws SAXException, IOException + { + boolean wrap; + + if (prettyPrinting && space.empty ()) + fatal ("stack discipline", null); + + wrap = prettyPrinting && "default".equals (space.peek ()); + if (!wrap) { + out.write (buf, offset, length); + column += length; + return; + } + + // we're pretty printing and want to fill lines out only + // to the desired line length. + while (length > 0) { + int target = lineLength - column; + boolean wrote = false; + + // Do we even have a problem? + if (target > length || noWrap) { + out.write (buf, offset, length); + column += length; + return; + } + + // break the line at a space character, trying to fill + // as much of the line as possible. + char c; + + for (int i = target - 1; i >= 0; i--) { + if ((c = buf [offset + i]) == ' ' || c == '\t') { + i++; + out.write (buf, offset, i); + doIndent (); + offset += i; + length -= i; + wrote = true; + break; + } + } + if (wrote) + continue; + + // no space character permitting break before target + // line length is filled. So, take the next one. + if (target < 0) + target = 0; + for (int i = target; i < length; i++) + if ((c = buf [offset + i]) == ' ' || c == '\t') { + i++; + out.write (buf, offset, i); + doIndent (); + offset += i; + length -= i; + wrote = true; + break; + } + if (wrote) + continue; + + // no such luck. + out.write (buf, offset, length); + column += length; + break; + } + } +} diff --git a/libjava/classpath/gnu/xml/util/package.html b/libjava/classpath/gnu/xml/util/package.html new file mode 100644 index 00000000000..6e6c0d77d47 --- /dev/null +++ b/libjava/classpath/gnu/xml/util/package.html @@ -0,0 +1,20 @@ +<!DOCTYPE html PUBLIC + "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/1999/PR-xhtml1-19991210/DTD/xhtml1-transitional.dtd"> + +<html><head><title> org.brownell.xml package </title> </head> +<!-- +/* + * Copyright (C) 1999,2000 The Free Software Foundation, Inc. + */ +--> +<body> + <p> This package contains XML utilities, including SAX2 XML writers + and a parser of DOM trees, plus a command line driver. + That <a href="DoParse.html">driver</a> + connects parsers simple processing pipelines. + It can be handy for command line validation or + transformation tasks, possibly in batch mode, + or within Makefiles. </p> + +</body></html> diff --git a/libjava/classpath/gnu/xml/xpath/AndExpr.java b/libjava/classpath/gnu/xml/xpath/AndExpr.java new file mode 100644 index 00000000000..dd2e1c9a159 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/AndExpr.java @@ -0,0 +1,87 @@ +/* AndExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Logical and. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class AndExpr + extends Expr +{ + + final Expr lhs; + final Expr rhs; + + public AndExpr(Expr lhs, Expr rhs) + { + this.lhs = lhs; + this.rhs = rhs; + } + + public Object evaluate(Node context, int pos, int len) + { + Object left = lhs.evaluate(context, pos, len); + if (!_boolean(context, left)) + { + return Boolean.FALSE; + } + Object right = rhs.evaluate(context, pos, len); + return _boolean(context, right) ? Boolean.TRUE : Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new AndExpr(lhs.clone(context), rhs.clone(context)); + } + + public boolean references(QName var) + { + return (lhs.references(var) || rhs.references(var)); + } + + public String toString() + { + return lhs + " and " + rhs; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java b/libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java new file mode 100644 index 00000000000..3cef4adf3cc --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java @@ -0,0 +1,154 @@ +/* ArithmeticExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Binary arithmetic expression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ArithmeticExpr + extends Expr +{ + + static final int ADD = 0; + static final int SUBTRACT = 1; + static final int MULTIPLY = 2; + static final int DIVIDE = 3; + static final int MODULO = 4; + + final Expr lhs; + final Expr rhs; + final int op; + + ArithmeticExpr(Expr lhs, Expr rhs, int op) + { + this.lhs = lhs; + this.rhs = rhs; + switch (op) + { + case ADD: + case SUBTRACT: + case MULTIPLY: + case DIVIDE: + case MODULO: + this.op = op; + break; + default: + throw new IllegalArgumentException(); + } + } + + public Object evaluate(Node context, int pos, int len) + { + Object left = lhs.evaluate(context, pos, len); + Object right = rhs.evaluate(context, pos, len); + + double ln = _number(context, left); + double rn = _number(context, right); + switch (op) + { + case ADD: + return new Double(ln + rn); + case SUBTRACT: + return new Double(ln - rn); + case MULTIPLY: + return new Double(ln * rn); + case DIVIDE: + if (rn == 0.0d || rn == -0.0d) + { + return new Double(ln < 0.0d ? + Double.NEGATIVE_INFINITY : + Double.POSITIVE_INFINITY); + } + return new Double(ln / rn); + case MODULO: + if (rn == 0.0d || rn == -0.0d) + { + return new Double(ln < 0.0d ? + Double.NEGATIVE_INFINITY : + Double.POSITIVE_INFINITY); + } + return new Double(ln % rn); + default: + throw new IllegalStateException(); + } + } + + public Expr clone(Object context) + { + return new ArithmeticExpr(lhs.clone(context), rhs.clone(context), op); + } + + public boolean references(QName var) + { + return (lhs.references(var) || rhs.references(var)); + } + + public String toString() + { + StringBuffer buf = new StringBuffer(); + buf.append(lhs); + buf.append(' '); + switch (op) + { + case ADD: + buf.append('+'); + break; + case SUBTRACT: + buf.append('-'); + break; + case MULTIPLY: + buf.append('*'); + break; + case DIVIDE: + buf.append("div"); + break; + case MODULO: + buf.append("mod"); + break; + } + buf.append(' '); + buf.append(rhs); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/BooleanFunction.java b/libjava/classpath/gnu/xml/xpath/BooleanFunction.java new file mode 100644 index 00000000000..256de0382a8 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/BooleanFunction.java @@ -0,0 +1,95 @@ +/* BooleanFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>boolean</code> function converts its argument to a boolean as + * follows: + * <ul> + * <li>a number is true if and only if it is neither positive or negative + * zero nor NaN</li> + * <li>a node-set is true if and only if it is non-empty</li> + * <li>a string is true if and only if its length is non-zero</li> + * <li>an object of a type other than the four basic types is converted to a + * boolean in a way that is dependent on that type</li> + * </ul> + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class BooleanFunction + extends Expr +{ + + final Expr arg; + + BooleanFunction(List args) + { + this((Expr) args.get(0)); + } + + BooleanFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + return _boolean(context, val) ? Boolean.TRUE : Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new BooleanFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "boolean(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/CeilingFunction.java b/libjava/classpath/gnu/xml/xpath/CeilingFunction.java new file mode 100644 index 00000000000..7db08fc5d61 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/CeilingFunction.java @@ -0,0 +1,89 @@ +/* CeilingFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>ceiling</code> function returns the smallest (closest to + * negative infinity) number that is not less than the argument and that + * is an integer. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CeilingFunction + extends Expr +{ + + final Expr arg; + + CeilingFunction(List args) + { + this((Expr) args.get(0)); + } + + CeilingFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + double n = _number(context, val); + return new Double(Math.ceil(n)); + } + + public Expr clone(Object context) + { + return new CeilingFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "ceiling(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/ConcatFunction.java b/libjava/classpath/gnu/xml/xpath/ConcatFunction.java new file mode 100644 index 00000000000..fddd7ae2520 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/ConcatFunction.java @@ -0,0 +1,113 @@ +/* ConcatFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>concat</code> function returns the concatenation of its arguments. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ConcatFunction + extends Expr +{ + + final List args; + + ConcatFunction(List args) + { + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + StringBuffer buf = new StringBuffer(); + for (Iterator i = args.iterator(); i.hasNext(); ) + { + Expr arg = (Expr) i.next(); + Object val = arg.evaluate(context, pos, len); + buf.append(_string(context, val)); + } + return buf.toString(); + } + + public Expr clone(Object context) + { + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + return new ConcatFunction(args2); + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + + public String toString() + { + StringBuffer buf = new StringBuffer("concat("); + int len = args.size(); + for (int i = 0; i < len; i++) + { + if (i > 0) + { + buf.append(','); + } + buf.append(args.get(i)); + } + buf.append(')'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Constant.java b/libjava/classpath/gnu/xml/xpath/Constant.java new file mode 100644 index 00000000000..d5ca764a145 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Constant.java @@ -0,0 +1,98 @@ +/* Constant.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Constant value (string literal or number). + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class Constant + extends Expr +{ + + final Object value; + + public Constant(Object value) + { + this.value = value; + } + + public Object evaluate(Node context, int pos, int len) + { + return value; + } + + public Expr clone(Object context) + { + return new Constant(value); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + String ret = value.toString(); + if (value instanceof String) + { + if (ret.indexOf('\'') == -1) + { + return '\'' + ret + '\''; + } + else + { + return '"' + ret + '"'; + } + } + if (value instanceof Double) + { + if (ret.endsWith(".0")) + { + ret = ret.substring(0, ret.length() - 2); + } + } + return ret; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/ContainsFunction.java b/libjava/classpath/gnu/xml/xpath/ContainsFunction.java new file mode 100644 index 00000000000..fc2f33faf52 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/ContainsFunction.java @@ -0,0 +1,92 @@ +/* ContainsFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>contains</code> function returns true if the first argument + * string contains the second argument string, and otherwise returns false. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ContainsFunction + extends Expr +{ + + final Expr arg1; + final Expr arg2; + + ContainsFunction(List args) + { + this((Expr) args.get(0), (Expr) args.get(1)); + } + + ContainsFunction(Expr arg1, Expr arg2) + { + this.arg1 = arg1; + this.arg2 = arg2; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val1 = arg1.evaluate(context, pos, len); + Object val2 = arg2.evaluate(context, pos, len); + String s1 = _string(context, val1); + String s2 = _string(context, val2); + return (s1.indexOf(s2) != -1) ? Boolean.TRUE : Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new ContainsFunction(arg1.clone(context), arg2.clone(context)); + } + + public boolean references(QName var) + { + return (arg1.references(var) || arg2.references(var)); + } + + public String toString() + { + return "contains(" + arg1 + "," + arg2 + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/CountFunction.java b/libjava/classpath/gnu/xml/xpath/CountFunction.java new file mode 100644 index 00000000000..cb534bffc9e --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/CountFunction.java @@ -0,0 +1,88 @@ +/* CountFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>count</code> function returns the number of nodes in the + * argument node-set. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class CountFunction + extends Expr +{ + + final Expr arg; + + CountFunction(List args) + { + this((Expr) args.get(0)); + } + + CountFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + return new Double((double) ((Collection) val).size()); + } + + public Expr clone(Object context) + { + return new CountFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "count(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java b/libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java new file mode 100644 index 00000000000..c7d7de9f7c7 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java @@ -0,0 +1,63 @@ +/* DocumentOrderComparator.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Comparator; +import org.w3c.dom.Node; + +/** + * Sorts nodes into document order. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class DocumentOrderComparator + implements Comparator +{ + + public int compare(Object o1, Object o2) + { + if (o1 instanceof Node && o2 instanceof Node) + { + Node n1 = (Node)o1; + Node n2 = (Node)o2; + return (int) n1.compareDocumentPosition(n2); + } + return 0; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/EqualityExpr.java b/libjava/classpath/gnu/xml/xpath/EqualityExpr.java new file mode 100644 index 00000000000..6d00cee89a9 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/EqualityExpr.java @@ -0,0 +1,268 @@ +/* EqualityExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.Iterator; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Boolean equality expression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class EqualityExpr + extends Expr +{ + + final Expr lhs; + final Expr rhs; + final boolean invert; + + EqualityExpr(Expr lhs, Expr rhs, boolean invert) + { + this.lhs = lhs; + this.rhs = rhs; + this.invert = invert; + } + + public Object evaluate(Node context, int pos, int len) + { + boolean val = evaluateImpl(context, pos, len); + if (invert) + { + return val ? Boolean.FALSE : Boolean.TRUE; + } + else + { + return val ? Boolean.TRUE : Boolean.FALSE; + } + } + + private boolean evaluateImpl(Node context, int pos, int len) + { + Object left = lhs.evaluate(context, pos, len); + Object right = rhs.evaluate(context, pos, len); + + /* + * If both objects to be compared are node-sets, then the comparison + * will be true if and only if there is a node in the first node-set and + * a node in the second node-set such that the result of performing the + * comparison on the string-values of the two nodes is true. + */ + boolean flns = left instanceof Collection; + boolean frns = right instanceof Collection; + if (flns && frns) + { + Collection lns = (Collection) left; + Collection rns = (Collection) right; + if (lns.isEmpty()) + { + return false; + } + boolean all = true; + for (Iterator i = lns.iterator(); i.hasNext(); ) + { + Node ltest = (Node) i.next(); + for (Iterator j = rns.iterator(); j.hasNext(); ) + { + Node rtest = (Node) j.next(); + if (ltest == rtest || ltest.equals(rtest)) + { + // much shorter + if (!invert) + { + return true; + } + } + else if (stringValue(ltest).equals(stringValue(rtest))) + { + if (!invert) + { + return true; + } + } + else + { + all = false; + } + } + } + return all; + } + /* + * If one object to be compared is a node-set and the other is a number, + * then the comparison will be true if and only if there is a node in + * the node-set such that the result of performing the comparison on the + * number to be compared and on the result of converting the + * string-value of that node to a number using the number function is + * true. + */ + boolean fln = left instanceof Double; + boolean frn = right instanceof Double; + if ((flns && frn) || (frns && fln)) + { + Collection ns = flns ? (Collection) left : (Collection) right; + double n = fln ? ((Double) left).doubleValue() : + ((Double) right).doubleValue(); + boolean all = true; + for (Iterator i = ns.iterator(); i.hasNext(); ) + { + Node test = (Node) i.next(); + double nn = _number(context, stringValue(test)); + if (nn == n) + { + if (!invert) + { + return true; + } + } + else + { + all = false; + } + } + return invert ? all : false; + } + /* + * If one object to be compared is a node-set and the other is a + * string, then the comparison will be true if and only if there is a + * node in the node-set such that the result of performing the + * comparison on the string-value of the node and the other string is + * true. + */ + boolean fls = left instanceof String; + boolean frs = right instanceof String; + if ((flns && frs) || (frns && fls)) + { + Collection ns = flns ? (Collection) left : (Collection) right; + String s = fls ? (String) left : (String) right; + boolean all = true; + for (Iterator i = ns.iterator(); i.hasNext(); ) + { + Node test = (Node) i.next(); + if (stringValue(test).equals(s)) + { + if (!invert) + { + return true; + } + } + else + { + all = false; + } + } + return invert ? all : false; + } + /* + * If one object to be compared is a node-set and the other is a + * boolean, then the comparison will be true if and only if the result + * of performing the comparison on the boolean and on the result of + * converting the node-set to a boolean using the boolean function is + * true. + */ + boolean flb = left instanceof Boolean; + boolean frb = right instanceof Boolean; + if ((flns && frb) || (frns && flb)) + { + Collection ns = flns ? (Collection) left : (Collection) right; + boolean b = flb ? ((Boolean) left).booleanValue() : + ((Boolean) right).booleanValue(); + return _boolean(context, ns) == b; + } + /* + * If at least one object to be compared is a boolean, then each object + * to be compared is converted to a boolean as if by applying the + * boolean function. + */ + if (flb || frb) + { + boolean lb = flb ? ((Boolean) left).booleanValue() : + _boolean(context, left); + boolean rb = frb ? ((Boolean) right).booleanValue() : + _boolean(context, right); + return lb == rb; + } + /* + * Otherwise, if at least one object to be compared is + * a number, then each object to be compared is converted to a number as + * if by applying the number function. + */ + if (fln || frn) + { + double ln = fln ? ((Double) left).doubleValue() : + _number(context, left); + double rn = frn ? ((Double) right).doubleValue() : + _number(context, right); + return ln == rn; + } + /* + * Otherwise, both objects to be + * compared are converted to strings as if by applying the string + * function. + */ + String ls = fls ? (String) left : _string(context, left); + String rs = frs ? (String) right : _string(context, right); + return ls.equals(rs); + } + + public Expr clone(Object context) + { + return new EqualityExpr(lhs.clone(context), rhs.clone(context), invert); + } + + public boolean references(QName var) + { + return (lhs.references(var) || rhs.references(var)); + } + + public String toString() + { + if (invert) + { + return lhs + " != " + rhs; + } + else + { + return lhs + " = " + rhs; + } + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Expr.java b/libjava/classpath/gnu/xml/xpath/Expr.java new file mode 100644 index 00000000000..b4b55dcf94e --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Expr.java @@ -0,0 +1,476 @@ +/* Expr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.io.IOException; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.StringTokenizer; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * An XPath expression. + * This can be evaluated in the context of a node to produce a result. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class Expr + implements XPathExpression +{ + + protected static final Comparator documentOrderComparator = + new DocumentOrderComparator(); + + protected static final DecimalFormat decimalFormat = + new DecimalFormat("####################################################" + + ".####################################################", + new DecimalFormatSymbols(Locale.US)); + + public Object evaluate(Object item, QName returnType) + throws XPathExpressionException + { + Object ret = null; + Node context = null; + if (item instanceof Node) + { + context = (Node) item; + ret = evaluate(context, 1, 1); + if (XPathConstants.STRING == returnType && + !(ret instanceof String)) + { + ret = _string(context, ret); + } + else if (XPathConstants.NUMBER == returnType && + !(ret instanceof Double)) + { + ret = new Double(_number(context, ret)); + } + else if (XPathConstants.BOOLEAN == returnType && + !(ret instanceof Boolean)) + { + ret = _boolean(context, ret) ? Boolean.TRUE : Boolean.FALSE; + } + else if (XPathConstants.NODE == returnType) + { + if (ret instanceof Collection) + { + Collection ns = (Collection) ret; + switch (ns.size()) + { + case 0: + ret = null; + break; + case 1: + ret = (Node) ns.iterator().next(); + break; + default: + throw new XPathExpressionException("multiple nodes in node-set"); + } + } + else if (ret != null) + { + throw new XPathExpressionException("return value is not a node-set"); + } + } + else if (XPathConstants.NODESET == returnType) + { + if (ret != null && !(ret instanceof Collection)) + { + throw new XPathExpressionException("return value is not a node-set"); + } + } + } + return ret; + } + + public String evaluate(Object item) + throws XPathExpressionException + { + return (String) evaluate(item, XPathConstants.STRING); + } + + public Object evaluate(InputSource source, QName returnType) + throws XPathExpressionException + { + try + { + DocumentBuilderFactory factory = + new gnu.xml.dom.JAXPFactory(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(source); + return evaluate(doc, returnType); + } + catch (ParserConfigurationException e) + { + throw new XPathExpressionException(e); + } + catch (SAXException e) + { + throw new XPathExpressionException(e); + } + catch (IOException e) + { + throw new XPathExpressionException(e); + } + } + + public String evaluate(InputSource source) + throws XPathExpressionException + { + return (String) evaluate(source, XPathConstants.STRING); + } + + public abstract Object evaluate(Node context, int pos, int len); + + public abstract Expr clone(Object context); + + public abstract boolean references(QName var); + + /* -- 4.1 Node Set Functions -- */ + + /** + * The id function selects elements by their unique ID. + * When the argument to id is of type node-set, then the result is + * the union of the result of applying id to the string-value of each of + * the nodes in the argument node-set. When the argument to id is of any + * other type, the argument is converted to a string as if by a call to + * the string function; the string is split into a whitespace-separated + * list of tokens (whitespace is any sequence of characters matching the + * production S); the result is a node-set containing the elements in the + * same document as the context node that have a unique ID equal to any of + * the tokens in the list. + */ + public static Collection _id(Node context, Object object) + { + Set ret = new HashSet(); + if (object instanceof Collection) + { + Collection nodeSet = (Collection) object; + for (Iterator i = nodeSet.iterator(); i.hasNext(); ) + { + String string = stringValue((Node) i.next()); + ret.addAll(_id (context, string)); + } + } + else + { + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + String string = _string(context, object); + StringTokenizer st = new StringTokenizer(string, " \t\r\n"); + while (st.hasMoreTokens()) + { + Node element = doc.getElementById(st.nextToken()); + if (element != null) + { + ret.add(element); + } + } + } + return ret; + } + + /** + * The local-name function returns the local part of the expanded-name of + * the node in the argument node-set that is first in document order. If + * the argument node-set is empty or the first node has no expanded-name, + * an empty string is returned. If the argument is omitted, it defaults to + * a node-set with the context node as its only member. + */ + public static String _local_name(Node context, Collection nodeSet) + { + Node node = (nodeSet == null || nodeSet.size() == 0) ? context : + firstNode(nodeSet); + return node.getLocalName(); + } + + /** + * The namespace-uri function returns the namespace URI of the + * expanded-name of the node in the argument node-set that is first in + * document order. If the argument node-set is empty, the first node has + * no expanded-name, or the namespace URI of the expanded-name is null, an + * empty string is returned. If the argument is omitted, it defaults to a + * node-set with the context node as its only member. + */ + public static String _namespace_uri(Node context, Collection nodeSet) + { + Node node = (nodeSet == null || nodeSet.size() == 0) ? context : + firstNode(nodeSet); + return node.getNamespaceURI(); + } + + /** + * The name function returns a string containing a QName representing the + * expanded-name of the node in the argument node-set that is first in + * document order. The QName must represent the expanded-name with respect + * to the namespace declarations in effect on the node whose expanded-name + * is being represented. Typically, this will be the QName that occurred + * in the XML source. This need not be the case if there are namespace + * declarations in effect on the node that associate multiple prefixes + * with the same namespace. However, an implementation may include + * information about the original prefix in its representation of nodes; + * in this case, an implementation can ensure that the returned string is + * always the same as the QName used in the XML source. If the argument + * node-set is empty or the first node has no expanded-name, an empty + * string is returned. If the argument it omitted, it defaults to a + * node-set with the context node as its only member. + */ + public static String _name(Node context, Collection nodeSet) + { + Node node = (nodeSet == null || nodeSet.size() == 0) ? context : + firstNode(nodeSet); + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + case Node.ELEMENT_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + return node.getNodeName(); + default: + return ""; + } + } + + /** + * Returns the first node in the set in document order. + */ + static Node firstNode(Collection nodeSet) + { + List list = new ArrayList(nodeSet); + Collections.sort(list, documentOrderComparator); + return (Node) list.get(0); + } + + /* -- 4.2 String Functions -- */ + + /** + * Implementation of the XPath <code>string</code> function. + */ + public static String _string(Node context, Object object) + { + if (object == null) + { + return stringValue(context); + } + if (object instanceof String) + { + return (String) object; + } + if (object instanceof Boolean) + { + return object.toString(); + } + if (object instanceof Double) + { + double d = ((Double) object).doubleValue(); + if (Double.isNaN(d)) + { + return "NaN"; + } + else if (d == 0.0d) + { + return "0"; + } + else if (Double.isInfinite(d)) + { + if (d < 0) + { + return "-Infinity"; + } + else + { + return "Infinity"; + } + } + else + { + String ret = decimalFormat.format(d); + if (ret.endsWith (".0")) + { + ret = ret.substring(0, ret.length() - 2); + } + return ret; + } + } + if (object instanceof Collection) + { + Collection nodeSet = (Collection) object; + if (nodeSet.isEmpty()) + { + return ""; + } + Node node = firstNode(nodeSet); + return stringValue(node); + } + throw new IllegalArgumentException(object.toString()); + } + + /* -- 4.3 Boolean Functions -- */ + + /** + * Implementation of the XPath <code>boolean</code> function. + */ + public static boolean _boolean(Node context, Object object) + { + if (object instanceof Boolean) + { + return ((Boolean) object).booleanValue(); + } + if (object instanceof Double) + { + return ((Double) object).doubleValue() != 0.0; + } + if (object instanceof String) + { + return ((String) object).length() != 0; + } + if (object instanceof Collection) + { + return ((Collection) object).size() != 0; + } + return false; // TODO user defined types + } + + /* -- 4.4 Number Functions -- */ + + /** + * Implementation of the XPath <code>number</code> function. + */ + public static double _number(Node context, Object object) + { + if (object == null) + { + object = Collections.singleton(context); + } + if (object instanceof Double) + { + return ((Double) object).doubleValue(); + } + if (object instanceof Boolean) + { + return ((Boolean) object).booleanValue() ? 1.0 : 0.0; + } + if (object instanceof Collection) + { + // Convert node-set to string + object = stringValue((Collection) object); + } + if (object instanceof String) + { + String string = ((String) object).trim(); + try + { + return Double.parseDouble(string); + } + catch (NumberFormatException e) + { + return Double.NaN; + } + } + return Double.NaN; // TODO user-defined types + } + + /** + * Computes the XPath string-value of the specified node-set. + */ + public static String stringValue(Collection nodeSet) + { + StringBuffer buf = new StringBuffer(); + for (Iterator i = nodeSet.iterator(); i.hasNext(); ) + { + buf.append(stringValue((Node) i.next())); + } + return buf.toString(); + } + + /** + * Computes the XPath string-value of the specified node. + */ + public static String stringValue(Node node) + { + return stringValue(node, false); + } + + static String stringValue(Node node, boolean elementMode) + { + switch (node.getNodeType()) + { + case Node.DOCUMENT_NODE: // 5.1 Root Node + case Node.DOCUMENT_FRAGMENT_NODE: + case Node.ELEMENT_NODE: // 5.2 Element Nodes + StringBuffer buf = new StringBuffer(); + for (Node ctx = node.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + buf.append(stringValue(ctx, true)); + } + return buf.toString(); + case Node.TEXT_NODE: // 5.7 Text Nodes + case Node.CDATA_SECTION_NODE: + return node.getNodeValue(); + case Node.ATTRIBUTE_NODE: // 5.3 Attribute Nodes + case Node.PROCESSING_INSTRUCTION_NODE: // 5.5 Processing Instruction + case Node.COMMENT_NODE: // 5.6 Comment Nodes + if (!elementMode) + { + return node.getNodeValue(); + } + default: + return ""; + } + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/FalseFunction.java b/libjava/classpath/gnu/xml/xpath/FalseFunction.java new file mode 100644 index 00000000000..87660ec81a8 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/FalseFunction.java @@ -0,0 +1,72 @@ +/* FalseFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>false</code> function returns false. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class FalseFunction + extends Expr +{ + + public Object evaluate(Node context, int pos, int len) + { + return Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new FalseFunction(); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + return "false()"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/FloorFunction.java b/libjava/classpath/gnu/xml/xpath/FloorFunction.java new file mode 100644 index 00000000000..c03c0e7edaf --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/FloorFunction.java @@ -0,0 +1,89 @@ +/* FloorFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>floor</code> function returns the largest (closest to positive + * infinity) number that is not greater than the argument and that is an + * integer. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class FloorFunction + extends Expr +{ + + final Expr arg; + + FloorFunction(List args) + { + this((Expr) args.get(0)); + } + + FloorFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + double n = _number(context, val); + return new Double(Math.floor(n)); + } + + public Expr clone(Object context) + { + return new FloorFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "floor(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Function.java b/libjava/classpath/gnu/xml/xpath/Function.java new file mode 100644 index 00000000000..aecd49d9c73 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Function.java @@ -0,0 +1,57 @@ +/* Function.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; + +/** + * Interface to be implemented by external functions that need to receive + * parameter values. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public interface Function +{ + + /** + * Sets the list of expressions to evaluate as parameter values. + */ + void setArguments(List args); + +} + diff --git a/libjava/classpath/gnu/xml/xpath/FunctionCall.java b/libjava/classpath/gnu/xml/xpath/FunctionCall.java new file mode 100644 index 00000000000..669efbf1866 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/FunctionCall.java @@ -0,0 +1,163 @@ +/* FunctionCall.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; +import javax.xml.xpath.XPathFunctionResolver; +import org.w3c.dom.Node; + +/** + * Executes an XPath core or extension function. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class FunctionCall + extends Expr +{ + + final XPathFunctionResolver resolver; + final String name; + final List args; + + public FunctionCall(XPathFunctionResolver resolver, String name) + { + this(resolver, name, Collections.EMPTY_LIST); + } + + public FunctionCall(XPathFunctionResolver resolver, String name, List args) + { + this.resolver = resolver; + this.name = name; + this.args = args; + } + + public Object evaluate(Node context, int pos, int len) + { + if (resolver != null) + { + QName qname = QName.valueOf(name); + int arity = args.size(); + XPathFunction function = resolver.resolveFunction(qname, arity); + if (function != null) + { + //System.err.println("Calling "+toString()+" with "+values); + if (function instanceof Expr) + { + if (function instanceof Function) + { + ((Function) function).setArguments(args); + } + return ((Expr) function).evaluate(context, pos, len); + } + else + { + List values = new ArrayList(arity); + for (int i = 0; i < arity; i++) + { + Expr arg = (Expr) args.get(i); + values.add(arg.evaluate(context, pos, len)); + } + try + { + return function.evaluate(values); + } + catch (XPathFunctionException e) + { + e.printStackTrace(System.err); // FIXME + throw new RuntimeException(e.getMessage(), e); + } + } + } + } + throw new IllegalArgumentException("Invalid function call: " + + toString()); + } + + public Expr clone(Object context) + { + int len = args.size(); + List args2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + args2.add(((Expr) args.get(i)).clone(context)); + } + XPathFunctionResolver r = resolver; + if (context instanceof XPathFunctionResolver) + { + r = (XPathFunctionResolver) context; + } + return new FunctionCall(r, name, args2); + } + + public boolean references(QName var) + { + for (Iterator i = args.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + + public String toString() + { + StringBuffer buf = new StringBuffer(); + buf.append(name); + buf.append('('); + int len = args.size(); + for (int i = 0; i < len; i++) + { + if (i > 0) + { + buf.append(','); + } + buf.append(args.get(i)); + } + buf.append(')'); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/IdFunction.java b/libjava/classpath/gnu/xml/xpath/IdFunction.java new file mode 100644 index 00000000000..74979541957 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/IdFunction.java @@ -0,0 +1,102 @@ +/* IdFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>id</code> function selects elements by their unique ID. + * When the argument to id is of type node-set, then the result is + * the union of the result of applying id to the string-value of each of the + * nodes in the argument node-set. When the argument to id is of any other + * type, the argument is converted to a string as if by a call to the string + * function; the string is split into a whitespace-separated list of tokens + * (whitespace is any sequence of characters matching the production S); the + * result is a node-set containing the elements in the same document as the + * context node that have a unique ID equal to any of the tokens in the + * list. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class IdFunction + extends Pattern +{ + + final Expr arg; + + IdFunction(List args) + { + this((Expr) args.get(0)); + } + + public IdFunction(Expr arg) + { + this.arg = arg; + } + + public boolean matches(Node context) + { + Object ret = evaluate(context, 1, 1); + return !((Collection) ret).isEmpty(); + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + return _id(context, val); + } + + public Expr clone(Object context) + { + return new IdFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "id(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/LangFunction.java b/libjava/classpath/gnu/xml/xpath/LangFunction.java new file mode 100644 index 00000000000..2c2506d1b97 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/LangFunction.java @@ -0,0 +1,116 @@ +/* LangFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * The <code>lang</code> function returns true or false depending on whether + * the language of the context node as specified by xml:lang attributes is + * the same as or is a sublanguage of the language specified by the argument + * string. The language of the context node is determined by the value of + * the xml:lang attribute on the context node, or, if the context node has + * no xml:lang attribute, by the value of the xml:lang attribute on the + * nearest ancestor of the context node that has an xml:lang attribute. If + * there is no such attribute, then lang returns false. If there is such an + * attribute, then lang returns true if the attribute value is equal to the + * argument ignoring case, or if there is some suffix starting with - such + * that the attribute value is equal to the argument ignoring that suffix of + * the attribute value and ignoring case. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class LangFunction + extends Expr +{ + + final Expr arg; + + LangFunction(List args) + { + this((Expr) args.get(0)); + } + + LangFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + String lang = _string(context, val); + String clang = getLang(context); + while (clang == null && context != null) + { + context = context.getParentNode(); + clang = getLang(context); + } + boolean ret = (clang == null) ? false : + clang.toLowerCase().startsWith(lang.toLowerCase()); + return ret ? Boolean.TRUE : Boolean.FALSE; + } + + String getLang(Node node) + { + if (node.getNodeType() == Node.ELEMENT_NODE) + { + return ((Element) node).getAttribute("xml:lang"); + } + return null; + } + + public Expr clone(Object context) + { + return new IdFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "lang(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/LastFunction.java b/libjava/classpath/gnu/xml/xpath/LastFunction.java new file mode 100644 index 00000000000..751a0a9c1c9 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/LastFunction.java @@ -0,0 +1,73 @@ +/* LastFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>last</code> function returns a number equal to the context + * size from the expression evaluation context. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class LastFunction + extends Expr +{ + + public Object evaluate(Node context, int pos, int len) + { + return new Double((double) len); + } + + public Expr clone(Object context) + { + return new LastFunction(); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + return "last()"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/LocalNameFunction.java b/libjava/classpath/gnu/xml/xpath/LocalNameFunction.java new file mode 100644 index 00000000000..f8ace9cd5f9 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/LocalNameFunction.java @@ -0,0 +1,93 @@ +/* LocalNameFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>local-name</code> function returns the local part of the + * expanded-name of the node in the argument node-set that is first in + * document order. + * If the argument node-set is empty or the first node has no expanded-name, + * an empty string is returned. If the argument is omitted, it defaults to a + * node-set with the context node as its only member. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class LocalNameFunction + extends Expr +{ + + final Expr arg; + + LocalNameFunction(List args) + { + this(args.size() > 0 ? (Expr) args.get(0) : null); + } + + LocalNameFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + return _local_name(context, (Collection) val); + } + + public Expr clone(Object context) + { + return new LocalNameFunction((arg == null) ? null : + arg.clone(context)); + } + + public boolean references(QName var) + { + return (arg == null) ? false : arg.references(var); + } + + public String toString() + { + return (arg == null) ? "local-name()" : "local-name(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NameFunction.java b/libjava/classpath/gnu/xml/xpath/NameFunction.java new file mode 100644 index 00000000000..dc5e612037a --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NameFunction.java @@ -0,0 +1,101 @@ +/* NameFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>name</code> function returns a string containing a QName + * representing the expanded-name of the node in the argument node-set that + * is first in document order. The QName must represent the expanded-name + * with respect to the namespace declarations in effect on the node whose + * expanded-name is being represented. Typically, this will be the QName + * that occurred in the XML source. This need not be the case if there are + * namespace declarations in effect on the node that associate multiple + * prefixes with the same namespace. However, an implementation may include + * information about the original prefix in its representation of nodes; in + * this case, an implementation can ensure that the returned string is + * always the same as the QName used in the XML source. If the argument + * node-set is empty or the first node has no expanded-name, an empty string + * is returned. If the argument it omitted, it defaults to a node-set with + * the context node as its only member. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NameFunction + extends Expr +{ + + final Expr arg; + + NameFunction(List args) + { + this(args.size() > 0 ? (Expr) args.get(0) : null); + } + + NameFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + return _name(context, (Collection) val); + } + + public Expr clone(Object context) + { + return new NameFunction((arg == null) ? null : + arg.clone(context)); + } + + public boolean references(QName var) + { + return (arg == null) ? false : arg.references(var); + } + + public String toString() + { + return (arg == null) ? "name()" : "name(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NameTest.java b/libjava/classpath/gnu/xml/xpath/NameTest.java new file mode 100644 index 00000000000..c89ba4dd16b --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NameTest.java @@ -0,0 +1,142 @@ +/* NameTest.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Tests whether a node has the specified name. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class NameTest + extends Test +{ + + final QName qName; + final boolean anyLocalName; + final boolean any; + + public NameTest(QName qName, boolean anyLocalName, boolean any) + { + this.anyLocalName = anyLocalName; + this.any = any; + this.qName = qName; + } + + public boolean matchesAny() + { + return any; + } + + public boolean matchesAnyLocalName() + { + return anyLocalName; + } + + public boolean matches(Node node, int pos, int len) + { + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + // Do not match namespace attributes + String uri = node.getNamespaceURI(); + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) || + XMLConstants.XMLNS_ATTRIBUTE.equals(node.getPrefix()) || + XMLConstants.XMLNS_ATTRIBUTE.equals(node.getNodeName())) + { + return false; + } + // Fall through + case Node.ELEMENT_NODE: + break; + default: + return false; + } + if (any) + { + return true; + } + String uri = qName.getNamespaceURI(); + String nodeUri = node.getNamespaceURI(); + String nodeLocalName = node.getLocalName(); + if (nodeLocalName != null && !equal(uri, nodeUri)) + { + return false; + } + if (anyLocalName) + { + return true; + } + String localName = qName.getLocalPart(); + if (nodeLocalName != null) + { + nodeLocalName = node.getNodeName(); + } + return (localName.equals(nodeLocalName)); + } + + final boolean equal(String s1, String s2) + { + return (((s1 == null || s1.length() == 0) && + (s2 == null || s2.length() == 0)) || + s1 != null && s1.equals(s2)); + } + + public Test clone(Object context) + { + return new NameTest(qName, anyLocalName, any); + } + + public boolean references(QName var) + { + return false; + } + + public String toString () + { + if (any) + { + return "*"; + } + return qName.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NamespaceTest.java b/libjava/classpath/gnu/xml/xpath/NamespaceTest.java new file mode 100644 index 00000000000..6d41166bcb9 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NamespaceTest.java @@ -0,0 +1,128 @@ +/* NamespaceTest.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Tests whether a namespace attribute has the specified name. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class NamespaceTest + extends Test +{ + + final QName qName; + final boolean anyLocalName; + final boolean any; + + public NamespaceTest(QName qName, boolean anyLocalName, boolean any) + { + this.anyLocalName = anyLocalName; + this.any = any; + this.qName = qName; + } + + public boolean matchesAny() + { + return any; + } + + public boolean matchesAnyLocalName() + { + return anyLocalName; + } + + public boolean matches(Node node, int pos, int len) + { + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + // Only match namespace attributes + String uri = node.getNamespaceURI(); + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) || + XMLConstants.XMLNS_ATTRIBUTE.equals(node.getPrefix()) || + XMLConstants.XMLNS_ATTRIBUTE.equals(node.getNodeName())) + { + break; + } + // Fall through + default: + // Only process namespace attributes + return false; + } + if (any) + { + return true; + } + if (anyLocalName) + { + return true; + } + String localName = qName.getLocalPart(); + String nodeLocalName = node.getLocalName(); + if (nodeLocalName == null) + { + nodeLocalName = node.getNodeName(); + } + return (localName.equals(nodeLocalName)); + } + + public Test clone(Object context) + { + return new NamespaceTest(qName, anyLocalName, any); + } + + public boolean references(QName var) + { + return false; + } + + public String toString () + { + if (any) + { + return "*"; + } + return qName.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java b/libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java new file mode 100644 index 00000000000..e67ec42065e --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java @@ -0,0 +1,93 @@ +/* NamespaceUriFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>namespace-uri</code> function returns the namespace URI of the + * expanded-name of the node in the argument node-set that is first in + * document order. If the argument node-set is empty, the first node has no + * expanded-name, or the namespace URI of the expanded-name is null, an + * empty string is returned. If the argument is omitted, it defaults to a + * node-set with the context node as its only member. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NamespaceUriFunction + extends Expr +{ + + final Expr arg; + + NamespaceUriFunction(List args) + { + this(args.size() > 0 ? (Expr) args.get(0) : null); + } + + NamespaceUriFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + return _namespace_uri(context, (Collection) val); + } + + public Expr clone(Object context) + { + return new NamespaceUriFunction((arg == null) ? null : + arg.clone(context)); + } + + public boolean references(QName var) + { + return (arg == null) ? false : arg.references(var); + } + + public String toString() + { + return (arg == null) ? "namespace-uri()" : "namespace-uri(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NegativeExpr.java b/libjava/classpath/gnu/xml/xpath/NegativeExpr.java new file mode 100644 index 00000000000..9e24aff81a5 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NegativeExpr.java @@ -0,0 +1,81 @@ +/* NegativeExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Unary negative. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NegativeExpr + extends Expr +{ + + final Expr expr; + + NegativeExpr(Expr expr) + { + this.expr = expr; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = expr.evaluate(context, pos, len); + double n = _number(context, val); + return new Double(-n); + } + + public Expr clone(Object context) + { + return new NegativeExpr(expr.clone(context)); + } + + public boolean references(QName var) + { + return expr.references(var); + } + + public String toString() + { + return "-" + expr; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NodeTypeTest.java b/libjava/classpath/gnu/xml/xpath/NodeTypeTest.java new file mode 100644 index 00000000000..807374132e5 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NodeTypeTest.java @@ -0,0 +1,136 @@ +/* NodeTypeTest.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Tests whether a node is of a given type. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class NodeTypeTest + extends Test +{ + + final short type; + final String data; + + public NodeTypeTest(short type) + { + this(type, null); + } + + public NodeTypeTest(short type, String data) + { + this.type = type; + this.data = data; + } + + public short getNodeType() + { + return type; + } + + public String getData() + { + return data; + } + + public boolean matches(Node node, int pos, int len) + { + short nodeType = node.getNodeType(); + switch (nodeType) + { + case Node.ELEMENT_NODE: + case Node.ATTRIBUTE_NODE: + case Node.TEXT_NODE: + case Node.CDATA_SECTION_NODE: + case Node.COMMENT_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + if (type > 0) + { + if (nodeType != type) + { + return false; + } + if (data != null && !data.equals(node.getNodeValue())) + { + return false; + } + } + return true; + default: + // Not part of XPath data model + return false; + } + } + + public Test clone(Object context) + { + return new NodeTypeTest(type, data); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + switch (type) + { + case 0: + return "node()"; + case Node.TEXT_NODE: + return "text()"; + case Node.COMMENT_NODE: + return "comment()"; + case Node.PROCESSING_INSTRUCTION_NODE: + if (data != null) + { + return "processing-instruction('" + data + "')"; + } + return "processing-instruction()"; + default: + throw new IllegalStateException(); + } + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java b/libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java new file mode 100644 index 00000000000..b7358e8d8e4 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java @@ -0,0 +1,105 @@ +/* NormalizeSpaceFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import java.util.StringTokenizer; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>normalize-space</code> function returns the argument string + * with whitespace normalized by stripping leading and trailing whitespace + * and replacing sequences of whitespace characters by a single space. + * Whitespace characters are the same as those allowed by the S production + * in XML. If the argument is omitted, it defaults to the context node + * converted to a string, in other words the string-value of the context + * node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NormalizeSpaceFunction + extends Expr +{ + + final Expr arg; + + NormalizeSpaceFunction(List args) + { + this((Expr) args.get(0)); + } + + NormalizeSpaceFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + String s = _string(context, val); + StringTokenizer st = new StringTokenizer(s, " \t\r\n"); + StringBuffer buf = new StringBuffer(); + if (st.hasMoreTokens()) + { + buf.append(st.nextToken()); + while (st.hasMoreTokens()) + { + buf.append(' '); + buf.append(st.nextToken()); + } + } + return buf.toString(); + } + + public Expr clone(Object context) + { + return new NormalizeSpaceFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return (arg == null) ? false : arg.references(var); + } + + public String toString() + { + return (arg == null) ? "normalize-space()" : "normalize-space(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NotFunction.java b/libjava/classpath/gnu/xml/xpath/NotFunction.java new file mode 100644 index 00000000000..bbed66a7311 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NotFunction.java @@ -0,0 +1,87 @@ +/* NotFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>not</code> function returns true if its argument is false, + * and false otherwise. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NotFunction + extends Expr +{ + + final Expr arg; + + NotFunction(List args) + { + this((Expr) args.get(0)); + } + + NotFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + return _boolean(context, val) ? Boolean.FALSE : Boolean.TRUE; + } + + public Expr clone(Object context) + { + return new NotFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "not(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/NumberFunction.java b/libjava/classpath/gnu/xml/xpath/NumberFunction.java new file mode 100644 index 00000000000..79553ce81fb --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/NumberFunction.java @@ -0,0 +1,102 @@ +/* NumberFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>number</code> function converts its argument to a number as + * follows: + * <ul> + * <li>a string that consists of optional whitespace followed by an optional + * minus sign followed by a Number followed by whitespace is converted to + * the IEEE 754 number that is nearest (according to the IEEE 754 + * round-to-nearest rule) to the mathematical value represented by the + * string; any other string is converted to NaN</li> + * <li>boolean true is converted to 1; boolean false is converted to 0</li> + * <li>a node-set is first converted to a string as if by a call to the + * string function and then converted in the same way as a string + * argument</li> + * <li>an object of a type other than the four basic types is converted to a + * number in a way that is dependent on that type</li> + * </ul> + * If the argument is omitted, it defaults to a node-set with the context + * node as its only member. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class NumberFunction + extends Expr +{ + + final Expr arg; + + NumberFunction(List args) + { + this(args.size() > 0 ? (Expr) args.get(0) : null); + } + + NumberFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + return new Double(_number(context, val)); + } + + public Expr clone(Object context) + { + return new NumberFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "number(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/OrExpr.java b/libjava/classpath/gnu/xml/xpath/OrExpr.java new file mode 100644 index 00000000000..1085f51127e --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/OrExpr.java @@ -0,0 +1,87 @@ +/* OrExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Logical or. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class OrExpr + extends Expr +{ + + final Expr lhs; + final Expr rhs; + + public OrExpr(Expr lhs, Expr rhs) + { + this.lhs = lhs; + this.rhs = rhs; + } + + public Object evaluate(Node context, int pos, int len) + { + Object left = lhs.evaluate(context, pos, len); + if (_boolean(context, left)) + { + return Boolean.TRUE; + } + Object right = rhs.evaluate(context, pos, len); + return _boolean(context, right) ? Boolean.TRUE : Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new OrExpr(lhs.clone(context), rhs.clone(context)); + } + + public boolean references(QName var) + { + return (lhs.references(var) || rhs.references(var)); + } + + public String toString() + { + return lhs + " or " + rhs; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java b/libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java new file mode 100644 index 00000000000..f343857c313 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java @@ -0,0 +1,90 @@ +/* ParenthesizedExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Simple subexpression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class ParenthesizedExpr + extends Expr +{ + + final Expr expr; + + ParenthesizedExpr(Expr expr) + { + this.expr = expr; + } + + public Object evaluate(Node context, int pos, int len) + { + Object ret = expr.evaluate(context, pos, len); + if (ret instanceof Collection) + { + List list = new ArrayList((Collection) ret); + Collections.sort(list, documentOrderComparator); + ret = list; + } + return ret; + } + + public Expr clone(Object context) + { + return new ParenthesizedExpr(expr.clone(context)); + } + + public boolean references(QName var) + { + return expr.references(var); + } + + public String toString() + { + return "(" + expr + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Path.java b/libjava/classpath/gnu/xml/xpath/Path.java new file mode 100644 index 00000000000..4b01f0918ab --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Path.java @@ -0,0 +1,54 @@ +/* Path.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import org.w3c.dom.Node; + +/** + * An XPath path component expression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +abstract class Path + extends Pattern +{ + + abstract Collection evaluate(Node context, Collection nodeSet); + +} diff --git a/libjava/classpath/gnu/xml/xpath/Pattern.java b/libjava/classpath/gnu/xml/xpath/Pattern.java new file mode 100644 index 00000000000..859ab569281 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Pattern.java @@ -0,0 +1,54 @@ +/* Pattern.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import org.w3c.dom.Node; + +/** + * Interface implemented by expressions that can form part of XSL patterns. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class Pattern + extends Expr +{ + + public abstract boolean matches(Node context); + +} + diff --git a/libjava/classpath/gnu/xml/xpath/PositionFunction.java b/libjava/classpath/gnu/xml/xpath/PositionFunction.java new file mode 100644 index 00000000000..3060eeaedb4 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/PositionFunction.java @@ -0,0 +1,73 @@ +/* PositionFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>position</code> function returns a number equal to the context + * position from the expression evaluation context. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class PositionFunction + extends Expr +{ + + public Object evaluate(Node context, int pos, int len) + { + return new Double((double) pos); + } + + public Expr clone(Object context) + { + return new PositionFunction(); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + return "position()"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Predicate.java b/libjava/classpath/gnu/xml/xpath/Predicate.java new file mode 100644 index 00000000000..5538c55aacf --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Predicate.java @@ -0,0 +1,85 @@ +/* Predicate.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Tests whether an expression matches against a given context node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +class Predicate + extends Test +{ + + final Expr expr; + + Predicate(Expr expr) + { + this.expr = expr; + } + + public boolean matches(Node node, int pos, int len) + { + Object ret = expr.evaluate(node, pos, len); + if (ret instanceof Double) + { + // Same as [position() = x] + return ((Double) ret).intValue() == pos; + } + return Expr._boolean(node, expr.evaluate(node, pos, len)); + } + + public Test clone(Object context) + { + return new Predicate(expr.clone(context)); + } + + public boolean references(QName var) + { + return expr.references(var); + } + + public String toString() + { + return "[" + expr + "]"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/RelationalExpr.java b/libjava/classpath/gnu/xml/xpath/RelationalExpr.java new file mode 100644 index 00000000000..5cbb6bb2260 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/RelationalExpr.java @@ -0,0 +1,107 @@ +/* RelationalExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * Numerical comparison expression. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class RelationalExpr + extends Expr +{ + + final Expr lhs; + final Expr rhs; + final boolean lt; + final boolean eq; + + RelationalExpr(Expr lhs, Expr rhs, boolean lt, boolean eq) + { + this.lhs = lhs; + this.rhs = rhs; + this.lt = lt; + this.eq = eq; + } + + public Object evaluate(Node context, int pos, int len) + { + Object left = lhs.evaluate(context, pos, len); + Object right = rhs.evaluate(context, pos, len); + double ln = _number(context, left); + double rn = _number(context, right); + if (eq && ln == rn) + { + return Boolean.TRUE; + } + if (lt) + { + if (ln < rn || Double.isInfinite(rn)) + { + return Boolean.TRUE; + } + } + else + { + if (ln > rn || Double.isInfinite(ln)) + { + return Boolean.TRUE; + } + } + return Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new RelationalExpr(lhs.clone(context), rhs.clone(context), lt, eq); + } + + public boolean references(QName var) + { + return (lhs.references(var) || rhs.references(var)); + } + + public String toString() + { + return lhs + " " + (lt ? "<" : ">") + (eq ? "=" : "") + " " + rhs; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Root.java b/libjava/classpath/gnu/xml/xpath/Root.java new file mode 100644 index 00000000000..1f79b395812 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Root.java @@ -0,0 +1,87 @@ +/* Root.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.Collections; +import javax.xml.namespace.QName; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +/** + * Expression that evaluates to the document root. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class Root + extends Path +{ + + public boolean matches(Node node) + { + return (node.getNodeType() == Node.DOCUMENT_NODE); + } + + public Object evaluate(Node context, int pos, int len) + { + return evaluate(context, Collections.EMPTY_SET); + } + + Collection evaluate(Node context, Collection ns) + { + Document doc = (context instanceof Document) ? (Document) context : + context.getOwnerDocument(); + return Collections.singleton(doc); + } + + public Expr clone(Object context) + { + return new Root(); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + return "/"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/RoundFunction.java b/libjava/classpath/gnu/xml/xpath/RoundFunction.java new file mode 100644 index 00000000000..bfbfd357da8 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/RoundFunction.java @@ -0,0 +1,96 @@ +/* RoundFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>round</code> function returns the number that is closest to the + * argument and that is an integer. If there are two such numbers, then the + * one that is closest to positive infinity is returned. If the argument is + * NaN, then NaN is returned. If the argument is positive infinity, then + * positive infinity is returned. If the argument is negative infinity, then + * negative infinity is returned. If the argument is positive zero, then + * positive zero is returned. If the argument is negative zero, then + * negative zero is returned. If the argument is less than zero, but greater + * than or equal to -0.5, then negative zero is returned. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class RoundFunction + extends Expr +{ + + final Expr arg; + + RoundFunction(List args) + { + this((Expr) args.get(0)); + } + + RoundFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + double n = _number(context, val); + return (Double.isNaN(n) || Double.isInfinite(n)) ? + new Double(n) : new Double(Math.round(n)); + } + + public Expr clone(Object context) + { + return new RoundFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "round(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Selector.java b/libjava/classpath/gnu/xml/xpath/Selector.java new file mode 100644 index 00000000000..598038064b5 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Selector.java @@ -0,0 +1,519 @@ +/* Selector.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import org.w3c.dom.Attr; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * A single component of a location path. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class Selector + extends Path +{ + + public static final int ANCESTOR = 0; + public static final int ANCESTOR_OR_SELF = 1; + public static final int ATTRIBUTE = 2; + public static final int CHILD = 3; + public static final int DESCENDANT = 4; + public static final int DESCENDANT_OR_SELF = 5; + public static final int FOLLOWING = 6; + public static final int FOLLOWING_SIBLING = 7; + public static final int NAMESPACE = 8; + public static final int PARENT = 9; + public static final int PRECEDING = 10; + public static final int PRECEDING_SIBLING = 11; + public static final int SELF = 12; + + /** + * Axis to select nodes in. + */ + final int axis; + + /** + * List of tests to perform on candidates. + */ + final Test[] tests; + + public Selector(int axis, List tests) + { + this.axis = axis; + this.tests = new Test[tests.size()]; + tests.toArray(this.tests); + if (axis == NAMESPACE && + this.tests.length > 0 && + this.tests[0] instanceof NameTest) + { + NameTest nt = (NameTest) this.tests[0]; + this.tests[0] = new NamespaceTest(nt.qName, nt.anyLocalName, nt.any); + } + } + + /** + * Returns the list of tests to perform on candidates. + */ + public Test[] getTests() + { + return tests; + } + + public boolean matches(Node context) + { + short nodeType = context.getNodeType(); + switch (axis) + { + case CHILD: + if (nodeType == Node.ATTRIBUTE_NODE) + { + return false; + } + break; + case ATTRIBUTE: + case NAMESPACE: + if (nodeType != Node.ATTRIBUTE_NODE) + { + return false; + } + break; + case DESCENDANT_OR_SELF: + return true; + default: + return false; + } + int tlen = tests.length; + if (tlen > 0) + { + int pos = getContextPosition(context); + int len = getContextSize(context); + for (int j = 0; j < tlen && len > 0; j++) + { + Test test = tests[j]; + if (!test.matches(context, pos, len)) + { + return false; + } + } + } + return true; + } + + private int getContextPosition(Node ctx) + { + int pos = 1; + for (ctx = ctx.getPreviousSibling(); ctx != null; + ctx = ctx.getPreviousSibling()) + { + pos++; + } + return pos; + } + + private int getContextSize(Node ctx) + { + if (ctx.getNodeType() == Node.ATTRIBUTE_NODE) + { + Node parent = ((Attr) ctx).getOwnerElement(); + return parent.getAttributes().getLength(); + } + Node parent = ctx.getParentNode(); + if (parent != null) + { + return parent.getChildNodes().getLength(); + } + return 1; + } + + public Object evaluate(Node context, int pos, int len) + { + Set acc = new LinkedHashSet(); + addCandidates(context, acc); + List candidates = new ArrayList(acc); + //Collections.sort(candidates, documentOrderComparator); + List ret = filterCandidates(candidates, false); + return ret; + } + + Collection evaluate(Node context, Collection ns) + { + Set acc = new LinkedHashSet(); + for (Iterator i = ns.iterator(); i.hasNext(); ) + { + addCandidates((Node) i.next(), acc); + } + List candidates = new ArrayList(acc); + //Collections.sort(candidates, documentOrderComparator); + List ret = filterCandidates(candidates, true); + return ret; + } + + /** + * Filter the given list of candidates according to the node tests. + */ + List filterCandidates(List candidates, boolean cascade) + { + int len = candidates.size(); + int tlen = tests.length; + if (tlen > 0 && len > 0) + { + // Present the result of each successful generation to the next test + for (int j = 0; j < tlen && len > 0; j++) + { + Test test = tests[j]; + List successful = new ArrayList(len); + for (int i = 0; i < len; i++) + { + Node node = (Node) candidates.get(i); + if (cascade) + { + // Documents and DocumentFragments should be considered + // if part of a location path where the axis involves + // the SELF concept + short nodeType = node.getNodeType(); + if ((nodeType == Node.DOCUMENT_NODE || + nodeType == Node.DOCUMENT_FRAGMENT_NODE) && + (axis == DESCENDANT_OR_SELF || + axis == ANCESTOR_OR_SELF || + axis == SELF) && + (tests.length == 1 && + tests[0] instanceof NodeTypeTest && + ((NodeTypeTest) tests[0]).type == (short) 0)) + { + successful.add(node); + continue; + } + } + if (test.matches(node, i + 1, len)) + { + successful.add(node); + } + /* + System.err.println("Testing "+node); + int p = getContextPosition(node); + int l = getContextSize(node); + if (test.matches(node, p, l)) + { + successful.add(node); + }*/ + } + candidates = successful; + len = candidates.size(); + } + } + return candidates; + } + + void addCandidates(Node context, Collection candidates) + { + // Build list of candidates + switch (axis) + { + case CHILD: + addChildNodes(context, candidates, false); + break; + case DESCENDANT: + addChildNodes(context, candidates, true); + break; + case DESCENDANT_OR_SELF: + candidates.add (context); + addChildNodes(context, candidates, true); + break; + case PARENT: + addParentNode(context, candidates, false); + break; + case ANCESTOR: + addParentNode(context, candidates, true); + break; + case ANCESTOR_OR_SELF: + candidates.add(context); + addParentNode(context, candidates, true); + break; + case FOLLOWING_SIBLING: + addFollowingNodes(context, candidates, false); + break; + case PRECEDING_SIBLING: + addPrecedingNodes(context, candidates, false); + break; + case FOLLOWING: + addFollowingNodes(context, candidates, true); + break; + case PRECEDING: + addPrecedingNodes(context, candidates, true); + break; + case ATTRIBUTE: + addAttributes(context, candidates); + break; + case NAMESPACE: + addNamespaceAttributes(context, candidates); + break; + case SELF: + candidates.add(context); + break; + } + } + + void addChildNodes(Node context, Collection acc, boolean recurse) + { + Node child = context.getFirstChild(); + while (child != null) + { + acc.add(child); + if (recurse) + { + addChildNodes(child, acc, recurse); + } + child = child.getNextSibling(); + } + } + + void addParentNode(Node context, Collection acc, boolean recurse) + { + Node parent = (context.getNodeType() == Node.ATTRIBUTE_NODE) ? + ((Attr) context).getOwnerElement() : context.getParentNode(); + if (parent != null) + { + acc.add(parent); + if (recurse) + { + addParentNode(parent, acc, recurse); + } + } + } + + void addFollowingNodes(Node context, Collection acc, boolean recurse) + { + Node cur = context.getNextSibling(); + while (cur != null) + { + acc.add(cur); + if (recurse) + { + addChildNodes(cur, acc, true); + } + cur = cur.getNextSibling(); + } + if (recurse) + { + context = (context.getNodeType() == Node.ATTRIBUTE_NODE) ? + ((Attr) context).getOwnerElement() : context.getParentNode(); + if (context != null) + { + addFollowingNodes(context, acc, recurse); + } + } + } + + void addPrecedingNodes(Node context, Collection acc, boolean recurse) + { + Node cur = context.getPreviousSibling(); + while (cur != null) + { + acc.add(cur); + if (recurse) + { + addChildNodes(cur, acc, true); + } + cur = cur.getPreviousSibling(); + } + if (recurse) + { + context = (context.getNodeType() == Node.ATTRIBUTE_NODE) ? + ((Attr) context).getOwnerElement() : context.getParentNode(); + if (context != null) + { + addPrecedingNodes(context, acc, recurse); + } + } + } + + void addAttributes(Node context, Collection acc) + { + NamedNodeMap attrs = context.getAttributes(); + if (attrs != null) + { + int attrLen = attrs.getLength(); + for (int i = 0; i < attrLen; i++) + { + Node attr = attrs.item(i); + if (!isNamespaceAttribute(attr)) + { + acc.add(attr); + } + } + } + } + + void addNamespaceAttributes(Node context, Collection acc) + { + NamedNodeMap attrs = context.getAttributes(); + if (attrs != null) + { + int attrLen = attrs.getLength(); + for (int i = 0; i < attrLen; i++) + { + Node attr = attrs.item(i); + if (isNamespaceAttribute(attr)) + { + acc.add(attr); + } + } + } + } + + final boolean isNamespaceAttribute(Node node) + { + String uri = node.getNamespaceURI(); + return (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) || + XMLConstants.XMLNS_ATTRIBUTE.equals(node.getPrefix()) || + XMLConstants.XMLNS_ATTRIBUTE.equals(node.getNodeName())); + } + + public Expr clone(Object context) + { + int len = tests.length; + List tests2 = new ArrayList(len); + for (int i = 0; i < len; i++) + { + tests2.add(tests[i].clone(context)); + } + return new Selector(axis, tests2); + } + + public boolean references(QName var) + { + for (int i = 0; i < tests.length; i++) + { + if (tests[i].references(var)) + { + return true; + } + } + return false; + } + + public String toString() + { + StringBuffer buf = new StringBuffer(); + switch (axis) + { + case ANCESTOR: + buf.append("ancestor::"); + break; + case ANCESTOR_OR_SELF: + buf.append("ancestor-or-self::"); + break; + case ATTRIBUTE: + if (tests.length == 0 || + (tests[0] instanceof NameTest)) + { + buf.append('@'); + } + else + { + buf.append("attribute::"); + } + break; + case CHILD: + //buf.append("child::"); + break; + case DESCENDANT: + buf.append("descendant::"); + break; + case DESCENDANT_OR_SELF: + buf.append("descendant-or-self::"); + break; + case FOLLOWING: + buf.append("following::"); + break; + case FOLLOWING_SIBLING: + buf.append("following-sibling::"); + break; + case NAMESPACE: + buf.append("namespace::"); + break; + case PARENT: + if (tests.length == 0 || + (tests[0] instanceof NodeTypeTest && + ((NodeTypeTest) tests[0]).type == 0)) + { + return ".."; + } + buf.append("parent::"); + break; + case PRECEDING: + buf.append("preceding::"); + break; + case PRECEDING_SIBLING: + buf.append("preceding-sibling::"); + break; + case SELF: + if (tests.length == 0 || + (tests[0] instanceof NodeTypeTest && + ((NodeTypeTest) tests[0]).type == 0)) + { + return "."; + } + buf.append("self::"); + break; + } + if (tests.length == 0) + { + buf.append('*'); + } + else + { + for (int i = 0; i < tests.length; i++) + { + buf.append(tests[i]); + } + } + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/StartsWithFunction.java b/libjava/classpath/gnu/xml/xpath/StartsWithFunction.java new file mode 100644 index 00000000000..ec021551e76 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/StartsWithFunction.java @@ -0,0 +1,92 @@ +/* StartsWithFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>starts-with</code> function returns true if the first argument + * string starts with the second argument string, and otherwise returns false. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class StartsWithFunction + extends Expr +{ + + final Expr arg1; + final Expr arg2; + + StartsWithFunction(List args) + { + this((Expr) args.get(0), (Expr) args.get(1)); + } + + StartsWithFunction(Expr arg1, Expr arg2) + { + this.arg1 = arg1; + this.arg2 = arg2; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val1 = arg1.evaluate(context, pos, len); + Object val2 = arg2.evaluate(context, pos, len); + String s1 = _string(context, val1); + String s2 = _string(context, val2); + return s1.startsWith(s2) ? Boolean.TRUE : Boolean.FALSE; + } + + public Expr clone(Object context) + { + return new StartsWithFunction(arg1.clone(context), arg2.clone(context)); + } + + public boolean references(QName var) + { + return (arg1.references(var) || arg2.references(var)); + } + + public String toString() + { + return "starts-with(" + arg1 + "," + arg2 + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Steps.java b/libjava/classpath/gnu/xml/xpath/Steps.java new file mode 100644 index 00000000000..9ef6cd35f3d --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Steps.java @@ -0,0 +1,253 @@ +/* Steps.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.Set; +import javax.xml.namespace.QName; +import org.w3c.dom.Attr; +import org.w3c.dom.Node; + +/** + * A list of transitions between components in a location path. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class Steps + extends Path +{ + + final LinkedList path; + + public Steps() + { + this(new LinkedList()); + } + + Steps(LinkedList path) + { + this.path = path; + } + + public boolean matches(Node context) + { + // Right to left + return matches(context, path.size() - 1); + } + + boolean matches(Node context, int pos) + { + Pattern right = (Pattern) path.get(pos); + if (!right.matches(context)) + { + return false; + } + if (pos > 0) + { + Pattern left = (Pattern) path.get(pos - 1); + Iterator j = possibleContexts(right, context).iterator(); + while (j.hasNext()) + { + Node candidate = (Node) j.next(); + if (left.matches(candidate) && + matches(candidate, pos - 1)) + { + return true; + } + // keep going, there may be another candidate + } + return false; + } + return true; + } + + /** + * Essentially the reverse of Selector.addCandidates. + * The idea is to determine possible context nodes for a match. + */ + Collection possibleContexts(Pattern pattern, Node context) + { + if (pattern instanceof Selector) + { + Selector s = (Selector) pattern; + Collection candidates = new LinkedHashSet(); + switch (s.axis) + { + case Selector.PARENT: + s.addChildNodes(context, candidates, false); + break; + case Selector.ANCESTOR: + s.addChildNodes(context, candidates, true); + break; + case Selector.ANCESTOR_OR_SELF: + candidates.add (context); + s.addChildNodes(context, candidates, true); + break; + case Selector.CHILD: + s.addParentNode(context, candidates, false); + break; + case Selector.DESCENDANT: + s.addParentNode(context, candidates, true); + break; + case Selector.DESCENDANT_OR_SELF: + candidates.add(context); + s.addParentNode(context, candidates, true); + break; + case Selector.PRECEDING_SIBLING: + s.addFollowingNodes(context, candidates, false); + break; + case Selector.FOLLOWING_SIBLING: + s.addPrecedingNodes(context, candidates, false); + break; + case Selector.PRECEDING: + s.addFollowingNodes(context, candidates, true); + break; + case Selector.FOLLOWING: + s.addPrecedingNodes(context, candidates, true); + break; + case Selector.ATTRIBUTE: + case Selector.NAMESPACE: + if (context.getNodeType() == Node.ATTRIBUTE_NODE) + { + candidates.add(((Attr) context).getOwnerElement()); + } + break; + case Selector.SELF: + candidates.add(context); + break; + } + return candidates; + } + return Collections.EMPTY_SET; + } + + public Object evaluate(Node context, int pos, int len) + { + //System.err.println(toString()+" evaluate"); + // Left to right + Iterator i = path.iterator(); + Expr lhs = (Expr) i.next(); + Object val = lhs.evaluate(context, pos, len); + //System.err.println("\tevaluate "+lhs+" = "+val); + while (val instanceof Collection && i.hasNext()) + { + Path rhs = (Path) i.next(); + val = rhs.evaluate(context, (Collection) val); + //System.err.println("\tevaluate "+rhs+" = "+val); + } + return val; + } + + Collection evaluate(Node context, Collection ns) + { + // Left to right + Iterator i = path.iterator(); + Expr lhs = (Expr) i.next(); + if (lhs instanceof Path) + { + ns = ((Path) lhs).evaluate(context, ns); + } + else + { + Set acc = new LinkedHashSet(); + int pos = 1, len = ns.size(); + for (Iterator j = ns.iterator(); j.hasNext(); ) + { + Node node = (Node) j.next(); + Object ret = lhs.evaluate(node, pos++, len); + if (ret instanceof Collection) + { + acc.addAll((Collection) ret); + } + } + ns = acc; + } + while (i.hasNext()) + { + Path rhs = (Path) i.next(); + ns = rhs.evaluate(context, ns); + } + return ns; + } + + public Expr clone(Object context) + { + int len = path.size(); + LinkedList path2 = new LinkedList(); + for (int i = 0; i < len; i++) + { + path2.add(((Expr) path.get(i)).clone(context)); + } + return new Steps(path2); + } + + public boolean references(QName var) + { + for (Iterator i = path.iterator(); i.hasNext(); ) + { + if (((Expr) i.next()).references(var)) + { + return true; + } + } + return false; + } + + public String toString() + { + StringBuffer buf = new StringBuffer(); + Iterator i = path.iterator(); + Expr expr = (Expr) i.next(); + if (!(expr instanceof Root)) + { + buf.append(expr); + } + while (i.hasNext()) + { + expr = (Expr) i.next(); + buf.append('/'); + buf.append(expr); + } + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/StringFunction.java b/libjava/classpath/gnu/xml/xpath/StringFunction.java new file mode 100644 index 00000000000..0a4c681d16b --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/StringFunction.java @@ -0,0 +1,119 @@ +/* StringFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>string function converts an object to a string as follows: + * <ul> + * <li>A node-set is converted to a string by returning the string-value of + * the node in the node-set that is first in document order. If the node-set + * is empty, an empty string is returned.</li> + * <li>A number is converted to a string as follows + * <ul> + * <li>NaN is converted to the string NaN</li> + * <li>positive zero is converted to the string 0</li> + * <li>negative zero is converted to the string 0</li> + * <li>positive infinity is converted to the string Infinity</li> + * <li>negative infinity is converted to the string -Infinity</li> + * <li>if the number is an integer, the number is represented in decimal + * form as a Number with no decimal point and no leading zeros, preceded by + * a minus sign (-) if the number is negative</li> + * <li>otherwise, the number is represented in decimal form as a Number + * including a decimal point with at least one digit before the decimal + * point and at least one digit after the decimal point, preceded by a minus + * sign (-) if the number is negative; there must be no leading zeros before + * the decimal point apart possibly from the one required digit immediately + * before the decimal point; beyond the one required digit after the decimal + * point there must be as many, but only as many, more digits as are needed + * to uniquely distinguish the number from all other IEEE 754 numeric + * values.</li> + * </ul> + * </li> + * <li>The boolean false value is converted to the string false. The boolean + * true value is converted to the string true.</li> + * <li>An object of a type other than the four basic types is converted to a + * string in a way that is dependent on that type.</li> + * </ul> + * If the argument is omitted, it defaults to a node-set with the context + * node as its only member. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class StringFunction + extends Expr +{ + + final Expr arg; + + StringFunction(List args) + { + this(args.size() > 0 ? (Expr) args.get(0) : null); + } + + StringFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + return _string(context, val); + } + + public Expr clone(Object context) + { + return new StringFunction((arg == null) ? null : + arg.clone(context)); + } + + public boolean references(QName var) + { + return (arg == null) ? false : arg.references(var); + } + + public String toString() + { + return (arg == null) ? "string()" : "string(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/StringLengthFunction.java b/libjava/classpath/gnu/xml/xpath/StringLengthFunction.java new file mode 100644 index 00000000000..7f5ceafeef5 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/StringLengthFunction.java @@ -0,0 +1,91 @@ +/* StringLengthFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>string-length</code> function returns the number of characters + * in the string. + * If the argument is omitted, it defaults to the context node converted to + * a string, in other words the string-value of the context node. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class StringLengthFunction + extends Expr +{ + + final Expr arg; + + StringLengthFunction(List args) + { + this(args.isEmpty() ? null : (Expr) args.get(0)); + } + + StringLengthFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = (arg == null) ? null : arg.evaluate(context, pos, len); + String s = _string(context, val); + return new Double((double) s.length()); + } + + public Expr clone(Object context) + { + return new StringLengthFunction((arg == null) ? null : + arg.clone(context)); + } + + public boolean references(QName var) + { + return (arg == null) ? false : arg.references(var); + } + + public String toString() + { + return (arg == null) ? "string-length()" : "string-length(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java b/libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java new file mode 100644 index 00000000000..8144cf84433 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java @@ -0,0 +1,98 @@ +/* SubstringAfterFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>substring-after</code> function returns the substring of the + * first argument string that follows the first occurrence of the second + * argument string in the first argument string, or the empty string if the + * first argument string does not contain the second argument string. For + * example, substring-after("1999/04/01","/") returns 04/01, and + * substring-after("1999/04/01","19") returns 99/04/01. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class SubstringAfterFunction + extends Expr +{ + + final Expr arg1; + final Expr arg2; + + SubstringAfterFunction(List args) + { + this((Expr) args.get(0), (Expr) args.get(1)); + } + + SubstringAfterFunction(Expr arg1, Expr arg2) + { + this.arg1 = arg1; + this.arg2 = arg2; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val1 = arg1.evaluate(context, pos, len); + Object val2 = arg2.evaluate(context, pos, len); + String s1 = _string(context, val1); + String s2 = _string(context, val2); + int index = s1.indexOf(s2); + return (index == -1) ? "" : s1.substring(index + s2.length()); + } + + public Expr clone(Object context) + { + return new SubstringAfterFunction(arg1.clone(context), + arg2.clone(context)); + } + + public boolean references(QName var) + { + return (arg1.references(var) || arg2.references(var)); + } + + public String toString() + { + return "substring-after(" + arg1 + "," + arg2 + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java b/libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java new file mode 100644 index 00000000000..2d92a3e0349 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java @@ -0,0 +1,97 @@ +/* SubstringBeforeFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>substring-before</code> function returns the substring of the + * first argument string that precedes the first occurrence of the second + * argument string in the first argument string, or the empty string if the + * first argument string does not contain the second argument string. For + * example, substring-before("1999/04/01","/") returns 1999. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class SubstringBeforeFunction + extends Expr +{ + + final Expr arg1; + final Expr arg2; + + SubstringBeforeFunction(List args) + { + this((Expr) args.get(0), (Expr) args.get(1)); + } + + SubstringBeforeFunction(Expr arg1, Expr arg2) + { + this.arg1 = arg1; + this.arg2 = arg2; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val1 = arg1.evaluate(context, pos, len); + Object val2 = arg2.evaluate(context, pos, len); + String s1 = _string(context, val1); + String s2 = _string(context, val2); + int index = s1.indexOf(s2); + return (index == -1) ? "" : s1.substring(0, index); + } + + public Expr clone(Object context) + { + return new SubstringBeforeFunction(arg1.clone(context), + arg2.clone(context)); + } + + public boolean references(QName var) + { + return (arg1.references(var) || arg2.references(var)); + } + + public String toString() + { + return "substring-before(" + arg1 + "," + arg2 + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/SubstringFunction.java b/libjava/classpath/gnu/xml/xpath/SubstringFunction.java new file mode 100644 index 00000000000..d65c4facb8a --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/SubstringFunction.java @@ -0,0 +1,129 @@ +/* SubstringFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The substring function returns the substring of the first argument + * starting at the position specified in the second argument with length + * specified in the third argument. For example, substring("12345",2,3) + * returns "234". If the third argument is not specified, it returns the + * substring starting at the position specified in the second argument and + * continuing to the end of the string. For example, substring("12345",2) + * returns "2345". + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class SubstringFunction + extends Expr +{ + + final Expr arg1; + final Expr arg2; + final Expr arg3; + + SubstringFunction(List args) + { + this((Expr) args.get(0), (Expr) args.get(1), + (args.size() > 2) ? (Expr) args.get(2) : null); + } + + SubstringFunction(Expr arg1, Expr arg2, Expr arg3) + { + this.arg1 = arg1; + this.arg2 = arg2; + this.arg3 = arg3; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val1 = arg1.evaluate(context, pos, len); + Object val2 = arg2.evaluate(context, pos, len); + String s = _string(context, val1); + int p = (val2 instanceof Double) ? + ((Double) val2).intValue() : + (int) Math.round(_number(context, val2)); + p--; + if (p < 0) + { + p = 0; + } + + int l = s.length() - p; + if (l <= 0) + { + return ""; + } + + if (arg3 != null) + { + Object val3 = arg3.evaluate(context, pos, len); + int v3 = (val3 instanceof Double) ? + ((Double) val3).intValue() : + (int) Math.round(_number(context, val3)); + if (v3 < l) + { + l = v3; + } + } + + return s.substring(p, p + l); + } + + public Expr clone(Object context) + { + return new SubstringFunction(arg1.clone(context), arg2.clone(context), + (arg3 == null) ? null : arg3.clone(context)); + } + + public boolean references(QName var) + { + return (arg1.references(var) || arg2.references(var) || + (arg3 == null) ? false : arg3.references(var)); + } + + public String toString() + { + return (arg3 == null) ? "substring(" + arg1 + "," + arg2 + ")" : + "substring(" + arg1 + "," + arg2 + "," + arg3 + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/SumFunction.java b/libjava/classpath/gnu/xml/xpath/SumFunction.java new file mode 100644 index 00000000000..93c2e806d48 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/SumFunction.java @@ -0,0 +1,100 @@ +/* SumFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>sum</code> function returns the sum, for each node in the + * argument node-set, of the result of converting the string-values of the + * node to a number. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class SumFunction + extends Expr +{ + + final Expr arg; + + SumFunction(List args) + { + this((Expr) args.get(0)); + } + + SumFunction(Expr arg) + { + this.arg = arg; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val = arg.evaluate(context, pos, len); + double sum = 0.0d; + if (val instanceof Collection) + { + for (Iterator i = ((Collection) val).iterator(); i.hasNext(); ) + { + Node node = (Node) i.next(); + String s = stringValue(node); + sum += _number(context, s); + } + } + return new Double(sum); + } + + public Expr clone(Object context) + { + return new SumFunction(arg.clone(context)); + } + + public boolean references(QName var) + { + return arg.references(var); + } + + public String toString() + { + return "sum(" + arg + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/Test.java b/libjava/classpath/gnu/xml/xpath/Test.java new file mode 100644 index 00000000000..94837ffaca1 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/Test.java @@ -0,0 +1,58 @@ +/* Test.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * A test that can be performed on a node to determine whether to include it + * in a selection. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class Test +{ + + public abstract boolean matches(Node node, int pos, int len); + + public abstract Test clone(Object context); + + public abstract boolean references(QName var); + +} diff --git a/libjava/classpath/gnu/xml/xpath/TranslateFunction.java b/libjava/classpath/gnu/xml/xpath/TranslateFunction.java new file mode 100644 index 00000000000..54e8b9c37f5 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/TranslateFunction.java @@ -0,0 +1,133 @@ +/* TranslateFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.List; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>translate</code> function returns the first argument string + * with occurrences of characters in the second argument string replaced by + * the character at the corresponding position in the third argument string. + * For example, translate("bar","abc","ABC") returns the string BAr. If + * there is a character in the second argument string with no character at a + * corresponding position in the third argument string (because the second + * argument string is longer than the third argument string), then + * occurrences of that character in the first argument string are removed. + * For example, translate("--aaa--","abc-","ABC") returns "AAA". If a + * character occurs more than once in the second argument string, then the + * first occurrence determines the replacement character. If the third + * argument string is longer than the second argument string, then excess + * characters are ignored. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class TranslateFunction + extends Expr +{ + + final Expr arg1; + final Expr arg2; + final Expr arg3; + + TranslateFunction(List args) + { + this((Expr) args.get(0), (Expr) args.get(1), (Expr) args.get(2)); + } + + TranslateFunction(Expr arg1, Expr arg2, Expr arg3) + { + this.arg1 = arg1; + this.arg2 = arg2; + this.arg3 = arg3; + } + + public Object evaluate(Node context, int pos, int len) + { + Object val1 = arg1.evaluate(context, pos, len); + Object val2 = arg2.evaluate(context, pos, len); + Object val3 = arg3.evaluate(context, pos, len); + String string = _string(context, val1); + String search = _string(context, val2); + String replace = _string(context, val3); + StringBuffer buf = new StringBuffer(); + int l1 = string.length(); + int l2 = search.length(); + int l3 = replace.length(); + for (int i = 0; i < l1; i++) + { + char c = string.charAt(i); + boolean replaced = false; + for (int j = 0; j < l2; j++) + { + if (c == search.charAt(j)) + { + if (j < l3) + { + buf.append(replace.charAt(j)); + } + replaced = true; + } + } + if (!replaced) + { + buf.append(c); + } + } + return new String(buf); + } + + public Expr clone(Object context) + { + return new TranslateFunction(arg1.clone(context), arg2.clone(context), + arg3.clone(context)); + } + + public boolean references(QName var) + { + return (arg1.references(var) || arg2.references(var) || + arg3.references(var)); + } + + public String toString() + { + return "translate(" + arg1 + "," + arg2 + "," + arg3 + ")"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/TrueFunction.java b/libjava/classpath/gnu/xml/xpath/TrueFunction.java new file mode 100644 index 00000000000..6ad42d726fb --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/TrueFunction.java @@ -0,0 +1,72 @@ +/* TrueFunction.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The <code>true</code> function returns true. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +final class TrueFunction + extends Expr +{ + + public Object evaluate(Node context, int pos, int len) + { + return Boolean.TRUE; + } + + public Expr clone(Object context) + { + return new TrueFunction(); + } + + public boolean references(QName var) + { + return false; + } + + public String toString() + { + return "true()"; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/UnionExpr.java b/libjava/classpath/gnu/xml/xpath/UnionExpr.java new file mode 100644 index 00000000000..5078713b693 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/UnionExpr.java @@ -0,0 +1,108 @@ +/* UnionExpr.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.xml.namespace.QName; +import org.w3c.dom.Node; + +/** + * The union of two node-sets. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public final class UnionExpr + extends Pattern +{ + + final Expr lhs; + final Expr rhs; + + public UnionExpr(Expr lhs, Expr rhs) + { + this.lhs = lhs; + this.rhs = rhs; + } + + public boolean matches(Node context) + { + if (lhs instanceof Pattern && rhs instanceof Pattern) + { + return ((Pattern) lhs).matches(context) || + ((Pattern) rhs).matches(context); + } + return false; + } + + public Object evaluate(Node context, int pos, int len) + { + Object left = lhs.evaluate(context, pos, len); + Object right = rhs.evaluate(context, pos, len); + if (left instanceof Collection && right instanceof Collection) + { + Set set = new HashSet(); + set.addAll ((Collection) left); + set.addAll ((Collection) right); + List list = new ArrayList(set); + Collections.sort(list, documentOrderComparator); + return list; + } + return Collections.EMPTY_SET; + } + + public Expr clone(Object context) + { + return new UnionExpr(lhs.clone(context), rhs.clone(context)); + } + + public boolean references(QName var) + { + return (lhs.references(var) || rhs.references(var)); + } + + public String toString() + { + return lhs + " | " + rhs; + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/VariableReference.java b/libjava/classpath/gnu/xml/xpath/VariableReference.java new file mode 100644 index 00000000000..813a37f96eb --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/VariableReference.java @@ -0,0 +1,100 @@ +/* VariableReference.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathVariableResolver; +import org.w3c.dom.Node; +import gnu.xml.transform.Bindings; + +public class VariableReference + extends Expr +{ + + final XPathVariableResolver resolver; + final QName name; + + public VariableReference(XPathVariableResolver resolver, QName name) + { + this.resolver = resolver; + this.name = name; + } + + public Object evaluate(Node context, int pos, int len) + { + if (resolver != null) + { + if (resolver instanceof Bindings) + { + // Needs context to operate properly + return ((Bindings) resolver).get(name, context, pos, len); + } + return resolver.resolveVariable(name); + } + throw new IllegalStateException("no variable resolver"); + } + + public Expr clone(Object context) + { + XPathVariableResolver r = resolver; + if (context instanceof XPathVariableResolver) + { + r = (XPathVariableResolver) context; + } + return new VariableReference(r, name); + } + + public boolean references(QName var) + { + return name.equals(var); + } + + public String toString() + { + StringBuffer buf = new StringBuffer("$"); + String prefix = name.getPrefix(); + if (prefix != null && !"".equals(prefix)) + { + buf.append(prefix); + buf.append(':'); + } + buf.append(name.getLocalPart()); + return buf.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java b/libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java new file mode 100644 index 00000000000..45dc57ed14a --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java @@ -0,0 +1,90 @@ +/* XPathFactoryImpl.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathFactory; +import javax.xml.xpath.XPathFactoryConfigurationException; +import javax.xml.xpath.XPathFunctionResolver; +import javax.xml.xpath.XPathVariableResolver; + +/** + * GNU XPath factory implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XPathFactoryImpl + extends XPathFactory +{ + + XPathVariableResolver variableResolver; + XPathFunctionResolver functionResolver; + + public boolean isObjectModelSupported(String objectModel) + { + return XPathFactory.DEFAULT_OBJECT_MODEL_URI.equals(objectModel); + } + + public void setFeature(String name, boolean value) + throws XPathFactoryConfigurationException + { + throw new XPathFactoryConfigurationException(name); + } + + public boolean getFeature(String name) + throws XPathFactoryConfigurationException + { + throw new XPathFactoryConfigurationException(name); + } + + public void setXPathVariableResolver(XPathVariableResolver resolver) + { + variableResolver = resolver; + } + + public void setXPathFunctionResolver(XPathFunctionResolver resolver) + { + functionResolver = resolver; + } + + public XPath newXPath() + { + return new XPathImpl(null, variableResolver, functionResolver); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/XPathImpl.java b/libjava/classpath/gnu/xml/xpath/XPathImpl.java new file mode 100644 index 00000000000..3511834ca90 --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/XPathImpl.java @@ -0,0 +1,164 @@ +/* XPathImpl.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.io.IOException; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFunctionResolver; +import javax.xml.xpath.XPathVariableResolver; +import org.xml.sax.InputSource; + +/** + * JAXP XPath implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XPathImpl + implements XPath +{ + + XPathParser parser; + NamespaceContext namespaceContext; + XPathVariableResolver variableResolver; + XPathFunctionResolver functionResolver; + + XPathImpl(NamespaceContext namespaceContext, + XPathVariableResolver variableResolver, + XPathFunctionResolver functionResolver) + { + parser = new XPathParser(); + this.namespaceContext = namespaceContext; + this.variableResolver = variableResolver; + this.functionResolver = functionResolver; + reset(); + } + + public void reset() + { + parser.namespaceContext = namespaceContext; + parser.variableResolver = variableResolver; + parser.functionResolver = functionResolver; + } + + public void setXPathVariableResolver(XPathVariableResolver resolver) + { + parser.variableResolver = resolver; + } + + public XPathVariableResolver getXPathVariableResolver() + { + return parser.variableResolver; + } + + public void setXPathFunctionResolver(XPathFunctionResolver resolver) + { + parser.functionResolver = resolver; + } + + public XPathFunctionResolver getXPathFunctionResolver() + { + return parser.functionResolver; + } + + public void setNamespaceContext(NamespaceContext nsContext) + { + parser.namespaceContext = nsContext; + } + + public NamespaceContext getNamespaceContext() + { + return parser.namespaceContext; + } + + public XPathExpression compile(String expression) + throws XPathExpressionException + { + XPathTokenizer tokenizer = new XPathTokenizer(expression); + try + { + return (Expr) parser.yyparse(tokenizer); + } + catch (IOException e) + { + throw new XPathExpressionException(e); + } + catch (XPathParser.yyException e) + { + throw new XPathExpressionException(expression); + } + } + + public Object evaluate(String expression, + Object item, + QName returnType) + throws XPathExpressionException + { + XPathExpression expr = compile(expression); + return expr.evaluate(item, returnType); + } + + public String evaluate(String expression, + Object item) + throws XPathExpressionException + { + XPathExpression expr = compile(expression); + return expr.evaluate(item); + } + + public Object evaluate(String expression, + InputSource source, + QName returnType) + throws XPathExpressionException + { + XPathExpression expr = compile(expression); + return expr.evaluate(source, returnType); + } + + public String evaluate(String expression, + InputSource source) + throws XPathExpressionException + { + XPathExpression expr = compile(expression); + return expr.evaluate(source); + } + +} diff --git a/libjava/classpath/gnu/xml/xpath/XPathParser.java b/libjava/classpath/gnu/xml/xpath/XPathParser.java new file mode 100644 index 00000000000..624dcd8d46a --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/XPathParser.java @@ -0,0 +1,1464 @@ +// created by jay 0.8 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de + + // line 2 "XPathParser.y" +/* + * XPathParser.java + * Copyright (C) 2004 The Free Software Foundation + * + * This file is part of GNU JAXP, a library. + * + * GNU JAXP is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GNU JAXP is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under + * terms of your choice, provided that you also meet, for each linked + * independent module, the terms and conditions of the license of that + * module. An independent module is a module which is not derived from + * or based on this library. If you modify this library, you may extend + * this exception to your version of the library, but you are not + * obliged to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunctionResolver; +import javax.xml.xpath.XPathVariableResolver; +import org.w3c.dom.Node; + +/** + * An XPath 1.0 parser. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XPathParser +{ + + NamespaceContext namespaceContext; + XPathVariableResolver variableResolver; + XPathFunctionResolver functionResolver; + + QName getQName(String name) + { + QName qName = QName.valueOf(name); + if (namespaceContext != null) + { + String prefix = qName.getPrefix(); + String uri = qName.getNamespaceURI(); + if (prefix != null && (uri == null || uri.length() == 0)) + { + uri = namespaceContext.getNamespaceURI(prefix); + String localName = qName.getLocalPart(); + qName = new QName(uri, localName, prefix); + } + } + return qName; + } + + Expr lookupFunction(String name, List args) + { + int arity = args.size(); + if ("position".equals(name) && arity == 0) + { + return new PositionFunction(); + } + else if ("last".equals(name) && arity == 0) + { + return new LastFunction(); + } + else if ("string".equals(name) && (arity == 1 || arity == 0)) + { + return new StringFunction(args); + } + else if ("number".equals(name) && (arity == 1 || arity == 0)) + { + return new NumberFunction(args); + } + else if ("boolean".equals(name) && arity == 1) + { + return new BooleanFunction(args); + } + else if ("count".equals(name) && arity == 1) + { + return new CountFunction(args); + } + else if ("not".equals(name) && arity == 1) + { + return new NotFunction(args); + } + else if ("id".equals(name) && arity == 1) + { + return new IdFunction(args); + } + else if ("concat".equals(name) && arity > 1) + { + return new ConcatFunction(args); + } + else if ("true".equals(name) && arity == 0) + { + return new TrueFunction(); + } + else if ("false".equals(name) && arity == 0) + { + return new FalseFunction(); + } + else if ("name".equals(name) && (arity == 1 || arity == 0)) + { + return new NameFunction(args); + } + else if ("local-name".equals(name) && (arity == 1 || arity == 0)) + { + return new LocalNameFunction(args); + } + else if ("namespace-uri".equals(name) && (arity == 1 || arity == 0)) + { + return new NamespaceUriFunction(args); + } + else if ("starts-with".equals(name) && arity == 2) + { + return new StartsWithFunction(args); + } + else if ("contains".equals(name) && arity == 2) + { + return new ContainsFunction(args); + } + else if ("string-length".equals(name) && (arity == 1 || arity == 0)) + { + return new StringLengthFunction(args); + } + else if ("translate".equals(name) && arity == 3) + { + return new TranslateFunction(args); + } + else if ("normalize-space".equals(name) && (arity == 1 || arity == 0)) + { + return new NormalizeSpaceFunction(args); + } + else if ("substring".equals(name) && (arity == 2 || arity == 3)) + { + return new SubstringFunction(args); + } + else if ("substring-before".equals(name) && arity == 2) + { + return new SubstringBeforeFunction(args); + } + else if ("substring-after".equals(name) && arity == 2) + { + return new SubstringAfterFunction(args); + } + else if ("lang".equals(name) && arity == 1) + { + return new LangFunction(args); + } + else if ("sum".equals(name) && arity == 1) + { + return new SumFunction(args); + } + else if ("floor".equals(name) && arity == 1) + { + return new FloorFunction(args); + } + else if ("ceiling".equals(name) && arity == 1) + { + return new CeilingFunction(args); + } + else if ("round".equals(name) && arity == 1) + { + return new RoundFunction(args); + } + else if (functionResolver != null) + { + QName qName = QName.valueOf(name); + Object function = functionResolver.resolveFunction(qName, arity); + if (function != null && + function instanceof Function && + function instanceof Expr) + { + Function f = (Function) function; + f.setArguments(args); + return (Expr) function; + } + } + return new FunctionCall(functionResolver, name, args); + } + + // line 211 "-" +// %token constants + + public static final int LITERAL = 257; + public static final int DIGITS = 258; + public static final int NAME = 259; + public static final int LP = 260; + public static final int RP = 261; + public static final int LB = 262; + public static final int RB = 263; + public static final int COMMA = 264; + public static final int PIPE = 265; + public static final int SLASH = 266; + public static final int DOUBLE_SLASH = 267; + public static final int EQ = 268; + public static final int NE = 269; + public static final int GT = 270; + public static final int LT = 271; + public static final int GTE = 272; + public static final int LTE = 273; + public static final int PLUS = 274; + public static final int MINUS = 275; + public static final int AT = 276; + public static final int STAR = 277; + public static final int DOLLAR = 278; + public static final int COLON = 279; + public static final int DOUBLE_COLON = 280; + public static final int DOT = 281; + public static final int DOUBLE_DOT = 282; + public static final int ANCESTOR = 283; + public static final int ANCESTOR_OR_SELF = 284; + public static final int ATTRIBUTE = 285; + public static final int CHILD = 286; + public static final int DESCENDANT = 287; + public static final int DESCENDANT_OR_SELF = 288; + public static final int FOLLOWING = 289; + public static final int FOLLOWING_SIBLING = 290; + public static final int NAMESPACE = 291; + public static final int PARENT = 292; + public static final int PRECEDING = 293; + public static final int PRECEDING_SIBLING = 294; + public static final int SELF = 295; + public static final int DIV = 296; + public static final int MOD = 297; + public static final int OR = 298; + public static final int AND = 299; + public static final int COMMENT = 300; + public static final int PROCESSING_INSTRUCTION = 301; + public static final int TEXT = 302; + public static final int NODE = 303; + public static final int UNARY = 304; + public static final int yyErrorCode = 256; + + /** thrown for irrecoverable syntax errors and stack overflow. + */ + public static class yyException extends java.lang.Exception { + public yyException (String message) { + super(message); + } + } + + /** must be implemented by a scanner object to supply input to the parser. + */ + public interface yyInput { + /** move on to next token. + @return false if positioned beyond tokens. + @throws IOException on input error. + */ + boolean advance () throws java.io.IOException; + /** classifies current token. + Should not be called if advance() returned false. + @return current %token or single character. + */ + int token (); + /** associated with current token. + Should not be called if advance() returned false. + @return value for token(). + */ + Object value (); + } + + /** simplified error message. + @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a> + */ + public void yyerror (String message) { + yyerror(message, null); + } + + /** (syntax) error message. + Can be overwritten to control message format. + @param message text to be displayed. + @param expected vector of acceptable tokens, if available. + */ + public void yyerror (String message, String[] expected) { + if (expected != null && expected.length > 0) { + System.err.print(message+", expecting"); + for (int n = 0; n < expected.length; ++ n) + System.err.print(" "+expected[n]); + System.err.println(); + } else + System.err.println(message); + } + + /** debugging support, requires the package jay.yydebug. + Set to null to suppress debugging messages. + */ +//t protected jay.yydebug.yyDebug yydebug; + + protected static final int yyFinal = 30; + + /** index-checked interface to yyName[]. + @param token single character or %token value. + @return token name or [illegal] or [unknown]. + */ +//t public static final String yyname (int token) { +//t if (token < 0 || token > YyNameClass.yyName.length) return "[illegal]"; +//t String name; +//t if ((name = YyNameClass.yyName[token]) != null) return name; +//t return "[unknown]"; +//t } + + /** computes list of expected tokens on error by tracing the tables. + @param state for which to compute the list. + @return list of token names. + */ + protected String[] yyExpecting (int state) { + int token, n, len = 0; + boolean[] ok = new boolean[YyNameClass.yyName.length]; + + if ((n = YySindexClass.yySindex[state]) != 0) + for (token = n < 0 ? -n : 0; + token < YyNameClass.yyName.length && n+token < YyTableClass.yyTable.length; ++ token) + if (YyCheckClass.yyCheck[n+token] == token && !ok[token] && YyNameClass.yyName[token] != null) { + ++ len; + ok[token] = true; + } + if ((n = YyRindexClass.yyRindex[state]) != 0) + for (token = n < 0 ? -n : 0; + token < YyNameClass.yyName.length && n+token < YyTableClass.yyTable.length; ++ token) + if (YyCheckClass.yyCheck[n+token] == token && !ok[token] && YyNameClass.yyName[token] != null) { + ++ len; + ok[token] = true; + } + + String result[] = new String[len]; + for (n = token = 0; n < len; ++ token) + if (ok[token]) result[n++] = YyNameClass.yyName[token]; + return result; + } + + /** the generated parser, with debugging messages. + Maintains a state and a value stack, currently with fixed maximum size. + @param yyLex scanner. + @param yydebug debug message writer implementing yyDebug, or null. + @return result of the last reduction, if any. + @throws yyException on irrecoverable parse error. + */ + public Object yyparse (yyInput yyLex, Object yydebug) + throws java.io.IOException, yyException { +//t this.yydebug = (jay.yydebug.yyDebug)yydebug; + return yyparse(yyLex); + } + + /** initial size and increment of the state/value stack [default 256]. + This is not final so that it can be overwritten outside of invocations + of yyparse(). + */ + protected int yyMax; + + /** executed at the beginning of a reduce action. + Used as $$ = yyDefault($1), prior to the user-specified action, if any. + Can be overwritten to provide deep copy, etc. + @param first value for $1, or null. + @return first. + */ + protected Object yyDefault (Object first) { + return first; + } + + /** the generated parser. + Maintains a state and a value stack, currently with fixed maximum size. + @param yyLex scanner. + @return result of the last reduction, if any. + @throws yyException on irrecoverable parse error. + */ + public Object yyparse (yyInput yyLex) + throws java.io.IOException, yyException { + if (yyMax <= 0) yyMax = 256; // initial size + int yyState = 0, yyStates[] = new int[yyMax]; // state stack + Object yyVal = null, yyVals[] = new Object[yyMax]; // value stack + int yyToken = -1; // current input + int yyErrorFlag = 0; // #tks to shift + + yyLoop: for (int yyTop = 0;; ++ yyTop) { + if (yyTop >= yyStates.length) { // dynamically increase + int[] i = new int[yyStates.length+yyMax]; + System.arraycopy(yyStates, 0, i, 0, yyStates.length); + yyStates = i; + Object[] o = new Object[yyVals.length+yyMax]; + System.arraycopy(yyVals, 0, o, 0, yyVals.length); + yyVals = o; + } + yyStates[yyTop] = yyState; + yyVals[yyTop] = yyVal; +//t if (yydebug != null) yydebug.push(yyState, yyVal); + + yyDiscarded: for (;;) { // discarding a token does not change stack + int yyN; + if ((yyN = YyDefRedClass.yyDefRed[yyState]) == 0) { // else [default] reduce (yyN) + if (yyToken < 0) { + yyToken = yyLex.advance() ? yyLex.token() : 0; +//t if (yydebug != null) +//t yydebug.lex(yyState, yyToken, yyname(yyToken), yyLex.value()); + } + if ((yyN = YySindexClass.yySindex[yyState]) != 0 && (yyN += yyToken) >= 0 + && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyToken) { +//t if (yydebug != null) +//t yydebug.shift(yyState, YyTableClass.yyTable[yyN], yyErrorFlag-1); + yyState = YyTableClass.yyTable[yyN]; // shift to yyN + yyVal = yyLex.value(); + yyToken = -1; + if (yyErrorFlag > 0) -- yyErrorFlag; + continue yyLoop; + } + if ((yyN = YyRindexClass.yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0 + && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyToken) + yyN = YyTableClass.yyTable[yyN]; // reduce (yyN) + else + switch (yyErrorFlag) { + + case 0: + yyerror("syntax error", yyExpecting(yyState)); +//t if (yydebug != null) yydebug.error("syntax error"); + + case 1: case 2: + yyErrorFlag = 3; + do { + if ((yyN = YySindexClass.yySindex[yyStates[yyTop]]) != 0 + && (yyN += yyErrorCode) >= 0 && yyN < YyTableClass.yyTable.length + && YyCheckClass.yyCheck[yyN] == yyErrorCode) { +//t if (yydebug != null) +//t yydebug.shift(yyStates[yyTop], YyTableClass.yyTable[yyN], 3); + yyState = YyTableClass.yyTable[yyN]; + yyVal = yyLex.value(); + continue yyLoop; + } +//t if (yydebug != null) yydebug.pop(yyStates[yyTop]); + } while (-- yyTop >= 0); +//t if (yydebug != null) yydebug.reject(); + throw new yyException("irrecoverable syntax error"); + + case 3: + if (yyToken == 0) { +//t if (yydebug != null) yydebug.reject(); + throw new yyException("irrecoverable syntax error at end-of-file"); + } +//t if (yydebug != null) +//t yydebug.discard(yyState, yyToken, yyname(yyToken), +//t yyLex.value()); + yyToken = -1; + continue yyDiscarded; // leave stack alone + } + } + int yyV = yyTop + 1-YyLenClass.yyLen[yyN]; +//t if (yydebug != null) +//t yydebug.reduce(yyState, yyStates[yyV-1], yyN, YyRuleClass.yyRule[yyN], YyLenClass.yyLen[yyN]); + yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]); + switch (yyN) { +case 4: + // line 277 "XPathParser.y" + { + yyVal = new Root(); + } + break; +case 5: + // line 281 "XPathParser.y" + { + Steps steps; + if (yyVals[0+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[0+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[0+yyTop]); + } + steps.path.addFirst(new Root()); + yyVal = steps; + /*$$ = new Step(new Root(), (Path) $2);*/ + } + break; +case 6: + // line 297 "XPathParser.y" + { + Test nt = new NodeTypeTest((short) 0); + Selector s = new Selector(Selector.DESCENDANT_OR_SELF, + Collections.singletonList (nt)); + Steps steps; + if (yyVals[0+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[0+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[0+yyTop]); + } + steps.path.addFirst(s); + steps.path.addFirst(new Root()); + yyVal = steps; + /*Step step = new Step(s, (Path) $2);*/ + /*$$ = new Step(new Root(), step);*/ + } + break; +case 8: + // line 322 "XPathParser.y" + { + Steps steps; + if (yyVals[-2+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[-2+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[-2+yyTop]); + } + steps.path.addLast(yyVals[0+yyTop]); + yyVal = steps; + /*$$ = new Step((Expr) $1, (Path) $3);*/ + } + break; +case 9: + // line 338 "XPathParser.y" + { + Test nt = new NodeTypeTest((short) 0); + Selector s = new Selector(Selector.DESCENDANT_OR_SELF, + Collections.singletonList (nt)); + Steps steps; + if (yyVals[-2+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[-2+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[-2+yyTop]); + } + steps.path.addLast(s); + steps.path.addLast(yyVals[0+yyTop]); + yyVal = steps; + /*Step step = new Step(s, (Path) $3);*/ + /*$$ = new Step((Expr) $1, step);*/ + } + break; +case 10: + // line 362 "XPathParser.y" + { + yyVal = new Selector (Selector.CHILD, (List) yyVals[0+yyTop]); + } + break; +case 11: + // line 366 "XPathParser.y" + { + yyVal = new Selector (Selector.ATTRIBUTE, (List) yyVals[0+yyTop]); + } + break; +case 12: + // line 370 "XPathParser.y" + { + yyVal = new Selector (((Integer) yyVals[-2+yyTop]).intValue (), (List) yyVals[0+yyTop]); + } + break; +case 13: + // line 374 "XPathParser.y" + { + yyVal = new Selector (Selector.SELF, Collections.EMPTY_LIST); + } + break; +case 14: + // line 378 "XPathParser.y" + { + yyVal = new Selector (Selector.PARENT, Collections.EMPTY_LIST); + } + break; +case 15: + // line 385 "XPathParser.y" + { + List list = new ArrayList(); + list.add(yyVals[0+yyTop]); + yyVal = list; + } + break; +case 16: + // line 391 "XPathParser.y" + { + List list = (List)yyVals[-1+yyTop]; + list.add(yyVals[0+yyTop]); + yyVal = list; + } + break; +case 17: + // line 415 "XPathParser.y" + { + yyVal = new Integer(Selector.ANCESTOR); + } + break; +case 18: + // line 419 "XPathParser.y" + { + yyVal = new Integer(Selector.ANCESTOR_OR_SELF); + } + break; +case 19: + // line 423 "XPathParser.y" + { + yyVal = new Integer(Selector.ATTRIBUTE); + } + break; +case 20: + // line 427 "XPathParser.y" + { + yyVal = new Integer(Selector.CHILD); + } + break; +case 21: + // line 431 "XPathParser.y" + { + yyVal = new Integer(Selector.DESCENDANT); + } + break; +case 22: + // line 435 "XPathParser.y" + { + yyVal = new Integer(Selector.DESCENDANT_OR_SELF); + } + break; +case 23: + // line 439 "XPathParser.y" + { + yyVal = new Integer(Selector.FOLLOWING); + } + break; +case 24: + // line 443 "XPathParser.y" + { + yyVal = new Integer(Selector.FOLLOWING_SIBLING); + } + break; +case 25: + // line 447 "XPathParser.y" + { + yyVal = new Integer(Selector.NAMESPACE); + } + break; +case 26: + // line 451 "XPathParser.y" + { + yyVal = new Integer(Selector.PARENT); + } + break; +case 27: + // line 455 "XPathParser.y" + { + yyVal = new Integer(Selector.PRECEDING); + } + break; +case 28: + // line 459 "XPathParser.y" + { + yyVal = new Integer(Selector.PRECEDING_SIBLING); + } + break; +case 29: + // line 463 "XPathParser.y" + { + yyVal = new Integer(Selector.SELF); + } + break; +case 31: + // line 472 "XPathParser.y" + { + yyVal = new NodeTypeTest(Node.PROCESSING_INSTRUCTION_NODE, (String) yyVals[-1+yyTop]); + } + break; +case 32: + // line 477 "XPathParser.y" + { + yyVal = new NodeTypeTest(((Short) yyVals[-1+yyTop]).shortValue()); + } + break; +case 33: + // line 484 "XPathParser.y" + { + yyVal = new Predicate((Expr) yyVals[-1+yyTop]); + } + break; +case 35: + // line 492 "XPathParser.y" + { + yyVal = new ParenthesizedExpr((Expr) yyVals[-1+yyTop]); + } + break; +case 36: + // line 496 "XPathParser.y" + { + yyVal = new Constant(yyVals[0+yyTop]); + } + break; +case 37: + // line 500 "XPathParser.y" + { + yyVal = new Constant(yyVals[0+yyTop]); + } + break; +case 39: + // line 508 "XPathParser.y" + { + yyVal = lookupFunction((String) yyVals[-2+yyTop], Collections.EMPTY_LIST); + } + break; +case 40: + // line 512 "XPathParser.y" + { + yyVal = lookupFunction((String) yyVals[-3+yyTop], (List) yyVals[-1+yyTop]); + } + break; +case 41: + // line 519 "XPathParser.y" + { + List list = new ArrayList(); + list.add(yyVals[0+yyTop]); + yyVal = list; + } + break; +case 42: + // line 525 "XPathParser.y" + { + List list = (List) yyVals[0+yyTop]; + list.add(0, yyVals[-2+yyTop]); + yyVal = list; + } + break; +case 44: + // line 535 "XPathParser.y" + { + yyVal = new UnionExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop]); + } + break; +case 47: + // line 544 "XPathParser.y" + { + Steps steps; + if (yyVals[0+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[0+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[0+yyTop]); + } + steps.path.addFirst(yyVals[-2+yyTop]); + yyVal = steps; + /*$$ = new Step ((Expr) $1, (Path) $3);*/ + } + break; +case 48: + // line 560 "XPathParser.y" + { + Test nt = new NodeTypeTest((short) 0); + Selector s = new Selector(Selector.DESCENDANT_OR_SELF, + Collections.singletonList(nt)); + Steps steps; + if (yyVals[0+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[0+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[0+yyTop]); + } + steps.path.addFirst(s); + steps.path.addFirst(yyVals[-2+yyTop]); + yyVal = steps; + /*Step step = new Step (s, (Path) $3);*/ + /*$$ = new Step ((Expr) $1, step);*/ + } + break; +case 50: + // line 585 "XPathParser.y" + { + Predicate filter = (Predicate) yyVals[0+yyTop]; + Selector s = new Selector(Selector.SELF, + Collections.singletonList(filter)); + Steps steps; + if (yyVals[-1+yyTop] instanceof Steps) + { + steps = (Steps) yyVals[-1+yyTop]; + } + else + { + steps = new Steps(); + steps.path.addFirst(yyVals[-1+yyTop]); + } + steps.path.addLast(s); + yyVal = steps; + /*$$ = new Step ((Expr) $1, s);*/ + } + break; +case 52: + // line 608 "XPathParser.y" + { + yyVal = new OrExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop]); + } + break; +case 54: + // line 616 "XPathParser.y" + { + yyVal = new AndExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop]); + } + break; +case 56: + // line 624 "XPathParser.y" + { + yyVal = new EqualityExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], false); + } + break; +case 57: + // line 628 "XPathParser.y" + { + yyVal = new EqualityExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], true); + } + break; +case 59: + // line 636 "XPathParser.y" + { + yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], true, false); + } + break; +case 60: + // line 640 "XPathParser.y" + { + yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], false, false); + } + break; +case 61: + // line 644 "XPathParser.y" + { + yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], true, true); + } + break; +case 62: + // line 648 "XPathParser.y" + { + yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], false, true); + } + break; +case 64: + // line 656 "XPathParser.y" + { + yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.ADD); + } + break; +case 65: + // line 660 "XPathParser.y" + { + yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.SUBTRACT); + } + break; +case 67: + // line 668 "XPathParser.y" + { + yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.MULTIPLY); + } + break; +case 68: + // line 672 "XPathParser.y" + { + yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.DIVIDE); + } + break; +case 69: + // line 676 "XPathParser.y" + { + yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.MODULO); + } + break; +case 71: + // line 684 "XPathParser.y" + { + yyVal = new NegativeExpr((Expr) yyVals[0+yyTop]); + } + break; +case 72: + // line 691 "XPathParser.y" + { + yyVal = new Double((String) yyVals[0+yyTop] + ".0"); + } + break; +case 73: + // line 695 "XPathParser.y" + { + yyVal = new Double((String) yyVals[-1+yyTop] + ".0"); + } + break; +case 74: + // line 699 "XPathParser.y" + { + yyVal = new Double((String) yyVals[-2+yyTop] + "." + (String) yyVals[0+yyTop]); + } + break; +case 75: + // line 703 "XPathParser.y" + { + yyVal = new Double("0." + (String) yyVals[0+yyTop]); + } + break; +case 77: + // line 732 "XPathParser.y" + { + String name = (String) yyVals[0+yyTop]; + yyVal = new VariableReference(variableResolver, getQName(name)); + } + break; +case 78: + // line 740 "XPathParser.y" + { + yyVal = new NameTest(null, true, true); + } + break; +case 79: + // line 744 "XPathParser.y" + { + QName qName = getQName((String) yyVals[-2+yyTop]); + yyVal = new NameTest(qName, true, false); + } + break; +case 80: + // line 749 "XPathParser.y" + { + QName qName = getQName((String) yyVals[0+yyTop]); + yyVal = new NameTest(qName, false, false); + } + break; +case 82: + // line 758 "XPathParser.y" + { + yyVal = (String) yyVals[-2+yyTop] + ':' + (String) yyVals[0+yyTop]; + } + break; +case 83: + // line 765 "XPathParser.y" + { + yyVal = new Short(Node.COMMENT_NODE); + } + break; +case 84: + // line 769 "XPathParser.y" + { + yyVal = new Short(Node.TEXT_NODE); + } + break; +case 85: + // line 773 "XPathParser.y" + { + yyVal = new Short(Node.PROCESSING_INSTRUCTION_NODE); + } + break; +case 86: + // line 777 "XPathParser.y" + { + yyVal = new Short((short) 0); + } + break; + // line 988 "-" + } + yyTop -= YyLenClass.yyLen[yyN]; + yyState = yyStates[yyTop]; + int yyM = YyLhsClass.yyLhs[yyN]; + if (yyState == 0 && yyM == 0) { +//t if (yydebug != null) yydebug.shift(0, yyFinal); + yyState = yyFinal; + if (yyToken < 0) { + yyToken = yyLex.advance() ? yyLex.token() : 0; +//t if (yydebug != null) +//t yydebug.lex(yyState, yyToken,yyname(yyToken), yyLex.value()); + } + if (yyToken == 0) { +//t if (yydebug != null) yydebug.accept(yyVal); + return yyVal; + } + continue yyLoop; + } + if ((yyN = YyGindexClass.yyGindex[yyM]) != 0 && (yyN += yyState) >= 0 + && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyState) + yyState = YyTableClass.yyTable[yyN]; + else + yyState = YyDgotoClass.yyDgoto[yyM]; +//t if (yydebug != null) yydebug.shift(yyStates[yyTop], yyState); + continue yyLoop; + } + } + } + + protected static final class YyLhsClass { + + public static final short yyLhs [] = { -1, + 0, 2, 2, 4, 4, 4, 3, 3, 3, 5, + 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, + 8, 8, 9, 12, 12, 12, 12, 12, 15, 15, + 17, 17, 18, 18, 19, 19, 19, 19, 20, 20, + 1, 1, 21, 21, 22, 22, 22, 23, 23, 23, + 23, 23, 24, 24, 24, 25, 25, 25, 25, 26, + 26, 14, 14, 14, 14, 16, 13, 10, 10, 10, + 27, 27, 11, 11, 11, 11, + }; + } /* End of class YyLhsClass */ + + protected static final class YyLenClass { + + public static final short yyLen [] = { 2, + 1, 1, 1, 1, 2, 2, 1, 3, 3, 1, + 2, 3, 1, 1, 1, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 3, 2, 3, 1, 3, 1, 1, 1, 3, 4, + 1, 3, 1, 3, 1, 1, 3, 3, 1, 2, + 1, 3, 1, 3, 1, 3, 3, 1, 3, 3, + 3, 3, 1, 3, 3, 1, 3, 3, 3, 1, + 2, 1, 2, 3, 2, 1, 2, 1, 3, 1, + 1, 3, 1, 1, 1, 1, + }; + } /* End class YyLenClass */ + + protected static final class YyDefRedClass { + + public static final short yyDefRed [] = { 0, + 36, 0, 0, 0, 0, 0, 0, 0, 78, 0, + 0, 14, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 83, 0, 84, 86, 0, + 0, 45, 0, 3, 7, 0, 0, 15, 30, 0, + 49, 34, 37, 38, 0, 0, 43, 0, 0, 0, + 0, 0, 0, 66, 0, 0, 0, 0, 13, 0, + 80, 0, 71, 0, 0, 77, 75, 0, 0, 0, + 0, 0, 16, 0, 32, 0, 0, 0, 0, 50, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 74, 82, 79, 35, 0, 31, 0, 8, + 9, 0, 0, 39, 0, 0, 44, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 67, 68, + 69, 33, 0, 40, 42, + }; + } /* End of class YyDefRedClass */ + + protected static final class YyDgotoClass { + + public static final short yyDgoto [] = { 105, + 31, 32, 33, 34, 35, 36, 37, 38, 73, 39, + 40, 41, 42, 43, 44, 45, 106, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, + }; + } /* End of class YyDgotoClass */ + + protected static final class YySindexClass { + + public static final short yySindex [] = { -97, + 0, -271, -267, -97, -239, -239, -97, -199, 0, -236, + -222, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, -218, 0, 0, 0, + -257, 0, -241, 0, 0, -205, -221, 0, 0, -194, + 0, 0, 0, 0, -190, -185, 0, -238, -211, -234, + -255, -209, -275, 0, 0, -169, -250, -168, 0, -241, + 0, -241, 0, -205, -187, 0, 0, -167, -97, -239, + -239, -97, 0, -199, 0, -151, -43, -239, -239, 0, + -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, + -97, -97, 0, 0, 0, 0, -164, 0, -211, 0, + 0, -166, -205, 0, -165, -163, 0, -241, -241, -234, + -255, -255, -209, -209, -209, -209, -275, -275, 0, 0, + 0, 0, -97, 0, 0, + }; + } /* End of class YySindexClass */ + + protected static final class YyRindexClass { + + public static final short yyRindex [] = { 0, + 0, 58, 1, 0, 420, 0, 0, 0, 0, 0, + 129, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, -161, 0, 0, 0, + 40, 0, 237, 0, 0, 168, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 459, 0, 277, 557, 544, + 656, 561, 474, 0, 19, 75, 0, 0, 0, 295, + 0, 334, 0, 183, 114, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 686, 0, + 0, 0, 222, 0, -156, 0, 0, 351, 405, 553, + 665, 697, 577, 600, 617, 639, 513, 528, 0, 0, + 0, 0, 0, 0, 0, + }; + } /* End of class YyRindexClass */ + + protected static final class YyGindexClass { + + public static final short yyGindex [] = { 7, + 0, 0, 8, 0, 3, -3, 0, 0, 48, 0, + 0, 0, 0, 0, 0, 0, -12, 0, 35, 0, + 44, 36, -1, -54, 2, -7, -2, + }; + } /* End of class YyGindexClass */ + + protected static final class YyTableClass { + + public static final short yyTable [] = { 63, + 81, 90, 61, 61, 64, 61, 30, 66, 94, 56, + 58, 57, 60, 62, 84, 85, 86, 87, 80, 3, + 91, 92, 65, 72, 70, 71, 95, 78, 79, 113, + 114, 115, 116, 82, 83, 67, 8, 9, 68, 1, + 69, 59, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 72, 72, 74, 3, + 26, 27, 28, 29, 88, 89, 75, 61, 61, 76, + 103, 61, 100, 101, 73, 61, 61, 9, 102, 77, + 111, 112, 119, 120, 121, 108, 109, 81, 93, 117, + 118, 97, 96, 98, 94, 80, 122, 124, 123, 85, + 26, 27, 28, 29, 41, 1, 2, 3, 4, 104, + 125, 107, 99, 81, 5, 6, 110, 0, 0, 0, + 0, 0, 0, 7, 8, 9, 10, 0, 13, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 23, 24, 25, 0, 0, 0, 0, 26, 27, + 28, 29, 0, 0, 0, 0, 0, 0, 0, 1, + 2, 3, 4, 0, 0, 0, 0, 10, 5, 6, + 0, 0, 0, 0, 0, 0, 0, 7, 8, 9, + 10, 0, 11, 11, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, + 0, 0, 26, 27, 28, 29, 0, 0, 0, 0, + 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, + 0, 12, 5, 6, 0, 0, 0, 0, 0, 0, + 0, 0, 8, 9, 10, 0, 2, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 0, 0, 0, 0, 26, 27, 28, 29, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 46, 81, 76, 80, + 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, + 80, 80, 80, 80, 5, 80, 81, 81, 81, 81, + 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 80, 80, 80, 80, 72, 72, + 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, + 72, 72, 72, 6, 72, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, + 47, 73, 0, 72, 72, 72, 72, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 73, 73, 73, 73, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 13, + 81, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 48, 13, 0, 0, 0, 81, + 81, 81, 81, 0, 0, 0, 0, 0, 0, 4, + 0, 0, 0, 0, 13, 13, 13, 13, 10, 0, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 11, 10, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 70, 11, + 0, 0, 0, 10, 10, 10, 10, 0, 0, 0, + 0, 0, 0, 63, 0, 0, 0, 0, 11, 11, + 11, 11, 12, 0, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 2, 12, 2, + 2, 2, 0, 0, 2, 2, 2, 2, 2, 2, + 2, 2, 64, 2, 0, 0, 0, 12, 12, 12, + 12, 0, 0, 0, 0, 0, 0, 65, 0, 0, + 0, 0, 2, 2, 2, 2, 0, 46, 0, 46, + 46, 46, 0, 53, 46, 46, 46, 46, 46, 46, + 46, 46, 54, 46, 0, 5, 51, 5, 5, 5, + 58, 0, 5, 5, 5, 5, 5, 5, 5, 5, + 0, 5, 46, 46, 46, 46, 60, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 5, 5, 5, 5, 6, 0, 6, 6, 6, 59, + 0, 6, 6, 6, 6, 6, 6, 6, 6, 0, + 6, 47, 0, 47, 47, 47, 62, 0, 47, 47, + 47, 47, 47, 47, 47, 47, 0, 47, 0, 6, + 6, 6, 6, 0, 0, 0, 0, 0, 61, 0, + 0, 0, 0, 0, 0, 0, 47, 47, 47, 47, + 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, + 0, 0, 0, 0, 56, 48, 0, 48, 48, 48, + 0, 0, 48, 48, 48, 48, 48, 48, 48, 48, + 4, 48, 4, 4, 4, 52, 0, 4, 4, 4, + 4, 4, 4, 4, 4, 0, 57, 0, 0, 0, + 48, 48, 48, 48, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 4, 4, 4, 4, 70, + 0, 70, 70, 0, 0, 0, 70, 70, 70, 70, + 70, 70, 70, 70, 63, 70, 63, 63, 0, 0, + 0, 63, 63, 63, 63, 63, 63, 63, 63, 0, + 0, 0, 0, 0, 70, 70, 70, 70, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 63, 63, 64, 0, 64, 64, 0, 0, 0, + 64, 64, 64, 64, 64, 64, 64, 64, 65, 0, + 65, 65, 0, 0, 0, 65, 65, 65, 65, 65, + 65, 65, 65, 0, 53, 0, 53, 53, 0, 0, + 64, 64, 0, 54, 0, 54, 54, 51, 0, 51, + 51, 58, 0, 58, 58, 65, 65, 0, 58, 58, + 58, 58, 58, 58, 0, 0, 0, 60, 0, 60, + 60, 53, 53, 0, 60, 60, 60, 60, 60, 60, + 54, 54, 0, 0, 51, 0, 0, 0, 58, 58, + 59, 0, 59, 59, 0, 0, 0, 59, 59, 59, + 59, 59, 59, 0, 60, 60, 0, 62, 0, 62, + 62, 0, 0, 0, 62, 62, 62, 62, 62, 62, + 0, 0, 0, 0, 0, 0, 0, 59, 59, 61, + 0, 61, 61, 0, 0, 0, 61, 61, 61, 61, + 61, 61, 0, 0, 62, 62, 55, 0, 55, 55, + 0, 0, 0, 55, 55, 56, 0, 56, 56, 0, + 0, 0, 56, 56, 0, 0, 61, 61, 0, 0, + 0, 0, 0, 0, 0, 0, 52, 0, 52, 52, + 0, 0, 0, 55, 55, 0, 0, 57, 0, 57, + 57, 0, 56, 56, 57, 57, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 57, 57, + }; + } /* End of class YyTableClass */ + + protected static final class YyCheckClass { + + public static final short yyCheck [] = { 7, + 0, 277, 5, 6, 8, 8, 0, 10, 259, 281, + 4, 279, 5, 6, 270, 271, 272, 273, 0, 259, + 296, 297, 259, 262, 266, 267, 277, 266, 267, 84, + 85, 86, 87, 268, 269, 258, 276, 277, 257, 0, + 298, 281, 282, 283, 284, 285, 286, 287, 288, 289, + 290, 291, 292, 293, 294, 295, 262, 0, 280, 259, + 300, 301, 302, 303, 274, 275, 261, 70, 71, 260, + 74, 74, 70, 71, 0, 78, 79, 277, 72, 265, + 82, 83, 90, 91, 92, 78, 79, 299, 258, 88, + 89, 279, 261, 261, 259, 48, 263, 261, 264, 261, + 300, 301, 302, 303, 261, 257, 258, 259, 260, 261, + 123, 77, 69, 0, 266, 267, 81, -1, -1, -1, + -1, -1, -1, 275, 276, 277, 278, -1, 0, 281, + 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, + 292, 293, 294, 295, -1, -1, -1, -1, 300, 301, + 302, 303, -1, -1, -1, -1, -1, -1, -1, 257, + 258, 259, 260, -1, -1, -1, -1, 0, 266, 267, + -1, -1, -1, -1, -1, -1, -1, 275, 276, 277, + 278, -1, 0, 281, 282, 283, 284, 285, 286, 287, + 288, 289, 290, 291, 292, 293, 294, 295, -1, -1, + -1, -1, 300, 301, 302, 303, -1, -1, -1, -1, + -1, -1, -1, 257, 258, 259, 260, -1, -1, -1, + -1, 0, 266, 267, -1, -1, -1, -1, -1, -1, + -1, -1, 276, 277, 278, -1, 0, 281, 282, 283, + 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, + 294, 295, -1, -1, -1, -1, 300, 301, 302, 303, + 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, + 270, 271, 272, 273, 274, 275, 0, 277, 260, 261, + 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, + 272, 273, 274, 275, 0, 277, 296, 297, 298, 299, + 261, -1, 263, 264, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, 296, 297, 298, 299, 261, 262, + 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, + 273, 274, 275, 0, 277, 261, 262, 263, 264, 265, + 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, + 0, 277, -1, 296, 297, 298, 299, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 296, 297, 298, 299, 261, 262, 263, 264, 265, 266, + 267, 268, 269, 270, 271, 272, 273, 274, 275, 261, + 277, 263, 264, 265, 266, 267, 268, 269, 270, 271, + 272, 273, 274, 275, 0, 277, -1, -1, -1, 296, + 297, 298, 299, -1, -1, -1, -1, -1, -1, 0, + -1, -1, -1, -1, 296, 297, 298, 299, 261, -1, + 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, + 273, 274, 275, 261, 277, 263, 264, 265, 266, 267, + 268, 269, 270, 271, 272, 273, 274, 275, 0, 277, + -1, -1, -1, 296, 297, 298, 299, -1, -1, -1, + -1, -1, -1, 0, -1, -1, -1, -1, 296, 297, + 298, 299, 261, -1, 263, 264, 265, 266, 267, 268, + 269, 270, 271, 272, 273, 274, 275, 261, 277, 263, + 264, 265, -1, -1, 268, 269, 270, 271, 272, 273, + 274, 275, 0, 277, -1, -1, -1, 296, 297, 298, + 299, -1, -1, -1, -1, -1, -1, 0, -1, -1, + -1, -1, 296, 297, 298, 299, -1, 261, -1, 263, + 264, 265, -1, 0, 268, 269, 270, 271, 272, 273, + 274, 275, 0, 277, -1, 261, 0, 263, 264, 265, + 0, -1, 268, 269, 270, 271, 272, 273, 274, 275, + -1, 277, 296, 297, 298, 299, 0, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 296, 297, 298, 299, 261, -1, 263, 264, 265, 0, + -1, 268, 269, 270, 271, 272, 273, 274, 275, -1, + 277, 261, -1, 263, 264, 265, 0, -1, 268, 269, + 270, 271, 272, 273, 274, 275, -1, 277, -1, 296, + 297, 298, 299, -1, -1, -1, -1, -1, 0, -1, + -1, -1, -1, -1, -1, -1, 296, 297, 298, 299, + -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, + -1, -1, -1, -1, 0, 261, -1, 263, 264, 265, + -1, -1, 268, 269, 270, 271, 272, 273, 274, 275, + 261, 277, 263, 264, 265, 0, -1, 268, 269, 270, + 271, 272, 273, 274, 275, -1, 0, -1, -1, -1, + 296, 297, 298, 299, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, 296, 297, 298, 299, 261, + -1, 263, 264, -1, -1, -1, 268, 269, 270, 271, + 272, 273, 274, 275, 261, 277, 263, 264, -1, -1, + -1, 268, 269, 270, 271, 272, 273, 274, 275, -1, + -1, -1, -1, -1, 296, 297, 298, 299, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, 298, 299, 261, -1, 263, 264, -1, -1, -1, + 268, 269, 270, 271, 272, 273, 274, 275, 261, -1, + 263, 264, -1, -1, -1, 268, 269, 270, 271, 272, + 273, 274, 275, -1, 261, -1, 263, 264, -1, -1, + 298, 299, -1, 261, -1, 263, 264, 261, -1, 263, + 264, 261, -1, 263, 264, 298, 299, -1, 268, 269, + 270, 271, 272, 273, -1, -1, -1, 261, -1, 263, + 264, 298, 299, -1, 268, 269, 270, 271, 272, 273, + 298, 299, -1, -1, 298, -1, -1, -1, 298, 299, + 261, -1, 263, 264, -1, -1, -1, 268, 269, 270, + 271, 272, 273, -1, 298, 299, -1, 261, -1, 263, + 264, -1, -1, -1, 268, 269, 270, 271, 272, 273, + -1, -1, -1, -1, -1, -1, -1, 298, 299, 261, + -1, 263, 264, -1, -1, -1, 268, 269, 270, 271, + 272, 273, -1, -1, 298, 299, 261, -1, 263, 264, + -1, -1, -1, 268, 269, 261, -1, 263, 264, -1, + -1, -1, 268, 269, -1, -1, 298, 299, -1, -1, + -1, -1, -1, -1, -1, -1, 261, -1, 263, 264, + -1, -1, -1, 298, 299, -1, -1, 261, -1, 263, + 264, -1, 298, 299, 268, 269, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 298, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, 298, 299, + }; + } /* End of class YyCheckClass */ + + +//t protected static final class YyRuleClass { + +//t public static final String yyRule [] = { +//t "$accept : expr", +//t "expr : or_expr", +//t "location_path : relative_location_path", +//t "location_path : absolute_location_path", +//t "absolute_location_path : SLASH", +//t "absolute_location_path : SLASH relative_location_path", +//t "absolute_location_path : DOUBLE_SLASH relative_location_path", +//t "relative_location_path : step", +//t "relative_location_path : relative_location_path SLASH step", +//t "relative_location_path : relative_location_path DOUBLE_SLASH step", +//t "step : step_node_test", +//t "step : AT step_node_test", +//t "step : axis_name DOUBLE_COLON step_node_test", +//t "step : DOT", +//t "step : DOUBLE_DOT", +//t "step_node_test : node_test", +//t "step_node_test : step_node_test predicate", +//t "axis_name : ANCESTOR", +//t "axis_name : ANCESTOR_OR_SELF", +//t "axis_name : ATTRIBUTE", +//t "axis_name : CHILD", +//t "axis_name : DESCENDANT", +//t "axis_name : DESCENDANT_OR_SELF", +//t "axis_name : FOLLOWING", +//t "axis_name : FOLLOWING_SIBLING", +//t "axis_name : NAMESPACE", +//t "axis_name : PARENT", +//t "axis_name : PRECEDING", +//t "axis_name : PRECEDING_SIBLING", +//t "axis_name : SELF", +//t "node_test : name_test", +//t "node_test : PROCESSING_INSTRUCTION LITERAL RP", +//t "node_test : node_type RP", +//t "predicate : LB expr RB", +//t "primary_expr : variable_reference", +//t "primary_expr : LP expr RP", +//t "primary_expr : LITERAL", +//t "primary_expr : number", +//t "primary_expr : function_call", +//t "function_call : function_name LP RP", +//t "function_call : function_name LP argument_list RP", +//t "argument_list : expr", +//t "argument_list : expr COMMA argument_list", +//t "union_expr : path_expr", +//t "union_expr : union_expr PIPE path_expr", +//t "path_expr : location_path", +//t "path_expr : filter_expr", +//t "path_expr : filter_expr SLASH relative_location_path", +//t "path_expr : filter_expr DOUBLE_SLASH relative_location_path", +//t "filter_expr : primary_expr", +//t "filter_expr : filter_expr predicate", +//t "or_expr : and_expr", +//t "or_expr : or_expr OR and_expr", +//t "and_expr : equality_expr", +//t "and_expr : and_expr AND equality_expr", +//t "equality_expr : relational_expr", +//t "equality_expr : equality_expr EQ relational_expr", +//t "equality_expr : equality_expr NE relational_expr", +//t "relational_expr : additive_expr", +//t "relational_expr : relational_expr LT additive_expr", +//t "relational_expr : relational_expr GT additive_expr", +//t "relational_expr : relational_expr LTE additive_expr", +//t "relational_expr : relational_expr GTE additive_expr", +//t "additive_expr : multiplicative_expr", +//t "additive_expr : additive_expr PLUS multiplicative_expr", +//t "additive_expr : additive_expr MINUS multiplicative_expr", +//t "multiplicative_expr : unary_expr", +//t "multiplicative_expr : multiplicative_expr STAR unary_expr", +//t "multiplicative_expr : multiplicative_expr DIV unary_expr", +//t "multiplicative_expr : multiplicative_expr MOD unary_expr", +//t "unary_expr : union_expr", +//t "unary_expr : MINUS unary_expr", +//t "number : DIGITS", +//t "number : DIGITS DOT", +//t "number : DIGITS DOT DIGITS", +//t "number : DOT DIGITS", +//t "function_name : qname", +//t "variable_reference : DOLLAR qname", +//t "name_test : STAR", +//t "name_test : NAME COLON STAR", +//t "name_test : qname", +//t "qname : NAME", +//t "qname : NAME COLON NAME", +//t "node_type : COMMENT", +//t "node_type : TEXT", +//t "node_type : PROCESSING_INSTRUCTION", +//t "node_type : NODE", +//t }; +//t } /* End of class YyRuleClass */ + + protected static final class YyNameClass { + + public static final String yyName [] = { + "end-of-file",null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,null,null,null,null,null,null,null, + null,null,null,null,null,null,null,"LITERAL","DIGITS","NAME","LP", + "RP","LB","RB","COMMA","PIPE","SLASH","DOUBLE_SLASH","EQ","NE","GT", + "LT","GTE","LTE","PLUS","MINUS","AT","STAR","DOLLAR","COLON", + "DOUBLE_COLON","DOT","DOUBLE_DOT","ANCESTOR","ANCESTOR_OR_SELF", + "ATTRIBUTE","CHILD","DESCENDANT","DESCENDANT_OR_SELF","FOLLOWING", + "FOLLOWING_SIBLING","NAMESPACE","PARENT","PRECEDING", + "PRECEDING_SIBLING","SELF","DIV","MOD","OR","AND","COMMENT", + "PROCESSING_INSTRUCTION","TEXT","NODE","UNARY", + }; + } /* End of class YyNameClass */ + + + // line 783 "XPathParser.y" + +} + // line 1463 "-" diff --git a/libjava/classpath/gnu/xml/xpath/XPathParser.y b/libjava/classpath/gnu/xml/xpath/XPathParser.y new file mode 100644 index 00000000000..d7dc4f1e8aa --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/XPathParser.y @@ -0,0 +1,784 @@ +%{ +/* + * XPathParser.java + * Copyright (C) 2004 The Free Software Foundation + * + * This file is part of GNU JAXP, a library. + * + * GNU JAXP is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GNU JAXP is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under + * terms of your choice, provided that you also meet, for each linked + * independent module, the terms and conditions of the license of that + * module. An independent module is a module which is not derived from + * or based on this library. If you modify this library, you may extend + * this exception to your version of the library, but you are not + * obliged to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +package gnu.xml.xpath; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunctionResolver; +import javax.xml.xpath.XPathVariableResolver; +import org.w3c.dom.Node; + +/** + * An XPath 1.0 parser. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XPathParser +{ + + NamespaceContext namespaceContext; + XPathVariableResolver variableResolver; + XPathFunctionResolver functionResolver; + + QName getQName(String name) + { + QName qName = QName.valueOf(name); + if (namespaceContext != null) + { + String prefix = qName.getPrefix(); + String uri = qName.getNamespaceURI(); + if (prefix != null && (uri == null || uri.length() == 0)) + { + uri = namespaceContext.getNamespaceURI(prefix); + String localName = qName.getLocalPart(); + qName = new QName(uri, localName, prefix); + } + } + return qName; + } + + Expr lookupFunction(String name, List args) + { + int arity = args.size(); + if ("position".equals(name) && arity == 0) + { + return new PositionFunction(); + } + else if ("last".equals(name) && arity == 0) + { + return new LastFunction(); + } + else if ("string".equals(name) && (arity == 1 || arity == 0)) + { + return new StringFunction(args); + } + else if ("number".equals(name) && (arity == 1 || arity == 0)) + { + return new NumberFunction(args); + } + else if ("boolean".equals(name) && arity == 1) + { + return new BooleanFunction(args); + } + else if ("count".equals(name) && arity == 1) + { + return new CountFunction(args); + } + else if ("not".equals(name) && arity == 1) + { + return new NotFunction(args); + } + else if ("id".equals(name) && arity == 1) + { + return new IdFunction(args); + } + else if ("concat".equals(name) && arity > 1) + { + return new ConcatFunction(args); + } + else if ("true".equals(name) && arity == 0) + { + return new TrueFunction(); + } + else if ("false".equals(name) && arity == 0) + { + return new FalseFunction(); + } + else if ("name".equals(name) && (arity == 1 || arity == 0)) + { + return new NameFunction(args); + } + else if ("local-name".equals(name) && (arity == 1 || arity == 0)) + { + return new LocalNameFunction(args); + } + else if ("namespace-uri".equals(name) && (arity == 1 || arity == 0)) + { + return new NamespaceUriFunction(args); + } + else if ("starts-with".equals(name) && arity == 2) + { + return new StartsWithFunction(args); + } + else if ("contains".equals(name) && arity == 2) + { + return new ContainsFunction(args); + } + else if ("string-length".equals(name) && (arity == 1 || arity == 0)) + { + return new StringLengthFunction(args); + } + else if ("translate".equals(name) && arity == 3) + { + return new TranslateFunction(args); + } + else if ("normalize-space".equals(name) && (arity == 1 || arity == 0)) + { + return new NormalizeSpaceFunction(args); + } + else if ("substring".equals(name) && (arity == 2 || arity == 3)) + { + return new SubstringFunction(args); + } + else if ("substring-before".equals(name) && arity == 2) + { + return new SubstringBeforeFunction(args); + } + else if ("substring-after".equals(name) && arity == 2) + { + return new SubstringAfterFunction(args); + } + else if ("lang".equals(name) && arity == 1) + { + return new LangFunction(args); + } + else if ("sum".equals(name) && arity == 1) + { + return new SumFunction(args); + } + else if ("floor".equals(name) && arity == 1) + { + return new FloorFunction(args); + } + else if ("ceiling".equals(name) && arity == 1) + { + return new CeilingFunction(args); + } + else if ("round".equals(name) && arity == 1) + { + return new RoundFunction(args); + } + else if (functionResolver != null) + { + QName qName = QName.valueOf(name); + Object function = functionResolver.resolveFunction(qName, arity); + if (function != null && + function instanceof Function && + function instanceof Expr) + { + Function f = (Function) function; + f.setArguments(args); + return (Expr) function; + } + } + return new FunctionCall(functionResolver, name, args); + } + +%} + +%token LITERAL +%token DIGITS +%token NAME + +%token LP // '(' +%token RP // ')' +%token LB // '[' +%token RB // ']' +%token COMMA // ',' +%token PIPE // '|' +%token SLASH // '/' +%token DOUBLE_SLASH // '//' +%token EQ // '=' +%token NE // '!=' +%token GT // '>' +%token LT // '<' +%token GTE // '>=' +%token LTE // '<=' +%token PLUS // '+' +%token MINUS // '-' +%token AT // '@' +%token STAR // '*' +%token DOLLAR // '$' +%token COLON // ':' +%token DOUBLE_COLON // '::' +%token DOT // '.' +%token DOUBLE_DOT // '..' + +%token ANCESTOR +%token ANCESTOR_OR_SELF +%token ATTRIBUTE +%token CHILD +%token DESCENDANT +%token DESCENDANT_OR_SELF +%token FOLLOWING +%token FOLLOWING_SIBLING +%token NAMESPACE +%token PARENT +%token PRECEDING +%token PRECEDING_SIBLING +%token SELF +%token DIV +%token MOD +%token OR +%token AND +%token COMMENT +%token PROCESSING_INSTRUCTION +%token TEXT +%token NODE + +%right UNARY + +%start expr + +%% + +expr: + or_expr + ; + +location_path: + relative_location_path + | absolute_location_path + ; + +absolute_location_path: + SLASH + { + $$ = new Root(); + } + | SLASH relative_location_path + { + Steps steps; + if ($2 instanceof Steps) + { + steps = (Steps) $2; + } + else + { + steps = new Steps(); + steps.path.addFirst($2); + } + steps.path.addFirst(new Root()); + $$ = steps; + //$$ = new Step(new Root(), (Path) $2); + } + | DOUBLE_SLASH relative_location_path + { + Test nt = new NodeTypeTest((short) 0); + Selector s = new Selector(Selector.DESCENDANT_OR_SELF, + Collections.singletonList (nt)); + Steps steps; + if ($2 instanceof Steps) + { + steps = (Steps) $2; + } + else + { + steps = new Steps(); + steps.path.addFirst($2); + } + steps.path.addFirst(s); + steps.path.addFirst(new Root()); + $$ = steps; + //Step step = new Step(s, (Path) $2); + //$$ = new Step(new Root(), step); + } + ; + +relative_location_path: + step + | relative_location_path SLASH step + { + Steps steps; + if ($1 instanceof Steps) + { + steps = (Steps) $1; + } + else + { + steps = new Steps(); + steps.path.addFirst($1); + } + steps.path.addLast($3); + $$ = steps; + //$$ = new Step((Expr) $1, (Path) $3); + } + | relative_location_path DOUBLE_SLASH step + { + Test nt = new NodeTypeTest((short) 0); + Selector s = new Selector(Selector.DESCENDANT_OR_SELF, + Collections.singletonList (nt)); + Steps steps; + if ($1 instanceof Steps) + { + steps = (Steps) $1; + } + else + { + steps = new Steps(); + steps.path.addFirst($1); + } + steps.path.addLast(s); + steps.path.addLast($3); + $$ = steps; + //Step step = new Step(s, (Path) $3); + //$$ = new Step((Expr) $1, step); + } + ; + +step: + step_node_test + { + $$ = new Selector (Selector.CHILD, (List) $1); + } + | AT step_node_test + { + $$ = new Selector (Selector.ATTRIBUTE, (List) $2); + } + | axis_name DOUBLE_COLON step_node_test + { + $$ = new Selector (((Integer) $1).intValue (), (List) $3); + } + | DOT + { + $$ = new Selector (Selector.SELF, Collections.EMPTY_LIST); + } + | DOUBLE_DOT + { + $$ = new Selector (Selector.PARENT, Collections.EMPTY_LIST); + } + ; + +step_node_test: + node_test + { + List list = new ArrayList(); + list.add($1); + $$ = list; + } + | step_node_test predicate + { + List list = (List)$1; + list.add($2); + $$ = list; + } + ; + +/*predicate_list: + predicate + { + List list = new ArrayList (); + list.add ($1); + $$ = list; + } + | predicate predicate_list + { + List list = (List) $3; + list.add (0, $1); + $$ = list; + } + ;*/ + +axis_name: + ANCESTOR + { + $$ = new Integer(Selector.ANCESTOR); + } + | ANCESTOR_OR_SELF + { + $$ = new Integer(Selector.ANCESTOR_OR_SELF); + } + | ATTRIBUTE + { + $$ = new Integer(Selector.ATTRIBUTE); + } + | CHILD + { + $$ = new Integer(Selector.CHILD); + } + | DESCENDANT + { + $$ = new Integer(Selector.DESCENDANT); + } + | DESCENDANT_OR_SELF + { + $$ = new Integer(Selector.DESCENDANT_OR_SELF); + } + | FOLLOWING + { + $$ = new Integer(Selector.FOLLOWING); + } + | FOLLOWING_SIBLING + { + $$ = new Integer(Selector.FOLLOWING_SIBLING); + } + | NAMESPACE + { + $$ = new Integer(Selector.NAMESPACE); + } + | PARENT + { + $$ = new Integer(Selector.PARENT); + } + | PRECEDING + { + $$ = new Integer(Selector.PRECEDING); + } + | PRECEDING_SIBLING + { + $$ = new Integer(Selector.PRECEDING_SIBLING); + } + | SELF + { + $$ = new Integer(Selector.SELF); + } + ; + +node_test: + name_test + /*| PROCESSING_INSTRUCTION LP LITERAL RP*/ + | PROCESSING_INSTRUCTION LITERAL RP + { + $$ = new NodeTypeTest(Node.PROCESSING_INSTRUCTION_NODE, (String) $2); + } + /*| node_type LP RP*/ + | node_type RP + { + $$ = new NodeTypeTest(((Short) $1).shortValue()); + } + ; + +predicate: + LB expr RB + { + $$ = new Predicate((Expr) $2); + } + ; + +primary_expr: + variable_reference + | LP expr RP + { + $$ = new ParenthesizedExpr((Expr) $2); + } + | LITERAL + { + $$ = new Constant($1); + } + | number + { + $$ = new Constant($1); + } + | function_call + ; + +function_call: + function_name LP RP + { + $$ = lookupFunction((String) $1, Collections.EMPTY_LIST); + } + | function_name LP argument_list RP + { + $$ = lookupFunction((String) $1, (List) $3); + } + ; + +argument_list: + expr + { + List list = new ArrayList(); + list.add($1); + $$ = list; + } + | expr COMMA argument_list + { + List list = (List) $3; + list.add(0, $1); + $$ = list; + } + ; + +union_expr: + path_expr + | union_expr PIPE path_expr + { + $$ = new UnionExpr((Expr) $1, (Expr) $3); + } + ; + +path_expr: + location_path + | filter_expr + | filter_expr SLASH relative_location_path + { + Steps steps; + if ($3 instanceof Steps) + { + steps = (Steps) $3; + } + else + { + steps = new Steps(); + steps.path.addFirst($3); + } + steps.path.addFirst($1); + $$ = steps; + //$$ = new Step ((Expr) $1, (Path) $3); + } + | filter_expr DOUBLE_SLASH relative_location_path + { + Test nt = new NodeTypeTest((short) 0); + Selector s = new Selector(Selector.DESCENDANT_OR_SELF, + Collections.singletonList(nt)); + Steps steps; + if ($3 instanceof Steps) + { + steps = (Steps) $3; + } + else + { + steps = new Steps(); + steps.path.addFirst($3); + } + steps.path.addFirst(s); + steps.path.addFirst($1); + $$ = steps; + //Step step = new Step (s, (Path) $3); + //$$ = new Step ((Expr) $1, step); + } + ; + +filter_expr: + primary_expr + | filter_expr predicate + { + Predicate filter = (Predicate) $2; + Selector s = new Selector(Selector.SELF, + Collections.singletonList(filter)); + Steps steps; + if ($1 instanceof Steps) + { + steps = (Steps) $1; + } + else + { + steps = new Steps(); + steps.path.addFirst($1); + } + steps.path.addLast(s); + $$ = steps; + //$$ = new Step ((Expr) $1, s); + } + ; + +or_expr: + and_expr + | or_expr OR and_expr + { + $$ = new OrExpr((Expr) $1, (Expr) $3); + } + ; + +and_expr: + equality_expr + | and_expr AND equality_expr + { + $$ = new AndExpr((Expr) $1, (Expr) $3); + } + ; + +equality_expr: + relational_expr + | equality_expr EQ relational_expr + { + $$ = new EqualityExpr((Expr) $1, (Expr) $3, false); + } + | equality_expr NE relational_expr + { + $$ = new EqualityExpr((Expr) $1, (Expr) $3, true); + } + ; + +relational_expr: + additive_expr + | relational_expr LT additive_expr + { + $$ = new RelationalExpr((Expr) $1, (Expr) $3, true, false); + } + | relational_expr GT additive_expr + { + $$ = new RelationalExpr((Expr) $1, (Expr) $3, false, false); + } + | relational_expr LTE additive_expr + { + $$ = new RelationalExpr((Expr) $1, (Expr) $3, true, true); + } + | relational_expr GTE additive_expr + { + $$ = new RelationalExpr((Expr) $1, (Expr) $3, false, true); + } + ; + +additive_expr: + multiplicative_expr + | additive_expr PLUS multiplicative_expr + { + $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.ADD); + } + | additive_expr MINUS multiplicative_expr + { + $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.SUBTRACT); + } + ; + +multiplicative_expr: + unary_expr + | multiplicative_expr STAR unary_expr + { + $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.MULTIPLY); + } + | multiplicative_expr DIV unary_expr + { + $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.DIVIDE); + } + | multiplicative_expr MOD unary_expr + { + $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.MODULO); + } + ; + +unary_expr: + union_expr + | MINUS unary_expr %prec UNARY + { + $$ = new NegativeExpr((Expr) $2); + } + ; + +number: + DIGITS + { + $$ = new Double((String) $1 + ".0"); + } + | DIGITS DOT + { + $$ = new Double((String) $1 + ".0"); + } + | DIGITS DOT DIGITS + { + $$ = new Double((String) $1 + "." + (String) $3); + } + | DOT DIGITS + { + $$ = new Double("0." + (String) $2); + } + ; + +function_name: + qname +/* | node_type + { + switch (((Short) $1).shortValue ()) + { + case Node.COMMENT_NODE: + $$ = "comment"; + break; + case Node.TEXT_NODE: + $$ = "text"; + break; + case Node.PROCESSING_INSTRUCTION_NODE: + $$ = "processing-instruction"; + break; + default: + $$ = "node"; + break; + } + }*/ + ; + +variable_reference: + DOLLAR qname + { + String name = (String) $2; + $$ = new VariableReference(variableResolver, getQName(name)); + } + ; + +name_test: + STAR + { + $$ = new NameTest(null, true, true); + } + | NAME COLON STAR + { + QName qName = getQName((String) $1); + $$ = new NameTest(qName, true, false); + } + | qname + { + QName qName = getQName((String) $1); + $$ = new NameTest(qName, false, false); + } + ; + +qname: + NAME + | NAME COLON NAME + { + $$ = (String) $1 + ':' + (String) $3; + } + ; + +node_type: + COMMENT + { + $$ = new Short(Node.COMMENT_NODE); + } + | TEXT + { + $$ = new Short(Node.TEXT_NODE); + } + | PROCESSING_INSTRUCTION + { + $$ = new Short(Node.PROCESSING_INSTRUCTION_NODE); + } + | NODE + { + $$ = new Short((short) 0); + } + ; + +%% + +} diff --git a/libjava/classpath/gnu/xml/xpath/XPathTokenizer.java b/libjava/classpath/gnu/xml/xpath/XPathTokenizer.java new file mode 100644 index 00000000000..a287137827b --- /dev/null +++ b/libjava/classpath/gnu/xml/xpath/XPathTokenizer.java @@ -0,0 +1,592 @@ +/* XPathTokenizer.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.xpath; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.util.Map; +import java.util.TreeMap; + +/*import antlr.Token; +import antlr.TokenStream; +import antlr.TokenStreamException; +import antlr.TokenStreamIOException;*/ + +/** + * XPath 1.0 expression tokenizer. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XPathTokenizer +implements XPathParser.yyInput +//implements TokenStream +{ + + static class XPathToken + //extends Token + { + + int type; + String val; + + XPathToken (int type) + { + this (type, null); + } + + XPathToken (int type, String val) + { + //super (type); + this.type = type; + this.val = val; + } + + public String getText () + { + return val; + } + + public String toString () + { + return val; + } + + } + + static final Map keywords = new TreeMap (); + static + { + keywords.put ("ancestor", new Integer (XPathParser.ANCESTOR)); + keywords.put ("ancestor-or-self", new Integer (XPathParser.ANCESTOR_OR_SELF)); + keywords.put ("attribute", new Integer (XPathParser.ATTRIBUTE)); + keywords.put ("child", new Integer (XPathParser.CHILD)); + keywords.put ("descendant", new Integer (XPathParser.DESCENDANT)); + keywords.put ("descendant-or-self", new Integer (XPathParser.DESCENDANT_OR_SELF)); + keywords.put ("following", new Integer (XPathParser.FOLLOWING)); + keywords.put ("following-sibling", new Integer (XPathParser.FOLLOWING_SIBLING)); + keywords.put ("namespace", new Integer (XPathParser.NAMESPACE)); + keywords.put ("parent", new Integer (XPathParser.PARENT)); + keywords.put ("preceding", new Integer (XPathParser.PRECEDING)); + keywords.put ("preceding-sibling", new Integer (XPathParser.PRECEDING_SIBLING)); + keywords.put ("self", new Integer (XPathParser.SELF)); + keywords.put ("div", new Integer (XPathParser.DIV)); + keywords.put ("mod", new Integer (XPathParser.MOD)); + keywords.put ("or", new Integer (XPathParser.OR)); + keywords.put ("and", new Integer (XPathParser.AND)); + keywords.put ("comment", new Integer (XPathParser.COMMENT)); + keywords.put ("processing-instruction", new Integer (XPathParser.PROCESSING_INSTRUCTION)); + keywords.put ("text", new Integer (XPathParser.TEXT)); + keywords.put ("node", new Integer (XPathParser.NODE)); + } + + Reader in; + XPathToken token; + XPathToken lastToken; + + public XPathTokenizer (String expr) + { + this (new StringReader (expr)); + } + + XPathTokenizer (Reader in) + { + this.in = in.markSupported () ? in : new BufferedReader (in); + } + + /* Begin ANTLR specific * + + public Token nextToken () + throws TokenStreamException + { + try + { + if (!advance ()) + { + throw new TokenStreamException ("eof"); + } + token (); + return token; + } + catch (IOException e) + { + throw new TokenStreamIOException (e); + } + } + + * End ANTLR specific */ + + public boolean advance () + throws IOException + { + lastToken = token; + int c = in.read (); + switch (c) + { + case -1: // eof + return false; + case 0x20: + case 0x09: + case 0x0d: + case 0x0a: // skip whitespace + return advance (); + case 0x22: // " + case 0x27: // ' + token = consume_literal (c); + break; + case 0x28: // ( + token = new XPathToken (XPathParser.LP); + break; + case 0x29: // ) + token = new XPathToken (XPathParser.RP); + break; + case 0x5b: // [ + token = new XPathToken (XPathParser.LB); + break; + case 0x5d: // ] + token = new XPathToken (XPathParser.RB); + break; + case 0x2c: // , + token = new XPathToken (XPathParser.COMMA); + break; + case 0x7c: // | + token = new XPathToken (XPathParser.PIPE); + break; + case 0x2f: // / + in.mark (1); + int d1 = in.read (); + if (d1 == 0x2f) + { + token = new XPathToken (XPathParser.DOUBLE_SLASH); + } + else + { + in.reset (); + token = new XPathToken (XPathParser.SLASH); + } + break; + case 0x3d: // = + token = new XPathToken (XPathParser.EQ); + break; + case 0x21: // ! + in.mark (1); + int d2 = in.read (); + if (d2 == 0x3d) // = + { + token = new XPathToken (XPathParser.NE); + } + else + { + in.reset (); + token = new XPathToken (XPathParser.yyErrorCode); + } + break; + case 0x3e: // > + in.mark (1); + int d3 = in.read (); + if (d3 == 0x3d) // = + { + token = new XPathToken (XPathParser.GTE); + } + else + { + in.reset (); + token = new XPathToken (XPathParser.GT); + } + break; + case 0x3c: // < + in.mark (1); + int d4 = in.read (); + if (d4 == 0x3d) // = + { + token = new XPathToken (XPathParser.LTE); + } + else + { + in.reset (); + token = new XPathToken (XPathParser.LT); + } + break; + case 0x2b: // + + token = new XPathToken (XPathParser.PLUS); + break; + case 0x2d: // - + token = new XPathToken (XPathParser.MINUS); + break; + case 0x40: // @ + token = new XPathToken (XPathParser.AT); + break; + case 0x2a: // * + token = new XPathToken (XPathParser.STAR); + break; + case 0x24: // $ + token = new XPathToken (XPathParser.DOLLAR); + break; + case 0x3a: // : + in.mark (1); + int d5 = in.read (); + if (d5 == 0x3a) + { + token = new XPathToken (XPathParser.DOUBLE_COLON); + } + else + { + in.reset (); + token = new XPathToken (XPathParser.COLON); + } + break; + case 0x2e: // . + in.mark (1); + int d6 = in.read (); + if (d6 == 0x2e) + { + token = new XPathToken (XPathParser.DOUBLE_DOT); + } + else + { + in.reset (); + token = new XPathToken (XPathParser.DOT); + } + break; + default: + if (c >= 0x30 && c <= 0x39) + { + token = consume_digits (c); + } + else if (c == 0x5f || Character.isLetter ((char) c)) + { + token = consume_name (c); + } + else + { + token = new XPathToken (XPathParser.yyErrorCode); + } + } + return true; + } + + public int token () + { + return token.type; + } + + public Object value () + { + return token.val; + } + + XPathToken consume_literal (int delimiter) + throws IOException + { + StringBuffer buf = new StringBuffer (); + while (true) + { + int c = in.read (); + if (c == -1) + { + return new XPathToken (XPathParser.yyErrorCode); + } + else if (c == delimiter) + { + return new XPathToken (XPathParser.LITERAL, buf.toString ()); + } + else + { + buf.append ((char) c); + } + } + } + + XPathToken consume_digits (int c) + throws IOException + { + StringBuffer buf = new StringBuffer (); + buf.append ((char) c); + while (true) + { + in.mark (1); + c = in.read (); + if (c >= 0x30 && c <= 0x39) + { + buf.append ((char) c); + } + else + { + in.reset (); + return new XPathToken (XPathParser.DIGITS, buf.toString ()); + } + } + } + + XPathToken consume_name (int c) + throws IOException + { + StringBuffer buf = new StringBuffer (); + buf.append ((char) c); + while (true) + { + in.mark (1); + c = in.read (); + if (isNameChar (c)) + { + buf.append ((char) c); + } + else + { + in.reset (); + String name = buf.toString (); + Integer keyword = (Integer) keywords.get (name); + if (keyword == null) + { + return new XPathToken (XPathParser.NAME, name); + } + else + { + int val = keyword.intValue (); + switch (val) + { + case XPathParser.NODE: + case XPathParser.COMMENT: + case XPathParser.TEXT: + case XPathParser.PROCESSING_INSTRUCTION: + // Consume subsequent ( + in.mark (1); + do + { + c = in.read (); + } + while (c == 0x20 || c == 0x09); + if (c != 0x28) + { + in.reset (); + return new XPathToken (XPathParser.NAME, name); + } + break; + case XPathParser.CHILD: + case XPathParser.PARENT: + case XPathParser.SELF: + case XPathParser.DESCENDANT: + case XPathParser.ANCESTOR: + case XPathParser.DESCENDANT_OR_SELF: + case XPathParser.ANCESTOR_OR_SELF: + case XPathParser.ATTRIBUTE: + case XPathParser.NAMESPACE: + case XPathParser.FOLLOWING: + case XPathParser.FOLLOWING_SIBLING: + case XPathParser.PRECEDING: + case XPathParser.PRECEDING_SIBLING: + // Check that this is an axis specifier + in.mark(1); + do + { + c = in.read(); + } + while (c == 0x20 || c == 0x09); + if (c == 0x3a) + { + c = in.read(); + if (c == 0x3a) + { + in.reset(); + return new XPathToken(val); + } + } + in.reset(); + return new XPathToken(XPathParser.NAME, name); + case XPathParser.DIV: + case XPathParser.MOD: + // May be a name + if (lastToken == null) + { + return new XPathToken(XPathParser.NAME, name); + } + switch (lastToken.type) + { + case XPathParser.LP: + case XPathParser.LB: + case XPathParser.COMMA: + case XPathParser.PIPE: + case XPathParser.EQ: + case XPathParser.NE: + case XPathParser.GT: + case XPathParser.LT: + case XPathParser.GTE: + case XPathParser.LTE: + case XPathParser.PLUS: + case XPathParser.MINUS: + case XPathParser.STAR: + case XPathParser.AT: + case XPathParser.DOLLAR: + case XPathParser.COLON: + case XPathParser.DOUBLE_COLON: + case XPathParser.DIV: + case XPathParser.MOD: + case XPathParser.OR: + case XPathParser.AND: + case XPathParser.SLASH: + return new XPathToken(XPathParser.NAME, name); + } + break; + } + return new XPathToken (val); + } + } + } + } + + boolean isNameChar (int c) + { + /* Name */ + return (c == 0x5f + || c == 0x2d + || c == 0x2e + || (c >= 0x30 && c <= 0x39) + /* CombiningChar */ + || (c >= 0x0300 && c <= 0x0345) + || (c >= 0x0360 && c <= 0x0361) + || (c >= 0x0483 && c <= 0x0486) + || (c >= 0x0591 && c <= 0x05A1) + || (c >= 0x05A3 && c <= 0x05B9) + || (c >= 0x05BB && c <= 0x05BD) + || c == 0x05BF + || (c >= 0x05C1 && c <= 0x05C2) + || c == 0x05C4 + || (c >= 0x064B && c <= 0x0652) + || c == 0x0670 + || (c >= 0x06D6 && c <= 0x06DC) + || (c >= 0x06DD && c <= 0x06DF) + || (c >= 0x06E0 && c <= 0x06E4) + || (c >= 0x06E7 && c <= 0x06E8) + || (c >= 0x06EA && c <= 0x06ED) + || (c >= 0x0901 && c <= 0x0903) + || c == 0x093C + || (c >= 0x093E && c <= 0x094C) + || c == 0x094D + || (c >= 0x0951 && c <= 0x0954) + || (c >= 0x0962 && c <= 0x0963) + || (c >= 0x0981 && c <= 0x0983) + || c == 0x09BC + || c == 0x09BE + || c == 0x09BF + || (c >= 0x09C0 && c <= 0x09C4) + || (c >= 0x09C7 && c <= 0x09C8) + || (c >= 0x09CB && c <= 0x09CD) + || c == 0x09D7 + || (c >= 0x09E2 && c <= 0x09E3) + || c == 0x0A02 + || c == 0x0A3C + || c == 0x0A3E + || c == 0x0A3F + || (c >= 0x0A40 && c <= 0x0A42) + || (c >= 0x0A47 && c <= 0x0A48) + || (c >= 0x0A4B && c <= 0x0A4D) + || (c >= 0x0A70 && c <= 0x0A71) + || (c >= 0x0A81 && c <= 0x0A83) + || c == 0x0ABC + || (c >= 0x0ABE && c <= 0x0AC5) + || (c >= 0x0AC7 && c <= 0x0AC9) + || (c >= 0x0ACB && c <= 0x0ACD) + || (c >= 0x0B01 && c <= 0x0B03) + || c == 0x0B3C + || (c >= 0x0B3E && c <= 0x0B43) + || (c >= 0x0B47 && c <= 0x0B48) + || (c >= 0x0B4B && c <= 0x0B4D) + || (c >= 0x0B56 && c <= 0x0B57) + || (c >= 0x0B82 && c <= 0x0B83) + || (c >= 0x0BBE && c <= 0x0BC2) + || (c >= 0x0BC6 && c <= 0x0BC8) + || (c >= 0x0BCA && c <= 0x0BCD) + || c == 0x0BD7 + || (c >= 0x0C01 && c <= 0x0C03) + || (c >= 0x0C3E && c <= 0x0C44) + || (c >= 0x0C46 && c <= 0x0C48) + || (c >= 0x0C4A && c <= 0x0C4D) + || (c >= 0x0C55 && c <= 0x0C56) + || (c >= 0x0C82 && c <= 0x0C83) + || (c >= 0x0CBE && c <= 0x0CC4) + || (c >= 0x0CC6 && c <= 0x0CC8) + || (c >= 0x0CCA && c <= 0x0CCD) + || (c >= 0x0CD5 && c <= 0x0CD6) + || (c >= 0x0D02 && c <= 0x0D03) + || (c >= 0x0D3E && c <= 0x0D43) + || (c >= 0x0D46 && c <= 0x0D48) + || (c >= 0x0D4A && c <= 0x0D4D) + || c == 0x0D57 + || c == 0x0E31 + || (c >= 0x0E34 && c <= 0x0E3A) + || (c >= 0x0E47 && c <= 0x0E4E) + || c == 0x0EB1 + || (c >= 0x0EB4 && c <= 0x0EB9) + || (c >= 0x0EBB && c <= 0x0EBC) + || (c >= 0x0EC8 && c <= 0x0ECD) + || (c >= 0x0F18 && c <= 0x0F19) + || c == 0x0F35 + || c == 0x0F37 + || c == 0x0F39 + || c == 0x0F3E + || c == 0x0F3F + || (c >= 0x0F71 && c <= 0x0F84) + || (c >= 0x0F86 && c <= 0x0F8B) + || (c >= 0x0F90 && c <= 0x0F95) + || c == 0x0F97 + || (c >= 0x0F99 && c <= 0x0FAD) + || (c >= 0x0FB1 && c <= 0x0FB7) + || c == 0x0FB9 + || (c >= 0x20D0 && c <= 0x20DC) + || c == 0x20E1 + || (c >= 0x302A && c <= 0x302F) + || c == 0x3099 + || c == 0x309A + /* Extender */ + || c == 0x00B7 + || c == 0x02D0 + || c == 0x02D1 + || c == 0x0387 + || c == 0x0640 + || c == 0x0E46 + || c == 0x0EC6 + || c == 0x3005 + || (c >= 0x3031 && c <= 0x3035) + || (c >= 0x309D && c <= 0x309E) + || (c >= 0x30FC && c <= 0x30FE) + /* Name */ + || Character.isLetter ((char) c)); + } + +} |