Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2005, 2006, 2007 Henri Sivonen
   * Copyright (c) 2007 Mozilla Foundation
   *
   * Permission is hereby granted, free of charge, to any person obtaining a 
   * copy of this software and associated documentation files (the "Software"), 
   * to deal in the Software without restriction, including without limitation 
   * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
   * and/or sell copies of the Software, and to permit persons to whom the 
  * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in 
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
  * DEALINGS IN THE SOFTWARE.
  */
 
 package nu.validator.messages;
 
 

Author(s):
hsivonen
Version:
$Id: XhtmlMessageEmitter.java 54 2007-09-20 15:37:38Z hsivonen $
 
 public class XhtmlMessageEmitter extends MessageEmitter implements ImageReviewHandler {
 
     private static final int IMAGE_CLAMP = 180;
     
     private static final char[] COLON_SPACE = { ':'' ' };
 
     private static final char[] PERIOD = { '.' };
 
     private static final char[] ON_LINE = "On line ".toCharArray();
 
     private static final char[] AT_LINE = "At line ".toCharArray();
 
     private static final char[] FROM_LINE = "From line ".toCharArray();
 
     private static final char[] TO_LINE = "; to line ".toCharArray();
 
     private static final char[] COLUMN = ", column ".toCharArray();
 
     private static final char[] IN_RESOURCE = " in resource ".toCharArray();
 
     private static final char[] NOT_RESOLVABLE = "Not resolvable".toCharArray();
 
     private static final char[] EMPTY_STRING_AS_ALT = "Omit image in non-graphical presentation".toCharArray();
 
     private static final char[] NO_ALT = "Not available".toCharArray();
 
     private static final char[] IMAGE = "Image".toCharArray();
 
     private static final char[] TEXTUAL_ALTERNATIVE = "Textual alternative".toCharArray();
 
     private static final char[] LOCATION = "Location".toCharArray();
 
     private static final char[] IMAGE_REPORT = "Image report".toCharArray();
 
     private static final char[] SOURCE_CODE = "Source".toCharArray();
     
     private final AttributesImpl attrs = new AttributesImpl();
     
     private boolean listOpen = false;
 
     private final ContentHandler contentHandler;
 
     private final XhtmlSaxEmitter emitter;
 
     private final XhtmlMessageTextHandler messageTextHandler;
 
     private final XhtmlExtractHandler extractHandler;
 
     private boolean textEmitted;
 
     private String systemId;
 
     private int oneBasedFirstLine;
 
     private int oneBasedFirstColumn;
 
     private int oneBasedLastLine;
 
     private int oneBasedLastColumn;
    private boolean willShowSource;
    
    private final TreeParser treeParser;

    

Parameters:
contentHandler
    public XhtmlMessageEmitter(ContentHandler contentHandler) {
        super();
        this. = contentHandler;
        this. = new XhtmlSaxEmitter(contentHandler);
        this. = new XhtmlMessageTextHandler();
        this. = new XhtmlExtractHandler();
        this. = new TreeParser(contentHandlernull);
    }
    private void maybeOpenList() throws SAXException {
        if (!this.) {
            this..startElement("ol");
            this. = true;
        }
    }
    private void emitErrorLevel(char[] levelthrows SAXException {
        this..startElement("strong");
        this..characters(level);
        this..endElement("strong");
    }
    @Override
    public void endMessage() throws SAXException {
        maybeCloseTextPara();
        this..endElement("li");
    }
    private void maybeCloseTextPara() throws SAXException {
        if (!) {
            this..characters();
            this..endElement("p");
            maybeEmitLocation(true);
        }
    }
    private void maybeEmitLocation(boolean withParathrows SAXException {
        if ( == -1 &&  == null) {
            return;
        }
        if (withPara) {
            this..startElementWithClass("p""location");
        }
        if ( == -1) {
            emitSystemId();
        } else if ( == -1) {
            emitLineLocation();
        } else if ( == -1
                || ( ==  &&  == )) {
            emitSingleLocation();
        } else {
            emitRangeLocation();
        }
        if (withPara) {
            this..endElement("p");
        }
    }

    
    private void maybeEmitInResource() throws SAXException {
        if ( != null) {
            this..characters();
            emitSystemId();
        }
    }

    
    private void emitSystemId() throws SAXException {
        this..startElementWithClass("span""url");
        this..characters();
        this..endElement("span");
    }
    private void emitRangeLocation() throws SAXException {
        if ( &&  == null) {
            .clear();
            .addAttribute("href""#l" +  + "c" + );
            .startElement("a");
        }
        this..characters();
        this..startElementWithClass("span""first-line");
        this..characters(Integer.toString());
        this..endElement("span");
        this..characters();
        this..startElementWithClass("span""first-col");
        this..characters(Integer.toString());
        this..endElement("span");
        this..characters();
        this..startElementWithClass("span""last-line");
        this..characters(Integer.toString());
        this..endElement("span");
        this..characters();
        this..startElementWithClass("span""last-col");
        this..characters(Integer.toString());
        this..endElement("span");
        maybeEmitInResource();
        if ( &&  == null) {
            .endElement("a");
        }
    }
    private void emitSingleLocation() throws SAXException {
        if ( &&  == null) {
            .clear();
            .addAttribute("href""#cl" +  + "c" + );
            .startElement("a");
        }
        this..characters();
        this..startElementWithClass("span""last-line");
        this..characters(Integer.toString());
        this..endElement("span");
        this..characters();
        this..startElementWithClass("span""last-col");
        this..characters(Integer.toString());
        this..endElement("span");
        maybeEmitInResource();
        if ( &&  == null) {
            .endElement("a");
        }
    }
    private void emitLineLocation() throws SAXException {
        if ( &&  == null) {
            .clear();
            .addAttribute("href""#l" + );
            .startElement("a");
        }
        this..characters();
        this..startElementWithClass("span""last-line");
        this..characters(Integer.toString());
        this..endElement("span");
        maybeEmitInResource();
        if ( &&  == null) {
            .endElement("a");
        }
    }
    @Override
    public void startMessage(MessageType typeString aSystemId,
            int aOneBasedFirstLineint aOneBasedFirstColumn,
            int aOneBasedLastLineint aOneBasedLastColumnboolean exact)
            throws SAXException {
        this. = aSystemId;
        this. = aOneBasedFirstLine;
        this. = aOneBasedFirstColumn;
        this. = aOneBasedLastLine;
        this. = aOneBasedLastColumn;
        this.maybeOpenList();
        this..startElementWithClass("li"type.getFlatType());
        this..startElement("p");
        emitErrorLevel(type.getPresentationName());
        this. = false;
    }

    
    @Override
    public void endMessages() throws SAXException {
        maybeCloseList();
    }

    
    private void maybeCloseList() throws SAXException {
        if (this.) {
            this..endElement("ol");
            this. = false;
        }
    }

    
    @Override
    public void endText() throws SAXException {
        this..endElement("span");
        this..endElement("p");
        this. = true;
        maybeEmitLocation(true);
    }

    
    @Override
    public void startMessages(String documentUri, @SuppressWarnings("hiding")
    boolean willShowSourcethrows SAXException {
        this. = willShowSource;
    }

    
    @Override
    public MessageTextHandler startText() throws SAXException {
        this..characters();
        this..startElement("span");
        return ;
    }

    
    @Override
    public void endSource() throws SAXException {
        .endElement("code");
        .endElement("p");
    }

    
    @Override
    public SourceHandler startSource() throws SAXException {
        maybeCloseTextPara();
        .startElementWithClass("p""extract");
        .startElement("code");
        return ;
    }

    
    @Override
    public void endFullSource() throws SAXException {
    }

    
    @Override
    public SourceHandler startFullSource(int lineOffsetthrows SAXException {
        maybeCloseList();
        
        .clear();
        .addAttribute("id""source");
        this..startElement("h2");
        this..characters();
        this..endElement("h2");
        
        return new XhtmlSourceHandler(lineOffset);
    }

    
    @Override
    public void endResult() throws SAXException {
    }

    
    @Override
    public ResultHandler startResult() throws SAXException {
        maybeCloseList();
        return new XhtmlResultHandler();
    }

    
    @Override
    public void endElaboration() throws SAXException {
    }

    
    @Override
    public ContentHandler startElaboration() throws SAXException {
        return ;
    }

    
    @Override
    public void endImageReview() throws SAXException {
    }

    
    @Override
    public ImageReviewHandler startImageReview(DocumentFragment instructionboolean fatalthrows SAXException {
        .clear();
        .addAttribute("id""imagereport");
        this..startElement("h2");
        this..characters();
        this..endElement("h2");
        
        .parse(instruction);
        
        return this;
    }
    public void endImageGroup() throws SAXException {
        this..endElement("tbody");
        this..endElement("table");
    }
    public void image(Image imageboolean showAltString aSystemId,
            int aOneBasedFirstLineint aOneBasedFirstColumn,
            int aOneBasedLastLineint aOneBasedLastColumnthrows SAXException {
        this. = null;
        this. = aOneBasedFirstLine;
        this. = aOneBasedFirstColumn;
        this. = aOneBasedLastLine;
        this. = aOneBasedLastColumn;
        this..startElement("tr");
        
        imageCell(image);
        if (showAlt) {
            altCell(image.getAlt(), image.getLang(), image.isRtl());
        }
        locationCell();
        
        this..endElement("tr");
    }
    private void locationCell() throws SAXException {
        this..startElementWithClass("td""location");       
        maybeEmitLocation(false);
        this..endElement("td");
    }
    private void altCell(String altString langboolean rtlthrows SAXException {
        .clear();
        .addAttribute("class""alt");
        if (rtl && !(alt == null || "".equals(alt))) {
            .addAttribute("dir""rtl");
        }        
        this..startElement("td");       
        if (alt == null) {
            this..startElement("i");       
            this..characters();
            this..endElement("i");            
        } else if ("".equals(alt)) {
            this..startElement("i");       
            this..characters();
            this..endElement("i");                        
        } else {
            .clear();
            .addAttribute("http://www.w3.org/XML/1998/namespace""lang""xml:lang""CDATA"lang);
            this..startElement("span");       
            this..characters(alt);
            this..endElement("span");                                    
        }
        this..endElement("td");        
    }
    private void imageCell(Image imagethrows SAXException {
        this..startElementWithClass("td""img");       
        String src = image.getSrc();
        if (src == null) {
            this..startElement("i");       
            this..characters();
            this..endElement("i");                        
        } else {
            int width = image.getWidth();
            int height = image.getHeight();
            if (width < 1 || height < 1) {
                width = height = -1;
            } else if (width > height) {
                if (width > ) {
                    height = (int) Math.ceil(height * (((double))/((double)width)));
                    width = ;
                }
            } else {
                if (height > ) {
                    width = (int) Math.ceil(width * (((double))/((double)height)));
                    height = ;                
                }
            }
            .clear();
            .addAttribute("src"src);
            if (width != -1) {
                .addAttribute("width", Integer.toString(width));
                .addAttribute("height", Integer.toString(height));
            }
            this..startElement("img");
            this..endElement("img");            
        }
        this..endElement("td");
    }
    public void startImageGroup(char[] headingDocumentFragment instruction,
            boolean hasAltthrows SAXException {
        this..startElement("h3");               
        this..characters(heading);
        this..endElement("h3");               
        
        .parse(instruction);
        
        this..startElementWithClass("table""imagereview");     
        this..startElement("colgroup"); 
        this..startElementWithClass("col""img"); 
        this..endElement("col");               
        if (hasAlt) {
            this..startElementWithClass("col""alt"); 
            this..endElement("col");               
        }
        
        this..startElementWithClass("col""location"); 
        this..endElement("col");               
        
        this..endElement("colgroup");               
        
        this..startElement("thead");               
        this..startElement("tr");               
        this..startElementWithClass("th""img");               
        this..characters();        
        this..endElement("th");               
        if (hasAlt) {
            this..startElementWithClass("th""alt");               
            this..characters();        
            this..endElement("th");               
        }
        
        this..startElementWithClass("th""location");               
        this..characters();        
        this..endElement("th");                       
        
        this..endElement("tr");               
        this..endElement("thead");               
        this..startElement("tbody");               
    }
New to GrepCode? Check out our FAQ X