Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * You are given an HTML code, written in the old non-semantic style using tags like <div id="header">, <div class="section">, etc.
- * Your task is to write a program that converts this HTML to semantic HTML by changing tags like <div id="header">
- to their semantic equivalent like <header>.
- * The non-semantic tags that should be converted are always <div>s and have either id or class with one of the following values:
- "main", "header", "nav", "article", "section", "aside" or "footer".
- * Their corresponding closing tags are always followed by a comment like <!-- header -->, <!-- nav -->, etc.
- staying at the same line, after the tag.
- * Each line from the input holds either an HTML opening tag or an HTML closing tag or HTML text content.
- * There will be no tags that span several lines and no lines that hold multiple tags.
- * Attributes values will always be enclosed in double quotes ".
- * Tags will never have id and class at the same time.
- * The HTML will be valid. Opening and closing tags will match correctly.
- * Whitespace may occur between attribute names, values and around comments.
- * The Input will be read from the console.
- * It will contain a variable number of lines and will end with the keyword "END".
- * Output Format:
- * The Output is the semantic version of the input HTML.
- * In all converted tags you should replace multiple spaces (like <header style="color:red">) with a single space
- * and remove excessive spaces at the end (like <footer >).
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class SemanticHTML {
- public static void main(String[] args) throws IOException {
- Pattern openTagPattern = Pattern
- .compile("(\\s*<)div\\s(.*?)\\s?((?:id|class)\\s*=\\s*\"(?<tag>[a-z]+)\")\\s?(.*?)\\s?>");
- Pattern closedTagPattern = Pattern.compile("(\\s*</)div>\\s+<!--\\s*(?<tag>[a-z]+)\\s*-->");
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(System.in));
- String inputLine = reader.readLine();
- Matcher openMatch;
- Matcher closedMatch;
- while (!inputLine.equals("END")) {
- openMatch = openTagPattern.matcher(inputLine);
- closedMatch = closedTagPattern.matcher(inputLine);
- if (openMatch.find()) {
- System.out.println(convertToOpeningSemanticTag(openMatch));
- } else if (closedMatch.find()) {
- System.out.printf("%s%s>\n", closedMatch.group(1), closedMatch.group("tag"));
- } else {
- System.out.println(inputLine);
- }
- inputLine = reader.readLine();
- }
- }
- private static String convertToOpeningSemanticTag(Matcher fullMatch) {
- StringBuilder sb = new StringBuilder();
- sb.append(fullMatch.group(1)).append(fullMatch.group("tag"));
- String firstAdd = fullMatch.group(2).trim().replaceAll("\\s+", " ");
- String secondAdd = fullMatch.group(5).trim().replaceAll("\\s+", " ");
- if (firstAdd.length() > 0) {
- sb.append(' ').append(firstAdd);
- }
- if (secondAdd.length() > 0) {
- sb.append(' ').append(secondAdd);
- }
- return sb.append('>').toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement