001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xslt.model.impl;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Set;
025:
026: import javax.xml.namespace.QName;
027:
028: import org.netbeans.modules.xml.xam.ComponentUpdater;
029: import org.netbeans.modules.xml.xam.ModelSource;
030: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
031: import org.netbeans.modules.xml.xam.dom.ChangeInfo;
032: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
033: import org.netbeans.modules.xslt.model.LiteralResultElement;
034: import org.netbeans.modules.xslt.model.SequenceConstructor;
035: import org.netbeans.modules.xslt.model.XslComponent;
036: import org.netbeans.modules.xslt.model.XslModel;
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Node;
039:
040: /**
041: * @author ads
042: *
043: */
044: class XslModelImpl extends AbstractDocumentModel<XslComponent>
045: implements XslModel {
046:
047: XslModelImpl(ModelSource source) {
048: super (source);
049: myFactory = new XslComponentFactoryImpl(this );
050: }
051:
052: /* (non-Javadoc)
053: * @see org.netbeans.modules.xml.xam.dom.AbstractDocumentModel#createRootComponent(org.w3c.dom.Element)
054: */
055: @Override
056: public XslComponent createRootComponent(Element root) {
057: StylesheetImpl stylesheet = (StylesheetImpl) myFactory.create(
058: root, null);
059: if (stylesheet != null) {
060: myRoot = stylesheet;
061: } else {
062: return null;
063: }
064: return getStylesheet();
065: }
066:
067: /* (non-Javadoc)
068: * @see org.netbeans.modules.xml.xam.dom.AbstractDocumentModel#getComponentUpdater()
069: */
070: @Override
071: protected ComponentUpdater<XslComponent> getComponentUpdater() {
072: if (mySyncUpdateVisitor == null) {
073: mySyncUpdateVisitor = new SyncUpdateVisitor();
074: }
075: return mySyncUpdateVisitor;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.netbeans.modules.xslt.model.XslModel#getFactory()
080: */
081: public XslComponentFactoryImpl getFactory() {
082: return myFactory;
083: }
084:
085: /* (non-Javadoc)
086: * @see org.netbeans.modules.xslt.model.XslModel#getStylesheet()
087: */
088: public StylesheetImpl getStylesheet() {
089: return (StylesheetImpl) getRootComponent();
090: }
091:
092: /* (non-Javadoc)
093: * @see org.netbeans.modules.xml.xam.dom.DocumentModel#createComponent(org.netbeans.modules.xml.xam.dom.DocumentComponent, org.w3c.dom.Element)
094: */
095: public XslComponent createComponent(XslComponent parent,
096: Element element) {
097: return getFactory().create(element, parent);
098: }
099:
100: /* (non-Javadoc)
101: * @see org.netbeans.modules.xml.xam.dom.DocumentModel#getRootComponent()
102: */
103: public XslComponent getRootComponent() {
104: return myRoot;
105: }
106:
107: @Override
108: public Set<QName> getQNames() {
109: return XslElements.allQNames();
110: }
111:
112: public ChangeInfo prepareChangeInfo(List<Node> pathToRoot) {
113: ChangeInfo change = super .prepareChangeInfo(pathToRoot);
114: DocumentComponent parentComponent = findComponent(change
115: .getRootToParentPath());
116: if (parentComponent == null) {
117: return change;
118: }
119:
120: // this are conditions where subject element is Literal element
121: if (parentComponent instanceof SequenceConstructor
122: && !change.isDomainElement()
123: && change.getChangedElement() != null) {
124: prepareLiterlElementChange(change, parentComponent);
125: } else {
126: change.setParentComponent(parentComponent);
127: }
128: return change;
129: }
130:
131: private void prepareLiterlElementChange(ChangeInfo change,
132: DocumentComponent parentComponent) {
133: if (change.getOtherNonDomainElementNodes() == null
134: || change.getOtherNonDomainElementNodes().isEmpty()) {
135: // case add or remove literal result element
136: change.setDomainElement(true);
137: change.setParentComponent(null);
138: } else {
139: List<Element> rootToChanged = new ArrayList<Element>(change
140: .getRootToParentPath());
141: rootToChanged.add(change.getChangedElement());
142: DocumentComponent changedComponent = findComponent(rootToChanged);
143: if (changedComponent != null
144: && changedComponent.getClass().isAssignableFrom(
145: LiteralResultElement.class)) {
146: // case literal result element is changed
147: change.markNonDomainChildAsChanged();
148: change.setParentComponent(null);
149: }
150: }
151: }
152:
153: private XslComponentFactoryImpl myFactory;
154:
155: private StylesheetImpl myRoot;
156:
157: private SyncUpdateVisitor mySyncUpdateVisitor;
158:
159: }
|