All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kz.greetgo.msoffice.xlsx.reader.AbstractXmlHandler Maven / Gradle / Ivy

There is a newer version: 0.5.9
Show newest version
package kz.greetgo.msoffice.xlsx.reader;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractXmlHandler extends DefaultHandler {

  private final List tags = new ArrayList<>();

  @Override
  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    tags.add(localName);
    characters.setLength(0);
    try {
      startTag(tagPath(), attributes);
    } catch (SAXException | RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  protected String tagPath() {
    return "/" + String.join("/", tags);
  }

  protected abstract void startTag(String tagPath, Attributes attributes) throws Exception;

  private final StringBuilder characters = new StringBuilder();

  @Override
  public void characters(char[] ch, int start, int length) {
    characters.append(ch, start, length);
  }

  protected String text() {
    return characters.toString();
  }

  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    try {
      endTag(tagPath());
      tags.remove(tags.size() - 1);
      characters.setLength(0);
    } catch (SAXException | RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  protected abstract void endTag(String tagPath) throws Exception;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy