📄 templateprocessor.java
字号:
variable.getAttributeNS( TemplateProcessor.TEMPLATE_NS, TemplateTags.ID); if (! this.templateVariables.hasVariableValue(variableId, variableName)) { throw new TemplateProcessorException( "No variable with name '" + variableName + "' in class '" + className + "'."); } conditionValue = this.templateVariables.getVariableValue( variableId, variableName); if (conditionValue == null) { // variable in condition has no value, SQL statement would be meaningless, return; } } } } if (conditionValue.equals("")) { // literal in condition conditionValue = elem.getFirstChild().getNodeValue(); } sqlStatement += "'" + conditionValue + "'"; } } } try { ResultSet rs = sqlLogic.executeQuery(sqlStatement); // enumerate all rows (instances) int instanceCnt=0; while (rs.next()) { // set variables setVariables(instanceNode, rs); // write RDF instance for each row (each instance) in result set writeInstance(instanceNode, rs, parentProperty, instanceCnt++); } } catch (SQLException e) { throw new TemplateProcessorException( "Database query error when parsing instances of class '" + className + "'", e); } } /** * sets values of variables for an instance * @param instanceNode - instance node from a template document * @param instance - result set selected from a database * @throws TemplateProcessorException - when SQL exception is throwed during the result set processing */ public void setVariables(Element instanceNode, ResultSet instance) throws TemplateProcessorException { String instanceId = instanceNode.getAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.ID); if (!instanceId.equals("")) { String className = instanceNode.getAttributeNS( TemplateProcessor.TEMPLATE_NS, TemplateTags.NAME); MappingClass mp = (MappingClass) classes.get(className); HashMap classVariables = mp.getVariables(); Iterator iter = classVariables.keySet().iterator(); String variableName; String variableValue = ""; while (iter.hasNext()) { variableName = (String) iter.next(); try { variableValue = instance.getString( (String) classVariables.get(variableName)); } catch (SQLException e) { throw new TemplateProcessorException( "Database query error when setting up variable '" + variableName, e); } this.templateVariables.addVariable( instanceId, variableName, variableValue); } } } /** * writes the instance representation as an RDF code * @param instanceNode - instance node from a template document * @param instance - result set from a database * @param parentProperty - a property that contains the processed instance * @return string with RDF representation of the processed instance * @throws TemplateProcessorException */ private void writeInstance(Element instanceNode, ResultSet instance, MappingProperty parentProperty, int instanceCnt) throws TemplateProcessorException { String className = instanceNode.getAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.NAME); //System.out.print(className); // if node id is set on the instance String nodeId = instanceNode.getAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.NODE_ID); if (instanceCnt>0 && !nodeId.equals("")) { nodeId += "_" + instanceCnt; } MappingClass mp = (MappingClass) classes.get(className); String rdfLabel = mp.getRdfLabel(); //writing instance // is container item? if (parentProperty != null) { if (parentProperty.getContainer() != MappingProperty.NO_CONTAINER) { writeToWriter("<rdf:li>\n"); } } //start tag writeToWriter("<" + rdfLabel); writeAttributes(mp.getAttributes(), instance); writeToWriter((!nodeId.equals("")?" rdf:nodeID=\"" + nodeId + "\"":"") + ">\n"); //content of instance NodeList nodelist = instanceNode.getChildNodes(); writeInstanceContent(nodelist, mp, instance); //end tag writeToWriter("</" + rdfLabel + ">\n"); //instance. //is container item? if (parentProperty != null) { if (parentProperty.getContainer() != MappingProperty.NO_CONTAINER) { writeToWriter("</rdf:li>\n"); } } } private void writeInstanceContent(NodeList nodelist, MappingClass mp, ResultSet instance) throws TemplateProcessorException { for (int i = 0, n = nodelist.getLength(); i < n; i++) { if (nodelist.item(i).getNodeType() == Node.ELEMENT_NODE) { Element elem = (Element) nodelist.item(i); if (elem.getLocalName().equals(TemplateTags.PUT_PROPERTY)) { writeProperty(mp, elem, instance); } if (elem.getLocalName().equals(TemplateTags.PUT_INSTANCE)) { parseInstanceNode(elem, null); } } } } /** * writes the property representation as an RDF code * @param mp - a mapping class of an instance that contains the processed property * @param propertyNode - property node from a template document * @param instance - result set selected from a database * @return string with RDF representation of the processed property * @throws TemplateProcessorException */ private void writeProperty( MappingClass mp, Element propertyNode, ResultSet instance) throws TemplateProcessorException { String propertyName = propertyNode.getAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.NAME); String containerNodeId = propertyNode.getAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.CONTAINER_NODE_ID); MappingProperty property = (MappingProperty) mp.getProperties().get(propertyName); if (property == null) { throw new TemplateProcessorException( "No property with name '" + propertyName + "' in class '" + mp.getTemplateName() + "'"); } String rdfLabel = (String) property.getRdfLabel(); //start tag writeToWriter("<" + rdfLabel); writeAttributes(property.getAttributes(), instance); writeToWriter(">"); if (property.getContainer() != MappingProperty.NO_CONTAINER) { writeToWriter("<rdf:"+property.getContainerLabel()+ ((!containerNodeId.equals(""))?(" rdf:nodeID=\"" + containerNodeId + "\""):"") + ">\n"); //rdf += "<rdf:"+property.getContainerLabel()+ " rdf:nodeID=\"" + property.getContainerNodeId() + "\">\n"; } // property content if (propertyNode.getChildNodes().getLength() == 0) { // datatype property try { writeToWriter(property.getPrefix()); if (!((String) property.getSql()).equals("")) { writeToWriter(filterDatabaseData(instance.getString((String) property.getSql()))); } writeToWriter(property.getSuffix()); } catch (SQLException e) { throw new TemplateProcessorException( "Database query error when parsing property '" + propertyName, e); } } else { // object property NodeList list = propertyNode.getChildNodes(); for (int i = 0, n = list.getLength(); i < n; i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { Element elem = (Element) list.item(i); if (elem.getLocalName().equals(TemplateTags.PUT_INSTANCE)) { parseInstanceNode(elem, property); } } } } // end tag if (property.getContainer() != MappingProperty.NO_CONTAINER) { writeToWriter("</rdf:"+property.getContainerLabel()+">\n"); } ; writeToWriter("</" + rdfLabel + ">\n"); } /** * writes attributes to an RDF representation of an instance or property * @param attributeList - attribute list from an instance or property * @param instance - result set selected from a database * @return an RDF attributes of an instance or property * @throws TemplateProcessorException */ private void writeAttributes(List attributeList, ResultSet instance) throws TemplateProcessorException { if (attributeList.isEmpty()) return; MappingAttribute ma; for (int i = 0, n = attributeList.size(); i < n; i++) { ma = (MappingAttribute) attributeList.get(i); try { writeToWriter(" " + ma.getRdfLabel() + "=\"" + ma.getPrefix() + ((!ma.getSql().equals(""))?filterDatabaseData(instance.getString(ma.getSql())):"") + ma.getSuffix() + "\""); } catch (SQLException e) { //e.printStackTrace(); throw new TemplateProcessorException( "Database query error when parsing attribute with rdf label '" + ma.getRdfLabel(), e); } } } /** * get URL of a mapping document from a template document * @return URL of mapping document * @throws TemplateProcessorException - when no mapping is specified in a template document */ private String getMapping() throws TemplateProcessorException { String mapping = ""; NodeList mappingNodeList = template.getElementsByTagNameNS( TemplateProcessor.TEMPLATE_NS, TemplateTags.MAPPING); Element elem = null; try { elem = (Element) mappingNodeList.item(mappingNodeList.getLength() - 1); } catch (Exception e) { throw new TemplateProcessorException("No mapping url in template."); } if (elem != null) { if (elem.hasAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.URL)) mapping = elem.getAttributeNS(TemplateProcessor.TEMPLATE_NS, TemplateTags.URL); } if (mapping.equals("")) throw new TemplateProcessorException("No mapping url in template."); return mapping; } /** * add table name to an existing SQL query * @param sqlQuery - existing SQL query * @param table - table name to be added to SQL query * @return updated query */ private String addTableToSql(String sqlQuery, String table) { String[] tmp = sqlQuery.split(" where "); return tmp[0] + ", " + table + " where " + tmp[1]; } /** * filters data selected from a database. * only XML filter is available now * @param databaseData - string from a database * @return filtered string */ private String filterDatabaseData(String databaseData) { if (this.XmlFilter && databaseData!=null) { return databaseData.replaceAll("\\<.*?\\>","").replaceAll(" "," ").replaceAll("&"," and ").replaceAll("&"," and "); } return databaseData; } /* public static void main(String[] args) throws TemplateProcessorException, MappingProcessorException { if (true) { try { File file = new File("/home/martin/work/projects/eclipse/rdfServlet/WEB-INF/resources/templates/person.xml"); //File file = new File("/home/martin/work/projects/eclipse/rdfServlet/WEB-INF/resources/templates/publication.xml"); //File file = new File("/home/martin/work/projects/eclipse/rdfServlet/WEB-INF/resources/templates/course.xml"); InputStream is = new FileInputStream(file); Map map = new HashMap(); map.put("username", "muller"); //map.put("id", "98843"); //map.put("id", "16067"); TemplateProcessor tp = new TemplateProcessor(is, map); System.out.println(tp.parse()); } catch (IOException e) { throw new TemplateProcessorException("File not found", e); } catch (TemplateProcessorException e) { System.out.println(e.getMessage()); throw e; } } } */ private void writeToWriter(String rdf) throws TemplateProcessorException { if (this.writer == null) return; try { if (rdf == null) rdf = ""; this.writer.write(rdf); } catch (IOException e) { throw new TemplateProcessorException( "Error writing RDF: " + e.getMessage(), e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -