Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OperatorValidator.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.svg;
  19. import java.util.LinkedList;
  20. import java.util.Queue;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertTrue;
  23. class OperatorValidator {
  24. private interface Match {
  25. boolean match(String line);
  26. }
  27. private static class MatchSequence implements OperatorValidator.Match {
  28. private final Queue<OperatorValidator.Match> expectedMatches = new LinkedList<OperatorValidator.Match>();
  29. private OperatorValidator.Match currentMatch;
  30. private static final OperatorValidator.Match FINAL_MATCH = new Match() {
  31. public boolean match(String line) {
  32. return false;
  33. }
  34. };
  35. public boolean isExhausted() {
  36. return currentMatch == FINAL_MATCH;
  37. }
  38. public void addMatch(OperatorValidator.Match match) {
  39. if (currentMatch == null) {
  40. currentMatch = match;
  41. } else {
  42. expectedMatches.add(match);
  43. }
  44. }
  45. public boolean match(String line) {
  46. boolean match = currentMatch.match(line);
  47. if (match) {
  48. if (expectedMatches.isEmpty()) {
  49. currentMatch = FINAL_MATCH;
  50. } else {
  51. currentMatch = expectedMatches.remove();
  52. }
  53. }
  54. return match;
  55. }
  56. }
  57. private static class OperatorMatch implements OperatorValidator.Match {
  58. final String operator;
  59. final String line;
  60. OperatorMatch(String operator, String line) {
  61. this.operator = operator;
  62. this.line = line;
  63. }
  64. public boolean match(String line) {
  65. if (line.contains(operator)) {
  66. assertEquals(this.line, line);
  67. return true;
  68. }
  69. return false;
  70. }
  71. }
  72. private final OperatorValidator.MatchSequence matchSequence = new MatchSequence();
  73. public OperatorValidator addOperatorMatch(String operator, String expectedLine) {
  74. matchSequence.addMatch(new OperatorMatch(operator, expectedLine));
  75. return this;
  76. }
  77. public void check(String line) {
  78. matchSequence.match(line);
  79. }
  80. public void end() {
  81. assertTrue("Expected operators remain", matchSequence.isExhausted());
  82. }
  83. }