Browse Source
Merge in CORE/base-third from ~HUGH.C/base-third:feature/10.0 to feature/10.0 * commit '252c889448d5082248441ed11a3a7830d5706c3e': REPORT-33031 review 改进 REPORT-33031 无插件PDF导出-HTML解析:块元素(如:div、p)支持 border 属性“全家桶”research/11.0
Hugh.C
5 years ago
10 changed files with 497 additions and 0 deletions
@ -0,0 +1,39 @@
|
||||
package com.fr.third.v2.lowagie.text.html; |
||||
|
||||
import java.awt.Color; |
||||
|
||||
/** |
||||
* @author Hugh.C |
||||
* @version 1.0 |
||||
* Created by Hugh.C on 2020/6/15 |
||||
*/ |
||||
public class Border { |
||||
private float width; |
||||
private String style; |
||||
private Color color = Color.BLACK; |
||||
|
||||
public float getWidth() { |
||||
return width; |
||||
} |
||||
|
||||
public void setWidth(float width) { |
||||
this.width = width; |
||||
} |
||||
|
||||
public String getStyle() { |
||||
return style; |
||||
} |
||||
|
||||
public void setStyle(String style) { |
||||
this.style = style; |
||||
} |
||||
|
||||
public Color getColor() { |
||||
return color; |
||||
} |
||||
|
||||
public void setColor(Color color) { |
||||
this.color = color; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,106 @@
|
||||
package com.fr.third.v2.lowagie.text.html; |
||||
|
||||
import java.awt.Color; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Hugh.C |
||||
* @version 1.0 |
||||
* Created by Hugh.C on 2020/6/15 |
||||
*/ |
||||
public class BorderAttribute { |
||||
protected Border top = new Border(); |
||||
protected Border right = new Border(); |
||||
protected Border bottom = new Border(); |
||||
protected Border left = new Border(); |
||||
|
||||
public Border getTop() { return top; } |
||||
|
||||
public Border getRight() { |
||||
return right; |
||||
} |
||||
|
||||
public Border getBottom() { |
||||
return bottom; |
||||
} |
||||
|
||||
public Border getLeft() { |
||||
return left; |
||||
} |
||||
|
||||
/** |
||||
* 将解析好的边框属性转化为 BorderAttribute |
||||
* |
||||
* @param map |
||||
* @return |
||||
*/ |
||||
public BorderAttribute applyBorderAttr(Map<String, String> map) { |
||||
if (null == map || map.isEmpty()) { |
||||
return this; |
||||
} |
||||
String topWidth = map.get(Markup.CSS_KEY_BORDERWIDTHTOP); |
||||
String style = map.get(Markup.CSS_KEY_BORDERSTYLETOP); |
||||
if (verify(topWidth, style)) { |
||||
Color color = Markup.decodeColor(map.get(Markup.CSS_KEY_BORDERCOLORTOP)); |
||||
top.setWidth(CSSUtils.parseFloat(topWidth)); |
||||
top.setStyle(style); |
||||
top.setColor(null == color ? Color.BLACK : color); |
||||
} |
||||
String rightWidth = map.get(Markup.CSS_KEY_BORDERWIDTHRIGHT); |
||||
style = map.get(Markup.CSS_KEY_BORDERSTYLERIGHT); |
||||
if (verify(rightWidth, style)) { |
||||
Color color = Markup.decodeColor(map.get(Markup.CSS_KEY_BORDERCOLORRIGHT)); |
||||
right.setWidth(CSSUtils.parseFloat(rightWidth)); |
||||
right.setStyle(style); |
||||
right.setColor(null == color ? Color.BLACK : color); |
||||
} |
||||
String bottomWidth = map.get(Markup.CSS_KEY_BORDERWIDTHBOTTOM); |
||||
style = map.get(Markup.CSS_KEY_BORDERSTYLEBOTTOM); |
||||
if (verify(bottomWidth, style)) { |
||||
Color color = Markup.decodeColor(map.get(Markup.CSS_KEY_BORDERCOLORBOTTOM)); |
||||
bottom.setWidth(CSSUtils.parseFloat(bottomWidth)); |
||||
bottom.setStyle(style); |
||||
bottom.setColor(null == color ? Color.BLACK : color); |
||||
} |
||||
String leftWidth = map.get(Markup.CSS_KEY_BORDERWIDTHLEFT); |
||||
style = map.get(Markup.CSS_KEY_BORDERSTYLELEFT); |
||||
if (verify(leftWidth, style)) { |
||||
Color color = Markup.decodeColor(map.get(Markup.CSS_KEY_BORDERCOLORLEFT)); |
||||
left.setWidth(CSSUtils.parseFloat(leftWidth)); |
||||
left.setStyle(style); |
||||
left.setColor(null == color ? Color.BLACK : color); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* 将边框属性 拼接成 html style 中的值 |
||||
* |
||||
* @return |
||||
*/ |
||||
public String toStyleAttr() { |
||||
StringBuffer sb = new StringBuffer(); |
||||
sb.append(border2StyleAttr(Markup.CSS_KEY_BORDERTOP, top)).append(border2StyleAttr(Markup.CSS_KEY_BORDERRIGHT, right)) |
||||
.append(border2StyleAttr(Markup.CSS_KEY_BORDERBOTTOM, bottom)).append(border2StyleAttr(Markup.CSS_KEY_BORDERLEFT, left)); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
private String border2StyleAttr(String key, Border border) { |
||||
if (!verify(border)) { |
||||
return ""; |
||||
} |
||||
Color color = null == border.getColor() ? Color.BLACK : border.getColor(); |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append(key).append(":").append(border.getWidth()).append("px ") |
||||
.append(border.getStyle()).append(" ").append(null == color ? "" : "rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ");"); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
private boolean verify(String width, String style) { |
||||
return null != width && width.length() > 0 && null != style && style.length() > 0; |
||||
} |
||||
|
||||
private boolean verify(Border border) { |
||||
return null != border && border.getWidth() > 0 && null != border.getStyle(); |
||||
} |
||||
} |
@ -0,0 +1,79 @@
|
||||
package com.fr.third.v2.lowagie.text.html; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Hugh.C |
||||
* @version 1.0 |
||||
* Created by Hugh.C on 2020/6/15 |
||||
*/ |
||||
public enum BorderEnum { |
||||
TOP { |
||||
@Override |
||||
public void fillAttr(Map<String, String> map, List<String> borderAttr) { |
||||
map.put(Markup.CSS_KEY_BORDERWIDTHTOP, borderAttr.get(0)); |
||||
map.put(Markup.CSS_KEY_BORDERSTYLETOP, borderAttr.get(1)); |
||||
map.put(Markup.CSS_KEY_BORDERCOLORTOP, borderAttr.get(2)); |
||||
} |
||||
}, |
||||
RIGHT { |
||||
@Override |
||||
public void fillAttr(Map<String, String> map, List<String> borderAttr) { |
||||
map.put(Markup.CSS_KEY_BORDERWIDTHRIGHT, borderAttr.get(0)); |
||||
map.put(Markup.CSS_KEY_BORDERSTYLERIGHT, borderAttr.get(1)); |
||||
map.put(Markup.CSS_KEY_BORDERCOLORRIGHT, borderAttr.get(2)); |
||||
} |
||||
}, |
||||
BOTTOM { |
||||
@Override |
||||
public void fillAttr(Map<String, String> map, List<String> borderAttr) { |
||||
map.put(Markup.CSS_KEY_BORDERWIDTHBOTTOM, borderAttr.get(0)); |
||||
map.put(Markup.CSS_KEY_BORDERSTYLEBOTTOM, borderAttr.get(1)); |
||||
map.put(Markup.CSS_KEY_BORDERCOLORBOTTOM, borderAttr.get(2)); |
||||
} |
||||
}, |
||||
LEFT { |
||||
public void fillAttr(Map<String, String> map, List<String> borderAttr) { |
||||
map.put(Markup.CSS_KEY_BORDERWIDTHLEFT, borderAttr.get(0)); |
||||
map.put(Markup.CSS_KEY_BORDERSTYLELEFT, borderAttr.get(1)); |
||||
map.put(Markup.CSS_KEY_BORDERCOLORLEFT, borderAttr.get(2)); |
||||
} |
||||
}, |
||||
|
||||
DEFAULT { |
||||
@Override |
||||
public void fillAttr(Map<String, String> map, List<String> borderAttr) { |
||||
} |
||||
}; |
||||
|
||||
public static BorderEnum get(String str) { |
||||
switch (str) { |
||||
case Markup.CSS_KEY_BORDERTOP: |
||||
return TOP; |
||||
case Markup.CSS_KEY_BORDERRIGHT: |
||||
return RIGHT; |
||||
case Markup.CSS_KEY_BORDERBOTTOM: |
||||
return BOTTOM; |
||||
case Markup.CSS_KEY_BORDERLEFT: |
||||
return LEFT; |
||||
default: |
||||
return DEFAULT; |
||||
} |
||||
} |
||||
|
||||
public abstract void fillAttr(Map<String, String> map, List<String> borderAttr); |
||||
|
||||
public void dealAttr(Map<String, String> map, List<String> borderAttr) { |
||||
if (null == map || null == borderAttr || borderAttr.size() > 3 || borderAttr.size() < 2) { |
||||
return; |
||||
} |
||||
//保证 borderAttr 大小为3,即 线条粗细、样式、颜色都需要具备
|
||||
if (2 == borderAttr.size()) { |
||||
borderAttr.add("black"); |
||||
} |
||||
fillAttr(map,borderAttr); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,83 @@
|
||||
package com.fr.third.v2.lowagie.text.html.utils; |
||||
|
||||
|
||||
import com.fr.third.v2.lowagie.text.html.BorderEnum; |
||||
import com.fr.third.v2.lowagie.text.html.HtmlTags; |
||||
import com.fr.third.v2.lowagie.text.html.Markup; |
||||
import com.fr.third.v2.lowagie.text.html.simpleparser.ChainedProperties; |
||||
import com.fr.third.v2.lowagie.text.html.simpleparser.HtmlConstants; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Hugh.C |
||||
* @version 1.0 |
||||
* Created by Hugh.C on 2020/5/27 |
||||
*/ |
||||
public class BorderUtils { |
||||
|
||||
/** |
||||
* 解析border属性 |
||||
* |
||||
* @param props |
||||
* @param lastTag true:解析最后一个tag 的边框属性 |
||||
*/ |
||||
public static Map<String, String> parse2RulesMap(ChainedProperties props, boolean lastTag) { |
||||
Map<String, String> map = new HashMap<String, String>(); |
||||
String value = lastTag ? props.getLastChainProperty(Markup.CSS_KEY_BORDER) : props.getPropertyFromChain(HtmlTags.DIV, Markup.CSS_KEY_BORDER); |
||||
//解析 border
|
||||
if (null != value) { |
||||
List<String> list = Markup.parseBorderAttr(value); |
||||
for (BorderEnum border : BorderEnum.values()) { |
||||
border.dealAttr(map, list); |
||||
} |
||||
} |
||||
|
||||
//解析 border-width
|
||||
parseBorderAttr(Markup.CSS_KEY_BORDERWIDTH, HtmlConstants.BORDER_WIDTH, map, props, lastTag); |
||||
//解析 border-style
|
||||
parseBorderAttr(Markup.CSS_KEY_BORDERSTYLE, HtmlConstants.BORDER_STYLE, map, props, lastTag); |
||||
//解析 border-color
|
||||
parseBorderAttr(Markup.CSS_KEY_BORDERCOLOR, HtmlConstants.BORDER_COLOR, map, props, lastTag); |
||||
|
||||
//解析 border-top|right|bottom|left
|
||||
for (String border : HtmlConstants.BORDER) { |
||||
value = lastTag ? props.getLastChainProperty(border) : props.getPropertyFromChain(HtmlTags.DIV, border); |
||||
if (null != value) { |
||||
BorderEnum.get(border).dealAttr(map, Markup.parseBorderAttr(value)); |
||||
} |
||||
} |
||||
|
||||
//解析 border-top|right|bottom|left-width
|
||||
parseBorderAttr(HtmlConstants.BORDER_WIDTH, map, props, lastTag); |
||||
//解析 border-top|right|bottom|left-style
|
||||
parseBorderAttr(HtmlConstants.BORDER_STYLE, map, props, lastTag); |
||||
//解析 border-top|right|bottom|left-color
|
||||
parseBorderAttr(HtmlConstants.BORDER_COLOR, map, props, lastTag); |
||||
return map; |
||||
} |
||||
|
||||
private static void parseBorderAttr(List<String> attrKeys, Map<String, String> map, ChainedProperties props, boolean lastTag) { |
||||
String value = null; |
||||
for (String key : attrKeys) { |
||||
value = lastTag ? props.getLastChainProperty(key) : props.getPropertyFromChain(HtmlTags.DIV, key); |
||||
if (null != value) { |
||||
map.put(key, value); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static void parseBorderAttr(String key, List<String> attrKeys, Map<String, String> map, ChainedProperties props, boolean lastTag) { |
||||
String value = lastTag ? props.getLastChainProperty(key) : props.getPropertyFromChain(HtmlTags.DIV, key); |
||||
if (null != value) { |
||||
List<String> list = Markup.parseNESW(value); |
||||
if (null != list) { |
||||
for (int i = 0; i < attrKeys.size() && i < list.size(); i++) { |
||||
map.put(attrKeys.get(i), list.get(i)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue