Good Grief

Just another SquidPower.com weblog

Find HTML Tags inside String using RegEx

Posted by David Alden on August 15th, 2007

Java method that returns a boolean. Finds HTML tags inside a String passed into the method. Uses RegEx, Pattern, Matcher

public static boolean hasHtmlTags(String content) {
   if (content != null && !content.equals(“”)) {
     //String toTest = “<html><title>some text</title></html>”;
    
String regEx = “<\\w+[^>]*>(.*?)</\\w+>”;
     String regEx2 = “<\\w+>[\\w*\\s*]*</\\w+>”;
     Pattern p = Pattern.compile(regEx, Pattern.
CASE_INSENSITIVE);
    
Pattern p2 = Pattern.compile(regEx2, Pattern.CASE_INSENSITIVE);
     Matcher m = p.matcher(content);
     Matcher m2 = p2.matcher(content);
    
if (m.find() || m2.find()) {
      
return true;
     }
   }
  
return false;
}

Posted in Java | No Comments »

Copy an array of objects instead of referencing

Posted by David Alden on July 11th, 2007

If you want to copy an entire array of objects, you can’t just say array2 = array1, this will make a reference to the array and all of it’s objects. The best way to make a copy of an array is through a “deep clone”. This function makes copies of all objects inside of an array instead of making references to them

public function clone( source : Object ) : * {
  
var byteArray : ByteArray = new ByteArray();
   byteArray.writeObject(source);
   byteArray.position = 0;
  
return(byteArray.readObject());
}

Posted in Flex2 ActionScript 3.0 | 2 Comments »

A few handy SQL date functions

Posted by David Alden on November 21st, 2006

– to get the last day of the month for the date specified
– try different years, it even works for leap years
SELECT LAST_DAY(’1-FEB-2006′) FROM dual

– to add 1 month to a date
SELECT ADD_MONTHS(’1-FEB-2005′, 3) FROM dual

– to SUBTRACT 1 month from a date
SELECT ADD_MONTHS(SYSDATE, -1) FROM dual

– to add a year to a date
SELECT ADD_MONTHS(SYSDATE, 12) FROM dual

– to add 1 day to a date
SELECT SYSDATE+1 FROM dual

– to add 1 hour to a date
SELECT SYSDATE+1/24 FROM dual

– to add 2 hours to a date
SELECT SYSDATE+2/24 FROM dual

– to subtract 3 days from a date
SELECT TO_DATE(’1-FEB-2005′)-3 FROM dual

– to subtract 3 hours from a date
SELECT SYSDATE-3/24 FROM dual

– to_date function to format a date
SELECT TO_DATE(SYSDATE, ‘DD-MM-YYY’) FROM dual

– to_char function to convert months to a character format
SELECT TO_CHAR(SYSDATE) FROM dual

– to get the time zone, also current_date can be used instead of sysdate
SELECT SESSIONTIMEZONE, current_date FROM dual

– select a record that has a date between 2 dates
SELECT ‘1-FEB-2006′
FROM dual
WHERE ‘2-FEB-2006′ BETWEEN ‘1-JAN-2006′ AND SYSDATE

– round to the nearest year
SELECT ROUND(TO_DATE(’27-OCT-2000′),’YEAR’) FROM DUAL

– round DOWN to the nearest month
SELECT ROUND(TO_DATE(’15-FEB-2005′),’MONTH’) FROM dual

– round UP to the nearest month
SELECT ROUND(TO_DATE(’16-FEB-2005′),’MONTH’) FROM dual

Posted in SQL | No Comments »

One checkbox to select all

Posted by David Alden on August 24th, 2006

This code is similar to the code on Hotmail and Yahoo mail that allows you to select or unselect all checkboxes with just one checkbox. It uses the document.getElementByTag(”input”), and then filters out the inputs for “checkbox” here’s the code and a sample image:

Check All Checkboxes with one checkbox

