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

swim.xml.PIParser Maven / Gradle / Ivy

Go to download

eXtensible Markup Language (XML) codec that incrementally parses and writes swim-structure values

The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// 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 swim.xml;

import swim.codec.Diagnostic;
import swim.codec.Input;
import swim.codec.Output;
import swim.codec.Parser;

final class PIParser extends Parser {

  final XmlParser xml;
  final Parser targetParser;
  final Output output;
  final int step;

  PIParser(XmlParser xml, Parser targetParser, Output output, int step) {
    this.xml = xml;
    this.targetParser = targetParser;
    this.output = output;
    this.step = step;
  }

  @Override
  public Parser feed(Input input) {
    return PIParser.parse(input, this.xml, this.targetParser, this.output, this.step);
  }

  static  Parser parse(Input input, XmlParser xml, Parser targetParser,
                             Output output, int step) {
    int c = 0;
    if (step == 1) {
      if (input.isCont()) {
        if (input.head() == '<') {
          input = input.step();
          step = 2;
        } else {
          return Parser.error(Diagnostic.expected('<', input));
        }
      } else if (input.isDone()) {
        return Parser.error(Diagnostic.expected('<', input));
      }
    }
    if (step == 2) {
      if (input.isCont()) {
        if (input.head() == '?') {
          input = input.step();
          step = 3;
        } else {
          return Parser.error(Diagnostic.expected('?', input));
        }
      } else if (input.isDone()) {
        return Parser.error(Diagnostic.expected('?', input));
      }
    }
    if (step == 3) {
      if (targetParser == null) {
        targetParser = xml.parsePITarget(input);
      } else {
        targetParser = targetParser.feed(input);
      }
      if (targetParser.isDone()) {
        final String target = targetParser.bind();
        if (!"xml".equalsIgnoreCase(target)) {
          return xml.parsePITargetRest(input, target);
        } else {
          return Parser.error(Diagnostic.message("illegal processing instruction target: " + target, input));
        }
      } else if (targetParser.isError()) {
        return targetParser.asError();
      }
    }
    if (step == 4) {
      if (input.isCont()) {
        if (Xml.isWhitespace(input.head())) {
          input = input.step();
          if (output == null) {
            output = xml.piOutput(targetParser.bind());
          }
          step = 5;
        } else {
          return Parser.error(Diagnostic.expected("space", input));
        }
      } else if (input.isDone()) {
        return Parser.error(Diagnostic.expected("space", input));
      }
    }
    do {
      if (step == 5) {
        while (input.isCont()) {
          c = input.head();
          if (Xml.isChar(c) && c != '?') {
            input = input.step();
            output = output.write(c);
          } else {
            break;
          }
        }
        if (input.isCont()) {
          if (c == '?') {
            input = input.step();
            step = 6;
          } else {
            return Parser.error(Diagnostic.unexpected(input));
          }
        } else if (input.isDone()) {
          return Parser.error(Diagnostic.unexpected(input));
        } else {
          break;
        }
      }
      if (step == 6) {
        if (input.isCont()) {
          c = input.head();
          if (c == '>') {
            input = input.step();
            return done(output.bind());
          } else {
            output = output.write('?');
            step = 5;
            continue;
          }
        } else if (input.isDone()) {
          return Parser.error(Diagnostic.unexpected(input));
        } else {
          break;
        }
      }
      break;
    } while (true);
    if (input.isError()) {
      return Parser.error(input.trap());
    }
    return new PIParser(xml, targetParser, output, step);
  }

  static  Parser parse(Input input, XmlParser xml) {
    return PIParser.parse(input, xml, null, null, 1);
  }

  static  Parser parseRest(Input input, XmlParser xml) {
    return PIParser.parse(input, xml, null, null, 3);
  }

  static  Parser parseTargetRest(Input input, XmlParser xml, String target) {
    return PIParser.parse(input, xml, done(target), null, 4);
  }

}