001package org.hl7.fhir.dstu3.utils.formats;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.util.Stack;
035
036import org.w3c.dom.Document;
037import org.w3c.dom.Element;
038import org.w3c.dom.Node;
039import org.w3c.dom.UserDataHandler;
040import org.w3c.dom.events.Event;
041import org.w3c.dom.events.EventListener;
042import org.w3c.dom.events.EventTarget;
043import org.w3c.dom.events.MutationEvent;
044import org.xml.sax.Attributes;
045import org.xml.sax.Locator;
046import org.xml.sax.SAXException;
047import org.xml.sax.XMLReader;
048import org.xml.sax.helpers.LocatorImpl;
049import org.xml.sax.helpers.XMLFilterImpl;
050
051// http://javacoalface.blogspot.com.au/2011/04/line-and-column-numbers-in-xml-dom.html
052
053public class XmlLocationAnnotator extends XMLFilterImpl  {
054
055  private Locator locator;
056  private Stack<Locator> locatorStack = new Stack<Locator>();
057  private Stack<Element> elementStack = new Stack<Element>();
058  private UserDataHandler dataHandler = new LocationDataHandler();
059
060  public XmlLocationAnnotator(XMLReader xmlReader, Document dom) {
061      super(xmlReader);
062
063      // Add listener to DOM, so we know which node was added.
064      EventListener modListener = new EventListener() {
065          @Override
066          public void handleEvent(Event e) {
067              EventTarget target = ((MutationEvent) e).getTarget();
068              elementStack.push((Element) target);
069          }
070      };
071      ((EventTarget) dom).addEventListener("DOMNodeInserted", modListener, true);
072  }
073
074  @Override
075  public void setDocumentLocator(Locator locator) {
076      super.setDocumentLocator(locator);
077      this.locator = locator;
078  }
079
080  @Override
081  public void startElement(String uri, String localName,
082          String qName, Attributes atts) throws SAXException {
083      super.startElement(uri, localName, qName, atts);
084
085      // Keep snapshot of start location,
086      // for later when end of element is found.
087      locatorStack.push(new LocatorImpl(locator));
088  }
089
090  @Override
091  public void endElement(String uri, String localName, String qName)
092          throws SAXException {
093
094      // Mutation event fired by the adding of element end,
095      // and so lastAddedElement will be set.
096      super.endElement(uri, localName, qName);
097     
098      if (locatorStack.size() > 0) {
099          Locator startLocator = locatorStack.pop();
100         
101          XmlLocationData location = new XmlLocationData(
102                  startLocator.getSystemId(),
103                  startLocator.getLineNumber(),
104                  startLocator.getColumnNumber(),
105                  locator.getLineNumber(),
106                  locator.getColumnNumber());
107         Element lastAddedElement = elementStack.pop();
108         
109          lastAddedElement.setUserData(
110                  XmlLocationData.LOCATION_DATA_KEY, location,
111                  dataHandler);
112      }
113  }
114
115  // Ensure location data copied to any new DOM node.
116  private class LocationDataHandler implements UserDataHandler {
117
118      @Override
119      public void handle(short operation, String key, Object data,
120              Node src, Node dst) {
121         
122          if (src != null && dst != null) {
123              XmlLocationData locatonData = (XmlLocationData)
124                      src.getUserData(XmlLocationData.LOCATION_DATA_KEY);
125             
126              if (locatonData != null) {
127                  dst.setUserData(XmlLocationData.LOCATION_DATA_KEY,
128                          locatonData, dataHandler);
129              }
130          }
131      }
132  }
133}