@Test public void parseHtmlFragmentMultiRoot() { List<String> errorList = UtilHtml.hasUnclosedTag("<div></div><div></div>"); assertEquals(0, errorList.size()); }
public static List<String> hasUnclosedTag(String content) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = null; List<String> errorList = new ArrayList<>(); try { eventReader = inputFactory.createXMLEventReader( new ByteArrayInputStream(("<template>" + content + "</template>").getBytes(StandardCharsets.UTF_8)), "utf-8"); Stack<StartElement> stack = new Stack<StartElement>(); while (eventReader.hasNext()) { try { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); stack.push(startElement); } if (event.isEndElement()) { EndElement endElement = event.asEndElement(); stack.pop(); } } catch (XMLStreamException e) { if (!stack.isEmpty()) { StartElement startElement = stack.pop(); String elementName = startElement.getName().getLocalPart(); if (Arrays.stream(TAG_SHOULD_CLOSE_LIST).anyMatch(elementName::equals)) { errorList.add(e.getMessage()); } } else { errorList.add(e.getMessage()); } break; } } } catch (XMLStreamException e) { errorList.add(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException e) { Debug.logError(e, MODULE); } } } return errorList; }
UtilHtml { public static List<String> hasUnclosedTag(String content) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = null; List<String> errorList = new ArrayList<>(); try { eventReader = inputFactory.createXMLEventReader( new ByteArrayInputStream(("<template>" + content + "</template>").getBytes(StandardCharsets.UTF_8)), "utf-8"); Stack<StartElement> stack = new Stack<StartElement>(); while (eventReader.hasNext()) { try { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); stack.push(startElement); } if (event.isEndElement()) { EndElement endElement = event.asEndElement(); stack.pop(); } } catch (XMLStreamException e) { if (!stack.isEmpty()) { StartElement startElement = stack.pop(); String elementName = startElement.getName().getLocalPart(); if (Arrays.stream(TAG_SHOULD_CLOSE_LIST).anyMatch(elementName::equals)) { errorList.add(e.getMessage()); } } else { errorList.add(e.getMessage()); } break; } } } catch (XMLStreamException e) { errorList.add(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException e) { Debug.logError(e, MODULE); } } } return errorList; } }
UtilHtml { public static List<String> hasUnclosedTag(String content) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = null; List<String> errorList = new ArrayList<>(); try { eventReader = inputFactory.createXMLEventReader( new ByteArrayInputStream(("<template>" + content + "</template>").getBytes(StandardCharsets.UTF_8)), "utf-8"); Stack<StartElement> stack = new Stack<StartElement>(); while (eventReader.hasNext()) { try { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); stack.push(startElement); } if (event.isEndElement()) { EndElement endElement = event.asEndElement(); stack.pop(); } } catch (XMLStreamException e) { if (!stack.isEmpty()) { StartElement startElement = stack.pop(); String elementName = startElement.getName().getLocalPart(); if (Arrays.stream(TAG_SHOULD_CLOSE_LIST).anyMatch(elementName::equals)) { errorList.add(e.getMessage()); } } else { errorList.add(e.getMessage()); } break; } } } catch (XMLStreamException e) { errorList.add(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException e) { Debug.logError(e, MODULE); } } } return errorList; } private  UtilHtml(); }
UtilHtml { public static List<String> hasUnclosedTag(String content) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = null; List<String> errorList = new ArrayList<>(); try { eventReader = inputFactory.createXMLEventReader( new ByteArrayInputStream(("<template>" + content + "</template>").getBytes(StandardCharsets.UTF_8)), "utf-8"); Stack<StartElement> stack = new Stack<StartElement>(); while (eventReader.hasNext()) { try { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); stack.push(startElement); } if (event.isEndElement()) { EndElement endElement = event.asEndElement(); stack.pop(); } } catch (XMLStreamException e) { if (!stack.isEmpty()) { StartElement startElement = stack.pop(); String elementName = startElement.getName().getLocalPart(); if (Arrays.stream(TAG_SHOULD_CLOSE_LIST).anyMatch(elementName::equals)) { errorList.add(e.getMessage()); } } else { errorList.add(e.getMessage()); } break; } } } catch (XMLStreamException e) { errorList.add(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException e) { Debug.logError(e, MODULE); } } } return errorList; } private  UtilHtml(); static List<ParseError> validateHtmlFragmentWithJSoup(String content); static List<String> hasUnclosedTag(String content); static List<String> getVisualThemeFolderNamesToExempt(); static void logHtmlWarning(String content, String location, String error, String module); }
UtilHtml { public static List<String> hasUnclosedTag(String content) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = null; List<String> errorList = new ArrayList<>(); try { eventReader = inputFactory.createXMLEventReader( new ByteArrayInputStream(("<template>" + content + "</template>").getBytes(StandardCharsets.UTF_8)), "utf-8"); Stack<StartElement> stack = new Stack<StartElement>(); while (eventReader.hasNext()) { try { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); stack.push(startElement); } if (event.isEndElement()) { EndElement endElement = event.asEndElement(); stack.pop(); } } catch (XMLStreamException e) { if (!stack.isEmpty()) { StartElement startElement = stack.pop(); String elementName = startElement.getName().getLocalPart(); if (Arrays.stream(TAG_SHOULD_CLOSE_LIST).anyMatch(elementName::equals)) { errorList.add(e.getMessage()); } } else { errorList.add(e.getMessage()); } break; } } } catch (XMLStreamException e) { errorList.add(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException e) { Debug.logError(e, MODULE); } } } return errorList; } private  UtilHtml(); static List<ParseError> validateHtmlFragmentWithJSoup(String content); static List<String> hasUnclosedTag(String content); static List<String> getVisualThemeFolderNamesToExempt(); static void logHtmlWarning(String content, String location, String error, String module);  }