DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Coding Topics

article thumbnail
Class Loading Fun with Groovy
Sometimes you need special measures. Not to make Groovy work, but to make your applications or frameworks a little bit more powerful or versatile.
January 17, 2008
by Steven Devijver
· 42,980 Views · 1 Like
article thumbnail
Ruby On Rails: Change Class Name Into Human Readable String
This turns a class name (like LineItem) into a nice string (like "line item") line_item = LineItem.new puts line_item.class.name.underscore.humanize.lowcase #spits out "line item"
January 9, 2008
by Chris O'Sullivan
· 7,174 Views
article thumbnail
Groovy - Plain Text Word Wrap Method
// Groovy Method to perform word-wrap to a specified length. // Returns a List of strings representing the wrapped text // Quick and Dirty method for plain text word-wrap to a specified width static class TextUtils { static String[] wrapntab(input, linewidth = 70, indent = 0) throws IllegalArgumentException { if(input == null) throw new IllegalArgumentException("Input String must be non-null") if(linewidth <= 1) throw new IllegalArgumentException("Line Width must be greater than 1") if(indent <= 0) throw new IllegalArgumentException("Indent must be greater than 0") def olines = [] def oline = " " * indent input.split(" ").each() { wrd -> if( (oline.size() + wrd.size()) <= linewidth ) { oline <<= wrd <<= " " }else{ olines += oline oline = " " * indent } } olines += oline return olines } } // TEST // the input String input = "Note From SUPPLIER: Booking confirmed by fax. 4 standard rooms - 3 twin shared, 1 single room, please advise if guests require meals.. " // call static wrapntab method to break the input string into 70 char wide lines with a 4 char initial indent olines = TextUtils.wrapntab(input,70,4) // print the output olines.each() { println it }
December 4, 2007
by Snippets Manager
· 3,271 Views
article thumbnail
Get All Classes Within A Package
The code below gets all classes within a given package. Notice that it should only work for classes found locally, getting really ALL classes is impossible. /** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @param packageName The base package * @return The classes * @throws ClassNotFoundException * @throws IOException */ private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration resources = classLoader.getResources(path); List dirs = new ArrayList(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList classes = new ArrayList(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); } /** * Recursive method used to find all classes in a given directory and subdirs. * * @param directory The base directory * @param packageName The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException */ private static List findClasses(File directory, String packageName) throws ClassNotFoundException { List classes = new ArrayList(); if (!directory.exists()) { return classes; } File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; }
November 30, 2007
by Victor Tatai
· 124,702 Views · 7 Likes
article thumbnail
Active Record YAML Backup Solution - Drop In Rake Task And Config File To Backup Db And Directorys Of Your Choice (user Uploaded Files)
EZ drop in backup rake task for your rails projects - works well - 5 - 10 min set up, one rake file and one config file backs up db and any directory's to any number of servers with rsync BSD License or whatever, but it would be cool if you told me your using it 2007 ISS http://industrialstrengthinc.com this is a sample config file and a rake task you can drop into any rails project to do backups, easy to automate. Backs up your specified environment db to activerecord yml files (one per table) and zips them up in a human readable timestamped file syncs that and any other set of arbitrary directory's to any number of arbitrary servers with rsync be sure to set ssh key based logins to run: rake backup also: rake backup:db, rake backup:restoredb (promts for a file created by backup:db), rake backup:push (to push out what u got) note: this uses something like 30 lines of code from some blog site I got it from that I cant recall, yay for that guy, thanks ## example crontab entry: 3 * * * * cd /rails_deployment_dir/current && nice rake backup RAILS_ENV=production >> /rails_deployment_dir/production_backup_system.log ## config file - goes in config/backup.yml production: dirs: - db/backups - public/uploaded_images servers: - name: backup server number 1 host: gridserver.com port: 22 user: [email protected] dir: /home/blah/backups - name: backup server two host: kradradio.com port: 22 user: kraduser dir: /home/kraduser/backups_from_my_rails_proj development: dirs: - db/backups - public/uploaded_images servers: - name: local self host: localhost port: 5222 user: oneman dir: /home/oneman/Documents/development_backup ## rake file lib/tasks/backup.rake desc "Backup Everything Specified in config/backup.yml" task :backup => [ "backup:db", "backup:push"] namespace :backup do RAILS_APPDIR = RAILS_ROOT.sub("/config/..","") def interesting_tables ActiveRecord::Base.connection.tables.sort.reject! do |tbl| ['schema_info', 'sessions', 'public_exceptions'].include?(tbl) end end desc "Push backup to remote server" task :push => [:environment] do FileUtils.chdir(RAILS_APPDIR) backup_config = YAML::load( File.open( 'config/backup.yml' ) )[RAILS_ENV] for server in backup_config["servers"] puts "Backing up #{RAILS_ENV} directorys #{backup_config['dirs'].join(', ')} to #{server['name']}" puts "Time is " + Time.now.rfc2822 + "\n\n" for dir in backup_config["dirs"] local_dir = RAILS_APPDIR + "/" + dir + "/" remote_dir = server['dir'] + "/" + dir.split("/").last + "/" puts "Syncing #{local_dir} to #{server['host']}#{remote_dir}" sh "/usr/bin/rsync -avz -e 'ssh -p#{server['port']} ' #{local_dir} #{server['user']}@#{server['host']}:#{remote_dir}" end puts "Completed backup to #{server['name']}\n\n" end end task :storedb => :environment do backupdir = RAILS_APPDIR + '/db/backup' FileUtils.mkdir_p(backupdir) FileUtils.chdir(backupdir) puts "Dumping database to activerecord yaml files in #{backupdir}" interesting_tables.each do |tbl| klass = tbl.classify.constantize puts "Writing #{tbl}..." File.open("#{tbl}.yml", 'w+') { |f| YAML.dump klass.find(:all).collect(&:attributes), f } end puts "Database Dumped.\n\n" end desc "Dump Current Environment Db to file" task :db => [:environment, :storedb ] do backupdir = RAILS_APPDIR + '/db/backup' archivedir = RAILS_APPDIR + '/db/backups' backup_filename = "#{RAILS_ENV}_db_backup_#{Time.now.strftime("%B.%d.%Y_at_%I.%M.%S%p_%Z")}.tar.bz2" FileUtils.mkdir_p(archivedir) puts "Archiving #{backupdir} yaml files to #{backup_filename}\n\n" `tar -C #{backupdir} -cjf #{backup_filename} *` `mv #{backup_filename} #{archivedir}` end desc "Restore Current Environment Db from a file" task :restoredb => [:environment] do backupdir = RAILS_APPDIR + '/db/backup' archivedir = RAILS_APPDIR + '/db/backups' print "Input a file to load into the db: #{archivedir}/" backup_filename = STDIN.gets.chomp puts "Loading backup file: #{backup_filename}" FileUtils.chdir(archivedir) `tar -xjf #{backup_filename}` `mv *.yml #{backupdir}` FileUtils.mkdir_p(backupdir) FileUtils.chdir(backupdir) interesting_tables.each do |tbl| puts "Clearing #{tbl} table.." ActiveRecord::Base.connection.execute "TRUNCATE #{tbl}" puts "Loading #{tbl} backup file..." table = YAML.load_file("#{tbl}.yml") if table.length > 0 && table.first.key?("id") highestid = 0 table.each do |fixture| if fixture["id"] > highestid highestid = fixture["id"] end end ActiveRecord::Base.connection.execute "SELECT setval('#{tbl}_id_seq',#{highestid})" puts "Setting #{tbl}_id sequence to #{highestid}" end #klass = tbl.classify.constantize ActiveRecord::Base.transaction do puts "Inserting #{table.length} values into #{tbl}" table.each do |fixture| ActiveRecord::Base.connection.execute "INSERT INTO #{tbl} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert' end puts "#{tbl} table restored.\n\n" end end end end
November 16, 2007
by Snippets Manager
· 2,331 Views
article thumbnail
Javascript Sprintf
September 19, 2007
by Snippets Manager
· 498 Views
article thumbnail
Java: RegEx: Splitting A Space-, Comma-, And Semi-colon Separated List
// Greedy RegEx quantifier used // X+ = X, one or more times // [\\s,;]+ = one or more times of either \s , or ; String test_data = "hello world, this is a test, ;again"; _logger.debug("Source: " + test_data); for (String tag : test_data.split("[\\s,;]+")) { _logger.debug("Received tag: [" + tag + "]"); }
August 5, 2007
by Snippets Manager
· 38,172 Views · 1 Like
article thumbnail
Class PHP XMLHttpRequest Emulator Using Curl.
// XMLHttpRequest emulator using curl. * @version 0.5 2007/07/16 23:00:13 * @link http://www.myopera.com/moises-l Comments & suggestions * @link http://files.myopera.com/moises-l/files/class.XMLHttpRequest.php Available at * @copyright GPL © 2007, Moises Lima * @license http://creativecommons.org/licenses/by-nc-sa/2.5/ Released under a Creative Commons License */ class XMLHttpRequest{ /** * String version of data returned from server process. * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-responsetext * @access public * @var string * @name $responseText */ var $responseText; /** * DOM-compatible document object of data returned from server process. * which can be examined and parsed using W3C DOM node tree methods and properties * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-responsexml * @access public * @var object * @name $responseXML */ var $responseXML; /** * The http status code returned by server as a number (e.g. 404 for "Not Found" or 200 for "OK"). * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-status * @access public * @var number * @name $status */ var $status; /** * The http status code returned by server as a string (e.g. "Not Found" or "OK") * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-statustext * @access public * @var string * @name $statusText */ var $statusText; /** * The state of the object * 0 = uninitialized * 1 = loading * 2 = loaded * 3 = interactive * 4 = complete * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-readystate * @access public * @var number * @name $readyState */ var $readyState; /** * The error string * @link http://www.w3.org/TR/XMLHttpRequest/#notcovered * @access public * @var string * @name $error */ var $error; /** * An event handler for an event that fires at every state change * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-onreadystatechange * @access public * @name $onreadystatechange */ var $onreadystatechange; /** * An event handler for an event that fires at finished requisition * @link http://www.w3.org/TR/XMLHttpRequest/#notcovered * @access public * @name $onload */ var $onload; /** * An event handler for an event that fires at errors * @link http://www.w3.org/TR/XMLHttpRequest/#notcovered * @access public * @name $onerror */ var $onerror; // http://www.w3.org/TR/XMLHttpRequest/#notcovered /** * cURL handle * @access private * @name $curl */ var $curl; /** * responseHeaders process * @access private * @name $responseHeaders */ var $responseHeaders; /** * cURL headers * @access private * @name $headers */ var $headers=array("Connection: Keep-Alive","Keep-Alive: 300"); /** * Curl info * @access public * @name $curl_version * @var Array */ var $curl_version; /** * TRUE to follow any "Location: " header that the server sends as part of the HTTP header. * @access public * @name $followLocation * @var Bolean */ var $followLocation; /** * Class constructor (compatibility with PHP 4). */ function XMLHttpRequest(){ $this->open="function open() { [native code] }"; $this->setRequestHeader="function setRequestHeader() { [native code] }"; $this->getAllResponseHeaders="function getAllResponseHeaders() { [native code] }"; $this->getResponseHeader="function getResponseHeader() { [native code] }"; $this->send="function send() { [native code] }"; $this->readyState = 0; $this->curl = curl_init(); $this->curl_version = curl_version(); $this->followLocation=false; curl_setopt($this->curl, CURLOPT_HEADER, true); if(isset($_SERVER['HTTP_USER_AGENT'])){ curl_setopt($this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ); }else{ curl_setopt($this->curl, CURLOPT_USERAGENT, "XMLHttpRequest/0.2"); } curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLOPT_TIMEOUT, 1000); curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 300); } /** * @access private */ function __toString(){ return "[object XMLHttpRequest]"; } /** * Specifies the method, URL, and other optional attributes of a request. * @access public * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-open * @param String $method HTTP Methods defined in section 5.1.1 of RFC 2616 http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html * @param String $url Specifies either the absolute or a relative URL of the data on the Web service. * @param Bolean $async FakeSauro Erectus. * @param String $user specifies the name of the user for HTTP authentication. * @param String $password specifies the password of the user for HTTP authentication. * @return void */ function open($method, $url, $async=true, $user="", $password=""){ $this->readyState = 1; if(!empty($method) && !empty($url)){ $method=strtoupper(trim($method)); /* if(!ereg("^(GET|POST|HEAD|PUT|DELETE|OPTIONS)$",$method)){ throw new Exception("Unknown HTTP request method [$method]"); } */ if(isset($_SERVER['HTTP_REFERER']) && empty($this->url) ){ curl_setopt($this->curl, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']); }elseif(isset($this->url)){ curl_setopt($this->curl, CURLOPT_REFERER, $this->url); } $this->url = $url; curl_setopt($this->curl, CURLOPT_URL, $this->url); if($method=="POST"){ curl_setopt($this->curl, CURLOPT_POST, 1); }elseif($method=="GET"){ curl_setopt($this->curl, CURLOPT_POST, 0); }else{ curl_setopt($this->curl, CURLOPT_POST, 0); curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); } } if(ereg("^(https)",$url)){ curl_setopt($this->curl,CURLOPT_SSL_VERIFYPEER,false); } if(!empty($user) && !empty($password)){ curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($this->curl,CURLOPT_USERPWD,$user.":". $password); } } /** * Assigns a label/value pair to the header to be sent with a request. * @access public * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-setrequestheader * @param String $label Specifies the header label. * @param String $value Specifies the header value. * @return void */ function setRequestHeader($label, $value){ $this->headers[] = "$label: $value"; curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers); } /** * Returns complete set of headers (labels and values) as a string. * @access public * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-getallresponseheaders * @return string Complete set of headers (labels and values) as a string */ function getAllResponseHeaders(){ return $this->responseHeaders; } /** * Returns the value of the specified http header. * @access public * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-getresponseheader. * @param String $label * @return String|null The string value of a single header label. */ function getResponseHeader($label){ $value=array(); preg_match_all('/(?s)'.$label.': (.*?)\s\n/i', $this->responseHeaders , $value); if(count($value ) > 0){ return implode(', ' , $value[1]); } return null; } function getResponseHeader2($label){ $value=array(); preg_match('/(?s)'.$label.': (.*?)\s\n/i', $this->responseHeaders , $value); if(count($value ) > 0){ return $value[1]; } return null; } /** * Transmits the request, optionally with postable string or DOM object data. * @access public * @link http://www.w3.org/TR/XMLHttpRequest/#dfn-getresponseheader * @param String $data * @return void */ function send($data=null){ $sT=array(); if(isset($this->onreadystatechange))eval($this->onreadystatechange); if($data){ curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); } $this->response= curl_exec($this->curl); $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); $this->responseHeaders = substr($this->response, 0, $header_size - 4); if($this->followLocation){ $location=array(); while(preg_match('/Location:(.*?)\n/', $this->responseHeaders, $location)){ curl_setopt($this->curl, CURLOPT_REFERER, $this->url); $url = @parse_url(trim(array_pop($location))); if (!$url){ break; } $last_url = parse_url(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL)); if (!isset($url['scheme']))$url['scheme'] = $last_url['scheme']; if (!isset($url['host']))$url['host'] = $last_url['host']; if (!isset($url['path']))$url['path'] = $last_url['path']; $this->url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query'])?'?'.$url['query']:''); curl_setopt($this->curl, CURLOPT_POST, 0); //curl_setopt($this->curl, CURLOPT_POSTFIELDS,0); curl_setopt($this->curl, CURLOPT_URL, $this->url); $this->response= curl_exec($this->curl); $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); $this->responseHeaders = substr($this->response, 0, $header_size - 4); } } $this->error = curl_error($this->curl); if ($this->error) { if(isset($this->onerror))eval($this->onerror); } $this->readyState = 2; if(isset($this->onreadystatechange))eval($this->onreadystatechange); $this->responseText = substr($this->response, $header_size); preg_match('/^HTTP\/\d\.\d\s+(\d{3}) (.*)\s\n/i', $this->responseHeaders , $sT); if(count($sT ) > 2){ $this->responseHeaders = ereg_replace ($sT[0], "", $this->responseHeaders); $this->status = $sT[1]; $this->statusText = $sT[2]; } if(version_compare(PHP_VERSION , "5", ">=")){ if (preg_match('/(application|text)\/[\w+\+]?xml/i', $this->getResponseHeader("Content-Type"))){ libxml_use_internal_errors(true); $this->responseXML = new DOMDocument(); $this->responseXML->loadXML($this->responseText); $errors = libxml_get_errors(); if (!empty($errors)){ $this->responseXML=null; $error=$errors[0]; $this->error= trim($error->message) ." in $this->url on line $error->line column: $error->column "; if(isset($this->onerror))eval($this->onerror); } libxml_clear_errors(); } } $this->readyState = 3; if(isset($this->onreadystatechange))eval($this->onreadystatechange); $this->headers=Array(); $this->readyState = 4; if(isset($this->onreadystatechange))eval($this->onreadystatechange); if(isset($this->onload))eval($this->onload); } /** * Closes a cURL session and frees all resources. * @name close * @access public * @return void */ function close(){ curl_close($this->curl); } } ?>
July 18, 2007
by Snippets Manager
· 3,532 Views
article thumbnail
PHP - Change Active Directory Password
You are changing the password for ". $info[$i]["givenname"][0] .", " . $info[$i]["sn"][0] ." (" . $info[$i]["samaccountname"][0] .") to " . $_POST['user_pass'] ." \n"; $passwd1 = $_POST['user_pass']; $userDn = $info[$i]["distinguishedname"][0]; $newPassword = $passwd1; $newPassword = "\"" . $newPassword . "\""; $len = strlen($newPassword); for ($i = 0; $i < $len; $i++){ $newPassw .= "{$newPassword{$i}\000";} $newPassword = $newPassw; $userdata["unicodePwd"] = $newPassword; $result = ldap_mod_replace($ldap, $userDn , $userdata); if ($result) echo "Your password has been changed!" ; else echo "There was a problem changing your password, please call IT for help"; } } @ldap_close($ldap); ?>
May 23, 2007
by Snippets Manager
· 4,618 Views
article thumbnail
Remove Empty XML Nodes
Remove nodes like (without attributes & without children) public static void RemoveEmptyNodes(XmlDocument doc) { XmlNodeList nodes = doc.SelectNodes("//node()"); foreach (XmlNode node in nodes) if ((node.Attributes.Count == 0) && (node.ChildNodes.Count == 0)) node.ParentNode.RemoveChild(node); }
May 15, 2007
by Snippets Manager
· 8,509 Views
article thumbnail
Java: Lucene: Simple In-Memory Search Example
// Adapted from http://javatechniques.com/blog/lucene-in-memory-text-search-example // Works with present APIs in Lucene 2.1.0 /** * A simple example of an in-memory search using Lucene. */ import java.io.IOException; import java.io.StringReader; import org.apache.lucene.search.Hits; import org.apache.lucene.search.Query; import org.apache.lucene.document.Field; import org.apache.lucene.search.Searcher; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.document.Document; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.analysis.standard.StandardAnalyzer; public class InMemoryExample { public static void main(String[] args) { // Construct a RAMDirectory to hold the in-memory representation // of the index. RAMDirectory idx = new RAMDirectory(); try { // Make an writer to create the index IndexWriter writer = new IndexWriter(idx, new StandardAnalyzer(), true); // Add some Document objects containing quotes writer.addDocument(createDocument("Theodore Roosevelt", "It behooves every man to remember that the work of the " + "critic, is of altogether secondary importance, and that, " + "in the end, progress is accomplished by the man who does " + "things.")); writer.addDocument(createDocument("Friedrich Hayek", "The case for individual freedom rests largely on the " + "recognition of the inevitable and universal ignorance " + "of all of us concerning a great many of the factors on " + "which the achievements of our ends and welfare depend.")); writer.addDocument(createDocument("Ayn Rand", "There is nothing to take a man’s freedom away from " + "him, save other men. To be free, a man must be free " + "of his brothers.")); writer.addDocument(createDocument("Mohandas Gandhi", "Freedom is not worth having if it does not connote " + "freedom to err.")); // Optimize and close the writer to finish building the index writer.optimize(); writer.close(); // Build an IndexSearcher using the in-memory index Searcher searcher = new IndexSearcher(idx); // Run some queries search(searcher, "freedom"); search(searcher, "free"); search(searcher, "progress or achievements"); searcher.close(); } catch (IOException ioe) { // In this example we aren’t really doing an I/O, so this // exception should never actually be thrown. ioe.printStackTrace(); } catch (ParseException pe) { pe.printStackTrace(); } } /** * Make a Document object with an un-indexed title field and an indexed * content field. */ private static Document createDocument(String title, String content) { Document doc = new Document(); // Add the title as an unindexed field… doc.add(new Field("title", title, Field.Store.YES, Field.Index.NO)); // …and the content as an indexed field. Note that indexed // Text fields are constructed using a Reader. Lucene can read // and index very large chunks of text, without storing the // entire content verbatim in the index. In this example we // can just wrap the content string in a StringReader. doc.add(new Field("content", new StringReader(content))); return doc; } /** * Searches for the given string in the "content" field */ private static void search(Searcher searcher, String queryString) throws ParseException, IOException { // Build a Query object QueryParser parser = new QueryParser("content", new StandardAnalyzer()); Query query = parser.parse(queryString); // Search for the query Hits hits = searcher.search(query); // Examine the Hits object to see if there were any matches int hitCount = hits.length(); if (hitCount == 0) { System.out.println("No matches were found for \"" + queryString + "\""); } else { System.out.println("Hits for \"" + queryString + "\" were found in quotes by:"); // Iterate over the Documents in the Hits object for (int i = 0; i < hitCount; i++) { Document doc = hits.doc(i); // Print the value that we stored in the "title" field. Note // that this Field was not indexed, but (unlike the // "contents" field) was stored verbatim and can be // retrieved. System.out.println(" " + (i + 1) + ". " + doc.get("title")); } } System.out.println(); } }
May 15, 2007
by Snippets Manager
· 6,186 Views
article thumbnail
Java DOM : Creating An XML Document From XML File
// description of your code here try { // // Create the XML Document // DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.parse(filePath); // ... } catch (Exception e) { // ... }
May 14, 2007
by Snippets Manager
· 19,220 Views
article thumbnail
How To Convert A String With A Date To A Calendar
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); Date date = sdf.parse(strDate); Calendar cal = Calendar.getInstance(); cal.setTime(date);
May 8, 2007
by Snippets Manager
· 160,493 Views · 3 Likes
article thumbnail
Range() In Java
A range is a very handy feature of programing languages like Python. * range( 10 ) -> 0 1 2 3 4 5 6 7 8 9 * range( 5, 10 ) -> 5 6 7 8 9 * range( 0, 10, 3 ) -> 0 3 6 9 * range( '0', '9' ) -> 012345678 With an extended for loop it is possible to use such a feature too: /* * This project is made available under the terms of the BSD license, more information can be found at * http://www.opensource.org/licenses/bsd-license.html * * Copyright (c) 2007. Christian Ullenboom (http://www.tutego.com/) and contributors. All rights reserved. */ package com.tutego; import java.util.Iterator; /** * Class that generates immutable sequences (ranges) as Iterable * objects. A range represents a start (0 if not given), an stop (mandatory) and * an optional step (1 by default). The start value is included in the range, * the stop value is exclusive. Every range is handled by an Iterable * which can by used in an extended for loop. * * * for ( int i : range( 0, 10, 3 ) ) * System.out.print( i + " " ); // 0 3 6 9 * * * @author Christian Ullenboom (tutego) * @version 1.0 */ public class Range { public static Iterable range( final int start, final int stop, final int step ) { if ( step <= 0 ) throw new IllegalArgumentException( "step > 0 isrequired!" ); return new Iterable() { public Iterator iterator() { return new Iterator() { private int counter = start; public boolean hasNext() { return counter < stop; } public Integer next() { try { return counter; } finally { counter += step; } } public void remove() { } }; } }; } public static Iterable range( final int start, final int stop ) { return range( start, stop, 1 ); } public static Iterable range( final int stop ) { return range( 0, stop, 1 ); } } This is an example: package com.tutego; import static com.tutego.Range.range; public class RangeDemo { public static void main( String[] args ) { for ( int i : range( 10 ) ) System.out.print( i + " " ); System.out.println(); for ( int i : range( 5, 10 ) ) System.out.print( i + " " ); System.out.println(); for ( int i : range( 0, 10, 3 ) ) System.out.print( i + " " ); System.out.println(); for ( int i : range( '0', '9' ) ) System.out.print( (char) i ); System.out.println(); String[] a = { "Mary", "had", "a", "little", "lamb" }; for ( int i : range(a.length ) ) System.out.printf( "%d %s%n", i, a[i] ); } }
April 24, 2007
by Snippets Manager
· 32,575 Views · 1 Like
article thumbnail
Reading Corrupted/partial Zip Files In Python
First a simple script for reading non-corruted zipfiles in python: filename = 'foo.zip' import zipfile z = zipfile.ZipFile(filename) for i in z.infolist(): print i.filename, i.file_size z.read('somefile') Next we use 'zip -FF foo.zip' to fix the zipfile, before reading it: filename = 'foo.zip' import zipfile try: z = zipfile.ZipFile(filename) except zipfile.BadZipfile: import commands commands.getoutput('zip -FF '+filename) z = zipfile.ZipFile(filename) for i in z.infolist(): print i.filename, i.file_size try: z.read('somefile') except zipfile.BadZipfile: print 'Bad CRC-32' In short: use 'zip -FF file.zip' to fix the file. It will restore the filelist.
April 9, 2007
by Snippets Manager
· 9,979 Views · 3 Likes
article thumbnail
Get MD5 Hash In A Few Lines Of Java
1 import java.security.*; 2 import java.math.*; 3 4 public class MD5 { 5 public static void main(String args[]) throws Exception{ 6 String s="This is a test"; 7 MessageDigest m=MessageDigest.getInstance("MD5"); 8 m.update(s.getBytes(),0,s.length()); 9 System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16)); 10 } 11 }
March 18, 2007
by Greg Miller
· 46,089 Views · 2 Likes
article thumbnail
JavaScript: Programmatically Click The Form Submit Button
// Programmatically Click the Form Submit Button // by using the 'click()' method submitTags : function() { var btnSubmitTags = document.getElementById( TagsHelperConfig.FORM_TAGS_ENTRY_SUBMIT_BUTTON_ID ); // Programmatically click the submit button btnSubmitTags.click(); }
March 6, 2007
by Snippets Manager
· 12,433 Views · 3 Likes
article thumbnail
Add A Jar File To Java Load Path At Run Time
import java.net.URL; import java.io.IOException; import java.net.URLClassLoader; import java.net.MalformedURLException; public class JarFileLoader extends URLClassLoader { public JarFileLoader (URL[] urls) { super (urls); } public void addFile (String path) throws MalformedURLException { String urlPath = "jar:file://" + path + "!/"; addURL (new URL (urlPath)); } public static void main (String args []) { try { System.out.println ("First attempt..."); Class.forName ("org.gjt.mm.mysql.Driver"); } catch (Exception ex) { System.out.println ("Failed."); } try { URL urls [] = {}; JarFileLoader cl = new JarFileLoader (urls); cl.addFile ("/opt/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar"); System.out.println ("Second attempt..."); cl.loadClass ("org.gjt.mm.mysql.Driver"); System.out.println ("Success!"); } catch (Exception ex) { System.out.println ("Failed."); ex.printStackTrace (); } } }
February 23, 2007
by Snippets Manager
· 31,578 Views
article thumbnail
Retrieving All Parameters to a Java Servlet
Here's a really simple code snippet for retrieving parameters to a Java servlet.
February 10, 2007
by Snippets Manager
· 27,192 Views · 2 Likes
article thumbnail
Get The Unix Epoch Time In One Line Of C#
One line of C#, so much awesomeness: The Unix epoch time.
January 3, 2007
by Greg Miller
· 31,139 Views
  • Previous
  • ...
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×