HtmlStatusResponseParser.java 2.69 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.crx.packaging;

import com.day.crx.packaging.JSONResponse;
import com.day.crx.packaging.impl.proxy.AttributeList;
import com.day.crx.packaging.impl.proxy.DocumentHandler;
import com.day.crx.packaging.impl.proxy.HtmlParser;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class HtmlStatusResponseParser {
    private static final Set<String> TAGS;

    public static JSONResponse parse(String data) throws IOException {
        Handler h = new Handler();
        HtmlParser p = new HtmlParser();
        p.setDocumentHandler(h);
        p.setTagInclusionSet(TAGS);
        p.update(data.toCharArray(), 0, data.length());
        p.finished();
        return h.resp;
    }

    static {
        HashSet<String> tags = new HashSet<String>();
        tags.add("A");
        tags.add("DIV");
        TAGS = Collections.unmodifiableSet(tags);
    }

    private static class Handler
    implements DocumentHandler {
        private String id = null;
        private int statusCode = 0;
        private String statusMessage = "";
        private final JSONResponse resp = new JSONResponse();

        private Handler() {
        }

        @Override
        public void characters(char[] ch, int off, int len) throws IOException {
            if (this.id == null) {
                return;
            }
            String str = new String(ch, off, len);
            if ("Status".equals(this.id)) {
                this.statusCode = Integer.parseInt(str);
                this.resp.setStatus(this.statusCode);
                this.resp.setSuccess(this.statusCode == 200);
            } else if ("Message".equals(this.id)) {
                this.statusMessage = str;
                this.resp.setMessage(this.statusMessage);
            } else if ("Path".equals(this.id)) {
                this.resp.setPath(str);
            } else if (!"Referer".equals(this.id)) {
                if ("Location".equals(this.id)) {
                    this.resp.setLocation(str);
                } else if ("ParentLocation".equals(this.id)) {
                    // empty if block
                }
            }
            this.id = null;
        }

        @Override
        public void onStartElement(String name, AttributeList attList, char[] ch, int off, int len, boolean endSlash) throws IOException {
            this.id = attList.getValue("id");
        }

        @Override
        public void onEndElement(String name, char[] ch, int off, int len) throws IOException {
        }

        @Override
        public void onStart() throws IOException {
        }

        @Override
        public void onEnd() throws IOException {
        }
    }

}