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 Languages Topics

article thumbnail
From Java to Groovy in a Few Easy Steps
Groovy and Java are really close cousins, and their syntaxes are very similar, hence why Groovy is so easy to learn for Java developers.
January 27, 2008
by Guillaume Laforge
· 121,806 Views · 3 Likes
article thumbnail
Inserting Variable Headers in Apache
A buddy of mine was discussing a problem he was having with his corporate web site. It's hosted on multiple web servers behind a load balancer. The problem is, they are having problems with one of the web servers but they can't figure out which. I remember that back at JupiterHosting, we had a similar problem and that one of my guys there injected a header that allowed us to track which server a specific request was coming from. Unfortunately for my friend, that was about as far as I remembered. (I was management then, I didn't pay attention to details) However, he's a bright guy and had already thought of this...the difference was, he knew basically how to do it. I started playing with ideas on my development server and got about 75% of the way there when he IMed me that he had it working. (So much for speed) I fired up FireFox and TamperData and sure enough, he had a custom header in there. (BTW, TamperData is invaluable for debugging things like this.) However, when I followed his direction, it just was not happening for me. Googling around found me a LOT of copies of the Apache manual, but no concrete examples. So, since I couldn't find the answer on the web, I decided to post how I did this in case some other Apache noob comes looking for it. First, an important detail, my development environment is a customized version of CentOS 5. I use yum for just about everything, except Apache, PHP, MySQL and their support programs. I use a script that comes with DirectAdmin, my production control panel of choice, to maintain those pieces. The important thing to note here is that CentOS does NOT use apachectrl to start and stop apache. The answer to inserting a header, of course, revolves around PassEnv. If you have mod_env installed in your Apache 1.3.7 or Apache 2.x, this will work. I'm working with Virtual Hosts and each development site has it's on conf file that is included into the main httpd.conf. The great thing about PassEvn though is you can put it in an .htaccess file as well. This means for testing, you don't have to constantly be bouncing the service. To identify my server, I decided I wanted to add a header that displayed the host name that the page was being served from. This means in my conf file or .htaccess, I need the following: PassEnv HOSTNAME Header set X-MyHeader "%{HOSTNAME}e" That gets us about 75% there. The PassEnv makes the HOSTNAME environment variable available to APache and the Header command actually sets the new header. "%{VARAIABLENAME}e" is the syntax for displaying the variable in the header. All that is fine and good and if you drop that in your .htaccess file and then hit a page on the site, you will get: X-MyHeader (null) Now, my friend said he added: export HOSTNAME=`hostname` to his apachectrl script and all was good. (note that those are backticks and not single quotes...very important) I tried this and (if you read the note above, you see this coming) it did not fix the problem. Digging deeper, I found that apachectrl sources a file /usr/sbin/envvars. Looking it, it's obvious that this is where they expect you to put these commands so you don't have to have a customized version of apachectrl. So I took my export out of apachectrl and put it in envvars. Still no dice, yeah, I know, you expected this. The problem, as I explained above, is that if you are using "service httpd start" on CentOS then apachectrl is being ignored. Lickily for us, /etc/init.d/httpd is another bash script. So I grabbed the lines out of apachectrl that sourced /usr/sbin/envvars, inserted them in /etc/init.d/httpd and BINGO, we have a header. if test -f /usr/sbin/envvars; then . /usr/sbin/envvars fi I put them high up in the file, near where it sources the functions file. So, it is possible, and even easy to add custom headers into Apache that include environment variables. You need to make sure that mod_env and mod_headers are both installed and the rest is just figuring out where to put things. Yes you can also access this new information in PHP via the $_ENV super global, just not in the way you might expect. A var_dump of the $_ENV on my development server now looks like this: array(8) { ["HOSTNAME"]=> string(5) "david" ["TERM"]=> string(5) "xterm" ["LD_LIBRARY_PATH"]=> string(15) "/etc/httpd/lib:" ["PATH"]=> string(29) "/sbin:/usr/sbin:/bin:/usr/bin" ["PWD"]=> string(1) "/" ["LANG"]=> string(11) "en_US.UTF-8" ["SHLVL"]=> string(1) "2" ["_"]=> string(15) "/usr/sbin/httpd" } As you can see, the header X-MyHeader is not exposed, the variable it contains, HOSTNAME, is. For the record, I do realize that hostname was probably not the best example since it's usually in the $_SERVER array. However, the point of the experiment was not to expose this information to PHP, it was to put it in the response from the server.
January 26, 2008
by Cal Evans
· 19,904 Views
article thumbnail
3D Model Interaction with Java 3D
This tutorial is based on a computer graphics assignment for which i was given the task of creating an application in which some articulated animal would walk using a hierarchical model. I had 4 days to complete this assignment, so i had to learn java 3d quickly, but i ended up having to read fragmented documentation, mostly focused on theory i already knew, with practical examples that were either too simple or too complex. The objective of this tutorial is to provide a guide for writing a basic java 3d application with a 3d model loaded from disk; it's less generic than the official java3d application tutorial and less focused on theory than other tutorials, but more straightforward for the experienced java developer who already knows basic cg theory and just wants to know what goes where very quickly - going deeper in the APIs is up to you. Requirements JDK version 1.5 or above (the examples use java 5 features) java 3d version 1.4 or above installed experience with jfc basic computer graphics knowledge (3d transforms, illumination types) a 3d model visualizer, like poseray a 3d model converter like 3dwin will be useful if you find some interesting 3d model in a format not supported by any java 3d loader Models You can download free 3d models on websites like turbosquid or the 3d archive . Free models may not have the quality you are looking for, so if you are on a serious/commercial project, you should probably consider purchasing a quality model. If you really want to model your objects you can try blender. Visualizing the model I will use a cockroach I downloaded from the 3d archive. You can choose another model if you will as long you know what you're doing. I will use poseray to visualize the model. Poseray cannot open every 3d format, so if the format of your model is not supported by poseray, you will have to use some other program like 3dwin to convert it to a format poseray accepts. poseray is actually intended to work with moray and povray, but it works very well for the purpose of viewing 3d models. 1. load the model 2. check the model (shot #1) 3. check the model (shot #2) 4. check how this model is branched These are the pieces that form the complete model. you will have to analyze how your model is branched to see if you can animate or interact with it as you plan. Every component of the model has a name - let it be the parts of your main subject or just other components from the scene. You can get these names on your program, but it's easier to check which part is which here. you can use the update function if the names aren't descriptive enough. You will need to know the name of every part if you plan to texturize or animate them independently. In the end, all that matters is that you save your file in a format that java loaders will recognize. preferably, save it in the wavefront .obj or lightwave . LWO format because java 3d comes with loaders for these file formats by default. Other loaders are available, but you will have to download them separately. Other java3d loaders Loading the model Wavefront .obj format import java.io.filereader; import java.io.ioexception; import com.sun.j3d.loaders.scene; // contains the object loaded from disk. import com.sun.j3d.loaders.objectfile.objectfile; // loader of .obj models public static scene loadscene(string location) throws ioexception { objectfile loader = new objectfile(objectfile.resize); return loader.load(new filereader(location)); } Lightwave .lwo format import com.sun.j3d.loaders.lw3d.lw3dloader; // loader of .lwo models public static scene loadscene(string location) throws ioexception { lw3dloader loader = new lw3dloader(); return loader.load(new filereader(location)); } Recommended reading: objectfile javadoc , lw3dloader javadoc . Basic setup Now that you know how to load the model let's see how it will look on your program before proceeding to further manipulation. the most important class of this example is the simple universe, which saves you from having to configure the view of your scene. a directional light is added to allow you to view your object (no light and you will see a plain black). You can view the source in . Roach as seen on the example program Recommended reading: simpleuniverse javadoc Getting the scene components We need to obtain a reference to every body part we need to manipulate (or just scene component, if you are not using a model of an animal). If you want to create a variable for every component and assign a meaningful name to each one, you will have to know what name maps to what component. the following piece of code demonstrates how to list the name of every named object from the scene: import javax.media.j3d.shape3d; void listscenenamedobjects(scene scene) { map namemap = scene.getnamedobjects(); for (string name : namemap.keyset()) { system.out.printf("name: %s\n", name); } } Have in mind that every shape3d is already part of the branchgroup of the scene, you have loaded. If you want to create another graph with your custom hierarchy, you will have to get a reference to one specific shape3d and then remove it from the branchgroup : import javax.media.j3d.branchgroup; /* obtains a reference to a specific component in the scene */ shape3d eyes = namemap.get("eyes"); /* the graph that still contains a reference to "eyes" */ branchgroup root = scene.getscenegroup(); /* removes "eyes" from this graph */ root.removechild(eyes); /* now you are free to use "eyes" in your custom graph */ Always remember you cannot add a component to more than one graph. If one component is already part of a graph and you try to add it to another, you will get a multipleparentexception. If you need the same component in more than one graph, you can clone them. Transformations Basic transformation steps: Add the parts you want to transform to a transformgroup ; Apply the transformgroup.allow_transform_write capability to the group if it wasn't set; Create or use some previously created instance of transform3d ; Configure this instance of transform3d as / if necessary; Apply this transform3d instance on the transformgroup instance. That implies you will have to keep references to instances of these classes in order to transform specific nodes of your graph. The following piece of code demonstrates translation, rotation on multiple axis and non-uniform scaling. It uses code created on previous sections. import javax.vecmath.vector3f; import javax.vecmath.vector3d; import javax.media.j3d.transformgroup; import javax.media.j3d.transform3d; map namemap = scene.getnamedobjects(); /* get the node you want to transform */ shape3d wing = namemap.get("wing"); /* add it to a transformgroup */ transformgroup transformgroup = new transformgroup(); transformgroup.addchild(wing); /* necessary to allow this group to be transformed */ transformgroup.setcapability(transformgroup.allow_transform_write); /* accumulates all transforms */ transform3d transforms = new transform3d(); /* creates rotation transforms for x, y and z axis */ transform3d rotx = new transform3d(); transform3d roty = new transform3d(); transform3d rotz = new transform3d(); rotx.rotx(15d); // +15 degrees on the x axis roty.roty(30d); // +30 degrees on the y axis rotz.rotz(-20d); // -20 degrees on the z axis /* combines all rotation transforms */ transforms.mul(rotx, roty); transforms.mul(transforms, rotz); /* translation: translates 2 on x, 3 on y and -10 on z */ vector3f translationvector = new vector3f(2f, 3f, -10f); transforms.settranslation(translationvector); /* non uniform scaling: scales 3x on x, 1x on y and 2x on z */ vector3d scale = new vector3d(3d, 1d, 2d); transforms.setscale(scale); /* apply all transformations */ transformgroup.settransform(transforms); Recommended reading: transform3d javadoc , transformgroup javadoc Hierarchical model Now that you have access to all components separately, you can build your custom hierarchical graph. If you have been using swing or awt, you are already familiar with the hierarchical model. for instance, you can have a jframe , which then adds a jpanel , which then adds a jlabel and so forth. Many properties applied on the root are propagated to children, like the isvisible() property. With a 3d model, all transforms and texturizations will be applied to all children (subgraphs). imagine if you had to apply the same transform over and over to many model parts just to make one movement? Java 3d has a class called group , which is basically an n-tree: every children has only one parent and an arbitrary number of children. you will use subclasses of group to create your scenes. java 3d has also the leaf class, which is used to construct objects on the tree which wouldn't make sense with children, like background, camera, behaviour, etc. hierarchical model of the scene I will use the transformgroup class as the default node for building the graph. you may use other subclasses of group if you have other needs. You may want to keep a reference of every transformgroup you create if you are going to do some interaction (like making a cockroach walk). Note that the code above suffers from the same flaws of programatic gui construction. you can define the graph in xml and create a custom parser if you need reusability. if possible, you can also edit the model graph in a model editor to avoid having to perform these steps on your program. Hierarchical construction of the graph transformgroup getcockroach(scene scene) { /* obtain the scene's branchgroup, from which components are removed */ branchgroup root = scene.getscenegroup(); map namemap = scene.getnamedobjects(); /* remove all children (you don't want a multiparentexception) */ root.removeallchildren(); /* construct the groups */ transformgroup leftlegs = new transformgroup(); transformgroup rightlegs = new transformgroup(); transformgroup body = new transformgroup(); transformgroup roach = new transformgroup(); /* build the graph --> left legs */ leftlegs.addchild(namemap.get("luplegf")); leftlegs.addchild(namemap.get("luplegm")); leftlegs.addchild(namemap.get("luplegr")); leftlegs.addchild(namemap.get("lmidlegf")); leftlegs.addchild(namemap.get("lmidlegm")); leftlegs.addchild(namemap.get("lmidlegr")); leftlegs.addchild(namemap.get("llowlegf")); leftlegs.addchild(namemap.get("llowlegm")); leftlegs.addchild(namemap.get("llowlegr")); leftlegs.addchild(namemap.get("lfootf")); leftlegs.addchild(namemap.get("lfootm")); leftlegs.addchild(namemap.get("lfootr")); /* build the graph --> right legs */ rightlegs.addchild(namemap.get("ruplegf")); rightlegs.addchild(namemap.get("ruplegm")); rightlegs.addchild(namemap.get("ruplegr")); rightlegs.addchild(namemap.get("rmidlegf")); rightlegs.addchild(namemap.get("rmidlegm")); rightlegs.addchild(namemap.get("rmidlegr")); rightlegs.addchild(namemap.get("rlowlegf")); rightlegs.addchild(namemap.get("rlowlegm")); rightlegs.addchild(namemap.get("rlowlegr")); rightlegs.addchild(namemap.get("rfootf")); rightlegs.addchild(namemap.get("rfootm")); rightlegs.addchild(namemap.get("rfootr")); /* build the graph --> remaining body */ body.addchild(namemap.get("antena")); body.addchild(namemap.get("antenar")); body.addchild(namemap.get("wing")); body.addchild(namemap.get("abdomen")); body.addchild(namemap.get("head")); body.addchild(namemap.get("prothorx")); body.addchild(namemap.get("eyes")); body.addchild(namemap.get("lpalp")); body.addchild(namemap.get("rpalp")); /* build the graph --> roach */ roach.addchild(leftlegs); roach.addchild(rightlegs); roach.addchild(body); /* enable transform capability (it is not enabled by default) */ enabletransformcapability(leftlegs, rightlegs, body, roach); return roach; } void enabletransformcapability(transformgroup... parts) { for (transformgroup part : parts) { part.setcapability(transformgroup.allow_transform_write); } } Note that i have declared the transform groups locally, but on your program you will have to declare them globally or keep a reference to them somewhere if you plan to add interaction to your model. we will configure the camera (actually a view) and add lights . I did a fairly simple hierarchy because the movement this cockroach will do is just as simple. in my assignment i had to do an interaction in which the legs would articulate, which implied in a different (i.e. more complex) setup for the hierarchy of the legs. Appearance The loaded cockroach is quite pale since no material descriptors were associated with it, but this is not a problem, as you can define your textures for each component of your graph. you must read the material javadoc to understand what is being done here. To save some effort, I will declare some constants for ambient, emissive and specular light colors. the user may choose the diffuse color - the light which is emitted when the object is under the influence of some light. import javax.vecmath.color3f; private static final color3f specular_light_color = new color3f(color.white); private static final color3f ambient_light_color = new color3f(color.light_gray); private static final color3f emissive_light_color = new color3f(color.black); Now you can create a method that returns an apperance based on a given color : import javax.media.j3d.material; import javax.media.j3d.appearance; appearance getappearance(color color) { appearance app = new appearance(); app.setmaterial(getmaterial(color)); return app; } material getmaterial(color color) { return new material(ambient_light_color, emissive_light_color, new color3f(color), specular_light_color, 100f); } It's possible to use an image as a texture, but there are some constraints: the image must be equal in width and height and must be a power of 2. If you have ever used swing, you know you have to pass an instance of component to mediatracker if you want to track the loading of an image. loading a texture uses a similar process: import javax.media.j3d.texture2d; import com.sun.j3d.utils.image.textureloader; appearance getappearance(string path, component canvas, int dimension) { appearance appearance = new appearance(); appearance.settexture(gettexture(path, canvas, dimension)); return appearance; } texture gettexture(string path, component canvas, int dimension) { textureloader textureloader = new textureloader(path, canvas); texture2d texture = new texture2d(texture2d.base_level, texture2d.rgb, dimension, dimension); texture.setimage(0, textureloader.getimage()); return texture; } Applying the material: scene cockroach = getscenefromfile("roach_mod.obj"); map namemap = cockroach.getnamedobjects(); color brown = new color(165, 42, 42); appearance brownappearance = getappearance(brown); namemap.get("wing").setappearance(brownappearance); a material responds to different light positions As far as I've tested, if you assign a texture instead of a material, the object will not respond to different light configurations, instead, it will look like being constantly illuminated. roach with a texture Lights As you have seen, we still need to add two lights and one camera (a view). if you have read the , you have seen a directional light being added to the root of the scene. it's interesting to make the light go with the roach wherever it goes if you don't want it to get completely black after walking out of the reach of the light - in this case you will need to add your lights as leafs on the same node which contains the object you want to illuminate. On the other hand, if you want your object to become shadowed as it moves, you should add the lights to a node other than the one you used to add the model. Except from finding the right vector to point the light to your object, creating and configuring lights is mostly simple. the following figure demonstrates how to construct an ambient light and a directional light: import javax.media.j3d.directionallight; import javax.media.j3d.ambientlight; color3f directionallightcolor = new color3f(color.blue); color3f ambientlightcolor = new color3f(color.white); vector3f lightdirection = new vector3f(-1f, -1f, -1f); ambientlight ambientlight = new ambientlight(ambientlightcolor); directionallight directionallight = new directionallight(directionallightcolor, lightdirection); bounds influenceregion = new boundingsphere(); ambientlight.setinfluencingbounds(influenceregion); directionallight.setinfluencingbounds(influenceregion); Why do you need an influence region? for the same reason you need clipping: to avoid doing useless calculations. see the light javadoc for more information. camera If you want to view your scene on different angles, you will need a camera. java 3d uses a view based model - there are no camera objects, but a viewplatform object. Whenever you want to change the view of your scene, all you have to do is to change parameters on the viewplatform object. If you are using simpleuniverse to facilitate the view configuration of your program, you don't need to add any viewplatform instance to the root node because simpleuniverse has already added that for you. the viewplatform created by simpleuniverse is inside a multitransformgroup , which you can obtain via view ing platform . The following code demonstrates how to obtain this multitransformgroup and use it to change the view of the scene: import com.sun.j3d.utils.universe.viewingplatform; /* you don't have to create a viewingplatform if you are using simpleuniverse */ viewingplatform vp = universe.getviewingplatform(); /* you don't need to add the vp to a transformgroup because the vp is already added in a multitransformgroup; 0 is the topmost transformgroup */ transformgroup vpgroup = vp.getmultitransformgroup().gettransformgroup(0); /* you can transform the view platform as you do with other objects */ transform3d vptranslation = new transform3d(); vector3f translationvector = new vector3f(1.9f, 1.2f, 6f); vptranslation.settranslation(translationvector); vpgroup.settransform(vptranslation); example: translation vectors used on the viewplatorm 0.0, -1.2, 6.0 1.9, 1.2, 6.0 0.0, 1.2, 6.0 -1.9, 1.2, 6.0 Do not confuse viewplatform with view ing platform - the latter is a convenience class used to "set up the view side of the graph" - it contains a viewplatform . Recommended reading: viewingplatform javadoc , viewplatform javadoc Background Unless you want your background to be plain black, you should specify one. just remember to always add the background to the root node of your scene; add it anywhere else and you will get an undesirable illegalsharingexception . color background import javax.media.j3d.background; /* a dull gray background */ background background = new background(new color3f(color.light_gray)); /* incluencregion is a boundingsphere. see the "lights" section for details */ background.setapplicationbounds(influenceregion); /* root is a branchgroup, root node of your scene object */ root.addchild(background); Image background textureloader t = new textureloader("leaves.jpg", canvas); background background = new background(t.getimage()); background.setimagescalemode(background.scale_repeat); // tiles the image background.setapplicationbounds(influenceregion); root.addchild(background); This static background is quite boring. If you are looking for something more interesting, such as a celestial sphere, you should use apply a geometry to a background. you can find examples on java2s website. Recommended reading: background javadoc Interacting with the model Now it's time to use the transformgroup references you've kept a while ago. You will use them to control the movement of the model. The cockroach will do a very silly movement: the left legs will move forward while the right legs stand still, then the right legs move forward while left legs stand still; the body will always move a little bit forward on every movement. it's far from realistic, but you can derive more complex movements if you learn this one. (if you're concerned, as far as my assignment, the movement was more complex than that...) the class behavior will be used to interact with the model. The behavior class is like a listener - you have to implement it to achieve the desired reaction. It has to be activated every time it's used, or it won't react to the next stimulus . The stimulus used on this section will be a key press, but you can use many others - check wakeupcriterion 's direct known subclasses to check for other options. After implementing your behavior subclass, all you have to do is to add it on the node you want to animate. Instance variables /** groups that will be animated. */ transformgroup[] groups; /** used to transform the groups you will animate. */ transform3d[] transforms; /** used to translate the groups you will animate. */ vector3f[] translations; /** type of event for which groups will react. */ wakeuponawtevent wake; /** increments 1 every time the user hits a key. */ int hitcount; /** decides which group will be animated based on the hitcount. */ int bodypartindex; Constructor simpletripodmovement(transformgroup... groups) { this.groups = groups; // you can add a groups count security check if you will wake = new wakeuponawtevent(keyevent.key_pressed); // you decide which key later translations = new vector3f[groups.length]; transforms = new transform3d[groups.length]; for (int i = 0; i < groups.length; i++) { translations[i] = new vector3f(0f, 0f, 0f); transforms[i] = new transform3d(); } } Implementation of initialize public void initialize() { // overriden method wakeupon(wake); // inherited method } Implementation of processstimulus public void processstimulus(enumeration enumeration) { keyevent k = (keyevent) wake.getawtevent()[0]; /* moves only if the key pressed is the right directional key and if the hit count is a multiple of 4 */ if ((k.getkeycode() == keyevent.vk_right) && (hitcount++ % 4 == 0)) { /* selects the body part to be moved */ bodypartindex = (bodypartindex + 1) % 3; /* moves 0.1 on z axis */ translations[bodypartindex].set(translations[bodypartindex].x, translations[bodypartindex].y, translations[bodypartindex].z + 0.1f); transforms[bodypartindex].settranslation(translations[bodypartindex]); groups[bodypartindex].settransform(transforms[bodypartindex]); } /* if you don't put it here, it won't respond the next time you press a key */ wakeupon(wake); } Applying the behavior /** * adds a simple tripod movement to the given roach. * * @param parts parts that will be animated * @param roach supernode of parts * @param bounds world bounds, the smae used for lighting */ void addbehavior(transformgroup[] parts, transformgroup roach, bounds bounds) { behavior behavior = new cockroachbehavior(parts); /* behavior will not work if you don't set the scheduling bounds! */ behavior.setschedulingbounds(bounds); roach.addchild(behavior); } As you have probably noticed, this class is tightly coupled with the objects it animates, but that is predictable; from behavior 's javadoc: the application must provide the behavior object with references to those scene graph elements that the behavior object will manipulate. The application provides those references as arguments to the behavior's constructor when it creates the behavior object. alternatively, the behavior object itself can obtain access to the relevant scene graph elements either when java 3d invokes its initialize method or each time java 3d invokes its processstimulus method. Recommended reading: behavior javadoc Resources cockroach object [you may have to convert it] cockroach wings texture cockroach head texture ground texture leaves background source code [you will need to download the model separately] executable jar [you will need to download the model separately] executable jar + model complete project [src + resources] References java3d javadoc com.sun.j3d.* packages javadoc java3d application tutorial from sun a basic hierarchical model of the top part of a human torso
January 26, 2008
by Dalton Filho
· 65,048 Views
article thumbnail
Multiple Backgrounds: Oh, What a Beautiful Thing.
The current spec for CSS3 includes support for multiple backgrounds in the background property. This is going to be fantastic for semantically-minded CSS developers. Many of the extra hooks that get thrown into HTML are there only to help out extra background images. Think about this common technique for blockquotes. This is some blockquoted text. The extra span in there is completely un-semantic, but it is often used so that you can get an extra background image in there. One for the quote mark in the upper left and one for the quote mark in the lower right: [img_assist|nid=349|title=|desc=|link=none|align=center|width=500|height=149] Blockquote example from here. With multiple backgrounds the extra hook is not needed. You can apply both the upper left and lower right image both to the blockquote element. Here is what the CSS will look like: blockquote { background: url('left.jpg') top left no-repeat, url('right.jpg') top right no-repeat, url('middle.jpg') top center repeat-x; } Notice you can set both the location and how it will repeat in each of the comma-separated backgrounds. I like the clean syntax of this, but it does present a problem. It is not backwards-compatible whatsoever. Older browsers that are not supporting this will just see no background at all, instead of for example, just the first image which would make sense. That means we can't just start using this in a forward-enhancement movement, unless we declare browser-specific stylesheets for the browsers that support it. At the time of this writing, only Safari is supporting multiple backgrounds. Here is a link to a quick example of some buttons utilizing multiple backgrounds in order to shrink and grow seamlessly. Remember, Safari-only right now. Remind you of anything? Sliding doors. Multiple backgrounds completely absolute sliding doors. Better semantics... No more complicated work-around techniques.... Oh, what a beautiful thing.
January 21, 2008
by Chris Coyier
· 10,965 Views
article thumbnail
A Groovy DSL from Scratch in Two Hours
Through DZone I found Architecture Rules, a lovely little framework that abstracts JDepend. Architecture Rules is configured via its own XML schema.
January 20, 2008
by Steven Devijver
· 63,068 Views · 5 Likes
article thumbnail
GroovyShell and memory leaks
Time to talk about creating new classes at runtime in Groovy. There seems to be some fear, uncertainty and doubt about memory leaks and evaluating code with Groovy in the form of calling an eval() method. The code that seems to cause consternation is this: def shell = new GroovyShell() 1000.times { shell.evaluate "x = 100" } The groovy.lang.GroovyShell instance will call the parseClass() method on an internal groovy.lang.GroovyClassLoader instance, which will create a 1000 new classes. The classes will all extend the groovy.lang.Script class. With every new Class created a little bit more memory will be used. As long as the groovy.lang.GroovyShell instance and thus its internal groovy.lang.GroovyClassLoader instance is not garbage collected this memory will remain occupied, even if you don't keep a reference to these classes. This is standard Java ClassLoader behavior. So, how to solve this problem? Well, ClassLoaders in Java are somewhat hard to handle, but it's not so hard once you understand how they work. But lets also consider the root of the problem, namely the fact that Groovy creates a new Class for each script that is evaluated. Before answering why Groovy always creates new Class objects when evaluating code let's first try to fix the code above. One way to fix it is this: def shell = new GroovyShell() 1000.times { shell.evaluate "x = 100" } shell = null By setting the shell variable to null, will the GroovyClassLoader instance be garbage collected? We can guarantee it will in this bit of code. But then again this code does not do anything useful :-) Here's another way to fix it: def shell = new GroovyShell() def script = shell.parse "x = 100" 1000.times { script.run() } By evaluating - parsing - the code only once and calling the run() method on the groovy.lang.Script instance a 1000 times we only use 1/1000th of the memory :-) The parse() method returns a groovy.lang.Script instance. But again, let's consider a more realistic use case. After all, the article that originally critized Groovy for causing memory leaks implies that evaluating code any number of times is a valid requirement in entreprise applications. Let's say it's more of a corner case but still, the functionality is there and it can solve real-world problems. Evaluating Groovy code may be particularly useful and critical when evaluating code on demand. This could happen when an application reads code from file or a database to execute custom business logic. Let's consider the case where developers create a DSL or Domain Specific Language like this: assert customer instanceof Customer assert invoice instanceof Invoice letterHead { customer { name = customer.name address { line1 = "${customer.streetName}, ${customer.streetNumber}" line2 = "${customer.zipCode} ${customer.location}, ${customer.state}" } } invoiceSummary { number = invoice.id creationDate = invoice.createdOn dueDate = invoice.payableOn } } To parse this DSL developers wrote this code (using the iText PDF library): import com.lowagie.text.* import com.lowagie.text.pdf.* class LetterHeadFormatter { static byte[] createLetterHeadForInvoice(Customer cust, Invoice inv, String dsl) { Script dslScript = new GroovyShell().parse(dsl) dslScript.binding.variables.customer = cust dslScript.binding.variables.invoice = inv Document doc = new Document(PageSize.A4) def out = new ByteArrayOutputStream() PdfWriter writer = PdfWriter.getInstance(doc, out) doc.open() dslScript.metaClass = createEMC(writer, dslScript) dslScript.run() doc.close() return out.toByteArray() } static ExpandoMetaClass createEMC(PdfWriter writer, Script script) { ExpandoMetaClass emc = new ExpandoMetaClass(script.class, false) emc.letterHead = { Closure cl -> PdfContentByte content = writer.directContent cl.delegate = new LetterHeadDelegate(content) cl.resolveStrategy = Closure.DELEGATE_FIRST cl() } emc.initialize() return emc } } (Check the attachements of this article to download this code. Read the README.txt file if you want to run the load test yourself, and please report back the results. Also, check the PDF file for the output of the DSL.) On line 6 the parse() method is called. I wrote a load test that calls the createLetterHeadForInvoice() method 1 million (!) times (with regular calls to System.gc()). On my Windows XP machine, when I run the load test with Ant the java process memory usage fluctuates between 32 and 37Mb and remains stable over the course of several hours. Are the GroovyShell and internal GroovyClassLoader instances garbage collected? Yes they are. Is there a memory leak? No. So why does Groovy create Classes when evaluating scripts? Every bit of code in Groovy is a java.lang.Class. This means that it's loaded by a java.lang.ClassLoader and remains in memory unless the ClassLoader can be garbage collected. Why isn't a Class object garbage collected as soon as it's no longer used? Why does the ClassLoader itself have to be garbage collected before the classes it has loaded are removed from memory? If classes would be automatically discarded and reloaded their static variables and static initialization would be executed on each reload. That would be quite surprising and unpredictable. That's why ClassLoaders have to keep hold of their classes, to assure predictable behavior. There may be other technical reasons, but this is the most obvious one. Once the ClassLoader object itself gets garbage collected (because it's no longer referenced in any stack) the garbage collector will attempt to unload all its Class objects. Obviously, creating a lot of classes at runtime in the same ClassLoader will increase the memory usage and will typically create a memory leak. On the other hand, since every Groovy class is a real Java Class (without exception) you don't have to make the distinction. In conclusion: there is no memory leak in GroovyShell or GroovyClassLoader. Download the sample code and verify for yourself. Your code can create a memory leak by the way ClassLoaders are used - either explicitly by your code or implicitly.
January 19, 2008
by Steven Devijver
· 33,218 Views · 4 Likes
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,920 Views · 1 Like
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,248 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,614 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,308 Views
article thumbnail
Javascript Sprintf
September 19, 2007
by Snippets Manager
· 477 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,149 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,512 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,603 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,498 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,175 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,207 Views
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,544 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,953 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
· 45,996 Views · 2 Likes
  • Previous
  • ...
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 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
×