    @SuppressWarnings("unchecked")
    private ReaderFeed processEntrys(String urlStr, String currentFlag) throws UnsupportedEncodingException, IOException, JDOMException {
        String key = "processEntrys@" + urlStr + "_" + currentFlag;
        if (cache.containsKey(key)) {
            return (ReaderFeed) cache.get(key);
        }
        List<Post> postList = new ArrayList<Post>();
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Cookie", "SID=" + sid);
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        SAXBuilder builder = new SAXBuilder(false);
        Document doc = builder.build(reader);
        Element root = doc.getRootElement();
        Namespace grNamespace = root.getNamespace("gr");
        Namespace namespace = root.getNamespace();
        String newflag = root.getChildText("continuation", grNamespace);
        String title = root.getChildText("title", namespace);
        String subTitle = root.getChildText("subtitle", namespace);
        List<Element> entryList = root.getChildren("entry", namespace);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        for (Element e : entryList) {
            Post post = new Post();
            post.setTitle(e.getChildText("title", namespace));
            try {
                post.setDate(sdf.parse(e.getChildText("published", namespace)));
            } catch (ParseException e1) {
            }
            post.setUrl(e.getChild("link", namespace).getAttributeValue("href"));
            post.setSauthor(e.getChild("author", namespace).getChildText("name", namespace));
            String content = e.getChildText("content", namespace);
            if (StringUtils.isEmpty(content)) {
                content = e.getChildText("description", namespace);
            }
            if (StringUtils.isEmpty(content)) {
                content = e.getChildText("summary", namespace);
            }
            post.setContent(content);
            postList.add(post);
        }
        ReaderFeed readerFeed = new ReaderFeed();
        readerFeed.setTitle(title);
        readerFeed.setSubTitle(subTitle);
        readerFeed.setFlag(newflag);
        readerFeed.setPostList(postList);
        cache.put(key, readerFeed);
        return readerFeed;
    }


    protected void setUp() throws Exception {
        super.setUp();
        bundles = Activator.bundleContext.getBundles();
        for (int i = 0; i < bundles.length; ++i) {
            if (bundles[i] != null) {
                if ((bundles[i].getSymbolicName() == null) || (!bundles[i].getSymbolicName().startsWith(PSS))) {
                    bundles[i] = null;
                }
            }
        }
        checklist = new Properties();
        try {
            URL url = Activator.bundleContext.getBundle().getResource(CHECK_FILE);
            InputStream is = new BufferedInputStream(url.openStream());
            checklist.load(is);
            is.close();
        } catch (FileNotFoundException fe) {
            fail("Failed to find service checklist file");
        } catch (IOException e) {
            fail("Failed to load service checklist file");
        }
        if (pssTracker == null) {
            pssTracker = new ServiceTracker(Activator.bundleContext, IPssManager.class.getName(), null);
        }
        pssTracker.open();
    }


    public void reset(String componentName, int currentPilot) {
        try {
            PreparedStatement psta = jdbc.prepareStatement("DELETE FROM component_prop " + "WHERE pilot_id = ? " + "AND component_name = ?");
            psta.setInt(1, currentPilot);
            psta.setString(2, componentName);
            psta.executeUpdate();
            jdbc.commit();
        } catch (SQLException e) {
            jdbc.rollback();
            log.debug(e);
        }
    }


    public void reset(int currentPilot) {
        try {
            PreparedStatement psta = jdbc.prepareStatement("DELETE FROM component_prop " + "WHERE pilot_id = ? ");
            psta.setInt(1, currentPilot);
            psta.executeUpdate();
            jdbc.commit();
        } catch (SQLException e) {
            jdbc.rollback();
            log.debug(e);
        }
    }


    public void writeBack(File destinationFile, boolean makeCopy) throws IOException {
        if (makeCopy) {
            FileChannel sourceChannel = new java.io.FileInputStream(getFile()).getChannel();
            FileChannel destinationChannel = new java.io.FileOutputStream(destinationFile).getChannel();
            sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
            sourceChannel.close();
            destinationChannel.close();
        } else {
            getFile().renameTo(destinationFile);
        }
        if (getExifTime() != null && getOriginalTime() != null && !getExifTime().equals(getOriginalTime())) {
            String adjustArgument = "-ts" + m_dfJhead.format(getExifTime());
            ProcessBuilder pb = new ProcessBuilder(m_tm.getJheadCommand(), adjustArgument, destinationFile.getAbsolutePath());
            pb.directory(destinationFile.getParentFile());
            System.out.println(pb.command().get(0) + " " + pb.command().get(1) + " " + pb.command().get(2));
            final Process p = pb.start();
            try {
                p.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public SparqlQueryLoaderImpl() throws IOException {
        ClassLoader loader = SparqlQueryLoaderImpl.class.getClassLoader();
        URL url = loader.getResource(PROPERTIES_FILENAME);
        InputStream stream = url.openStream();
        properties.load(stream);
        stream.close();
        String names = getProperties().getProperty(NAMES_PARAMETER);
        StringTokenizer st = new StringTokenizer(names, ",");
        while (st.hasMoreTokens()) {
            String name = st.nextToken();
            String value = readContents(String.format("sparql/%s.sparql", name));
            getName2Query().put(name, value);
        }
    }


    public static void copy(String fileFrom, String fileTo) throws IOException {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputStream = new FileInputStream(fileFrom);
            outputStream = new FileOutputStream(fileTo);
            inputChannel = inputStream.getChannel();
            outputChannel = outputStream.getChannel();
            inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        } finally {
            try {
                inputChannel.close();
            } finally {
                try {
                    outputChannel.close();
                } finally {
                    try {
                        inputStream.close();
                    } finally {
                        outputStream.close();
                    }
                }
            }
        }
    }


    private ByteBuffer getByteBuffer(String resource) throws IOException {
        ClassLoader classLoader = this.getClass().getClassLoader();
        InputStream in = classLoader.getResourceAsStream(resource);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(in, out);
        return ByteBuffer.wrap(out.toByteArray());
    }


    public static void main(String[] args) {
        final MavenDeployerGui gui = new MavenDeployerGui();
        final Chooser repositoryChooser = new Chooser(gui.formPanel, JFileChooser.DIRECTORIES_ONLY);
        final Chooser artifactChooser = new Chooser(gui.formPanel, JFileChooser.FILES_ONLY);
        final Chooser pomChooser = new Chooser(gui.formPanel, JFileChooser.FILES_ONLY, new POMFilter());
        gui.cancel.setEnabled(false);
        gui.cbDeployPOM.setVisible(false);
        gui.cbDeployPOM.setEnabled(false);
        gui.mavenBin.setText(findMavenExecutable());
        gui.repositoryBrowser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                File repo = repositoryChooser.chooseFrom(currentDir);
                if (repo != null) {
                    currentDir = repositoryChooser.currentFolder;
                    gui.repositoryURL.setText(repo.getAbsolutePath());
                }
            }
        });
        gui.artifactBrowser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                File artifact = artifactChooser.chooseFrom(currentDir);
                if (artifact != null) {
                    currentDir = artifactChooser.currentFolder;
                    gui.artifactFile.setText(artifact.getAbsolutePath());
                }
            }
        });
        gui.deploy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                deployer = new Deployer(gui, pom);
                deployer.execute();
            }
        });
        gui.clear.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                gui.console.setText("");
            }
        });
        gui.cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (deployer != null) {
                    deployer.stop();
                    deployer = null;
                }
            }
        });
        gui.cbDeployPOM.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                readPOM(gui);
            }
        });
        gui.loadPOM.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                pom = pomChooser.chooseFrom(currentDir);
                if (pom != null) {
                    currentDir = pomChooser.currentFolder;
                    readPOM(gui);
                    gui.cbDeployPOM.setText("Deploy also " + pom.getAbsolutePath());
                    gui.cbDeployPOM.setEnabled(true);
                    gui.cbDeployPOM.setVisible(true);
                }
            }
        });
        String version = "";
        try {
            URL url = Thread.currentThread().getContextClassLoader().getResource("META-INF/maven/com.mycila.maven/maven-deployer/pom.properties");
            Properties p = new Properties();
            p.load(url.openStream());
            version = " " + p.getProperty("version");
        } catch (Exception ignored) {
            version = " x.y";
        }
        JFrame frame = new JFrame("Maven Deployer" + version + " - By Mathieu Carbou (http://blog.mycila.com)");
        frame.setContentPane(gui.formPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }


    public static SOAPMessage call(SOAPMessage request, URL url) throws IOException, SOAPException {
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.connect();
        request.writeTo(conn.getOutputStream());
        MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        return mf.createMessage(null, conn.getInputStream());
    }


    private static void copyFile(final File sourceFile, final File destFile) throws IOException {
        if (!destFile.exists()) {
            if (!destFile.createNewFile()) {
                throw new IOException("Destination file cannot be created: " + destFile.getPath());
            }
        }
        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }


    public static String encrypt(String text) {
        char[] toEncrypt = text.toCharArray();
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest dig = MessageDigest.getInstance("MD5");
            dig.reset();
            String pw = "";
            for (int i = 0; i < toEncrypt.length; i++) {
                pw += toEncrypt[i];
            }
            dig.update(pw.getBytes());
            byte[] digest = dig.digest();
            int digestLength = digest.length;
            for (int i = 0; i < digestLength; i++) {
                hexString.append(hexDigit(digest[i]));
            }
        } catch (java.security.NoSuchAlgorithmException ae) {
            ae.printStackTrace();
        }
        return hexString.toString();
    }


    public static void copyCompletely(InputStream input, OutputStream output) throws IOException {
        if ((output instanceof FileOutputStream) && (input instanceof FileInputStream)) {
            try {
                FileChannel target = ((FileOutputStream) output).getChannel();
                FileChannel source = ((FileInputStream) input).getChannel();
                source.transferTo(0, Integer.MAX_VALUE, target);
                source.close();
                target.close();
                return;
            } catch (Exception e) {
            }
        }
        byte[] buf = new byte[8192];
        while (true) {
            int length = input.read(buf);
            if (length < 0) break;
            output.write(buf, 0, length);
        }
        try {
            input.close();
        } catch (IOException ignore) {
        }
        try {
            output.close();
        } catch (IOException ignore) {
        }
    }


    public void run() {
        btnReintentar.setEnabled(false);
        try {
            lblEstado.setText("Conectando con servidor...");
            escribir("\nConectando con servidor...");
            URL url = new URL("http://apeiron.sourceforge.net/version.php");
            lblEstado.setText("Obteniendo informaci�n de versi�n...");
            escribir("Ok\n");
            escribir("Obteniendo informaci�n sobre �ltima versi�n...");
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            String linea = br.readLine();
            escribir("Ok\n");
            if (linea != null) {
                escribir("Versi�n mas reciente: " + linea + "\n");
                if (Principal.version < Double.parseDouble(linea)) {
                    lblEstado.setText("Hay una nueva versi�n: Apeiron " + linea);
                    escribir("Puede obtener la actualizaci�n de: http://apeiron.sourceforge.net\n");
                    btnActualizar.setEnabled(true);
                    setVisible(true);
                } else {
                    lblEstado.setText("Usted tiene la �ltima versi�n");
                }
            }
            br.close();
        } catch (MalformedURLException e) {
            escribir("Fall�\n" + e + "\n");
            e.printStackTrace();
        } catch (IOException e) {
            escribir("Fall�\n" + e + "\n");
            e.printStackTrace();
        }
        btnReintentar.setEnabled(true);
    }


    public static void insert(Connection c, MLPApprox net, int azioneId, String descrizione, int[] indiciID, int output, Date from, Date to) throws SQLException {
        try {
            PreparedStatement ps = c.prepareStatement(insertNet, PreparedStatement.RETURN_GENERATED_KEYS);
            ArrayList<Integer> indexes = new ArrayList<Integer>(indiciID.length);
            for (int i = 0; i < indiciID.length; i++) indexes.add(indiciID[i]);
            ps.setObject(1, net);
            ps.setInt(2, azioneId);
            ps.setObject(3, indexes);
            ps.setInt(4, output);
            ps.setDate(5, from);
            ps.setDate(6, to);
            ps.setString(7, descrizione);
            ps.executeUpdate();
            ResultSet key = ps.getGeneratedKeys();
            if (key.next()) {
                int id = key.getInt(1);
                for (int i = 0; i < indiciID.length; i++) {
                    PreparedStatement psIndex = c.prepareStatement(insertNetIndex);
                    psIndex.setInt(1, indiciID[i]);
                    psIndex.setInt(2, id);
                    psIndex.executeUpdate();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                c.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
                throw e1;
            }
            throw e;
        }
    }


    public void getDataFiles(String server, String username, String password, String folder, String destinationFolder) {
        try {
            FTPClient ftp = new FTPClient();
            ftp.connect(server);
            ftp.login(username, password);
            System.out.println("Connected to " + server + ".");
            System.out.print(ftp.getReplyString());
            ftp.enterLocalActiveMode();
            ftp.changeWorkingDirectory(folder);
            System.out.println("Changed to " + folder);
            FTPFile[] files = ftp.listFiles();
            System.out.println("Number of files in dir: " + files.length);
            for (int i = 0; i < files.length; i++) {
                getFiles(ftp, files[i], destinationFolder);
            }
            ftp.logout();
            ftp.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void putDataFiles(String server, String username, String password, String folder, String destinationFolder) {
        try {
            FTPClient ftp = new FTPClient();
            ftp.connect(server);
            System.out.println("Connected");
            ftp.login(username, password);
            System.out.println("Logged in to " + server + ".");
            System.out.print(ftp.getReplyString());
            ftp.changeWorkingDirectory(destinationFolder);
            System.out.println("Changed to directory " + destinationFolder);
            File localRoot = new File(folder);
            File[] files = localRoot.listFiles();
            System.out.println("Number of files in dir: " + files.length);
            for (int i = 0; i < files.length; i++) {
                putFiles(ftp, files[i]);
            }
            ftp.logout();
            ftp.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


private Document getResponse(HttpGet request) throws ClientProtocolException, IOException, ParserConfigurationException, IllegalStateException, SAXException, NoRoutesException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() != HTTP_STATUS_OK) throw new ClientProtocolException("The HTTP request is wrong.");
        allResponse = EntityUtils.toString(response.getEntity());
        String responseText = "";
        switch(modeOption) {
            case DRIVING:
                try {
                    responseText = allResponse.substring(allResponse.indexOf("<ol"), allResponse.indexOf("
") + 5);
                } catch (Exception e) {
                    throw new NoRoutesException();
                }
                break;
            case WALKING:
                try {
                    responseText = allResponse.substring(allResponse.indexOf("<ol"), allResponse.indexOf("
") + 5);
                } catch (Exception e) {
                    throw new NoRoutesException();
                }
                break;
            case PUBLIC_TRANSIT:
                String responseWithErrors = "";
                try {
                    responseWithErrors = allResponse.substring(allResponse.indexOf("
"), allResponse.indexOf("
"));
                } catch (Exception e) {
                    throw new NoRoutesException();
                }
                responseText = responseWithErrors.replaceAll(".gif\">", ".gif\"/>").replaceAll("colspan=2", "colspan=\"2\"").replaceAll("nowrap", "").replaceAll("«", "").replaceAll(" ", "").replaceAll("»", "");
                break;
        }
        File xmlFile = new File("./data/temp/response.xml");
        PrintWriter writer = new PrintWriter(xmlFile);
        writer.println(responseText);
        writer.close();
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(xmlFile);
    }


    @Override
    public boolean copy(Document document, Folder folder) throws Exception {
        boolean isCopied = false;
        if (document.getId() != null && folder.getId() != null) {
            Document copiedDoc = new DocumentModel();
            copiedDoc.setValues(document.getValues());
            copiedDoc.setFolder(folder);
            copiedDoc.setId(null);
            em.persist(copiedDoc);
            resourceAuthorityService.applyAuthority(copiedDoc);
            List<Preference> preferences = prefService.findAll();
            Preference preference = new PreferenceModel();
            if (preferences != null && !preferences.isEmpty()) {
                preference = preferences.get(0);
            }
            String repo = preference.getRepository();
            SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATEFORMAT_YYYYMMDD);
            Calendar calendar = Calendar.getInstance();
            StringBuffer sbRepo = new StringBuffer(repo);
            sbRepo.append(File.separator);
            StringBuffer sbFolder = new StringBuffer(sdf.format(calendar.getTime()));
            sbFolder.append(File.separator).append(calendar.get(Calendar.HOUR_OF_DAY));
            File fFolder = new File(sbRepo.append(sbFolder).toString());
            if (!fFolder.exists()) {
                fFolder.mkdirs();
            }
            copiedDoc.setLocation(sbFolder.toString());
            em.merge(copiedDoc);
            File in = new File(repo + File.separator + document.getLocation() + File.separator + document.getId() + "." + document.getExt());
            File out = new File(fFolder.getAbsolutePath() + File.separator + copiedDoc.getId() + "." + copiedDoc.getExt());
            FileChannel inChannel = new FileInputStream(in).getChannel();
            FileChannel outChannel = new FileOutputStream(out).getChannel();
            try {
                inChannel.transferTo(0, inChannel.size(), outChannel);
            } catch (IOException e) {
                throw e;
            } finally {
                if (inChannel != null) inChannel.close();
                if (outChannel != null) outChannel.close();
            }
        }
        return isCopied;
    }


    public void readData(int choice) throws IOException {
        for (i = 0; i < max; i++) for (j = 0; j < max; j++) {
            phase_x[i][j] = 0.0;
            phase_y[i][j] = 0.0;
        }
        URL url;
        InputStream is;
        InputStreamReader isr;
        if (choice == 0) {
            url = getClass().getResource("resources/Phase_623_620_Achromat.dat");
            is = url.openStream();
            isr = new InputStreamReader(is);
        } else {
            url = getClass().getResource("resources/Phase_623_620_NoAchromat.dat");
            is = url.openStream();
            isr = new InputStreamReader(is);
        }
        BufferedReader br = new BufferedReader(isr);
        s = br.readLine();
        StringTokenizer st = new StringTokenizer(s);
        i = 0;
        j = 0;
        phase_x[i][j] = 4 * Double.parseDouble(st.nextToken());
        phase_y[i][j] = 4 * Double.parseDouble(st.nextToken());
        xgridmin = phase_x[i][j];
        ygridmin = phase_y[i][j];
        temp_prev = phase_x[i][j];
        kd[i][j] = Double.parseDouble(st.nextToken());
        kfs[i][j] = Double.parseDouble(st.nextToken());
        kfl[i][j] = Double.parseDouble(st.nextToken());
        kdee[i][j] = Double.parseDouble(st.nextToken());
        kdc[i][j] = Double.parseDouble(st.nextToken());
        kfc[i][j] = Double.parseDouble(st.nextToken());
        j++;
        int k = 0;
        while ((s = br.readLine()) != null) {
            st = new StringTokenizer(s);
            temp_new = 4 * Double.parseDouble(st.nextToken());
            if (temp_new != temp_prev) {
                temp_prev = temp_new;
                i++;
                j = 0;
            }
            phase_x[i][j] = temp_new;
            phase_y[i][j] = 4 * Double.parseDouble(st.nextToken());
            kd[i][j] = Double.parseDouble(st.nextToken());
            kfs[i][j] = Double.parseDouble(st.nextToken());
            kfl[i][j] = Double.parseDouble(st.nextToken());
            kdee[i][j] = Double.parseDouble(st.nextToken());
            kdc[i][j] = Double.parseDouble(st.nextToken());
            kfc[i][j] = Double.parseDouble(st.nextToken());
            imax = i;
            jmax = j;
            j++;
            k++;
        }
        xgridmax = phase_x[i][j - 1];
        ygridmax = phase_y[i][j - 1];
    }


    @TestTargets({ @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies that the ObjectInputStream constructor calls checkPermission on security manager.", method = "ObjectInputStream", args = { InputStream.class }) })
    public void test_ObjectInputStream2() throws IOException {
        class TestSecurityManager extends SecurityManager {

            boolean called;

            Permission permission;

            void reset() {
                called = false;
                permission = null;
            }

            @Override
            public void checkPermission(Permission permission) {
                if (permission instanceof SerializablePermission) {
                    called = true;
                    this.permission = permission;
                }
            }
        }
        class TestObjectInputStream extends ObjectInputStream {

            TestObjectInputStream(InputStream s) throws StreamCorruptedException, IOException {
                super(s);
            }
        }
        class TestObjectInputStream_readFields extends ObjectInputStream {

            TestObjectInputStream_readFields(InputStream s) throws StreamCorruptedException, IOException {
                super(s);
            }

            @Override
            public GetField readFields() throws IOException, ClassNotFoundException, NotActiveException {
                return super.readFields();
            }
        }
        class TestObjectInputStream_readUnshared extends ObjectInputStream {

            TestObjectInputStream_readUnshared(InputStream s) throws StreamCorruptedException, IOException {
                super(s);
            }

            @Override
            public Object readUnshared() throws IOException, ClassNotFoundException {
                return super.readUnshared();
            }
        }
        long id = new java.util.Date().getTime();
        String filename = "SecurityPermissionsTest_" + id;
        File f = File.createTempFile(filename, null);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
        oos.writeObject(new Node());
        oos.flush();
        oos.close();
        f.deleteOnExit();
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        s.reset();
        new ObjectInputStream(new FileInputStream(f));
        assertTrue("ObjectInputStream(InputStream) ctor must not call checkPermission on security manager on a class which neither overwrites methods readFields nor readUnshared", !s.called);
        s.reset();
        new TestObjectInputStream(new FileInputStream(f));
        assertTrue("ObjectInputStream(InputStream) ctor must not call checkPermission on security manager on a class which neither overwrites methods readFields nor readUnshared", !s.called);
        s.reset();
        new TestObjectInputStream_readFields(new FileInputStream(f));
        assertTrue("ObjectInputStream(InputStream) ctor must call checkPermission on security manager on a class which overwrites method readFields", s.called);
        assertEquals("Name of SerializablePermission is not correct", "enableSubclassImplementation", s.permission.getName());
        s.reset();
        new TestObjectInputStream_readUnshared(new FileInputStream(f));
        assertTrue("ObjectInputStream(InputStream) ctor must call checkPermission on security manager on a class which overwrites method readUnshared", s.called);
        assertEquals("Name of SerializablePermission is not correct", "enableSubclassImplementation", s.permission.getName());
    }


    public static JuneClass loadClass(Map<String, Entity> globals, String packageName, String baseClassName) {
        try {
            JuneClass $class = null;
            String resourceName = (packageName.length() > 0 ? packageName.replace('.', '/') + "/" : "") + baseClassName.replace('.', '$') + ".class";
            URL url = Resolver.class.getClassLoader().getResource(resourceName);
            if (url != null) {
                ClassBuilder builder = new ClassBuilder(globals);
                InputStream stream = url.openStream();
                try {
                    new ClassReader(new BufferedInputStream(stream)).accept(builder, ClassReader.SKIP_CODE);
                } finally {
                    stream.close();
                }
                $class = builder.$class;
                $class.loaded = true;
            }
            return $class;
        } catch (Exception e) {
            throw Helper.throwAny(e);
        }
    }


    public static void copyFileNIO(String src, String dst) {
        try {
            FileChannel srcChannel = new FileInputStream(src).getChannel();
            FileChannel dstChannel = new FileOutputStream(dst).getChannel();
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
            srcChannel.close();
            dstChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void config() throws IOException {
        Reader reader = new FileReader(new File("src/test/resources/test.yml"));
        Writer writer = new FileWriter(new File("src/site/apt/config.apt"));
        writer.write("------\n");
        writer.write(FileUtils.readFully(reader));
        writer.flush();
        writer.close();
    }


    public static String scramble(String text) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(text.getBytes("UTF-8"));
            StringBuffer sb = new StringBuffer();
            for (byte b : md.digest()) sb.append(Integer.toString(b & 0xFF, 16));
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            return null;
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }


    private static FTPClient getFtpClient(String ftpHost, String ftpUsername, String ftpPassword) throws SocketException, IOException {
        FTPClient ftp = new FTPClient();
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        ftp.connect(ftpHost);
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return null;
        }
        if (!ftp.login(ftpUsername, ftpPassword)) {
            return null;
        }
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        return ftp;
    }


    private synchronized void renewToken() {
        if (!(System.currentTimeMillis() > (lastTokenRenewal + 10000))) return;
        lastTokenRenewal = System.currentTimeMillis();
        String token = null;
        System.out.println("loading error - refresh token");
        byte[] buff = null;
        try {
            BufferedInputStream bis = null;
            System.out.println("Calling timeout : " + getServingURL() + "?token_timeout=true");
            URL remoteurl = new URL(getServingURL() + "?token_timeout=true");
            URLConnection connection = remoteurl.openConnection();
            connection.setRequestProperty("Referer", getServingURL());
            int length = connection.getContentLength();
            InputStream in = connection.getInputStream();
            buff = new byte[length];
            int bytesRead = 0;
            while (bytesRead < length) {
                bytesRead += in.read(buff, bytesRead, in.available());
            }
            token = new String(buff);
        } catch (Exception e) {
        }
        if (token != null && !token.equals("")) {
            token = token.trim();
            this.info.setToken(token);
        } else {
            System.out.println("Token returned was null");
        }
    }


    private void getRandomGUID(boolean secure, Object o) {
        MessageDigest md5 = null;
        StringBuffer sbValueBeforeMD5 = new StringBuffer();
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Error: " + e);
        }
        try {
            long time = System.currentTimeMillis();
            long rand = 0;
            if (secure) {
                rand = mySecureRand.nextLong();
            } else {
                rand = myRand.nextLong();
            }
            sbValueBeforeMD5.append(o.getClass().getName());
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(s_id);
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(Long.toString(time));
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(Long.toString(rand));
            valueBeforeMD5 = sbValueBeforeMD5.toString();
            md5.update(valueBeforeMD5.getBytes());
            byte[] array = md5.digest();
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < array.length; ++j) {
                int b = array[j] & 0xFF;
                if (b < 0x10) sb.append('0');
                sb.append(Integer.toHexString(b));
            }
            valueAfterMD5 = sb.toString();
        } catch (Exception e) {
            System.out.println("Error:" + e);
        }
    }


    public static long copy(File src, long amount, File dst) {
        final int BUFFER_SIZE = 1024;
        long amountToRead = amount;
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(dst));
            byte[] buf = new byte[BUFFER_SIZE];
            while (amountToRead > 0) {
                int read = in.read(buf, 0, (int) Math.min(BUFFER_SIZE, amountToRead));
                if (read == -1) break;
                amountToRead -= read;
                out.write(buf, 0, read);
            }
        } catch (IOException e) {
        } finally {
            close(in);
            flush(out);
            close(out);
        }
        return amount - amountToRead;
    }


    private void copyImage(ProjectElement e) throws Exception {
        String fn = e.getName();
        if (!fn.toLowerCase().endsWith(".png")) {
            if (fn.contains(".")) {
                fn = fn.substring(0, fn.lastIndexOf('.')) + ".png";
            } else {
                fn += ".png";
            }
        }
        File img = new File(resFolder, fn);
        File imgz = new File(resoutFolder.getAbsolutePath(), fn + ".zlib");
        boolean copy = true;
        if (img.exists() && config.containsKey(img.getName())) {
            long modified = Long.parseLong(config.get(img.getName()));
            if (modified >= img.lastModified()) {
                copy = false;
            }
        }
        if (copy) {
            convertImage(e.getFile(), img);
            config.put(img.getName(), String.valueOf(img.lastModified()));
        }
        DeflaterOutputStream out = new DeflaterOutputStream(new BufferedOutputStream(new FileOutputStream(imgz)));
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(img));
        int read;
        while ((read = in.read()) != -1) {
            out.write(read);
        }
        out.close();
        in.close();
        imageFiles.add(imgz);
        imageNames.put(imgz, e.getName());
    }


    private void createImageArchive() throws Exception {
        imageArchive = new File(resoutFolder, "images.CrAr");
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(imageArchive)));
        out.writeInt(toNativeEndian(imageFiles.size()));
        for (int i = 0; i < imageFiles.size(); i++) {
            File f = imageFiles.get(i);
            out.writeLong(toNativeEndian(f.length()));
            out.writeLong(toNativeEndian(new File(resFolder, f.getName().substring(0, f.getName().length() - 5)).length()));
        }
        for (int i = 0; i < imageFiles.size(); i++) {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(imageFiles.get(i)));
            int read;
            while ((read = in.read()) != -1) {
                out.write(read);
            }
            in.close();
        }
        out.close();
    }


private void appendArchive() throws Exception {
        String cmd;
        if (profile == CompilationProfile.UNIX_GCC) {
            cmd = "cat";
        } else if (profile == CompilationProfile.MINGW_WINDOWS) {
            cmd = "type";
        } else {
            throw new Exception("Unknown cat equivalent for profile " + profile);
        }
        compFrame.writeLine("" + cmd + " \"" + imageArchive.getAbsolutePath() + "\" >> \"" + outputFile.getAbsolutePath() + "\"");
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile, true));
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(imageArchive));
        int read;
        while ((read = in.read()) != -1) {
            out.write(read);
        }
        in.close();
        out.close();
    }


    private static void copy(File source, File dest) throws FileNotFoundException, IOException {
        FileInputStream input = new FileInputStream(source);
        FileOutputStream output = new FileOutputStream(dest);
        System.out.println("Copying " + source + " to " + dest);
        IOUtils.copy(input, output);
        output.close();
        input.close();
        dest.setLastModified(source.lastModified());
    }


    private BundleURLClassPath createBundleURLClassPath(Bundle bundle, Version version, File bundleFile, File cache, boolean alreadyCached) throws Exception {
        String bundleClassPath = (String) bundle.getHeaders().get(Constants.BUNDLE_CLASSPATH);
        if (bundleClassPath == null) {
            bundleClassPath = ".";
        }
        ManifestEntry[] entries = ManifestEntry.parse(bundleClassPath);
        String[] classPaths = new String[0];
        for (int i = 0; i < entries.length; i++) {
            String classPath = entries[i].getName();
            if (classPath.startsWith("/")) {
                classPath = classPath.substring(1);
            }
            if (classPath.endsWith(".jar")) {
                try {
                    File file = new File(cache, classPath);
                    if (!alreadyCached) {
                        file.getParentFile().mkdirs();
                        String url = new StringBuilder("jar:").append(bundleFile.toURI().toURL().toString()).append("!/").append(classPath).toString();
                        OutputStream os = new FileOutputStream(file);
                        InputStream is = new URL(url).openStream();
                        IOUtil.copy(is, os);
                        is.close();
                        os.close();
                    } else {
                        if (!file.exists()) {
                            throw new IOException(new StringBuilder("classpath ").append(classPath).append(" not found").toString());
                        }
                    }
                } catch (IOException e) {
                    FrameworkEvent frameworkEvent = new FrameworkEvent(FrameworkEvent.INFO, bundle, e);
                    framework.postFrameworkEvent(frameworkEvent);
                    continue;
                }
            }
            classPaths = (String[]) ArrayUtil.add(classPaths, classPath);
        }
        if (!alreadyCached) {
            String bundleNativeCode = (String) bundle.getHeaders().get(Constants.BUNDLE_NATIVECODE);
            if (bundleNativeCode != null) {
                entries = ManifestEntry.parse(bundleNativeCode);
                for (int i = 0; i < entries.length; i++) {
                    ManifestEntry entry = entries[i];
                    String libPath = entry.getName();
                    String url = new StringBuilder("jar:").append(bundleFile.toURI().toURL().toString()).append("!/").append(libPath).toString();
                    File file = new File(cache, libPath);
                    file.getParentFile().mkdirs();
                    OutputStream os = new FileOutputStream(file);
                    InputStream is = new URL(url).openStream();
                    IOUtil.copy(is, os);
                    is.close();
                    os.close();
                }
            }
        }
        BundleURLClassPath urlClassPath = new BundleURLClassPathImpl(bundle, version, classPaths, cache);
        return urlClassPath;
    }


    Bundle install(String location, InputStream is) throws BundleException {
        synchronized (bundlesLock) {
            SecurityManager securityManager = System.getSecurityManager();
            if (securityManager != null) {
                securityManager.checkPermission(new AdminPermission(new StringBuilder("(location=").append(location).append(")").toString(), org.osgi.framework.AdminPermission.EXTENSIONLIFECYCLE));
            }
            long bundleId = getNextBundleId();
            AbstractBundle bundle = null;
            try {
                if (is == null) {
                    URL url = new URL(location);
                    is = url.openStream();
                }
                File temp = new File(getTempFolder(), Long.toString(System.currentTimeMillis()));
                OutputStream os;
                os = new FileOutputStream(temp);
                IOUtil.copy(is, os);
                os.close();
                is.close();
                Manifest manifest = ManifestUtil.getJarManifest(new FileInputStream(temp));
                Dictionary headers = ManifestUtil.toDictionary(manifest);
                Version version = Version.parseVersion((String) headers.get(Constants.BUNDLE_VERSION));
                File cache = createNewCache(bundleId, version);
                File manifestFile = new File(cache, BUNDLE_MANIFEST_FILE);
                os = new FileOutputStream(manifestFile);
                ManifestUtil.storeManifest(headers, os);
                os.close();
                if (isBundleInstalled((String) headers.get(Constants.BUNDLE_SYMBOLICNAME))) {
                    throw new BundleException(new StringBuilder("Bundle(location=").append(location).append(") already installed.").toString());
                }
                ManifestEntry[] entries = ManifestEntry.parse(headers.get(Constants.FRAGMENT_HOST));
                if (entries != null) {
                    if (entries[0].hasAttribute("extension")) {
                        String extension = entries[0].getAttributeValue("extension");
                        if (extension.equals("bootclasspath")) {
                            String symbolicName = entries[0].getName();
                            if (!symbolicName.equals(framework.getSymbolicName()) && !symbolicName.equals(Constants.SYSTEM_BUNDLE_SYMBOLICNAME)) {
                                throw new BundleException(new StringBuilder("Trying to install a fragment Bundle(location=").append(location).append(") with extension 'bootclasspath' but host is not System Bundle.").toString(), new UnsupportedOperationException());
                            }
                        }
                    }
                }
                String requiredEE = (String) headers.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
                if (requiredEE != null) {
                    BundleContext context = framework.getBundleContext();
                    String ee = context.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT);
                    if (!ee.contains(requiredEE)) {
                        throw new BundleException(new StringBuilder("Bundle(location=").append(location).append(")  requires an unsopperted execution environment (=").append(requiredEE).append(").").toString());
                    }
                }
                if (FrameworkUtil.isFragmentHost(headers)) {
                    bundle = new FragmentBundle(framework);
                } else {
                    bundle = new HostBundle(framework);
                }
                File bundlefile = new File(cache, Storage.BUNDLE_FILE);
                temp.renameTo(bundlefile);
                long lastModified = bundlefile.lastModified();
                BundleInfo info = new BundleInfo(bundleId, location, lastModified, framework.getInitialBundleStartLevel());
                info.setHeaders(headers);
                info.setCache(cache);
                storeBundleInfo(info);
                bundleInfosByBundle.put(bundle, info);
                BundleURLClassPath classPath = createBundleURLClassPath(bundle, version, bundlefile, cache, false);
                classPathsByBundle.put(bundle, new BundleURLClassPath[] { classPath });
                synchronized (bundlesLock) {
                    bundles = (Bundle[]) ArrayUtil.add(bundles, bundle);
                }
                return bundle;
            } catch (Exception e) {
                if (bundle != null) {
                    File bundleFolder = getBundleFolder(bundleId);
                    try {
                        IOUtil.delete(bundleFolder);
                    } catch (IOException e1) {
                    }
                }
                e.printStackTrace();
                throw new BundleException(e.getMessage(), e);
            }
        }
    }


    @Override
    public Cal3dModel loadModel(URL url, String skin) throws IOException, IncorrectFormatException, ParsingErrorException {
        boolean baseURLWasNull = setBaseURLFromModelURL(url);
        Cal3dModel model = new Cal3dModel(getFlags());
        loadCal3dModel(getBaseURL(), url.toExternalForm(), new InputStreamReader(url.openStream()), model);
        if (baseURLWasNull) {
            popBaseURL();
        }
        return (model);
    }


    protected Document loadDocument() throws MalformedURLException, DocumentException, IOException {
        if (jiraFilterURL.startsWith("file")) {
            URL url = getSourceURL();
            return parseDocument(url);
        } else {
            HttpClient httpClient = new DefaultHttpClient();
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            formparams.add(new BasicNameValuePair("os_username", jiraUser));
            formparams.add(new BasicNameValuePair("os_password", jiraPassword));
            formparams.add(new BasicNameValuePair("os_cookie", "true"));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
            HttpPost post = new HttpPost(getJiraRootUrl() + "/secure/login.jsp");
            post.setEntity(entity);
            HttpResponse response = httpClient.execute(post);
            response.getEntity().consumeContent();
            String url_str = StringEscapeUtils.unescapeXml(jiraFilterURL);
            HttpGet get = new HttpGet(url_str);
            response = httpClient.execute(get);
            return parseDocument(response.getEntity().getContent());
        }
    }


    private void downloadFile(String url, File destFile) {
        try {
            System.out.println("Downloading " + url + " to " + destFile + "...");
            destFile.getParentFile().mkdirs();
            OutputStream out = new FileOutputStream(destFile);
            URLConnection conn = new URL(url).openConnection();
            InputStream in = conn.getInputStream();
            int totalSize = conn.getContentLength(), downloadedSize = 0, size;
            byte[] buffer = new byte[32768];
            ProgressMonitor pm = new ProgressMonitor(null, "Downloading " + url, "", 0, totalSize);
            pm.setMillisToDecideToPopup(100);
            pm.setMillisToPopup(500);
            boolean canceled = false;
            while ((size = in.read(buffer)) > 0 && !(canceled = pm.isCanceled())) {
                out.write(buffer, 0, size);
                pm.setProgress(downloadedSize += size);
                pm.setNote((100 * downloadedSize / totalSize) + "% finished");
            }
            in.close();
            out.close();
            if (canceled) {
                destFile.delete();
                fatalError("Starting canceled", "Downloading canceled. Exiting...");
            }
            pm.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            destFile.delete();
            fatalError("Download Error", "Couldn't download file " + url + ": " + ex);
        }
    }


    public boolean validateZipFile(File zipFile) {
        String tempdir = Config.CONTEXT.getRealPath(getBackupTempFilePath());
        try {
            deleteTempFiles();
            File ftempDir = new File(tempdir);
            ftempDir.mkdirs();
            File tempZip = new File(tempdir + File.separator + zipFile.getName());
            tempZip.createNewFile();
            FileChannel ic = new FileInputStream(zipFile).getChannel();
            FileChannel oc = new FileOutputStream(tempZip).getChannel();
            for (long i = 0; i <= ic.size(); i++) {
                ic.transferTo(0, 1000000, oc);
                i = i + 999999;
            }
            ic.close();
            oc.close();
            if (zipFile != null && zipFile.getName().toLowerCase().endsWith(".zip")) {
                ZipFile z = new ZipFile(zipFile);
                ZipUtil.extract(z, new File(Config.CONTEXT.getRealPath(backupTempFilePath)));
            }
            return true;
        } catch (Exception e) {
            Logger.error(this, "Error with file", e);
            return false;
        }
    }


    public String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }


    private Reader getReader() throws IOException {
        if (data != null) {
            if (url != null) throw new IllegalArgumentException("URL for source data and the data itself must never be specified together.");
            if (charset != null) throw new IllegalArgumentException("Charset has sense only for URL-based data");
            return new StringReader(data);
        } else if (url != null) {
            InputStream stream = url.openStream();
            if (charset == null) return new InputStreamReader(stream); else return new InputStreamReader(stream, charset);
        }
        return null;
    }


    public void open(String server, String user, String pass, int port, String option) throws Exception {
        log.info("Login to FTP: " + server);
        this.port = port;
        ftp = new FTPClient();
        ftp.connect(server, port);
        ftp.login(user, pass);
        checkReply("FTP server refused connection." + server);
        modeBINARY();
        this.me = this;
    }


    public void chmod(String remoteFile, String mode) {
        String remotePath = connector.getRemoteDirectory();
        remotePath += PATH_SEPARATOR + remoteFile;
        FTPClient ftp = new FTPClient();
        try {
            String hostname = connector.getUrl().getHost();
            ftp.connect(hostname);
            log.info("Connected to " + hostname);
            log.info(ftp.getReplyString());
            boolean loggedIn = ftp.login(connector.getUsername(), connector.getPassword());
            if (loggedIn) {
                String parameters = "chmod " + mode + " " + remotePath;
                ftp.site(parameters);
                ftp.logout();
            }
            ftp.disconnect();
        } catch (SocketException e) {
            log.error("File chmod failed with message: " + e.getMessage());
        } catch (IOException e) {
            log.error("File chmod failed with message: " + e.getMessage());
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
    }


    public void delete(String remoteFile) {
        String remotePath = connector.getRemoteDirectory();
        remotePath += PATH_SEPARATOR + remoteFile;
        FTPClient ftp = new FTPClient();
        try {
            String hostname = connector.getUrl().getHost();
            ftp.connect(hostname);
            log.info("Connected to " + hostname);
            log.info(ftp.getReplyString());
            boolean loggedIn = ftp.login(connector.getUsername(), connector.getPassword());
            if (loggedIn) {
                ftp.deleteFile(remotePath);
                ftp.logout();
            }
            ftp.disconnect();
        } catch (SocketException e) {
            log.error("File chmod failed with message: " + e.getMessage());
        } catch (IOException e) {
            log.error("File chmod failed with message: " + e.getMessage());
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
    }


    public Map<String, String> getMetadata(WebFileObject fo) throws IOException {
        if (!fo.wfs.getRootURL().getProtocol().equals("ftp")) {
            String realName = fo.pathname;
            boolean exists;
            URL ur = new URL(fo.wfs.getRootURL(), realName);
            HttpURLConnection connect = (HttpURLConnection) ur.openConnection();
            connect.setRequestMethod("HEAD");
            HttpURLConnection.setFollowRedirects(false);
            connect.connect();
            HttpURLConnection.setFollowRedirects(true);
            if (connect.getResponseCode() == 303) {
                String surl = connect.getHeaderField("Location");
                if (surl.startsWith(fo.wfs.root.toString())) {
                    realName = surl.substring(fo.wfs.root.toString().length());
                }
                connect.disconnect();
                ur = new URL(fo.wfs.getRootURL(), realName);
                connect = (HttpURLConnection) ur.openConnection();
                connect.setRequestMethod("HEAD");
                connect.connect();
            }
            exists = connect.getResponseCode() != 404;
            Map<String, String> result = new HashMap<String, String>();
            Map<String, List<String>> fields = connect.getHeaderFields();
            for (Entry<String, List<String>> e : fields.entrySet()) {
                String key = e.getKey();
                List<String> value = e.getValue();
                result.put(key, value.get(0));
            }
            result.put(META_EXIST, String.valueOf(exists));
            connect.disconnect();
            return result;
        } else {
            Map<String, String> result = new HashMap<String, String>();
            URL url = new URL(fo.wfs.getRootURL(), fo.pathname);
            URLConnection urlc = url.openConnection();
            try {
                urlc.connect();
                urlc.getInputStream().close();
                result.put(META_EXIST, "true");
            } catch (IOException ex) {
                result.put(META_EXIST, "false");
            }
            return result;
        }
    }


    private void copy(File source, File destinationDirectory) throws IOException {
        if (source.isDirectory()) {
            File newDir = new File(destinationDirectory, source.getName());
            newDir.mkdir();
            File[] children = source.listFiles();
            for (int i = 0; i < children.length; i++) {
                if (children[i].getName().equals(".svn")) {
                    continue;
                }
                copy(children[i], newDir);
            }
        } else {
            File newFile = new File(destinationDirectory, source.getName());
            if (newFile.exists() && source.lastModified() == newFile.lastModified()) {
                return;
            }
            FileOutputStream output = new FileOutputStream(newFile);
            FileInputStream input = new FileInputStream(source);
            byte[] buff = new byte[2048];
            int read = 0;
            while ((read = input.read(buff)) > 0) {
                output.write(buff, 0, read);
            }
            output.flush();
            output.close();
            input.close();
        }
    }


    public synchronized String encrypt(String plaintext) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
            md.update(plaintext.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            log().error("failed to encrypt the password.", e);
            throw new RuntimeException("failed to encrypt the password.", e);
        } catch (UnsupportedEncodingException e) {
            log().error("failed to encrypt the password.", e);
            throw new RuntimeException("failed to encrypt the password.", e);
        }
        byte raw[] = md.digest();
        String hash = (new BASE64Encoder()).encode(raw);
        return hash;
    }


    @Override
    public void handlePeerEvent(PeerEvent event) {
        if (event.geteventInfo() instanceof EventServiceInfo) {
            EventServiceInfo info = (EventServiceInfo) event.geteventInfo();
            if (info.getServiceState() != ServiceState.Deployed) return;
            long bid = info.getBundleId();
            Bundle bundle = context.getBundle(bid);
            Enumeration entries = bundle.findEntries("OSGI-INF/PrivacyPolicy/", "*.xml", true);
            if (entries != null) {
                if (entries.hasMoreElements()) {
                    try {
                        URL url = (URL) entries.nextElement();
                        BufferedInputStream in = new BufferedInputStream(url.openStream());
                        XMLPolicyReader reader = new XMLPolicyReader(this.context);
                        RequestPolicy policy = reader.readPolicyFromFile(in);
                        if (policy != null) {
                            this.policyMgr.addPrivacyPolicyForService(info.getServiceID(), policy);
                        }
                    } catch (IOException ioe) {
                    }
                }
            }
        }
    }


    public void parse() {
        InputSource urlInputStream = null;
        SAXParserFactory spf = null;
        SAXParser sp = null;
        HttpURLConnection conn = null;
        try {
            URL url = new URL(this.urlString);
            _setProxy();
            conn = (HttpURLConnection) url.openConnection();
            urlInputStream = new InputSource(StreamUtils.inputStreamToReader(conn.getInputStream()));
            spf = SAXParserFactory.newInstance();
            if (spf != null) {
                sp = spf.newSAXParser();
                sp.parse(urlInputStream, this);
            }
        } catch (Exception e) {
            if (conn != null) {
                if (conn.getHeaderField("X-RateLimit-Limit") != null) {
                    String rateLimit = conn.getHeaderField("X-RateLimit-Limit");
                    String rateRemaining = conn.getHeaderField("X-RateLimit-Remaining");
                    long rateReset = Long.valueOf(conn.getHeaderField("X-RateLimit-Reset")) * 1000;
                    LOG.warn("Possible rate limits?  LIMIT:" + rateLimit + "  REMAINING:" + rateRemaining + "  RESET:" + new Date(rateReset));
                }
            }
            e.printStackTrace();
            LOG.warn("error parsing rss feed", e);
        } finally {
        }
    }


    public static Document getXHTMLDocument(URL _url) throws IOException {
        final Tidy tidy = new Tidy();
        tidy.setQuiet(true);
        tidy.setShowWarnings(false);
        tidy.setXmlOut(true);
        final BufferedInputStream input_stream = new BufferedInputStream(_url.openStream());
        return tidy.parseDOM(input_stream, null);
    }


    public void handleRequestInternal(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
        final String servetPath = httpServletRequest.getServletPath();
        final String previousToken = httpServletRequest.getHeader(IF_NONE_MATCH);
        final String currentToken = getETagValue(httpServletRequest);
        httpServletResponse.setHeader(ETAG, currentToken);
        final Date modifiedDate = new Date(httpServletRequest.getDateHeader(IF_MODIFIED_SINCE));
        final Calendar calendar = Calendar.getInstance();
        final Date now = calendar.getTime();
        calendar.setTime(modifiedDate);
        calendar.add(Calendar.MINUTE, getEtagExpiration());
        if (currentToken.equals(previousToken) && (now.getTime() < calendar.getTime().getTime())) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_MODIFIED);
            httpServletResponse.setHeader(LAST_MODIFIED, httpServletRequest.getHeader(IF_MODIFIED_SINCE));
            if (LOG.isDebugEnabled()) {
                LOG.debug("ETag the same, will return 304");
            }
        } else {
            httpServletResponse.setDateHeader(LAST_MODIFIED, (new Date()).getTime());
            final String width = httpServletRequest.getParameter(Constants.WIDTH);
            final String height = httpServletRequest.getParameter(Constants.HEIGHT);
            final ImageNameStrategy imageNameStrategy = imageService.getImageNameStrategy(servetPath);
            String code = imageNameStrategy.getCode(servetPath);
            String fileName = imageNameStrategy.getFileName(servetPath);
            final String imageRealPathPrefix = getRealPathPrefix(httpServletRequest.getServerName().toLowerCase());
            String original = imageRealPathPrefix + imageNameStrategy.getFullFileNamePath(fileName, code);
            final File origFile = new File(original);
            if (!origFile.exists()) {
                code = Constants.NO_IMAGE;
                fileName = imageNameStrategy.getFileName(code);
                original = imageRealPathPrefix + imageNameStrategy.getFullFileNamePath(fileName, code);
            }
            String resizedImageFileName = null;
            if (width != null && height != null && imageService.isSizeAllowed(width, height)) {
                resizedImageFileName = imageRealPathPrefix + imageNameStrategy.getFullFileNamePath(fileName, code, width, height);
            }
            final File imageFile = getImageFile(original, resizedImageFileName, width, height);
            final FileInputStream fileInputStream = new FileInputStream(imageFile);
            IOUtils.copy(fileInputStream, httpServletResponse.getOutputStream());
            fileInputStream.close();
        }
    }


    public void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) {
                InputStream inStream = new FileInputStream(oldPath);
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
        }
    }


    public ValidationReport validate(OriginalDeployUnitDescription unit) throws UnitValidationException {
        ValidationReport vr = new DefaultValidationReport();
        errorHandler = new SimpleErrorHandler(vr);
        vr.setFileUri(unit.getAbsolutePath());
        SAXParser parser;
        SAXReader reader = null;
        try {
            parser = factory.newSAXParser();
            reader = new SAXReader(parser.getXMLReader());
            reader.setValidation(false);
            reader.setErrorHandler(this.errorHandler);
        } catch (ParserConfigurationException e) {
            throw new UnitValidationException("The configuration of parser is illegal.", e);
        } catch (SAXException e) {
            String m = "Something is wrong when register schema";
            logger.error(m, e);
            throw new UnitValidationException(m, e);
        }
        ZipInputStream zipInputStream;
        InputStream tempInput = null;
        try {
            tempInput = new FileInputStream(unit.getAbsolutePath());
        } catch (FileNotFoundException e1) {
            String m = String.format("The file [%s] don't exist.", unit.getAbsolutePath());
            logger.error(m, e1);
            throw new UnitValidationException(m, e1);
        }
        zipInputStream = new ZipInputStream(tempInput);
        ZipEntry zipEntry = null;
        try {
            zipEntry = zipInputStream.getNextEntry();
            if (zipEntry == null) {
                String m = String.format("Error when get zipEntry. Maybe the [%s] is not zip file!", unit.getAbsolutePath());
                logger.error(m);
                throw new UnitValidationException(m);
            }
            while (zipEntry != null) {
                if (configFiles.contains(zipEntry.getName())) {
                    byte[] extra = new byte[(int) zipEntry.getSize()];
                    zipInputStream.read(extra);
                    File file = File.createTempFile("temp", "extra");
                    file.deleteOnExit();
                    logger.info("[TempFile:]" + file.getAbsoluteFile());
                    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(extra);
                    FileOutputStream tempFileOutputStream = new FileOutputStream(file);
                    IOUtils.copy(byteInputStream, tempFileOutputStream);
                    tempFileOutputStream.flush();
                    IOUtils.closeQuietly(tempFileOutputStream);
                    InputStream inputStream = new FileInputStream(file);
                    reader.read(inputStream, unit.getAbsolutePath() + ":" + zipEntry.getName());
                    IOUtils.closeQuietly(inputStream);
                }
                zipEntry = zipInputStream.getNextEntry();
            }
        } catch (IOException e) {
            ValidationMessage vm = new XMLValidationMessage("IOError", 0, 0, unit.getUrl() + ":" + zipEntry.getName(), e);
            vr.addValidationMessage(vm);
        } catch (DocumentException e) {
            ValidationMessage vm = new XMLValidationMessage("Document Error.", 0, 0, unit.getUrl() + ":" + zipEntry.getName(), e);
            vr.addValidationMessage(vm);
        } finally {
            IOUtils.closeQuietly(tempInput);
            IOUtils.closeQuietly(zipInputStream);
        }
        return vr;
    }


    private void copyFile(File sourcefile, File targetfile) {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(sourcefile));
            out = new BufferedOutputStream(new FileOutputStream(targetfile));
            byte[] buffer = new byte[4096];
            int bytesread = 0;
            while ((bytesread = in.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private void bubbleSort(int values[]) {
        PerfMonTimer timerOuter = PerfMonTimer.start("SortingTest.bubbleSort");
        try {
            int len = values.length - 1;
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len - i; j++) {
                    if (values[j] > values[j + 1]) {
                        int tmp = values[j];
                        values[j] = values[j + 1];
                        values[j + 1] = tmp;
                    }
                }
            }
        } finally {
            PerfMonTimer.stop(timerOuter);
        }
    }


    public static SearchItem register(String... args) {
        SearchItem _return = new SearchItem();
        String line = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(URL_REGISTER);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
            nameValuePairs.add(new BasicNameValuePair("format", "xml"));
            nameValuePairs.add(new BasicNameValuePair("firtname", args[0]));
            nameValuePairs.add(new BasicNameValuePair("lastname", args[1]));
            nameValuePairs.add(new BasicNameValuePair("email", args[2]));
            nameValuePairs.add(new BasicNameValuePair("phone", args[3]));
            nameValuePairs.add(new BasicNameValuePair("password", args[4]));
            nameValuePairs.add(new BasicNameValuePair("confirmpassword", args[5]));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);
            line = EntityUtils.toString(response.getEntity());
            Document document = XMLfunctions.XMLfromString(line);
            NodeList nodes = document.getElementsByTagName("response");
            Element e = (Element) nodes.item(0);
            _return.set(0, XMLfunctions.getValue(e, "success"));
            if ("false".endsWith(_return.get(0))) {
                _return.set(1, XMLfunctions.getValue(e, "error"));
            } else {
                _return.set(1, XMLfunctions.getValue(e, "message"));
            }
            return _return;
        } catch (Exception e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            line = null;
            _return.set(0, "false");
            _return.set(1, "");
        }
        return _return;
    }


    public static SearchItem loadRecord(String id, boolean isContact) {
        String line = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(isContact ? URL_RECORD_CONTACT : URL_RECORD_COMPANY);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
            nameValuePairs.add(new BasicNameValuePair("format", "xml"));
            nameValuePairs.add(new BasicNameValuePair("token", Common.token));
            nameValuePairs.add(new BasicNameValuePair("id", id));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);
            line = EntityUtils.toString(response.getEntity());
            Document document = XMLfunctions.XMLfromString(line);
            NodeList nodes = document.getElementsByTagName("response");
            Element e = (Element) nodes.item(0);
            String Name__Last__First_ = XMLfunctions.getValue(e, isContact ? "Name__Last__First_" : "Name");
            String phone = "";
            if (!isContact) phone = XMLfunctions.getValue(e, "Phone");
            String Email1 = XMLfunctions.getValue(e, isContact ? "Personal_Email" : "Email");
            String Home_Fax = XMLfunctions.getValue(e, isContact ? "Home_Fax" : "Fax1");
            String Address1 = XMLfunctions.getValue(e, "Address1");
            String Address2 = XMLfunctions.getValue(e, "Address2");
            String City = XMLfunctions.getValue(e, "City");
            String State = XMLfunctions.getValue(e, "State");
            String Zip = XMLfunctions.getValue(e, "Zip");
            String Country = XMLfunctions.getValue(e, "Country");
            String Profile = XMLfunctions.getValue(e, "Profile");
            String success = XMLfunctions.getValue(e, "success");
            String error = XMLfunctions.getValue(e, "error");
            SearchItem item = new SearchItem();
            item.set(1, Name__Last__First_);
            item.set(2, phone);
            item.set(3, phone);
            item.set(4, Email1);
            item.set(5, Home_Fax);
            item.set(6, Address1);
            item.set(7, Address2);
            item.set(8, City);
            item.set(9, State);
            item.set(10, Zip);
            item.set(11, Profile);
            item.set(12, Country);
            item.set(13, success);
            item.set(14, error);
            return item;
        } catch (Exception e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            line = null;
        }
        return null;
    }


    public static RecordResponse loadRecord(RecordRequest recordRequest) {
        RecordResponse recordResponse = new RecordResponse();
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(recordRequest.isContact() ? URL_RECORD_CONTACT : URL_RECORD_COMPANY);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
            nameValuePairs.add(new BasicNameValuePair("format", "xml"));
            nameValuePairs.add(new BasicNameValuePair("token", recordRequest.getToken()));
            nameValuePairs.add(new BasicNameValuePair("id", recordRequest.getId()));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);
            String line = EntityUtils.toString(response.getEntity());
            Document document = XMLfunctions.XMLfromString(line);
            NodeList nodes = document.getElementsByTagName("response");
            Element e = (Element) nodes.item(0);
            String Name__Last__First_ = XMLfunctions.getValue(e, recordRequest.isContact() ? "Name__Last__First_" : "Name");
            String phone = "";
            if (!recordRequest.isContact()) phone = XMLfunctions.getValue(e, "Phone");
            String Email1 = XMLfunctions.getValue(e, recordRequest.isContact() ? "Personal_Email" : "Email");
            String Home_Fax = XMLfunctions.getValue(e, recordRequest.isContact() ? "Home_Fax" : "Fax1");
            String Address1 = XMLfunctions.getValue(e, "Address1");
            String Address2 = XMLfunctions.getValue(e, "Address2");
            String City = XMLfunctions.getValue(e, "City");
            String State = XMLfunctions.getValue(e, "State");
            String Zip = XMLfunctions.getValue(e, "Zip");
            String Country = XMLfunctions.getValue(e, "Country");
            String Profile = XMLfunctions.getValue(e, "Profile");
            String success = XMLfunctions.getValue(e, "success");
            String error = XMLfunctions.getValue(e, "error");
            recordResponse.setName(Name__Last__First_);
            recordResponse.setPhone(phone);
            recordResponse.setEmail(Email1);
            recordResponse.setHomeFax(Home_Fax);
            recordResponse.setAddress1(Address1);
            recordResponse.setAddress2(Address2);
            recordResponse.setCity(City);
            recordResponse.setState(State);
            recordResponse.setZip(Zip);
            recordResponse.setProfile(Profile);
            recordResponse.setCountry(Country);
            recordResponse.setSuccess(success);
            recordResponse.setError(error);
        } catch (Exception e) {
        }
        return recordResponse;
    }


    public static void main(String[] args) throws IOException {
        System.out.println("Start filtering zgps...");
        final Config config = ConfigUtils.loadConfig(args[0]);
        final String CONFIG_MODULE = "GPSFilterZGPS";
        File sourceFileSelectedStages = new File(config.findParam(CONFIG_MODULE, "sourceFileSelectedStages"));
        File sourceFileZGPS = new File(config.findParam(CONFIG_MODULE, "sourceFileZGPS"));
        File targetFile = new File(config.findParam(CONFIG_MODULE, "targetFile"));
        System.out.println("Start reading selected stages...");
        FilterZGPSSelectedStages selectedStages = new FilterZGPSSelectedStages();
        selectedStages.createSelectedStages(sourceFileSelectedStages);
        System.out.println("Done. " + selectedStages.getSelectedStages().size() + " stages were stored");
        System.out.println("Start reading and writing zgps...");
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFileZGPS)));
            BufferedWriter out = new BufferedWriter(new FileWriter(targetFile));
            out.write(in.readLine());
            out.newLine();
            String lineFromInputFile;
            while ((lineFromInputFile = in.readLine()) != null) {
                String[] entries = lineFromInputFile.split("\t");
                if (selectedStages.containsStage(Integer.parseInt(entries[0]), Integer.parseInt(entries[1]), Integer.parseInt(entries[2]))) {
                    out.write(lineFromInputFile);
                    out.newLine();
                    out.flush();
                }
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            System.out.println("Could not find source file for selected stages.");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IO Exception while reading or writing zgps.");
            e.printStackTrace();
        }
        System.out.println("Done.");
    }


    public static void copy(String from_name, String to_name) throws IOException {
        File from_file = new File(from_name);
        File to_file = new File(to_name);
        if (!from_file.exists()) abort("FileCopy: no such source file: " + from_name);
        if (!from_file.isFile()) abort("FileCopy: can't copy directory: " + from_name);
        if (!from_file.canRead()) abort("FileCopy: source file is unreadable: " + from_name);
        if (to_file.isDirectory()) to_file = new File(to_file, from_file.getName());
        String parent = to_file.getParent();
        if (parent == null) parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists()) abort("FileCopy: destination directory doesn't exist: " + parent);
        if (dir.isFile()) abort("FileCopy: destination is not a directory: " + parent);
        if (!dir.canWrite()) abort("FileCopy: destination directory is unwriteable: " + parent);
        FileInputStream from = null;
        FileOutputStream to = null;
        try {
            from = new FileInputStream(from_file);
            to = new FileOutputStream(to_file);
            byte[] buffer = new byte[4096];
            int bytes_read;
            while ((bytes_read = from.read(buffer)) != -1) to.write(buffer, 0, bytes_read);
        } finally {
            if (from != null) try {
                from.close();
            } catch (IOException e) {
                ;
            }
            if (to != null) try {
                to.close();
            } catch (IOException e) {
                ;
            }
        }
    }


    public static String encriptarPassword(String aPassword) throws BusinessException {
        ILogger logger = LogFactory.getLogger(User.class);
        String methodId = "encriptarPassword";
        try {
            MessageDigest currentAlgorithm = MessageDigest.getInstance("MD5");
            currentAlgorithm.reset();
            currentAlgorithm.update(aPassword.getBytes());
            byte[] hash = currentAlgorithm.digest();
            String d = "";
            for (int i = 0; i < hash.length; i++) {
                int v = hash[i] & 0xFF;
                if (v < 16) d += "0";
                d += Integer.toString(v, 16).toUpperCase();
            }
            return d.toLowerCase();
        } catch (NoSuchAlgorithmException nsae) {
            logger.logError(methodId + "error al encriptar password:" + nsae, nsae);
            throw new BusinessException("Error al encriptar password");
        }
    }


    public static ISimpleChemObjectReader createReader(URL url, String urlString, String type) throws CDKException {
        if (type == null) {
            type = "mol";
        }
        ISimpleChemObjectReader cor = null;
        try {
            Reader input = new BufferedReader(getReader(url));
            FormatFactory formatFactory = new FormatFactory(8192);
            IChemFormat format = formatFactory.guessFormat(input);
            if (format != null) {
                if (format instanceof RGroupQueryFormat) {
                    cor = new RGroupQueryReader();
                    cor.setReader(input);
                } else if (format instanceof CMLFormat) {
                    cor = new CMLReader(urlString);
                    cor.setReader(url.openStream());
                } else if (format instanceof MDLV2000Format) {
                    cor = new MDLV2000Reader(getReader(url));
                    cor.setReader(input);
                }
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        if (cor == null) {
            if (type.equals(JCPFileFilter.cml) || type.equals(JCPFileFilter.xml)) {
                cor = new CMLReader(urlString);
            } else if (type.equals(JCPFileFilter.sdf)) {
                cor = new MDLV2000Reader(getReader(url));
            } else if (type.equals(JCPFileFilter.mol)) {
                cor = new MDLV2000Reader(getReader(url));
            } else if (type.equals(JCPFileFilter.inchi)) {
                try {
                    cor = new INChIReader(new URL(urlString).openStream());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (type.equals(JCPFileFilter.rxn)) {
                cor = new MDLRXNV2000Reader(getReader(url));
            } else if (type.equals(JCPFileFilter.smi)) {
                cor = new SMILESReader(getReader(url));
            }
        }
        if (cor == null) {
            throw new CDKException(GT._("Could not determine file format"));
        }
        if (cor instanceof MDLV2000Reader) {
            try {
                BufferedReader in = new BufferedReader(getReader(url));
                String line;
                while ((line = in.readLine()) != null) {
                    if (line.equals("$$$$")) {
                        String message = GT._("It seems you opened a mol or sdf" + " file containing several molecules. " + "Only the first one will be shown");
                        JOptionPane.showMessageDialog(null, message, GT._("sdf-like file"), JOptionPane.INFORMATION_MESSAGE);
                        break;
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return cor;
    }


    private Comic[] getAllComics() {
        try {
            URL comicURL = new URL(comicSite + "list");
            InputStream is = comicURL.openStream();
            BufferedReader buffread = new BufferedReader(new InputStreamReader(is));
            Vector tmplist = new Vector();
            while (buffread.ready()) {
                String comic = buffread.readLine();
                tmplist.add(comic);
            }
            Comic[] list = new Comic[tmplist.size()];
            activated = new boolean[tmplist.size()];
            titles = new String[tmplist.size()];
            for (int i = 0; i < tmplist.size(); i++) {
                try {
                    URL curl = new URL(comicSite + (String) tmplist.get(i));
                    BufferedInputStream bis = new BufferedInputStream(curl.openStream());
                    Properties cprop = new Properties();
                    cprop.load(bis);
                    Comic c = new Comic(cprop, false);
                    list[i] = c;
                    titles[i] = c.getName();
                    activated[i] = comicsmanager.isLoaded(c.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            for (int i = 0; i < list.length; i++) {
                System.out.println(list[i]);
            }
            return list;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    private static InputStream connect(final String url) throws IOException {
        InputStream in = null;
        try {
            final URLConnection conn = new URL(url).openConnection();
            conn.setConnectTimeout(YahooGeocoding.connectTimeOut);
            conn.setReadTimeout(YahooGeocoding.readTimeOut);
            conn.setRequestProperty("User-Agent", YahooGeocoding.USER_AGENT);
            in = conn.getInputStream();
            return in;
        } catch (final IOException e) {
            Util.d("problems connecting to geonames url " + url + "Exception:" + e);
        }
        return in;
    }


    public static String getSHA1Digest(String inputStr) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = null;
        byte[] sha1hash = null;
        md = MessageDigest.getInstance("SHA");
        sha1hash = new byte[40];
        md.update(inputStr.getBytes("iso-8859-1"), 0, inputStr.length());
        sha1hash = md.digest();
        return convertToHex(sha1hash);
    }


    private MediaWikiResult getFromUri(String url) throws OntologyServiceException {
        try {
            logger.info("getting " + url);
            HttpURLConnection connection = (java.net.HttpURLConnection) new URL(url).openConnection();
            connection.setRequestProperty("User-Agent", "OntoCat-" + Math.random());
            BufferedInputStream bin = new BufferedInputStream(connection.getInputStream());
            JAXBContext jaxbContext = JAXBContext.newInstance("uk.ac.ebi.ontocat.mediawiki.jaxb");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            return (MediaWikiResult) unmarshaller.unmarshal(bin);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OntologyServiceException(e);
        }
    }


    public final boolean login(String user, String pass) {
        if (user == null || pass == null) return false;
        connectionInfo.setData("com.tensegrity.palojava.pass#" + user, pass);
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(pass.getBytes());
            pass = asHexString(md.digest());
        } catch (NoSuchAlgorithmException ex) {
            throw new PaloException("Failed to create encrypted password for " + "user '" + user + "'!", ex);
        }
        connectionInfo.setUser(user);
        connectionInfo.setPassword(pass);
        return loginInternal(user, pass);
    }


    public RegionInfo(String name, int databaseID, int units, float xMin, float xMax, float yMin, float yMax, float zMin, float zMax, String imageURL) {
        this.name = name;
        this.databaseID = databaseID;
        this.units = units;
        this.xMin = xMin;
        this.xMax = xMax;
        this.yMin = yMin;
        this.yMax = yMax;
        this.zMin = zMin;
        this.zMax = zMax;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(this.name.getBytes());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream daos = new DataOutputStream(baos);
            daos.writeInt(this.databaseID);
            daos.writeInt(this.units);
            daos.writeDouble(this.xMin);
            daos.writeDouble(this.xMax);
            daos.writeDouble(this.yMin);
            daos.writeDouble(this.yMax);
            daos.writeDouble(this.zMin);
            daos.writeDouble(this.zMax);
            daos.flush();
            byte[] hashValue = digest.digest(baos.toByteArray());
            int hashCode = 0;
            for (int i = 0; i < hashValue.length; i++) {
                hashCode += (int) hashValue[i] << (i % 4);
            }
            this.hashcode = hashCode;
        } catch (Exception e) {
            throw new IllegalArgumentException("Error occurred while generating hashcode for region " + this.name);
        }
        if (imageURL != null) {
            URL url = null;
            try {
                url = new URL(imageURL);
            } catch (MalformedURLException murle) {
            }
            if (url != null) {
                BufferedImage tmpImage = null;
                try {
                    tmpImage = ImageIO.read(url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mapImage = tmpImage;
            } else this.mapImage = null;
        } else this.mapImage = null;
    }


    public String md5(String phrase) {
        MessageDigest m;
        String coded = new String();
        try {
            m = MessageDigest.getInstance("MD5");
            m.update(phrase.getBytes(), 0, phrase.length());
            coded = (new BigInteger(1, m.digest()).toString(16)).toString();
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        if (coded.length() < 32) {
            coded = "0" + coded;
        }
        return coded;
    }


    public static void copyURLToFile(URL source, File destination) throws IOException {
        InputStream input = source.openStream();
        try {
            FileOutputStream output = openOutputStream(destination);
            try {
                IOUtils.copy(input, output);
            } finally {
                IOUtils.closeQuietly(output);
            }
        } finally {
            IOUtils.closeQuietly(input);
        }
    }


    public boolean open(String mode) {
        if (source instanceof String) return false; else if (mode == null) mode = ""; else mode = mode.toLowerCase();
        boolean toread = false, towrite = false;
        if (mode.indexOf("r") >= 0) toread = true;
        if (mode.indexOf("w") >= 0) towrite = true;
        if (!toread && !towrite) toread = towrite = true;
        try {
            if (toread && input == null) {
                if (isDirectory()) return true; else if (reader != null) return true; else if (source instanceof File) input = new FileInputStream((File) source); else if (source instanceof Socket) input = ((Socket) source).getInputStream(); else if (source instanceof URL) return getUrlInfo(toread, towrite); else return false;
            }
            if (towrite && output == null) {
                if (isDirectory()) return false; else if (writer != null) return true; else if (source instanceof File) output = new FileOutputStream((File) source); else if (source instanceof Socket) output = ((Socket) source).getOutputStream(); else if (source instanceof URL) return getUrlInfo(toread, towrite); else return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    private String MD5Sum(String input) {
        String hashtext = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(input.getBytes());
            byte[] digest = md.digest();
            BigInteger bigInt = new BigInteger(1, digest);
            hashtext = bigInt.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hashtext;
    }


    private static Collection<String> createTopLevelFiles(Configuration configuration, Collections collections, Sets sets) throws FlickrException, SAXException, IOException, JDOMException, TransformerException {
        Collection<String> createdFiles = new HashSet<String>();
        File toplevelXmlFilename = getToplevelXmlFilename(configuration.photosBaseDirectory);
        Logger.getLogger(FlickrDownload.class).info("Creating XML file " + toplevelXmlFilename.getAbsolutePath());
        MediaIndexer indexer = new XmlMediaIndexer(configuration);
        Element toplevel = new Element("flickr").addContent(XmlUtils.createApplicationXml()).addContent(XmlUtils.createUserXml(configuration)).addContent(collections.createTopLevelXml()).addContent(sets.createTopLevelXml()).addContent(new Stats(sets).createStatsXml(indexer));
        createdFiles.addAll(indexer.writeIndex());
        XmlUtils.outputXmlFile(toplevelXmlFilename, toplevel);
        createdFiles.add(toplevelXmlFilename.getName());
        Logger.getLogger(FlickrDownload.class).info("Copying support files and performing XSLT transformations");
        IOUtils.copyToFileAndCloseStreams(XmlUtils.class.getResourceAsStream("xslt/" + PHOTOS_CSS_FILENAME), new File(configuration.photosBaseDirectory, PHOTOS_CSS_FILENAME));
        createdFiles.add(PHOTOS_CSS_FILENAME);
        IOUtils.copyToFileAndCloseStreams(XmlUtils.class.getResourceAsStream("xslt/" + PLAY_ICON_FILENAME), new File(configuration.photosBaseDirectory, PLAY_ICON_FILENAME));
        createdFiles.add(PLAY_ICON_FILENAME);
        XmlUtils.performXsltTransformation(configuration, "all_sets.xsl", toplevelXmlFilename, new File(configuration.photosBaseDirectory, ALL_SETS_HTML_FILENAME));
        createdFiles.add(ALL_SETS_HTML_FILENAME);
        XmlUtils.performXsltTransformation(configuration, "all_collections.xsl", toplevelXmlFilename, new File(configuration.photosBaseDirectory, ALL_COLLECTIONS_HTML_FILENAME));
        createdFiles.add(ALL_COLLECTIONS_HTML_FILENAME);
        createdFiles.add(Collections.COLLECTIONS_ICON_DIRECTORY);
        XmlUtils.performXsltTransformation(configuration, "stats.xsl", toplevelXmlFilename, new File(configuration.photosBaseDirectory, STATS_HTML_FILENAME));
        createdFiles.add(STATS_HTML_FILENAME);
        sets.performXsltTransformation();
        for (AbstractSet set : sets.getSets()) {
            createdFiles.add(set.getSetId());
        }
        return createdFiles;
    }


        public static void copyFile(File source, File destination) throws IOException {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination));
            byte[] buffer = new byte[4096];
            int read = -1;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            out.flush();
            out.close();
            in.close();
        }


    public static String sha1(String clearText, String seed) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update((seed + clearText).getBytes());
            return convertToHex(md.digest());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }