ldifexport.java

来自「JAVA开源LDAP浏览器jxplorer的源码!」· Java 代码 · 共 417 行 · 第 1/2 页

JAVA
417
字号
            return false;

        }
        else if(newRoot == null || newRoot.length() <= 0)
        {
            JOptionPane.showMessageDialog(this, CBIntText.get("Please enter a 'New Root DN'."),
                                            CBIntText.get("New Root DN"), JOptionPane.INFORMATION_MESSAGE );
            return false;
        }

        return true;
    }


    /**
     *    Launch a DataQuery that will write the ldif file.
     *
     */
    protected void doFileWrite(File saveFile)
    {
        if (saveFile == null)
            CBUtility.error(CBIntText.get("Unable to write to empty file"), null);

        final File myFile = saveFile;

        dataSource.extendedRequest(new DataQuery(DataQuery.EXTENDED)
        {
            public void doExtendedRequest(Broker b)
            {
                try
                {
                    FileWriter myFileWriter = new FileWriter(myFile);

                    pbar = new CBpbar(LdifExport.this, CBIntText.get("Saving LDIF file"), CBIntText.get("Saving Data"));

//XXX  SCHEMA-FIX; what's happening here?                  if (b instanceof SchemaBroker)
//                        ((SchemaBroker)b).setRaw(true);

                    myFileWriter.write("version: 1\n");

                    DN oldRoot = new DN(rootDN.getText());    // the original DN
                    DN newRoot = new DN(newRootDN.getText()); // the replacement DN (may be identical!)

                    if (usingSearch)
                    {
                        Vector bloop = searchTree.getAllNodes(new DN(rootDN.getText()));
                        saveLdifList(bloop, myFileWriter, oldRoot.toString(), newRoot.toString(), b);
                    }
                    else
                    {
                        saveLdifTree(oldRoot, myFileWriter, oldRoot.toString(), newRoot.toString(), b);
                    }

					//TE: this seems to resolve the problem of no being able to access the ldif file until
					//TE: focus is moved from the current DN or the user exits JX.  See Bug numbers 669 & 533.
					//TE: NOTE: this fix could be the cause of an 'Extended' error...not sure!
					myFileWriter.close();
                }
                catch (Exception e)
                {
                    setException(e);
                }

//XXX                if (b instanceof SchemaBroker)
//                   ((SchemaBroker)b).setRaw(false);

				if(pbar.isCanceled())	//TE: delete the file if the user cancels the export.
					myFile.delete();

                closeDown();
                return;
            }
        });
    }



    /**
     *    Write a subtree to an ldif file by recursing through the
     *    tree, calling saveLdifEntry as it goes...
     *
     *    @param treeApex the root node of the sub tree to be written out.
     *    @param saveFile the file being written to...
     *    @param origPrefix the original DN prefix, that may be modified
     *                           on write to be replacementPrefix.  This may be
     *                           null if no action is to be taken.
     *    @param newPrefix another DN to replace the originalPrefix.
     *    @return number of entries written
     */

    public boolean saveLdifTree(DN treeApex, FileWriter saveFile, String origPrefix, String newPrefix, Broker broker)
    {
        // sanity checks...
        if (treeApex==null) return false;
        if (pbar == null) return false;

        if (newPrefix==null) origPrefix = null;                 // sanity check
        if ((origPrefix!=null)&&(origPrefix.equals(newPrefix))) // sanity check
            {origPrefix = null; newPrefix = null; }

        if (pbar.isCanceled()) return false;                    // user canceled

        Attributes atts = null;

        try
        {
            if (treeApex.isEmpty() == false)
            {
                atts = broker.unthreadedReadEntry(treeApex, null);
            }

            if (atts != null)
            {
                DN escapeMe = new DN(treeApex);
                ldifutil.writeLdifEntry(escapeMe.toString(), saveFile, origPrefix, newPrefix, atts);     // save the current dn...
            }
            // need to get this as a DXNamingEnumeration to set progress bar...
            DXNamingEnumeration children = broker.unthreadedList(treeApex);

            pbar.push(children.size()); // might be zero...

            while (children != null && children.hasMore())
            {
                String subDNString = ((NameClassPair)children.next()).getName();
                DN child = new DN(treeApex);        // this could be done by string manip.,
                DN subDN = new DN(subDNString);
                child.addChildRDN(subDN.getLowestRDN());           // but then what happens if DN naming changes?

                if (saveLdifTree(child, saveFile, origPrefix, newPrefix, broker)==false)  // recursively traverse tree and write data
                    return false;
            }
        }
        catch (NamingException e)
        {
            CBUtility.error(this, CBIntText.get("Unable to read dn: {0} from directory", new String[] {treeApex.toString()}), e);
        }
        catch (Exception e)
        {
            CBUtility.error(this, CBIntText.get("General error reading dn: {0} from directory", new String[] {treeApex.toString()}), e);
            e.printStackTrace();
        }

        pbar.pop();
        pbar.inc();

        return true;
    }

    /**
     *    Writes a list of entries to an ldif file.
     *
     *    @param dns the list of the dns of objects to write out...
     *    @param saveFile the file being written to...
     *    @param originalPrefix the original DN prefix, that may be modified
     *                           on write to be replacementPrefix.  This may be
     *                           null if no action is to be taken.
     *    @param replacementPrefix another DN to replace the originalPrefix.
     */

    public void saveLdifList(Vector dns, FileWriter saveFile, String originalPrefix, String replacementPrefix, Broker broker)
    {
        if (replacementPrefix==null) originalPrefix = null;                        // sanity check.
        if ((originalPrefix!=null)&&(originalPrefix.equals(replacementPrefix)))   // sanity check.
        {
            originalPrefix = null;
            replacementPrefix = null;
        }
        int size = dns.size();
        pbar.push(size);

        for (int i=0; i<size; i++)
        {
            DN dn = (DN)dns.elementAt(i);

            try
            {
                Attributes atts = broker.unthreadedReadEntry(dn, null);
                ldifutil.writeLdifEntry(dn.toString(), saveFile, originalPrefix, replacementPrefix, atts);
            }
            catch (NamingException e)
            {
                log.log(Level.WARNING, "Unable to read dn: '" + dn.toString() + "' from directory ", e);
            }
            catch (Exception e)
            {
                log.log(Level.WARNING, "General error reading: dn: '" + dn.toString() + "' from directory ", e);
            }

            if (pbar.isCanceled()) return;
            pbar.inc();
        }
        pbar.close();                // no need to pop out; we're done...
    }

    private void closeDown()
    {
        try
        {
            if (saveFile != null) saveFile.close();//TE: Always == null!
            log.warning("Closed LDIF file");
        }
        catch (IOException e) {;}

		if (pbar != null) pbar.close();
        setVisible(false);
        dispose();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?