ASP Array helper functions 07 May 2002 at 00:00
'______________________________________________________________________________
' Determines if an array contains a specific value
' @param	m_arr		Array to be searched
' @param	m_val		Value to be searched for in array
' @return	True if value found in array, else false
Function ArrayContains(ByVal m_arr, ByVal m_val)
  Dim m_i
  If NOT IsArray(m_arr) Then
    ' raise custom error here
  End If
  For m_i = 0 To UBound(m_arr)
    If m_arr(m_i) = m_val Then
      ArrayContains = True
      Exit Function
    End If
  Next
  ArrayContains = False
End Function

'______________________________________________________________________________
' removes duplicates from an array
Function RemoveDuplicates(ByVal m_arr)
  Dim m_newArr()
  Redim Preserve m_newArr(0)
  Dim m_i
  If NOT IsArray(m_arr) Then
    ' raise custom error here
  End If
  For m_i = 0 To UBound(m_arr)
    If NOT ArrayContains(m_newArr, m_arr(m_i)) Then
      Redim Preserve m_newArr(UBound(m_newArr) + 1)
      m_newArr(UBound(m_newArr)) = m_arr(m_i)
    End If
  Next
  RemoveDuplicates = m_newArr
End Function

'______________________________________________________________________________
' converts a string to an array, where each character is an element in the array
Function StringToArray(ByVal m_str)
  Dim m_arr()
  Dim m_i
  ' check that string was passed
  If m_str = "" Then
    ' raise customer error here
  End If
  For m_i = 1 To Len(m_str)
    Redim Preserve m_arr(m_i)
    m_arr(m_i) = Mid(m_str, m_i, 1)
  Next
  StringToArray = m_arr
End Function
Java : Reading properties/settings from a file 14 May 2002 at 00:00
You can load properties stored in a key=value format (each property on a new line) using an InputStream and a Properties object:
  BufferedInputStream bis = null;
  Properties prop = null;
  String strFile = "package" + File.separator + "properties.file";
  // load properties file
  try {
    bis = new BufferedInputStream(new FileInputStream(new File(strFile)));  
  }
  catch (FileNotFoundException fnfe) {
    // throw error here
  }
  // load properties variables with details in the file
  prop = new Properties();
  try {
    prop.load(bis);
    // load all the properties
    strProperty = prop.getProperty("keyname");	
    // close the input stream
    bis.close();
  }
  catch (Exception ioe) {
    // throw error
  }
Java Undo / Redo functionality 14 May 2002 at 00:00
Undo / Redo functionality can be added to document classes in the javax.swing.text package very easily. All you need to do is add a listener for changes, and use a UndoManager to alter those changes.

For example, you have a JTextPane which is using a DefaultStyledDocument as it's document source:

....
public class MyTextPane extends JTextPane {
  
  // create undo manager
  private UndoManager undo = new UndoManager();
  
  public MyTextPane() {
    ...
    // add undoable edit listener to the document
    this.getStyledDocument().addUndoableEditListener(new MyUndoableEditListener());
  }

  // add undo method
  public void undo() {
    undo.undo();
  }

  // add redo method
  public void redo() {
    undo.redo();
  }

  // create private class for listening to document changes
  private class StdUndoableEditListener implements UndoableEditListener {
    public void undoableEditHappened(UndoableEditEvent e) {
      // add each edit to the undo manager
      undo.addEdit(e.getEdit());
    }
  }
}
Java Drag and Drop 16 May 2002 at 00:00
If, for example you want an application to support the dragging of files from a file list to your component, and your component to open those files, use the following code:

//import drag and drop and datatransfer packages
import java.awt.dnd.*;
import java.awt.datatransfer.*;

public class MyClass extends AnotherClass {

  // declare variables - including a DropTarget object to which you will 
  // link a target component
  private DropTarget dropTarget = null;

  public MyClass() {
    // in constructor or initialisation method, initialise the DropTarget 
    // object and link it to your target component
    dropTarget = new DropTarget();
    try {
      dropTarget.addDropTargetListener(new DragDropListener());
    }
    catch (TooManyListenersException tmle) {
      // process error
    }
    yourComponent.setDropTarget(dropTarget);
  }
Write a private class for handling the drop action - note that different operating systems receive different file flavours - e.g. windows gets files as a filelist whereas Linux gets a long string
  private class DragDropListener extends DropTargetAdapter {
		
  public void drop(DropTargetDropEvent dtde) {
    Transferable transferable = dtde.getTransferable();
    dtde.acceptDrop(dtde.getDropAction());
    DataFlavor[] flavors = transferable.getTransferDataFlavors();			
    boolean ok = false;
    for (int i=0; i<flavors.length; i++) {
      DataFlavor df = flavors[i];
      if (df.equals(DataFlavor.javaFileListFlavor)) {
        try {
          java.util.List<File> list = (java.util.List<File>)transferable.getTransferData(DataFlavor.javaFileListFlavor);
          for (int j=0; j<list.size(); j++) {
            openFile(list.get(j).getAbsolutePath());
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
        ok = true;
      }
      else if (df.equals(DataFlavor.stringFlavor)) {
        try {
          String file = (String)transferable.getTransferData(DataFlavor.stringFlavor);
          String[] files = file.split(System.getProperty("line.separator"));
          for (int j=0; j<files.length; j++) {
            file = files[j].trim().replace("file://", "");
            openFile(file);
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
        ok = true;
      }
    }
    dtde.dropComplete(true);
    if (!ok) {
      // raise error
    }
  }
PHP : Redirecting Scripts 21 May 2002 at 00:00
In order to transfer the user to another page programatically, write a location header to the page, for example:
  // turn buffering on, so no other content is written to page before the 
  // redirect headers
  ob_start();
  ..
  ..
  // remove data in buffer and write header
  ob_clean();
  header("Location: mypage.htm");
  exit;
PHP Cookies 23 May 2002 at 00:00
Setting cookies, is relatively simple, using the following code:
  setcookie("cookiename", $cookie_val, time() + 3600, "", "");
Note: You should set all the above parameters to ensure that the cookie gets written in Internet Explorer 5+. Also, the PHP manual mentions another cookie for secure connections as an additional parameter. For some reason, setting this parameter causes the cookie not to be written to IE 5+, so be sure not to set it.

To retrieve the cookie, use the following:

  $HTTP_COOKIE_VARS["cookiename"]

To delete the cookie:

  // set the cookie expiry date to some time in the past
  setcookie ("cookiename", "", time() - 3600, "", "");
Listing Linux Fonts 31 May 2002 at 00:00
To view the fonts installed on your machine, you can use the xlsfonts command. This lists all the fonts available to the XWindows system on your machine.

To view one of these fonts, use the xfd command.

Example:

To view the olglyph-19 font, you would issue the following command from your XWindows terminal:

  xfd -fn olglyph-19
PostgreSQL Sequences 31 May 2002 at 00:00
Sequence objects (also called sequence generators or just sequences) are special single-row tables created with CREATE SEQUENCE. Sequential fields in tables are linked to sequence objects, so you can insert integers into these fields manually, or leave it up to the sequence object to create the next integer for you.

This is useful when you want the sequential number for further processing - you can manually retrieve the next number in the sequence using

SELECT nextval('sequence_name');
which returns a single-row recordset with one field called "nextval". To insert your new record into the table with the sequential field, just manually insert this value instead of omitting the normally self-incrementing field.