<html>
<html>
<script>
  function checkAllCheckboxes(){
  var allCheckboxes = document.getElementsByTagName(”input”);
  for(var i = 0; i < allCheckboxes.length; i++){
    if(allCheckboxes[i].type == “checkbox” && allCheckboxes[i].name != “checkAllBoxes”){
       allCheckboxes[i].checked = document.getAssessments.checkAllBoxes.checked;
    }
  }
}
</script>

<body>
<form name=”getAssessments” method=”post”>
<input type=”checkbox” name=”checkAllBoxes” onclick=”checkAllCheckboxes();”/>
<br/>
<input type=”checkbox” name=”evaluationVersion_152″/>option 1
<br/>
<input type=”checkbox” name=”evaluationVersion_153″/>option 2
<br/>
<input type=”checkbox” name=”evaluationVersion_154″/>option 3
<br/>
<input type=”checkbox” name=”evaluationVersion_155″/>option 4
<br/>
<input type=”checkbox” name=”evaluationVersion_163″/>option 5
<br/>
<input type=”checkbox” name=”evaluationVersion_165″/>option 6
</form>
</body>
</html>

Posted in JavaScript | 2 Comments »

WinCvs Commit 04

Posted by David Alden on August 23rd, 2006

This isn’t talking about the 30 year mortgage you just signed a year ago and want to get out of already because none of your toilets flush properly. Committing in WinCvs is basically taking the copy of the changes you’ve made to a file on your computer and replacing them with the most recent copy that is on the server. Although committing in WinCvs is pretty serious, you can usually revert to an old copy if you accidentally commit a file that you later realize you shouldn’t have. The server is supposed to keep track of all the versions that have been committed. To commit a file, you need to first open WinCvs, right click a file in the file list pane that has a red icon to the left of it, then select Commit from the menu. The following image is what you should see.

WinCvs Tutorial Commit 1

Enter a brief message into the text area that describes the changes that you made to the file. This is an important part of committing files to CVS so that your coworkers can know what changes were made to the files that they’re updating. It’s also good for personal use in case you make a change and then two months later come back and want to know why you made the change you made.

Click OK and your file is now the most current version on the server, the same file that your coworkers will get when they update their project.

Posted in CVS, Tutorials | No Comments »

WinCvs ExamDiff 03

Posted by David Alden on August 23rd, 2006

ExamDiff is a visual file and directory comparison tool for Microsoft Windows.

ExamDiff features a double-pane view that allows for side-by-side comparisons, with color-coded line numbers indicating whether each line is added, deleted, or changed. You can’t edit the files directly in ExamDiff but you can easily edit either file with Notepad or the text editor of your choice by selecting one of the numerous toolbar buttons.

ExamDiff should already be installed on your (squidpower) computer and if you want you can open it by going to start > All Programs > ExamDiff > ExamDiff

But ExamDiff may be used inside of WinCvs to compare two files. Usually the one that is on the server is compared with the one that is on your computer that you have modified. You first need to set up WinCvs to tell it where your ExamDiff program is at so that it can open up the two files that you want to compare. First open WinCvs then click the Admin menu and select Preferences… A dialog box should open and then you should select the WinCvs tab.

WinCvs Tutorial ExamDiff 1

Make sure that your dialog box looks similar to the image above, that the External diff: check box is selected and the text box should have the path C:\Program Files\ExamDiff\ExamDiff.exe (or wherever your ExamDiff program is located) Your default editor can really be whatever editor you want, but I personally suggest to leave it as a simple text editor built into windows such as Notepad or WordPad. Click OK to save the settings. Now you’re all set up to compare files using WinCvs.

In the same folder as this tutorial there is a file named CVSTutorial.txt Open it in Notepad or Wordpad, which ever you prefer. When you open this file you should see the following text: DELETE THIS TEXT-Do what it says by deleting the text you see. Save and close the file.-Open WinCvs, in the left navigation pane make sure that you’re in the folder C:\Work\training\CVS Training (new)

-In the right file list pane, you should see the file CVSTutorial.txtwith a red icon to the left of it, similar to the following image

