@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if (qName != null && qName.length() > 0) localName = qName; level++; L.d(tab() + "<" + localName + ">"); currentAttributes = attributes; elements.push(localName); // String currentElement = elements.peek(); if (!insideFeed && "feed".equals(localName)) { insideFeed = true; } else if ("entry".equals(localName)) { if (!insideFeed) { insideFeed = true; singleEntry = true; } insideEntry = true; entryInfo = new EntryInfo(); } else if ("category".equals(localName)) { if (insideEntry) { String category = attributes.getValue("label"); if (category != null) entryInfo.categories.add(category); } } else if ("id".equals(localName)) { } else if ("updated".equals(localName)) { } else if ("title".equals(localName)) { } else if ("link".equals(localName)) { LinkInfo link = new LinkInfo(url, attributes); if (link.isValid() && insideFeed) { L.d(tab() + link.toString()); if (insideEntry) { if (link.type != null) { entryInfo.links.add(link); int priority = link.getPriority(); if (link.type.startsWith("application/atom+xml")) { entryInfo.link = link; } else if (priority > 0 && (entryInfo.link == null || entryInfo.link.getPriority() < priority)) { entryInfo.link = link; } } } else { if ("self".equals(link.rel)) docInfo.selfLink = link; else if ("alternate".equals(link.rel)) docInfo.alternateLink = link; } } } else if ("author".equals(localName)) { authorInfo = new AuthorInfo(); } }
/** * Takes a DocInfo object and converts it to a string that the file will be named as. * * @param info Course info to be converted * @return String of a DocInfo object in the form: lname_fname subject courseNum-section semester * year */ private String generateSaveFileName(DocInfo info) { return info.getInstLName() + "_" + info.getInstFName() + " " + info.getSubject() + " " + info.getCourseNum() + "-" + info.getSection() + " " + info.getSemester() + " " + info.getYear() + ".docx"; }
@Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); String s = new String(ch, start, length); s = s.trim(); if (s.length() == 0 || (s.length() == 1 && s.charAt(0) == '\n')) return; // ignore empty line L.d(tab() + " {" + s + "}"); String currentElement = elements.peek(); if (currentElement == null) return; if (insideFeed) { if ("id".equals(currentElement)) { if (insideEntry) entryInfo.id = s; else docInfo.id = s; } else if ("updated".equals(currentElement)) { long ts = parseTimestamp(s); if (insideEntry) entryInfo.updated = ts; else docInfo.updated = ts; } else if ("title".equals(currentElement)) { if (!insideEntry) docInfo.title = s; else entryInfo.title = entryInfo.title + s; } else if ("summary".equals(currentElement)) { if (insideEntry) entryInfo.summary = entryInfo.summary + s; } else if ("name".equals(currentElement)) { if (authorInfo != null) authorInfo.name = s; } else if ("uri".equals(currentElement)) { if (authorInfo != null) authorInfo.uri = s; } else if ("icon".equals(currentElement)) { if (!insideEntry) docInfo.icon = s; else entryInfo.icon = s; } else if ("link".equals(currentElement)) { // rel, type, title, href if (!insideEntry) docInfo.icon = s; else entryInfo.icon = s; } else if ("content".equals(currentElement)) { if (insideEntry) entryInfo.content = entryInfo.content + s; } else if ("subtitle".equals(currentElement)) { if (!insideEntry) docInfo.subtitle = s; } } }
/** * Generates, saves, and opens Word template comments sheet based on a DocInfo object. It * generates a header table with course info stored in the DocInfo object. Each evaluation * question has its own table below the header table. The file is saved at the location specified * with a name generated by the program based on the course info. */ public void generateCommentTemplate(DocInfo info, File saveLoc) { try { // Check if file exists. If it does exist, give the user the option // to cancel. File wordDoc = new File(saveLoc, generateSaveFileName(info)); if (wordDoc.exists()) { String message = "The comment template sheet already exists. By selecting " + "yes, the file will be overwritten. Are you sure you want to overwrite " + "the file?"; int response = JOptionPane.showConfirmDialog(null, message); if (response != JOptionPane.YES_OPTION) { return; } } wordMLPackage = WordprocessingMLPackage.createPackage(); factory = Context.getWmlObjectFactory(); // Add title to top of document wordMLPackage.getMainDocumentPart().addParagraphOfText("Student Feedback to Instructor"); // Create table for header information Tbl courseInfoTable = factory.createTbl(); // Create instructor name row Tr instNameRow = factory.createTr(); addStyledTableCellWithWidth( instNameRow, "Instructor Name:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( instNameRow, info.getInstFName() + " " + info.getInstLName(), false, "20", TABLE_LONG_FIELD_LENGTH); // Create subject/course number row Tr courseInfoRow = factory.createTr(); addStyledTableCellWithWidth(courseInfoRow, "Course Subject & Number:", true, "20", 3000); addStyledTableCellWithWidth( courseInfoRow, info.getSubject() + " " + info.getCourseNum(), false, "20", TABLE_LONG_FIELD_LENGTH); // Create section row Tr sectionRow = factory.createTr(); addStyledTableCellWithWidth(sectionRow, "Section:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( sectionRow, info.getSection(), false, "20", TABLE_LONG_FIELD_LENGTH); // Semester/Year row Tr semesterRow = factory.createTr(); addStyledTableCellWithWidth(semesterRow, "Semester:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( semesterRow, info.getSemester().toString() + " " + info.getYear(), false, "20", TABLE_LONG_FIELD_LENGTH); // Add rows to table courseInfoTable.getContent().add(instNameRow); courseInfoTable.getContent().add(courseInfoRow); courseInfoTable.getContent().add(sectionRow); courseInfoTable.getContent().add(semesterRow); // Add border around entire table addBorders(courseInfoTable); // Place tables on document wordMLPackage.getMainDocumentPart().addObject(courseInfoTable); // Create a new table for each evaluation question for (String question : evalQuestions) { // Add empty paragraph so there is a space between questions wordMLPackage.getMainDocumentPart().addParagraphOfText(NEW_LINE); // Create table and add to doc Tbl questionTable = factory.createTbl(); // Row with question Tr questionRow = factory.createTr(); addStyledTableCellWithWidth(questionRow, question, true, "20", 10000); questionTable.getContent().add(questionRow); // Row with blank line for input Tr blankRow = factory.createTr(); addStyledTableCellWithWidth(blankRow, "", true, "20", 10000); questionTable.getContent().add(blankRow); addBorders(questionTable); // Add table to document wordMLPackage.getMainDocumentPart().addObject(questionTable); } wordMLPackage.save(wordDoc); openFile(wordDoc); } catch (InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Docx4JException e) { JOptionPane.showMessageDialog( null, "The comment sheet could not be saved. Make sure the document is not already open in Word."); e.printStackTrace(); } }
/** * Generates an OIT scan sheet. If checkbox for print is open, uses a Desktop object to print to * the default printer. The current system date is used as the date. * * @param info Course inform * @param print If true, the document will be sent to default printer */ public void generateOITSheet(DocInfo info, boolean print) { try { // Create new doc wordMLPackage = WordprocessingMLPackage.createPackage(); factory = Context.getWmlObjectFactory(); // Add title to top of document wordMLPackage.getMainDocumentPart().addParagraphOfText("OIT Scan Cover Sheet"); // Create table for header information Tbl infoTable = factory.createTbl(); // Create instructor name row Tr instNameRow = factory.createTr(); addStyledTableCellWithWidth( instNameRow, "Instructor Name:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( instNameRow, info.getInstFName() + " " + info.getInstLName(), false, "20", TABLE_LONG_FIELD_LENGTH); // Create subject/course number row Tr courseInfoRow = factory.createTr(); addStyledTableCellWithWidth(courseInfoRow, "Course Subject & Number:", true, "20", 3000); addStyledTableCellWithWidth( courseInfoRow, info.getSubject() + " " + info.getCourseNum(), false, "20", TABLE_LONG_FIELD_LENGTH); // Create section row Tr sectionRow = factory.createTr(); addStyledTableCellWithWidth(sectionRow, "Section:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( sectionRow, info.getSection(), false, "20", TABLE_LONG_FIELD_LENGTH); // Semester/Year row Tr semesterRow = factory.createTr(); addStyledTableCellWithWidth(semesterRow, "Semester:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( semesterRow, info.getSemester().toString() + " " + info.getYear(), false, "20", TABLE_LONG_FIELD_LENGTH); // Faculty supp name row Tr facSuppNameRow = factory.createTr(); addStyledTableCellWithWidth( facSuppNameRow, "Faculty Support Name:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( facSuppNameRow, info.getFacSuppName(), false, "20", TABLE_LONG_FIELD_LENGTH); // Faculty supp name row Tr facSuppExtenRow = factory.createTr(); addStyledTableCellWithWidth( facSuppExtenRow, "Faculty Support Extension:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( facSuppExtenRow, info.getFacSuppExten(), false, "20", TABLE_LONG_FIELD_LENGTH); // Mailbox row Tr mailboxRow = factory.createTr(); addStyledTableCellWithWidth( mailboxRow, "I would like the results delivered to mailbox:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( mailboxRow, info.getMailbox(), false, "20", TABLE_LONG_FIELD_LENGTH); Tr dateRow = factory.createTr(); DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date date = new Date(); System.out.println(dateFormat.format(date)); addStyledTableCellWithWidth( dateRow, "Date of Request:", true, "20", TABLE_SHORT_FIELD_LENGTH); addStyledTableCellWithWidth( dateRow, dateFormat.format(date), false, "20", TABLE_LONG_FIELD_LENGTH); // Add rows to table infoTable.getContent().add(instNameRow); infoTable.getContent().add(courseInfoRow); infoTable.getContent().add(sectionRow); infoTable.getContent().add(semesterRow); infoTable.getContent().add(facSuppNameRow); infoTable.getContent().add(facSuppExtenRow); infoTable.getContent().add(mailboxRow); infoTable.getContent().add(dateRow); // Add border around entire table addBorders(infoTable); // Place tables on document wordMLPackage.getMainDocumentPart().addObject(infoTable); // Save in project files wordMLPackage.save(new File(OIT_SCAN_SHEET_SAVE_LOC)); // Print or open file based on user decision if (print) { desktop.print(new File(OIT_SCAN_SHEET_SAVE_LOC)); } else { desktop.open(new File(OIT_SCAN_SHEET_SAVE_LOC)); } System.out.println("OIT Scan sheet generated."); } catch (InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Docx4JException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog( null, "Unable to open the OIT scan sheet. Make sure the document is not open."); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }