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

br.com.objectos.css.io.SelectorReader Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * 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.
 */
package br.com.objectos.css.io;

import java.io.IOException;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import br.com.objectos.codereader.CodeReader;
import br.com.objectos.codereader.Keyword;
import br.com.objectos.core.lang.Strings;
import br.com.objectos.core.util.ImmutableList;
import br.com.objectos.css.Selector;

/**
 * @author [email protected] (Marcio Endo)
 */
public class SelectorReader {

  private static final List ACTIONS = ImmutableList. builder()
      .add(IdSelector.action())
      .add(ClassSelector.action())
      .add(AttributeSelector.action())
      .add(PseudoClassSelector.ACTION)

      .add(ChildSelector.action())
      .add(SiblingSelector.action(SiblingOperator.ADJACENT))
      .add(SiblingSelector.action(SiblingOperator.GENERAL))

      .add(DescendantSelector.action())
      .add(DescendantSelector.finalizerAction())
      .build();

  private final SelectorBuilder builder = SelectorBuilder.start();
  private final CodeReader reader;

  private SelectorReader(CodeReader reader) {
    this.reader = reader;
  }

  public static Selector parse(String query) {
    try (CodeReader reader = CodeReader.open(new StringReader(query))) {
      return new SelectorReader(reader).parse();
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  public void acceptBuilderFinalizer(Consumer finalizer) {
    finalizer.accept(builder);
  }

  public void add(Selector selector) {
    builder.add(selector);
  }

  public void add(Function provider) {
    builder.add(provider.apply(reader));
  }

  public void addMaybeQualifiedTag(Function provider) {
    builder.add(maybeQualifiedTag(provider));
  }

  public void deleteKeyword(Keyword keyword) {
    reader.deleteKeyword(keyword);
  }

  public boolean empty() {
    return reader.empty();
  }

  public String get() {
    return reader.get();
  }

  public boolean matches(Keyword keyword) {
    return keyword.matches(reader);
  }

  public void skipWhitespace() {
    reader.skipOver(Keyword.whitespace());
  }

  private Selector parse() {
    while (reader.hasNext()) {
      reader.consume();

      for (SelectorAction action : ACTIONS) {
        if (action.matches(this)) {
          action.execute(this);
          break;
        }
      }
    }

    return builder.build(reader);
  }

  private Selector maybeQualifiedTag(Function provider) {
    String maybeName = reader.get();
    return Strings.isNullOrEmpty(maybeName)
        ? provider.apply(reader)
        : CssSelectors.and(CssSelectors.tag(maybeName), provider.apply(reader));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy