* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
- *
+ �*
* Contributors:
* IBM Corporation - Initial API and implementation
**********************************************************************/
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+
/**
- * A Memento is a class independent container for persistence
- * info. It is a reflection of 3 storage requirements.
- *
- * 1) We need the ability to persist an object and restore it.
- * 2) The class for an object may be absent. If so we would
- * like to skip the object and keep reading.
- * 3) The class for an object may change. If so the new class
- * should be able to read the old persistence info.
- *
- * We could ask the objects to serialize themselves into an
- * ObjectOutputStream, DataOutputStream, or Hashtable. However
- * all of these approaches fail to meet the second requirement.
- *
+ * A Memento is a class independent container for persistence info. It is a
+ * reflection of 3 storage requirements.
+ *
+ * 1) We need the ability to persist an object and restore it. 2) The class for
+ * an object may be absent. If so we would like to skip the object and keep
+ * reading. 3) The class for an object may change. If so the new class should be
+ * able to read the old persistence info.
+ *
+ * We could ask the objects to serialize themselves into an ObjectOutputStream,
+ * DataOutputStream, or Hashtable. However all of these approaches fail to meet
+ * the second requirement.
+ *
* Memento supports binary persistance with a version ID.
*/
public final class XMLMemento implements IMemento {
private Document factory;
+
private Element element;
/**
- * Answer a memento for the document and element. For simplicity
- * you should use createReadRoot and createWriteRoot to create the initial
- * mementos on a document.
+ * Answer a memento for the document and element. For simplicity you should
+ * use createReadRoot and createWriteRoot to create the initial mementos on
+ * a document.
*/
public XMLMemento(Document doc, Element el) {
factory = doc;
}
/**
- * Create a Document from a Reader and answer a root memento for reading
- * a document.
+ * Create a Document from a Reader and answer a root memento for reading a
+ * document.
*/
protected static XMLMemento createReadRoot(Reader reader) {
Document document = null;
try {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
document = parser.parse(new InputSource(reader));
Node node = document.getFirstChild();
} finally {
try {
reader.close();
- } catch (Exception e) { }
+ } catch (Exception e) {
+ }
}
return null;
}
public static XMLMemento createWriteRoot(String type) {
Document document;
try {
- document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ document = DocumentBuilderFactory.newInstance()
+ .newDocumentBuilder().newDocument();
Element element = document.createElement(type);
document.appendChild(element);
return new XMLMemento(document, element);
return null;
// Find the first node which is a child of this node.
- for (int nX = 0; nX < size; nX ++) {
+ for (int nX = 0; nX < size; nX++) {
Node node = nodes.item(nX);
if (node instanceof Element) {
- Element element2 = (Element)node;
+ Element element2 = (Element) node;
if (element2.getNodeName().equals(type))
return new XMLMemento(factory, element2);
}
/**
* @see IMemento.
*/
- public IMemento [] getChildren(String type) {
+ public IMemento[] getChildren(String type) {
// Get the nodes.
NodeList nodes = element.getChildNodes();
int size = nodes.getLength();
// Extract each node with given fType.
ArrayList list = new ArrayList(size);
- for (int nX = 0; nX < size; nX ++) {
+ for (int nX = 0; nX < size; nX++) {
Node node = nodes.item(nX);
if (node instanceof Element) {
- Element element2 = (Element)node;
+ Element element2 = (Element) node;
if (element2.getNodeName().equals(type))
list.add(element2);
}
// Create a memento for each node.
size = list.size();
- IMemento [] results = new IMemento[size];
- for (int x = 0; x < size; x ++) {
- results[x] = new XMLMemento(factory, (Element)list.get(x));
+ IMemento[] results = new IMemento[size];
+ for (int x = 0; x < size; x++) {
+ results[x] = new XMLMemento(factory, (Element) list.get(x));
}
return results;
}
/**
* Return the contents of this memento as a byte array.
- *
+ *
* @return byte[]
*/
public byte[] getContents() throws IOException {
/**
* Returns an input stream for writing to the disk with a local locale.
- *
+ *
* @return java.io.InputStream
*/
public InputStream getInputStream() throws IOException {
/**
* Loads a memento from the given filename.
- *
- * @param in java.io.InputStream
+ *
+ * @param in
+ * java.io.InputStream
* @return org.eclipse.ui.IMemento
* @exception java.io.IOException
*/
/**
* Loads a memento from the given filename.
- *
- * @param in java.io.InputStream
+ *
+ * @param in
+ * java.io.InputStream
* @return org.eclipse.ui.IMemento
* @exception java.io.IOException
*/
public static IMemento loadCorruptMemento(InputStream in) {
Document document = null;
try {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
document = parser.parse(in);
Node node = document.getFirstChild();
} finally {
try {
in.close();
- } catch (Exception e) { }
+ } catch (Exception e) {
+ }
}
return null;
}
/**
* Loads a memento from the given filename.
- *
- * @param filename java.lang.String
+ *
+ * @param filename
+ * java.lang.String
* @return org.eclipse.ui.IMemento
* @exception java.io.IOException
*/
/**
* Loads a memento from the given filename.
- *
- * @param url java.net.URL
+ *
+ * @param url
+ * java.net.URL
* @return org.eclipse.ui.IMemento
* @exception java.io.IOException
*/
public static IMemento loadMemento(URL url) throws IOException {
- return XMLMemento.createReadRoot(new InputStreamReader(url.openStream()));
+ return XMLMemento
+ .createReadRoot(new InputStreamReader(url.openStream()));
}
/**
private void putElement(Element element2) {
NamedNodeMap nodeMap = element2.getAttributes();
int size = nodeMap.getLength();
- for (int i = 0; i < size; i++){
- Attr attr = (Attr)nodeMap.item(i);
- putString(attr.getName(),attr.getValue());
+ for (int i = 0; i < size; i++) {
+ Attr attr = (Attr) nodeMap.item(i);
+ putString(attr.getName(), attr.getValue());
}
NodeList nodes = element2.getChildNodes();
size = nodes.getLength();
- for (int i = 0; i < size; i ++) {
+ for (int i = 0; i < size; i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
- XMLMemento child = (XMLMemento)createChild(node.getNodeName());
- child.putElement((Element)node);
+ XMLMemento child = (XMLMemento) createChild(node.getNodeName());
+ child.putElement((Element) node);
}
}
}
Result result = new StreamResult(writer);
Source source = new DOMSource(factory);
try {
- Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ Transformer transformer = TransformerFactory.newInstance()
+ .newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.transform(source, result);
Result result = new StreamResult(os);
Source source = new DOMSource(factory);
try {
- Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ Transformer transformer = TransformerFactory.newInstance()
+ .newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.transform(source, result);
/**
* Saves the memento to the given file.
- *
- * @param filename java.lang.String
+ *
+ * @param filename
+ * java.lang.String
* @exception java.io.IOException
*/
public void saveToFile(String filename) throws IOException {
if (w != null) {
try {
w.close();
- } catch (Exception e) { }
+ } catch (Exception e) {
+ }
}
}
}