org.apache.maven.doxia.site.inheritance.DefaultSiteModelInheritanceAssembler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doxia-site-model Show documentation
Show all versions of doxia-site-model Show documentation
The Site Model handles the descriptor for sites, also known as site.xml.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.maven.doxia.site.inheritance;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.doxia.site.Body;
import org.apache.maven.doxia.site.LinkItem;
import org.apache.maven.doxia.site.Logo;
import org.apache.maven.doxia.site.Menu;
import org.apache.maven.doxia.site.MenuItem;
import org.apache.maven.doxia.site.SiteModel;
import org.codehaus.plexus.util.xml.Xpp3Dom;
/**
* Manage inheritance of the site model.
*
* @author Brett Porter
* @author Henning P. Schmiedehausen
*/
@Singleton
@Named
public class DefaultSiteModelInheritanceAssembler implements SiteModelInheritanceAssembler {
/** {@inheritDoc} */
public void assembleModelInheritance(
String name, SiteModel child, SiteModel parent, String childBaseUrl, String parentBaseUrl) {
if (parent == null || !child.isMergeParent()) {
return;
}
child.setCombineSelf(parent.getCombineSelf());
URLRebaser urlContainer = new URLRebaser(parentBaseUrl, childBaseUrl);
if (child.getBannerLeft() == null && parent.getBannerLeft() != null) {
child.setBannerLeft(parent.getBannerLeft().clone());
rebaseLinkItemPaths(child.getBannerLeft(), urlContainer);
}
if (child.getBannerRight() == null && parent.getBannerRight() != null) {
child.setBannerRight(parent.getBannerRight().clone());
rebaseLinkItemPaths(child.getBannerRight(), urlContainer);
}
if (child.isDefaultPublishDate() && parent.getPublishDate() != null) {
child.setPublishDate(parent.getPublishDate().clone());
}
if (child.isDefaultVersion() && parent.getVersion() != null) {
child.setVersion(parent.getVersion().clone());
}
if (child.getEdit() == null && parent.getEdit() != null) {
child.setEdit(parent.getEdit());
}
if (child.getSkin() == null && parent.getSkin() != null) {
child.setSkin(parent.getSkin().clone());
}
child.setPoweredBy(mergePoweredByLists(child.getPoweredBy(), parent.getPoweredBy(), urlContainer));
if (parent.getLastModified() > child.getLastModified()) {
child.setLastModified(parent.getLastModified());
}
assembleBodyInheritance(name, child, parent, urlContainer);
assembleCustomInheritance(child, parent);
}
/** {@inheritDoc} */
public void resolvePaths(final SiteModel siteModel, final String baseUrl) {
if (baseUrl == null) {
return;
}
if (siteModel.getBannerLeft() != null) {
relativizeLinkItemPaths(siteModel.getBannerLeft(), baseUrl);
}
if (siteModel.getBannerRight() != null) {
relativizeLinkItemPaths(siteModel.getBannerRight(), baseUrl);
}
for (Logo logo : siteModel.getPoweredBy()) {
relativizeLinkItemPaths(logo, baseUrl);
}
if (siteModel.getBody() != null) {
for (LinkItem linkItem : siteModel.getBody().getLinks()) {
relativizeLinkItemPaths(linkItem, baseUrl);
}
for (LinkItem linkItem : siteModel.getBody().getBreadcrumbs()) {
relativizeLinkItemPaths(linkItem, baseUrl);
}
for (Menu menu : siteModel.getBody().getMenus()) {
relativizeMenuPaths(menu.getItems(), baseUrl);
if (menu.getImage() != null) {
menu.getImage().setSrc(relativizeLink(menu.getImage().getSrc(), baseUrl));
}
}
}
}
private void assembleCustomInheritance(final SiteModel child, final SiteModel parent) {
if (child.getCustom() == null) {
child.setCustom(parent.getCustom());
} else {
child.setCustom(Xpp3Dom.mergeXpp3Dom((Xpp3Dom) child.getCustom(), (Xpp3Dom) parent.getCustom()));
}
}
private void assembleBodyInheritance(
final String name, final SiteModel child, final SiteModel parent, final URLRebaser urlContainer) {
Body cBody = child.getBody();
Body pBody = parent.getBody();
if (cBody != null || pBody != null) {
if (cBody == null) {
cBody = new Body();
child.setBody(cBody);
}
if (pBody == null) {
pBody = new Body();
}
if (cBody.getHead() == null && pBody.getHead() != null) {
cBody.setHead(pBody.getHead());
}
cBody.setLinks(mergeLinkItemLists(cBody.getLinks(), pBody.getLinks(), urlContainer, false));
if (cBody.getBreadcrumbs().isEmpty() && !pBody.getBreadcrumbs().isEmpty()) {
LinkItem breadcrumb = new LinkItem();
breadcrumb.setName(name);
breadcrumb.setHref("index.html");
cBody.getBreadcrumbs().add(breadcrumb);
}
cBody.setBreadcrumbs(
mergeLinkItemLists(cBody.getBreadcrumbs(), pBody.getBreadcrumbs(), urlContainer, true));
cBody.setMenus(mergeMenus(cBody.getMenus(), pBody.getMenus(), urlContainer));
if (cBody.getFooter() == null && pBody.getFooter() != null) {
cBody.setFooter(pBody.getFooter());
}
}
}
private List