/** * Take a list of external links and copy the referred files to a given directory. * This method should be run off the Event Dispatch Thread. A progress bar, if given, * will be updated on the EDT. * * @param files The list of file links. * @param toDir The directory to copy the files to. * @param metaData The MetaData for the database containing the external links. This is needed * because the database might have its own file directory. * @param prog A JProgressBar which will be updated to show the progress of the process. * This argument can be null if no progress bar is needed. * @param deleteOriginalFiles if true, the files in their original locations will be deleted * after copying, for each file whose source directory is different from the destination * directory differs. * @param callback An ActionListener which should be notified when the process is finished. * This parameter can be null if no callback is needed. */ public static void copyExternalLinksToDirectory(final List<FileListEntry> files, File toDir, MetaData metaData, final JProgressBar prog, boolean deleteOriginalFiles, final ActionListener callback) { if (prog != null) SwingUtilities.invokeLater(new Runnable() { public void run() { prog.setMaximum(files.size()); prog.setValue(0); prog.setIndeterminate(false); } }); Set<String> fileNames = new HashSet<String>(); int i=0; for (Iterator<FileListEntry> iterator = files.iterator(); iterator.hasNext();) { FileListEntry entry = iterator.next(); File file = new File(entry.getLink()); // We try to check the extension for the file: String name = file.getName(); int pos = name.lastIndexOf('.'); String extension = ((pos >= 0) && (pos < name.length() - 1)) ? name.substring(pos + 1) .trim().toLowerCase() : null; // Find the default directory for this field type, if any: String[] dir = metaData.getFileDirectory(extension); // Include the standard "file" directory: String[] fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD); // Include the directory of the bib file: ArrayList<String> al = new ArrayList<String>(); for (int i2 = 0; i2 < dir.length; i2++) if (!al.contains(dir[i2])) al.add(dir[i2]); for (int i2 = 0; i2 < fileDir.length; i2++) if (!al.contains(fileDir[i2])) al.add(fileDir[i2]); String[] dirs = al.toArray(new String[al.size()]); File tmp = Util.expandFilename(entry.getLink(), dirs); if (tmp != null) file = tmp; // Check if we have arrived at an existing file: if (file.exists()) { if (fileNames.contains(name)) { // Oops, a file of that name already exists.... } else { fileNames.add(name); File destination = new File(toDir, name); // Check if the source and destination locations differ: if (!destination.equals(file)) { try { // Copy the file: Util.copyFile(file, destination, false); // Delete the original file if requested: if (deleteOriginalFiles) file.delete(); } catch (IOException ex) { ex.printStackTrace(); } } else { // Destination and source is the same. Do nothing. } // Update progress bar: i++; final int j = i; if (prog != null) SwingUtilities.invokeLater(new Runnable() { public void run() { prog.setValue(j); } }); } } else { // The link could not be resolved to an existing file. } } if (callback != null) { callback.actionPerformed(null); } }
public List<String> findCitedKeys() throws com.sun.star.container.NoSuchElementException, WrappedTargetException { XReferenceMarksSupplier supplier = (XReferenceMarksSupplier) UnoRuntime.queryInterface( XReferenceMarksSupplier.class, xCurrentComponent); XNameAccess xNamedMarks = supplier.getReferenceMarks(); String[] names = xNamedMarks.getElementNames(); ArrayList<String> keys = new ArrayList<String>(); for (int i = 0; i < names.length; i++) { Object bookmark = xNamedMarks.getByName(names[i]); XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface( XTextContent.class, bookmark); String name = names[i]; List<String> newKeys = parseRefMarkName(name); for (String key : newKeys) if (!keys.contains(key)) keys.add(key); } return keys; }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/external/AccessLinksForEntries.java File path: /jabref-2.10/src/java/net/sf/jabref/oo/OOBibBase.java
Method name: void copyExternalLinksToDirectory(List, File, MetaData, JProgressBar, boolean, ActionListener) Method name: List findCitedKeys()
Number of AST nodes: 3 Number of AST nodes: 3
1
/**
1
public List<String> findCitedKeys() throws com.sun.star.container.NoSuchElementException, WrappedTargetException {
2
     * Take a list of external links and copy the referred files to a given directory.
2
3
     * This method should be run off the Event Dispatch Thread. A progress bar, if given,
3
        XReferenceMarksSupplier supplier = (XReferenceMarksSupplier) UnoRuntime.queryInterface(
4
     * will be updated on the EDT.
4
                XReferenceMarksSupplier.class, xCurrentComponent);
5
     *
5
        XNameAccess xNamedMarks = supplier.getReferenceMarks();
6
     * @param files The list of file links.
6
        String[] names = xNamedMarks.getElementNames();
7
     * @param toDir The directory to copy the files to.
7
        ArrayList<String> keys = new ArrayList<String>();
8
     * @param metaData The MetaData for the database containing the external links. This is needed
8
        for (int i = 0; i < names.length; i++) {
9
     *  because the database might have its own file directory.
9
            Object bookmark = xNamedMarks.getByName(names[i]);
10
     * @param prog A JProgressBar which will be updated to show the progress of the process.
10
            XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(
11
     *  This argument can be null if no progress bar is needed.
11
                    XTextContent.class, bookmark);
12
     * @param deleteOriginalFiles if true, the files in their original locations will be deleted
12
13
     *  after copying, for each file whose source directory is different from the destination
13
            String name = names[i];
14
     *  directory differs.
14
            List<String> newKeys = parseRefMarkName(name);
15
     * @param callback An ActionListener which should be notified when the process is finished.
15
            for (String key : newKeys)
16
     *  This parameter can be null if no callback is needed.
16
                if (!keys.contains(key))
17
     */
17
                    keys.add(key);
18
    public static void copyExternalLinksToDirectory(final List<FileListEntry> files, File toDir,
18
        }
19
                                                    MetaData metaData, final JProgressBar prog,
19
20
                                                    boolean deleteOriginalFiles,
20
        return keys;
21
                                                    final ActionListener callback) {
21
    }
22
23
        if (prog != null) SwingUtilities.invokeLater(new Runnable() {
24
            public void run() {
25
                prog.setMaximum(files.size());
26
                prog.setValue(0);
27
                prog.setIndeterminate(false);
28
            }
29
        });
30
31
        Set<String> fileNames = new HashSet<String>();
32
33
        int i=0;
34
35
        for (Iterator<FileListEntry> iterator = files.iterator(); iterator.hasNext();) {
36
            FileListEntry entry = iterator.next();
37
            File file = new File(entry.getLink());
38
39
            // We try to check the extension for the file:
40
            String name = file.getName();
41
            int pos = name.lastIndexOf('.');
42
            String extension = ((pos >= 0) && (pos < name.length() - 1)) ? name.substring(pos + 1)
43
                .trim().toLowerCase() : null;
44
45
            // Find the default directory for this field type, if any:
46
            String[] dir = metaData.getFileDirectory(extension);
47
            // Include the standard "file" directory:
48
            String[] fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
49
            // Include the directory of the bib file:
50
            ArrayList<String> al = new ArrayList<String>();
51
            for (int i2 = 0; i2 < dir.length; i2++)
52
                if (!al.contains(dir[i2])) al.add(dir[i2]);
53
            for (int i2 = 0; i2 < fileDir.length; i2++)
54
                if (!al.contains(fileDir[i2])) al.add(fileDir[i2]);
55
56
            String[] dirs = al.toArray(new String[al.size()]);
57
            File tmp = Util.expandFilename(entry.getLink(), dirs);
58
            if (tmp != null)
59
                file = tmp;
60
61
            // Check if we have arrived at an existing file:
62
            if (file.exists()) {
63
                if (fileNames.contains(name)) {
64
                    // Oops, a file of that name already exists....
65
                }
66
                else {
67
                    fileNames.add(name);
68
                    File destination = new File(toDir, name);
69
70
                    // Check if the source and destination locations differ:
71
                    if (!destination.equals(file)) {
72
                        try {
73
                            // Copy the file:
74
                            Util.copyFile(file, destination, false);
75
                            // Delete the original file if requested:
76
                            if (deleteOriginalFiles)
77
                                file.delete();
78
                            
79
                        } catch (IOException ex) {
80
                            ex.printStackTrace();
81
                        }
82
                    }
83
                    else {
84
                        // Destination and source is the same. Do nothing.
85
                    }
86
                    // Update progress bar:
87
                    i++;
88
                    final int j = i;
89
90
                    if (prog != null) SwingUtilities.invokeLater(new Runnable() {
91
                        public void run() {
92
                            prog.setValue(j);
93
                        }
94
                    });
95
                }
96
            }
97
            else {
98
                // The link could not be resolved to an existing file.
99
                
100
            }
101
        }
102
103
        if (callback != null) {
104
            callback.actionPerformed(null);
105
        }
106
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements3
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment0
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.600
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    17
    for (int i2 = 0; i2 < fileDir.length; i2++)
    17
    for (int i2 = 0; i2 < fileDir.length; i2++)
    10
    for (String key : newKeys)
    Differences
    Expression1Expression2Difference
    java.lang.String[]java.util.ListVARIABLE_TYPE_MISMATCH
    java.lang.String[]java.util.ListVARIABLE_TYPE_MISMATCH
    java.lang.String[]java.util.ListVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type java.lang.String[] of variable fileDir does not match with type java.util.List<java.lang.String> of variable newKeys
    • Make classes java.lang.String[] and java.util.List extend a common superclass
    Type java.lang.String[] of variable fileDir does not match with type java.util.List<java.lang.String> of variable newKeys
    • Make classes java.lang.String[] and java.util.List extend a common superclass
    Type java.lang.String[] of variable fileDir does not match with type java.util.List<java.lang.String> of variable newKeys
    • Make classes java.lang.String[] and java.util.List extend a common superclass
    10
    for (String key : newKeys)
    18
    if (!al.contains(fileDir[i2]))
    18
    if (!al.contains(fileDir[i2]))
    11
    if (!keys.contains(key))
    Differences
    Expression1Expression2Difference
    fileDir[i2]keyTYPE_COMPATIBLE_REPLACEMENT
    alkeysVARIABLE_NAME_MISMATCH
    Preondition Violations
    Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression key cannot be parameterized, because it has dependencies to/from statements that will be extracted
    11
    if (!keys.contains(key))
    19
    al.add(fileDir[i2]);
    19
    al.add(fileDir[i2]);
    12
    keys.add(key);
    Differences
    Expression1Expression2Difference
    fileDir[i2]keyTYPE_COMPATIBLE_REPLACEMENT
    alkeysVARIABLE_NAME_MISMATCH
    Preondition Violations
    Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression key cannot be parameterized, because it has dependencies to/from statements that will be extracted
    12
    keys.add(key);
    Precondition Violations (7)
    Row Violation
    1Type java.lang.String[] of variable fileDir does not match with type java.util.List<java.lang.String> of variable newKeys
    2Type java.lang.String[] of variable fileDir does not match with type java.util.List<java.lang.String> of variable newKeys
    3Type java.lang.String[] of variable fileDir does not match with type java.util.List<java.lang.String> of variable newKeys
    4Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    5Expression key cannot be parameterized, because it has dependencies to/from statements that will be extracted
    6Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    7Expression key cannot be parameterized, because it has dependencies to/from statements that will be extracted