001/*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024package microsoft.exchange.webservices.data.core.response;
025
026import java.util.Iterator;
027
028import javax.xml.parsers.DocumentBuilder;
029import javax.xml.parsers.DocumentBuilderFactory;
030import javax.xml.parsers.ParserConfigurationException;
031import javax.xml.stream.XMLEventReader;
032import javax.xml.stream.events.Attribute;
033import javax.xml.stream.events.Namespace;
034import javax.xml.stream.events.StartElement;
035import javax.xml.stream.events.XMLEvent;
036
037import org.w3c.dom.Document;
038import org.w3c.dom.Element;
039import org.w3c.dom.Node;
040
041import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
042import microsoft.exchange.webservices.data.core.EwsUtilities;
043import microsoft.exchange.webservices.data.core.ExchangeService;
044import microsoft.exchange.webservices.data.core.XmlElementNames;
045import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
046import microsoft.exchange.webservices.data.security.XmlNodeType;
047
048
049/**
050 * Represents the response to a ExecuteDiagnosticMethod operation
051 */
052public final class ExecuteDiagnosticMethodResponse extends ServiceResponse {
053
054
055  /**
056   * Initializes a new instance of the ExecuteDiagnosticMethodResponse class.
057   *
058   * @param service The service
059   */
060  public ExecuteDiagnosticMethodResponse(ExchangeService service) {
061    super();
062    EwsUtilities.ewsAssert(service != null, "ExecuteDiagnosticMethodResponse.ctor", "service is null");
063  }
064
065  /**
066   * Reads response elements from XML.
067   *
068   * @throws Exception
069   */
070  @Override
071  protected void readElementsFromXml(EwsServiceXmlReader reader)
072      throws Exception {
073    reader.readStartElement(XmlNamespace.Messages,
074        XmlElementNames.ReturnValue);
075
076    XMLEventReader returnValueReader = reader.getXmlReaderForNode();
077    //this.returnValue = (Document) new SafeXmlDocument();
078    {
079      this.returnValue = retriveDocument(returnValueReader);
080    }
081
082    reader.skipCurrentElement();
083    reader.readEndElementIfNecessary(XmlNamespace.Messages,
084        XmlElementNames.ReturnValue);
085  }
086
087
088  /**
089   * @return document
090   * @throws javax.xml.parsers.ParserConfigurationException
091   */
092  public Document retriveDocument(XMLEventReader xmlEventReader)
093      throws ParserConfigurationException {
094    DocumentBuilderFactory dbfInstance = DocumentBuilderFactory
095        .newInstance();
096    DocumentBuilder documentBuilder = dbfInstance.newDocumentBuilder();
097    Document document = documentBuilder.newDocument();
098
099    Element currentElement = document.getDocumentElement();
100
101    while (xmlEventReader.hasNext()) {
102      XMLEvent xmleve = (XMLEvent) xmlEventReader.next();
103
104      if (xmleve.getEventType() == XmlNodeType.END_ELEMENT) {
105        Node node = currentElement.getParentNode();
106        if (node instanceof Document) {
107          currentElement = ((Document) node).getDocumentElement();
108        } else {
109          currentElement = (Element) currentElement.getParentNode();
110        }
111      }
112
113      if (xmleve.getEventType() == XmlNodeType.START_ELEMENT) {
114        // startElement((StartElement) xmleve,doc);
115        StartElement ele = (StartElement) xmleve;
116        Element element = null;
117        element = document.createElementNS(ele.getName()
118            .getNamespaceURI(), ele.getName().getLocalPart());
119
120        for (Iterator<Attribute> ite = ele.getAttributes(); ite.hasNext();) {
121          Attribute attr = ite.next();
122          element.setAttribute(attr.getName().getLocalPart(),
123              attr.getValue());
124        }
125
126        String xmlns = EwsUtilities.WSTrustFebruary2005Namespace;//"http://schemas.xmlsoap.org/wsdl/";
127        for (Iterator<Namespace> ite = ele.getNamespaces(); ite.hasNext();) {
128          Namespace ns = ite.next();
129          String name = ns.getPrefix();
130          if (!name.isEmpty()) {
131            element.setAttributeNS(xmlns, name,
132                ns.getNamespaceURI());
133          } else {
134            xmlns = ns.getNamespaceURI();
135          }
136        }
137
138        if (currentElement == null) {
139          document.appendChild(element);
140        } else {
141          currentElement.appendChild(element);
142        }
143
144        currentElement = element;
145        element.setUserData("location", ele.getLocation(), null);
146      }
147    }
148    return document;
149  }
150
151  private Document returnValue;
152
153  /**
154   * Gets the return value.
155   */
156  public Document getReturnValue() {
157    return returnValue;
158  }
159
160  /**
161   * Sets the return value.
162   */
163  private void setReturnValue(Document value) {
164    returnValue = value;
165  }
166}