Math & ScienceListing XML and Java source codes

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
piotrekde
Posts: 3
Joined: Tue Jul 06, 2010 2:59 pm

Listing XML and Java source codes

Post by piotrekde »

Hi,

I am trying to create pattern to list my Java source code. It looks pretty nice, but there are some problems when I try to list XML code. Here is how it's done:

Code: Select all

\documentclass[11pt,a4paper]{article}

\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\usepackage{caption}

% ustawienia odnosnie listowania kodu zrodlowego ******
\lstset{language=Java,captionpos=t,tabsize=3,frame=no,keywordstyle=\color{blue},
	commentstyle=\color{gray},stringstyle=\color{red},numbers=left,numberstyle=\tiny,
	numbersep=5pt,breaklines=true,showstringspaces=false,basicstyle=\footnotesize,emph={label}}


\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{darkgray}{\parbox[c][0.3cm]{\textwidth}{#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
% ****************************************************



\begin{document}

\section{IoC - dependency injection}

\begin{lstlisting}[language=XML,caption=applicationContext.xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="weatherService" class="pl.edu.uj.spring.WeatherServiceImpl">
		<property name="weatherDao">
			<ref local="weatherDao"/>
		</property>
	</bean>
	
	<bean id="weatherDao" class="pl.edu.uj.spring.StaticDataWeatherDaoImpl">
	</bean>
</beans>
\end{lstlisting}

\begin{lstlisting}[caption=Application.java]
package pl.edu.uj.spring;

import java.util.GregorianCalendar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("pl/edu/uj/spring/applicationContext.xml");
		WeatherService ws = (WeatherService) ctx.getBean("weatherService");
		
		Double high = ws.getHistoricalHigh(new GregorianCalendar(2010,07,05).getTime());
		System.out.println(high);
	}
}
\end{lstlisting}

\end{document}
For Java result is nice:
java.png
java.png (34.71 KiB) Viewed 13722 times
But in case of XML it doesn't look as expected:
xml.png
xml.png (31.72 KiB) Viewed 13722 times
Why other XML tags such as beans,bean, properties are not colored? Why only the first attribute (version) is blue, but second (encoding) is black?

I am LaTeX newbie so your patience is very appreciated :)
Thanks,
Piotr


Edit by localghost: No external links! Attachments go onto the forum server (see Board Rules).

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Listing XML and Java source codes

Post by localghost »

piotrekde wrote:[...] Why other XML tags such as beans,bean, properties are not colored? Why only the first attribute (version) is blue, but second (encoding) is black? [...]
I'm not an expert. But these tags may not be predefined. Are they basic elements of XML? Perhaps you can take a look at the default language definitions. Since I never needed those settings, this is all I can advise.


Best regards and welcome to the board
Thorsten
piotrekde
Posts: 3
Joined: Tue Jul 06, 2010 2:59 pm

Listing XML and Java source codes

Post by piotrekde »

localghost wrote: I'm not an expert. But these tags may not be predefined. Are they basic elements of XML? Perhaps you can take a look at the default language definitions.
I think that you are right. I just thought that everything between < > will be treated as XML tag. Can you please give me an advice where to look at those default language definitions?

Thanks
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Listing XML and Java source codes

Post by localghost »

Looking at the caption of Table 1 in the listings manual, it tells me that some languages have a preliminary definition. And XML is among them. Its definition can be found in the »lstlang1.sty« file which is located in the same installation folder as the package file itself. I extracted the relevant part.

Code: Select all

\lst@definelanguage{XML}%
  {keywords={,CDATA,DOCTYPE,ATTLIST,termdef,ELEMENT,EMPTY,ANY,ID,%
      IDREF,IDREFS,ENTITY,ENTITIES,NMTOKEN,NMTOKENS,NOTATION,%
      INCLUDE,IGNORE,SYSTEM,PUBLIC,NDATA,PUBLIC,%
      PCDATA,REQUIRED,IMPLIED,FIXED,%%% preceded by #
      xml,xml:space,xml:lang,version,standalone,default,preserve},%
   alsoother=$,%
   alsoletter=:,%
   tag=**[s]<>,%
   morestring=[d]",% ??? doubled
   morestring=[d]',% ??? doubled
   MoreSelectCharTable=%
      \lst@CArgX--\relax\lst@DefDelimB{}{}%
          {\ifnum\lst@mode=\lst@tagmode\else
               \expandafter\@gobblethree
           \fi}%
          \lst@BeginComment\lst@commentmode{{\lst@commentstyle}}%
      \lst@CArgX--\relax\lst@DefDelimE{}{}{}%
          \lst@EndComment\lst@commentmode
      \lst@CArgX[CDATA[\relax\lst@CDef{}%
          {\ifnum\lst@mode=\lst@tagmode
               \expandafter\lst@BeginCDATA
           \else \expandafter\lst@CArgEmpty
           \fi}%
          \@empty
      \lst@CArgX]]\relax\lst@CDef{}%
          {\ifnum\lst@mode=\lst@GPmode
               \expandafter\lst@EndComment
           \else \expandafter\lst@CArgEmpty
           \fi}%
          \@empty
  }[keywords,comments,strings,html]%
If you know other important keywords (or perhaps all), you can supplement this list and either modify the above mentioned file or overwrite these settings in the preamble of your document.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Listing XML and Java source codes

Post by frabjous »

That seems like a lot of work. How you considered trying something like the minted or [url=htp://ctan.org/pkg/texments]texments[/url] packages?

By default, for the xml, you'd get:
xml.png
xml.png (40.75 KiB) Viewed 13701 times
And the java looks like:
java.png
java.png (46.37 KiB) Viewed 13701 times
And all you need is \begin{minted}{xml} ... \end{minted} or \begin{minted}{java} ... \end{minted}. See the documentation for customization if you wish.
piotrekde
Posts: 3
Joined: Tue Jul 06, 2010 2:59 pm

Listing XML and Java source codes

Post by piotrekde »

It works, thank you very much!

I've added some extra lines to lstlang1.sty:

Code: Select all

\lst@definelanguage{XML}%
  {keywords={,CDATA,DOCTYPE,ATTLIST,termdef,ELEMENT,EMPTY,ANY,ID,%
      IDREF,IDREFS,ENTITY,ENTITIES,NMTOKEN,NMTOKENS,NOTATION,%
      INCLUDE,IGNORE,SYSTEM,PUBLIC,NDATA,PUBLIC,%
      PCDATA,REQUIRED,IMPLIED,FIXED,%%% preceded by #
      xml,xml:space,xml:lang,version,standalone,default,preserve,
      %% Added by me:
      encoding, xmlns, xmlns:xsi, xsi:schemaLocation,%
      id, class, name, local},%
...and now those added keywords are colored as others.

But I have one additional question about it.
"encoding", "xmlns" and so on are XML keywords and they should be the same color as "xml", "xml:space". But I've got also other special strings in my XML such as "beans", "bean", "property" which are Springsource specific (not XML). Is there any way to add them somewhere, and set their color to some specific (other than XML tags)?
That seems like a lot of work. How you considered trying something like the minted or texments packages?
Unfortunately :) Thank for advice, I'll try it.
Post Reply