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

org.cache2k.xmlConfiguration.StaxConfigTokenizer Maven / Gradle / Ivy

Go to download

A light weight and high performance Java caching library. XML configuration file support. This artifact is included in cache2k-all.

There is a newer version: 1.0.2.Final
Show newest version
package org.cache2k.xmlConfiguration;

/*
 * #%L
 * cache2k XML configuration
 * %%
 * Copyright (C) 2000 - 2016 headissue GmbH, Munich
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.util.LinkedList;

/**
 * @author Jens Wilke
 */
public class StaxConfigTokenizer extends AbstractConfigurationTokenizer {

  private XMLStreamReader input;
  private LinkedList hierarchy = new LinkedList();
  private String startName;
  private String value;

  public StaxConfigTokenizer(final String _source, final InputStream in, final String _encoding)
    throws XMLStreamException {
    super(_source);
    XMLInputFactory f = XMLInputFactory.newInstance();
    input = f.createXMLStreamReader(in, _encoding);
  }

  @Override
  public int getLineNumber() {
    return input.getLocation().getLineNumber();
  }

  @Override
  public Item next() throws Exception {
    while (input.hasNext()) {
      int _type = input.next();
      switch (_type) {
        case XMLStreamReader.START_ELEMENT :
          if (startName != null) {
            hierarchy.push(startName);
            startName = input.getLocalName();
            return returnNest(hierarchy.element());
          }
          startName = input.getLocalName(); break;
        case XMLStreamReader.CHARACTERS :
          value = input.getText(); break;
        case XMLStreamReader.END_ELEMENT :
          String _name = input.getLocalName();
          if (startName != null && startName.equals(_name)) {
            startName = null;
            return returnProperty(_name, value);
          }
          if (_name.equals(hierarchy.element())) {
            hierarchy.pop();
            return returnUnnest();
          }
          break;
      }
    }
    return null;
  }

  public static class Factory implements TokenizerFactory {

    @Override
    public ConfigurationTokenizer createTokenizer(final String _source, final InputStream in, final String _encoding)
      throws XMLStreamException {
      return new StaxConfigTokenizer(_source, in, _encoding);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy