⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldifexport.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ca.directory.jxplorer;

import java.awt.*;
import java.util.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.io.*;
import javax.swing.*;

import javax.naming.*;
import javax.naming.directory.*;

import com.ca.directory.jxplorer.tree.*;
import com.ca.directory.jxplorer.broker.*;
import com.ca.commons.naming.*;
import com.ca.commons.cbutil.*;

public class LdifExport extends CBDialog
{
    JTextArea rootDN, newRootDN;
    DataSource dataSource;
    SmartTree searchTree;
    SmartTree schemaTree;

    FileWriter saveFile;
    LdifUtility ldifutil = new LdifUtility();

    boolean usingSearch;

    CBpbar pbar;  // shows how far an operation has progressed for large ops.

    static String lastDirectory = null;

    private static Logger log = Logger.getLogger(LdifExport.class.getName());

    /**
     *    Constructor for the LdifExport window.  Takes a DN, and
     *    a jndi broker.  If it is exporting from a search result
     *    set, it takes the search tree as a parameter, and a boolean
     *    flag specifying that the tree should be used to list the DNs
     *    to be exported; otherwise it does a full dump from the provided
     *    DN.<p>
     *
     *    The constructor sets up the GUI, defining buttons and fields
     *    and registering button listeners.
     *
     *    @param D the base DN to work from
     *    @param broker the jndi broker to use to read entry attribtues from,
     *                  and to physically write the ldif file
     *    @param searchTree (possibly) a tree containing a list of search
     *                      results, to be used if the search flag is set
     *    @param usingSearch a boolean flag that forces the reading of the
     *                       list of DNs to save from the tree, rather than
     *                       directly from the directory...
     */
    public LdifExport(DN D, DataSource broker, SmartTree searchTree, boolean usingSearch, Frame owner)
    {
        this(D, broker, searchTree, usingSearch, owner, HelpIDs.LDIF_EXPORT_TREE);
    }

    /**
     *    Constructor for the LdifExport window.  Takes a DN, and
     *    a jndi broker.  If it is exporting from a search result
     *    set, it takes the search tree as a parameter, and a boolean
     *    flag specifying that the tree should be used to list the DNs
     *    to be exported; otherwise it does a full dump from the provided
     *    DN.<p>
     *
     *    The constructor sets up the GUI, defining buttons and fields
     *    and registering button listeners.
     *
     *    @param D the base DN to work from
     *    @param broker the jndi broker to use to read entry attribtues from,
     *                  and to physically write the ldif file
     *    @param searchTree (possibly) a tree containing a list of search
     *                      results, to be used if the search flag is set
     *    @param usingSearch a boolean flag that forces the reading of the
     *                       list of DNs to save from the tree, rather than
     *                       directly from the directory...
     *    @param helpID the ID of the help page to attach to the Help button.
     */
    public LdifExport(DN D, DataSource broker, SmartTree searchTree, boolean usingSearch, Frame owner, String helpID)
    {
        super(owner, CBIntText.get("LDIF Export"), helpID);

        OK.setToolTipText(CBIntText.get("Perform the LDIF export"));
        Cancel.setToolTipText(CBIntText.get("Cancel without performing an LDIF export"));
        Help.setToolTipText(CBIntText.get("Display help about LDIF exporting"));

        if (D==null) D = new DN();

        this.dataSource = broker;
        this.searchTree = searchTree;
        this.usingSearch = usingSearch;

        display.add(new JLabel(CBIntText.get("Root DN")),0,0);

        display.makeHeavy();

        rootDN = new JTextArea(D.toString());
		rootDN.setLineWrap(true);		//TE: allows line wrapping.
		display.add(new JScrollPane(rootDN, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),1,0);		//TE: scroll pane that scrolls vertically on need and never scrolls horizontally.

		display.makeLight();

		display.add(new JLabel(CBIntText.get("New root DN")),0,1);

		display.makeHeavy();
        newRootDN = new JTextArea(D.toString());
		newRootDN.setLineWrap(true);		//TE: allows line wrapping.
		display.add(new JScrollPane(newRootDN, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),1,1);		//TE: scroll pane that scrolls vertically on need and never scrolls horizontally.

        display.makeLight();
    }

    /**
    *    A quick spot of mucking around to add '.ldif' to naked files.
    *
    */

    protected File adjustFileName(File readFile)
    {
        if (readFile == null) return null;            // sanity check

        if (readFile.exists()) return readFile;       // don't do anything if file already exists...

        String name = readFile.getName();
        if (name.indexOf('.') != -1) return readFile; // ... or if it already has an extension.

        name = name + ".ldif";

        return new File(readFile.getParentFile(), name);
    }

    /**
     *    This method is called by the base class when the OK button is pressed.
     *    Handles actually writing the ldif file (relying heavily on LdifUtility for
     *    the grunt work).  Does the actual file writing in a separate thread.
     */

    public void doOK()
    {
        if(!checkRootDN())      //TE: bug 5057.
            return;

        setVisible(false);

        JFileChooser chooser = new JFileChooser(JXplorer.getProperty("ldif.homeDir"));

        chooser.addChoosableFileFilter(new CBFileFilter(new String[] {"ldif", "ldi"},"Ldif Files (*.ldif, *.ldi)"));

        int option = chooser.showSaveDialog(this);

        if (option == JFileChooser.APPROVE_OPTION) // only do something if user chose 'ok'
        {
            File readFile = chooser.getSelectedFile();
            if (readFile == null)
            {
                CBUtility.error(CBIntText.get("Please select a file"));
            }
            else
            {
                readFile = adjustFileName(readFile);  // whack an '.ldif' on the end if necessary.

				int response = -1;
				if (readFile.exists())	//TE: ask the user if they want to overwrite an existing file.
				{
					response = JOptionPane.showConfirmDialog(this, CBIntText.get("File ''{0}'' already exsists.  Do you want to replace it?", new String[] {readFile.toString()}),
	        							CBIntText.get("Overwrite Confirmation"), JOptionPane.YES_NO_OPTION );

					if (response != JOptionPane.YES_OPTION)
					{
						setVisible(true);
						return;
					}
				}

                JXplorer.setProperty("ldif.homeDir", readFile.getParent());
                doFileWrite(readFile);
            }
        }
    }

    /**
     * Does three checks.<br><br>
     * 1) if there is a root DN and a new root DN.  If not a confirmation message appears
     * asking if the user wants to export the full subtree.<br>
     * 2) if there is no root DN.  In this case a dialog appears asking for it.
     * 3) if there is no new root DN.  In this case a dialog appears asking for it.
     * @return true only if all the checks succeed (or user approves the export full subtree).
     */
    public boolean checkRootDN()
    {
        String oldRoot = (rootDN.getText()).trim();    // the original DN
        String newRoot = (newRootDN.getText()).trim(); // the replacement DN (may be identical!)

        if((oldRoot == null || oldRoot.length() <= 0) && (newRoot == null || newRoot.length() <= 0))
        {
            int response = JOptionPane.showConfirmDialog(this, CBIntText.get("Without a 'Root DN' and a 'New Root DN', the full tree will be exported.  Do you want to continue?"),
                                CBIntText.get("Export Full Tree"), JOptionPane.YES_NO_OPTION );
            if (response != JOptionPane.YES_OPTION)
                return false;

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

⌨️ 快捷键说明

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