📄 baseentry.java
字号:
* Extension profile. * * @throws IOException */ public void generateAtom(XmlWriter w, ExtensionProfile extProfile) throws IOException { Vector<XmlWriter.Namespace> nsDecls = new Vector<XmlWriter.Namespace>(namespaceDeclsAtom); nsDecls.addAll(extProfile.getNamespaceDecls()); generateStartElement(w, Namespaces.atomNs, "entry", null, nsDecls); if (state.id != null) { w.simpleElement(Namespaces.atomNs, "id", null, state.id); } if (state.published != null) { w.simpleElement(Namespaces.atomNs, "published", null, state.published.toString()); } if (state.updated != null) { w.simpleElement(Namespaces.atomNs, "updated", null, state.updated.toString()); } if (state.pubControl != null) { state.pubControl.generateAtom(w, extProfile); } w.startRepeatingElement(); for (Category cat : state.categories) { cat.generateAtom(w); } w.endRepeatingElement(); if (state.title != null) { state.title.generateAtom(w, "title"); } if (state.summary != null) { state.summary.generateAtom(w, "summary"); } if (state.rights != null) { state.rights.generateAtom(w, "rights"); } if (state.content != null) { state.content.generateAtom(w); } w.startRepeatingElement(); for (Link link : state.links) { link.generateAtom(w, extProfile); } w.endRepeatingElement(); w.startRepeatingElement(); for (Person author : state.authors) { author.generateAtom(extProfile, w, "author"); } w.endRepeatingElement(); w.startRepeatingElement(); for (Person contributor : state.contributors) { contributor.generateAtom(extProfile, w, "contributor"); } w.endRepeatingElement(); if (state.source != null) { state.source.generateAtom(w, extProfile); } // Invoke ExtensionPoint. generateExtensions(w, extProfile); w.endElement(Namespaces.atomNs, "entry"); } /** * Generates XML in the RSS format. * * @param w * Output writer. * * @param extProfile * Extension profile. * * @throws IOException */ public void generateRss(XmlWriter w, ExtensionProfile extProfile) throws IOException { Vector<XmlWriter.Namespace> nsDecls = new Vector<XmlWriter.Namespace>(namespaceDeclsRss); nsDecls.addAll(extProfile.getNamespaceDecls()); generateStartElement(w, Namespaces.rssNs, "item", null, nsDecls); if (state.id != null) { List<Attribute> attrs = new ArrayList<Attribute>(1); attrs.add(new Attribute("isPermaLink", "false")); w.simpleElement(Namespaces.rssNs, "guid", attrs, state.id); } String lang = null; if (state.content != null) { lang = state.content.getLang(); } if (lang == null && state.summary != null) { lang = state.summary.getLang(); } if (lang == null && state.title != null) { lang = state.title.getLang(); } if (lang != null) { w.simpleElement(Namespaces.rssNs, "language", null, lang); } if (state.published != null) { w.simpleElement(Namespaces.rssNs, "pubDate", null, state.published.toStringRfc822()); } if (state.updated != null) { w.simpleElement(Namespaces.atomNs, "updated", null, state.updated.toString()); } w.startRepeatingElement(); for (Category cat : state.categories) { cat.generateRss(w); } w.endRepeatingElement(); if (state.title != null) { state.title.generateRss(w, "title", TextConstruct.RssFormat.PLAIN_TEXT); } if (state.summary != null) { state.summary.generateAtom(w, "summary"); } if (state.content != null) { state.content.generateRss(w); } w.startRepeatingElement(); for (Link link : state.links) { link.generateRss(w); } w.endRepeatingElement(); w.startRepeatingElement(); for (Person author : state.authors) { author.generateRss(w, "author"); } w.endRepeatingElement(); w.startRepeatingElement(); for (Person contributor : state.contributors) { contributor.generateRss(w, "author"); } w.endRepeatingElement(); // Invoke ExtensionPoint. generateExtensions(w, extProfile); w.endElement(Namespaces.rssNs, "item"); } /** Top-level namespace declarations for generated XML. */ private static final Collection<XmlWriter.Namespace> namespaceDeclsAtom = new Vector<XmlWriter.Namespace>(1); private static final Collection<XmlWriter.Namespace> namespaceDeclsRss = new Vector<XmlWriter.Namespace>(1); static { namespaceDeclsAtom.add(Namespaces.atomNs); namespaceDeclsRss.add(Namespaces.atomNs); } /** * Reads an entry representation from the provided {@link ParseSource}. * The return type of the entry will be determined using dynamic adaptation * based upon any {@link Kind} category tag found in the input content. If * no kind tag is found an {@link Entry} instance will be returned. */ public static BaseEntry readEntry(ParseSource source) throws IOException, ParseException, ServiceException { return readEntry(source, null, null); } public static <T extends BaseEntry> T readEntry(ParseSource source, Class <T> entryClass, ExtensionProfile extProfile) throws IOException, ParseException, ServiceException { if (source == null) { throw new NullPointerException("Null source"); } // Determine the parse entry type boolean isAdapting = (entryClass == null); if (isAdapting) { entryClass = (Class<T>) Entry.class; } // Create a new entry instance. T entry; try { entry = entryClass.newInstance(); } catch (IllegalAccessException iae) { throw new ServiceException("Unable to create entry", iae); } catch (InstantiationException ie) { throw new ServiceException("Unable to create entry", ie); } // Initialize the extension profile (if not provided) if (extProfile == null) { extProfile = new ExtensionProfile(); entry.declareExtensions(extProfile); if (isAdapting) { extProfile.setAutoExtending(true); } } // Parse the content if (source.getReader() != null) { entry.parseAtom(extProfile, source.getReader()); } else if (source.getInputStream() != null) { entry.parseAtom(extProfile, source.getInputStream()); } else if (source.getParser() != null) { entry.parseAtom(extProfile, source.getParser()); } else { throw new IllegalStateException("Unexpected source: " + source); } // Adapt if requested and the entry contained a kind tag if (isAdapting) { BaseEntry adaptedEntry = entry.getAdaptedEntry(); if (adaptedEntry != null) { entry = (T) adaptedEntry; } } return entry; } /** * Parses XML in the Atom format. * * @param extProfile * Extension profile. * * @param input * XML input stream. */ public void parseAtom(ExtensionProfile extProfile, InputStream input) throws IOException, ParseException { AtomHandler handler = new AtomHandler(extProfile); new XmlParser().parse(input, handler, Namespaces.atom, "entry"); } /** * Parses XML in the Atom format. * * @param extProfile * Extension profile. * * @param reader * XML Reader. The caller is responsible for ensuring * that the character encoding is correct. */ public void parseAtom(ExtensionProfile extProfile, Reader reader) throws IOException, ParseException { AtomHandler handler = new AtomHandler(extProfile); new XmlParser().parse(reader, handler, Namespaces.atom, "entry"); } /** * Parses XML in the Atom format from a parser-defined content source. * * @param extProfile * Extension profile. * @param parser * XML parser. */ public void parseAtom(ExtensionProfile extProfile, XmlParser parser) throws IOException, ParseException { AtomHandler handler = new AtomHandler(extProfile); parser.parse(handler, Namespaces.atom, "entry"); } /** Returns information about the content element processing. */ protected Content.ChildHandlerInfo getContentHandlerInfo(Attributes attrs) throws ParseException, IOException { return Content.getChildHandler(attrs); } /** {@code <atom:entry>} parser. */ public class AtomHandler extends ExtensionPoint.ExtensionHandler { public AtomHandler(ExtensionProfile extProfile) throws IOException { super(extProfile, BaseEntry.this.getClass()); } public XmlParser.ElementHandler getChildHandler(String namespace, String localName, Attributes attrs) throws ParseException, IOException { if (namespace.equals(Namespaces.atom)) { if (localName.equals("id")) { return new IdHandler(); } else if (localName.equals("published")) { return new PublishedHandler(); } else if (localName.equals("updated")) { return new UpdatedHandler(); } else if (localName.equals("title")) { TextConstruct.ChildHandlerInfo chi = TextConstruct.getChildHandler(attrs); if (state.title != null) { throw new ParseException("Duplicate title."); } state.title = chi.textConstruct; return chi.handler; } else if (localName.equals("summary")) { TextConstruct.ChildHandlerInfo chi = TextConstruct.getChildHandler(attrs); if (state.summary != null) { throw new ParseException("Duplicate summary."); } state.summary = chi.textConstruct; return chi.handler; } else if (localName.equals("rights")) { TextConstruct.ChildHandlerInfo chi = TextConstruct.getChildHandler(attrs); if (state.rights != null) { throw new ParseException("Duplicate rights."); } state.rights = chi.textConstruct; return chi.handler; } else if (localName.equals("content")) { if (state.content != null) { throw new ParseException("Duplicate content."); } Content.ChildHandlerInfo chi = getContentHandlerInfo(attrs); state.content = chi.content; return chi.handler; } else if (localName.equals("category")) { Category cat = new Category(); return cat.new AtomHandler(extProfile, state.categories, BaseEntry.this); } else if (localName.equals("link")) { Link link = new Link(); state.links.add(link); return link.new AtomHandler(extProfile); } else if (localName.equals("author")) { Person author = new Person(); state.authors.add(author); return author.new AtomHandler(extProfile); } else if (localName.equals("contributor")) { Person contributor = new Person(); state.contributors.add(contributor); return contributor.new AtomHandler(extProfile); } else if (localName.equals("source")) { state.source = new Source(); return state.source.new SourceHandler(extProfile); } } else if (namespace.equals(Namespaces.atomPub)) { if (localName.equals("control")) { state.pubControl = new PubControl(); return state.pubControl.new AtomHandler(extProfile); } } else { return super.getChildHandler(namespace, localName, attrs); } return null; } /** {@code <atom:id>} parser. */ class IdHandler extends XmlParser.ElementHandler { public void processEndElement() throws ParseException { if (state.id != null) { throw new ParseException("Duplicate entry ID."); } if (value == null) { throw new ParseException("ID must have a value."); } state.id = value; } } /** {@code <atom:published>} parser. */ class PublishedHandler extends Rfc3339Handler { public void processEndElement() throws ParseException { super.processEndElement(); state.published = getDateTime(); } } /** {@code <atom:updated>} parser. */ class UpdatedHandler extends Rfc3339Handler { public void processEndElement() throws ParseException { super.processEndElement(); state.updated = getDateTime(); } } } /** * Locates and returns the most specific {@link Kind.Adaptor} BaseEntry * subtype for this entry. If none can be found for the current class, * {@code null} will be returned. */ public BaseEntry<?> getAdaptedEntry() throws Kind.AdaptorException { BaseEntry adaptedEntry = null; // Find the BaseEntry adaptor instance that is most specific. for (Kind.Adaptor adaptor : getAdaptors()) { if (!(adaptor instanceof BaseEntry)) { continue; } // If first matching adaptor or a narrower subtype of the current one, // then use it. if (adaptedEntry == null || adaptedEntry.getClass().isAssignableFrom(adaptor.getClass())) { adaptedEntry = (BaseEntry) adaptor; } } return adaptedEntry; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -