
org.ttzero.excel.entity.Comments Maven / Gradle / Ivy
/*
* Copyright (c) 2017-2020, [email protected] All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ttzero.excel.entity;
import org.ttzero.excel.manager.TopNS;
import org.ttzero.excel.entity.style.Font;
import org.ttzero.excel.manager.Const;
import org.ttzero.excel.reader.ExcelReader;
import org.ttzero.excel.util.ExtBufferedWriter;
import org.ttzero.excel.util.FileUtil;
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import static org.ttzero.excel.util.StringUtil.isNotEmpty;
/**
* 批注的简单实现
*
* @author guanquan.wang at 2020-05-21 16:31
*/
@TopNS(prefix = "", value = "comments", uri = Const.SCHEMA_MAIN)
public class Comments implements Storable, Closeable {
/** Comments Cache*/
public List commentList = new ArrayList<>();
public int id;
public String author;
public Comments() { }
public Comments(int id, String author) {
this.id = id;
this.author = author;
}
public C addComment(String ref, String title, String value) {
C c = new C();
c.ref = ref;
c.text = new ArrayList<>();
if (isNotEmpty(title)) {
parse(title, true, c.text);
}
if (isNotEmpty(value)) {
parse(value, false, c.text);
}
commentList.add(c);
return c;
}
public C addComment(String ref, Comment comment) {
C c = new C();
c.ref = ref;
c.text = new ArrayList<>();
c.width = comment.getWidth();
c.height = comment.getHeight();
if (isNotEmpty(comment.getTitle())) {
parse(comment.getTitle(), true, c.text);
}
if (isNotEmpty(comment.getValue())) {
parse(comment.getValue(), false, c.text);
}
commentList.add(c);
return c;
}
protected void parse(String val, boolean bold, List list) {
// a simple implement
R r = new R();
r.rPr = new Pr("宋体", 9);
if (bold) {
r.rPr.bold();
if (val.charAt(val.length() - 1) != 10) {
val += (char) 10;
}
}
r.t = val;
list.add(r);
}
public void flush() {
// TODO Write tmp and clear cache
}
@Override
public void close() {
// Ignore
}
@Override
public void writeTo(Path root) throws IOException {
if (commentList.isEmpty()) return;
try (ExtBufferedWriter writer = new ExtBufferedWriter(
Files.newBufferedWriter(root.resolve("comments" + id + Const.Suffix.XML)))) {
writer.write(Const.EXCEL_XML_DECLARATION);
writer.newLine();
TopNS topNS = this.getClass().getAnnotation(TopNS.class);
writer.write('<');
writer.write(topNS.value());
writer.write(" xmlns=\"");
writer.write(topNS.uri()[0]);
writer.write("\">");
writer.escapeWrite(isNotEmpty(author) ? author : System.getProperty("user.name"));
writer.write(" ");
for (C c : commentList) {
writer.write("");
for (R r : c.text) {
writer.write("");
writer.write(r.rPr.toString());
writer.write(" 0 ? " xml:space=\"preserve\">" : ">"));
writer.escapeWrite(r.t);
writer.write(" ");
}
writer.write(" ");
}
writer.write(" ");
}
// Write vml
vml(root);
}
protected void vml(Path root) throws IOException {
Path parent = root.resolve("drawings");
if (!Files.exists(parent)) {
FileUtil.mkdir(parent);
}
try (ExtBufferedWriter writer = new ExtBufferedWriter(
Files.newBufferedWriter(parent.resolve("vmlDrawing" + id + Const.Suffix.VML)))) {
writer.write("");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
int i = 1;
for (C c : commentList) {
long cr = ExcelReader.coordinateToLong(c.ref);
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" ");
writer.write(" False ");
writer.write(" ");writer.write((cr >> 16) - 1);writer.write(" ");
writer.write(" ");writer.write((cr & 0x7FFF) - 1);writer.write(" ");
writer.write(" ");
writer.write(" ");
}
writer.write(" ");
}
}
public static class C {
public String ref;
public List text;
public Double width, height;
@Override
public String toString() {
StringBuilder buf = new StringBuilder("");
for (R r : text)
buf.append(r);
buf.append(" ").append(" ");
return buf.toString();
}
}
public static class R {
public Pr rPr;
public String t;
@Override
public String toString() {
return "" + rPr + " 0 ? " xml:space=\"preserve\">" : ">") +
t + " " + " ";
}
}
public static class Pr extends Font {
public static final String[] STYLE = {"", "", "", "", "", "", "", ""};
public Pr(String name, int size) {
super(name, size);
}
@Override
public String toString() {
return "" + STYLE[getStyle() & 0x07] +
" " +
" " +
" ";
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy