001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.text.cheatsheet.simple;
011:
012: import org.eclipse.jface.text.IDocument;
013: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
014: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
015: import org.eclipse.pde.internal.core.text.NodeDocumentHandler;
016: import org.xml.sax.Attributes;
017: import org.xml.sax.SAXException;
018:
019: /**
020: * SimpleCSDocumentHandler
021: *
022: */
023: public class SimpleCSDocumentHandler extends NodeDocumentHandler {
024:
025: private SimpleCSModel fModel;
026:
027: private String fCollapsibleParentName;
028:
029: /**
030: * @param reconciling
031: */
032: public SimpleCSDocumentHandler(SimpleCSModel model,
033: boolean reconciling) {
034: super (reconciling, model.getFactory());
035: fModel = model;
036: fCollapsibleParentName = null;
037: }
038:
039: /* (non-Javadoc)
040: * @see org.eclipse.pde.internal.core.text.DocumentHandler#getDocument()
041: */
042: protected IDocument getDocument() {
043: return fModel.getDocument();
044: }
045:
046: /* (non-Javadoc)
047: * @see org.eclipse.pde.internal.core.text.NodeDocumentHandler#getRootNode()
048: */
049: protected IDocumentElementNode getRootNode() {
050: return (IDocumentElementNode) fModel.getSimpleCS();
051: }
052:
053: /**
054: * @param tagName
055: */
056: private void setCollapsibleParentName(String tagName) {
057: fCollapsibleParentName = tagName;
058: }
059:
060: /**
061: * @return
062: */
063: private String getCollapsibleParentName() {
064: return fCollapsibleParentName;
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.pde.internal.core.text.DocumentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
069: */
070: public void startElement(String uri, String localName, String name,
071: Attributes attributes) throws SAXException {
072:
073: IDocumentElementNode parent = getLastParsedDocumentNode();
074: if ((parent != null) && (parent.isContentCollapsed() == true)) {
075: setCollapsibleParentName(parent.getXMLTagName());
076: processCollapsedStartElement(name, attributes, parent);
077: } else {
078: super .startElement(uri, localName, name, attributes);
079: }
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.pde.internal.core.text.DocumentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
084: */
085: public void endElement(String uri, String localName, String name)
086: throws SAXException {
087:
088: if ((getCollapsibleParentName() != null)
089: && (getCollapsibleParentName().equals(name))) {
090: setCollapsibleParentName(null);
091: }
092:
093: if ((getCollapsibleParentName() != null)) {
094: IDocumentElementNode parent = getLastParsedDocumentNode();
095: processCollapsedEndElement(name, parent);
096: } else {
097: super .endElement(uri, localName, name);
098: }
099: }
100:
101: /**
102: * @param name
103: * @param parent
104: */
105: private void processCollapsedEndElement(String name,
106: IDocumentElementNode parent) {
107: // Get the document node
108: IDocumentElementNode node = getDocumentNode(name, parent);
109: // If the start element is self-terminating, no end tag is required
110: boolean terminate = node.canTerminateStartTag();
111: if (terminate) {
112: return;
113: }
114: // Serialize the document node XML end tag
115: StringBuffer endElementString = new StringBuffer();
116: endElementString.append('<');
117: endElementString.append('/');
118: endElementString.append(name);
119: endElementString.append('>');
120: // Set the XML end tag string as text in the text node
121: getDocumentTextNode(endElementString.toString(), parent);
122: }
123:
124: /**
125: * @param name
126: * @param attributes
127: * @param parent
128: */
129: private void processCollapsedStartElement(String name,
130: Attributes attributes, IDocumentElementNode parent) {
131: // Create the document node
132: IDocumentElementNode node = getDocumentNode(name, parent);
133: // Create the attributes
134: for (int i = 0; i < attributes.getLength(); i++) {
135: String attName = attributes.getQName(i);
136: String attValue = attributes.getValue(i);
137: IDocumentAttributeNode attribute = getDocumentAttribute(
138: attName, attValue, node);
139: if (attribute != null) {
140: node.setXMLAttribute(attribute);
141: }
142: }
143: // Serialize the document node XML start tag
144: boolean terminate = node.canTerminateStartTag();
145: String startElementString = node.writeShallow(terminate);
146: // Set the XML start tag string as text in the text node
147: getDocumentTextNode(startElementString, parent);
148: }
149:
150: }
|