WinCvs Tutorial ExamDiff 2

Right click the file CVSTutorial.txt and select Diff from the menu, the screen that displays should be similar to the following image:

WinCvs Tutorial ExamDiff 3

Make sure the use the external diff checkbox is selected and click OK the screen that displays should be the ExamDiff window and should look something similar to the following image:

WinCvs Tutorial ExamDiff 4

You can see 2 panes here, the one on the left is the file pulled from the server being compared to the one on the right, your local copy that you just changed. This program is basically just used to compare 2 files, the original and the one you’ve changed. This can also be like having a backup copy of the file(s) you’re working on. Let’s imagine that you messed up, you didn’t really want to delete the text that you just deleted and you want to get the un-messed up copy from the server. All you do is (after closing the ExamDiff window) right click the file CVSTutorial.txt and select Update from the menu you should see something similar to the image on the following page: 

WinCvs Tutorial ExamDiff 5

Making sure the Get the clean copy checkbox is selected click OK. Get the clean copy basically means that no matter what changes you’ve made on your local machine, the file is to be replaced with whatever copy is on the server. So, when you clicked OK your file was replaced with the copy that you saw in ExamDiff that still had the text DELETE THIS TEXT. If you open that same text file you’ll see that same text in there that you deleted before you got the update. ExamDiff is very useful in comparing the file that you’re working on with the file on the server to see what changes you’ve made. Use if often! Personally, I use it almost every time I make a change to a file before I commit it to the server. If you do that you’ll have less conflicts with the same files that you and your co-workers are changing.

Posted in CVS, Tutorials | No Comments »

WinCvs Login 02

Posted by David Alden on August 23rd, 2006

Let’s start out this tutorial first by checking out an existing repository, the Training repository. Go to the WinCvs window and first login to the correct repository by clicking on the Admin menu and then selecting Login…

The following screen is what you should see:

WinCvs Tutorial Login 1

Now select the … button at the bottom right of this window. The following screen is what you should see:

WinCvs Tutorial Login 2

In the Protocol combo box select pserver
In the Repository path type /cvs/training
When you selected pserver new options should come up in the Keywords TextArea. Enter the following:
Username: your username
Password: your password
Hostname: the server on which CVS is running

Click OK and it should take you back to the previous window, but now with the CVSROOT filled in with the text you just entered. Click OK again and it will log you into the training repository. In the bottom part your WinCvs window (it’s really called the Output) you should see the following.

WinCvs Tutorial Login 3

Something similar may display in the Output window, but what you’re really looking for is ***** CVS exited normally with code 0 ***** Basically code 0 tells you that everything worked correctly and there were no errors. Now that you’re logged into the training repository you can check out the training module. To do this click on the Remote menu and select Checkout module…The following screen is what you should see:

 WinCvs Tutorial Login 4

In the Module name and path on the server: text box type. In the Local folder to checkout to: type c:\work\training Click the … button to the right of the CVSROOT text box, a window should come up, fill out the fields in the same way you did for the login. Click OK in this window and the next. When you click OK in the 2nd time a warning box should come up asking you to create the folder c:\work\training. Select Yes. You should now see a bunch of green and orange lines scrolling in the Output window of WinCvs. Don’t get scared, you haven’t broken anything just yet (unless you see smoke coming out of your monitor), this is just the files being copied from CVS to your hard drive. The image on the next page is more or less what you should see in your Output window.

WinCvs Tutorial Login 5

Again, you should always check the Output window for code 0 to make sure there are no errors. Go on to the next tutorial (Exam Diff)

Posted in CVS, Tutorials | No Comments »

WinCvs Intro 01

Posted by David Alden on August 23rd, 2006

