use of org.apache.xmlbeans.XmlCursor in project pmph by BCSquad.
the class WordHelper method fromDeclarationEtcBOList.
/**
* 从一个或多个专家信息对象中读取数据,转化成若干Word文档
*
* @param materialName
* 教材名称
* @param list
* DeclarationEtcBO实例集合
* @param filter
* 使用可填项的二进制选中
* @param extensions
* 教材扩展项
* @return 包含文档名称和Word格式文档的键值对
* @throws CheckedServiceException
* 已检查的异常
*/
public HashMap<String, XWPFDocument> fromDeclarationEtcBOList(String materialName, List<DeclarationEtcBO> list, String filter, List<MaterialExtension> extensions) throws CheckedServiceException {
InputStream is;
XWPFDocument document;
String path = this.getClass().getClassLoader().getResource("ResumeTemplate.docx").getPath();
HashMap<String, XWPFDocument> map = new HashMap<>(list.size());
for (DeclarationEtcBO bo : list) {
try {
is = new FileInputStream(path);
document = new XWPFDocument(is);
} catch (IOException ex) {
logger.error("读取Word模板文件'ResumeTemplate.docx'时出现错误:{}", ex.getMessage());
throw new CheckedServiceException(CheckedExceptionBusiness.MATERIAL, CheckedExceptionResult.FILE_CREATION_FAILED, "未找到模板文件");
}
if (StringUtil.notEmpty(materialName)) {
List<XWPFRun> runs = document.getParagraphs().get(0).getRuns();
runs.get(0).setText(materialName.concat("申报表"), 0);
}
List<XWPFParagraph> xwpfParagraphs = document.getParagraphs();
List<XWPFTable> tables = document.getTables();
int i = 21;
XWPFTable old = tables.get(19);
for (MaterialExtension extension : extensions) {
XmlCursor cursor = xwpfParagraphs.get(i).getCTP().newCursor();
// ---这个是关键
XWPFParagraph xwpfParagraph = document.insertNewParagraph(cursor);
CTPPr pPPr = xwpfParagraph.getCTP().getPPr() != null ? xwpfParagraph.getCTP().getPPr() : xwpfParagraph.getCTP().addNewPPr();
CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing() : pPPr.addNewSpacing();
pSpacing.setLine(BigInteger.valueOf(360L));
pSpacing.setBeforeLines(BigInteger.valueOf(100L));
XWPFRun xwpfRun = xwpfParagraph.createRun();
xwpfRun.setText(extension.getExtensionName());
xwpfRun.setFontSize(12);
xwpfRun.setFontFamily("宋体");
xwpfRun.setBold(true);
/* 以下填充扩展项内容 */
cursor = xwpfParagraphs.get(i + 1).getCTP().newCursor();
XWPFTable t = document.insertNewTbl(cursor);
XWPFTableRow row = t.getRow(0);
XWPFTableCell cell = row.getCell(0);
CTTc cttc = cell.getCTTc();
CTTcPr tcpr = cttc.addNewTcPr();
CTTblWidth tcw = tcpr.addNewTcW();
tcw.setType(old.getRow(0).getCell(0).getCTTc().getTcPr().getTcW().getType());
tcw.setW(old.getRow(0).getCell(0).getCTTc().getTcPr().getTcW().getW());
if (old.getRow(0).getCell(0).getCTTc().getTcPr().getGridSpan() != null) {
tcpr.setGridSpan(old.getRow(0).getCell(0).getCTTc().getTcPr().getGridSpan());
}
if (!bo.getDecExtensionVOs().isEmpty()) {
for (DecExtensionVO decExtensionVO : bo.getDecExtensionVOs()) {
if (extension.getId().equals(decExtensionVO.getExtensionId())) {
String content = decExtensionVO.getContent();
if (StringUtil.notEmpty(content)) {
XWPFRun run = cell.getParagraphs().get(0).createRun();
run.setText(content);
}
break;
}
}
}
i++;
}
/* 申报单位 */
String chosenOrgName = bo.getChosenOrgName();
if (StringUtil.notEmpty(chosenOrgName)) {
xwpfParagraphs.get(i).createRun().setText(chosenOrgName);
}
String filename = generateFileName(bo);
fillDeclarationPosition(tables.get(0), bo);
fillDeclarationData(tables.get(1), bo);
fillDecEduExpData(tables.get(2), bo.getDecEduExps());
fillDecWorkExpData(tables.get(3), bo.getDecWorkExps());
fillDecTeachExpData(tables.get(4), bo.getDecTeachExps());
fillDecAchievementData(tables.get(5), bo.getDecAchievement());
fillDecAcadeData(tables.get(6), bo.getDecAcades());
fillDecLastPositionData(tables.get(7), bo.getDecLastPositions());
fillDecNationalPlanData(tables.get(8), bo.getDecNationalPlans());
fillDecTextbookPmphData(tables.get(9), bo.getDecTextbookPmphs());
fillDecTextbookData(tables.get(10), bo.getDecTextbooks());
fillDecMoocDigitalData(tables.get(11), bo.getDecMoocDigital());
fillDecCourseConstructionData(tables.get(12), bo.getDecCourseConstructions());
fillDecResearchData(tables.get(13), bo.getDecResearchs());
fillDecMonographData(tables.get(14), bo.getDecMonographs());
fillDecPublishRewardData(tables.get(15), bo.getPublishRewards());
fillDecSciData(tables.get(16), bo.getDecScis());
fillDecClinicalRewardData(tables.get(17), bo.getDecClinicalRewards());
fillDecAcadeRewardData(tables.get(18), bo.getDecAcadeRewards());
fillDecIntentionData(tables.get(19), bo.getDecIntention());
map.put(filename, removeEmptyTables(document, filter));
map.put(filename, document);
}
return map;
}
use of org.apache.xmlbeans.XmlCursor in project knime-core by knime.
the class PMMLRuleTranslator method findFirst.
/**
* Finds the first xml {@link SimpleRule} within the {@code rule} {@link CompoundRule}.
*
* @param rule A {@link CompoundRule}.
* @return The first {@link SimpleRule} the should provide the outcome.
*/
private SimpleRule findFirst(final CompoundRule rule) {
XmlCursor newCursor = rule.newCursor();
if (newCursor.toFirstChild()) {
do {
XmlObject object = newCursor.getObject();
if (object instanceof SimpleRuleDocument.SimpleRule) {
SimpleRuleDocument.SimpleRule sr = (SimpleRuleDocument.SimpleRule) object;
return sr;
}
if (object instanceof CompoundRule) {
CompoundRule cp = (CompoundRule) object;
SimpleRule first = findFirst(cp);
if (first != null) {
return first;
}
}
} while (newCursor.toNextSibling());
}
assert false : rule;
return null;
}
use of org.apache.xmlbeans.XmlCursor in project knime-core by knime.
the class PMMLPortObject method addPMMLModelFromHandler.
/**
* Appends the pmml model of the content handler by invoking its
* {@link PMMLContentHandler#addPMMLModel(DocumentFragment,
* PMMLPortObjectSpec)} method.
* Only {@link PMMLModelType} elements can be added.
* @param handler PMML content handler
* @throws SAXException if the pmml model could not be added
*/
@Deprecated
public void addPMMLModelFromHandler(final PMMLContentHandler handler) throws SAXException {
XmlObject model = null;
try {
model = XmlObject.Factory.parse(handler.getPMMLModelFragment(m_spec));
} catch (Exception e) {
throw new SAXException(e);
}
PMML pmmlXml = m_pmmlDoc.getPMML();
XmlCursor pmmlCursor = pmmlXml.newCursor();
pmmlCursor.toEndToken();
XmlCursor modelCursor = model.newCursor();
modelCursor.toFirstChild();
modelCursor.copyXml(pmmlCursor);
modelCursor.dispose();
pmmlCursor.dispose();
}
use of org.apache.xmlbeans.XmlCursor in project knime-core by knime.
the class PMMLPortObject method addModelTranslater.
/**
* Adds the model of the content translater to the PMML document.
* @param modelTranslator the model translator containing the model to be
* added
*/
public void addModelTranslater(final PMMLTranslator modelTranslator) {
SchemaType type = modelTranslator.exportTo(m_pmmlDoc, m_spec);
LocalTransformations localTransformations = moveDerivedFields(type);
/* Remove mining fields from mining schema that where created as a
* derived field. In KNIME the origin of columns is not distinguished
* and all columns are added to the mining schema. But in PMML this
* results in duplicate entries. Those columns should only appear once
* as derived field in the transformation dictionary or local
* transformations. */
Set<String> derivedFields = new HashSet<String>();
for (DerivedField derivedField : getDerivedFields()) {
derivedFields.add(derivedField.getName());
}
MiningSchema miningSchema = PMMLUtils.getFirstMiningSchema(m_pmmlDoc, type);
if (miningSchema == null) {
LOGGER.info("No mining schema found.");
return;
}
MiningField[] miningFieldArray = miningSchema.getMiningFieldArray();
List<MiningField> miningFields = new ArrayList<MiningField>(Arrays.asList(miningFieldArray));
Set<String> miningFieldNames = new HashSet<String>();
for (MiningField miningField : miningFieldArray) {
String miningFieldName = miningField.getName();
if (derivedFields.contains(miningFieldName)) {
LOGGER.debug("Removing field \"" + miningFieldName + "\" from MiningFields as it is a DerivedField.");
miningFields.remove(miningField);
} else {
miningFieldNames.add(miningFieldName);
}
}
/* According to the PMML Spec DerivedFields must ultimately refer back
* to active MiningFields of the model's MiningSchema. Therefore we
* have to add all referred DataFields to the MiningSchema. */
String fullPath = NAMESPACE_DECLARATION + "$this/pmml:DerivedField/*/@field" + "| $this/pmml:DerivedField//pmml:FieldColumnPair/@field";
XmlObject[] xmlDescendants = localTransformations.selectPath(fullPath);
Set<String> referencedFields = new LinkedHashSet<String>();
// collect all referred field names
for (XmlObject xo : xmlDescendants) {
XmlCursor xmlCursor = xo.newCursor();
referencedFields.add(xmlCursor.getTextValue());
xmlCursor.dispose();
}
for (String referencedField : referencedFields) {
if (!derivedFields.contains(referencedField) && !miningFieldNames.contains(referencedField)) {
/* Add them to the mining schema if they are not already
* contained there and if they don't refer to derived fields. */
MiningField miningField = MiningField.Factory.newInstance();
miningField.setName(referencedField);
miningField.setInvalidValueTreatment(INVALIDVALUETREATMENTMETHOD.AS_IS);
LOGGER.debug("Adding field \"" + referencedField + "\" to MiningSchema because it is referenced in " + "LocalTransformations.");
miningFields.add(miningField);
}
}
miningSchema.setMiningFieldArray(miningFields.toArray(new MiningField[0]));
}
use of org.apache.xmlbeans.XmlCursor in project knime-core by knime.
the class PMMLUtils method isOldKNIMEPMML.
/**
* @param xmlDoc the {@link XmlObject} to verify
* @return true if the xmlDoc is a 3.x or 4.0 PMML document produced by
* KNIME
*/
public static boolean isOldKNIMEPMML(final XmlObject xmlDoc) {
// Insert a cursor and move it to the PMML element.
XmlCursor pmmlCursor = xmlDoc.newCursor();
// the PMML element
pmmlCursor.toFirstChild();
if (!"PMML".equals(pmmlCursor.getName().getLocalPart())) {
return false;
}
String xmlns = "";
String version = "";
while (!pmmlCursor.toNextToken().isNone() && pmmlCursor.isAnyAttr()) {
String name = pmmlCursor.getName().getLocalPart();
if (pmmlCursor.currentTokenType().isNamespace() && name.isEmpty()) {
// the default namespace
xmlns = pmmlCursor.getTextValue();
} else if ("version".equals(name)) {
// the version attribute
version = pmmlCursor.getTextValue();
}
}
pmmlCursor.dispose();
String application = "";
XmlCursor appCursor = xmlDoc.newCursor();
String query = "declare namespace p='" + xmlns + "';" + "$this//p:Application";
appCursor.selectPath(query);
if (appCursor.hasNextSelection()) {
appCursor.toNextSelection();
application = appCursor.getAttributeText(new QName("name"));
}
appCursor.dispose();
return (version.startsWith("3.") || "4.0".equals(version)) && (// learned by KNIME
"KNIME".equals(application) || // learned in R
"Rattle/PMML".equals(application));
}
Aggregations