@Test public void testParse1() throws IOException { try (Reader in = new InputStreamReader( SgmlParserTest.class.getResourceAsStream("parsertest1.sgml"), StandardCharsets.UTF_8)) { SgmlParser parser = new SgmlParser(); parser.parse(in, new SgmlParser.ContentHandler() { }); } }
public void parse(Reader in, ContentHandler handler) throws IOException { StringBuilder buffer = new StringBuilder(); boolean isInsideTag = false; boolean isStartTag = true; int lastChar = -1; int c; while ((c = in.read()) != -1) { if ('<' == c) { if (isInsideTag) { throw new InvalidFormatException("Did not expect < char!"); } if (buffer.toString().trim().length() > 0) { handler.characters(buffer.toString().trim()); } buffer.setLength(0); isInsideTag = true; isStartTag = true; } buffer.appendCodePoint(c); if ('/' == c && lastChar == '<') { isStartTag = false; } if ('>' == c) { if (!isInsideTag) { throw new InvalidFormatException("Did not expect > char!"); } if (isStartTag) { handler.startElement(extractTagName(buffer), getAttributes(buffer)); } else { handler.endElement(extractTagName(buffer)); } buffer.setLength(0); isInsideTag = false; } lastChar = c; } if (isInsideTag) { throw new InvalidFormatException("Did not find matching > char!"); } }
SgmlParser { public void parse(Reader in, ContentHandler handler) throws IOException { StringBuilder buffer = new StringBuilder(); boolean isInsideTag = false; boolean isStartTag = true; int lastChar = -1; int c; while ((c = in.read()) != -1) { if ('<' == c) { if (isInsideTag) { throw new InvalidFormatException("Did not expect < char!"); } if (buffer.toString().trim().length() > 0) { handler.characters(buffer.toString().trim()); } buffer.setLength(0); isInsideTag = true; isStartTag = true; } buffer.appendCodePoint(c); if ('/' == c && lastChar == '<') { isStartTag = false; } if ('>' == c) { if (!isInsideTag) { throw new InvalidFormatException("Did not expect > char!"); } if (isStartTag) { handler.startElement(extractTagName(buffer), getAttributes(buffer)); } else { handler.endElement(extractTagName(buffer)); } buffer.setLength(0); isInsideTag = false; } lastChar = c; } if (isInsideTag) { throw new InvalidFormatException("Did not find matching > char!"); } } }
SgmlParser { public void parse(Reader in, ContentHandler handler) throws IOException { StringBuilder buffer = new StringBuilder(); boolean isInsideTag = false; boolean isStartTag = true; int lastChar = -1; int c; while ((c = in.read()) != -1) { if ('<' == c) { if (isInsideTag) { throw new InvalidFormatException("Did not expect < char!"); } if (buffer.toString().trim().length() > 0) { handler.characters(buffer.toString().trim()); } buffer.setLength(0); isInsideTag = true; isStartTag = true; } buffer.appendCodePoint(c); if ('/' == c && lastChar == '<') { isStartTag = false; } if ('>' == c) { if (!isInsideTag) { throw new InvalidFormatException("Did not expect > char!"); } if (isStartTag) { handler.startElement(extractTagName(buffer), getAttributes(buffer)); } else { handler.endElement(extractTagName(buffer)); } buffer.setLength(0); isInsideTag = false; } lastChar = c; } if (isInsideTag) { throw new InvalidFormatException("Did not find matching > char!"); } }  }
SgmlParser { public void parse(Reader in, ContentHandler handler) throws IOException { StringBuilder buffer = new StringBuilder(); boolean isInsideTag = false; boolean isStartTag = true; int lastChar = -1; int c; while ((c = in.read()) != -1) { if ('<' == c) { if (isInsideTag) { throw new InvalidFormatException("Did not expect < char!"); } if (buffer.toString().trim().length() > 0) { handler.characters(buffer.toString().trim()); } buffer.setLength(0); isInsideTag = true; isStartTag = true; } buffer.appendCodePoint(c); if ('/' == c && lastChar == '<') { isStartTag = false; } if ('>' == c) { if (!isInsideTag) { throw new InvalidFormatException("Did not expect > char!"); } if (isStartTag) { handler.startElement(extractTagName(buffer), getAttributes(buffer)); } else { handler.endElement(extractTagName(buffer)); } buffer.setLength(0); isInsideTag = false; } lastChar = c; } if (isInsideTag) { throw new InvalidFormatException("Did not find matching > char!"); } }  void parse(Reader in, ContentHandler handler); }
SgmlParser { public void parse(Reader in, ContentHandler handler) throws IOException { StringBuilder buffer = new StringBuilder(); boolean isInsideTag = false; boolean isStartTag = true; int lastChar = -1; int c; while ((c = in.read()) != -1) { if ('<' == c) { if (isInsideTag) { throw new InvalidFormatException("Did not expect < char!"); } if (buffer.toString().trim().length() > 0) { handler.characters(buffer.toString().trim()); } buffer.setLength(0); isInsideTag = true; isStartTag = true; } buffer.appendCodePoint(c); if ('/' == c && lastChar == '<') { isStartTag = false; } if ('>' == c) { if (!isInsideTag) { throw new InvalidFormatException("Did not expect > char!"); } if (isStartTag) { handler.startElement(extractTagName(buffer), getAttributes(buffer)); } else { handler.endElement(extractTagName(buffer)); } buffer.setLength(0); isInsideTag = false; } lastChar = c; } if (isInsideTag) { throw new InvalidFormatException("Did not find matching > char!"); } }  void parse(Reader in, ContentHandler handler);  }