The CVSNT Versioning System implements a version control system: it keeps track of all changes in a set of files, typically the implementation of a software project, and allows several developers to collaborate. It is compatible with and originally based on Concurrent Versions System (CVS), which has become popular in the open-source world and is released under the GNU General Public License. (http://en.wikipedia.org/wiki/WinCvs)

Here (squidpower), we currently (as of August 23, 2006) use a versioning system interface called WinCvs. It’s a program that you should use here everyday, if you fail to learn how to use it properly or even just decide not to use it for whatever reason, the programmers working on the same project as you may or may not get frustrated with you. If you didn’t understand all of the technical stuff in the first paragraph, then this program’s main function here at (squidpower) is to allow more than one person to work on the same project at the same time, and for each person working on the same project to keep updated on all changes that everyone is making. Personally, one of my favorite features of WinCvs is that it allows you to retrieve an old copy of code that you wrote and maybe lost or made a change that you later find out you didn’t want to make. It keeps a copy of every version that you submit to it for later retrieval. Now that you know what WinCvs does, open it up (assuming you’re using Windows XP and that WinCvs is already installed) by clicking on start -> All Programs -> WinCvs -> WinCvs. You should see a screen come up that looks similar to the following image.

CVS intro

Terminology

You should become familiar the following terms with when using and speaking to others about WinCvs. In an attempt to avoid confusion, here’s a short list of the most essential terms.

Repository
A repository is a central place where
data is stored and maintained. A repository can be a place where multiple databases or files are located for distribution over a network, or a repository can be a location that is directly accessible to the user without having to travel across a network. (http://en.wikipedia.org/wiki/Repository)

Checkout
Normally used to describe the first retrieval of an entire module from the repository.

Commit
Sending your modifications to the repository.

Export
Refers to extraction of an entire module from the repository, without any CVS administrative files: Exported modules will not be under CVS control.

Import
Normally refers to the process of creating a new module in the repository by sending an entire directory structure.

Module
A directory hierarchy. A software project normally exists as a single module in the repository.

Release
The version of an entire product.

Revision
The version of a single file.

Tag
A symbolic name given to a set of files at a specific time of development.

Update
Get other users’ modifications from the repository. Updates the local copy only. (The whole update process only functions properly if the other programmers working on the same project actually commit the files that they’re working on)

Please continue on to the Login Tutorial

Posted in CVS, Tutorials | No Comments »

Order Imports in Flex Builder 2

Posted by David Alden on August 16th, 2006

Flex Builder 2 allows you to organize your imports alphabetically. This is very handy when you begin to have several imports in one file. To do this open a Flex file or project in Flex Builder 2 and select all of the imports that you wish to sort. Right click the selection of imports go to “Source” and then “Organize Imports”

Another way to do this would be through the keyboard shortcut. Select all of the imports that you wish to sort and then on the keyboard press ctrl + shift + o

click attached image

 Order Imports in Flex Builder 2

Posted in Flex Builder 2 | No Comments »

Edit multiple fields with one RichTextEditor

Posted by David Alden on August 16th, 2006

( Flex 2 ActionScript 3.0 ) This code connects one Rich Text Editor to any amount of text-input fields. This is great if you want the ability for rich text editing in multiple fields but don’t want a rich text editor for each field. Here’s the code:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” height=”550″>
 
<mx:Script>
 <![CDATA[

 private var currentFocus : Object = null;

 private function syncText( event : Event ): void{
   var currentFocus : Object = currentFocus;
   if(currentFocus != null){
     currentFocus.htmlText = rte.htmlText;
   }
}

private function setCurrentFocus( event : MouseEvent ) : void {
  currentFocus = Object(event.currentTarget);
  rte.htmlText = currentFocus.htmlText;
}

]]>
</mx:Script>

  <mx:TextArea editable=”false” click=”setCurrentFocus(event)” />

  <mx:TextArea editable=”false” click=”setCurrentFocus(event)” />

  <mx:TextArea editable=”false” click=”setCurrentFocus(event)” />

  <mx:RichTextEditor id=”rte” change=”syncText(event)” />

  <mx:Label fontWeight=”bold” text=”***Select a TextArea, click in the Rich Text Editor and type text***” />

</mx:Application>

Posted in Flex2 ActionScript 3.0 | No Comments »