Exemplo n.º 1
0
        static java.util.Properties loadFilePrefsImpl(java.io.File file)
        {
            Properties result = new Properties();

            if (!file.exists())
            {
                file.getParentFile().mkdirs();
                return(result);
            }

            if (file.canRead())
            {
                java.io.InputStream        inJ   = null;
                java.nio.channels.FileLock lockJ = null;
                try {
                    java.io.FileInputStream istream = new java.io.FileInputStream(file);
                    inJ = new java.io.BufferedInputStream(istream);
                    java.nio.channels.FileChannel channel = istream.getChannel();
                    lockJ = channel.lockJ(0L, java.lang.Long.MAX_VALUE, true);
                    org.w3c.dom.Document doc     = builder.parse(inJ);
                    org.w3c.dom.NodeList entries = org.apache.xpath.XPathAPI.selectNodeList(doc
                                                                                            .getDocumentElement(), "entry"); //$NON-NLS-1$
                    int length = entries.getLength();
                    for (int i = 0; i < length; i++)
                    {
                        org.w3c.dom.Element node = (org.w3c.dom.Element)entries.item(i);
                        String key   = node.getAttribute("key");                        //$NON-NLS-1$
                        String value = node.getAttribute("value");                      //$NON-NLS-1$
                        result.setProperty(key, value);
                    }
                    return(result);
                } catch (java.io.IOException e) {
                } catch (org.xml.sax.SAXException e) {
                } catch (javax.xml.transform.TransformerException e) {
                    // transform shouldn't fail for xpath call
                    throw new java.lang.AssertionError(e);
                } finally {
                    releaseQuietly(lockJ);
                    closeQuietly(inJ);
                }
            }
            else
            {
                file.delete();
            }
            return(result);
        }
Exemplo n.º 2
0
        /*
         * opens a DBF file. This is based on Pratap Pereira's
         * Xbase.pm perl module
         * @return column definition list (java.util.Hashtable<Object,Object>)
         *
         * @author Thomas Morgner <*****@*****.**> added check for
         * file exists, before the file is opened. Opening a non existing
         * file will create a new file, and we get errors while trying
         * to read the non-existend headers
         */
        java.util.Hashtable <Object, Object> open_dbf() //throws TinySQLException
        {
            try
            {
                java.io.File f = new java.io.File(fullPath);
                if (TinySQLGlobals.DEBUG)
                {
                    java.lang.SystemJ.outJ.println("Try to open  " + f.getAbsolutePath());
                }
                if (!f.exists())
                {
                    throw new TinySQLException("Unable to open " + f.getAbsolutePath()
                                               + " - does not exist. or can't be read.");
                }
                else if (!f.canRead())
                {
                    throw new TinySQLException("Unable to open " + f.getAbsolutePath()
                                               + " - file can't be read (permissions?).");
                }
                if (f.canWrite())
                {
                    ftbl = new java.io.RandomAccessFile(f, "rw");
                }
                else
                {
                    /*
                     *          Open readonly if the file is not writeable. Needed for
                     *          databases on CD-Rom
                     */
                    ftbl = new java.io.RandomAccessFile(f, "r");
                }

                /*
                 *       Read the first 32 bytes ...
                 */
                dbfHeader = new DBFHeader(ftbl);

                /*
                 *       read the column info (each is a 32 byte bulk) ...
                 */
                java.util.Hashtable <Object, Object> coldef_list = new java.util.Hashtable <Object, Object>();
                columnNameKeys = new java.util.Vector <Object>();
                int locn = 0; // offset of the current column
                for (int i = 1; i <= dbfHeader.numFields; i++)
                {
                    TsColumn coldef = DBFFile.readColdef(ftbl, table, i, locn);
                    locn += coldef.size; // increment locn by the length of this field.
                    coldef_list.put(coldef.name, coldef);
                    columnNameKeys.addElement(coldef.name);
                }
                fileOpen = true;
                return(coldef_list);
            }
            catch (Exception e)
            {
                if (TinySQLGlobals.DEBUG)
                {
                    java.lang.SystemJ.err.println(e.ToString());                      // e.printStackTrace();
                }
                throw new TinySQLException(e.getMessage());
            }
        }