001package org.hl7.fhir.r4.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
032import java.util.Stack;
033
034import org.w3c.dom.Document;
035import org.w3c.dom.Element;
036import org.w3c.dom.Node;
037import org.w3c.dom.UserDataHandler;
038import org.w3c.dom.events.Event;
039import org.w3c.dom.events.EventListener;
040import org.w3c.dom.events.EventTarget;
041import org.w3c.dom.events.MutationEvent;
042import org.xml.sax.Attributes;
043import org.xml.sax.Locator;
044import org.xml.sax.SAXException;
045import org.xml.sax.XMLReader;
046import org.xml.sax.helpers.LocatorImpl;
047import org.xml.sax.helpers.XMLFilterImpl;
048
049// http://javacoalface.blogspot.com.au/2011/04/line-and-column-numbers-in-xml-dom.html
050
051public class XmlLocationAnnotator extends XMLFilterImpl {
052
053  private Locator locator;
054  private Stack<Locator> locatorStack = new Stack<Locator>();
055  private Stack<Element> elementStack = new Stack<Element>();
056  private UserDataHandler dataHandler = new LocationDataHandler();
057
058  public XmlLocationAnnotator(XMLReader xmlReader, Document dom) {
059    super(xmlReader);
060
061    // Add listener to DOM, so we know which node was added.
062    EventListener modListener = new EventListener() {
063      @Override
064      public void handleEvent(Event e) {
065        EventTarget target = ((MutationEvent) e).getTarget();
066        elementStack.push((Element) target);
067      }
068    };
069    ((EventTarget) dom).addEventListener("DOMNodeInserted", modListener, true);
070  }
071
072  @Override
073  public void setDocumentLocator(Locator locator) {
074    super.setDocumentLocator(locator);
075    this.locator = locator;
076  }
077
078  @Override
079  public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
080    super.startElement(uri, localName, qName, atts);
081
082    // Keep snapshot of start location,
083    // for later when end of element is found.
084    locatorStack.push(new LocatorImpl(locator));
085  }
086
087  @Override
088  public void endElement(String uri, String localName, String qName) throws SAXException {
089
090    // Mutation event fired by the adding of element end,
091    // and so lastAddedElement will be set.
092    super.endElement(uri, localName, qName);
093
094    if (locatorStack.size() > 0) {
095      Locator startLocator = locatorStack.pop();
096
097      XmlLocationData location = new XmlLocationData(startLocator.getSystemId(), startLocator.getLineNumber(),
098          startLocator.getColumnNumber(), locator.getLineNumber(), locator.getColumnNumber());
099      Element lastAddedElement = elementStack.pop();
100
101      lastAddedElement.setUserData(XmlLocationData.LOCATION_DATA_KEY, location, dataHandler);
102    }
103  }
104
105  // Ensure location data copied to any new DOM node.
106  private class LocationDataHandler implements UserDataHandler {
107
108    @Override
109    public void handle(short operation, String key, Object data, Node src, Node dst) {
110
111      if (src != null && dst != null) {
112        XmlLocationData locatonData = (XmlLocationData) src.getUserData(XmlLocationData.LOCATION_DATA_KEY);
113
114        if (locatonData != null) {
115          dst.setUserData(XmlLocationData.LOCATION_DATA_KEY, locatonData, dataHandler);
116        }
117      }
118    }
119  }
120}