2010年2月28日 星期日

HTML中顯示HTML程式碼

問題: 如果想將 HTML 程式碼貼在 Blog, 而希望 Blog 不要作顯示的轉換, 如原本的呈現<a href ="www.google.com.tw">Google</a>, 而非 Google 連結, 該如何作??

答: 將角括弧 < 用 & l t ; 表示, 而 > 用 & g t ;表示
當然我們也可撰寫 script 作轉換, 或用文字編輯器取代.


此外有不錯的工具:
Online syntax highlighting
HTML Source Code Syntax Highlighter
SyntaxHighlighter

Java Web Start in JNLP Sample for Dynamic Tree Demo

1. Make a directory name JNLP in tomcat/webapps

2. Put the source code of DynamicTreeDemo.java and DynamicTree.java in this directory. Note The Dynamic Tree Demo is able to run application program, and we will wrap it to java web start format.

Add the DynamicTreeDemo.java
/*
* This code is based on an example provided by Richard Stanford,
* a tutorial reader.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class DynamicTreeDemo extends JPanel {
private int newNodeSuffix = 1;

public DynamicTreeDemo(JFrame frame) {
//create the components
final DynamicTree treePanel = new DynamicTree();
populateTree(treePanel);

JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
treePanel.addObject("New Node " + newNodeSuffix++);
}
});

JButton removeButton = new JButton("Remove");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
treePanel.removeCurrentNode();
}
});

JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
treePanel.clear();
}
});

//Lay everything out.
setLayout(new BorderLayout());
treePanel.setPreferredSize(new Dimension(300, 150));
add(treePanel, BorderLayout.CENTER);

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.add(addButton);
panel.add(removeButton);
panel.add(clearButton);
add(panel, BorderLayout.EAST);
}

public void populateTree(DynamicTree treePanel) {
String p1Name = new String("Parent 1");
String p2Name = new String("Parent 2");
String c1Name = new String("Child 1");
String c2Name = new String("Child 2");

DefaultMutableTreeNode p1, p2;

p1 = treePanel.addObject(null, p1Name);
p2 = treePanel.addObject(null, p2Name);

treePanel.addObject(p1, c1Name);
treePanel.addObject(p1, c2Name);

treePanel.addObject(p2, c1Name);
treePanel.addObject(p2, c2Name);
}

public static void main(String[] args) {
JFrame frame = new JFrame("DynamicTreeDemo");

Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1,1));
contentPane.add(new DynamicTreeDemo(frame));

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

frame.pack();
frame.setVisible(true);
}
}

//---------------------------
add this forThe
DynamicTree.class

/**
Class:
DynamicTree
*/
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;

public class DynamicTree extends JPanel {
protected DefaultMutableTreeNode rootNode;
protected DefaultTreeModel treeModel;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();

public DynamicTree() {
super(new GridLayout(1,0));

rootNode = new DefaultMutableTreeNode("Root Node");
treeModel = new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(new MyTreeModelListener());
tree = new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);

JScrollPane scrollPane = new JScrollPane(tree);
add(scrollPane);
}

/** Remove all nodes except the root node. */
public void clear() {
rootNode.removeAllChildren();
treeModel.reload();
}

/** Remove the currently selected node. */
public void removeCurrentNode() {
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
(currentSelection.getLastPathComponent());
MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
if (parent != null) {
treeModel.removeNodeFromParent(currentNode);
return;
}
}

// Either there was no selection, or the root was selected.
toolkit.beep();
}

/** Add child to the currently selected node. */
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();

if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}

return addObject(parentNode, child, true);
}

public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child) {
return addObject(parent, child, false);
}

public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child,
boolean shouldBeVisible) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);

if (parent == null) {
parent = rootNode;
}

//It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
treeModel.insertNodeInto(childNode, parent,
parent.getChildCount());

//Make sure the user can see the lovely new node.
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}

class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());

/*
* If the event lists children, then the changed
* node is the child of the node we've already
* gotten. Otherwise, the changed node and the
* specified node are the same.
*/

int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode)(node.getChildAt(index));

System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e) {
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}
}



3. Compile this code using the eclipse

4. Create a JAR file containing your application's class files and resources
# jar cvf DynamicTreeDemo.jar .
  • c mean create
  • t mean table of cotents
  • f mean filename
  • . this current directory

5. Create a JNLP file that describes how your application should be launched.
Here is the dynamictree-webstart.jnlp example
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+"
codebase="http://localhost:8080/JNLP/"
href="dynamictree-webstart.jnlp">
<information>
<title>Dynamic Tree Demo</title>
<vendor>Andy</vendor>
</information>
<resources>
<!-- Application Resources -->
<j2se version="1.6+"
href="http://java.sun.com/products/autodl/j2se"/>
<jar href="DynamicTreeDemo.jar" main="true" />

</resources>
<application-desc
name="Dynamic Tree Demo Application"
main-class="DynamicTreeDemo">
</application-desc>
<update check="background"/>
</jnlp>


here:
  • <jnlp codebase> mean base location for all relative URLs specified in href attributes in the JNLP file.
  • <jnlp href> mean The URL of this JNLP file itself. Java Web Start requires this attribute to be set in order for the application to be included in the Application Manager.
  • <jar herf>mean the URL of the JAR file. The <tt>jar</tt> file will typically contain Java classes that contain the code for the particular application, but can also contain other resources, such as icons and configuration files.
  • <jar main> indicates if this JAR file contains the class containing the <code>main</code> method of the RIA.
  • <application-desc main-class> is the class containing the main method, for this example is DynamicTreeDemo class.

6. Create the HTML page from which your application will be launched.

<html>
<head>
</head>
<body>
<a href="dynamictree-webstart.jnlp">lanuch jnlp</a>
</body>
</html>


7.Lanuch the tomcat and type http://localhost:8080/JNLP/


Reference:

2010年2月27日 星期六

Hadoop2

dss

hadoop

Hadoop

test

Test