001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.xerces.impl.xs;
018:
019: import org.apache.xerces.xs.XSAnnotation;
020: import org.apache.xerces.xs.XSConstants;
021: import org.apache.xerces.xs.XSNamespaceItem;
022: import org.apache.xerces.dom.CoreDocumentImpl;
023: import org.apache.xerces.parsers.SAXParser;
024: import org.apache.xerces.parsers.DOMParser;
025:
026: import org.xml.sax.ContentHandler;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.InputSource;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.Document;
032: import java.io.StringReader;
033: import java.io.IOException;
034:
035: /**
036: * This is an implementation of the XSAnnotation schema component.
037: *
038: * @xerces.internal
039: *
040: * @version $Id: XSAnnotationImpl.java 446734 2006-09-15 20:51:23Z mrglavas $
041: */
042: public class XSAnnotationImpl implements XSAnnotation {
043:
044: // Data
045:
046: // the content of the annotation node, including all children, along
047: // with any non-schema attributes from its parent
048: private String fData = null;
049:
050: // the grammar which owns this annotation; we get parsers
051: // from here when we need them
052: private SchemaGrammar fGrammar = null;
053:
054: // constructors
055: public XSAnnotationImpl(String contents, SchemaGrammar grammar) {
056: fData = contents;
057: fGrammar = grammar;
058: }
059:
060: /**
061: * Write contents of the annotation to the specified DOM object. If the
062: * specified <code>target</code> object is a DOM in-scope namespace
063: * declarations for <code>annotation</code> element are added as
064: * attributes nodes of the serialized <code>annotation</code>, otherwise
065: * the corresponding events for all in-scope namespace declaration are
066: * sent via specified document handler.
067: * @param target A target pointer to the annotation target object, i.e.
068: * <code>org.w3c.dom.Document</code>,
069: * <code>org.xml.sax.ContentHandler</code>.
070: * @param targetType A target type.
071: * @return If the <code>target</code> is recognized type and supported by
072: * this implementation return true, otherwise return false.
073: */
074: public boolean writeAnnotation(Object target, short targetType) {
075: if (targetType == XSAnnotation.W3C_DOM_ELEMENT
076: || targetType == XSAnnotation.W3C_DOM_DOCUMENT) {
077: writeToDOM((Node) target, targetType);
078: return true;
079: } else if (targetType == SAX_CONTENTHANDLER) {
080: writeToSAX((ContentHandler) target);
081: return true;
082: }
083: return false;
084: }
085:
086: /**
087: * A text representation of annotation.
088: */
089: public String getAnnotationString() {
090: return fData;
091: }
092:
093: // XSObject methods
094:
095: /**
096: * The <code>type</code> of this object, i.e.
097: * <code>ELEMENT_DECLARATION</code>.
098: */
099: public short getType() {
100: return XSConstants.ANNOTATION;
101: }
102:
103: /**
104: * The name of type <code>NCName</code> of this declaration as defined in
105: * XML Namespaces.
106: */
107: public String getName() {
108: return null;
109: }
110:
111: /**
112: * The [target namespace] of this object, or <code>null</code> if it is
113: * unspecified.
114: */
115: public String getNamespace() {
116: return null;
117: }
118:
119: /**
120: * A namespace schema information item corresponding to the target
121: * namespace of the component, if it's globally declared; or null
122: * otherwise.
123: */
124: public XSNamespaceItem getNamespaceItem() {
125: return null;
126: }
127:
128: // private methods
129: private synchronized void writeToSAX(ContentHandler handler) {
130: // nothing must go wrong with this parse...
131: SAXParser parser = fGrammar.getSAXParser();
132: StringReader aReader = new StringReader(fData);
133: InputSource aSource = new InputSource(aReader);
134: parser.setContentHandler(handler);
135: try {
136: parser.parse(aSource);
137: } catch (SAXException e) {
138: // this should never happen!
139: // REVISIT: what to do with this?; should really not
140: // eat it...
141: } catch (IOException i) {
142: // ditto with above
143: }
144: }
145:
146: // this creates the new Annotation element as the first child
147: // of the Node
148: private synchronized void writeToDOM(Node target, short type) {
149: Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT) ? target
150: .getOwnerDocument()
151: : (Document) target;
152: DOMParser parser = fGrammar.getDOMParser();
153: StringReader aReader = new StringReader(fData);
154: InputSource aSource = new InputSource(aReader);
155: try {
156: parser.parse(aSource);
157: } catch (SAXException e) {
158: // this should never happen!
159: // REVISIT: what to do with this?; should really not
160: // eat it...
161: } catch (IOException i) {
162: // ditto with above
163: }
164: Document aDocument = parser.getDocument();
165: parser.dropDocumentReferences();
166: Element annotation = aDocument.getDocumentElement();
167: Node newElem = null;
168: if (futureOwner instanceof CoreDocumentImpl) {
169: newElem = futureOwner.adoptNode(annotation);
170: // adoptNode will return null when the DOM implementations are not compatible.
171: if (newElem == null) {
172: newElem = futureOwner.importNode(annotation, true);
173: }
174: } else {
175: newElem = futureOwner.importNode(annotation, true);
176: }
177: target.insertBefore(newElem, target.getFirstChild());
178: }
179:
180: }
|